2 This file is part of GNUnet.
3 Copyright (C) 2010-2016, 2018, 2019 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file transport/gnunet-service-tng.c
22 * @brief main for gnunet-service-tng
23 * @author Christian Grothoff
26 * - figure out how to transmit (selective) ACKs in case of uni-directional
27 * communicators (with/without core? DV-only?) When do we use ACKs?
28 * => communicators use selective ACKs for flow control
29 * => transport uses message-level ACKs for RTT, fragment confirmation
30 * => integrate DV into transport, use neither core nor communicators
31 * but rather give communicators transport-encapsulated messages
32 * (which could be core-data, background-channel traffic, or
33 * transport-to-transport traffic)
36 * - route_message() implementation, including using DV data structures
37 * (but not when routing certain message types, like DV learn,
38 * looks like now like we need two flags (DV/no-DV, confirmed-only,
40 * + NOTE: do NOT use PendingMessage for route_message(), as that is
41 * for fragmentation/reliability and ultimately core flow control!
42 * => route_message() should pick the queue
43 * => in case of DV routing, route_message should BOX the message, too.
44 * - We currently do NEVER tell CORE also about DV-connections (core_visible
45 * of `struct DistanceVector` is simply never set!)
46 * + When? Easy if we initiated the DV and got the challenge; do that NOW
47 * BUT what we passively learned DV (unconfirmed freshness)
48 * => Do we trigger Challenge->Response there as well, or 'wait' for
49 * our own DV initiations to discover?
50 * => What about DV routes that expire? Do we also only count on
51 * our own DV initiations for maintenance here, or do we
52 * try to specifically re-confirm the existence of a particular path?
53 * => OPITMIZATION-FIXME!
54 * + Where do we track what we told core? Careful: need to check
55 * the "core_visible' flag in both neighbours and DV before
56 * sending out notifications to CORE!
57 * - retransmission logic
58 * - track RTT, distance, loss, etc. => requires extra data structures!
61 * - change transport-core API to provide proper flow control in both
62 * directions, allow multiple messages per peer simultaneously (tag
63 * confirmations with unique message ID), and replace quota-out with
64 * proper flow control;
65 * - if messages are below MTU, consider adding ACKs and other stuff
66 * (requires planning at receiver, and additional MST-style demultiplex
68 * - could avoid copying body of message into each fragment and keep
69 * fragments as just pointers into the original message and only
70 * fully build fragments just before transmission (optimization, should
71 * reduce CPU and memory use)
73 * FIXME (without marks in the code!):
74 * - proper use/initialization of timestamps in messages exchanged
76 * - persistence of monotonic time obtained from other peers
77 * in PEERSTORE (by message type)
80 * - use shorthashmap on msg_uuid's when matching reliability/fragment ACKs
81 * against our pending message queue (requires additional per neighbour
82 * hash map to be maintained, avoids possible linear scan on pending msgs)
83 * - queue_send_msg and route_message both by API design have to make copies
84 * of the payload, and route_message on top of that requires a malloc/free.
85 * Change design to approximate "zero" copy better...
87 * Design realizations / discussion:
88 * - communicators do flow control by calling MQ "notify sent"
89 * when 'ready'. They determine flow implicitly (i.e. TCP blocking)
90 * or explicitly via backchannel FC ACKs. As long as the
91 * channel is not full, they may 'notify sent' even if the other
92 * peer has not yet confirmed receipt. The other peer confirming
93 * is _only_ for FC, not for more reliable transmission; reliable
94 * transmission (i.e. of fragments) is left to _transport_.
95 * - ACKs sent back in uni-directional communicators are done via
96 * the background channel API; here transport _may_ initially
97 * broadcast (with bounded # hops) if no path is known;
98 * - transport should _integrate_ DV-routing and build a view of
99 * the network; then background channel traffic can be
100 * routed via DV as well as explicit "DV" traffic.
101 * - background channel is also used for ACKs and NAT traversal support
102 * - transport service is responsible for AEAD'ing the background
103 * channel, timestamps and monotonic time are used against replay
104 * of old messages -> peerstore needs to be supplied with
105 * "latest timestamps seen" data
106 * - if transport implements DV, we likely need a 3rd peermap
107 * in addition to ephemerals and (direct) neighbours
108 * ==> check if stuff needs to be moved out of "Neighbour"
109 * - transport should encapsualte core-level messages and do its
110 * own ACKing for RTT/goodput/loss measurements _and_ fragment
113 #include "platform.h"
114 #include "gnunet_util_lib.h"
115 #include "gnunet_statistics_service.h"
116 #include "gnunet_transport_monitor_service.h"
117 #include "gnunet_peerstore_service.h"
118 #include "gnunet_hello_lib.h"
119 #include "gnunet_signatures.h"
120 #include "transport.h"
124 * What is the size we assume for a read operation in the
125 * absence of an MTU for the purpose of flow control?
127 #define IN_PACKET_SIZE_WITHOUT_MTU 128
130 * Minimum number of hops we should forward DV learn messages
131 * even if they are NOT useful for us in hope of looping
132 * back to the initiator?
134 * FIXME: allow initiator some control here instead?
136 #define MIN_DV_PATH_LENGTH_FOR_INITIATOR 3
139 * Maximum DV distance allowed ever.
141 #define MAX_DV_HOPS_ALLOWED 16
144 * Maximum number of DV learning activities we may
145 * have pending at the same time.
147 #define MAX_DV_LEARN_PENDING 64
150 * Maximum number of DV paths we keep simultaneously to the same target.
152 #define MAX_DV_PATHS_TO_TARGET 3
155 * If a queue delays the next message by more than this number
156 * of seconds we log a warning. Note: this is for testing,
157 * the value chosen here might be too aggressively low!
159 #define DELAY_WARN_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
162 * We only consider queues as "quality" connections when
163 * suppressing the generation of DV initiation messages if
164 * the latency of the queue is below this threshold.
166 #define DV_QUALITY_RTT_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
169 * How long do we consider a DV path valid if we see no
170 * further updates on it? Note: the value chosen here might be too low!
172 #define DV_PATH_VALIDITY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
175 * How long before paths expire would we like to (re)discover DV paths? Should
176 * be below #DV_PATH_VALIDITY_TIMEOUT.
178 #define DV_PATH_DISCOVERY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 4)
181 * How long are ephemeral keys valid?
183 #define EPHEMERAL_VALIDITY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
186 * How long do we keep partially reassembled messages around before giving up?
188 #define REASSEMBLY_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 4)
191 * What is the fastest rate at which we send challenges *if* we keep learning
192 * an address (gossip, DHT, etc.)?
194 #define FAST_VALIDATION_CHALLENGE_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
197 * What is the slowest rate at which we send challenges?
199 #define MAX_VALIDATION_CHALLENGE_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_DAYS, 1)
202 * What is the non-randomized base frequency at which we
203 * would initiate DV learn messages?
205 #define DV_LEARN_BASE_FREQUENCY GNUNET_TIME_UNIT_MINUTES
208 * How many good connections (confirmed, bi-directional, not DV)
209 * do we need to have to suppress initiating DV learn messages?
211 #define DV_LEARN_QUALITY_THRESHOLD 100
214 * When do we forget an invalid address for sure?
216 #define MAX_ADDRESS_VALID_UNTIL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MONTHS, 1)
218 * How long do we consider an address valid if we just checked?
220 #define ADDRESS_VALIDATION_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
223 * What is the maximum frequency at which we do address validation?
224 * A random value between 0 and this value is added when scheduling
225 * the #validation_task (both to ensure we do not validate too often,
226 * and to randomize a bit).
228 #define MIN_DELAY_ADDRESS_VALIDATION GNUNET_TIME_UNIT_MILLISECONDS
231 * How many network RTTs before an address validation expires should we begin
232 * trying to revalidate? (Note that the RTT used here is the one that we
233 * experienced during the last validation, not necessarily the latest RTT
236 #define VALIDATION_RTT_BUFFER_FACTOR 3
239 * How many messages can we have pending for a given communicator
240 * process before we start to throttle that communicator?
242 * Used if a communicator might be CPU-bound and cannot handle the traffic.
244 #define COMMUNICATOR_TOTAL_QUEUE_LIMIT 512
247 * How many messages can we have pending for a given queue (queue to
248 * a particular peer via a communicator) process before we start to
249 * throttle that queue?
251 #define QUEUE_LENGTH_LIMIT 32
254 GNUNET_NETWORK_STRUCT_BEGIN
257 * Outer layer of an encapsulated backchannel message.
259 struct TransportBackchannelEncapsulationMessage
262 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_BACKCHANNEL_ENCAPSULATION.
264 struct GNUNET_MessageHeader header;
267 * Distance the backchannel message has traveled, to be updated at
268 * each hop. Used to bound the number of hops in case a backchannel
269 * message is broadcast and thus travels without routing
270 * information (during initial backchannel discovery).
275 * Target's peer identity (as backchannels may be transmitted
276 * indirectly, or even be broadcast).
278 struct GNUNET_PeerIdentity target;
281 * Ephemeral key setup by the sender for @e target, used
282 * to encrypt the payload.
284 struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
287 * We use an IV here as the @e ephemeral_key is re-used for
288 * #EPHEMERAL_VALIDITY time to avoid re-signing it all the time.
290 struct GNUNET_ShortHashCode iv;
293 * HMAC over the ciphertext of the encrypted, variable-size
294 * body that follows. Verified via DH of @e target and
297 struct GNUNET_HashCode hmac;
299 /* Followed by encrypted, variable-size payload */
304 * Body by which a peer confirms that it is using an ephemeral key.
306 struct EphemeralConfirmation
310 * Purpose is #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_EPHEMERAL
312 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
315 * How long is this signature over the ephemeral key valid?
317 * Note that the receiver MUST IGNORE the absolute time, and only interpret
318 * the value as a mononic time and reject "older" values than the last one
319 * observed. This is necessary as we do not want to require synchronized
320 * clocks and may not have a bidirectional communication channel.
322 * Even with this, there is no real guarantee against replay achieved here,
323 * unless the latest timestamp is persisted. While persistence should be
324 * provided via PEERSTORE, we do not consider the mechanism reliable! Thus,
325 * communicators must protect against replay attacks when using backchannel
328 struct GNUNET_TIME_AbsoluteNBO ephemeral_validity;
331 * Target's peer identity.
333 struct GNUNET_PeerIdentity target;
336 * Ephemeral key setup by the sender for @e target, used
337 * to encrypt the payload.
339 struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
345 * Plaintext of the variable-size payload that is encrypted
346 * within a `struct TransportBackchannelEncapsulationMessage`
348 struct TransportBackchannelRequestPayload
352 * Sender's peer identity.
354 struct GNUNET_PeerIdentity sender;
357 * Signature of the sender over an
358 * #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_EPHEMERAL.
360 struct GNUNET_CRYPTO_EddsaSignature sender_sig;
363 * How long is this signature over the ephemeral key valid?
365 * Note that the receiver MUST IGNORE the absolute time, and only interpret
366 * the value as a mononic time and reject "older" values than the last one
367 * observed. This is necessary as we do not want to require synchronized
368 * clocks and may not have a bidirectional communication channel.
370 * Even with this, there is no real guarantee against replay achieved here,
371 * unless the latest timestamp is persisted. While persistence should be
372 * provided via PEERSTORE, we do not consider the mechanism reliable! Thus,
373 * communicators must protect against replay attacks when using backchannel
376 struct GNUNET_TIME_AbsoluteNBO ephemeral_validity;
379 * Current monotonic time of the sending transport service. Used to
380 * detect replayed messages. Note that the receiver should remember
381 * a list of the recently seen timestamps and only reject messages
382 * if the timestamp is in the list, or the list is "full" and the
383 * timestamp is smaller than the lowest in the list.
385 * Like the @e ephemeral_validity, the list of timestamps per peer should be
386 * persisted to guard against replays after restarts.
388 struct GNUNET_TIME_AbsoluteNBO monotonic_time;
390 /* Followed by a `struct GNUNET_MessageHeader` with a message
391 for a communicator */
393 /* Followed by a 0-termianted string specifying the name of
394 the communicator which is to receive the message */
400 * Outer layer of an encapsulated unfragmented application message sent
401 * over an unreliable channel.
403 struct TransportReliabilityBox
406 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_RELIABILITY_BOX
408 struct GNUNET_MessageHeader header;
411 * Number of messages still to be sent before a commulative
412 * ACK is requested. Zero if an ACK is requested immediately.
413 * In NBO. Note that the receiver may send the ACK faster
414 * if it believes that is reasonable.
416 uint32_t ack_countdown GNUNET_PACKED;
419 * Unique ID of the message used for signalling receipt of
420 * messages sent over possibly unreliable channels. Should
423 struct GNUNET_ShortHashCode msg_uuid;
428 * Confirmation that the receiver got a
429 * #GNUNET_MESSAGE_TYPE_TRANSPORT_RELIABILITY_BOX. Note that the
430 * confirmation may be transmitted over a completely different queue,
431 * so ACKs are identified by a combination of PID of sender and
432 * message UUID, without the queue playing any role!
434 struct TransportReliabilityAckMessage
437 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_RELIABILITY_ACK
439 struct GNUNET_MessageHeader header;
444 uint32_t reserved GNUNET_PACKED;
447 * How long was the ACK delayed relative to the average time of
448 * receipt of the messages being acknowledged? Used to calculate
449 * the average RTT by taking the receipt time of the ack minus the
450 * average transmission time of the sender minus this value.
452 struct GNUNET_TIME_RelativeNBO avg_ack_delay;
454 /* followed by any number of `struct GNUNET_ShortHashCode`
455 messages providing ACKs */
460 * Outer layer of an encapsulated fragmented application message.
462 struct TransportFragmentBox
465 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_FRAGMENT
467 struct GNUNET_MessageHeader header;
470 * Unique ID of this fragment (and fragment transmission!). Will
471 * change even if a fragement is retransmitted to make each
472 * transmission attempt unique! Should be incremented by one for
473 * each fragment transmission. If a client receives a duplicate
474 * fragment (same @e frag_off), it must send
475 * #GNUNET_MESSAGE_TYPE_TRANSPORT_FRAGMENT_ACK immediately.
477 uint32_t frag_uuid GNUNET_PACKED;
480 * Original message ID for of the message that all the1
481 * fragments belong to. Must be the same for all fragments.
483 struct GNUNET_ShortHashCode msg_uuid;
486 * Offset of this fragment in the overall message.
488 uint16_t frag_off GNUNET_PACKED;
491 * Total size of the message that is being fragmented.
493 uint16_t msg_size GNUNET_PACKED;
499 * Outer layer of an fragmented application message sent over a queue
500 * with finite MTU. When a #GNUNET_MESSAGE_TYPE_TRANSPORT_FRAGMENT is
501 * received, the receiver has two RTTs or 64 further fragments with
502 * the same basic message time to send an acknowledgement, possibly
503 * acknowledging up to 65 fragments in one ACK. ACKs must also be
504 * sent immediately once all fragments were sent.
506 struct TransportFragmentAckMessage
509 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_FRAGMENT_ACK
511 struct GNUNET_MessageHeader header;
514 * Unique ID of the lowest fragment UUID being acknowledged.
516 uint32_t frag_uuid GNUNET_PACKED;
519 * Bitfield of up to 64 additional fragments following the
520 * @e msg_uuid being acknowledged by this message.
522 uint64_t extra_acks GNUNET_PACKED;
525 * Original message ID for of the message that all the
526 * fragments belong to.
528 struct GNUNET_ShortHashCode msg_uuid;
531 * How long was the ACK delayed relative to the average time of
532 * receipt of the fragments being acknowledged? Used to calculate
533 * the average RTT by taking the receipt time of the ack minus the
534 * average transmission time of the sender minus this value.
536 struct GNUNET_TIME_RelativeNBO avg_ack_delay;
539 * How long until the receiver will stop trying reassembly
542 struct GNUNET_TIME_RelativeNBO reassembly_timeout;
547 * Content signed by the initator during DV learning.
549 * The signature is required to prevent DDoS attacks. A peer sending out this
550 * message is potentially generating a lot of traffic that will go back to the
551 * initator, as peers receiving this message will try to let the initiator
552 * know that they got the message.
554 * Without this signature, an attacker could abuse this mechanism for traffic
555 * amplification, sending a lot of traffic to a peer by putting out this type
556 * of message with the victim's peer identity.
558 * Even with just a signature, traffic amplification would be possible via
559 * replay attacks. The @e monotonic_time limits such replay attacks, as every
560 * potential amplificator will check the @e monotonic_time and only respond
561 * (at most) once per message.
566 * Purpose is #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DV_INITIATOR
568 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
571 * Time at the initiator when generating the signature.
573 * Note that the receiver MUST IGNORE the absolute time, and only interpret
574 * the value as a mononic time and reject "older" values than the last one
575 * observed. This is necessary as we do not want to require synchronized
576 * clocks and may not have a bidirectional communication channel.
578 * Even with this, there is no real guarantee against replay achieved here,
579 * unless the latest timestamp is persisted. Persistence should be
580 * provided via PEERSTORE if possible.
582 struct GNUNET_TIME_AbsoluteNBO monotonic_time;
585 * Challenge value used by the initiator to re-identify the path.
587 struct GNUNET_ShortHashCode challenge;
593 * Content signed by each peer during DV learning.
595 * This assues the initiator of the DV learning operation that the hop from @e
596 * pred via the signing peer to @e succ actually exists. This makes it
597 * impossible for an adversary to supply the network with bogus routes.
599 * The @e challenge is included to provide replay protection for the
600 * initiator. This way, the initiator knows that the hop existed after the
601 * original @e challenge was first transmitted, providing a freshness metric.
603 * Peers other than the initiator that passively learn paths by observing
604 * these messages do NOT benefit from this. Here, an adversary may indeed
605 * replay old messages. Thus, passively learned paths should always be
606 * immediately marked as "potentially stale".
611 * Purpose is #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DV_HOP
613 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
616 * Identity of the previous peer on the path.
618 struct GNUNET_PeerIdentity pred;
621 * Identity of the next peer on the path.
623 struct GNUNET_PeerIdentity succ;
626 * Challenge value used by the initiator to re-identify the path.
628 struct GNUNET_ShortHashCode challenge;
634 * An entry describing a peer on a path in a
635 * `struct TransportDVLearn` message.
640 * Identity of a peer on the path.
642 struct GNUNET_PeerIdentity hop;
645 * Signature of this hop over the path, of purpose
646 * #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DV_HOP
648 struct GNUNET_CRYPTO_EddsaSignature hop_sig;
654 * Internal message used by transport for distance vector learning.
655 * If @e num_hops does not exceed the threshold, peers should append
656 * themselves to the peer list and flood the message (possibly only
657 * to a subset of their neighbours to limit discoverability of the
658 * network topology). To the extend that the @e bidirectional bits
659 * are set, peers may learn the inverse paths even if they did not
662 * Unless received on a bidirectional queue and @e num_hops just
663 * zero, peers that can forward to the initator should always try to
664 * forward to the initiator.
666 struct TransportDVLearn
669 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_DV_LEARN
671 struct GNUNET_MessageHeader header;
674 * Number of hops this messages has travelled, in NBO. Zero if
677 uint16_t num_hops GNUNET_PACKED;
680 * Bitmask of the last 16 hops indicating whether they are confirmed
681 * available (without DV) in both directions or not, in NBO. Used
682 * to possibly instantly learn a path in both directions. Each peer
683 * should shift this value by one to the left, and then set the
684 * lowest bit IF the current sender can be reached from it (without
687 uint16_t bidirectional GNUNET_PACKED;
690 * Peers receiving this message and delaying forwarding to other
691 * peers for any reason should increment this value by the non-network
692 * delay created by the peer.
694 struct GNUNET_TIME_RelativeNBO non_network_delay;
697 * Signature of this hop over the path, of purpose
698 * #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DV_INITIATOR
700 struct GNUNET_CRYPTO_EddsaSignature init_sig;
703 * Identity of the peer that started this learning activity.
705 struct GNUNET_PeerIdentity initiator;
708 * Challenge value used by the initiator to re-identify the path.
710 struct GNUNET_ShortHashCode challenge;
712 /* Followed by @e num_hops `struct DVPathEntryP` values,
713 excluding the initiator of the DV trace; the last entry is the
714 current sender; the current peer must not be included. */
720 * Outer layer of an encapsulated message send over multiple hops.
721 * The path given only includes the identities of the subsequent
722 * peers, i.e. it will be empty if we are the receiver. Each
723 * forwarding peer should scan the list from the end, and if it can,
724 * forward to the respective peer. The list should then be shortened
725 * by all the entries up to and including that peer. Each hop should
726 * also increment @e total_hops to allow the receiver to get a precise
727 * estimate on the number of hops the message travelled. Senders must
728 * provide a learned path that thus should work, but intermediaries
729 * know of a shortcut, they are allowed to send the message via that
732 * If a peer finds itself still on the list, it must drop the message.
734 struct TransportDVBox
737 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_DV_BOX
739 struct GNUNET_MessageHeader header;
742 * Number of total hops this messages travelled. In NBO.
743 * @e origin sets this to zero, to be incremented at
746 uint16_t total_hops GNUNET_PACKED;
749 * Number of hops this messages includes. In NBO.
751 uint16_t num_hops GNUNET_PACKED;
754 * Identity of the peer that originated the message.
756 struct GNUNET_PeerIdentity origin;
758 /* Followed by @e num_hops `struct GNUNET_PeerIdentity` values;
759 excluding the @e origin and the current peer, the last must be
760 the ultimate target; if @e num_hops is zero, the receiver of this
761 message is the ultimate target. */
763 /* Followed by the actual message, which itself may be
764 another box, but not a DV_LEARN or DV_BOX message! */
769 * Message send to another peer to validate that it can indeed
770 * receive messages at a particular address.
772 struct TransportValidationChallenge
776 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_VALIDATION_CHALLENGE
778 struct GNUNET_MessageHeader header;
783 uint32_t reserved GNUNET_PACKED;
786 * Challenge to be signed by the receiving peer.
788 struct GNUNET_ShortHashCode challenge;
791 * Timestamp of the sender, to be copied into the reply
792 * to allow sender to calculate RTT.
794 struct GNUNET_TIME_AbsoluteNBO sender_time;
799 * Message signed by a peer to confirm that it can indeed
800 * receive messages at a particular address.
802 struct TransportValidationPS
806 * Purpose is #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_CHALLENGE
808 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
811 * How long does the sender believe the address on
812 * which the challenge was received to remain valid?
814 struct GNUNET_TIME_RelativeNBO validity_duration;
817 * Challenge signed by the receiving peer.
819 struct GNUNET_ShortHashCode challenge;
825 * Message send to a peer to respond to a
826 * #GNUNET_MESSAGE_TYPE_ADDRESS_VALIDATION_CHALLENGE
828 struct TransportValidationResponse
832 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_VALIDATION_RESPONSE
834 struct GNUNET_MessageHeader header;
839 uint32_t reserved GNUNET_PACKED;
842 * The peer's signature matching the
843 * #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_CHALLENGE purpose.
845 struct GNUNET_CRYPTO_EddsaSignature signature;
848 * The challenge that was signed by the receiving peer.
850 struct GNUNET_ShortHashCode challenge;
853 * Original timestamp of the sender (was @code{sender_time}),
854 * copied into the reply to allow sender to calculate RTT.
856 struct GNUNET_TIME_AbsoluteNBO origin_time;
859 * How long does the sender believe this address to remain
862 struct GNUNET_TIME_RelativeNBO validity_duration;
867 GNUNET_NETWORK_STRUCT_END
871 * What type of client is the `struct TransportClient` about?
876 * We do not know yet (client is fresh).
881 * Is the CORE service, we need to forward traffic to it.
886 * It is a monitor, forward monitor data.
891 * It is a communicator, use for communication.
896 * "Application" telling us where to connect (i.e. TOPOLOGY, DHT or CADET).
903 * When did we launch this DV learning activity?
905 struct LearnLaunchEntry
909 * Kept (also) in a DLL sorted by launch time.
911 struct LearnLaunchEntry *prev;
914 * Kept (also) in a DLL sorted by launch time.
916 struct LearnLaunchEntry *next;
919 * Challenge that uniquely identifies this activity.
921 struct GNUNET_ShortHashCode challenge;
924 * When did we transmit the DV learn message (used to calculate RTT) and
925 * determine freshness of paths learned via this operation.
927 struct GNUNET_TIME_Absolute launch_time;
933 * Entry in our cache of ephemeral keys we currently use. This way, we only
934 * sign an ephemeral once per @e target, and then can re-use it over multiple
935 * #GNUNET_MESSAGE_TYPE_TRANSPORT_BACKCHANNEL_ENCAPSULATION messages (as
936 * signing is expensive and in some cases we may use backchannel messages a
939 struct EphemeralCacheEntry
943 * Target's peer identity (we don't re-use ephemerals
944 * to limit linkability of messages).
946 struct GNUNET_PeerIdentity target;
949 * Signature affirming @e ephemeral_key of type
950 * #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_EPHEMERAL
952 struct GNUNET_CRYPTO_EddsaSignature sender_sig;
955 * How long is @e sender_sig valid
957 struct GNUNET_TIME_Absolute ephemeral_validity;
962 struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
965 * Our private ephemeral key.
967 struct GNUNET_CRYPTO_EcdhePrivateKey private_key;
970 * Node in the ephemeral cache for this entry.
971 * Used for expiration.
973 struct GNUNET_CONTAINER_HeapNode *hn;
978 * Client connected to the transport service.
980 struct TransportClient;
984 * A neighbour that at least one communicator is connected to.
990 * Entry in our #dv_routes table, representing a (set of) distance
991 * vector routes to a particular peer.
993 struct DistanceVector;
996 * One possible hop towards a DV target.
998 struct DistanceVectorHop
1002 * Kept in a MDLL, sorted by @e timeout.
1004 struct DistanceVectorHop *next_dv;
1007 * Kept in a MDLL, sorted by @e timeout.
1009 struct DistanceVectorHop *prev_dv;
1014 struct DistanceVectorHop *next_neighbour;
1019 struct DistanceVectorHop *prev_neighbour;
1022 * What would be the next hop to @e target?
1024 struct Neighbour *next_hop;
1027 * Distance vector entry this hop belongs with.
1029 struct DistanceVector *dv;
1032 * Array of @e distance hops to the target, excluding @e next_hop.
1033 * NULL if the entire path is us to @e next_hop to `target`. Allocated
1034 * at the end of this struct.
1036 const struct GNUNET_PeerIdentity *path;
1039 * At what time do we forget about this path unless we see it again
1042 struct GNUNET_TIME_Absolute timeout;
1045 * After what time do we know for sure that the path must have existed?
1046 * Set to ZERO if the path is learned by snooping on DV learn messages
1047 * initiated by other peers, and to the time at which we generated the
1048 * challenge for DV learn operations this peer initiated.
1050 struct GNUNET_TIME_Absolute freshness;
1053 * How many hops in total to the `target` (excluding @e next_hop and `target` itself),
1054 * thus 0 still means a distance of 2 hops (to @e next_hop and then to `target`)?
1056 unsigned int distance;
1061 * Entry in our #dv_routes table, representing a (set of) distance
1062 * vector routes to a particular peer.
1064 struct DistanceVector
1068 * To which peer is this a route?
1070 struct GNUNET_PeerIdentity target;
1073 * Known paths to @e target.
1075 struct DistanceVectorHop *dv_head;
1078 * Known paths to @e target.
1080 struct DistanceVectorHop *dv_tail;
1083 * Task scheduled to purge expired paths from @e dv_head MDLL.
1085 struct GNUNET_SCHEDULER_Task *timeout_task;
1088 * Is one of the DV paths in this struct 'confirmed' and thus
1089 * the cause for CORE to see this peer as connected? (Note that
1090 * the same may apply to a `struct Neighbour` at the same time.)
1097 * A queue is a message queue provided by a communicator
1098 * via which we can reach a particular neighbour.
1104 * Entry identifying transmission in one of our `struct
1105 * Queue` which still awaits an ACK. This is used to
1106 * ensure we do not overwhelm a communicator and limit the number of
1107 * messages outstanding per communicator (say in case communicator is
1108 * CPU bound) and per queue (in case bandwidth allocation exceeds
1109 * what the communicator can actually provide towards a particular
1118 struct QueueEntry *next;
1123 struct QueueEntry *prev;
1126 * Queue this entry is queued with.
1128 struct Queue *queue;
1131 * Message ID used for this message with the queue used for transmission.
1138 * A queue is a message queue provided by a communicator
1139 * via which we can reach a particular neighbour.
1146 struct Queue *next_neighbour;
1151 struct Queue *prev_neighbour;
1156 struct Queue *prev_client;
1161 struct Queue *next_client;
1164 * Head of DLL of unacked transmission requests.
1166 struct QueueEntry *queue_head;
1169 * End of DLL of unacked transmission requests.
1171 struct QueueEntry *queue_tail;
1174 * Which neighbour is this queue for?
1176 struct Neighbour *neighbour;
1179 * Which communicator offers this queue?
1181 struct TransportClient *tc;
1184 * Address served by the queue.
1186 const char *address;
1189 * Task scheduled for the time when this queue can (likely) transmit the
1190 * next message. Still needs to check with the @e tracker_out to be sure.
1192 struct GNUNET_SCHEDULER_Task *transmit_task;
1195 * Task scheduled to possibly notfiy core that this queue is no longer
1196 * counting as confirmed. Runs the #core_queue_visibility_check().
1198 struct GNUNET_SCHEDULER_Task *visibility_task;
1201 * Our current RTT estimate for this queue.
1203 struct GNUNET_TIME_Relative rtt;
1206 * How long do *we* consider this @e address to be valid? In the past or
1207 * zero if we have not yet validated it. Can be updated based on
1208 * challenge-response validations (via address validation logic), or when we
1209 * receive ACKs that we can definitively map to transmissions via this
1212 struct GNUNET_TIME_Absolute validated_until;
1215 * Message ID generator for transmissions on this queue.
1220 * Unique identifier of this queue with the communicator.
1225 * Maximum transmission unit supported by this queue.
1230 * Distance to the target of this queue.
1231 * FIXME: needed? DV is done differently these days...
1238 uint32_t num_msg_pending;
1243 uint32_t num_bytes_pending;
1246 * Length of the DLL starting at @e queue_head.
1248 unsigned int queue_length;
1251 * Network type offered by this queue.
1253 enum GNUNET_NetworkType nt;
1256 * Connection status for this queue.
1258 enum GNUNET_TRANSPORT_ConnectionStatus cs;
1261 * How much outbound bandwidth do we have available for this queue?
1263 struct GNUNET_BANDWIDTH_Tracker tracker_out;
1266 * How much inbound bandwidth do we have available for this queue?
1268 struct GNUNET_BANDWIDTH_Tracker tracker_in;
1273 * Information we keep for a message that we are reassembling.
1275 struct ReassemblyContext
1279 * Original message ID for of the message that all the
1280 * fragments belong to.
1282 struct GNUNET_ShortHashCode msg_uuid;
1285 * Which neighbour is this context for?
1287 struct Neighbour *neighbour;
1290 * Entry in the reassembly heap (sorted by expiration).
1292 struct GNUNET_CONTAINER_HeapNode *hn;
1295 * Bitfield with @e msg_size bits representing the positions
1296 * where we have received fragments. When we receive a fragment,
1297 * we check the bits in @e bitfield before incrementing @e msg_missing.
1299 * Allocated after the reassembled message.
1304 * Task for sending ACK. We may send ACKs either because of hitting
1305 * the @e extra_acks limit, or based on time and @e num_acks. This
1306 * task is for the latter case.
1308 struct GNUNET_SCHEDULER_Task *ack_task;
1311 * At what time will we give up reassembly of this message?
1313 struct GNUNET_TIME_Absolute reassembly_timeout;
1316 * Average delay of all acks in @e extra_acks and @e frag_uuid.
1317 * Should be reset to zero when @e num_acks is set to 0.
1319 struct GNUNET_TIME_Relative avg_ack_delay;
1322 * Time we received the last fragment. @e avg_ack_delay must be
1323 * incremented by now - @e last_frag multiplied by @e num_acks.
1325 struct GNUNET_TIME_Absolute last_frag;
1328 * Bitfield of up to 64 additional fragments following @e frag_uuid
1329 * to be acknowledged in the next cummulative ACK.
1331 uint64_t extra_acks;
1334 * Unique ID of the lowest fragment UUID to be acknowledged in the
1335 * next cummulative ACK. Only valid if @e num_acks > 0.
1340 * Number of ACKs we have accumulated so far. Reset to 0
1341 * whenever we send a #GNUNET_MESSAGE_TYPE_TRANSPORT_FRAGMENT_ACK.
1343 unsigned int num_acks;
1346 * How big is the message we are reassembling in total?
1351 * How many bytes of the message are still missing? Defragmentation
1352 * is complete when @e msg_missing == 0.
1354 uint16_t msg_missing;
1356 /* Followed by @e msg_size bytes of the (partially) defragmented original message */
1358 /* Followed by @e bitfield data */
1363 * A neighbour that at least one communicator is connected to.
1369 * Which peer is this about?
1371 struct GNUNET_PeerIdentity pid;
1374 * Map with `struct ReassemblyContext` structs for fragments under
1375 * reassembly. May be NULL if we currently have no fragments from
1376 * this @e pid (lazy initialization).
1378 struct GNUNET_CONTAINER_MultiShortmap *reassembly_map;
1381 * Heap with `struct ReassemblyContext` structs for fragments under
1382 * reassembly. May be NULL if we currently have no fragments from
1383 * this @e pid (lazy initialization).
1385 struct GNUNET_CONTAINER_Heap *reassembly_heap;
1388 * Task to free old entries from the @e reassembly_heap and @e reassembly_map.
1390 struct GNUNET_SCHEDULER_Task *reassembly_timeout_task;
1393 * Head of list of messages pending for this neighbour.
1395 struct PendingMessage *pending_msg_head;
1398 * Tail of list of messages pending for this neighbour.
1400 struct PendingMessage *pending_msg_tail;
1403 * Head of MDLL of DV hops that have this neighbour as next hop. Must be
1404 * purged if this neighbour goes down.
1406 struct DistanceVectorHop *dv_head;
1409 * Tail of MDLL of DV hops that have this neighbour as next hop. Must be
1410 * purged if this neighbour goes down.
1412 struct DistanceVectorHop *dv_tail;
1415 * Head of DLL of queues to this peer.
1417 struct Queue *queue_head;
1420 * Tail of DLL of queues to this peer.
1422 struct Queue *queue_tail;
1425 * Task run to cleanup pending messages that have exceeded their timeout.
1427 struct GNUNET_SCHEDULER_Task *timeout_task;
1430 * Quota at which CORE is allowed to transmit to this peer.
1432 * FIXME: not yet used, tricky to get right given multiple queues!
1433 * (=> Idea: measure???)
1434 * FIXME: how do we set this value initially when we tell CORE?
1435 * Options: start at a minimum value or at literally zero?
1436 * (=> Current thought: clean would be zero!)
1438 struct GNUNET_BANDWIDTH_Value32NBO quota_out;
1441 * What is the earliest timeout of any message in @e pending_msg_tail?
1443 struct GNUNET_TIME_Absolute earliest_timeout;
1446 * Do we have a confirmed working queue and are thus visible to
1454 * A peer that an application (client) would like us to talk to directly.
1460 * Which peer is this about?
1462 struct GNUNET_PeerIdentity pid;
1465 * Client responsible for the request.
1467 struct TransportClient *tc;
1470 * Handle for watching the peerstore for HELLOs for this peer.
1472 struct GNUNET_PEERSTORE_WatchContext *wc;
1475 * What kind of performance preference does this @e tc have?
1477 enum GNUNET_MQ_PreferenceKind pk;
1480 * How much bandwidth would this @e tc like to see?
1482 struct GNUNET_BANDWIDTH_Value32NBO bw;
1488 * Types of different pending messages.
1490 enum PendingMessageType
1494 * Ordinary message received from the CORE service.
1501 PMT_FRAGMENT_BOX = 1,
1506 PMT_RELIABILITY_BOX = 2,
1509 * Any type of acknowledgement.
1511 PMT_ACKNOWLEDGEMENT = 3,
1514 * Control traffic generated by the TRANSPORT service itself.
1522 * Transmission request that is awaiting delivery. The original
1523 * transmission requests from CORE may be too big for some queues.
1524 * In this case, a *tree* of fragments is created. At each
1525 * level of the tree, fragments are kept in a DLL ordered by which
1526 * fragment should be sent next (at the head). The tree is searched
1527 * top-down, with the original message at the root.
1529 * To select a node for transmission, first it is checked if the
1530 * current node's message fits with the MTU. If it does not, we
1531 * either calculate the next fragment (based on @e frag_off) from the
1532 * current node, or, if all fragments have already been created,
1533 * descend to the @e head_frag. Even though the node was already
1534 * fragmented, the fragment may be too big if the fragment was
1535 * generated for a queue with a larger MTU. In this case, the node
1536 * may be fragmented again, thus creating a tree.
1538 * When acknowledgements for fragments are received, the tree
1539 * must be pruned, removing those parts that were already
1540 * acknowledged. When fragments are sent over a reliable
1541 * channel, they can be immediately removed.
1543 * If a message is ever fragmented, then the original "full" message
1544 * is never again transmitted (even if it fits below the MTU), and
1545 * only (remaining) fragments are sent.
1547 struct PendingMessage
1550 * Kept in a MDLL of messages for this @a target.
1552 struct PendingMessage *next_neighbour;
1555 * Kept in a MDLL of messages for this @a target.
1557 struct PendingMessage *prev_neighbour;
1560 * Kept in a MDLL of messages from this @a client (if @e pmt is #PMT_CORE)
1562 struct PendingMessage *next_client;
1565 * Kept in a MDLL of messages from this @a client (if @e pmt is #PMT_CORE)
1567 struct PendingMessage *prev_client;
1570 * Kept in a MDLL of messages from this @a cpm (if @e pmt is #PMT_FRAGMENT_BOx)
1572 struct PendingMessage *next_frag;
1575 * Kept in a MDLL of messages from this @a cpm (if @e pmt is #PMT_FRAGMENT_BOX)
1577 struct PendingMessage *prev_frag;
1580 * This message, reliability boxed. Only possibly available if @e pmt is #PMT_CORE.
1582 struct PendingMessage *bpm;
1585 * Target of the request.
1587 struct Neighbour *target;
1590 * Client that issued the transmission request, if @e pmt is #PMT_CORE.
1592 struct TransportClient *client;
1595 * Head of a MDLL of fragments created for this core message.
1597 struct PendingMessage *head_frag;
1600 * Tail of a MDLL of fragments created for this core message.
1602 struct PendingMessage *tail_frag;
1605 * Our parent in the fragmentation tree.
1607 struct PendingMessage *frag_parent;
1610 * At what time should we give up on the transmission (and no longer retry)?
1612 struct GNUNET_TIME_Absolute timeout;
1615 * What is the earliest time for us to retry transmission of this message?
1617 struct GNUNET_TIME_Absolute next_attempt;
1620 * UUID to use for this message (used for reassembly of fragments, only
1621 * initialized if @e msg_uuid_set is #GNUNET_YES).
1623 struct GNUNET_ShortHashCode msg_uuid;
1626 * Counter incremented per generated fragment.
1628 uint32_t frag_uuidgen;
1631 * Type of the pending message.
1633 enum PendingMessageType pmt;
1636 * Size of the original message.
1641 * Offset at which we should generate the next fragment.
1646 * #GNUNET_YES once @e msg_uuid was initialized
1648 int16_t msg_uuid_set;
1650 /* Followed by @e bytes_msg to transmit */
1655 * One of the addresses of this peer.
1657 struct AddressListEntry
1663 struct AddressListEntry *next;
1668 struct AddressListEntry *prev;
1671 * Which communicator provides this address?
1673 struct TransportClient *tc;
1676 * The actual address.
1678 const char *address;
1681 * Current context for storing this address in the peerstore.
1683 struct GNUNET_PEERSTORE_StoreContext *sc;
1686 * Task to periodically do @e st operation.
1688 struct GNUNET_SCHEDULER_Task *st;
1691 * What is a typical lifetime the communicator expects this
1692 * address to have? (Always from now.)
1694 struct GNUNET_TIME_Relative expiration;
1697 * Address identifier used by the communicator.
1702 * Network type offered by this address.
1704 enum GNUNET_NetworkType nt;
1710 * Client connected to the transport service.
1712 struct TransportClient
1718 struct TransportClient *next;
1723 struct TransportClient *prev;
1726 * Handle to the client.
1728 struct GNUNET_SERVICE_Client *client;
1731 * Message queue to the client.
1733 struct GNUNET_MQ_Handle *mq;
1736 * What type of client is this?
1738 enum ClientType type;
1744 * Information for @e type #CT_CORE.
1749 * Head of list of messages pending for this client, sorted by
1750 * transmission time ("next_attempt" + possibly internal prioritization).
1752 struct PendingMessage *pending_msg_head;
1755 * Tail of list of messages pending for this client.
1757 struct PendingMessage *pending_msg_tail;
1762 * Information for @e type #CT_MONITOR.
1767 * Peer identity to monitor the addresses of.
1768 * Zero to monitor all neighbours. Valid if
1769 * @e type is #CT_MONITOR.
1771 struct GNUNET_PeerIdentity peer;
1774 * Is this a one-shot monitor?
1782 * Information for @e type #CT_COMMUNICATOR.
1786 * If @e type is #CT_COMMUNICATOR, this communicator
1787 * supports communicating using these addresses.
1789 char *address_prefix;
1792 * Head of DLL of queues offered by this communicator.
1794 struct Queue *queue_head;
1797 * Tail of DLL of queues offered by this communicator.
1799 struct Queue *queue_tail;
1802 * Head of list of the addresses of this peer offered by this communicator.
1804 struct AddressListEntry *addr_head;
1807 * Tail of list of the addresses of this peer offered by this communicator.
1809 struct AddressListEntry *addr_tail;
1812 * Number of queue entries in all queues to this communicator. Used
1813 * throttle sending to a communicator if we see that the communicator
1814 * is globally unable to keep up.
1816 unsigned int total_queue_length;
1819 * Characteristics of this communicator.
1821 enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc;
1826 * Information for @e type #CT_APPLICATION
1831 * Map of requests for peers the given client application would like to
1832 * see connections for. Maps from PIDs to `struct PeerRequest`.
1834 struct GNUNET_CONTAINER_MultiPeerMap *requests;
1844 * State we keep for validation activities. Each of these
1845 * is both in the #validation_heap and the #validation_map.
1847 struct ValidationState
1851 * For which peer is @a address to be validated (or possibly valid)?
1852 * Serves as key in the #validation_map.
1854 struct GNUNET_PeerIdentity pid;
1857 * How long did the peer claim this @e address to be valid? Capped at
1858 * minimum of #MAX_ADDRESS_VALID_UNTIL relative to the time where we last
1859 * were told about the address and the value claimed by the other peer at
1860 * that time. May be updated similarly when validation succeeds.
1862 struct GNUNET_TIME_Absolute valid_until;
1865 * How long do *we* consider this @e address to be valid?
1866 * In the past or zero if we have not yet validated it.
1868 struct GNUNET_TIME_Absolute validated_until;
1871 * When did we FIRST use the current @e challenge in a message?
1872 * Used to sanity-check @code{origin_time} in the response when
1873 * calculating the RTT. If the @code{origin_time} is not in
1874 * the expected range, the response is discarded as malicious.
1876 struct GNUNET_TIME_Absolute first_challenge_use;
1879 * When did we LAST use the current @e challenge in a message?
1880 * Used to sanity-check @code{origin_time} in the response when
1881 * calculating the RTT. If the @code{origin_time} is not in
1882 * the expected range, the response is discarded as malicious.
1884 struct GNUNET_TIME_Absolute last_challenge_use;
1887 * Next time we will send the @e challenge to the peer, if this time is past
1888 * @e valid_until, this validation state is released at this time. If the
1889 * address is valid, @e next_challenge is set to @e validated_until MINUS @e
1890 * validation_delay * #VALIDATION_RTT_BUFFER_FACTOR, such that we will try
1891 * to re-validate before the validity actually expires.
1893 struct GNUNET_TIME_Absolute next_challenge;
1896 * Current backoff factor we're applying for sending the @a challenge.
1897 * Reset to 0 if the @a challenge is confirmed upon validation.
1898 * Reduced to minimum of #FAST_VALIDATION_CHALLENGE_FREQ and half of the
1899 * existing value if we receive an unvalidated address again over
1900 * another channel (and thus should consider the information "fresh").
1901 * Maximum is #MAX_VALIDATION_CHALLENGE_FREQ.
1903 struct GNUNET_TIME_Relative challenge_backoff;
1906 * Initially set to "forever". Once @e validated_until is set, this value is
1907 * set to the RTT that tells us how long it took to receive the validation.
1909 struct GNUNET_TIME_Relative validation_rtt;
1912 * The challenge we sent to the peer to get it to validate the address. Note
1913 * that we rotate the challenge whenever we update @e validated_until to
1914 * avoid attacks where a peer simply replays an old challenge in the future.
1915 * (We must not rotate more often as otherwise we may discard valid answers
1916 * due to packet losses, latency and reorderings on the network).
1918 struct GNUNET_ShortHashCode challenge;
1921 * Claimed address of the peer.
1926 * Entry in the #validation_heap, which is sorted by @e next_challenge. The
1927 * heap is used to figure out when the next validation activity should be
1930 struct GNUNET_CONTAINER_HeapNode *hn;
1933 * Handle to a PEERSTORE store operation for this @e address. NULL if
1934 * no PEERSTORE operation is pending.
1936 struct GNUNET_PEERSTORE_StoreContext *sc;
1939 * We are technically ready to send the challenge, but we are waiting for
1940 * the respective queue to become available for transmission.
1948 * Head of linked list of all clients to this service.
1950 static struct TransportClient *clients_head;
1953 * Tail of linked list of all clients to this service.
1955 static struct TransportClient *clients_tail;
1958 * Statistics handle.
1960 static struct GNUNET_STATISTICS_Handle *GST_stats;
1963 * Configuration handle.
1965 static const struct GNUNET_CONFIGURATION_Handle *GST_cfg;
1970 static struct GNUNET_PeerIdentity GST_my_identity;
1975 static struct GNUNET_CRYPTO_EddsaPrivateKey *GST_my_private_key;
1978 * Map from PIDs to `struct Neighbour` entries. A peer is
1979 * a neighbour if we have an MQ to it from some communicator.
1981 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
1984 * Map from PIDs to `struct DistanceVector` entries describing
1985 * known paths to the peer.
1987 static struct GNUNET_CONTAINER_MultiPeerMap *dv_routes;
1990 * Map from PIDs to `struct ValidationState` entries describing
1991 * addresses we are aware of and their validity state.
1993 static struct GNUNET_CONTAINER_MultiPeerMap *validation_map;
1996 * Map from challenges to `struct LearnLaunchEntry` values.
1998 static struct GNUNET_CONTAINER_MultiShortmap *dvlearn_map;
2001 * Head of a DLL sorted by launch time.
2003 static struct LearnLaunchEntry *lle_head;
2006 * Tail of a DLL sorted by launch time.
2008 static struct LearnLaunchEntry *lle_tail;
2011 * MIN Heap sorted by "next_challenge" to `struct ValidationState` entries
2012 * sorting addresses we are aware of by when we should next try to (re)validate
2015 static struct GNUNET_CONTAINER_Heap *validation_heap;
2018 * Database for peer's HELLOs.
2020 static struct GNUNET_PEERSTORE_Handle *peerstore;
2023 * Heap sorting `struct EphemeralCacheEntry` by their
2024 * key/signature validity.
2026 static struct GNUNET_CONTAINER_Heap *ephemeral_heap;
2029 * Hash map for looking up `struct EphemeralCacheEntry`s
2030 * by peer identity. (We may have ephemerals in our
2031 * cache for which we do not have a neighbour entry,
2032 * and similar many neighbours may not need ephemerals,
2033 * so we use a second map.)
2035 static struct GNUNET_CONTAINER_MultiPeerMap *ephemeral_map;
2038 * Task to free expired ephemerals.
2040 static struct GNUNET_SCHEDULER_Task *ephemeral_task;
2043 * Task run to initiate DV learning.
2045 static struct GNUNET_SCHEDULER_Task *dvlearn_task;
2048 * Task to run address validation.
2050 static struct GNUNET_SCHEDULER_Task *validation_task;
2054 * Free cached ephemeral key.
2056 * @param ece cached signature to free
2059 free_ephemeral (struct EphemeralCacheEntry *ece)
2061 GNUNET_CONTAINER_multipeermap_remove (ephemeral_map,
2064 GNUNET_CONTAINER_heap_remove_node (ece->hn);
2070 * Free validation state.
2072 * @param vs validation state to free
2075 free_validation_state (struct ValidationState *vs)
2077 GNUNET_CONTAINER_multipeermap_remove (validation_map,
2080 GNUNET_CONTAINER_heap_remove_node (vs->hn);
2084 GNUNET_PEERSTORE_store_cancel (vs->sc);
2087 GNUNET_free (vs->address);
2093 * Lookup neighbour record for peer @a pid.
2095 * @param pid neighbour to look for
2096 * @return NULL if we do not have this peer as a neighbour
2098 static struct Neighbour *
2099 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
2101 return GNUNET_CONTAINER_multipeermap_get (neighbours,
2107 * Details about what to notify monitors about.
2112 * @deprecated To be discussed if we keep these...
2114 struct GNUNET_TIME_Absolute last_validation;
2115 struct GNUNET_TIME_Absolute valid_until;
2116 struct GNUNET_TIME_Absolute next_validation;
2119 * Current round-trip time estimate.
2121 struct GNUNET_TIME_Relative rtt;
2124 * Connection status.
2126 enum GNUNET_TRANSPORT_ConnectionStatus cs;
2131 uint32_t num_msg_pending;
2136 uint32_t num_bytes_pending;
2143 * Free a @dvh. Callers MAY want to check if this was the last path to the
2144 * `target`, and if so call #free_dv_route to also free the associated DV
2145 * entry in #dv_routes (if not, the associated scheduler job should eventually
2148 * @param dvh hop to free
2151 free_distance_vector_hop (struct DistanceVectorHop *dvh)
2153 struct Neighbour *n = dvh->next_hop;
2154 struct DistanceVector *dv = dvh->dv;
2156 GNUNET_CONTAINER_MDLL_remove (neighbour,
2160 GNUNET_CONTAINER_MDLL_remove (dv,
2169 * Free entry in #dv_routes. First frees all hops to the target, and
2170 * if there are no entries left, frees @a dv as well.
2172 * @param dv route to free
2175 free_dv_route (struct DistanceVector *dv)
2177 struct DistanceVectorHop *dvh;
2179 while (NULL != (dvh = dv->dv_head))
2180 free_distance_vector_hop (dvh);
2181 if (NULL == dv->dv_head)
2183 GNUNET_assert (GNUNET_YES ==
2184 GNUNET_CONTAINER_multipeermap_remove (dv_routes,
2187 if (NULL != dv->timeout_task)
2188 GNUNET_SCHEDULER_cancel (dv->timeout_task);
2195 * Notify monitor @a tc about an event. That @a tc
2196 * cares about the event has already been checked.
2198 * Send @a tc information in @a me about a @a peer's status with
2199 * respect to some @a address to all monitors that care.
2201 * @param tc monitor to inform
2202 * @param peer peer the information is about
2203 * @param address address the information is about
2204 * @param nt network type associated with @a address
2205 * @param me detailed information to transmit
2208 notify_monitor (struct TransportClient *tc,
2209 const struct GNUNET_PeerIdentity *peer,
2210 const char *address,
2211 enum GNUNET_NetworkType nt,
2212 const struct MonitorEvent *me)
2214 struct GNUNET_MQ_Envelope *env;
2215 struct GNUNET_TRANSPORT_MonitorData *md;
2216 size_t addr_len = strlen (address) + 1;
2218 env = GNUNET_MQ_msg_extra (md,
2220 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_DATA);
2221 md->nt = htonl ((uint32_t) nt);
2223 md->last_validation = GNUNET_TIME_absolute_hton (me->last_validation);
2224 md->valid_until = GNUNET_TIME_absolute_hton (me->valid_until);
2225 md->next_validation = GNUNET_TIME_absolute_hton (me->next_validation);
2226 md->rtt = GNUNET_TIME_relative_hton (me->rtt);
2227 md->cs = htonl ((uint32_t) me->cs);
2228 md->num_msg_pending = htonl (me->num_msg_pending);
2229 md->num_bytes_pending = htonl (me->num_bytes_pending);
2233 GNUNET_MQ_send (tc->mq,
2239 * Send information in @a me about a @a peer's status with respect
2240 * to some @a address to all monitors that care.
2242 * @param peer peer the information is about
2243 * @param address address the information is about
2244 * @param nt network type associated with @a address
2245 * @param me detailed information to transmit
2248 notify_monitors (const struct GNUNET_PeerIdentity *peer,
2249 const char *address,
2250 enum GNUNET_NetworkType nt,
2251 const struct MonitorEvent *me)
2253 for (struct TransportClient *tc = clients_head;
2257 if (CT_MONITOR != tc->type)
2259 if (tc->details.monitor.one_shot)
2261 if ( (0 != GNUNET_is_zero (&tc->details.monitor.peer)) &&
2262 (0 != GNUNET_memcmp (&tc->details.monitor.peer,
2275 * Called whenever a client connects. Allocates our
2276 * data structures associated with that client.
2278 * @param cls closure, NULL
2279 * @param client identification of the client
2280 * @param mq message queue for the client
2281 * @return our `struct TransportClient`
2284 client_connect_cb (void *cls,
2285 struct GNUNET_SERVICE_Client *client,
2286 struct GNUNET_MQ_Handle *mq)
2288 struct TransportClient *tc;
2291 tc = GNUNET_new (struct TransportClient);
2292 tc->client = client;
2294 GNUNET_CONTAINER_DLL_insert (clients_head,
2297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2298 "Client %p connected\n",
2307 * @param rc data structure to free
2310 free_reassembly_context (struct ReassemblyContext *rc)
2312 struct Neighbour *n = rc->neighbour;
2314 GNUNET_assert (rc ==
2315 GNUNET_CONTAINER_heap_remove_node (rc->hn));
2316 GNUNET_assert (GNUNET_OK ==
2317 GNUNET_CONTAINER_multishortmap_remove (n->reassembly_map,
2325 * Task run to clean up reassembly context of a neighbour that have expired.
2327 * @param cls a `struct Neighbour`
2330 reassembly_cleanup_task (void *cls)
2332 struct Neighbour *n = cls;
2333 struct ReassemblyContext *rc;
2335 n->reassembly_timeout_task = NULL;
2336 while (NULL != (rc = GNUNET_CONTAINER_heap_peek (n->reassembly_heap)))
2338 if (0 == GNUNET_TIME_absolute_get_remaining (rc->reassembly_timeout).rel_value_us)
2340 free_reassembly_context (rc);
2343 GNUNET_assert (NULL == n->reassembly_timeout_task);
2344 n->reassembly_timeout_task = GNUNET_SCHEDULER_add_at (rc->reassembly_timeout,
2345 &reassembly_cleanup_task,
2353 * function called to #free_reassembly_context().
2357 * @param value a `struct ReassemblyContext` to free
2358 * @return #GNUNET_OK (continue iteration)
2361 free_reassembly_cb (void *cls,
2362 const struct GNUNET_ShortHashCode *key,
2365 struct ReassemblyContext *rc = value;
2369 free_reassembly_context (rc);
2375 * Release memory used by @a neighbour.
2377 * @param neighbour neighbour entry to free
2380 free_neighbour (struct Neighbour *neighbour)
2382 struct DistanceVectorHop *dvh;
2384 GNUNET_assert (NULL == neighbour->queue_head);
2385 GNUNET_assert (GNUNET_YES ==
2386 GNUNET_CONTAINER_multipeermap_remove (neighbours,
2389 if (NULL != neighbour->timeout_task)
2390 GNUNET_SCHEDULER_cancel (neighbour->timeout_task);
2391 if (NULL != neighbour->reassembly_map)
2393 GNUNET_CONTAINER_multishortmap_iterate (neighbour->reassembly_map,
2394 &free_reassembly_cb,
2396 GNUNET_CONTAINER_multishortmap_destroy (neighbour->reassembly_map);
2397 neighbour->reassembly_map = NULL;
2398 GNUNET_CONTAINER_heap_destroy (neighbour->reassembly_heap);
2399 neighbour->reassembly_heap = NULL;
2401 while (NULL != (dvh = neighbour->dv_head))
2403 struct DistanceVector *dv = dvh->dv;
2405 free_distance_vector_hop (dvh);
2406 if (NULL == dv->dv_head)
2409 if (NULL != neighbour->reassembly_timeout_task)
2410 GNUNET_SCHEDULER_cancel (neighbour->reassembly_timeout_task);
2411 GNUNET_free (neighbour);
2416 * Send message to CORE clients that we lost a connection.
2418 * @param tc client to inform (must be CORE client)
2419 * @param pid peer the connection is for
2420 * @param quota_out current quota for the peer
2423 core_send_connect_info (struct TransportClient *tc,
2424 const struct GNUNET_PeerIdentity *pid,
2425 struct GNUNET_BANDWIDTH_Value32NBO quota_out)
2427 struct GNUNET_MQ_Envelope *env;
2428 struct ConnectInfoMessage *cim;
2430 GNUNET_assert (CT_CORE == tc->type);
2431 env = GNUNET_MQ_msg (cim,
2432 GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2433 cim->quota_out = quota_out;
2435 GNUNET_MQ_send (tc->mq,
2441 * Send message to CORE clients that we gained a connection
2443 * @param pid peer the queue was for
2444 * @param quota_out current quota for the peer
2447 cores_send_connect_info (const struct GNUNET_PeerIdentity *pid,
2448 struct GNUNET_BANDWIDTH_Value32NBO quota_out)
2450 for (struct TransportClient *tc = clients_head;
2454 if (CT_CORE != tc->type)
2456 core_send_connect_info (tc,
2464 * Send message to CORE clients that we lost a connection.
2466 * @param pid peer the connection was for
2469 cores_send_disconnect_info (const struct GNUNET_PeerIdentity *pid)
2471 for (struct TransportClient *tc = clients_head;
2475 struct GNUNET_MQ_Envelope *env;
2476 struct DisconnectInfoMessage *dim;
2478 if (CT_CORE != tc->type)
2480 env = GNUNET_MQ_msg (dim,
2481 GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2483 GNUNET_MQ_send (tc->mq,
2490 * We believe we are ready to transmit a message on a queue. Double-checks
2491 * with the queue's "tracker_out" and then gives the message to the
2492 * communicator for transmission (updating the tracker, and re-scheduling
2493 * itself if applicable).
2495 * @param cls the `struct Queue` to process transmissions for
2498 transmit_on_queue (void *cls);
2502 * Schedule next run of #transmit_on_queue(). Does NOTHING if
2503 * we should run immediately or if the message queue is empty.
2504 * Test for no task being added AND queue not being empty to
2505 * transmit immediately afterwards! This function must only
2506 * be called if the message queue is non-empty!
2508 * @param queue the queue to do scheduling for
2511 schedule_transmit_on_queue (struct Queue *queue)
2513 struct Neighbour *n = queue->neighbour;
2514 struct PendingMessage *pm = n->pending_msg_head;
2515 struct GNUNET_TIME_Relative out_delay;
2518 GNUNET_assert (NULL != pm);
2519 if (queue->tc->details.communicator.total_queue_length >=
2520 COMMUNICATOR_TOTAL_QUEUE_LIMIT)
2522 GNUNET_STATISTICS_update (GST_stats,
2523 "# Transmission throttled due to communicator queue limit",
2528 if (queue->queue_length >= QUEUE_LENGTH_LIMIT)
2530 GNUNET_STATISTICS_update (GST_stats,
2531 "# Transmission throttled due to queue queue limit",
2537 wsize = (0 == queue->mtu)
2538 ? pm->bytes_msg /* FIXME: add overheads? */
2540 out_delay = GNUNET_BANDWIDTH_tracker_get_delay (&queue->tracker_out,
2542 out_delay = GNUNET_TIME_relative_max (GNUNET_TIME_absolute_get_remaining (pm->next_attempt),
2544 if (0 == out_delay.rel_value_us)
2545 return; /* we should run immediately! */
2546 /* queue has changed since we were scheduled, reschedule again */
2547 queue->transmit_task
2548 = GNUNET_SCHEDULER_add_delayed (out_delay,
2551 if (out_delay.rel_value_us > DELAY_WARN_THRESHOLD.rel_value_us)
2552 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2553 "Next transmission on queue `%s' in %s (high delay)\n",
2555 GNUNET_STRINGS_relative_time_to_string (out_delay,
2558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2559 "Next transmission on queue `%s' in %s\n",
2561 GNUNET_STRINGS_relative_time_to_string (out_delay,
2567 * Check whether the CORE visibility of @a n changed. If so,
2568 * check whether we need to notify CORE.
2570 * @param n neighbour to perform the check for
2573 update_neighbour_core_visibility (struct Neighbour *n);
2579 * @param queue the queue to free
2582 free_queue (struct Queue *queue)
2584 struct Neighbour *neighbour = queue->neighbour;
2585 struct TransportClient *tc = queue->tc;
2586 struct MonitorEvent me = {
2587 .cs = GNUNET_TRANSPORT_CS_DOWN,
2588 .rtt = GNUNET_TIME_UNIT_FOREVER_REL
2590 struct QueueEntry *qe;
2593 if (NULL != queue->transmit_task)
2595 GNUNET_SCHEDULER_cancel (queue->transmit_task);
2596 queue->transmit_task = NULL;
2598 if (NULL != queue->visibility_task)
2600 GNUNET_SCHEDULER_cancel (queue->visibility_task);
2601 queue->visibility_task = NULL;
2603 GNUNET_CONTAINER_MDLL_remove (neighbour,
2604 neighbour->queue_head,
2605 neighbour->queue_tail,
2607 GNUNET_CONTAINER_MDLL_remove (client,
2608 tc->details.communicator.queue_head,
2609 tc->details.communicator.queue_tail,
2611 maxxed = (COMMUNICATOR_TOTAL_QUEUE_LIMIT >= tc->details.communicator.total_queue_length);
2612 while (NULL != (qe = queue->queue_head))
2614 GNUNET_CONTAINER_DLL_remove (queue->queue_head,
2617 queue->queue_length--;
2618 tc->details.communicator.total_queue_length--;
2621 GNUNET_assert (0 == queue->queue_length);
2623 (COMMUNICATOR_TOTAL_QUEUE_LIMIT < tc->details.communicator.total_queue_length) )
2625 /* Communicator dropped below threshold, resume all queues */
2626 GNUNET_STATISTICS_update (GST_stats,
2627 "# Transmission throttled due to communicator queue limit",
2630 for (struct Queue *s = tc->details.communicator.queue_head;
2633 schedule_transmit_on_queue (s);
2635 notify_monitors (&neighbour->pid,
2639 GNUNET_BANDWIDTH_tracker_notification_stop (&queue->tracker_in);
2640 GNUNET_BANDWIDTH_tracker_notification_stop (&queue->tracker_out);
2641 GNUNET_free (queue);
2643 update_neighbour_core_visibility (neighbour);
2644 cores_send_disconnect_info (&neighbour->pid);
2646 if (NULL == neighbour->queue_head)
2648 free_neighbour (neighbour);
2656 * @param ale address list entry to free
2659 free_address_list_entry (struct AddressListEntry *ale)
2661 struct TransportClient *tc = ale->tc;
2663 GNUNET_CONTAINER_DLL_remove (tc->details.communicator.addr_head,
2664 tc->details.communicator.addr_tail,
2666 if (NULL != ale->sc)
2668 GNUNET_PEERSTORE_store_cancel (ale->sc);
2671 if (NULL != ale->st)
2673 GNUNET_SCHEDULER_cancel (ale->st);
2681 * Stop the peer request in @a value.
2683 * @param cls a `struct TransportClient` that no longer makes the request
2684 * @param pid the peer's identity
2685 * @param value a `struct PeerRequest`
2686 * @return #GNUNET_YES (always)
2689 stop_peer_request (void *cls,
2690 const struct GNUNET_PeerIdentity *pid,
2693 struct TransportClient *tc = cls;
2694 struct PeerRequest *pr = value;
2696 GNUNET_PEERSTORE_watch_cancel (pr->wc);
2697 GNUNET_assert (GNUNET_YES ==
2698 GNUNET_CONTAINER_multipeermap_remove (tc->details.application.requests,
2708 * Called whenever a client is disconnected. Frees our
2709 * resources associated with that client.
2711 * @param cls closure, NULL
2712 * @param client identification of the client
2713 * @param app_ctx our `struct TransportClient`
2716 client_disconnect_cb (void *cls,
2717 struct GNUNET_SERVICE_Client *client,
2720 struct TransportClient *tc = app_ctx;
2723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2724 "Client %p disconnected, cleaning up.\n",
2726 GNUNET_CONTAINER_DLL_remove (clients_head,
2735 struct PendingMessage *pm;
2737 while (NULL != (pm = tc->details.core.pending_msg_head))
2739 GNUNET_CONTAINER_MDLL_remove (client,
2740 tc->details.core.pending_msg_head,
2741 tc->details.core.pending_msg_tail,
2749 case CT_COMMUNICATOR:
2752 struct AddressListEntry *ale;
2754 while (NULL != (q = tc->details.communicator.queue_head))
2756 while (NULL != (ale = tc->details.communicator.addr_head))
2757 free_address_list_entry (ale);
2758 GNUNET_free (tc->details.communicator.address_prefix);
2761 case CT_APPLICATION:
2762 GNUNET_CONTAINER_multipeermap_iterate (tc->details.application.requests,
2765 GNUNET_CONTAINER_multipeermap_destroy (tc->details.application.requests);
2773 * Iterator telling new CORE client about all existing
2774 * connections to peers.
2776 * @param cls the new `struct TransportClient`
2777 * @param pid a connected peer
2778 * @param value the `struct Neighbour` with more information
2779 * @return #GNUNET_OK (continue to iterate)
2782 notify_client_connect_info (void *cls,
2783 const struct GNUNET_PeerIdentity *pid,
2786 struct TransportClient *tc = cls;
2787 struct Neighbour *neighbour = value;
2789 core_send_connect_info (tc,
2791 neighbour->quota_out);
2797 * Initialize a "CORE" client. We got a start message from this
2798 * client, so add it to the list of clients for broadcasting of
2801 * @param cls the client
2802 * @param start the start message that was sent
2805 handle_client_start (void *cls,
2806 const struct StartMessage *start)
2808 struct TransportClient *tc = cls;
2811 options = ntohl (start->options);
2812 if ( (0 != (1 & options)) &&
2814 GNUNET_memcmp (&start->self,
2815 &GST_my_identity)) )
2817 /* client thinks this is a different peer, reject */
2819 GNUNET_SERVICE_client_drop (tc->client);
2822 if (CT_NONE != tc->type)
2825 GNUNET_SERVICE_client_drop (tc->client);
2829 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
2830 ¬ify_client_connect_info,
2832 GNUNET_SERVICE_client_continue (tc->client);
2837 * Client asked for transmission to a peer. Process the request.
2839 * @param cls the client
2840 * @param obm the send message that was sent
2843 check_client_send (void *cls,
2844 const struct OutboundMessage *obm)
2846 struct TransportClient *tc = cls;
2848 const struct GNUNET_MessageHeader *obmm;
2850 if (CT_CORE != tc->type)
2853 return GNUNET_SYSERR;
2855 size = ntohs (obm->header.size) - sizeof (struct OutboundMessage);
2856 if (size < sizeof (struct GNUNET_MessageHeader))
2859 return GNUNET_SYSERR;
2861 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
2862 if (size != ntohs (obmm->size))
2865 return GNUNET_SYSERR;
2872 * Free fragment tree below @e root, excluding @e root itself.
2874 * @param root root of the tree to free
2877 free_fragment_tree (struct PendingMessage *root)
2879 struct PendingMessage *frag;
2881 while (NULL != (frag = root->head_frag))
2883 free_fragment_tree (frag);
2884 GNUNET_CONTAINER_MDLL_remove (frag,
2894 * Release memory associated with @a pm and remove @a pm from associated
2895 * data structures. @a pm must be a top-level pending message and not
2896 * a fragment in the tree. The entire tree is freed (if applicable).
2898 * @param pm the pending message to free
2901 free_pending_message (struct PendingMessage *pm)
2903 struct TransportClient *tc = pm->client;
2904 struct Neighbour *target = pm->target;
2908 GNUNET_CONTAINER_MDLL_remove (client,
2909 tc->details.core.pending_msg_head,
2910 tc->details.core.pending_msg_tail,
2913 GNUNET_CONTAINER_MDLL_remove (neighbour,
2914 target->pending_msg_head,
2915 target->pending_msg_tail,
2917 free_fragment_tree (pm);
2918 GNUNET_free_non_null (pm->bpm);
2924 * Send a response to the @a pm that we have processed a
2925 * "send" request with status @a success. We
2926 * transmitted @a bytes_physical on the actual wire.
2927 * Sends a confirmation to the "core" client responsible
2928 * for the original request and free's @a pm.
2930 * @param pm handle to the original pending message
2931 * @param success status code, #GNUNET_OK on success, #GNUNET_SYSERR
2932 * for transmission failure
2933 * @param bytes_physical amount of bandwidth consumed
2936 client_send_response (struct PendingMessage *pm,
2938 uint32_t bytes_physical)
2940 struct TransportClient *tc = pm->client;
2941 struct Neighbour *target = pm->target;
2942 struct GNUNET_MQ_Envelope *env;
2943 struct SendOkMessage *som;
2947 env = GNUNET_MQ_msg (som,
2948 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
2949 som->success = htonl ((uint32_t) success);
2950 som->bytes_msg = htons (pm->bytes_msg);
2951 som->bytes_physical = htonl (bytes_physical);
2952 som->peer = target->pid;
2953 GNUNET_MQ_send (tc->mq,
2956 free_pending_message (pm);
2961 * Checks the message queue for a neighbour for messages that have timed
2962 * out and purges them.
2964 * @param cls a `struct Neighbour`
2967 check_queue_timeouts (void *cls)
2969 struct Neighbour *n = cls;
2970 struct PendingMessage *pm;
2971 struct GNUNET_TIME_Absolute now;
2972 struct GNUNET_TIME_Absolute earliest_timeout;
2974 n->timeout_task = NULL;
2975 earliest_timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2976 now = GNUNET_TIME_absolute_get ();
2977 for (struct PendingMessage *pos = n->pending_msg_head;
2981 pm = pos->next_neighbour;
2982 if (pos->timeout.abs_value_us <= now.abs_value_us)
2984 GNUNET_STATISTICS_update (GST_stats,
2985 "# messages dropped (timeout before confirmation)",
2988 client_send_response (pm,
2993 earliest_timeout = GNUNET_TIME_absolute_min (earliest_timeout,
2996 n->earliest_timeout = earliest_timeout;
2997 if (NULL != n->pending_msg_head)
2998 n->timeout_task = GNUNET_SCHEDULER_add_at (earliest_timeout,
2999 &check_queue_timeouts,
3005 * Client asked for transmission to a peer. Process the request.
3007 * @param cls the client
3008 * @param obm the send message that was sent
3011 handle_client_send (void *cls,
3012 const struct OutboundMessage *obm)
3014 struct TransportClient *tc = cls;
3015 struct PendingMessage *pm;
3016 const struct GNUNET_MessageHeader *obmm;
3017 struct Neighbour *target;
3021 GNUNET_assert (CT_CORE == tc->type);
3022 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
3023 bytes_msg = ntohs (obmm->size);
3024 target = lookup_neighbour (&obm->peer);
3027 /* Failure: don't have this peer as a neighbour (anymore).
3028 Might have gone down asynchronously, so this is NOT
3029 a protocol violation by CORE. Still count the event,
3030 as this should be rare. */
3031 struct GNUNET_MQ_Envelope *env;
3032 struct SendOkMessage *som;
3034 env = GNUNET_MQ_msg (som,
3035 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
3036 som->success = htonl (GNUNET_SYSERR);
3037 som->bytes_msg = htonl (bytes_msg);
3038 som->bytes_physical = htonl (0);
3039 som->peer = obm->peer;
3040 GNUNET_MQ_send (tc->mq,
3042 GNUNET_SERVICE_client_continue (tc->client);
3043 GNUNET_STATISTICS_update (GST_stats,
3044 "# messages dropped (neighbour unknown)",
3049 was_empty = (NULL == target->pending_msg_head);
3050 pm = GNUNET_malloc (sizeof (struct PendingMessage) + bytes_msg);
3052 pm->target = target;
3053 pm->bytes_msg = bytes_msg;
3054 pm->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
3058 GNUNET_CONTAINER_MDLL_insert (neighbour,
3059 target->pending_msg_head,
3060 target->pending_msg_tail,
3062 GNUNET_CONTAINER_MDLL_insert (client,
3063 tc->details.core.pending_msg_head,
3064 tc->details.core.pending_msg_tail,
3066 if (target->earliest_timeout.abs_value_us > pm->timeout.abs_value_us)
3068 target->earliest_timeout.abs_value_us = pm->timeout.abs_value_us;
3069 if (NULL != target->timeout_task)
3070 GNUNET_SCHEDULER_cancel (target->timeout_task);
3071 target->timeout_task
3072 = GNUNET_SCHEDULER_add_at (target->earliest_timeout,
3073 &check_queue_timeouts,
3077 return; /* all queues must already be busy */
3078 for (struct Queue *queue = target->queue_head;
3080 queue = queue->next_neighbour)
3082 /* try transmission on any queue that is idle */
3083 if (NULL == queue->transmit_task)
3084 queue->transmit_task = GNUNET_SCHEDULER_add_now (&transmit_on_queue,
3091 * Communicator started. Test message is well-formed.
3093 * @param cls the client
3094 * @param cam the send message that was sent
3097 check_communicator_available (void *cls,
3098 const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *cam)
3100 struct TransportClient *tc = cls;
3103 if (CT_NONE != tc->type)
3106 return GNUNET_SYSERR;
3108 tc->type = CT_COMMUNICATOR;
3109 size = ntohs (cam->header.size) - sizeof (*cam);
3111 return GNUNET_OK; /* receive-only communicator */
3112 GNUNET_MQ_check_zero_termination (cam);
3118 * Communicator started. Process the request.
3120 * @param cls the client
3121 * @param cam the send message that was sent
3124 handle_communicator_available (void *cls,
3125 const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *cam)
3127 struct TransportClient *tc = cls;
3130 size = ntohs (cam->header.size) - sizeof (*cam);
3132 return; /* receive-only communicator */
3133 tc->details.communicator.address_prefix
3134 = GNUNET_strdup ((const char *) &cam[1]);
3135 tc->details.communicator.cc
3136 = (enum GNUNET_TRANSPORT_CommunicatorCharacteristics) ntohl (cam->cc);
3137 GNUNET_SERVICE_client_continue (tc->client);
3142 * Communicator requests backchannel transmission. Check the request.
3144 * @param cls the client
3145 * @param cb the send message that was sent
3146 * @return #GNUNET_OK if message is well-formed
3149 check_communicator_backchannel (void *cls,
3150 const struct GNUNET_TRANSPORT_CommunicatorBackchannel *cb)
3152 const struct GNUNET_MessageHeader *inbox;
3158 msize = ntohs (cb->header.size) - sizeof (*cb);
3159 if (UINT16_MAX - msize >
3160 sizeof (struct TransportBackchannelEncapsulationMessage) +
3161 sizeof (struct TransportBackchannelRequestPayload) )
3164 return GNUNET_SYSERR;
3166 inbox = (const struct GNUNET_MessageHeader *) &cb[1];
3167 isize = ntohs (inbox->size);
3171 return GNUNET_SYSERR;
3173 is = (const char *) inbox;
3176 GNUNET_assert (msize > 0);
3177 if ('\0' != is[msize-1])
3180 return GNUNET_SYSERR;
3187 * Remove memory used by expired ephemeral keys.
3192 expire_ephemerals (void *cls)
3194 struct EphemeralCacheEntry *ece;
3197 ephemeral_task = NULL;
3198 while (NULL != (ece = GNUNET_CONTAINER_heap_peek (ephemeral_heap)))
3200 if (0 == GNUNET_TIME_absolute_get_remaining (ece->ephemeral_validity).rel_value_us)
3202 free_ephemeral (ece);
3205 ephemeral_task = GNUNET_SCHEDULER_add_at (ece->ephemeral_validity,
3214 * Lookup ephemeral key in our #ephemeral_map. If no valid one exists, generate
3215 * one, cache it and return it.
3217 * @param pid peer to look up ephemeral for
3218 * @param private_key[out] set to the private key
3219 * @param ephemeral_key[out] set to the key
3220 * @param ephemeral_sender_sig[out] set to the signature
3221 * @param ephemeral_validity[out] set to the validity expiration time
3224 lookup_ephemeral (const struct GNUNET_PeerIdentity *pid,
3225 struct GNUNET_CRYPTO_EcdhePrivateKey *private_key,
3226 struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral_key,
3227 struct GNUNET_CRYPTO_EddsaSignature *ephemeral_sender_sig,
3228 struct GNUNET_TIME_Absolute *ephemeral_validity)
3230 struct EphemeralCacheEntry *ece;
3231 struct EphemeralConfirmation ec;
3233 ece = GNUNET_CONTAINER_multipeermap_get (ephemeral_map,
3235 if ( (NULL != ece) &&
3236 (0 == GNUNET_TIME_absolute_get_remaining (ece->ephemeral_validity).rel_value_us) )
3238 free_ephemeral (ece);
3243 ece = GNUNET_new (struct EphemeralCacheEntry);
3245 ece->ephemeral_validity = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get_monotonic (GST_cfg),
3246 EPHEMERAL_VALIDITY);
3247 GNUNET_assert (GNUNET_OK ==
3248 GNUNET_CRYPTO_ecdhe_key_create2 (&ece->private_key));
3249 GNUNET_CRYPTO_ecdhe_key_get_public (&ece->private_key,
3250 &ece->ephemeral_key);
3251 ec.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_EPHEMERAL);
3252 ec.purpose.size = htonl (sizeof (ec));
3254 ec.ephemeral_key = ece->ephemeral_key;
3255 GNUNET_assert (GNUNET_OK ==
3256 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
3259 ece->hn = GNUNET_CONTAINER_heap_insert (ephemeral_heap,
3261 ece->ephemeral_validity.abs_value_us);
3262 GNUNET_assert (GNUNET_OK ==
3263 GNUNET_CONTAINER_multipeermap_put (ephemeral_map,
3266 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3267 if (NULL == ephemeral_task)
3268 ephemeral_task = GNUNET_SCHEDULER_add_at (ece->ephemeral_validity,
3272 *private_key = ece->private_key;
3273 *ephemeral_key = ece->ephemeral_key;
3274 *ephemeral_sender_sig = ece->sender_sig;
3275 *ephemeral_validity = ece->ephemeral_validity;
3280 * Send the control message @a payload on @a queue.
3282 * @param queue the queue to use for transmission
3283 * @param pm pending message to update once transmission is done, may be NULL!
3284 * @param payload the payload to send (encapsulated in a
3285 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG).
3286 * @param payload_size number of bytes in @a payload
3289 queue_send_msg (struct Queue *queue,
3290 struct PendingMessage *pm,
3291 const void *payload,
3292 size_t payload_size)
3294 struct Neighbour *n = queue->neighbour;
3295 struct GNUNET_TRANSPORT_SendMessageTo *smt;
3296 struct GNUNET_MQ_Envelope *env;
3298 env = GNUNET_MQ_msg_extra (smt,
3300 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG);
3301 smt->qid = queue->qid;
3302 smt->mid = queue->mid_gen;
3303 smt->receiver = n->pid;
3308 /* Pass the env to the communicator of queue for transmission. */
3309 struct QueueEntry *qe;
3311 qe = GNUNET_new (struct QueueEntry);
3312 qe->mid = queue->mid_gen++;
3314 // qe->pm = pm; // FIXME: not so easy, reference management on 'free(s)'!
3315 // (also, note that pm may be NULL!)
3316 GNUNET_CONTAINER_DLL_insert (queue->queue_head,
3319 GNUNET_assert (CT_COMMUNICATOR == queue->tc->type);
3320 queue->queue_length++;
3321 queue->tc->details.communicator.total_queue_length++;
3322 GNUNET_MQ_send (queue->tc->mq,
3329 * We need to transmit @a hdr to @a target. If necessary, this may
3330 * involve DV routing or even broadcasting and fragmentation.
3332 * @param target peer to receive @a hdr
3333 * @param hdr header of the message to route and #GNUNET_free()
3336 route_message (const struct GNUNET_PeerIdentity *target,
3337 struct GNUNET_MessageHeader *hdr)
3340 // 1: called to transmit backchannel message we initiated
3341 // 2: called to transmit fragment ack
3342 // 3: called to transmit reliability box
3343 // 4: called to forward backchannel message
3344 // 5: called to forward DV learn message (caller already picked random neighbour(s))!
3345 // 6: called to forward DV Box message
3346 // 7: called to forward valdiation response
3349 // a) Send ONLY to a *confirmed* direct neighbour
3350 // b) Send allowed to *unconfirmed* direct neighbour
3351 // c) Route also via *confirmed* DV to target
3352 // c) Route allowed via *unconfirmed DV to target
3353 // => One BIT "dv allowed or not", plus one BIT "confirmed/unconfirmed" might do!
3363 // FIXME: this one is tricky:
3364 // - we could try a direct, reliable channel
3365 // - if that is unavailable / for load balancing, we may try:
3366 // * multiple (?) direct unreliable channels - depending on loss rate?
3367 // * some (?) DV channels - if above unavailable / too lossy?
3368 // * _random_ other peers ("broadcasting") in hope of *discovering*
3369 // a path back! - if all else fails
3370 // => need more on DV first!
3372 // FIXME: send hdr to target, free hdr (possibly using DV, possibly broadcasting)
3378 * Structure of the key material used to encrypt backchannel messages.
3380 struct BackchannelKeyState
3383 * State of our block cipher.
3385 gcry_cipher_hd_t cipher;
3388 * Actual key material.
3393 * Key used for HMAC calculations (via #GNUNET_CRYPTO_hmac()).
3395 struct GNUNET_CRYPTO_AuthKey hmac_key;
3398 * Symmetric key to use for encryption.
3400 char aes_key[256/8];
3403 * Counter value to use during setup.
3405 char aes_ctr[128/8];
3412 bc_setup_key_state_from_km (const struct GNUNET_HashCode *km,
3413 const struct GNUNET_ShortHashCode *iv,
3414 struct BackchannelKeyState *key)
3416 /* must match #dh_key_derive_eph_pub */
3417 GNUNET_assert (GNUNET_YES ==
3418 GNUNET_CRYPTO_kdf (&key->material,
3419 sizeof (key->material),
3420 "transport-backchannel-key",
3421 strlen ("transport-backchannel-key"),
3426 gcry_cipher_open (&key->cipher,
3427 GCRY_CIPHER_AES256 /* low level: go for speed */,
3428 GCRY_CIPHER_MODE_CTR,
3430 gcry_cipher_setkey (key->cipher,
3431 &key->material.aes_key,
3432 sizeof (key->material.aes_key));
3433 gcry_cipher_setctr (key->cipher,
3434 &key->material.aes_ctr,
3435 sizeof (key->material.aes_ctr));
3440 * Derive backchannel encryption key material from @a priv_ephemeral
3441 * and @a target and @a iv.
3443 * @param priv_ephemeral ephemeral private key to use
3444 * @param target the target peer to encrypt to
3445 * @param iv unique IV to use
3446 * @param key[out] set to the key material
3449 dh_key_derive_eph_pid (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv_ephemeral,
3450 const struct GNUNET_PeerIdentity *target,
3451 const struct GNUNET_ShortHashCode *iv,
3452 struct BackchannelKeyState *key)
3454 struct GNUNET_HashCode km;
3456 GNUNET_assert (GNUNET_YES ==
3457 GNUNET_CRYPTO_ecdh_eddsa (priv_ephemeral,
3458 &target->public_key,
3460 bc_setup_key_state_from_km (&km,
3467 * Derive backchannel encryption key material from #GST_my_private_key
3468 * and @a pub_ephemeral and @a iv.
3470 * @param priv_ephemeral ephemeral private key to use
3471 * @param target the target peer to encrypt to
3472 * @param iv unique IV to use
3473 * @param key[out] set to the key material
3476 dh_key_derive_eph_pub (const struct GNUNET_CRYPTO_EcdhePublicKey *pub_ephemeral,
3477 const struct GNUNET_ShortHashCode *iv,
3478 struct BackchannelKeyState *key)
3480 struct GNUNET_HashCode km;
3482 GNUNET_assert (GNUNET_YES ==
3483 GNUNET_CRYPTO_eddsa_ecdh (GST_my_private_key,
3486 bc_setup_key_state_from_km (&km,
3493 * Do HMAC calculation for backchannel messages over @a data using key
3494 * material from @a key.
3496 * @param key key material (from DH)
3497 * @param hmac[out] set to the HMAC
3498 * @param data data to perform HMAC calculation over
3499 * @param data_size number of bytes in @a data
3502 bc_hmac (const struct BackchannelKeyState *key,
3503 struct GNUNET_HashCode *hmac,
3507 GNUNET_CRYPTO_hmac (&key->material.hmac_key,
3515 * Perform backchannel encryption using symmetric secret in @a key
3516 * to encrypt data from @a in to @a dst.
3518 * @param key[in,out] key material to use
3519 * @param dst where to write the result
3520 * @param in input data to encrypt (plaintext)
3521 * @param in_size number of bytes of input in @a in and available at @a dst
3524 bc_encrypt (struct BackchannelKeyState *key,
3530 gcry_cipher_encrypt (key->cipher,
3539 * Perform backchannel encryption using symmetric secret in @a key
3540 * to encrypt data from @a in to @a dst.
3542 * @param key[in,out] key material to use
3543 * @param ciph cipher text to decrypt
3544 * @param out[out] output data to generate (plaintext)
3545 * @param out_size number of bytes of input in @a ciph and available in @a out
3548 bc_decrypt (struct BackchannelKeyState *key,
3554 gcry_cipher_decrypt (key->cipher,
3563 * Clean up key material in @a key.
3565 * @param key key material to clean up (memory must not be free'd!)
3568 bc_key_clean (struct BackchannelKeyState *key)
3570 gcry_cipher_close (key->cipher);
3571 GNUNET_CRYPTO_zero_keys (&key->material,
3572 sizeof (key->material));
3577 * Communicator requests backchannel transmission. Process the request.
3579 * @param cls the client
3580 * @param cb the send message that was sent
3583 handle_communicator_backchannel (void *cls,
3584 const struct GNUNET_TRANSPORT_CommunicatorBackchannel *cb)
3586 struct TransportClient *tc = cls;
3587 struct GNUNET_CRYPTO_EcdhePrivateKey private_key;
3588 struct GNUNET_TIME_Absolute ephemeral_validity;
3589 struct TransportBackchannelEncapsulationMessage *enc;
3590 struct TransportBackchannelRequestPayload ppay;
3591 struct BackchannelKeyState key;
3595 /* encapsulate and encrypt message */
3596 msize = ntohs (cb->header.size) - sizeof (*cb) + sizeof (struct TransportBackchannelRequestPayload);
3597 enc = GNUNET_malloc (sizeof (*enc) + msize);
3598 enc->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BACKCHANNEL_ENCAPSULATION);
3599 enc->header.size = htons (sizeof (*enc) + msize);
3600 enc->target = cb->pid;
3601 lookup_ephemeral (&cb->pid,
3603 &enc->ephemeral_key,
3605 &ephemeral_validity);
3606 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
3609 dh_key_derive_eph_pid (&private_key,
3613 ppay.ephemeral_validity = GNUNET_TIME_absolute_hton (ephemeral_validity);
3614 ppay.monotonic_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (GST_cfg));
3615 mpos = (char *) &enc[1];
3622 &mpos[sizeof (ppay)],
3623 ntohs (cb->header.size) - sizeof (*cb));
3627 sizeof (ppay) + ntohs (cb->header.size) - sizeof (*cb));
3628 bc_key_clean (&key);
3629 route_message (&cb->pid,
3631 GNUNET_SERVICE_client_continue (tc->client);
3636 * Address of our peer added. Test message is well-formed.
3638 * @param cls the client
3639 * @param aam the send message that was sent
3640 * @return #GNUNET_OK if message is well-formed
3643 check_add_address (void *cls,
3644 const struct GNUNET_TRANSPORT_AddAddressMessage *aam)
3646 struct TransportClient *tc = cls;
3648 if (CT_COMMUNICATOR != tc->type)
3651 return GNUNET_SYSERR;
3653 GNUNET_MQ_check_zero_termination (aam);
3659 * Ask peerstore to store our address.
3661 * @param cls an `struct AddressListEntry *`
3664 store_pi (void *cls);
3668 * Function called when peerstore is done storing our address.
3670 * @param cls a `struct AddressListEntry`
3671 * @param success #GNUNET_YES if peerstore was successful
3674 peerstore_store_own_cb (void *cls,
3677 struct AddressListEntry *ale = cls;
3680 if (GNUNET_YES != success)
3681 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3682 "Failed to store our own address `%s' in peerstore!\n",
3684 /* refresh period is 1/4 of expiration time, that should be plenty
3685 without being excessive. */
3686 ale->st = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide (ale->expiration,
3694 * Ask peerstore to store our address.
3696 * @param cls an `struct AddressListEntry *`
3699 store_pi (void *cls)
3701 struct AddressListEntry *ale = cls;
3704 struct GNUNET_TIME_Absolute expiration;
3707 expiration = GNUNET_TIME_relative_to_absolute (ale->expiration);
3708 GNUNET_HELLO_sign_address (ale->address,
3714 ale->sc = GNUNET_PEERSTORE_store (peerstore,
3717 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY,
3721 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
3722 &peerstore_store_own_cb,
3725 if (NULL == ale->sc)
3727 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3728 "Failed to store our address `%s' with peerstore\n",
3730 ale->st = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
3738 * Address of our peer added. Process the request.
3740 * @param cls the client
3741 * @param aam the send message that was sent
3744 handle_add_address (void *cls,
3745 const struct GNUNET_TRANSPORT_AddAddressMessage *aam)
3747 struct TransportClient *tc = cls;
3748 struct AddressListEntry *ale;
3751 slen = ntohs (aam->header.size) - sizeof (*aam);
3752 ale = GNUNET_malloc (sizeof (struct AddressListEntry) + slen);
3754 ale->address = (const char *) &ale[1];
3755 ale->expiration = GNUNET_TIME_relative_ntoh (aam->expiration);
3756 ale->aid = aam->aid;
3757 ale->nt = (enum GNUNET_NetworkType) ntohl (aam->nt);
3761 GNUNET_CONTAINER_DLL_insert (tc->details.communicator.addr_head,
3762 tc->details.communicator.addr_tail,
3764 ale->st = GNUNET_SCHEDULER_add_now (&store_pi,
3766 GNUNET_SERVICE_client_continue (tc->client);
3771 * Address of our peer deleted. Process the request.
3773 * @param cls the client
3774 * @param dam the send message that was sent
3777 handle_del_address (void *cls,
3778 const struct GNUNET_TRANSPORT_DelAddressMessage *dam)
3780 struct TransportClient *tc = cls;
3782 if (CT_COMMUNICATOR != tc->type)
3785 GNUNET_SERVICE_client_drop (tc->client);
3788 for (struct AddressListEntry *ale = tc->details.communicator.addr_head;
3792 if (dam->aid != ale->aid)
3794 GNUNET_assert (ale->tc == tc);
3795 free_address_list_entry (ale);
3796 GNUNET_SERVICE_client_continue (tc->client);
3799 GNUNET_SERVICE_client_drop (tc->client);
3804 * Context from #handle_incoming_msg(). Closure for many
3805 * message handlers below.
3807 struct CommunicatorMessageContext
3810 * Which communicator provided us with the message.
3812 struct TransportClient *tc;
3815 * Additional information for flow control and about the sender.
3817 struct GNUNET_TRANSPORT_IncomingMessage im;
3820 * Number of hops the message has travelled (if DV-routed).
3821 * FIXME: make use of this in ACK handling!
3823 uint16_t total_hops;
3828 * Given an inbound message @a msg from a communicator @a cmc,
3829 * demultiplex it based on the type calling the right handler.
3831 * @param cmc context for demultiplexing
3832 * @param msg message to demultiplex
3835 demultiplex_with_cmc (struct CommunicatorMessageContext *cmc,
3836 const struct GNUNET_MessageHeader *msg);
3840 * Send ACK to communicator (if requested) and free @a cmc.
3842 * @param cmc context for which we are done handling the message
3845 finish_cmc_handling (struct CommunicatorMessageContext *cmc)
3847 if (0 != ntohl (cmc->im.fc_on))
3849 /* send ACK when done to communicator for flow control! */
3850 struct GNUNET_MQ_Envelope *env;
3851 struct GNUNET_TRANSPORT_IncomingMessageAck *ack;
3853 env = GNUNET_MQ_msg (ack,
3854 GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG_ACK);
3855 ack->reserved = htonl (0);
3856 ack->fc_id = cmc->im.fc_id;
3857 ack->sender = cmc->im.sender;
3858 GNUNET_MQ_send (cmc->tc->mq,
3861 GNUNET_SERVICE_client_continue (cmc->tc->client);
3867 * Communicator gave us an unencapsulated message to pass as-is to
3868 * CORE. Process the request.
3870 * @param cls a `struct CommunicatorMessageContext` (must call #finish_cmc_handling() when done)
3871 * @param mh the message that was received
3874 handle_raw_message (void *cls,
3875 const struct GNUNET_MessageHeader *mh)
3877 struct CommunicatorMessageContext *cmc = cls;
3878 uint16_t size = ntohs (mh->size);
3880 if ( (size > UINT16_MAX - sizeof (struct InboundMessage)) ||
3881 (size < sizeof (struct GNUNET_MessageHeader)) )
3883 struct GNUNET_SERVICE_Client *client = cmc->tc->client;
3886 finish_cmc_handling (cmc);
3887 GNUNET_SERVICE_client_drop (client);
3890 /* Forward to all CORE clients */
3891 for (struct TransportClient *tc = clients_head;
3895 struct GNUNET_MQ_Envelope *env;
3896 struct InboundMessage *im;
3898 if (CT_CORE != tc->type)
3900 env = GNUNET_MQ_msg_extra (im,
3902 GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3903 im->peer = cmc->im.sender;
3907 GNUNET_MQ_send (tc->mq,
3910 /* FIXME: consider doing this _only_ once the message
3911 was drained from the CORE MQs to extend flow control to CORE!
3912 (basically, increment counter in cmc, decrement on MQ send continuation! */
3913 finish_cmc_handling (cmc);
3918 * Communicator gave us a fragment box. Check the message.
3920 * @param cls a `struct CommunicatorMessageContext`
3921 * @param fb the send message that was sent
3922 * @return #GNUNET_YES if message is well-formed
3925 check_fragment_box (void *cls,
3926 const struct TransportFragmentBox *fb)
3928 uint16_t size = ntohs (fb->header.size);
3929 uint16_t bsize = size - sizeof (*fb);
3933 GNUNET_break_op (0);
3934 return GNUNET_SYSERR;
3936 if (bsize + ntohs (fb->frag_off) > ntohs (fb->msg_size))
3938 GNUNET_break_op (0);
3939 return GNUNET_SYSERR;
3941 if (ntohs (fb->frag_off) >= ntohs (fb->msg_size))
3943 GNUNET_break_op (0);
3944 return GNUNET_SYSERR;
3951 * Generate a fragment acknowledgement for an @a rc.
3953 * @param rc context to generate ACK for, @a rc ACK state is reset
3956 send_fragment_ack (struct ReassemblyContext *rc)
3958 struct TransportFragmentAckMessage *ack;
3960 ack = GNUNET_new (struct TransportFragmentAckMessage);
3961 ack->header.size = htons (sizeof (struct TransportFragmentAckMessage));
3962 ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_FRAGMENT_ACK);
3963 ack->frag_uuid = htonl (rc->frag_uuid);
3964 ack->extra_acks = GNUNET_htonll (rc->extra_acks);
3965 ack->msg_uuid = rc->msg_uuid;
3966 ack->avg_ack_delay = GNUNET_TIME_relative_hton (rc->avg_ack_delay);
3967 if (0 == rc->msg_missing)
3968 ack->reassembly_timeout
3969 = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL); /* signal completion */
3971 ack->reassembly_timeout
3972 = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (rc->reassembly_timeout));
3973 route_message (&rc->neighbour->pid,
3975 rc->avg_ack_delay = GNUNET_TIME_UNIT_ZERO;
3977 rc->extra_acks = 0LLU;
3982 * Communicator gave us a fragment. Process the request.
3984 * @param cls a `struct CommunicatorMessageContext` (must call #finish_cmc_handling() when done)
3985 * @param fb the message that was received
3988 handle_fragment_box (void *cls,
3989 const struct TransportFragmentBox *fb)
3991 struct CommunicatorMessageContext *cmc = cls;
3992 struct Neighbour *n;
3993 struct ReassemblyContext *rc;
3994 const struct GNUNET_MessageHeader *msg;
4000 struct GNUNET_TIME_Relative cdelay;
4003 n = GNUNET_CONTAINER_multipeermap_get (neighbours,
4007 struct GNUNET_SERVICE_Client *client = cmc->tc->client;
4010 finish_cmc_handling (cmc);
4011 GNUNET_SERVICE_client_drop (client);
4014 if (NULL == n->reassembly_map)
4016 n->reassembly_map = GNUNET_CONTAINER_multishortmap_create (8,
4018 n->reassembly_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4019 n->reassembly_timeout_task = GNUNET_SCHEDULER_add_delayed (REASSEMBLY_EXPIRATION,
4020 &reassembly_cleanup_task,
4023 msize = ntohs (fb->msg_size);
4024 rc = GNUNET_CONTAINER_multishortmap_get (n->reassembly_map,
4028 rc = GNUNET_malloc (sizeof (*rc) +
4029 msize + /* reassembly payload buffer */
4030 (msize + 7) / 8 * sizeof (uint8_t) /* bitfield */);
4031 rc->msg_uuid = fb->msg_uuid;
4033 rc->msg_size = msize;
4034 rc->reassembly_timeout = GNUNET_TIME_relative_to_absolute (REASSEMBLY_EXPIRATION);
4035 rc->last_frag = GNUNET_TIME_absolute_get ();
4036 rc->hn = GNUNET_CONTAINER_heap_insert (n->reassembly_heap,
4038 rc->reassembly_timeout.abs_value_us);
4039 GNUNET_assert (GNUNET_OK ==
4040 GNUNET_CONTAINER_multishortmap_put (n->reassembly_map,
4043 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4044 target = (char *) &rc[1];
4045 rc->bitfield = (uint8_t *) (target + rc->msg_size);
4046 rc->msg_missing = rc->msg_size;
4050 target = (char *) &rc[1];
4052 if (msize != rc->msg_size)
4055 finish_cmc_handling (cmc);
4060 fsize = ntohs (fb->header.size) - sizeof (*fb);
4061 frag_off = ntohs (fb->frag_off);
4062 memcpy (&target[frag_off],
4065 /* update bitfield and msg_missing */
4066 for (unsigned int i=frag_off;i<frag_off+fsize;i++)
4068 if (0 == (rc->bitfield[i / 8] & (1 << (i % 8))))
4070 rc->bitfield[i / 8] |= (1 << (i % 8));
4075 /* Compute cummulative ACK */
4076 frag_uuid = ntohl (fb->frag_uuid);
4077 cdelay = GNUNET_TIME_absolute_get_duration (rc->last_frag);
4078 cdelay = GNUNET_TIME_relative_multiply (cdelay,
4080 rc->last_frag = GNUNET_TIME_absolute_get ();
4081 rc->avg_ack_delay = GNUNET_TIME_relative_add (rc->avg_ack_delay,
4083 ack_now = GNUNET_NO;
4084 if (0 == rc->num_acks)
4086 /* case one: first ack */
4087 rc->frag_uuid = frag_uuid;
4088 rc->extra_acks = 0LLU;
4091 else if ( (frag_uuid >= rc->frag_uuid) &&
4092 (frag_uuid <= rc->frag_uuid + 64) )
4094 /* case two: ack fits after existing min UUID */
4095 if ( (frag_uuid == rc->frag_uuid) ||
4096 (0 != (rc->extra_acks & (1LLU << (frag_uuid - rc->frag_uuid - 1)))) )
4098 /* duplicate fragment, ack now! */
4099 ack_now = GNUNET_YES;
4103 rc->extra_acks |= (1LLU << (frag_uuid - rc->frag_uuid - 1));
4107 else if ( (rc->frag_uuid > frag_uuid) &&
4108 ( ( (rc->frag_uuid == frag_uuid + 64) &&
4109 (0 == rc->extra_acks) ) ||
4110 ( (rc->frag_uuid < frag_uuid + 64) &&
4111 (rc->extra_acks == (rc->extra_acks & ~ ((1LLU << (64 - (rc->frag_uuid - frag_uuid))) - 1LLU))) ) ) )
4113 /* can fit ack by shifting extra acks and starting at
4114 frag_uid, test above esured that the bits we will
4115 shift 'extra_acks' by are all zero. */
4116 rc->extra_acks <<= (rc->frag_uuid - frag_uuid);
4117 rc->extra_acks |= (1LLU << (rc->frag_uuid - frag_uuid - 1));
4118 rc->frag_uuid = frag_uuid;
4121 if (65 == rc->num_acks) /* FIXME: maybe use smaller threshold? This is very aggressive. */
4122 ack_now = GNUNET_YES; /* maximum acks received */
4123 // FIXME: possibly also ACK based on RTT (but for that we'd need to
4124 // determine the queue used for the ACK first!)
4126 /* is reassembly complete? */
4127 if (0 != rc->msg_missing)
4130 send_fragment_ack (rc);
4131 finish_cmc_handling (cmc);
4134 /* reassembly is complete, verify result */
4135 msg = (const struct GNUNET_MessageHeader *) &rc[1];
4136 if (ntohs (msg->size) != rc->msg_size)
4139 free_reassembly_context (rc);
4140 finish_cmc_handling (cmc);
4143 /* successful reassembly */
4144 send_fragment_ack (rc);
4145 demultiplex_with_cmc (cmc,
4147 /* FIXME: really free here? Might be bad if fragments are still
4148 en-route and we forget that we finished this reassembly immediately!
4149 -> keep around until timeout?
4150 -> shorten timeout based on ACK? */
4151 free_reassembly_context (rc);
4156 * Check the @a fa against the fragments associated with @a pm.
4157 * If it matches, remove the matching fragments from the transmission
4160 * @param pm pending message to check against the ack
4161 * @param fa the ack that was received
4162 * @return #GNUNET_YES if @a fa matched, #GNUNET_NO if not
4165 check_ack_against_pm (struct PendingMessage *pm,
4166 const struct TransportFragmentAckMessage *fa)
4169 struct PendingMessage *nxt;
4170 uint32_t fs = ntohl (fa->frag_uuid);
4171 uint64_t xtra = GNUNET_ntohll (fa->extra_acks);
4174 for (struct PendingMessage *frag = pm->head_frag;
4178 const struct TransportFragmentBox *tfb
4179 = (const struct TransportFragmentBox *) &pm[1];
4180 uint32_t fu = ntohl (tfb->frag_uuid);
4182 GNUNET_assert (PMT_FRAGMENT_BOX == frag->pmt);
4183 nxt = frag->next_frag;
4184 /* Check for exact match or match in the 'xtra' bitmask */
4188 (0 != (1LLU << (fu - fs - 1) & xtra)) ) )
4191 free_fragment_tree (frag);
4199 * Communicator gave us a fragment acknowledgement. Process the request.
4201 * @param cls a `struct CommunicatorMessageContext` (must call #finish_cmc_handling() when done)
4202 * @param fa the message that was received
4205 handle_fragment_ack (void *cls,
4206 const struct TransportFragmentAckMessage *fa)
4208 struct CommunicatorMessageContext *cmc = cls;
4209 struct Neighbour *n;
4212 n = GNUNET_CONTAINER_multipeermap_get (neighbours,
4216 struct GNUNET_SERVICE_Client *client = cmc->tc->client;
4219 finish_cmc_handling (cmc);
4220 GNUNET_SERVICE_client_drop (client);
4223 /* FIXME-OPTIMIZE: maybe use another hash map here? */
4224 matched = GNUNET_NO;
4225 for (struct PendingMessage *pm = n->pending_msg_head;
4227 pm = pm->prev_neighbour)
4230 GNUNET_memcmp (&fa->msg_uuid,
4233 matched = GNUNET_YES;
4235 check_ack_against_pm (pm,
4238 struct GNUNET_TIME_Relative avg_ack_delay
4239 = GNUNET_TIME_relative_ntoh (fa->avg_ack_delay);
4240 // FIXME: update RTT and other reliability data!
4241 // ISSUE: we don't know which of n's queues the message(s)
4242 // took (and in fact the different messages might have gone
4243 // over different queues and possibly over multiple).
4244 // => track queues with PendingMessages, and update RTT only if
4245 // the queue used is unique?
4246 // -> how can we get loss rates?
4247 // -> or, add extra state to Box and ACK to identify queue?
4248 // IDEA: generate MULTIPLE frag-uuids per fragment and track
4249 // the queue with the fragment! (-> this logic must
4250 // be moved into check_ack_against_pm!)
4251 (void) avg_ack_delay;
4255 GNUNET_STATISTICS_update (GST_stats,
4256 "# FRAGMENT_ACKS dropped, no matching fragment",
4260 if (NULL == pm->head_frag)
4262 // if entire message is ACKed, handle that as well.
4263 // => clean up PM, any post actions?
4264 free_pending_message (pm);
4268 struct GNUNET_TIME_Relative reassembly_timeout
4269 = GNUNET_TIME_relative_ntoh (fa->reassembly_timeout);
4270 // OPTIMIZE-FIXME: adjust retransmission strategy based on reassembly_timeout!
4271 (void) reassembly_timeout;
4275 if (GNUNET_NO == matched)
4277 GNUNET_STATISTICS_update (GST_stats,
4278 "# FRAGMENT_ACKS dropped, no matching pending message",
4282 finish_cmc_handling (cmc);
4287 * Communicator gave us a reliability box. Check the message.
4289 * @param cls a `struct CommunicatorMessageContext`
4290 * @param rb the send message that was sent
4291 * @return #GNUNET_YES if message is well-formed
4294 check_reliability_box (void *cls,
4295 const struct TransportReliabilityBox *rb)
4297 GNUNET_MQ_check_boxed_message (rb);
4303 * Communicator gave us a reliability box. Process the request.
4305 * @param cls a `struct CommunicatorMessageContext` (must call #finish_cmc_handling() when done)
4306 * @param rb the message that was received
4309 handle_reliability_box (void *cls,
4310 const struct TransportReliabilityBox *rb)
4312 struct CommunicatorMessageContext *cmc = cls;
4313 const struct GNUNET_MessageHeader *inbox = (const struct GNUNET_MessageHeader *) &rb[1];
4315 if (0 == ntohl (rb->ack_countdown))
4317 struct TransportReliabilityAckMessage *ack;
4319 /* FIXME: implement cummulative ACKs and ack_countdown,
4320 then setting the avg_ack_delay field below: */
4321 ack = GNUNET_malloc (sizeof (*ack) +
4322 sizeof (struct GNUNET_ShortHashCode));
4323 ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RELIABILITY_ACK);
4324 ack->header.size = htons (sizeof (*ack) +
4325 sizeof (struct GNUNET_ShortHashCode));
4328 sizeof (struct GNUNET_ShortHashCode));
4329 route_message (&cmc->im.sender,
4332 /* continue with inner message */
4333 demultiplex_with_cmc (cmc,
4339 * Communicator gave us a reliability ack. Process the request.
4341 * @param cls a `struct CommunicatorMessageContext` (must call #finish_cmc_handling() when done)
4342 * @param ra the message that was received
4345 handle_reliability_ack (void *cls,
4346 const struct TransportReliabilityAckMessage *ra)
4348 struct CommunicatorMessageContext *cmc = cls;
4349 struct Neighbour *n;
4350 unsigned int n_acks;
4351 const struct GNUNET_ShortHashCode *msg_uuids;
4352 struct PendingMessage *nxt;
4355 n = GNUNET_CONTAINER_multipeermap_get (neighbours,
4359 struct GNUNET_SERVICE_Client *client = cmc->tc->client;
4362 finish_cmc_handling (cmc);
4363 GNUNET_SERVICE_client_drop (client);
4366 n_acks = (ntohs (ra->header.size) - sizeof (*ra))
4367 / sizeof (struct GNUNET_ShortHashCode);
4368 msg_uuids = (const struct GNUNET_ShortHashCode *) &ra[1];
4370 /* FIXME-OPTIMIZE: maybe use another hash map here? */
4371 matched = GNUNET_NO;
4372 for (struct PendingMessage *pm = n->pending_msg_head;
4378 nxt = pm->next_neighbour;
4379 in_list = GNUNET_NO;
4380 for (unsigned int i=0;i<n_acks;i++)
4383 GNUNET_memcmp (&msg_uuids[i],
4386 in_list = GNUNET_YES;
4389 if (GNUNET_NO == in_list)
4392 /* this pm was acked! */
4393 matched = GNUNET_YES;
4394 free_pending_message (pm);
4397 struct GNUNET_TIME_Relative avg_ack_delay
4398 = GNUNET_TIME_relative_ntoh (ra->avg_ack_delay);
4399 // FIXME: update RTT and other reliability data!
4400 // ISSUE: we don't know which of n's queues the message(s)
4401 // took (and in fact the different messages might have gone
4402 // over different queues and possibly over multiple).
4403 // => track queues with PendingMessages, and update RTT only if
4404 // the queue used is unique?
4405 // -> how can we get loss rates?
4406 // -> or, add extra state to MSG and ACKs to identify queue?
4407 // -> if we do this, might just do the same for the avg_ack_delay!
4408 (void) avg_ack_delay;
4411 if (GNUNET_NO == matched)
4413 GNUNET_STATISTICS_update (GST_stats,
4414 "# FRAGMENT_ACKS dropped, no matching pending message",
4418 finish_cmc_handling (cmc);
4423 * Communicator gave us a backchannel encapsulation. Check the message.
4425 * @param cls a `struct CommunicatorMessageContext`
4426 * @param be the send message that was sent
4427 * @return #GNUNET_YES if message is well-formed
4430 check_backchannel_encapsulation (void *cls,
4431 const struct TransportBackchannelEncapsulationMessage *be)
4433 uint16_t size = ntohs (be->header.size);
4436 if (size - sizeof (*be) <
4437 sizeof (struct TransportBackchannelRequestPayload) +
4438 sizeof (struct GNUNET_MessageHeader) )
4440 GNUNET_break_op (0);
4441 return GNUNET_SYSERR;
4448 * Communicator gave us a backchannel encapsulation. Process the request.
4449 * (We are not the origin of the backchannel here, the communicator simply
4450 * received a backchannel message and we are expected to forward it.)
4452 * @param cls a `struct CommunicatorMessageContext` (must call #finish_cmc_handling() when done)
4453 * @param be the message that was received
4456 handle_backchannel_encapsulation (void *cls,
4457 const struct TransportBackchannelEncapsulationMessage *be)
4459 struct CommunicatorMessageContext *cmc = cls;
4460 struct BackchannelKeyState key;
4461 struct GNUNET_HashCode hmac;
4465 if (0 != GNUNET_memcmp (&be->target,
4468 /* not for me, try to route to target */
4469 /* FIXME: someone needs to update be->distance! */
4470 /* FIXME: BE routing can be special, should we put all of this
4471 on 'route_message'? Maybe at least pass some more arguments? */
4472 route_message (&be->target,
4473 GNUNET_copy_message (&be->header));
4474 finish_cmc_handling (cmc);
4477 dh_key_derive_eph_pub (&be->ephemeral_key,
4480 hdr = (const char *) &be[1];
4481 hdr_len = ntohs (be->header.size) - sizeof (*be);
4487 GNUNET_memcmp (&hmac,
4490 /* HMAC missmatch, disard! */
4491 GNUNET_break_op (0);
4492 finish_cmc_handling (cmc);
4495 /* begin actual decryption */
4497 struct TransportBackchannelRequestPayload ppay;
4498 char body[hdr_len - sizeof (ppay)];
4500 GNUNET_assert (hdr_len >= sizeof (ppay) + sizeof (struct GNUNET_MessageHeader));
4507 &hdr[sizeof (ppay)],
4508 hdr_len - sizeof (ppay));
4509 bc_key_clean (&key);
4510 // FIXME: verify signatures in ppay!
4511 // => check if ephemeral key is known & valid, if not
4512 // => verify sig, cache ephemeral key
4513 // => update monotonic_time of sender for replay detection
4515 // FIXME: forward to specified communicator!
4516 // (using GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL_INCOMING)
4518 finish_cmc_handling (cmc);
4523 * Task called when we should check if any of the DV paths
4524 * we have learned to a target are due for garbage collection.
4526 * Collects stale paths, and possibly frees the entire DV
4527 * entry if no paths are left. Otherwise re-schedules itself.
4529 * @param cls a `struct DistanceVector`
4532 path_cleanup_cb (void *cls)
4534 struct DistanceVector *dv = cls;
4535 struct DistanceVectorHop *pos;
4537 dv->timeout_task = NULL;
4538 while (NULL != (pos = dv->dv_head))
4540 GNUNET_assert (dv == pos->dv);
4541 if (GNUNET_TIME_absolute_get_remaining (pos->timeout).rel_value_us > 0)
4543 free_distance_vector_hop (pos);
4550 dv->timeout_task = GNUNET_SCHEDULER_add_at (pos->timeout,
4557 * We have learned a @a path through the network to some other peer, add it to
4558 * our DV data structure (returning #GNUNET_YES on success).
4560 * We do not add paths if we have a sufficient number of shorter
4561 * paths to this target already (returning #GNUNET_NO).
4563 * We also do not add problematic paths, like those where we lack the first
4564 * hop in our neighbour list (i.e. due to a topology change) or where some
4565 * non-first hop is in our neighbour list (returning #GNUNET_SYSERR).
4567 * @param path the path we learned, path[0] should be us,
4568 * and then path contains a valid path from us to `path[path_len-1]`
4569 * path[1] should be a direct neighbour (we should check!)
4570 * @param path_len number of entries on the @a path, at least three!
4571 * @param network_latency how long does the message take from us to `path[path_len-1]`?
4572 * set to "forever" if unknown
4573 * @return #GNUNET_YES on success,
4574 * #GNUNET_NO if we have better path(s) to the target
4575 * #GNUNET_SYSERR if the path is useless and/or invalid
4576 * (i.e. path[1] not a direct neighbour
4577 * or path[i+1] is a direct neighbour for i>0)
4580 learn_dv_path (const struct GNUNET_PeerIdentity *path,
4581 unsigned int path_len,
4582 struct GNUNET_TIME_Relative network_latency)
4584 struct DistanceVectorHop *hop;
4585 struct DistanceVector *dv;
4586 struct Neighbour *next_hop;
4587 unsigned int shorter_distance;
4591 /* what a boring path! not allowed! */
4593 return GNUNET_SYSERR;
4596 GNUNET_memcmp (&GST_my_identity,
4598 next_hop = GNUNET_CONTAINER_multipeermap_get (neighbours,
4600 if (NULL == next_hop)
4602 /* next hop must be a neighbour, otherwise this whole thing is useless! */
4604 return GNUNET_SYSERR;
4606 for (unsigned int i=2;i<path_len;i++)
4608 GNUNET_CONTAINER_multipeermap_get (neighbours,
4611 /* Useless path, we have a direct connection to some hop
4612 in the middle of the path, so this one doesn't even
4613 seem terribly useful for redundancy */
4614 return GNUNET_SYSERR;
4616 dv = GNUNET_CONTAINER_multipeermap_get (dv_routes,
4617 &path[path_len - 1]);
4620 dv = GNUNET_new (struct DistanceVector);
4621 dv->target = path[path_len - 1];
4622 dv->timeout_task = GNUNET_SCHEDULER_add_delayed (DV_PATH_VALIDITY_TIMEOUT,
4625 GNUNET_assert (GNUNET_OK ==
4626 GNUNET_CONTAINER_multipeermap_put (dv_routes,
4629 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
4631 /* Check if we have this path already! */
4632 shorter_distance = 0;
4633 for (struct DistanceVectorHop *pos = dv->dv_head;
4637 if (pos->distance < path_len - 2)
4639 /* Note that the distances in 'pos' excludes us (path[0]) and
4640 the next_hop (path[1]), so we need to subtract two
4641 and check next_hop explicitly */
4642 if ( (pos->distance == path_len - 2) &&
4643 (pos->next_hop == next_hop) )
4645 int match = GNUNET_YES;
4647 for (unsigned int i=0;i<pos->distance;i++)
4650 GNUNET_memcmp (&pos->path[i],
4657 if (GNUNET_YES == match)
4659 struct GNUNET_TIME_Relative last_timeout;
4661 /* Re-discovered known path, update timeout */
4662 GNUNET_STATISTICS_update (GST_stats,
4663 "# Known DV path refreshed",
4666 last_timeout = GNUNET_TIME_absolute_get_remaining (pos->timeout);
4668 = GNUNET_TIME_relative_to_absolute (DV_PATH_VALIDITY_TIMEOUT);
4669 GNUNET_CONTAINER_MDLL_remove (dv,
4673 GNUNET_CONTAINER_MDLL_insert (dv,
4677 if (last_timeout.rel_value_us <
4678 GNUNET_TIME_relative_subtract (DV_PATH_VALIDITY_TIMEOUT,
4679 DV_PATH_DISCOVERY_FREQUENCY).rel_value_us)
4681 /* Some peer send DV learn messages too often, we are learning
4682 the same path faster than it would be useful; do not forward! */
4689 /* Count how many shorter paths we have (incl. direct
4690 neighbours) before simply giving up on this one! */
4691 if (shorter_distance >= MAX_DV_PATHS_TO_TARGET)
4693 /* We have a shorter path already! */
4696 /* create new DV path entry */
4697 hop = GNUNET_malloc (sizeof (struct DistanceVectorHop) +
4698 sizeof (struct GNUNET_PeerIdentity) * (path_len - 2));
4699 hop->next_hop = next_hop;
4701 hop->path = (const struct GNUNET_PeerIdentity *) &hop[1];
4704 sizeof (struct GNUNET_PeerIdentity) * (path_len - 2));
4705 hop->timeout = GNUNET_TIME_relative_to_absolute (DV_PATH_VALIDITY_TIMEOUT);
4706 hop->distance = path_len - 2;
4707 GNUNET_CONTAINER_MDLL_insert (dv,
4711 GNUNET_CONTAINER_MDLL_insert (neighbour,
4720 * Communicator gave us a DV learn message. Check the message.
4722 * @param cls a `struct CommunicatorMessageContext`
4723 * @param dvl the send message that was sent
4724 * @return #GNUNET_YES if message is well-formed
4727 check_dv_learn (void *cls,
4728 const struct TransportDVLearn *dvl)
4730 uint16_t size = ntohs (dvl->header.size);
4731 uint16_t num_hops = ntohs (dvl->num_hops);
4732 const struct DVPathEntryP *hops = (const struct DVPathEntryP *) &dvl[1];
4735 if (size != sizeof (*dvl) + num_hops * sizeof (struct DVPathEntryP))
4737 GNUNET_break_op (0);
4738 return GNUNET_SYSERR;
4740 if (num_hops > MAX_DV_HOPS_ALLOWED)
4742 GNUNET_break_op (0);
4743 return GNUNET_SYSERR;
4745 for (unsigned int i=0;i<num_hops;i++)
4747 if (0 == GNUNET_memcmp (&dvl->initiator,
4750 GNUNET_break_op (0);
4751 return GNUNET_SYSERR;
4753 if (0 == GNUNET_memcmp (&GST_my_identity,
4756 GNUNET_break_op (0);
4757 return GNUNET_SYSERR;
4765 * Build and forward a DV learn message to @a next_hop.
4767 * @param next_hop peer to send the message to
4768 * @param msg message received
4769 * @param bi_history bitmask specifying hops on path that were bidirectional
4770 * @param nhops length of the @a hops array
4771 * @param hops path the message traversed so far
4772 * @param in_time when did we receive the message, used to calculate network delay
4775 forward_dv_learn (const struct GNUNET_PeerIdentity *next_hop,
4776 const struct TransportDVLearn *msg,
4777 uint16_t bi_history,
4779 const struct DVPathEntryP *hops,
4780 struct GNUNET_TIME_Absolute in_time)
4782 struct DVPathEntryP *dhops;
4783 struct TransportDVLearn *fwd;
4784 struct GNUNET_TIME_Relative nnd;
4786 /* compute message for forwarding */
4787 GNUNET_assert (nhops < MAX_DV_HOPS_ALLOWED);
4788 fwd = GNUNET_malloc (sizeof (struct TransportDVLearn) +
4789 (nhops + 1) * sizeof (struct DVPathEntryP));
4790 fwd->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DV_LEARN);
4791 fwd->header.size = htons (sizeof (struct TransportDVLearn) +
4792 (nhops + 1) * sizeof (struct DVPathEntryP));
4793 fwd->num_hops = htons (nhops + 1);
4794 fwd->bidirectional = htons (bi_history);
4795 nnd = GNUNET_TIME_relative_add (GNUNET_TIME_absolute_get_duration (in_time),
4796 GNUNET_TIME_relative_ntoh (msg->non_network_delay));
4797 fwd->non_network_delay = GNUNET_TIME_relative_hton (nnd);
4798 fwd->init_sig = msg->init_sig;
4799 fwd->initiator = msg->initiator;
4800 fwd->challenge = msg->challenge;
4801 dhops = (struct DVPathEntryP *) &fwd[1];
4802 GNUNET_memcpy (dhops,
4804 sizeof (struct DVPathEntryP) * nhops);
4805 dhops[nhops].hop = GST_my_identity;
4807 struct DvHopPS dhp = {
4808 .purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DV_HOP),
4809 .purpose.size = htonl (sizeof (dhp)),
4810 .pred = dhops[nhops-1].hop,
4812 .challenge = msg->challenge
4815 GNUNET_assert (GNUNET_OK ==
4816 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
4818 &dhops[nhops].hop_sig));
4820 route_message (next_hop,
4826 * Check signature of type #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DV_INITIATOR
4828 * @param init the signer
4829 * @param challenge the challenge that was signed
4830 * @param init_sig signature presumably by @a init
4831 * @return #GNUNET_OK if the signature is valid
4834 validate_dv_initiator_signature (const struct GNUNET_PeerIdentity *init,
4835 const struct GNUNET_ShortHashCode *challenge,
4836 const struct GNUNET_CRYPTO_EddsaSignature *init_sig)
4838 struct DvInitPS ip = {
4839 .purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DV_INITIATOR),
4840 .purpose.size = htonl (sizeof (ip)),
4841 .challenge = *challenge
4845 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DV_INITIATOR,
4850 GNUNET_break_op (0);
4851 return GNUNET_SYSERR;
4858 * Communicator gave us a DV learn message. Process the request.
4860 * @param cls a `struct CommunicatorMessageContext` (must call #finish_cmc_handling() when done)
4861 * @param dvl the message that was received
4864 handle_dv_learn (void *cls,
4865 const struct TransportDVLearn *dvl)
4867 struct CommunicatorMessageContext *cmc = cls;
4868 enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc;
4871 uint16_t bi_history;
4872 const struct DVPathEntryP *hops;
4875 struct GNUNET_TIME_Absolute in_time;
4877 nhops = ntohs (dvl->bidirectional); /* 0 = sender is initiator */
4878 bi_history = ntohs (dvl->bidirectional);
4879 hops = (const struct DVPathEntryP *) &dvl[1];
4883 if (0 != GNUNET_memcmp (&dvl->initiator,
4887 finish_cmc_handling (cmc);
4894 if (0 != GNUNET_memcmp (&hops[nhops - 1].hop,
4898 finish_cmc_handling (cmc);
4903 GNUNET_assert (CT_COMMUNICATOR == cmc->tc->type);
4904 cc = cmc->tc->details.communicator.cc;
4905 bi_hop = (GNUNET_TRANSPORT_CC_RELIABLE == cc); // FIXME: add bi-directional flag to cc?
4906 in_time = GNUNET_TIME_absolute_get ();
4908 /* continue communicator here, everything else can happen asynchronous! */
4909 finish_cmc_handling (cmc);
4911 /* OPTIMIZE-FIXME: Technically, we only need to bother checking
4912 the initiator signature if we send the message back to the initiator... */
4914 validate_dv_initiator_signature (&dvl->initiator,
4918 GNUNET_break_op (0);
4921 // FIXME: asynchronously (!) verify hop-by-hop signatures!
4922 // => if signature verification load too high, implement random drop strategy!?
4924 do_fwd = GNUNET_YES;
4925 if (0 == GNUNET_memcmp (&GST_my_identity,
4928 struct GNUNET_PeerIdentity path[nhops + 1];
4929 struct GNUNET_TIME_Relative host_latency_sum;
4930 struct GNUNET_TIME_Relative latency;
4931 struct GNUNET_TIME_Relative network_latency;
4933 /* We initiated this, learn the forward path! */
4934 path[0] = GST_my_identity;
4935 path[1] = hops[0].hop;
4936 host_latency_sum = GNUNET_TIME_relative_ntoh (dvl->non_network_delay);
4938 // Need also something to lookup initiation time
4939 // to compute RTT! -> add RTT argument here?
4940 latency = GNUNET_TIME_UNIT_FOREVER_REL; // FIXME: initialize properly
4941 // (based on dvl->challenge, we can identify time of origin!)
4943 network_latency = GNUNET_TIME_relative_subtract (latency,
4945 /* assumption: latency on all links is the same */
4946 network_latency = GNUNET_TIME_relative_divide (network_latency,
4949 for (unsigned int i=2;i<=nhops;i++)
4951 struct GNUNET_TIME_Relative ilat;
4953 /* assumption: linear latency increase per hop */
4954 ilat = GNUNET_TIME_relative_multiply (network_latency,
4956 path[i] = hops[i-1].hop;
4957 // FIXME: mark ALL of these as *confirmed* (with what timeout?)
4958 // -- and schedule a job for the confirmation to time out! --
4959 // and possibly do #cores_send_connect_info() if
4960 // the respective neighbour is NOT confirmed yet!
4961 learn_dv_path (path,
4965 /* as we initiated, do not forward again (would be circular!) */
4971 /* last hop was bi-directional, we could learn something here! */
4972 struct GNUNET_PeerIdentity path[nhops + 2];
4974 path[0] = GST_my_identity;
4975 path[1] = hops[nhops - 1].hop; /* direct neighbour == predecessor! */
4976 for (unsigned int i=0;i<nhops;i++)
4980 if (0 == (bi_history & (1 << i)))
4981 break; /* i-th hop not bi-directional, stop learning! */
4984 path[i + 2] = dvl->initiator;
4988 path[i + 2] = hops[nhops - i - 2].hop;
4991 iret = learn_dv_path (path,
4993 GNUNET_TIME_UNIT_FOREVER_REL);
4994 if (GNUNET_SYSERR == iret)
4996 /* path invalid or too long to be interesting for US, thus should also
4997 not be interesting to our neighbours, cut path when forwarding to
4998 'i' hops, except of course for the one that goes back to the
5000 GNUNET_STATISTICS_update (GST_stats,
5001 "# DV learn not forwarded due invalidity of path",
5007 if ( (GNUNET_NO == iret) &&
5010 /* we have better paths, and this is the longest target,
5011 so there cannot be anything interesting later */
5012 GNUNET_STATISTICS_update (GST_stats,
5013 "# DV learn not forwarded, got better paths",
5022 if (MAX_DV_HOPS_ALLOWED == nhops)
5024 /* At limit, we're out of here! */
5025 finish_cmc_handling (cmc);
5029 /* Forward to initiator, if path non-trivial and possible */
5030 bi_history = (bi_history << 1) | (bi_hop ? 1 : 0);
5031 did_initiator = GNUNET_NO;
5034 GNUNET_CONTAINER_multipeermap_contains (neighbours,
5037 /* send back to origin! */
5038 forward_dv_learn (&dvl->initiator,
5044 did_initiator = GNUNET_YES;
5046 /* We forward under two conditions: either we still learned something
5047 ourselves (do_fwd), or the path was darn short and thus the initiator is
5048 likely to still be very interested in this (and we did NOT already
5049 send it back to the initiator) */
5051 ( (nhops < MIN_DV_PATH_LENGTH_FOR_INITIATOR) &&
5052 (GNUNET_NO == did_initiator) ) )
5054 /* FIXME: loop over all neighbours, pick those with low
5055 queues AND that are not yet on the path; possibly
5056 adapt threshold to nhops! */
5058 forward_dv_learn (NULL, // fill in peer from iterator here!
5070 * Communicator gave us a DV box. Check the message.
5072 * @param cls a `struct CommunicatorMessageContext`
5073 * @param dvb the send message that was sent
5074 * @return #GNUNET_YES if message is well-formed
5077 check_dv_box (void *cls,
5078 const struct TransportDVBox *dvb)
5080 uint16_t size = ntohs (dvb->header.size);
5081 uint16_t num_hops = ntohs (dvb->num_hops);
5082 const struct GNUNET_PeerIdentity *hops = (const struct GNUNET_PeerIdentity *) &dvb[1];
5083 const struct GNUNET_MessageHeader *inbox = (const struct GNUNET_MessageHeader *) &hops[num_hops];
5088 if (size < sizeof (*dvb) + num_hops * sizeof (struct GNUNET_PeerIdentity) + sizeof (struct GNUNET_MessageHeader))
5090 GNUNET_break_op (0);
5091 return GNUNET_SYSERR;
5093 isize = ntohs (inbox->size);
5094 if (size != sizeof (*dvb) + num_hops * sizeof (struct GNUNET_PeerIdentity) + isize)
5096 GNUNET_break_op (0);
5097 return GNUNET_SYSERR;
5099 itype = ntohs (inbox->type);
5100 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_DV_BOX == itype) ||
5101 (GNUNET_MESSAGE_TYPE_TRANSPORT_DV_LEARN == itype) )
5103 GNUNET_break_op (0);
5104 return GNUNET_SYSERR;
5107 GNUNET_memcmp (&dvb->origin,
5110 GNUNET_break_op (0);
5111 return GNUNET_SYSERR;
5118 * Create a DV Box message and queue it for transmission to
5121 * @param next_hop peer to receive the message next
5122 * @param total_hops how many hops did the message take so far
5123 * @param num_hops length of the @a hops array
5124 * @param origin origin of the message
5125 * @param hops next peer(s) to the destination, including destination
5126 * @param payload payload of the box
5127 * @param payload_size number of bytes in @a payload
5130 forward_dv_box (struct Neighbour *next_hop,
5131 uint16_t total_hops,
5133 const struct GNUNET_PeerIdentity *origin,
5134 const struct GNUNET_PeerIdentity *hops,
5135 const void *payload,
5136 uint16_t payload_size)
5138 struct TransportDVBox *dvb;
5139 struct GNUNET_PeerIdentity *dhops;
5141 GNUNET_assert (UINT16_MAX <
5142 sizeof (struct TransportDVBox) +
5143 sizeof (struct GNUNET_PeerIdentity) * num_hops +
5145 dvb = GNUNET_malloc (sizeof (struct TransportDVBox) +
5146 sizeof (struct GNUNET_PeerIdentity) * num_hops +
5148 dvb->header.size = htons (sizeof (struct TransportDVBox) +
5149 sizeof (struct GNUNET_PeerIdentity) * num_hops +
5151 dvb->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DV_BOX);
5152 dvb->total_hops = htons (total_hops);
5153 dvb->num_hops = htons (num_hops);
5154 dvb->origin = *origin;
5155 dhops = (struct GNUNET_PeerIdentity *) &dvb[1];
5158 num_hops * sizeof (struct GNUNET_PeerIdentity));
5159 memcpy (&dhops[num_hops],
5162 route_message (&next_hop->pid,
5168 * Communicator gave us a DV box. Process the request.
5170 * @param cls a `struct CommunicatorMessageContext` (must call #finish_cmc_handling() when done)
5171 * @param dvb the message that was received
5174 handle_dv_box (void *cls,
5175 const struct TransportDVBox *dvb)
5177 struct CommunicatorMessageContext *cmc = cls;
5178 uint16_t size = ntohs (dvb->header.size) - sizeof (*dvb);
5179 uint16_t num_hops = ntohs (dvb->num_hops);
5180 const struct GNUNET_PeerIdentity *hops = (const struct GNUNET_PeerIdentity *) &dvb[1];
5181 const struct GNUNET_MessageHeader *inbox = (const struct GNUNET_MessageHeader *) &hops[num_hops];
5185 /* We're trying from the end of the hops array, as we may be
5186 able to find a shortcut unknown to the origin that way */
5187 for (int i=num_hops-1;i>=0;i--)
5189 struct Neighbour *n;
5192 GNUNET_memcmp (&hops[i],
5195 GNUNET_break_op (0);
5196 finish_cmc_handling (cmc);
5199 n = GNUNET_CONTAINER_multipeermap_get (neighbours,
5204 ntohs (dvb->total_hops) + 1,
5205 num_hops - i - 1, /* number of hops left */
5207 &hops[i+1], /* remaining hops */
5208 (const void *) &dvb[1],
5210 finish_cmc_handling (cmc);
5213 /* Woopsie, next hop not in neighbours, drop! */
5214 GNUNET_STATISTICS_update (GST_stats,
5215 "# DV Boxes dropped: next hop unknown",
5218 finish_cmc_handling (cmc);
5221 /* We are the target. Unbox and handle message. */
5222 cmc->im.sender = dvb->origin;
5223 cmc->total_hops = ntohs (dvb->total_hops);
5224 demultiplex_with_cmc (cmc,
5230 * Client notified us about transmission from a peer. Process the request.
5232 * @param cls a `struct TransportClient` which sent us the message
5233 * @param obm the send message that was sent
5234 * @return #GNUNET_YES if message is well-formed
5237 check_incoming_msg (void *cls,
5238 const struct GNUNET_TRANSPORT_IncomingMessage *im)
5240 struct TransportClient *tc = cls;
5242 if (CT_COMMUNICATOR != tc->type)
5245 return GNUNET_SYSERR;
5247 GNUNET_MQ_check_boxed_message (im);
5253 * Communicator gave us a transport address validation challenge. Process the request.
5255 * @param cls a `struct CommunicatorMessageContext` (must call #finish_cmc_handling() when done)
5256 * @param tvc the message that was received
5259 handle_validation_challenge (void *cls,
5260 const struct TransportValidationChallenge *tvc)
5262 struct CommunicatorMessageContext *cmc = cls;
5263 struct TransportValidationResponse *tvr;
5265 if (cmc->total_hops > 0)
5267 /* DV routing is not allowed for validation challenges! */
5268 GNUNET_break_op (0);
5269 finish_cmc_handling (cmc);
5272 tvr = GNUNET_new (struct TransportValidationResponse);
5273 tvr->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_VALIDATION_RESPONSE);
5274 tvr->header.size = htons (sizeof (*tvr));
5275 tvr->challenge = tvc->challenge;
5276 tvr->origin_time = tvc->sender_time;
5277 tvr->validity_duration = cmc->im.expected_address_validity;
5279 /* create signature */
5280 struct TransportValidationPS tvp = {
5281 .purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_CHALLENGE),
5282 .purpose.size = htonl (sizeof (tvp)),
5283 .validity_duration = tvr->validity_duration,
5284 .challenge = tvc->challenge
5287 GNUNET_assert (GNUNET_OK ==
5288 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
5292 route_message (&cmc->im.sender,
5294 finish_cmc_handling (cmc);
5299 * Closure for #check_known_challenge.
5301 struct CheckKnownChallengeContext
5304 * Set to the challenge we are looking for.
5306 const struct GNUNET_ShortHashCode *challenge;
5309 * Set to a matching validation state, if one was found.
5311 struct ValidationState *vs;
5316 * Test if the validation state in @a value matches the
5317 * challenge from @a cls.
5319 * @param cls a `struct CheckKnownChallengeContext`
5320 * @param pid unused (must match though)
5321 * @param value a `struct ValidationState`
5322 * @return #GNUNET_OK if not matching, #GNUNET_NO if match found
5325 check_known_challenge (void *cls,
5326 const struct GNUNET_PeerIdentity *pid,
5329 struct CheckKnownChallengeContext *ckac = cls;
5330 struct ValidationState *vs = value;
5333 if (0 != GNUNET_memcmp (&vs->challenge,
5342 * Function called when peerstore is done storing a
5343 * validated address.
5345 * @param cls a `struct ValidationState`
5346 * @param success #GNUNET_YES on success
5349 peerstore_store_validation_cb (void *cls,
5352 struct ValidationState *vs = cls;
5355 if (GNUNET_YES == success)
5357 GNUNET_STATISTICS_update (GST_stats,
5358 "# Peerstore failed to store foreign address",
5365 * Task run periodically to validate some address based on #validation_heap.
5370 validation_start_cb (void *cls);
5374 * Set the time for next_challenge of @a vs to @a new_time.
5375 * Updates the heap and if necessary reschedules the job.
5377 * @param vs validation state to update
5378 * @param new_time new time for revalidation
5381 update_next_challenge_time (struct ValidationState *vs,
5382 struct GNUNET_TIME_Absolute new_time)
5384 struct GNUNET_TIME_Relative delta;
5386 if (new_time.abs_value_us == vs->next_challenge.abs_value_us)
5387 return; /* be lazy */
5388 vs->next_challenge = new_time;
5390 vs->hn = GNUNET_CONTAINER_heap_insert (validation_heap,
5392 new_time.abs_value_us);
5394 GNUNET_CONTAINER_heap_update_cost (vs->hn,
5395 new_time.abs_value_us);
5396 if ( (vs != GNUNET_CONTAINER_heap_peek (validation_heap)) &&
5397 (NULL != validation_task) )
5399 if (NULL != validation_task)
5400 GNUNET_SCHEDULER_cancel (validation_task);
5401 /* randomize a bit */
5402 delta.rel_value_us = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
5403 MIN_DELAY_ADDRESS_VALIDATION.rel_value_us);
5404 new_time = GNUNET_TIME_absolute_add (new_time,
5406 validation_task = GNUNET_SCHEDULER_add_at (new_time,
5407 &validation_start_cb,
5413 * Find the queue matching @a pid and @a address.
5415 * @param pid peer the queue must go to
5416 * @param address address the queue must use
5417 * @return NULL if no such queue exists
5419 static struct Queue *
5420 find_queue (const struct GNUNET_PeerIdentity *pid,
5421 const char *address)
5423 struct Neighbour *n;
5425 n = GNUNET_CONTAINER_multipeermap_get (neighbours,
5429 for (struct Queue *pos = n->queue_head;
5431 pos = pos->next_neighbour)
5433 if (0 == strcmp (pos->address,
5442 * Task run periodically to check whether the validity of the given queue has
5443 * run its course. If so, finds either another queue to take over, or clears
5444 * the neighbour's `core_visible` flag. In the latter case, gives DV routes a
5445 * chance to take over, and if that fails, notifies CORE about the disconnect.
5447 * @param cls a `struct Queue`
5450 core_queue_visibility_check (void *cls)
5452 struct Queue *q = cls;
5454 q->visibility_task = NULL;
5455 if (0 != GNUNET_TIME_absolute_get_remaining (q->validated_until).rel_value_us)
5458 = GNUNET_SCHEDULER_add_at (q->validated_until,
5459 &core_queue_visibility_check,
5463 update_neighbour_core_visibility (q->neighbour);
5468 * Check whether the CORE visibility of @a n should change. Finds either a
5469 * queue to preserve the visibility, or clears the neighbour's `core_visible`
5470 * flag. In the latter case, gives DV routes a chance to take over, and if
5471 * that fails, notifies CORE about the disconnect. If so, check whether we
5472 * need to notify CORE.
5474 * @param n neighbour to perform the check for
5477 update_neighbour_core_visibility (struct Neighbour *n)
5479 struct DistanceVector *dv;
5481 GNUNET_assert (GNUNET_YES == n->core_visible);
5482 /* Check if _any_ queue of this neighbour is still valid, if so, schedule
5483 the #core_queue_visibility_check() task for that queue */
5484 for (struct Queue *q = n->queue_head;
5486 q = q->next_neighbour)
5488 if (0 != GNUNET_TIME_absolute_get_remaining (q->validated_until).rel_value_us)
5490 /* found a valid queue, use this one */
5492 = GNUNET_SCHEDULER_add_at (q->validated_until,
5493 &core_queue_visibility_check,
5498 n->core_visible = GNUNET_NO;
5500 /* Check if _any_ DV route to this neighbour is currently
5501 valid, if so, do NOT tell core about the loss of direct
5502 connectivity (DV still counts!) */
5503 dv = GNUNET_CONTAINER_multipeermap_get (dv_routes,
5505 if (GNUNET_YES == dv->core_visible)
5507 /* Nothing works anymore, need to tell CORE about the loss of
5509 cores_send_disconnect_info (&n->pid);
5514 * Communicator gave us a transport address validation response. Process the request.
5516 * @param cls a `struct CommunicatorMessageContext` (must call #finish_cmc_handling() when done)
5517 * @param tvr the message that was received
5520 handle_validation_response (void *cls,
5521 const struct TransportValidationResponse *tvr)
5523 struct CommunicatorMessageContext *cmc = cls;
5524 struct ValidationState *vs;
5525 struct CheckKnownChallengeContext ckac = {
5526 .challenge = &tvr->challenge,
5529 struct GNUNET_TIME_Absolute origin_time;
5531 struct DistanceVector *dv;
5533 /* check this is one of our challenges */
5534 (void) GNUNET_CONTAINER_multipeermap_get_multiple (validation_map,
5536 &check_known_challenge,
5538 if (NULL == (vs = ckac.vs))
5540 /* This can happen simply if we 'forgot' the challenge by now,
5541 i.e. because we received the validation response twice */
5542 GNUNET_STATISTICS_update (GST_stats,
5543 "# Validations dropped, challenge unknown",
5546 finish_cmc_handling (cmc);
5550 /* sanity check on origin time */
5551 origin_time = GNUNET_TIME_absolute_ntoh (tvr->origin_time);
5552 if ( (origin_time.abs_value_us < vs->first_challenge_use.abs_value_us) ||
5553 (origin_time.abs_value_us > vs->last_challenge_use.abs_value_us) )
5555 GNUNET_break_op (0);
5556 finish_cmc_handling (cmc);
5561 /* check signature */
5562 struct TransportValidationPS tvp = {
5563 .purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_CHALLENGE),
5564 .purpose.size = htonl (sizeof (tvp)),
5565 .validity_duration = tvr->validity_duration,
5566 .challenge = tvr->challenge
5570 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_CHALLENGE,
5573 &cmc->im.sender.public_key))
5575 GNUNET_break_op (0);
5576 finish_cmc_handling (cmc);
5581 /* validity is capped by our willingness to keep track of the
5582 validation entry and the maximum the other peer allows */
5584 = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_min (GNUNET_TIME_relative_ntoh (tvr->validity_duration),
5585 MAX_ADDRESS_VALID_UNTIL));
5587 = GNUNET_TIME_absolute_min (vs->valid_until,
5588 GNUNET_TIME_relative_to_absolute (ADDRESS_VALIDATION_LIFETIME));
5589 vs->validation_rtt = GNUNET_TIME_absolute_get_duration (origin_time);
5590 vs->challenge_backoff = GNUNET_TIME_UNIT_ZERO;
5591 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
5593 sizeof (vs->challenge));
5594 vs->first_challenge_use = GNUNET_TIME_absolute_subtract (vs->validated_until,
5595 GNUNET_TIME_relative_multiply (vs->validation_rtt,
5596 VALIDATION_RTT_BUFFER_FACTOR));
5597 vs->last_challenge_use = GNUNET_TIME_UNIT_ZERO_ABS; /* challenge was not yet used */
5598 update_next_challenge_time (vs,
5599 vs->first_challenge_use);
5600 vs->sc = GNUNET_PEERSTORE_store (peerstore,
5603 GNUNET_PEERSTORE_TRANSPORT_URLADDRESS_KEY,
5605 strlen (vs->address) + 1,
5607 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
5608 &peerstore_store_validation_cb,
5610 finish_cmc_handling (cmc);
5612 /* Finally, we now possibly have a confirmed (!) working queue,
5613 update queue status (if queue still is around) */
5614 q = find_queue (&vs->pid,
5618 GNUNET_STATISTICS_update (GST_stats,
5619 "# Queues lost at time of successful validation",
5624 q->validated_until = vs->validated_until;
5625 q->rtt = vs->validation_rtt;
5626 if (GNUNET_NO != q->neighbour->core_visible)
5627 return; /* nothing changed, we are done here */
5628 q->neighbour->core_visible = GNUNET_YES;
5630 = GNUNET_SCHEDULER_add_at (q->validated_until,
5631 &core_queue_visibility_check,
5633 /* Check if _any_ DV route to this neighbour is
5634 currently valid, if so, do NOT tell core anything! */
5635 dv = GNUNET_CONTAINER_multipeermap_get (dv_routes,
5636 &q->neighbour->pid);
5637 if (GNUNET_YES == dv->core_visible)
5638 return; /* nothing changed, done */
5639 /* We lacked a confirmed connection to the neighbour
5640 before, so tell CORE about it (finally!) */
5641 cores_send_connect_info (&q->neighbour->pid,
5642 GNUNET_BANDWIDTH_ZERO);
5647 * Incoming meessage. Process the request.
5649 * @param im the send message that was received
5652 handle_incoming_msg (void *cls,
5653 const struct GNUNET_TRANSPORT_IncomingMessage *im)
5655 struct TransportClient *tc = cls;
5656 struct CommunicatorMessageContext *cmc = GNUNET_new (struct CommunicatorMessageContext);
5660 demultiplex_with_cmc (cmc,
5661 (const struct GNUNET_MessageHeader *) &im[1]);
5666 * Given an inbound message @a msg from a communicator @a cmc,
5667 * demultiplex it based on the type calling the right handler.
5669 * @param cmc context for demultiplexing
5670 * @param msg message to demultiplex
5673 demultiplex_with_cmc (struct CommunicatorMessageContext *cmc,
5674 const struct GNUNET_MessageHeader *msg)
5676 struct GNUNET_MQ_MessageHandler handlers[] = {
5677 GNUNET_MQ_hd_var_size (fragment_box,
5678 GNUNET_MESSAGE_TYPE_TRANSPORT_FRAGMENT,
5679 struct TransportFragmentBox,
5681 GNUNET_MQ_hd_fixed_size (fragment_ack,
5682 GNUNET_MESSAGE_TYPE_TRANSPORT_FRAGMENT_ACK,
5683 struct TransportFragmentAckMessage,
5685 GNUNET_MQ_hd_var_size (reliability_box,
5686 GNUNET_MESSAGE_TYPE_TRANSPORT_RELIABILITY_BOX,
5687 struct TransportReliabilityBox,
5689 GNUNET_MQ_hd_fixed_size (reliability_ack,
5690 GNUNET_MESSAGE_TYPE_TRANSPORT_RELIABILITY_ACK,
5691 struct TransportReliabilityAckMessage,
5693 GNUNET_MQ_hd_var_size (backchannel_encapsulation,
5694 GNUNET_MESSAGE_TYPE_TRANSPORT_BACKCHANNEL_ENCAPSULATION,
5695 struct TransportBackchannelEncapsulationMessage,
5697 GNUNET_MQ_hd_var_size (dv_learn,
5698 GNUNET_MESSAGE_TYPE_TRANSPORT_DV_LEARN,
5699 struct TransportDVLearn,
5701 GNUNET_MQ_hd_var_size (dv_box,
5702 GNUNET_MESSAGE_TYPE_TRANSPORT_DV_BOX,
5703 struct TransportDVBox,
5705 GNUNET_MQ_hd_fixed_size (validation_challenge,
5706 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_VALIDATION_CHALLENGE,
5707 struct TransportValidationChallenge,
5709 GNUNET_MQ_hd_fixed_size (validation_response,
5710 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_VALIDATION_RESPONSE,
5711 struct TransportValidationResponse,
5713 GNUNET_MQ_handler_end()
5717 ret = GNUNET_MQ_handle_message (handlers,
5719 if (GNUNET_SYSERR == ret)
5722 GNUNET_SERVICE_client_drop (cmc->tc->client);
5726 if (GNUNET_NO == ret)
5728 /* unencapsulated 'raw' message */
5729 handle_raw_message (&cmc,
5736 * New queue became available. Check message.
5738 * @param cls the client
5739 * @param aqm the send message that was sent
5742 check_add_queue_message (void *cls,
5743 const struct GNUNET_TRANSPORT_AddQueueMessage *aqm)
5745 struct TransportClient *tc = cls;
5747 if (CT_COMMUNICATOR != tc->type)
5750 return GNUNET_SYSERR;
5752 GNUNET_MQ_check_zero_termination (aqm);
5758 * Bandwidth tracker informs us that the delay until we should receive
5761 * @param cls a `struct Queue` for which the delay changed
5764 tracker_update_in_cb (void *cls)
5766 struct Queue *queue = cls;
5767 struct GNUNET_TIME_Relative in_delay;
5770 rsize = (0 == queue->mtu) ? IN_PACKET_SIZE_WITHOUT_MTU : queue->mtu;
5771 in_delay = GNUNET_BANDWIDTH_tracker_get_delay (&queue->tracker_in,
5773 // FIXME: how exactly do we do inbound flow control?
5778 * If necessary, generates the UUID for a @a pm
5780 * @param pm pending message to generate UUID for.
5783 set_pending_message_uuid (struct PendingMessage *pm)
5785 if (pm->msg_uuid_set)
5787 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
5789 sizeof (pm->msg_uuid));
5790 pm->msg_uuid_set = GNUNET_YES;
5795 * Fragment the given @a pm to the given @a mtu. Adds
5796 * additional fragments to the neighbour as well. If the
5797 * @a mtu is too small, generates and error for the @a pm
5800 * @param pm pending message to fragment for transmission
5801 * @param mtu MTU to apply
5802 * @return new message to transmit
5804 static struct PendingMessage *
5805 fragment_message (struct PendingMessage *pm,
5808 struct PendingMessage *ff;
5810 set_pending_message_uuid (pm);
5812 /* This invariant is established in #handle_add_queue_message() */
5813 GNUNET_assert (mtu > sizeof (struct TransportFragmentBox));
5815 /* select fragment for transmission, descending the tree if it has
5816 been expanded until we are at a leaf or at a fragment that is small enough */
5818 while ( ( (ff->bytes_msg > mtu) ||
5820 (ff->frag_off == ff->bytes_msg) &&
5821 (NULL != ff->head_frag) )
5823 ff = ff->head_frag; /* descent into fragmented fragments */
5826 if ( ( (ff->bytes_msg > mtu) ||
5828 (pm->frag_off < pm->bytes_msg) )
5830 /* Did not yet calculate all fragments, calculate next fragment */
5831 struct PendingMessage *frag;
5832 struct TransportFragmentBox tfb;
5840 orig = (const char *) &ff[1];
5841 msize = ff->bytes_msg;
5844 const struct TransportFragmentBox *tfbo;
5846 tfbo = (const struct TransportFragmentBox *) orig;
5847 orig += sizeof (struct TransportFragmentBox);
5848 msize -= sizeof (struct TransportFragmentBox);
5849 xoff = ntohs (tfbo->frag_off);
5851 fragmax = mtu - sizeof (struct TransportFragmentBox);
5852 fragsize = GNUNET_MIN (msize - ff->frag_off,
5854 frag = GNUNET_malloc (sizeof (struct PendingMessage) +
5855 sizeof (struct TransportFragmentBox) +
5857 frag->target = pm->target;
5858 frag->frag_parent = ff;
5859 frag->timeout = pm->timeout;
5860 frag->bytes_msg = sizeof (struct TransportFragmentBox) + fragsize;
5861 frag->pmt = PMT_FRAGMENT_BOX;
5862 msg = (char *) &frag[1];
5863 tfb.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_FRAGMENT);
5864 tfb.header.size = htons (sizeof (struct TransportFragmentBox) +
5866 tfb.frag_uuid = htonl (pm->frag_uuidgen++);
5867 tfb.msg_uuid = pm->msg_uuid;
5868 tfb.frag_off = htons (ff->frag_off + xoff);
5869 tfb.msg_size = htons (pm->bytes_msg);
5873 memcpy (&msg[sizeof (tfb)],
5874 &orig[ff->frag_off],
5876 GNUNET_CONTAINER_MDLL_insert (frag,
5880 ff->frag_off += fragsize;
5884 /* Move head to the tail and return it */
5885 GNUNET_CONTAINER_MDLL_remove (frag,
5886 ff->frag_parent->head_frag,
5887 ff->frag_parent->tail_frag,
5889 GNUNET_CONTAINER_MDLL_insert_tail (frag,
5890 ff->frag_parent->head_frag,
5891 ff->frag_parent->tail_frag,
5898 * Reliability-box the given @a pm. On error (can there be any), NULL
5899 * may be returned, otherwise the "replacement" for @a pm (which
5900 * should then be added to the respective neighbour's queue instead of
5901 * @a pm). If the @a pm is already fragmented or reliability boxed,
5902 * or itself an ACK, this function simply returns @a pm.
5904 * @param pm pending message to box for transmission over unreliabile queue
5905 * @return new message to transmit
5907 static struct PendingMessage *
5908 reliability_box_message (struct PendingMessage *pm)
5910 struct TransportReliabilityBox rbox;
5911 struct PendingMessage *bpm;
5914 if (PMT_CORE != pm->pmt)
5915 return pm; /* already fragmented or reliability boxed, or control message: do nothing */
5916 if (NULL != pm->bpm)
5917 return pm->bpm; /* already computed earlier: do nothing */
5918 GNUNET_assert (NULL == pm->head_frag);
5919 if (pm->bytes_msg + sizeof (rbox) > UINT16_MAX)
5923 client_send_response (pm,
5928 bpm = GNUNET_malloc (sizeof (struct PendingMessage) +
5931 bpm->target = pm->target;
5932 bpm->frag_parent = pm;
5933 GNUNET_CONTAINER_MDLL_insert (frag,
5937 bpm->timeout = pm->timeout;
5938 bpm->pmt = PMT_RELIABILITY_BOX;
5939 bpm->bytes_msg = pm->bytes_msg + sizeof (rbox);
5940 set_pending_message_uuid (bpm);
5941 rbox.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RELIABILITY_BOX);
5942 rbox.header.size = htons (sizeof (rbox) + pm->bytes_msg);
5943 rbox.ack_countdown = htonl (0); // FIXME: implement ACK countdown support
5944 rbox.msg_uuid = pm->msg_uuid;
5945 msg = (char *) &bpm[1];
5949 memcpy (&msg[sizeof (rbox)],
5958 * We believe we are ready to transmit a message on a queue. Double-checks
5959 * with the queue's "tracker_out" and then gives the message to the
5960 * communicator for transmission (updating the tracker, and re-scheduling
5961 * itself if applicable).
5963 * @param cls the `struct Queue` to process transmissions for
5966 transmit_on_queue (void *cls)
5968 struct Queue *queue = cls;
5969 struct Neighbour *n = queue->neighbour;
5970 struct PendingMessage *pm;
5971 struct PendingMessage *s;
5974 queue->transmit_task = NULL;
5975 if (NULL == (pm = n->pending_msg_head))
5977 /* no message pending, nothing to do here! */
5980 schedule_transmit_on_queue (queue);
5981 if (NULL != queue->transmit_task)
5982 return; /* do it later */
5984 if (GNUNET_TRANSPORT_CC_RELIABLE != queue->tc->details.communicator.cc)
5985 overhead += sizeof (struct TransportReliabilityBox);
5987 if ( ( (0 != queue->mtu) &&
5988 (pm->bytes_msg + overhead > queue->mtu) ) ||
5989 (pm->bytes_msg > UINT16_MAX - sizeof (struct GNUNET_TRANSPORT_SendMessageTo)) ||
5990 (NULL != pm->head_frag /* fragments already exist, should
5991 respect that even if MTU is 0 for
5993 s = fragment_message (s,
5995 ? UINT16_MAX - sizeof (struct GNUNET_TRANSPORT_SendMessageTo)
5999 /* Fragmentation failed, try next message... */
6000 schedule_transmit_on_queue (queue);
6003 if (GNUNET_TRANSPORT_CC_RELIABLE != queue->tc->details.communicator.cc)
6004 s = reliability_box_message (s);
6007 /* Reliability boxing failed, try next message... */
6008 schedule_transmit_on_queue (queue);
6012 /* Pass 's' for transission to the communicator */
6013 queue_send_msg (queue,
6017 // FIXME: do something similar to the logic below
6018 // in defragmentation / reliability ACK handling!
6020 /* Check if this transmission somehow conclusively finished handing 'pm'
6021 even without any explicit ACKs */
6022 if ( (PMT_CORE == s->pmt) &&
6023 (GNUNET_TRANSPORT_CC_RELIABLE == queue->tc->details.communicator.cc) )
6025 /* Full message sent, and over reliabile channel */
6026 client_send_response (pm,
6030 else if ( (GNUNET_TRANSPORT_CC_RELIABLE == queue->tc->details.communicator.cc) &&
6031 (PMT_FRAGMENT_BOX == s->pmt) )
6033 struct PendingMessage *pos;
6035 /* Fragment sent over reliabile channel */
6036 free_fragment_tree (s);
6037 pos = s->frag_parent;
6038 GNUNET_CONTAINER_MDLL_remove (frag,
6043 /* check if subtree is done */
6044 while ( (NULL == pos->head_frag) &&
6045 (pos->frag_off == pos->bytes_msg) &&
6049 pos = s->frag_parent;
6050 GNUNET_CONTAINER_MDLL_remove (frag,
6057 /* Was this the last applicable fragmment? */
6058 if ( (NULL == pm->head_frag) &&
6059 (pm->frag_off == pm->bytes_msg) )
6060 client_send_response (pm,
6062 pm->bytes_msg /* FIXME: calculate and add overheads! */);
6064 else if (PMT_CORE != pm->pmt)
6066 /* This was an acknowledgement of some type, always free */
6067 free_pending_message (pm);
6071 /* message not finished, waiting for acknowledgement */
6072 struct Neighbour *neighbour = pm->target;
6073 /* Update time by which we might retransmit 's' based on queue
6074 characteristics (i.e. RTT); it takes one RTT for the message to
6075 arrive and the ACK to come back in the best case; but the other
6076 side is allowed to delay ACKs by 2 RTTs, so we use 4 RTT before
6077 retransmitting. Note that in the future this heuristic should
6078 likely be improved further (measure RTT stability, consider
6079 message urgency and size when delaying ACKs, etc.) */
6080 s->next_attempt = GNUNET_TIME_relative_to_absolute
6081 (GNUNET_TIME_relative_multiply (queue->rtt,
6085 struct PendingMessage *pos;
6087 /* re-insert sort in neighbour list */
6088 GNUNET_CONTAINER_MDLL_remove (neighbour,
6089 neighbour->pending_msg_head,
6090 neighbour->pending_msg_tail,
6092 pos = neighbour->pending_msg_tail;
6093 while ( (NULL != pos) &&
6094 (pm->next_attempt.abs_value_us > pos->next_attempt.abs_value_us) )
6095 pos = pos->prev_neighbour;
6096 GNUNET_CONTAINER_MDLL_insert_after (neighbour,
6097 neighbour->pending_msg_head,
6098 neighbour->pending_msg_tail,
6104 /* re-insert sort in fragment list */
6105 struct PendingMessage *fp = s->frag_parent;
6106 struct PendingMessage *pos;
6108 GNUNET_CONTAINER_MDLL_remove (frag,
6112 pos = fp->tail_frag;
6113 while ( (NULL != pos) &&
6114 (s->next_attempt.abs_value_us > pos->next_attempt.abs_value_us) )
6115 pos = pos->prev_frag;
6116 GNUNET_CONTAINER_MDLL_insert_after (frag,
6124 /* finally, re-schedule queue transmission task itself */
6125 schedule_transmit_on_queue (queue);
6130 * Bandwidth tracker informs us that the delay until we
6131 * can transmit again changed.
6133 * @param cls a `struct Queue` for which the delay changed
6136 tracker_update_out_cb (void *cls)
6138 struct Queue *queue = cls;
6139 struct Neighbour *n = queue->neighbour;
6141 if (NULL == n->pending_msg_head)
6143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6144 "Bandwidth allocation updated for empty transmission queue `%s'\n",
6146 return; /* no message pending, nothing to do here! */
6148 GNUNET_SCHEDULER_cancel (queue->transmit_task);
6149 queue->transmit_task = NULL;
6150 schedule_transmit_on_queue (queue);
6155 * Bandwidth tracker informs us that excessive outbound bandwidth was
6156 * allocated which is not being used.
6158 * @param cls a `struct Queue` for which the excess was noted
6161 tracker_excess_out_cb (void *cls)
6165 /* FIXME: trigger excess bandwidth report to core? Right now,
6166 this is done internally within transport_api2_core already,
6167 but we probably want to change the logic and trigger it
6168 from here via a message instead! */
6169 /* TODO: maybe inform someone at this point? */
6170 GNUNET_STATISTICS_update (GST_stats,
6171 "# Excess outbound bandwidth reported",
6179 * Bandwidth tracker informs us that excessive inbound bandwidth was allocated
6180 * which is not being used.
6182 * @param cls a `struct Queue` for which the excess was noted
6185 tracker_excess_in_cb (void *cls)
6189 /* TODO: maybe inform somone at this point? */
6190 GNUNET_STATISTICS_update (GST_stats,
6191 "# Excess inbound bandwidth reported",
6198 * Queue to a peer went down. Process the request.
6200 * @param cls the client
6201 * @param dqm the send message that was sent
6204 handle_del_queue_message (void *cls,
6205 const struct GNUNET_TRANSPORT_DelQueueMessage *dqm)
6207 struct TransportClient *tc = cls;
6209 if (CT_COMMUNICATOR != tc->type)
6212 GNUNET_SERVICE_client_drop (tc->client);
6215 for (struct Queue *queue = tc->details.communicator.queue_head;
6217 queue = queue->next_client)
6219 struct Neighbour *neighbour = queue->neighbour;
6221 if ( (dqm->qid != queue->qid) ||
6222 (0 != GNUNET_memcmp (&dqm->receiver,
6226 GNUNET_SERVICE_client_continue (tc->client);
6230 GNUNET_SERVICE_client_drop (tc->client);
6235 * Message was transmitted. Process the request.
6237 * @param cls the client
6238 * @param sma the send message that was sent
6241 handle_send_message_ack (void *cls,
6242 const struct GNUNET_TRANSPORT_SendMessageToAck *sma)
6244 struct TransportClient *tc = cls;
6245 struct QueueEntry *qe;
6247 if (CT_COMMUNICATOR != tc->type)
6250 GNUNET_SERVICE_client_drop (tc->client);
6254 /* find our queue entry matching the ACK */
6256 for (struct Queue *queue = tc->details.communicator.queue_head;
6258 queue = queue->next_client)
6260 if (0 != GNUNET_memcmp (&queue->neighbour->pid,
6263 for (struct QueueEntry *qep = queue->queue_head;
6267 if (qep->mid != sma->mid)
6276 /* this should never happen */
6278 GNUNET_SERVICE_client_drop (tc->client);
6281 GNUNET_CONTAINER_DLL_remove (qe->queue->queue_head,
6282 qe->queue->queue_tail,
6284 qe->queue->queue_length--;
6285 tc->details.communicator.total_queue_length--;
6286 GNUNET_SERVICE_client_continue (tc->client);
6288 /* if applicable, resume transmissions that waited on ACK */
6289 if (COMMUNICATOR_TOTAL_QUEUE_LIMIT - 1 == tc->details.communicator.total_queue_length)
6291 /* Communicator dropped below threshold, resume all queues */
6292 GNUNET_STATISTICS_update (GST_stats,
6293 "# Transmission throttled due to communicator queue limit",
6296 for (struct Queue *queue = tc->details.communicator.queue_head;
6298 queue = queue->next_client)
6299 schedule_transmit_on_queue (queue);
6301 else if (QUEUE_LENGTH_LIMIT - 1 == qe->queue->queue_length)
6303 /* queue dropped below threshold; only resume this one queue */
6304 GNUNET_STATISTICS_update (GST_stats,
6305 "# Transmission throttled due to queue queue limit",
6308 schedule_transmit_on_queue (qe->queue);
6311 /* TODO: we also should react on the status! */
6312 // FIXME: this probably requires queue->pm = s assignment!
6313 // FIXME: react to communicator status about transmission request. We got:
6314 sma->status; // OK success, SYSERR failure
6321 * Iterator telling new MONITOR client about all existing
6324 * @param cls the new `struct TransportClient`
6325 * @param pid a connected peer
6326 * @param value the `struct Neighbour` with more information
6327 * @return #GNUNET_OK (continue to iterate)
6330 notify_client_queues (void *cls,
6331 const struct GNUNET_PeerIdentity *pid,
6334 struct TransportClient *tc = cls;
6335 struct Neighbour *neighbour = value;
6337 GNUNET_assert (CT_MONITOR == tc->type);
6338 for (struct Queue *q = neighbour->queue_head;
6340 q = q->next_neighbour)
6342 struct MonitorEvent me = {
6345 .num_msg_pending = q->num_msg_pending,
6346 .num_bytes_pending = q->num_bytes_pending
6360 * Initialize a monitor client.
6362 * @param cls the client
6363 * @param start the start message that was sent
6366 handle_monitor_start (void *cls,
6367 const struct GNUNET_TRANSPORT_MonitorStart *start)
6369 struct TransportClient *tc = cls;
6371 if (CT_NONE != tc->type)
6374 GNUNET_SERVICE_client_drop (tc->client);
6377 tc->type = CT_MONITOR;
6378 tc->details.monitor.peer = start->peer;
6379 tc->details.monitor.one_shot = ntohl (start->one_shot);
6380 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
6381 ¬ify_client_queues,
6383 GNUNET_SERVICE_client_mark_monitor (tc->client);
6384 GNUNET_SERVICE_client_continue (tc->client);
6389 * Find transport client providing communication service
6390 * for the protocol @a prefix.
6392 * @param prefix communicator name
6393 * @return NULL if no such transport client is available
6395 static struct TransportClient *
6396 lookup_communicator (const char *prefix)
6398 for (struct TransportClient *tc = clients_head;
6402 if (CT_COMMUNICATOR != tc->type)
6404 if (0 == strcmp (prefix,
6405 tc->details.communicator.address_prefix))
6408 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6409 "Somone suggested use of communicator for `%s', but we do not have such a communicator!\n",
6416 * Signature of a function called with a communicator @a address of a peer
6417 * @a pid that an application wants us to connect to.
6419 * @param pid target peer
6420 * @param address the address to try
6423 suggest_to_connect (const struct GNUNET_PeerIdentity *pid,
6424 const char *address)
6426 static uint32_t idgen;
6427 struct TransportClient *tc;
6429 struct GNUNET_TRANSPORT_CreateQueue *cqm;
6430 struct GNUNET_MQ_Envelope *env;
6433 prefix = GNUNET_HELLO_address_to_prefix (address);
6436 GNUNET_break (0); /* We got an invalid address!? */
6439 tc = lookup_communicator (prefix);
6442 GNUNET_STATISTICS_update (GST_stats,
6443 "# Suggestions ignored due to missing communicator",
6448 /* forward suggestion for queue creation to communicator */
6449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6450 "Request #%u for `%s' communicator to create queue to `%s'\n",
6451 (unsigned int) idgen,
6454 alen = strlen (address) + 1;
6455 env = GNUNET_MQ_msg_extra (cqm,
6457 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE);
6458 cqm->request_id = htonl (idgen++);
6459 cqm->receiver = *pid;
6463 GNUNET_MQ_send (tc->mq,
6469 * The queue @a q (which matches the peer and address in @a vs) is
6470 * ready for queueing. We should now queue the validation request.
6472 * @param q queue to send on
6473 * @param vs state to derive validation challenge from
6476 validation_transmit_on_queue (struct Queue *q,
6477 struct ValidationState *vs)
6479 struct TransportValidationChallenge tvc;
6481 vs->last_challenge_use = GNUNET_TIME_absolute_get ();
6482 tvc.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_VALIDATION_CHALLENGE);
6483 tvc.header.size = htons (sizeof (tvc));
6484 tvc.reserved = htonl (0);
6485 tvc.challenge = vs->challenge;
6486 tvc.sender_time = GNUNET_TIME_absolute_hton (vs->last_challenge_use);
6495 * Task run periodically to validate some address based on #validation_heap.
6500 validation_start_cb (void *cls)
6502 struct ValidationState *vs;
6506 validation_task = NULL;
6507 vs = GNUNET_CONTAINER_heap_peek (validation_heap);
6508 /* drop validations past their expiration */
6509 while ( (NULL != vs) &&
6510 (0 == GNUNET_TIME_absolute_get_remaining (vs->valid_until).rel_value_us) )
6512 free_validation_state (vs);
6513 vs = GNUNET_CONTAINER_heap_peek (validation_heap);
6516 return; /* woopsie, no more addresses known, should only
6517 happen if we're really a lonely peer */
6518 q = find_queue (&vs->pid,
6522 vs->awaiting_queue = GNUNET_YES;
6523 suggest_to_connect (&vs->pid,
6527 validation_transmit_on_queue (q,
6529 /* Finally, reschedule next attempt */
6530 vs->challenge_backoff = GNUNET_TIME_randomized_backoff (vs->challenge_backoff,
6531 MAX_VALIDATION_CHALLENGE_FREQ);
6532 update_next_challenge_time (vs,
6533 GNUNET_TIME_relative_to_absolute (vs->challenge_backoff));
6538 * Closure for #check_connection_quality.
6540 struct QueueQualityContext
6543 * Set to the @e k'th queue encountered.
6548 * Set to the number of quality queues encountered.
6550 unsigned int quality_count;
6553 * Set to the total number of queues encountered.
6555 unsigned int num_queues;
6558 * Decremented for each queue, for selection of the
6559 * k-th queue in @e q.
6567 * Check whether any queue to the given neighbour is
6568 * of a good "quality" and if so, increment the counter.
6569 * Also counts the total number of queues, and returns
6570 * the k-th queue found.
6572 * @param cls a `struct QueueQualityContext *` with counters
6573 * @param pid peer this is about
6574 * @param value a `struct Neighbour`
6575 * @return #GNUNET_OK (continue to iterate)
6578 check_connection_quality (void *cls,
6579 const struct GNUNET_PeerIdentity *pid,
6582 struct QueueQualityContext *ctx = cls;
6583 struct Neighbour *n = value;
6588 for (struct Queue *q = n->queue_head;
6590 q = q->next_neighbour)
6592 if (0 != q->distance)
6593 continue; /* DV does not count */
6597 /* OPTIMIZE-FIXME: in the future, add reliability / goodput
6598 statistics and consider those as well here? */
6599 if (q->rtt.rel_value_us < DV_QUALITY_RTT_THRESHOLD.rel_value_us)
6600 do_inc = GNUNET_YES;
6602 if (GNUNET_YES == do_inc)
6603 ctx->quality_count++;
6609 * Task run when we CONSIDER initiating a DV learn
6610 * process. We first check that sending out a message is
6611 * even possible (queues exist), then that it is desirable
6612 * (if not, reschedule the task for later), and finally
6613 * we may then begin the job. If there are too many
6614 * entries in the #dvlearn_map, we purge the oldest entry
6620 start_dv_learn (void *cls)
6622 struct LearnLaunchEntry *lle;
6623 struct QueueQualityContext qqc;
6624 struct TransportDVLearn dvl;
6627 dvlearn_task = NULL;
6629 GNUNET_CONTAINER_multipeermap_size (neighbours))
6630 return; /* lost all connectivity, cannot do learning */
6631 qqc.quality_count = 0;
6633 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
6634 &check_connection_quality,
6636 if (qqc.quality_count > DV_LEARN_QUALITY_THRESHOLD)
6638 struct GNUNET_TIME_Relative delay;
6639 unsigned int factor;
6641 /* scale our retries by how far we are above the threshold */
6642 factor = qqc.quality_count / DV_LEARN_QUALITY_THRESHOLD;
6643 delay = GNUNET_TIME_relative_multiply (DV_LEARN_BASE_FREQUENCY,
6645 dvlearn_task = GNUNET_SCHEDULER_add_delayed (delay,
6650 /* remove old entries in #dvlearn_map if it has grown too big */
6651 while (MAX_DV_LEARN_PENDING >=
6652 GNUNET_CONTAINER_multishortmap_size (dvlearn_map))
6655 GNUNET_assert (GNUNET_YES ==
6656 GNUNET_CONTAINER_multishortmap_remove (dvlearn_map,
6659 GNUNET_CONTAINER_DLL_remove (lle_head,
6664 /* setup data structure for learning */
6665 lle = GNUNET_new (struct LearnLaunchEntry);
6666 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
6668 sizeof (lle->challenge));
6669 GNUNET_CONTAINER_DLL_insert (lle_head,
6672 GNUNET_break (GNUNET_YES ==
6673 GNUNET_CONTAINER_multishortmap_put (dvlearn_map,
6676 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
6677 dvl.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DV_LEARN);
6678 dvl.header.size = htons (sizeof (dvl));
6679 dvl.num_hops = htons (0);
6680 dvl.bidirectional = htons (0);
6681 dvl.non_network_delay = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO);
6683 struct DvInitPS dvip = {
6684 .purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DV_INITIATOR),
6685 .purpose.size = htonl (sizeof (dvip)),
6686 .challenge = lle->challenge
6689 GNUNET_assert (GNUNET_OK ==
6690 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
6694 dvl.initiator = GST_my_identity;
6695 dvl.challenge = lle->challenge;
6697 qqc.quality_count = 0;
6698 qqc.k = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
6702 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
6703 &check_connection_quality,
6705 GNUNET_assert (NULL != qqc.q);
6707 /* Do this as close to transmission time as possible! */
6708 lle->launch_time = GNUNET_TIME_absolute_get ();
6710 queue_send_msg (qqc.q,
6714 /* reschedule this job, randomizing the time it runs (but no
6717 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_randomize (DV_LEARN_BASE_FREQUENCY),
6724 * A new queue has been created, check if any address validation
6725 * requests have been waiting for it.
6727 * @param cls a `struct Queue`
6728 * @param pid peer concerned (unused)
6729 * @param value a `struct ValidationState`
6730 * @return #GNUNET_NO if a match was found and we can stop looking
6733 check_validation_request_pending (void *cls,
6734 const struct GNUNET_PeerIdentity *pid,
6737 struct Queue *q = cls;
6738 struct ValidationState *vs = value;
6741 if ( (GNUNET_YES == vs->awaiting_queue) &&
6742 (0 == strcmp (vs->address,
6745 vs->awaiting_queue = GNUNET_NO;
6746 validation_transmit_on_queue (q,
6755 * New queue became available. Process the request.
6757 * @param cls the client
6758 * @param aqm the send message that was sent
6761 handle_add_queue_message (void *cls,
6762 const struct GNUNET_TRANSPORT_AddQueueMessage *aqm)
6764 struct TransportClient *tc = cls;
6765 struct Queue *queue;
6766 struct Neighbour *neighbour;
6770 if (ntohl (aqm->mtu) <= sizeof (struct TransportFragmentBox))
6772 /* MTU so small as to be useless for transmissions,
6773 required for #fragment_message()! */
6774 GNUNET_break_op (0);
6775 GNUNET_SERVICE_client_drop (tc->client);
6778 neighbour = lookup_neighbour (&aqm->receiver);
6779 if (NULL == neighbour)
6781 neighbour = GNUNET_new (struct Neighbour);
6782 neighbour->earliest_timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
6783 neighbour->pid = aqm->receiver;
6784 GNUNET_assert (GNUNET_OK ==
6785 GNUNET_CONTAINER_multipeermap_put (neighbours,
6788 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
6790 addr_len = ntohs (aqm->header.size) - sizeof (*aqm);
6791 addr = (const char *) &aqm[1];
6793 queue = GNUNET_malloc (sizeof (struct Queue) + addr_len);
6795 queue->address = (const char *) &queue[1];
6796 queue->rtt = GNUNET_TIME_UNIT_FOREVER_REL;
6797 queue->qid = aqm->qid;
6798 queue->mtu = ntohl (aqm->mtu);
6799 queue->nt = (enum GNUNET_NetworkType) ntohl (aqm->nt);
6800 queue->cs = (enum GNUNET_TRANSPORT_ConnectionStatus) ntohl (aqm->cs);
6801 queue->neighbour = neighbour;
6802 GNUNET_BANDWIDTH_tracker_init2 (&queue->tracker_in,
6803 &tracker_update_in_cb,
6805 GNUNET_BANDWIDTH_ZERO,
6806 GNUNET_CONSTANTS_MAX_BANDWIDTH_CARRY_S,
6807 &tracker_excess_in_cb,
6809 GNUNET_BANDWIDTH_tracker_init2 (&queue->tracker_out,
6810 &tracker_update_out_cb,
6812 GNUNET_BANDWIDTH_ZERO,
6813 GNUNET_CONSTANTS_MAX_BANDWIDTH_CARRY_S,
6814 &tracker_excess_out_cb,
6819 /* notify monitors about new queue */
6821 struct MonitorEvent me = {
6826 notify_monitors (&neighbour->pid,
6831 GNUNET_CONTAINER_MDLL_insert (neighbour,
6832 neighbour->queue_head,
6833 neighbour->queue_tail,
6835 GNUNET_CONTAINER_MDLL_insert (client,
6836 tc->details.communicator.queue_head,
6837 tc->details.communicator.queue_tail,
6839 /* check if valdiations are waiting for the queue */
6840 (void) GNUNET_CONTAINER_multipeermap_get_multiple (validation_map,
6842 &check_validation_request_pending,
6844 /* might be our first queue, try launching DV learning */
6845 if (NULL == dvlearn_task)
6846 dvlearn_task = GNUNET_SCHEDULER_add_now (&start_dv_learn,
6848 GNUNET_SERVICE_client_continue (tc->client);
6853 * Communicator tells us that our request to create a queue "worked", that
6854 * is setting up the queue is now in process.
6856 * @param cls the `struct TransportClient`
6857 * @param cqr confirmation message
6860 handle_queue_create_ok (void *cls,
6861 const struct GNUNET_TRANSPORT_CreateQueueResponse *cqr)
6863 struct TransportClient *tc = cls;
6865 if (CT_COMMUNICATOR != tc->type)
6868 GNUNET_SERVICE_client_drop (tc->client);
6871 GNUNET_STATISTICS_update (GST_stats,
6872 "# Suggestions succeeded at communicator",
6875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6876 "Request #%u for communicator to create queue succeeded\n",
6877 (unsigned int) ntohs (cqr->request_id));
6878 GNUNET_SERVICE_client_continue (tc->client);
6883 * Communicator tells us that our request to create a queue failed. This usually
6884 * indicates that the provided address is simply invalid or that the communicator's
6885 * resources are exhausted.
6887 * @param cls the `struct TransportClient`
6888 * @param cqr failure message
6891 handle_queue_create_fail (void *cls,
6892 const struct GNUNET_TRANSPORT_CreateQueueResponse *cqr)
6894 struct TransportClient *tc = cls;
6896 if (CT_COMMUNICATOR != tc->type)
6899 GNUNET_SERVICE_client_drop (tc->client);
6902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6903 "Request #%u for communicator to create queue failed\n",
6904 (unsigned int) ntohs (cqr->request_id));
6905 GNUNET_STATISTICS_update (GST_stats,
6906 "# Suggestions failed in queue creation at communicator",
6909 GNUNET_SERVICE_client_continue (tc->client);
6914 * We have received a `struct ExpressPreferenceMessage` from an application client.
6916 * @param cls handle to the client
6917 * @param msg the start message
6920 handle_suggest_cancel (void *cls,
6921 const struct ExpressPreferenceMessage *msg)
6923 struct TransportClient *tc = cls;
6924 struct PeerRequest *pr;
6926 if (CT_APPLICATION != tc->type)
6929 GNUNET_SERVICE_client_drop (tc->client);
6932 pr = GNUNET_CONTAINER_multipeermap_get (tc->details.application.requests,
6937 GNUNET_SERVICE_client_drop (tc->client);
6940 (void) stop_peer_request (tc,
6943 GNUNET_SERVICE_client_continue (tc->client);
6948 * Check #GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_CONSIDER_VERIFY
6949 * messages. We do nothing here, real verification is done later.
6951 * @param cls a `struct TransportClient *`
6952 * @param msg message to verify
6953 * @return #GNUNET_OK
6956 check_address_consider_verify (void *cls,
6957 const struct GNUNET_TRANSPORT_AddressToVerify *hdr)
6966 * Closure for #check_known_address.
6968 struct CheckKnownAddressContext
6971 * Set to the address we are looking for.
6973 const char *address;
6976 * Set to a matching validation state, if one was found.
6978 struct ValidationState *vs;
6983 * Test if the validation state in @a value matches the
6984 * address from @a cls.
6986 * @param cls a `struct CheckKnownAddressContext`
6987 * @param pid unused (must match though)
6988 * @param value a `struct ValidationState`
6989 * @return #GNUNET_OK if not matching, #GNUNET_NO if match found
6992 check_known_address (void *cls,
6993 const struct GNUNET_PeerIdentity *pid,
6996 struct CheckKnownAddressContext *ckac = cls;
6997 struct ValidationState *vs = value;
7000 if (0 != strcmp (vs->address,
7009 * Start address validation.
7011 * @param pid peer the @a address is for
7012 * @param address an address to reach @a pid (presumably)
7013 * @param expiration when did @a pid claim @a address will become invalid
7016 start_address_validation (const struct GNUNET_PeerIdentity *pid,
7017 const char *address,
7018 struct GNUNET_TIME_Absolute expiration)
7020 struct GNUNET_TIME_Absolute now;
7021 struct ValidationState *vs;
7022 struct CheckKnownAddressContext ckac = {
7027 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
7028 return; /* expired */
7029 (void) GNUNET_CONTAINER_multipeermap_get_multiple (validation_map,
7031 &check_known_address,
7033 if (NULL != (vs = ckac.vs))
7035 /* if 'vs' is not currently valid, we need to speed up retrying the validation */
7036 if (vs->validated_until.abs_value_us < vs->next_challenge.abs_value_us)
7038 /* reduce backoff as we got a fresh advertisement */
7039 vs->challenge_backoff = GNUNET_TIME_relative_min (FAST_VALIDATION_CHALLENGE_FREQ,
7040 GNUNET_TIME_relative_divide (vs->challenge_backoff,
7042 update_next_challenge_time (vs,
7043 GNUNET_TIME_relative_to_absolute (vs->challenge_backoff));
7047 now = GNUNET_TIME_absolute_get();
7048 vs = GNUNET_new (struct ValidationState);
7050 vs->valid_until = expiration;
7051 vs->first_challenge_use = now;
7052 vs->validation_rtt = GNUNET_TIME_UNIT_FOREVER_REL;
7053 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
7055 sizeof (vs->challenge));
7056 vs->address = GNUNET_strdup (address);
7057 GNUNET_assert (GNUNET_YES ==
7058 GNUNET_CONTAINER_multipeermap_put (validation_map,
7061 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
7062 update_next_challenge_time (vs,
7068 * Function called by PEERSTORE for each matching record.
7070 * @param cls closure
7071 * @param record peerstore record information
7072 * @param emsg error message, or NULL if no errors
7075 handle_hello (void *cls,
7076 const struct GNUNET_PEERSTORE_Record *record,
7079 struct PeerRequest *pr = cls;
7084 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7085 "Got failure from PEERSTORE: %s\n",
7089 val = record->value;
7090 if ( (0 == record->value_size) ||
7091 ('\0' != val[record->value_size - 1]) )
7096 start_address_validation (&pr->pid,
7097 (const char *) record->value,
7103 * We have received a `struct ExpressPreferenceMessage` from an application client.
7105 * @param cls handle to the client
7106 * @param msg the start message
7109 handle_suggest (void *cls,
7110 const struct ExpressPreferenceMessage *msg)
7112 struct TransportClient *tc = cls;
7113 struct PeerRequest *pr;
7115 if (CT_NONE == tc->type)
7117 tc->type = CT_APPLICATION;
7118 tc->details.application.requests
7119 = GNUNET_CONTAINER_multipeermap_create (16,
7122 if (CT_APPLICATION != tc->type)
7125 GNUNET_SERVICE_client_drop (tc->client);
7128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7129 "Client suggested we talk to %s with preference %d at rate %u\n",
7130 GNUNET_i2s (&msg->peer),
7131 (int) ntohl (msg->pk),
7132 (int) ntohl (msg->bw.value__));
7133 pr = GNUNET_new (struct PeerRequest);
7135 pr->pid = msg->peer;
7137 pr->pk = (enum GNUNET_MQ_PreferenceKind) ntohl (msg->pk);
7139 GNUNET_CONTAINER_multipeermap_put (tc->details.application.requests,
7142 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
7146 GNUNET_SERVICE_client_drop (tc->client);
7149 pr->wc = GNUNET_PEERSTORE_watch (peerstore,
7152 GNUNET_PEERSTORE_TRANSPORT_URLADDRESS_KEY,
7155 GNUNET_SERVICE_client_continue (tc->client);
7160 * Given another peers address, consider checking it for validity
7161 * and then adding it to the Peerstore.
7163 * @param cls a `struct TransportClient`
7164 * @param hdr message containing the raw address data and
7165 * signature in the body, see #GNUNET_HELLO_extract_address()
7168 handle_address_consider_verify (void *cls,
7169 const struct GNUNET_TRANSPORT_AddressToVerify *hdr)
7171 struct TransportClient *tc = cls;
7173 enum GNUNET_NetworkType nt;
7174 struct GNUNET_TIME_Absolute expiration;
7177 // OPTIMIZE-FIXME: checking that we know this address already should
7178 // be done BEFORE checking the signature => HELLO API change!
7179 // OPTIMIZE-FIXME: pre-check: rate-limit signature verification / validation?!
7180 address = GNUNET_HELLO_extract_address (&hdr[1],
7181 ntohs (hdr->header.size) - sizeof (*hdr),
7185 if (NULL == address)
7187 GNUNET_break_op (0);
7190 start_address_validation (&hdr->peer,
7193 GNUNET_free (address);
7194 GNUNET_SERVICE_client_continue (tc->client);
7199 * Check #GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_HELLO_VALIDATION
7202 * @param cls a `struct TransportClient *`
7203 * @param m message to verify
7204 * @return #GNUNET_OK on success
7207 check_request_hello_validation (void *cls,
7208 const struct RequestHelloValidationMessage *m)
7211 GNUNET_MQ_check_zero_termination (m);
7217 * A client encountered an address of another peer. Consider validating it,
7218 * and if validation succeeds, persist it to PEERSTORE.
7220 * @param cls a `struct TransportClient *`
7221 * @param m message to verify
7224 handle_request_hello_validation (void *cls,
7225 const struct RequestHelloValidationMessage *m)
7227 struct TransportClient *tc = cls;
7229 start_address_validation (&m->peer,
7230 (const char *) &m[1],
7231 GNUNET_TIME_absolute_ntoh (m->expiration));
7232 GNUNET_SERVICE_client_continue (tc->client);
7237 * Free neighbour entry.
7241 * @param value a `struct Neighbour`
7242 * @return #GNUNET_OK (always)
7245 free_neighbour_cb (void *cls,
7246 const struct GNUNET_PeerIdentity *pid,
7249 struct Neighbour *neighbour = value;
7253 GNUNET_break (0); // should this ever happen?
7254 free_neighbour (neighbour);
7261 * Free DV route entry.
7265 * @param value a `struct DistanceVector`
7266 * @return #GNUNET_OK (always)
7269 free_dv_routes_cb (void *cls,
7270 const struct GNUNET_PeerIdentity *pid,
7273 struct DistanceVector *dv = value;
7284 * Free ephemeral entry.
7288 * @param value a `struct EphemeralCacheEntry`
7289 * @return #GNUNET_OK (always)
7292 free_ephemeral_cb (void *cls,
7293 const struct GNUNET_PeerIdentity *pid,
7296 struct EphemeralCacheEntry *ece = value;
7300 free_ephemeral (ece);
7306 * Free validation state.
7310 * @param value a `struct ValidationState`
7311 * @return #GNUNET_OK (always)
7314 free_validation_state_cb (void *cls,
7315 const struct GNUNET_PeerIdentity *pid,
7318 struct ValidationState *vs = value;
7322 free_validation_state (vs);
7328 * Function called when the service shuts down. Unloads our plugins
7329 * and cancels pending validations.
7331 * @param cls closure, unused
7334 do_shutdown (void *cls)
7336 struct LearnLaunchEntry *lle;
7339 if (NULL != ephemeral_task)
7341 GNUNET_SCHEDULER_cancel (ephemeral_task);
7342 ephemeral_task = NULL;
7344 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
7347 if (NULL != peerstore)
7349 GNUNET_PEERSTORE_disconnect (peerstore,
7353 if (NULL != GST_stats)
7355 GNUNET_STATISTICS_destroy (GST_stats,
7359 if (NULL != GST_my_private_key)
7361 GNUNET_free (GST_my_private_key);
7362 GST_my_private_key = NULL;
7364 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
7366 GNUNET_CONTAINER_multipeermap_iterate (validation_map,
7367 &free_validation_state_cb,
7369 GNUNET_CONTAINER_multipeermap_destroy (validation_map);
7370 validation_map = NULL;
7371 while (NULL != (lle = lle_head))
7373 GNUNET_CONTAINER_DLL_remove (lle_head,
7378 GNUNET_CONTAINER_multishortmap_destroy (dvlearn_map);
7380 GNUNET_CONTAINER_heap_destroy (validation_heap);
7381 validation_heap = NULL;
7382 GNUNET_CONTAINER_multipeermap_iterate (dv_routes,
7385 GNUNET_CONTAINER_multipeermap_destroy (dv_routes);
7387 GNUNET_CONTAINER_multipeermap_iterate (ephemeral_map,
7390 GNUNET_CONTAINER_multipeermap_destroy (ephemeral_map);
7391 ephemeral_map = NULL;
7392 GNUNET_CONTAINER_heap_destroy (ephemeral_heap);
7393 ephemeral_heap = NULL;
7398 * Initiate transport service.
7400 * @param cls closure
7401 * @param c configuration to use
7402 * @param service the initialized service
7406 const struct GNUNET_CONFIGURATION_Handle *c,
7407 struct GNUNET_SERVICE_Handle *service)
7413 neighbours = GNUNET_CONTAINER_multipeermap_create (1024,
7415 dv_routes = GNUNET_CONTAINER_multipeermap_create (1024,
7417 ephemeral_map = GNUNET_CONTAINER_multipeermap_create (32,
7419 ephemeral_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
7420 dvlearn_map = GNUNET_CONTAINER_multishortmap_create (2 * MAX_DV_LEARN_PENDING,
7422 validation_map = GNUNET_CONTAINER_multipeermap_create (1024,
7424 validation_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
7425 GST_my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (GST_cfg);
7426 if (NULL == GST_my_private_key)
7428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7429 _("Transport service is lacking key configuration settings. Exiting.\n"));
7430 GNUNET_SCHEDULER_shutdown ();
7433 GNUNET_CRYPTO_eddsa_key_get_public (GST_my_private_key,
7434 &GST_my_identity.public_key);
7435 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
7436 "My identity is `%s'\n",
7437 GNUNET_i2s_full (&GST_my_identity));
7438 GST_stats = GNUNET_STATISTICS_create ("transport",
7440 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
7442 peerstore = GNUNET_PEERSTORE_connect (GST_cfg);
7443 if (NULL == peerstore)
7446 GNUNET_SCHEDULER_shutdown ();
7453 * Define "main" method using service macro.
7457 GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN,
7460 &client_disconnect_cb,
7462 /* communication with applications */
7463 GNUNET_MQ_hd_fixed_size (suggest,
7464 GNUNET_MESSAGE_TYPE_TRANSPORT_SUGGEST,
7465 struct ExpressPreferenceMessage,
7467 GNUNET_MQ_hd_fixed_size (suggest_cancel,
7468 GNUNET_MESSAGE_TYPE_TRANSPORT_SUGGEST_CANCEL,
7469 struct ExpressPreferenceMessage,
7471 GNUNET_MQ_hd_var_size (request_hello_validation,
7472 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_HELLO_VALIDATION,
7473 struct RequestHelloValidationMessage,
7475 /* communication with core */
7476 GNUNET_MQ_hd_fixed_size (client_start,
7477 GNUNET_MESSAGE_TYPE_TRANSPORT_START,
7478 struct StartMessage,
7480 GNUNET_MQ_hd_var_size (client_send,
7481 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND,
7482 struct OutboundMessage,
7484 /* communication with communicators */
7485 GNUNET_MQ_hd_var_size (communicator_available,
7486 GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR,
7487 struct GNUNET_TRANSPORT_CommunicatorAvailableMessage,
7489 GNUNET_MQ_hd_var_size (communicator_backchannel,
7490 GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL,
7491 struct GNUNET_TRANSPORT_CommunicatorBackchannel,
7493 GNUNET_MQ_hd_var_size (add_address,
7494 GNUNET_MESSAGE_TYPE_TRANSPORT_ADD_ADDRESS,
7495 struct GNUNET_TRANSPORT_AddAddressMessage,
7497 GNUNET_MQ_hd_fixed_size (del_address,
7498 GNUNET_MESSAGE_TYPE_TRANSPORT_DEL_ADDRESS,
7499 struct GNUNET_TRANSPORT_DelAddressMessage,
7501 GNUNET_MQ_hd_var_size (incoming_msg,
7502 GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG,
7503 struct GNUNET_TRANSPORT_IncomingMessage,
7505 GNUNET_MQ_hd_fixed_size (queue_create_ok,
7506 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_OK,
7507 struct GNUNET_TRANSPORT_CreateQueueResponse,
7509 GNUNET_MQ_hd_fixed_size (queue_create_fail,
7510 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL,
7511 struct GNUNET_TRANSPORT_CreateQueueResponse,
7513 GNUNET_MQ_hd_var_size (add_queue_message,
7514 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_SETUP,
7515 struct GNUNET_TRANSPORT_AddQueueMessage,
7517 GNUNET_MQ_hd_var_size (address_consider_verify,
7518 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_CONSIDER_VERIFY,
7519 struct GNUNET_TRANSPORT_AddressToVerify,
7521 GNUNET_MQ_hd_fixed_size (del_queue_message,
7522 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_TEARDOWN,
7523 struct GNUNET_TRANSPORT_DelQueueMessage,
7525 GNUNET_MQ_hd_fixed_size (send_message_ack,
7526 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG_ACK,
7527 struct GNUNET_TRANSPORT_SendMessageToAck,
7529 /* communication with monitors */
7530 GNUNET_MQ_hd_fixed_size (monitor_start,
7531 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_START,
7532 struct GNUNET_TRANSPORT_MonitorStart,
7534 GNUNET_MQ_handler_end ());
7537 /* end of file gnunet-service-transport.c */