2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/gnunet-service-transport.c
23 * @brief low-level P2P messaging
24 * @author Christian Grothoff
28 #include "gnunet_client_lib.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "gnunet_plugin_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_service_lib.h"
38 #include "gnunet_signatures.h"
39 #include "gnunet_transport_plugin.h"
40 #include "transport.h"
42 #define DEBUG_BLACKLIST GNUNET_YES
44 #define DEBUG_PING_PONG GNUNET_YES
46 #define DEBUG_TRANSPORT_HELLO GNUNET_YES
49 * Should we do some additional checks (to validate behavior
52 #define EXTRA_CHECKS GNUNET_YES
55 * How many messages can we have pending for a given client process
56 * before we start to drop incoming messages? We typically should
57 * have only one client and so this would be the primary buffer for
58 * messages, so the number should be chosen rather generously.
60 * The expectation here is that most of the time the queue is large
61 * enough so that a drop is virtually never required. Note that
62 * this value must be about as large as 'TOTAL_MSGS' in the
63 * 'test_transport_api_reliability.c', otherwise that testcase may
66 #define MAX_PENDING (128 * 1024)
69 * Size of the per-transport blacklist hash maps.
71 #define TRANSPORT_BLACKLIST_HT_SIZE 16
74 * How often should we try to reconnect to a peer using a particular
75 * transport plugin before giving up? Note that the plugin may be
76 * added back to the list after PLUGIN_RETRY_FREQUENCY expires.
78 #define MAX_CONNECT_RETRY 3
81 * Limit on the number of ready-to-run tasks when validating
82 * HELLOs. If more tasks are ready to run, we will drop
83 * HELLOs instead of validating them.
85 #define MAX_HELLO_LOAD 4
88 * How often must a peer violate bandwidth quotas before we start
89 * to simply drop its messages?
91 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
94 * How long until a HELLO verification attempt should time out?
95 * Must be rather small, otherwise a partially successful HELLO
96 * validation (some addresses working) might not be available
97 * before a client's request for a connection fails for good.
98 * Besides, if a single request to an address takes a long time,
99 * then the peer is unlikely worthwhile anyway.
101 #define HELLO_VERIFICATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
104 * How long is a PONG signature valid? We'll recycle a signature until
105 * 1/4 of this time is remaining. PONGs should expire so that if our
106 * external addresses change an adversary cannot replay them indefinitely.
107 * OTOH, we don't want to spend too much time generating PONG signatures,
108 * so they must have some lifetime to reduce our CPU usage.
110 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
113 * Priority to use for PONG messages.
115 #define TRANSPORT_PONG_PRIORITY 4
118 * How often do we re-add (cheaper) plugins to our list of plugins
119 * to try for a given connected peer?
121 #define PLUGIN_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
124 * After how long do we expire an address in a HELLO that we just
125 * validated? This value is also used for our own addresses when we
128 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
132 * How long before an existing address expires should we again try to
133 * validate it? Must be (significantly) smaller than
134 * HELLO_ADDRESS_EXPIRATION.
136 #define HELLO_REVALIDATION_START_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
139 * Maximum frequency for re-evaluating latencies for all transport addresses.
141 #define LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
144 * Maximum frequency for re-evaluating latencies for connected addresses.
146 #define CONNECTED_LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
150 * List of addresses of other peers
152 struct ForeignAddressList
155 * This is a linked list.
157 struct ForeignAddressList *next;
160 * Which ready list does this entry belong to.
162 struct ReadyList *ready_list;
165 * How long until we auto-expire this address (unless it is
166 * re-confirmed by the transport)?
168 struct GNUNET_TIME_Absolute expires;
171 * Task used to re-validate addresses, updates latencies and
174 GNUNET_SCHEDULER_TaskIdentifier revalidate_task;
182 * Session (or NULL if no valid session currently exists or if the
183 * plugin does not use sessions).
185 struct Session *session;
188 * What was the last latency observed for this address, plugin and peer?
190 struct GNUNET_TIME_Relative latency;
193 * If we did not successfully transmit a message to the given peer
194 * via this connection during the specified time, we should consider
195 * the connection to be dead. This is used in the case that a TCP
196 * transport simply stalls writing to the stream but does not
197 * formerly get a signal that the other peer died.
199 struct GNUNET_TIME_Absolute timeout;
202 * How often have we tried to connect using this plugin? Used to
203 * discriminate against addresses that do not work well.
204 * FIXME: not yet used, but should be!
206 unsigned int connect_attempts;
209 * DV distance to this peer (1 if no DV is used).
210 * FIXME: need to set this from transport plugins!
220 * Have we ever estimated the latency of this address? Used to
221 * ensure that the first time we add an address, we immediately
227 * Are we currently connected via this address? The first time we
228 * successfully transmit or receive data to a peer via a particular
229 * address, we set this to GNUNET_YES. If we later get an error
230 * (disconnect notification, transmission failure, timeout), we set
231 * it back to GNUNET_NO.
236 * Is this plugin currently busy transmitting to the specific target?
237 * GNUNET_NO if not (initial, default state is GNUNET_NO). Internal
238 * messages do not count as 'in transmit'.
243 * Has this address been validated yet?
251 * Entry in linked list of network addresses for ourselves. Also
252 * includes a cached signature for 'struct TransportPongMessage's.
254 struct OwnAddressList
257 * This is a linked list.
259 struct OwnAddressList *next;
262 * How long until we actually auto-expire this address (unless it is
263 * re-confirmed by the transport)?
265 struct GNUNET_TIME_Absolute expires;
268 * How long until the current signature expires? (ZERO if the
269 * signature was never created).
271 struct GNUNET_TIME_Absolute pong_sig_expires;
274 * Signature for a 'struct TransportPongMessage' for this address.
276 struct GNUNET_CRYPTO_RsaSignature pong_signature;
287 * Entry in linked list of all of our plugins.
289 struct TransportPlugin
293 * This is a linked list.
295 struct TransportPlugin *next;
298 * API of the transport as returned by the plugin's
299 * initialization function.
301 struct GNUNET_TRANSPORT_PluginFunctions *api;
304 * Short name for the plugin (i.e. "tcp").
309 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
314 * List of our known addresses for this transport.
316 struct OwnAddressList *addresses;
319 * Environment this transport service is using
322 struct GNUNET_TRANSPORT_PluginEnvironment env;
325 * ID of task that is used to clean up expired addresses.
327 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
330 * Set to GNUNET_YES if we need to scrap the existing list of
331 * "addresses" and start fresh when we receive the next address
332 * update from a transport. Set to GNUNET_NO if we should just add
333 * the new address to the list and wait for the commit call.
338 * Hashmap of blacklisted peers for this particular transport.
340 struct GNUNET_CONTAINER_MultiHashMap *blacklist;
343 struct NeighbourList;
346 * For each neighbour we keep a list of messages
347 * that we still want to transmit to the neighbour.
353 * This is a doubly linked list.
355 struct MessageQueue *next;
358 * This is a doubly linked list.
360 struct MessageQueue *prev;
363 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
364 * stuck together in memory. Allocated at the end of this struct.
366 const char *message_buf;
369 * Size of the message buf
371 size_t message_buf_size;
374 * Client responsible for queueing the message;
375 * used to check that a client has no two messages
376 * pending for the same target. Can be NULL.
378 struct TransportClient *client;
381 * Using which specific address should we send this message?
383 struct ForeignAddressList *specific_address;
386 * Peer ID of the Neighbour this entry belongs to.
388 struct GNUNET_PeerIdentity neighbour_id;
391 * Plugin that we used for the transmission.
392 * NULL until we scheduled a transmission.
394 struct TransportPlugin *plugin;
397 * At what time should we fail?
399 struct GNUNET_TIME_Absolute timeout;
402 * Internal message of the transport system that should not be
403 * included in the usual SEND-SEND_OK transmission confirmation
404 * traffic management scheme. Typically, "internal_msg" will
405 * be set whenever "client" is NULL (but it is not strictly
411 * How important is the message?
413 unsigned int priority;
419 * For a given Neighbour, which plugins are available
420 * to talk to this peer and what are their costs?
425 * This is a linked list.
427 struct ReadyList *next;
430 * Which of our transport plugins does this entry
433 struct TransportPlugin *plugin;
436 * Transport addresses, latency, and readiness for
437 * this particular plugin.
439 struct ForeignAddressList *addresses;
442 * To which neighbour does this ready list belong to?
444 struct NeighbourList *neighbour;
450 * Entry in linked list of all of our current neighbours.
456 * This is a linked list.
458 struct NeighbourList *next;
461 * Which of our transports is connected to this peer
462 * and what is their status?
464 struct ReadyList *plugins;
467 * Head of list of messages we would like to send to this peer;
468 * must contain at most one message per client.
470 struct MessageQueue *messages_head;
473 * Tail of list of messages we would like to send to this peer; must
474 * contain at most one message per client.
476 struct MessageQueue *messages_tail;
479 * Buffer for at most one payload message used when we receive
480 * payload data before our PING-PONG has succeeded. We then
481 * store such messages in this intermediary buffer until the
482 * connection is fully up.
484 struct GNUNET_MessageHeader *pre_connect_message_buffer;
487 * Context for peerinfo iteration.
488 * NULL after we are done processing peerinfo's information.
490 struct GNUNET_PEERINFO_IteratorContext *piter;
493 * Public key for this peer. Valid only if the respective flag is set below.
495 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
498 * Identity of this neighbour.
500 struct GNUNET_PeerIdentity id;
503 * ID of task scheduled to run when this peer is about to
504 * time out (will free resources associated with the peer).
506 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
509 * ID of task scheduled to run when we should retry transmitting
510 * the head of the message queue. Actually triggered when the
511 * transmission is timing out (we trigger instantly when we have
512 * a chance of success).
514 GNUNET_SCHEDULER_TaskIdentifier retry_task;
517 * How long until we should consider this peer dead
518 * (if we don't receive another message in the
521 struct GNUNET_TIME_Absolute peer_timeout;
524 * Tracker for inbound bandwidth.
526 struct GNUNET_BANDWIDTH_Tracker in_tracker;
529 * The latency we have seen for this particular address for
530 * this particular peer. This latency may have been calculated
531 * over multiple transports. This value reflects how long it took
532 * us to receive a response when SENDING via this particular
533 * transport/neighbour/address combination!
535 * FIXME: we need to periodically send PINGs to update this
536 * latency (at least more often than the current "huge" (11h?)
539 struct GNUNET_TIME_Relative latency;
542 * How often has the other peer (recently) violated the
543 * inbound traffic limit? Incremented by 10 per violation,
544 * decremented by 1 per non-violation (for each
547 unsigned int quota_violation_count;
550 * DV distance to this peer (1 if no DV is used).
555 * Have we seen an PONG from this neighbour in the past (and
556 * not had a disconnect since)?
561 * Do we have a valid public key for this neighbour?
563 int public_key_valid;
568 * Message used to ask a peer to validate receipt (to check an address
569 * from a HELLO). Followed by the address we are trying to validate,
570 * or an empty address if we are just sending a PING to confirm that a
571 * connection which the receiver (of the PING) initiated is still valid.
573 struct TransportPingMessage
577 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
579 struct GNUNET_MessageHeader header;
582 * Challenge code (to ensure fresh reply).
584 uint32_t challenge GNUNET_PACKED;
587 * Who is the intended recipient?
589 struct GNUNET_PeerIdentity target;
595 * Message used to validate a HELLO. The challenge is included in the
596 * confirmation to make matching of replies to requests possible. The
597 * signature signs our public key, an expiration time and our address.<p>
599 * This message is followed by our transport address that the PING tried
600 * to confirm (if we liked it). The address can be empty (zero bytes)
601 * if the PING had not address either (and we received the request via
602 * a connection that we initiated).
604 struct TransportPongMessage
608 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
610 struct GNUNET_MessageHeader header;
613 * Challenge code from PING (showing freshness). Not part of what
614 * is signed so that we can re-use signatures.
616 uint32_t challenge GNUNET_PACKED;
621 struct GNUNET_CRYPTO_RsaSignature signature;
624 * What are we signing and why? Two possible reason codes can be here:
625 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
626 * plausible address for this peer (pid is set to identity of signer); or
627 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING to confirm that this is
628 * an address we used to connect to the peer with the given pid.
630 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
633 * When does this signature expire?
635 struct GNUNET_TIME_AbsoluteNBO expiration;
638 * Either the identity of the peer Who signed this message, or the
639 * identity of the peer that we're connected to using the given
640 * address (depending on purpose.type).
642 struct GNUNET_PeerIdentity pid;
645 * Size of address appended to this message (part of what is
646 * being signed, hence not redundant).
654 * Linked list of messages to be transmitted to the client. Each
655 * entry is followed by the actual message.
657 struct ClientMessageQueueEntry
660 * This is a doubly-linked list.
662 struct ClientMessageQueueEntry *next;
665 * This is a doubly-linked list.
667 struct ClientMessageQueueEntry *prev;
672 * Client connected to the transport service.
674 struct TransportClient
678 * This is a linked list.
680 struct TransportClient *next;
683 * Handle to the client.
685 struct GNUNET_SERVER_Client *client;
688 * Linked list of messages yet to be transmitted to
691 struct ClientMessageQueueEntry *message_queue_head;
694 * Tail of linked list of messages yet to be transmitted to the
697 struct ClientMessageQueueEntry *message_queue_tail;
700 * Current transmit request handle.
702 struct GNUNET_CONNECTION_TransmitHandle *th;
705 * Is a call to "transmit_send_continuation" pending? If so, we
706 * must not free this struct (even if the corresponding client
707 * disconnects) and instead only remove it from the linked list and
708 * set the "client" field to NULL.
713 * Length of the list of messages pending for this client.
715 unsigned int message_count;
721 * Context of currently active requests to peerinfo
722 * for validation of HELLOs.
724 struct CheckHelloValidatedContext;
728 * Entry in map of all HELLOs awaiting validation.
730 struct ValidationEntry
734 * NULL if this entry is not part of a larger HELLO validation.
736 struct CheckHelloValidatedContext *chvc;
739 * The address, actually a pointer to the end
740 * of this struct. Do not free!
745 * Name of the transport.
747 char *transport_name;
750 * The public key of the peer.
752 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
755 * ID of task that will clean up this entry if we don't succeed
756 * with the validation first.
758 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
761 * At what time did we send this validation?
763 struct GNUNET_TIME_Absolute send_time;
766 * Session being validated (or NULL for none).
768 struct Session *session;
771 * Challenge number we used.
784 * Context of currently active requests to peerinfo
785 * for validation of HELLOs.
787 struct CheckHelloValidatedContext
791 * This is a doubly-linked list.
793 struct CheckHelloValidatedContext *next;
796 * This is a doubly-linked list.
798 struct CheckHelloValidatedContext *prev;
801 * Hello that we are validating.
803 const struct GNUNET_HELLO_Message *hello;
806 * Context for peerinfo iteration.
807 * NULL after we are done processing peerinfo's information.
809 struct GNUNET_PEERINFO_IteratorContext *piter;
812 * Was a HELLO known for this peer to peerinfo?
817 * Number of validation entries currently referring to this
820 unsigned int ve_count;
828 static struct GNUNET_HELLO_Message *our_hello;
833 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
838 static struct GNUNET_PeerIdentity my_identity;
843 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
848 const struct GNUNET_CONFIGURATION_Handle *cfg;
851 * Linked list of all clients to this service.
853 static struct TransportClient *clients;
856 * All loaded plugins.
858 static struct TransportPlugin *plugins;
861 * Handle to peerinfo service.
863 static struct GNUNET_PEERINFO_Handle *peerinfo;
866 * All known neighbours and their HELLOs.
868 static struct NeighbourList *neighbours;
871 * Number of neighbours we'd like to have.
873 static uint32_t max_connect_per_transport;
876 * Head of linked list.
878 static struct CheckHelloValidatedContext *chvc_head;
881 * Tail of linked list.
883 static struct CheckHelloValidatedContext *chvc_tail;
886 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
887 * of the given peer that we are currently validating).
889 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
892 * Handle for reporting statistics.
894 static struct GNUNET_STATISTICS_Handle *stats;
897 * The peer specified by the given neighbour has timed-out or a plugin
898 * has disconnected. We may either need to do nothing (other plugins
899 * still up), or trigger a full disconnect and clean up. This
900 * function updates our state and do the necessary notifications.
901 * Also notifies our clients that the neighbour is now officially
904 * @param n the neighbour list entry for the peer
905 * @param check should we just check if all plugins
906 * disconnected or must we ask all plugins to
909 static void disconnect_neighbour (struct NeighbourList *n, int check);
912 * Check the ready list for the given neighbour and if a plugin is
913 * ready for transmission (and if we have a message), do so!
915 * @param neighbour target peer for which to transmit
917 static void try_transmission_to_peer (struct NeighbourList *neighbour);
921 * Find an entry in the neighbour list for a particular peer.
923 * @return NULL if not found.
925 static struct NeighbourList *
926 find_neighbour (const struct GNUNET_PeerIdentity *key)
928 struct NeighbourList *head = neighbours;
930 while ((head != NULL) &&
931 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
938 * Find an entry in the transport list for a particular transport.
940 * @return NULL if not found.
942 static struct TransportPlugin *
943 find_transport (const char *short_name)
945 struct TransportPlugin *head = plugins;
946 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
952 * Is a particular peer blacklisted for a particular transport?
954 * @param peer the peer to check for
955 * @param plugin the plugin used to connect to the peer
957 * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
960 is_blacklisted (const struct GNUNET_PeerIdentity *peer, struct TransportPlugin *plugin)
963 if (plugin->blacklist != NULL)
965 if (GNUNET_CONTAINER_multihashmap_contains(plugin->blacklist, &peer->hashPubKey) == GNUNET_YES)
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
969 _("Peer `%s:%s' is blacklisted!\n"),
970 plugin->short_name, GNUNET_i2s (peer));
981 add_peer_to_blacklist (struct GNUNET_PeerIdentity *peer, char *transport_name)
983 struct TransportPlugin *plugin;
985 plugin = find_transport(transport_name);
986 if (plugin == NULL) /* Nothing to do */
988 if (plugin->blacklist == NULL)
989 plugin->blacklist = GNUNET_CONTAINER_multihashmap_create(TRANSPORT_BLACKLIST_HT_SIZE);
990 GNUNET_assert(plugin->blacklist != NULL);
991 GNUNET_CONTAINER_multihashmap_put(plugin->blacklist, &peer->hashPubKey,
993 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
998 * Read the blacklist file, containing transport:peer entries.
999 * Provided the transport is loaded, set up hashmap with these
1000 * entries to blacklist peers by transport.
1004 read_blacklist_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1011 struct GNUNET_PeerIdentity pid;
1013 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1014 unsigned int entries_found;
1015 char *transport_name;
1018 GNUNET_CONFIGURATION_get_value_filename (cfg,
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 _("Option `%s' in section `%s' not specified!\n"),
1031 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1032 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1033 | GNUNET_DISK_PERM_USER_WRITE);
1034 if (0 != STAT (fn, &frstat))
1036 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1037 _("Could not read blacklist file `%s'\n"), fn);
1041 if (frstat.st_size == 0)
1044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1045 _("Blacklist file `%s' is empty.\n"),
1051 /* FIXME: use mmap */
1052 data = GNUNET_malloc_large (frstat.st_size);
1053 GNUNET_assert(data != NULL);
1054 if (frstat.st_size !=
1055 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1057 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1058 _("Failed to read blacklist from `%s'\n"), fn);
1065 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1067 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1068 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1071 while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') && !isspace ( (unsigned char) data[colon_pos]))
1074 if (colon_pos >= frstat.st_size)
1076 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1077 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1078 (unsigned long long) colon_pos);
1084 if (isspace( (unsigned char) data[colon_pos]))
1086 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1087 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1088 (unsigned long long) colon_pos);
1090 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1094 tsize = colon_pos - pos;
1095 if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size) || (tsize == 0))
1097 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1098 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1099 (unsigned long long) colon_pos);
1108 transport_name = GNUNET_malloc(tsize + 1);
1109 memcpy(transport_name, &data[pos], tsize);
1110 pos = colon_pos + 1;
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113 _("Read transport name %s in blacklist file.\n"),
1116 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1117 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1120 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1121 (unsigned long long) pos);
1123 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1125 GNUNET_free_non_null(transport_name);
1128 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1129 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1131 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1132 _("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
1133 (unsigned long long) pos,
1138 if (0 != memcmp (&pid,
1140 sizeof (struct GNUNET_PeerIdentity)))
1143 add_peer_to_blacklist (&pid,
1148 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1149 _("Found myself `%s' in blacklist (useless, ignored)\n"),
1153 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1154 GNUNET_free_non_null(transport_name);
1155 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1164 * Function called to notify a client about the socket being ready to
1165 * queue more data. "buf" will be NULL and "size" zero if the socket
1166 * was closed for writing in the meantime.
1168 * @param cls closure
1169 * @param size number of bytes available in buf
1170 * @param buf where the callee should write the message
1171 * @return number of bytes written to buf
1174 transmit_to_client_callback (void *cls, size_t size, void *buf)
1176 struct TransportClient *client = cls;
1177 struct ClientMessageQueueEntry *q;
1180 const struct GNUNET_MessageHeader *msg;
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1187 "Transmission to client failed, closing connection.\n");
1188 /* fatal error with client, free message queue! */
1189 while (NULL != (q = client->message_queue_head))
1191 GNUNET_STATISTICS_update (stats,
1192 gettext_noop ("# bytes discarded (could not transmit to client)"),
1193 ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
1195 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1196 client->message_queue_tail,
1200 client->message_count = 0;
1205 while (NULL != (q = client->message_queue_head))
1207 msg = (const struct GNUNET_MessageHeader *) &q[1];
1208 msize = ntohs (msg->size);
1209 if (msize + tsize > size)
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1213 "Transmitting message of type %u to client.\n",
1216 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1217 client->message_queue_tail,
1219 memcpy (&cbuf[tsize], msg, msize);
1222 client->message_count--;
1226 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1227 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1229 GNUNET_TIME_UNIT_FOREVER_REL,
1230 &transmit_to_client_callback,
1232 GNUNET_assert (client->th != NULL);
1239 * Convert an address to a string.
1241 * @param plugin name of the plugin responsible for the address
1242 * @param addr binary address
1243 * @param addr_len number of bytes in addr
1244 * @return NULL on error, otherwise address string
1247 a2s (const char *plugin,
1251 struct TransportPlugin *p;
1255 p = find_transport (plugin);
1258 return p->api->address_to_string (p->api->cls,
1265 * Mark the given FAL entry as 'connected' (and hence preferred for
1266 * sending); also mark all others for the same peer as 'not connected'
1267 * (since only one can be preferred).
1269 * @param fal address to set to 'connected'
1272 mark_address_connected (struct ForeignAddressList *fal)
1274 struct ForeignAddressList *pos;
1277 GNUNET_assert (GNUNET_YES == fal->validated);
1278 if (fal->connected == GNUNET_YES)
1279 return; /* nothing to do */
1281 pos = fal->ready_list->addresses;
1284 if (GNUNET_YES == pos->connected)
1287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1288 "Marking address `%s' as no longer connected (due to connect on other address)\n",
1289 a2s (pos->ready_list->plugin->short_name,
1293 GNUNET_break (cnt == GNUNET_YES);
1295 pos->connected = GNUNET_NO;
1296 GNUNET_STATISTICS_update (stats,
1297 gettext_noop ("# connected addresses"),
1303 fal->connected = GNUNET_YES;
1304 if (GNUNET_YES == cnt)
1306 GNUNET_STATISTICS_update (stats,
1307 gettext_noop ("# connected addresses"),
1315 * Send the specified message to the specified client. Since multiple
1316 * messages may be pending for the same client at a time, this code
1317 * makes sure that no message is lost.
1319 * @param client client to transmit the message to
1320 * @param msg the message to send
1321 * @param may_drop can this message be dropped if the
1322 * message queue for this client is getting far too large?
1325 transmit_to_client (struct TransportClient *client,
1326 const struct GNUNET_MessageHeader *msg, int may_drop)
1328 struct ClientMessageQueueEntry *q;
1331 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1333 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1335 ("Dropping message of type %u and size %u, have %u messages pending (%u is the soft limit)\n"),
1338 client->message_count,
1340 GNUNET_STATISTICS_update (stats,
1341 gettext_noop ("# messages dropped due to slow client"),
1346 msize = ntohs (msg->size);
1347 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1348 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1349 memcpy (&q[1], msg, msize);
1350 GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1351 client->message_queue_tail,
1352 client->message_queue_tail,
1354 client->message_count++;
1355 if (client->th == NULL)
1357 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1359 GNUNET_TIME_UNIT_FOREVER_REL,
1360 &transmit_to_client_callback,
1362 GNUNET_assert (client->th != NULL);
1368 * Transmit a 'SEND_OK' notification to the given client for the
1371 * @param client who to notify
1372 * @param n neighbour to notify about
1373 * @param result status code for the transmission request
1376 transmit_send_ok (struct TransportClient *client,
1377 struct NeighbourList *n,
1380 struct SendOkMessage send_ok_msg;
1382 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1383 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1384 send_ok_msg.success = htonl (result);
1385 send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1386 send_ok_msg.peer = n->id;
1387 transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1392 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1393 * upon "completion" of a send request. This tells the API
1394 * that it is now legal to send another message to the given
1397 * @param cls closure, identifies the entry on the
1398 * message queue that was transmitted and the
1399 * client responsible for queueing the message
1400 * @param target the peer receiving the message
1401 * @param result GNUNET_OK on success, if the transmission
1402 * failed, we should not tell the client to transmit
1406 transmit_send_continuation (void *cls,
1407 const struct GNUNET_PeerIdentity *target,
1410 struct MessageQueue *mq = cls;
1411 struct NeighbourList *n;
1413 GNUNET_STATISTICS_update (stats,
1414 gettext_noop ("# bytes pending with plugins"),
1415 - (int64_t) mq->message_buf_size,
1417 if (result == GNUNET_OK)
1419 GNUNET_STATISTICS_update (stats,
1420 gettext_noop ("# bytes successfully transmitted by plugins"),
1421 mq->message_buf_size,
1426 GNUNET_STATISTICS_update (stats,
1427 gettext_noop ("# bytes with transmission failure by plugins"),
1428 mq->message_buf_size,
1431 n = find_neighbour(&mq->neighbour_id);
1432 GNUNET_assert (n != NULL);
1433 if (mq->specific_address != NULL)
1435 if (result == GNUNET_OK)
1437 mq->specific_address->timeout =
1438 GNUNET_TIME_relative_to_absolute
1439 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1440 if (mq->specific_address->validated == GNUNET_YES)
1441 mark_address_connected (mq->specific_address);
1445 if (mq->specific_address->connected != GNUNET_NO)
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1449 "Marking address `%s' as no longer connected (due to transmission problem)\n",
1450 a2s (mq->specific_address->ready_list->plugin->short_name,
1451 mq->specific_address->addr,
1452 mq->specific_address->addrlen));
1454 GNUNET_STATISTICS_update (stats,
1455 gettext_noop ("# connected addresses"),
1458 mq->specific_address->connected = GNUNET_NO;
1461 if (! mq->internal_msg)
1462 mq->specific_address->in_transmit = GNUNET_NO;
1464 if (mq->client != NULL)
1465 transmit_send_ok (mq->client, n, result);
1467 try_transmission_to_peer (n);
1472 * Find an address in any of the available transports for
1473 * the given neighbour that would be good for message
1474 * transmission. This is essentially the transport selection
1477 * @param neighbour for whom to select an address
1478 * @return selected address, NULL if we have none
1480 struct ForeignAddressList *
1481 find_ready_address(struct NeighbourList *neighbour)
1483 struct ReadyList *head = neighbour->plugins;
1484 struct ForeignAddressList *addresses;
1485 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1486 struct ForeignAddressList *best_address;
1488 best_address = NULL;
1489 while (head != NULL)
1491 addresses = head->addresses;
1492 while (addresses != NULL)
1494 if ( (addresses->timeout.abs_value < now.abs_value) &&
1495 (addresses->connected == GNUNET_YES) )
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1499 "Marking long-time inactive connection to `%4s' as down.\n",
1500 GNUNET_i2s (&neighbour->id));
1502 GNUNET_STATISTICS_update (stats,
1503 gettext_noop ("# connected addresses"),
1506 addresses->connected = GNUNET_NO;
1508 addresses = addresses->next;
1511 addresses = head->addresses;
1512 while (addresses != NULL)
1514 #if DEBUG_TRANSPORT > 1
1515 if (addresses->addr != NULL)
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
1518 a2s (head->plugin->short_name,
1520 addresses->addrlen),
1521 GNUNET_i2s (&neighbour->id),
1522 addresses->connected,
1523 addresses->in_transmit,
1524 addresses->validated,
1525 addresses->connect_attempts,
1526 (unsigned long long) addresses->timeout.abs_value,
1527 (unsigned int) addresses->distance);
1529 if ( ( (best_address == NULL) ||
1530 (addresses->connected == GNUNET_YES) ||
1531 (best_address->connected == GNUNET_NO) ) &&
1532 (addresses->in_transmit == GNUNET_NO) &&
1533 ( (best_address == NULL) ||
1534 (addresses->latency.rel_value < best_address->latency.rel_value)) )
1535 best_address = addresses;
1536 /* FIXME: also give lower-latency addresses that are not
1537 connected a chance some times... */
1538 addresses = addresses->next;
1542 if (best_address != NULL)
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546 "Best address found (`%s') has latency of %llu ms.\n",
1547 (best_address->addrlen > 0)
1548 ? a2s (best_address->ready_list->plugin->short_name,
1550 best_address->addrlen)
1552 best_address->latency.rel_value);
1557 GNUNET_STATISTICS_update (stats,
1558 gettext_noop ("# transmission attempts failed (no address)"),
1562 return best_address;
1568 * We should re-try transmitting to the given peer,
1569 * hopefully we've learned something in the meantime.
1572 retry_transmission_task (void *cls,
1573 const struct GNUNET_SCHEDULER_TaskContext *tc)
1575 struct NeighbourList *n = cls;
1577 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1578 try_transmission_to_peer (n);
1583 * Check the ready list for the given neighbour and if a plugin is
1584 * ready for transmission (and if we have a message), do so!
1586 * @param neighbour target peer for which to transmit
1589 try_transmission_to_peer (struct NeighbourList *neighbour)
1591 struct ReadyList *rl;
1592 struct MessageQueue *mq;
1593 struct GNUNET_TIME_Relative timeout;
1597 if (neighbour->messages_head == NULL)
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1601 "Transmission queue for `%4s' is empty\n",
1602 GNUNET_i2s (&neighbour->id));
1604 return; /* nothing to do */
1607 mq = neighbour->messages_head;
1608 force_address = GNUNET_YES;
1609 if (mq->specific_address == NULL)
1611 mq->specific_address = find_ready_address(neighbour);
1612 GNUNET_STATISTICS_update (stats,
1613 gettext_noop ("# transport selected peer address freely"),
1616 force_address = GNUNET_NO;
1618 if (mq->specific_address == NULL)
1620 GNUNET_STATISTICS_update (stats,
1621 gettext_noop ("# transport failed to selected peer address"),
1624 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1625 if (timeout.rel_value == 0)
1628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1629 "No destination address available to transmit message of size %u to peer `%4s'\n",
1630 mq->message_buf_size,
1631 GNUNET_i2s (&mq->neighbour_id));
1633 GNUNET_STATISTICS_update (stats,
1634 gettext_noop ("# bytes in message queue for other peers"),
1635 - (int64_t) mq->message_buf_size,
1637 GNUNET_STATISTICS_update (stats,
1638 gettext_noop ("# bytes discarded (no destination address available)"),
1639 mq->message_buf_size,
1641 if (mq->client != NULL)
1642 transmit_send_ok (mq->client, neighbour, GNUNET_NO);
1643 GNUNET_CONTAINER_DLL_remove (neighbour->messages_head,
1644 neighbour->messages_tail,
1647 return; /* nobody ready */
1649 GNUNET_STATISTICS_update (stats,
1650 gettext_noop ("# message delivery deferred (no address)"),
1653 if (neighbour->retry_task != GNUNET_SCHEDULER_NO_TASK)
1654 GNUNET_SCHEDULER_cancel (neighbour->retry_task);
1655 neighbour->retry_task = GNUNET_SCHEDULER_add_delayed (timeout,
1656 &retry_transmission_task,
1659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1660 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
1661 mq->message_buf_size,
1662 GNUNET_i2s (&mq->neighbour_id),
1665 /* FIXME: might want to trigger peerinfo lookup here
1666 (unless that's already pending...) */
1669 GNUNET_CONTAINER_DLL_remove (neighbour->messages_head,
1670 neighbour->messages_tail,
1672 if (mq->specific_address->connected == GNUNET_NO)
1673 mq->specific_address->connect_attempts++;
1674 rl = mq->specific_address->ready_list;
1675 mq->plugin = rl->plugin;
1676 if (!mq->internal_msg)
1677 mq->specific_address->in_transmit = GNUNET_YES;
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1680 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
1681 mq->message_buf_size,
1682 GNUNET_i2s (&neighbour->id),
1683 (mq->specific_address->addr != NULL)
1684 ? a2s (mq->plugin->short_name,
1685 mq->specific_address->addr,
1686 mq->specific_address->addrlen)
1688 rl->plugin->short_name);
1690 GNUNET_STATISTICS_update (stats,
1691 gettext_noop ("# bytes in message queue for other peers"),
1692 - (int64_t) mq->message_buf_size,
1694 GNUNET_STATISTICS_update (stats,
1695 gettext_noop ("# bytes pending with plugins"),
1696 mq->message_buf_size,
1698 ret = rl->plugin->api->send (rl->plugin->api->cls,
1701 mq->message_buf_size,
1703 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1704 mq->specific_address->session,
1705 mq->specific_address->addr,
1706 mq->specific_address->addrlen,
1708 &transmit_send_continuation, mq);
1711 /* failure, but 'send' would not call continuation in this case,
1712 so we need to do it here! */
1713 transmit_send_continuation (mq,
1721 * Send the specified message to the specified peer.
1723 * @param client source of the transmission request (can be NULL)
1724 * @param peer_address ForeignAddressList where we should send this message
1725 * @param priority how important is the message
1726 * @param timeout how long do we have to transmit?
1727 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
1728 * @param message_buf_size total size of all messages in message_buf
1729 * @param is_internal is this an internal message; these are pre-pended and
1730 * also do not count for plugins being "ready" to transmit
1731 * @param neighbour handle to the neighbour for transmission
1734 transmit_to_peer (struct TransportClient *client,
1735 struct ForeignAddressList *peer_address,
1736 unsigned int priority,
1737 struct GNUNET_TIME_Relative timeout,
1738 const char *message_buf,
1739 size_t message_buf_size,
1740 int is_internal, struct NeighbourList *neighbour)
1742 struct MessageQueue *mq;
1747 /* check for duplicate submission */
1748 mq = neighbour->messages_head;
1751 if (mq->client == client)
1753 /* client transmitted to same peer twice
1754 before getting SEND_OK! */
1762 GNUNET_STATISTICS_update (stats,
1763 gettext_noop ("# bytes in message queue for other peers"),
1766 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
1767 mq->specific_address = peer_address;
1768 mq->client = client;
1769 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1770 memcpy (&mq[1], message_buf, message_buf_size);
1771 mq->message_buf = (const char*) &mq[1];
1772 mq->message_buf_size = message_buf_size;
1773 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
1774 mq->internal_msg = is_internal;
1775 mq->priority = priority;
1776 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1778 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
1779 neighbour->messages_tail,
1782 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
1783 neighbour->messages_tail,
1784 neighbour->messages_tail,
1786 try_transmission_to_peer (neighbour);
1793 struct GeneratorContext
1795 struct TransportPlugin *plug_pos;
1796 struct OwnAddressList *addr_pos;
1797 struct GNUNET_TIME_Absolute expiration;
1805 address_generator (void *cls, size_t max, void *buf)
1807 struct GeneratorContext *gc = cls;
1810 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
1812 gc->plug_pos = gc->plug_pos->next;
1813 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
1815 if (NULL == gc->plug_pos)
1820 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
1823 gc->addr_pos->addrlen, buf, max);
1824 gc->addr_pos = gc->addr_pos->next;
1830 * Construct our HELLO message from all of the addresses of
1831 * all of the transports.
1836 struct GNUNET_HELLO_Message *hello;
1837 struct TransportClient *cpos;
1838 struct NeighbourList *npos;
1839 struct GeneratorContext gc;
1841 gc.plug_pos = plugins;
1842 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
1843 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1844 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
1846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1847 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
1849 GNUNET_STATISTICS_update (stats,
1850 gettext_noop ("# refreshed my HELLO"),
1854 while (cpos != NULL)
1856 transmit_to_client (cpos,
1857 (const struct GNUNET_MessageHeader *) hello,
1862 GNUNET_free_non_null (our_hello);
1864 GNUNET_PEERINFO_add_peer (peerinfo, our_hello);
1866 while (npos != NULL)
1869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1870 "Transmitting updated `%s' to neighbour `%4s'\n",
1871 "HELLO", GNUNET_i2s (&npos->id));
1873 GNUNET_STATISTICS_update (stats,
1874 gettext_noop ("# transmitted my HELLO to other peers"),
1877 transmit_to_peer (NULL, NULL, 0,
1878 HELLO_ADDRESS_EXPIRATION,
1879 (const char *) our_hello,
1880 GNUNET_HELLO_size(our_hello),
1888 * Task used to clean up expired addresses for a plugin.
1890 * @param cls closure
1894 expire_address_task (void *cls,
1895 const struct GNUNET_SCHEDULER_TaskContext *tc);
1899 * Update the list of addresses for this plugin,
1900 * expiring those that are past their expiration date.
1902 * @param plugin addresses of which plugin should be recomputed?
1903 * @param fresh set to GNUNET_YES if a new address was added
1904 * and we need to regenerate the HELLO even if nobody
1908 update_addresses (struct TransportPlugin *plugin,
1911 static struct GNUNET_TIME_Absolute last_update;
1912 struct GNUNET_TIME_Relative min_remaining;
1913 struct GNUNET_TIME_Relative remaining;
1914 struct GNUNET_TIME_Absolute now;
1915 struct OwnAddressList *pos;
1916 struct OwnAddressList *prev;
1917 struct OwnAddressList *next;
1920 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_TASK)
1921 GNUNET_SCHEDULER_cancel (plugin->address_update_task);
1922 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1923 now = GNUNET_TIME_absolute_get ();
1924 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
1925 expired = (GNUNET_TIME_absolute_get_duration (last_update).rel_value > (HELLO_ADDRESS_EXPIRATION.rel_value / 4));
1927 pos = plugin->addresses;
1931 if (pos->expires.abs_value < now.abs_value)
1933 expired = GNUNET_YES;
1935 plugin->addresses = pos->next;
1937 prev->next = pos->next;
1942 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
1943 if (remaining.rel_value < min_remaining.rel_value)
1944 min_remaining = remaining;
1950 if (expired || fresh)
1955 min_remaining = GNUNET_TIME_relative_min (min_remaining,
1956 GNUNET_TIME_relative_divide (HELLO_ADDRESS_EXPIRATION,
1958 plugin->address_update_task
1959 = GNUNET_SCHEDULER_add_delayed (min_remaining,
1960 &expire_address_task, plugin);
1965 * Task used to clean up expired addresses for a plugin.
1967 * @param cls closure
1971 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1973 struct TransportPlugin *plugin = cls;
1975 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1976 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1977 update_addresses (plugin, GNUNET_NO);
1982 * Iterator over hash map entries that NULLs the session of validation
1983 * entries that match the given session.
1985 * @param cls closure (the 'struct Session*' to match against)
1986 * @param key current key code (peer ID, not used)
1987 * @param value value in the hash map ('struct ValidationEntry*')
1988 * @return GNUNET_YES (we should continue to iterate)
1991 remove_session_validations (void *cls,
1992 const GNUNET_HashCode * key,
1995 struct Session *session = cls;
1996 struct ValidationEntry *ve = value;
1998 if (session == ve->session)
2005 * We've been disconnected from the other peer (for some
2006 * connection-oriented transport). Either quickly
2007 * re-establish the connection or signal the disconnect
2010 * Only signal CORE level disconnect if ALL addresses
2011 * for the peer are exhausted.
2013 * @param p overall plugin context
2014 * @param nl neighbour that was disconnected
2017 try_fast_reconnect (struct TransportPlugin *p,
2018 struct NeighbourList *nl)
2020 /* FIXME-MW: fast reconnect / transport switching not implemented... */
2021 /* Note: the idea here is to hide problems with transports (or
2022 switching between plugins) from the core to eliminate the need to
2023 re-negotiate session keys and the like; OTOH, we should tell core
2024 quickly (much faster than timeout) `if a connection was lost and
2025 could not be re-established (i.e. other peer went down or is
2026 unable / refuses to communicate);
2028 So we should consider:
2029 1) ideally: our own willingness / need to connect
2030 2) prior failures to connect to this peer (by plugin)
2031 3) ideally: reasons why other peer terminated (as far as knowable)
2033 Most importantly, it must be POSSIBLE for another peer to terminate
2034 a connection for a while (without us instantly re-establishing it).
2035 Similarly, if another peer is gone we should quickly notify CORE.
2036 OTOH, if there was a minor glitch (i.e. crash of gnunet-service-transport
2037 on the other end), we should reconnect in such a way that BOTH CORE
2038 services never even notice.
2039 Furthermore, the same mechanism (or small variation) could be used
2040 to switch to a better-performing plugin (ATS).
2042 Finally, this needs to be tested throughly... */
2045 * GNUNET_NO in the call below makes transport disconnect the peer,
2046 * even if only a single address (out of say, six) went away. This
2047 * function must be careful to ONLY disconnect if the peer is gone,
2048 * not just a specifi address.
2050 * More specifically, half the places it was used had it WRONG.
2053 /* No reconnect, signal disconnect instead! */
2054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2055 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2056 "try_fast_reconnect");
2057 disconnect_neighbour (nl, GNUNET_YES);
2062 * Function that will be called whenever the plugin internally
2063 * cleans up a session pointer and hence the service needs to
2064 * discard all of those sessions as well. Plugins that do not
2065 * use sessions can simply omit calling this function and always
2066 * use NULL wherever a session pointer is needed.
2068 * @param cls closure
2069 * @param peer which peer was the session for
2070 * @param session which session is being destoyed
2073 plugin_env_session_end (void *cls,
2074 const struct GNUNET_PeerIdentity *peer,
2075 struct Session *session)
2077 struct TransportPlugin *p = cls;
2078 struct NeighbourList *nl;
2079 struct ReadyList *rl;
2080 struct ForeignAddressList *pos;
2081 struct ForeignAddressList *prev;
2083 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2084 &remove_session_validations,
2086 nl = find_neighbour (peer);
2088 return; /* was never marked as connected */
2092 if (rl->plugin == p)
2097 return; /* was never marked as connected */
2099 pos = rl->addresses;
2100 while ( (pos != NULL) &&
2101 (pos->session != session) )
2107 return; /* was never marked as connected */
2108 pos->session = NULL;
2109 if (pos->addrlen != 0)
2111 if (nl->received_pong != GNUNET_NO)
2112 try_fast_reconnect (p, nl);
2115 /* was inbound connection, free 'pos' */
2117 rl->addresses = pos->next;
2119 prev->next = pos->next;
2120 if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2122 GNUNET_SCHEDULER_cancel (pos->revalidate_task);
2123 pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2126 if (nl->received_pong == GNUNET_NO)
2127 return; /* nothing to do, never connected... */
2128 /* check if we have any validated addresses left */
2129 pos = rl->addresses;
2134 try_fast_reconnect (p, nl);
2139 /* no valid addresses left, signal disconnect! */
2141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2142 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2143 "plugin_env_session_end");
2144 /* FIXME: This doesn't mean there are no addresses left for this PEER,
2145 * it means there aren't any left for this PLUGIN/PEER combination! So
2146 * calling disconnect_neighbor here with GNUNET_NO forces disconnect
2147 * when it isn't necessary. Using GNUNET_YES at least checks to see
2148 * if there are any addresses that work first, so as not to overdo it.
2151 disconnect_neighbour (nl, GNUNET_YES);
2156 * Function that must be called by each plugin to notify the
2157 * transport service about the addresses under which the transport
2158 * provided by the plugin can be reached.
2160 * @param cls closure
2161 * @param name name of the transport that generated the address
2162 * @param addr one of the addresses of the host, NULL for the last address
2163 * the specific address format depends on the transport
2164 * @param addrlen length of the address
2165 * @param expires when should this address automatically expire?
2168 plugin_env_notify_address (void *cls,
2172 struct GNUNET_TIME_Relative expires)
2174 struct TransportPlugin *p = cls;
2175 struct OwnAddressList *al;
2176 struct GNUNET_TIME_Absolute abex;
2178 GNUNET_assert (addr != NULL);
2179 abex = GNUNET_TIME_relative_to_absolute (expires);
2180 GNUNET_assert (p == find_transport (name));
2184 if ((addrlen == al->addrlen) && (0 == memcmp (addr, &al[1], addrlen)))
2187 update_addresses (p, GNUNET_NO);
2193 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2194 al->next = p->addresses;
2197 al->addrlen = addrlen;
2198 memcpy (&al[1], addr, addrlen);
2199 update_addresses (p, GNUNET_YES);
2204 * Notify all of our clients about a peer connecting.
2207 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2208 struct GNUNET_TIME_Relative latency,
2211 struct ConnectInfoMessage * cim;
2212 struct TransportClient *cpos;
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2218 "Notifying clients about connection from `%s'\n",
2221 GNUNET_STATISTICS_update (stats,
2222 gettext_noop ("# peers connected"),
2227 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
2228 cim = GNUNET_malloc (size);
2230 cim->header.size = htons (size);
2231 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2232 cim->ats_count = htonl(2);
2233 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2234 (&(cim->ats))[0].value = htonl (distance);
2235 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
2236 (&(cim->ats))[1].value = htonl ((uint32_t) latency.rel_value);
2237 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2238 (&(cim->ats))[2].value = htonl (0);
2239 memcpy (&cim->id, peer, sizeof (struct GNUNET_PeerIdentity));
2241 while (cpos != NULL)
2243 transmit_to_client (cpos, &(cim->header), GNUNET_NO);
2251 * Notify all of our clients about a peer disconnecting.
2254 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2256 struct DisconnectInfoMessage dim;
2257 struct TransportClient *cpos;
2260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2261 "Notifying clients about lost connection to `%s'\n",
2264 GNUNET_STATISTICS_update (stats,
2265 gettext_noop ("# peers connected"),
2268 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2269 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2270 dim.reserved = htonl (0);
2271 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2273 while (cpos != NULL)
2275 transmit_to_client (cpos, &dim.header, GNUNET_NO);
2282 * Find a ForeignAddressList entry for the given neighbour
2283 * that matches the given address and transport.
2285 * @param neighbour which peer we care about
2286 * @param tname name of the transport plugin
2287 * @param session session to look for, NULL for 'any'; otherwise
2288 * can be used for the service to "learn" this session ID
2290 * @param addr binary address
2291 * @param addrlen length of addr
2292 * @return NULL if no such entry exists
2294 static struct ForeignAddressList *
2295 find_peer_address(struct NeighbourList *neighbour,
2297 struct Session *session,
2301 struct ReadyList *head;
2302 struct ForeignAddressList *pos;
2304 head = neighbour->plugins;
2305 while (head != NULL)
2307 if (0 == strcmp (tname, head->plugin->short_name))
2313 pos = head->addresses;
2314 while ( (pos != NULL) &&
2315 ( (pos->addrlen != addrlen) ||
2316 (memcmp(pos->addr, addr, addrlen) != 0) ) )
2318 if ( (session != NULL) &&
2319 (pos->session == session) )
2323 if ( (session != NULL) && (pos != NULL) )
2324 pos->session = session; /* learn it! */
2330 * Get the peer address struct for the given neighbour and
2331 * address. If it doesn't yet exist, create it.
2333 * @param neighbour which peer we care about
2334 * @param tname name of the transport plugin
2335 * @param session session of the plugin, or NULL for none
2336 * @param addr binary address
2337 * @param addrlen length of addr
2338 * @return NULL if we do not have a transport plugin for 'tname'
2340 static struct ForeignAddressList *
2341 add_peer_address (struct NeighbourList *neighbour,
2343 struct Session *session,
2347 struct ReadyList *head;
2348 struct ForeignAddressList *ret;
2350 ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2353 head = neighbour->plugins;
2355 while (head != NULL)
2357 if (0 == strcmp (tname, head->plugin->short_name))
2363 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2364 ret->session = session;
2367 ret->addr = (const char*) &ret[1];
2368 memcpy (&ret[1], addr, addrlen);
2374 ret->addrlen = addrlen;
2375 ret->expires = GNUNET_TIME_relative_to_absolute
2376 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2377 ret->latency = GNUNET_TIME_relative_get_forever();
2379 ret->timeout = GNUNET_TIME_relative_to_absolute
2380 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2381 ret->ready_list = head;
2382 ret->next = head->addresses;
2383 head->addresses = ret;
2389 * Closure for 'add_validated_address'.
2391 struct AddValidatedAddressContext
2394 * Entry that has been validated.
2396 const struct ValidationEntry *ve;
2399 * Flag set after we have added the address so
2400 * that we terminate the iteration next time.
2407 * Callback function used to fill a buffer of max bytes with a list of
2408 * addresses in the format used by HELLOs. Should use
2409 * "GNUNET_HELLO_add_address" as a helper function.
2411 * @param cls the 'struct AddValidatedAddressContext' with the validated address
2412 * @param max maximum number of bytes that can be written to buf
2413 * @param buf where to write the address information
2414 * @return number of bytes written, 0 to signal the
2415 * end of the iteration.
2418 add_validated_address (void *cls,
2419 size_t max, void *buf)
2421 struct AddValidatedAddressContext *avac = cls;
2422 const struct ValidationEntry *ve = avac->ve;
2424 if (GNUNET_YES == avac->done)
2426 avac->done = GNUNET_YES;
2427 return GNUNET_HELLO_add_address (ve->transport_name,
2428 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2438 * Closure for 'check_address_exists'.
2440 struct CheckAddressExistsClosure
2443 * Address to check for.
2448 * Name of the transport.
2455 struct Session *session;
2458 * Set to GNUNET_YES if the address exists.
2471 * Iterator over hash map entries. Checks if the given
2472 * validation entry is for the same address as what is given
2475 * @param cls the 'struct CheckAddressExistsClosure*'
2476 * @param key current key code (ignored)
2477 * @param value value in the hash map ('struct ValidationEntry')
2478 * @return GNUNET_YES if we should continue to
2479 * iterate (mismatch), GNUNET_NO if not (entry matched)
2482 check_address_exists (void *cls,
2483 const GNUNET_HashCode * key,
2486 struct CheckAddressExistsClosure *caec = cls;
2487 struct ValidationEntry *ve = value;
2489 if ( (0 == strcmp (caec->tname,
2490 ve->transport_name)) &&
2491 (caec->addrlen == ve->addrlen) &&
2492 (0 == memcmp (caec->addr,
2496 caec->exists = GNUNET_YES;
2499 if ( (ve->session != NULL) &&
2500 (caec->session == ve->session) )
2502 caec->exists = GNUNET_YES;
2511 * Iterator to free entries in the validation_map.
2513 * @param cls closure (unused)
2514 * @param key current key code
2515 * @param value value in the hash map (validation to abort)
2516 * @return GNUNET_YES (always)
2519 abort_validation (void *cls,
2520 const GNUNET_HashCode * key,
2523 struct ValidationEntry *va = value;
2525 if (GNUNET_SCHEDULER_NO_TASK != va->timeout_task)
2526 GNUNET_SCHEDULER_cancel (va->timeout_task);
2527 GNUNET_free (va->transport_name);
2528 if (va->chvc != NULL)
2530 va->chvc->ve_count--;
2531 if (va->chvc->ve_count == 0)
2533 GNUNET_CONTAINER_DLL_remove (chvc_head,
2536 GNUNET_free (va->chvc);
2546 * HELLO validation cleanup task (validation failed).
2548 * @param cls the 'struct ValidationEntry' that failed
2549 * @param tc scheduler context (unused)
2552 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2554 struct ValidationEntry *va = cls;
2555 struct GNUNET_PeerIdentity pid;
2557 va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2558 GNUNET_STATISTICS_update (stats,
2559 gettext_noop ("# address validation timeouts"),
2562 GNUNET_CRYPTO_hash (&va->publicKey,
2564 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2566 GNUNET_break (GNUNET_OK ==
2567 GNUNET_CONTAINER_multihashmap_remove (validation_map,
2570 abort_validation (NULL, NULL, va);
2575 neighbour_timeout_task (void *cls,
2576 const struct GNUNET_SCHEDULER_TaskContext *tc)
2578 struct NeighbourList *n = cls;
2581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2582 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
2584 GNUNET_STATISTICS_update (stats,
2585 gettext_noop ("# disconnects due to timeout"),
2588 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2589 disconnect_neighbour (n, GNUNET_NO);
2594 * Schedule the job that will cause us to send a PING to the
2595 * foreign address to evaluate its validity and latency.
2597 * @param fal address to PING
2600 schedule_next_ping (struct ForeignAddressList *fal);
2604 * Add the given address to the list of foreign addresses
2605 * available for the given peer (check for duplicates).
2607 * @param cls the respective 'struct NeighbourList' to update
2608 * @param tname name of the transport
2609 * @param expiration expiration time
2610 * @param addr the address
2611 * @param addrlen length of the address
2612 * @return GNUNET_OK (always)
2615 add_to_foreign_address_list (void *cls,
2617 struct GNUNET_TIME_Absolute expiration,
2621 struct NeighbourList *n = cls;
2622 struct ForeignAddressList *fal;
2625 GNUNET_STATISTICS_update (stats,
2626 gettext_noop ("# valid peer addresses returned by PEERINFO"),
2630 fal = find_peer_address (n, tname, NULL, addr, addrlen);
2633 #if DEBUG_TRANSPORT_HELLO
2634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2635 "Adding address `%s' (%s) for peer `%4s' due to PEERINFO data for %llums.\n",
2636 a2s (tname, addr, addrlen),
2638 GNUNET_i2s (&n->id),
2639 expiration.abs_value);
2641 fal = add_peer_address (n, tname, NULL, addr, addrlen);
2644 GNUNET_STATISTICS_update (stats,
2645 gettext_noop ("# previously validated addresses lacking transport"),
2651 fal->expires = GNUNET_TIME_absolute_max (expiration,
2653 schedule_next_ping (fal);
2659 fal->expires = GNUNET_TIME_absolute_max (expiration,
2664 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2665 "Failed to add new address for `%4s'\n",
2666 GNUNET_i2s (&n->id));
2669 if (fal->validated == GNUNET_NO)
2671 fal->validated = GNUNET_YES;
2672 GNUNET_STATISTICS_update (stats,
2673 gettext_noop ("# peer addresses considered valid"),
2677 if (try == GNUNET_YES)
2679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2680 "Have new addresses, will try to trigger transmissions.\n");
2681 try_transmission_to_peer (n);
2688 * Add addresses in validated HELLO "h" to the set of addresses
2689 * we have for this peer.
2691 * @param cls closure ('struct NeighbourList*')
2692 * @param peer id of the peer, NULL for last call
2693 * @param h hello message for the peer (can be NULL)
2696 add_hello_for_peer (void *cls,
2697 const struct GNUNET_PeerIdentity *peer,
2698 const struct GNUNET_HELLO_Message *h)
2700 struct NeighbourList *n = cls;
2704 GNUNET_STATISTICS_update (stats,
2705 gettext_noop ("# outstanding peerinfo iterate requests"),
2712 return; /* no HELLO available */
2714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2715 "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
2719 if (GNUNET_YES != n->public_key_valid)
2721 GNUNET_HELLO_get_key (h, &n->publicKey);
2722 n->public_key_valid = GNUNET_YES;
2724 GNUNET_HELLO_iterate_addresses (h,
2726 &add_to_foreign_address_list,
2732 * Create a fresh entry in our neighbour list for the given peer.
2733 * Will try to transmit our current HELLO to the new neighbour.
2734 * Do not call this function directly, use 'setup_peer_check_blacklist.
2736 * @param peer the peer for which we create the entry
2737 * @param do_hello should we schedule transmitting a HELLO
2738 * @return the new neighbour list entry
2740 static struct NeighbourList *
2741 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
2744 struct NeighbourList *n;
2745 struct TransportPlugin *tp;
2746 struct ReadyList *rl;
2749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2750 "Setting up state for neighbour `%4s'\n",
2753 GNUNET_assert (our_hello != NULL);
2754 GNUNET_STATISTICS_update (stats,
2755 gettext_noop ("# active neighbours"),
2758 n = GNUNET_malloc (sizeof (struct NeighbourList));
2759 n->next = neighbours;
2763 GNUNET_TIME_relative_to_absolute
2764 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2765 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2766 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2767 MAX_BANDWIDTH_CARRY_S);
2771 if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
2773 rl = GNUNET_malloc (sizeof (struct ReadyList));
2775 rl->next = n->plugins;
2778 rl->addresses = NULL;
2782 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2784 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2785 &neighbour_timeout_task, n);
2788 GNUNET_STATISTICS_update (stats,
2789 gettext_noop ("# peerinfo new neighbor iterate requests"),
2792 GNUNET_STATISTICS_update (stats,
2793 gettext_noop ("# outstanding peerinfo iterate requests"),
2796 n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
2797 GNUNET_TIME_UNIT_FOREVER_REL,
2798 &add_hello_for_peer, n);
2800 GNUNET_STATISTICS_update (stats,
2801 gettext_noop ("# HELLO's sent to new neighbors"),
2804 transmit_to_peer (NULL, NULL, 0,
2805 HELLO_ADDRESS_EXPIRATION,
2806 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
2814 * Function called after we have checked if communicating
2815 * with a given peer is acceptable.
2817 * @param cls closure
2818 * @param n NULL if communication is not acceptable
2820 typedef void (*SetupContinuation)(void *cls,
2821 struct NeighbourList *n);
2825 * Information kept for each client registered to perform
2831 * This is a linked list.
2833 struct Blacklisters *next;
2836 * This is a linked list.
2838 struct Blacklisters *prev;
2841 * Client responsible for this entry.
2843 struct GNUNET_SERVER_Client *client;
2846 * Blacklist check that we're currently performing.
2848 struct BlacklistCheck *bc;
2854 * Head of DLL of blacklisting clients.
2856 static struct Blacklisters *bl_head;
2859 * Tail of DLL of blacklisting clients.
2861 static struct Blacklisters *bl_tail;
2865 * Context we use when performing a blacklist check.
2867 struct BlacklistCheck
2871 * This is a linked list.
2873 struct BlacklistCheck *next;
2876 * This is a linked list.
2878 struct BlacklistCheck *prev;
2881 * Peer being checked.
2883 struct GNUNET_PeerIdentity peer;
2886 * Option for setup neighbour afterwards.
2891 * Continuation to call with the result.
2893 SetupContinuation cont;
2901 * Current transmission request handle for this client, or NULL if no
2902 * request is pending.
2904 struct GNUNET_CONNECTION_TransmitHandle *th;
2907 * Our current position in the blacklisters list.
2909 struct Blacklisters *bl_pos;
2912 * Current task performing the check.
2914 GNUNET_SCHEDULER_TaskIdentifier task;
2919 * Head of DLL of active blacklisting queries.
2921 static struct BlacklistCheck *bc_head;
2924 * Tail of DLL of active blacklisting queries.
2926 static struct BlacklistCheck *bc_tail;
2930 * Perform next action in the blacklist check.
2932 * @param cls the 'struct BlacklistCheck*'
2936 do_blacklist_check (void *cls,
2937 const struct GNUNET_SCHEDULER_TaskContext *tc);
2941 * Transmit blacklist query to the client.
2943 * @param cls the 'struct BlacklistCheck'
2944 * @param size number of bytes allowed
2945 * @param buf where to copy the message
2946 * @return number of bytes copied to buf
2949 transmit_blacklist_message (void *cls,
2953 struct BlacklistCheck *bc = cls;
2954 struct Blacklisters *bl;
2955 struct BlacklistMessage bm;
2960 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
2961 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
2966 bm.header.size = htons (sizeof (struct BlacklistMessage));
2967 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
2968 bm.is_allowed = htonl (0);
2970 memcpy (buf, &bm, sizeof (bm));
2971 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
2977 * Perform next action in the blacklist check.
2979 * @param cls the 'struct BlacklistCheck*'
2983 do_blacklist_check (void *cls,
2984 const struct GNUNET_SCHEDULER_TaskContext *tc)
2986 struct BlacklistCheck *bc = cls;
2987 struct Blacklisters *bl;
2989 bc->task = GNUNET_SCHEDULER_NO_TASK;
2993 bc->cont (bc->cont_cls,
2994 setup_new_neighbour (&bc->peer, bc->do_hello));
3001 bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
3002 sizeof (struct BlacklistMessage),
3003 GNUNET_TIME_UNIT_FOREVER_REL,
3004 &transmit_blacklist_message,
3011 * Obtain a 'struct NeighbourList' for the given peer. If such an entry
3012 * does not yet exist, check the blacklist. If the blacklist says creating
3013 * one is acceptable, create one and call the continuation; otherwise
3014 * call the continuation with NULL.
3016 * @param peer peer to setup or look up a struct NeighbourList for
3017 * @param do_hello should we also schedule sending our HELLO to the peer
3018 * if this is a new record
3019 * @param cont function to call with the 'struct NeigbhbourList*'
3020 * @param cont_cls closure for cont
3023 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
3025 SetupContinuation cont,
3028 struct NeighbourList *n;
3029 struct BlacklistCheck *bc;
3031 n = find_neighbour(peer);
3038 if (bl_head == NULL)
3041 cont (cont_cls, setup_new_neighbour (peer, do_hello));
3043 setup_new_neighbour(peer, do_hello);
3046 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3047 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3049 bc->do_hello = do_hello;
3051 bc->cont_cls = cont_cls;
3052 bc->bl_pos = bl_head;
3053 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3059 * Function called with the result of querying a new blacklister about
3060 * it being allowed (or not) to continue to talk to an existing neighbour.
3062 * @param cls the original 'struct NeighbourList'
3063 * @param n NULL if we need to disconnect
3066 confirm_or_drop_neighbour (void *cls,
3067 struct NeighbourList *n)
3069 struct NeighbourList * orig = cls;
3073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3074 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&orig->id),
3075 "confirm_or_drop_neighboUr");
3076 disconnect_neighbour (orig, GNUNET_NO);
3082 * Handle a request to start a blacklist.
3084 * @param cls closure (always NULL)
3085 * @param client identification of the client
3086 * @param message the actual message
3089 handle_blacklist_init (void *cls,
3090 struct GNUNET_SERVER_Client *client,
3091 const struct GNUNET_MessageHeader *message)
3093 struct Blacklisters *bl;
3094 struct BlacklistCheck *bc;
3095 struct NeighbourList *n;
3100 if (bl->client == client)
3103 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3108 bl = GNUNET_malloc (sizeof (struct Blacklisters));
3109 bl->client = client;
3110 GNUNET_SERVER_client_keep (client);
3111 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
3112 /* confirm that all existing connections are OK! */
3116 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3117 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3119 bc->do_hello = GNUNET_NO;
3120 bc->cont = &confirm_or_drop_neighbour;
3123 if (n == neighbours) /* all would wait for the same client, no need to
3124 create more than just the first task right now */
3125 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3133 * Handle a request to blacklist a peer.
3135 * @param cls closure (always NULL)
3136 * @param client identification of the client
3137 * @param message the actual message
3140 handle_blacklist_reply (void *cls,
3141 struct GNUNET_SERVER_Client *client,
3142 const struct GNUNET_MessageHeader *message)
3144 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
3145 struct Blacklisters *bl;
3146 struct BlacklistCheck *bc;
3149 while ( (bl != NULL) &&
3150 (bl->client != client) )
3154 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3159 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
3161 bc->cont (bc->cont_cls, NULL);
3162 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
3167 bc->bl_pos = bc->bl_pos->next;
3168 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3171 /* check if any other bc's are waiting for this blacklister */
3175 if ( (bc->bl_pos == bl) &&
3176 (GNUNET_SCHEDULER_NO_TASK == bc->task) )
3177 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3185 * Send periodic PING messages to a given foreign address.
3187 * @param cls our 'struct PeriodicValidationContext*'
3188 * @param tc task context
3191 send_periodic_ping (void *cls,
3192 const struct GNUNET_SCHEDULER_TaskContext *tc)
3194 struct ForeignAddressList *peer_address = cls;
3195 struct TransportPlugin *tp;
3196 struct ValidationEntry *va;
3197 struct NeighbourList *neighbour;
3198 struct TransportPingMessage ping;
3199 struct CheckAddressExistsClosure caec;
3201 uint16_t hello_size;
3205 peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3206 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3208 tp = peer_address->ready_list->plugin;
3209 neighbour = peer_address->ready_list->neighbour;
3210 if (GNUNET_YES != neighbour->public_key_valid)
3212 /* no public key yet, try again later */
3213 schedule_next_ping (peer_address);
3216 caec.addr = peer_address->addr;
3217 caec.addrlen = peer_address->addrlen;
3218 caec.tname = tp->short_name;
3219 caec.session = peer_address->session;
3220 caec.exists = GNUNET_NO;
3221 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3222 &check_address_exists,
3224 if (caec.exists == GNUNET_YES)
3226 /* During validation attempts we will likely trigger the other
3227 peer trying to validate our address which in turn will cause
3228 it to send us its HELLO, so we expect to hit this case rather
3229 frequently. Only print something if we are very verbose. */
3230 #if DEBUG_TRANSPORT > 1
3231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3232 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3233 (peer_address->addr != NULL)
3234 ? a2s (tp->short_name,
3236 peer_address->addrlen)
3239 GNUNET_i2s (&neighbour->id));
3241 schedule_next_ping (peer_address);
3244 va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3245 va->transport_name = GNUNET_strdup (tp->short_name);
3246 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
3248 va->send_time = GNUNET_TIME_absolute_get();
3249 va->session = peer_address->session;
3250 if (peer_address->addr != NULL)
3252 va->addr = (const void*) &va[1];
3253 memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3254 va->addrlen = peer_address->addrlen;
3256 memcpy(&va->publicKey,
3257 &neighbour->publicKey,
3258 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3260 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
3261 &timeout_hello_validation,
3263 GNUNET_CONTAINER_multihashmap_put (validation_map,
3264 &neighbour->id.hashPubKey,
3266 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3268 if (peer_address->validated != GNUNET_YES)
3269 hello_size = GNUNET_HELLO_size(our_hello);
3273 tsize = sizeof(struct TransportPingMessage) + hello_size;
3275 if (peer_address->addr != NULL)
3277 slen = strlen (tp->short_name) + 1;
3278 tsize += slen + peer_address->addrlen;
3282 slen = 0; /* make gcc happy */
3284 message_buf = GNUNET_malloc(tsize);
3285 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3286 ping.challenge = htonl(va->challenge);
3287 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3288 if (peer_address->validated != GNUNET_YES)
3290 memcpy(message_buf, our_hello, hello_size);
3293 if (peer_address->addr != NULL)
3295 ping.header.size = htons(sizeof(struct TransportPingMessage) +
3296 peer_address->addrlen +
3298 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3301 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3303 peer_address->addrlen);
3307 ping.header.size = htons(sizeof(struct TransportPingMessage));
3310 memcpy(&message_buf[hello_size],
3312 sizeof(struct TransportPingMessage));
3314 #if DEBUG_TRANSPORT_REVALIDATION
3315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3316 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s'\n",
3317 (peer_address->addr != NULL)
3318 ? a2s (peer_address->plugin->short_name,
3320 peer_address->addrlen)
3323 GNUNET_i2s (&neighbour->id),
3324 "HELLO", hello_size,
3327 if (peer_address->validated != GNUNET_YES)
3328 GNUNET_STATISTICS_update (stats,
3329 gettext_noop ("# PING with HELLO messages sent"),
3333 GNUNET_STATISTICS_update (stats,
3334 gettext_noop ("# PING without HELLO messages sent"),
3337 GNUNET_STATISTICS_update (stats,
3338 gettext_noop ("# PING messages sent for re-validation"),
3341 transmit_to_peer (NULL, peer_address,
3342 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3343 HELLO_VERIFICATION_TIMEOUT,
3345 GNUNET_YES, neighbour);
3346 GNUNET_free(message_buf);
3347 schedule_next_ping (peer_address);
3352 * Schedule the job that will cause us to send a PING to the
3353 * foreign address to evaluate its validity and latency.
3355 * @param fal address to PING
3358 schedule_next_ping (struct ForeignAddressList *fal)
3360 struct GNUNET_TIME_Relative delay;
3362 if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3364 delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3365 delay.rel_value /= 2; /* do before expiration */
3366 delay = GNUNET_TIME_relative_min (delay,
3367 LATENCY_EVALUATION_MAX_DELAY);
3368 if (GNUNET_YES != fal->estimated)
3370 delay = GNUNET_TIME_UNIT_ZERO;
3371 fal->estimated = GNUNET_YES;
3373 if (GNUNET_YES == fal->connected)
3375 delay = GNUNET_TIME_relative_min (delay,
3376 CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3378 /* FIXME: also adjust delay based on how close the last
3379 observed latency is to the latency of the best alternative */
3380 /* bound how fast we can go */
3381 delay = GNUNET_TIME_relative_max (delay,
3382 GNUNET_TIME_UNIT_SECONDS);
3383 /* randomize a bit (to avoid doing all at the same time) */
3384 delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3385 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(delay,
3386 &send_periodic_ping,
3394 * Function that will be called if we receive some payload
3395 * from another peer.
3397 * @param message the payload
3398 * @param n peer who claimed to be the sender
3401 handle_payload_message (const struct GNUNET_MessageHeader *message,
3402 struct NeighbourList *n)
3404 struct InboundMessage *im;
3405 struct TransportClient *cpos;
3408 msize = ntohs (message->size);
3409 if (n->received_pong == GNUNET_NO)
3411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3412 "Received message of type %u and size %u from `%4s', but no pong yet!!\n",
3413 ntohs (message->type),
3414 ntohs (message->size),
3415 GNUNET_i2s (&n->id));
3416 GNUNET_free_non_null (n->pre_connect_message_buffer);
3417 n->pre_connect_message_buffer = GNUNET_malloc (msize);
3418 memcpy (n->pre_connect_message_buffer, message, msize);
3423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3424 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
3425 ntohs (message->type),
3426 ntohs (message->size),
3427 GNUNET_i2s (&n->id));
3429 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3432 n->quota_violation_count++;
3434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3435 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
3436 n->in_tracker.available_bytes_per_s__,
3437 n->quota_violation_count);
3439 /* Discount 32k per violation */
3440 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3445 if (n->quota_violation_count > 0)
3447 /* try to add 32k back */
3448 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3450 n->quota_violation_count--;
3453 GNUNET_STATISTICS_update (stats,
3454 gettext_noop ("# payload received from other peers"),
3457 /* transmit message to all clients */
3458 uint32_t ats_count = 2;
3459 uint16_t size = sizeof (struct InboundMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) + msize;
3461 im = GNUNET_malloc (size);
3462 im->header.size = htons (size);
3463 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3465 im->ats_count = htonl(ats_count);
3466 /* Setting ATS data */
3467 (&(im->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
3468 (&(im->ats))[0].value = htonl (n->distance);
3469 (&(im->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
3470 (&(im->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
3471 (&(im->ats))[ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
3472 (&(im->ats))[ats_count].value = htonl (0);
3474 memcpy (&((&(im->ats))[ats_count+1]), message, msize);
3476 while (cpos != NULL)
3478 transmit_to_client (cpos, &im->header, GNUNET_YES);
3486 * Iterator over hash map entries. Checks if the given validation
3487 * entry is for the same challenge as what is given in the PONG.
3489 * @param cls the 'struct TransportPongMessage*'
3490 * @param key peer identity
3491 * @param value value in the hash map ('struct ValidationEntry')
3492 * @return GNUNET_YES if we should continue to
3493 * iterate (mismatch), GNUNET_NO if not (entry matched)
3496 check_pending_validation (void *cls,
3497 const GNUNET_HashCode * key,
3500 const struct TransportPongMessage *pong = cls;
3501 struct ValidationEntry *ve = value;
3502 struct AddValidatedAddressContext avac;
3503 unsigned int challenge = ntohl(pong->challenge);
3504 struct GNUNET_HELLO_Message *hello;
3505 struct GNUNET_PeerIdentity target;
3506 struct NeighbourList *n;
3507 struct ForeignAddressList *fal;
3508 struct OwnAddressList *oal;
3509 struct TransportPlugin *tp;
3510 struct GNUNET_MessageHeader *prem;
3516 ps = ntohs (pong->header.size);
3517 if (ps < sizeof (struct TransportPongMessage))
3519 GNUNET_break_op (0);
3522 addr = (const char*) &pong[1];
3523 slen = strlen (ve->transport_name) + 1;
3524 if ( (ps - sizeof (struct TransportPongMessage) < slen) ||
3525 (ve->challenge != challenge) ||
3526 (addr[slen-1] != '\0') ||
3527 (0 != strcmp (addr, ve->transport_name)) ||
3528 (ntohl (pong->purpose.size)
3529 != sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3531 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
3532 sizeof (struct GNUNET_PeerIdentity) + ps - sizeof (struct TransportPongMessage)) )
3537 alen = ps - sizeof (struct TransportPongMessage) - slen;
3538 switch (ntohl (pong->purpose.purpose))
3540 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN:
3541 if ( (ve->addrlen + slen != ntohl (pong->addrlen)) ||
3542 (0 != memcmp (&addr[slen],
3546 return GNUNET_YES; /* different entry, keep trying! */
3548 if (0 != memcmp (&pong->pid,
3550 sizeof (struct GNUNET_PeerIdentity)))
3552 GNUNET_break_op (0);
3556 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
3561 GNUNET_break_op (0);
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3567 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
3569 a2s (ve->transport_name,
3570 (const struct sockaddr *) ve->addr,
3572 ve->transport_name);
3575 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING:
3576 if (0 != memcmp (&pong->pid,
3578 sizeof (struct GNUNET_PeerIdentity)))
3580 GNUNET_break_op (0);
3583 if (ve->addrlen != 0)
3585 /* must have been for a different validation entry */
3588 tp = find_transport (ve->transport_name);
3594 oal = tp->addresses;
3597 if ( (oal->addrlen == alen) &&
3598 (0 == memcmp (&oal[1],
3606 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3607 _("Not accepting PONG with address `%s' since I cannot confirm having this address.\n"),
3608 a2s (ve->transport_name,
3614 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING,
3619 GNUNET_break_op (0);
3624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3625 "Confirmed that peer `%4s' is talking to us using address `%s' (%s) for us.\n",
3627 a2s (ve->transport_name,
3630 ve->transport_name);
3634 GNUNET_break_op (0);
3637 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
3639 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3640 _("Received expired signature. Check system time.\n"));
3643 GNUNET_STATISTICS_update (stats,
3644 gettext_noop ("# address validation successes"),
3647 /* create the updated HELLO */
3648 GNUNET_CRYPTO_hash (&ve->publicKey,
3649 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3650 &target.hashPubKey);
3651 if (ve->addr != NULL)
3653 avac.done = GNUNET_NO;
3655 hello = GNUNET_HELLO_create (&ve->publicKey,
3656 &add_validated_address,
3658 GNUNET_PEERINFO_add_peer (peerinfo,
3660 GNUNET_free (hello);
3662 n = find_neighbour (&target);
3665 n->publicKey = ve->publicKey;
3666 n->public_key_valid = GNUNET_YES;
3667 fal = add_peer_address (n,
3672 GNUNET_assert (fal != NULL);
3673 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
3674 fal->validated = GNUNET_YES;
3675 mark_address_connected (fal);
3676 GNUNET_STATISTICS_update (stats,
3677 gettext_noop ("# peer addresses considered valid"),
3680 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
3681 schedule_next_ping (fal);
3682 if (n->latency.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3683 n->latency = fal->latency;
3685 n->latency.rel_value = (fal->latency.rel_value + n->latency.rel_value) / 2;
3687 n->distance = fal->distance;
3688 if (GNUNET_NO == n->received_pong)
3690 n->received_pong = GNUNET_YES;
3691 notify_clients_connect (&target, n->latency, n->distance);
3692 if (NULL != (prem = n->pre_connect_message_buffer))
3694 n->pre_connect_message_buffer = NULL;
3695 handle_payload_message (prem, n);
3699 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
3701 GNUNET_SCHEDULER_cancel (n->retry_task);
3702 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
3703 try_transmission_to_peer (n);
3707 /* clean up validation entry */
3708 GNUNET_assert (GNUNET_YES ==
3709 GNUNET_CONTAINER_multihashmap_remove (validation_map,
3712 abort_validation (NULL, NULL, ve);
3718 * Function that will be called if we receive a validation
3719 * of an address challenge that we transmitted to another
3720 * peer. Note that the validation should only be considered
3721 * acceptable if the challenge matches AND if the sender
3722 * address is at least a plausible address for this peer
3723 * (otherwise we may be seeing a MiM attack).
3725 * @param cls closure
3726 * @param message the pong message
3727 * @param peer who responded to our challenge
3728 * @param sender_address string describing our sender address (as observed
3729 * by the other peer in binary format)
3730 * @param sender_address_len number of bytes in 'sender_address'
3733 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
3734 const struct GNUNET_PeerIdentity *peer,
3735 const char *sender_address,
3736 size_t sender_address_len)
3738 #if DEBUG_TRANSPORT > 1
3739 /* we get tons of these that just get discarded, only log
3740 if we are quite verbose */
3741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3742 "Receiving `%s' message from `%4s'.\n", "PONG",
3745 GNUNET_STATISTICS_update (stats,
3746 gettext_noop ("# PONG messages received"),
3749 if (GNUNET_SYSERR !=
3750 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
3752 &check_pending_validation,
3755 /* This is *expected* to happen a lot since we send
3756 PONGs to *all* known addresses of the sender of
3757 the PING, so most likely we get multiple PONGs
3758 per PING, and all but the first PONG will end up
3759 here. So really we should not print anything here
3760 unless we want to be very, very verbose... */
3761 #if DEBUG_TRANSPORT > 2
3762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3763 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
3775 * Try to validate a neighbour's address by sending him our HELLO and a PING.
3777 * @param cls the 'struct ValidationEntry*'
3778 * @param neighbour neighbour to validate, NULL if validation failed
3781 transmit_hello_and_ping (void *cls,
3782 struct NeighbourList *neighbour)
3784 struct ValidationEntry *va = cls;
3785 struct ForeignAddressList *peer_address;
3786 struct TransportPingMessage ping;
3787 uint16_t hello_size;
3790 struct GNUNET_PeerIdentity id;
3793 GNUNET_CRYPTO_hash (&va->publicKey,
3794 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3796 if (neighbour == NULL)
3798 /* FIXME: stats... */
3799 GNUNET_break (GNUNET_OK ==
3800 GNUNET_CONTAINER_multihashmap_remove (validation_map,
3803 abort_validation (NULL, NULL, va);
3806 neighbour->publicKey = va->publicKey;
3807 neighbour->public_key_valid = GNUNET_YES;
3808 peer_address = add_peer_address (neighbour,
3809 va->transport_name, NULL,
3810 (const void*) &va[1],
3812 if (peer_address == NULL)
3814 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3815 "Failed to add peer `%4s' for plugin `%s'\n",
3816 GNUNET_i2s (&neighbour->id),
3817 va->transport_name);
3818 GNUNET_break (GNUNET_OK ==
3819 GNUNET_CONTAINER_multihashmap_remove (validation_map,
3822 abort_validation (NULL, NULL, va);
3825 hello_size = GNUNET_HELLO_size(our_hello);
3826 slen = strlen(va->transport_name) + 1;
3827 tsize = sizeof(struct TransportPingMessage) + hello_size + va->addrlen + slen;
3828 message_buf = GNUNET_malloc(tsize);
3829 ping.challenge = htonl(va->challenge);
3830 ping.header.size = htons(sizeof(struct TransportPingMessage) + slen + va->addrlen);
3831 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3832 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3833 memcpy(message_buf, our_hello, hello_size);
3834 memcpy(&message_buf[hello_size],
3836 sizeof(struct TransportPingMessage));
3837 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3840 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3845 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
3848 : a2s (va->transport_name,
3849 (const void*) &va[1], va->addrlen),
3851 GNUNET_i2s (&neighbour->id),
3852 "HELLO", hello_size,
3853 "PING", sizeof (struct TransportPingMessage) + va->addrlen + slen);
3856 GNUNET_STATISTICS_update (stats,
3857 gettext_noop ("# PING messages sent for initial validation"),
3860 transmit_to_peer (NULL, peer_address,
3861 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3862 HELLO_VERIFICATION_TIMEOUT,
3864 GNUNET_YES, neighbour);
3865 GNUNET_free(message_buf);
3870 * Check if the given address is already being validated; if not,
3871 * append the given address to the list of entries that are being be
3872 * validated and initiate validation.
3874 * @param cls closure ('struct CheckHelloValidatedContext *')
3875 * @param tname name of the transport
3876 * @param expiration expiration time
3877 * @param addr the address
3878 * @param addrlen length of the address
3879 * @return GNUNET_OK (always)
3882 run_validation (void *cls,
3884 struct GNUNET_TIME_Absolute expiration,
3888 struct CheckHelloValidatedContext *chvc = cls;
3889 struct GNUNET_PeerIdentity id;
3890 struct TransportPlugin *tp;
3891 struct ValidationEntry *va;
3892 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
3893 struct CheckAddressExistsClosure caec;
3894 struct OwnAddressList *oal;
3896 GNUNET_assert (addr != NULL);
3898 GNUNET_STATISTICS_update (stats,
3899 gettext_noop ("# peer addresses scheduled for validation"),
3902 tp = find_transport (tname);
3905 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
3906 GNUNET_ERROR_TYPE_BULK,
3908 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
3910 GNUNET_STATISTICS_update (stats,
3911 gettext_noop ("# peer addresses not validated (plugin not available)"),
3916 /* check if this is one of our own addresses */
3917 oal = tp->addresses;
3920 if ( (oal->addrlen == addrlen) &&
3921 (0 == memcmp (&oal[1],
3925 /* not plausible, this address is equivalent to our own address! */
3926 GNUNET_STATISTICS_update (stats,
3927 gettext_noop ("# peer addresses not validated (loopback)"),
3934 GNUNET_HELLO_get_key (chvc->hello, &pk);
3935 GNUNET_CRYPTO_hash (&pk,
3937 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3940 if (is_blacklisted(&id, tp))
3943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3944 "Attempted to validate blacklisted peer `%s' using `%s'!\n",
3952 caec.addrlen = addrlen;
3953 caec.session = NULL;
3955 caec.exists = GNUNET_NO;
3956 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3957 &check_address_exists,
3959 if (caec.exists == GNUNET_YES)
3961 /* During validation attempts we will likely trigger the other
3962 peer trying to validate our address which in turn will cause
3963 it to send us its HELLO, so we expect to hit this case rather
3964 frequently. Only print something if we are very verbose. */
3965 #if DEBUG_TRANSPORT > 1
3966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3967 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3968 a2s (tname, addr, addrlen),
3972 GNUNET_STATISTICS_update (stats,
3973 gettext_noop ("# peer addresses not validated (in progress)"),
3978 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
3981 va->transport_name = GNUNET_strdup (tname);
3982 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
3984 va->send_time = GNUNET_TIME_absolute_get();
3985 va->addr = (const void*) &va[1];
3986 memcpy (&va[1], addr, addrlen);
3987 va->addrlen = addrlen;
3988 GNUNET_HELLO_get_key (chvc->hello,
3990 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
3991 &timeout_hello_validation,
3993 GNUNET_CONTAINER_multihashmap_put (validation_map,
3996 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3997 setup_peer_check_blacklist (&id, GNUNET_NO,
3998 &transmit_hello_and_ping,
4005 * Check if addresses in validated hello "h" overlap with
4006 * those in "chvc->hello" and validate the rest.
4008 * @param cls closure
4009 * @param peer id of the peer, NULL for last call
4010 * @param h hello message for the peer (can be NULL)
4013 check_hello_validated (void *cls,
4014 const struct GNUNET_PeerIdentity *peer,
4015 const struct GNUNET_HELLO_Message *h)
4017 struct CheckHelloValidatedContext *chvc = cls;
4018 struct GNUNET_HELLO_Message *plain_hello;
4019 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4020 struct GNUNET_PeerIdentity target;
4021 struct NeighbourList *n;
4025 GNUNET_STATISTICS_update (stats,
4026 gettext_noop ("# outstanding peerinfo iterate requests"),
4030 if (GNUNET_NO == chvc->hello_known)
4032 /* notify PEERINFO about the peer now, so that we at least
4033 have the public key if some other component needs it */
4034 GNUNET_HELLO_get_key (chvc->hello, &pk);
4035 GNUNET_CRYPTO_hash (&pk,
4036 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4037 &target.hashPubKey);
4038 plain_hello = GNUNET_HELLO_create (&pk,
4041 GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
4042 GNUNET_free (plain_hello);
4043 #if DEBUG_TRANSPORT_HELLO
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4045 "PEERINFO had no `%s' message for peer `%4s', full validation needed.\n",
4047 GNUNET_i2s (&target));
4049 GNUNET_STATISTICS_update (stats,
4050 gettext_noop ("# new HELLOs requiring full validation"),
4053 GNUNET_HELLO_iterate_addresses (chvc->hello,
4060 GNUNET_STATISTICS_update (stats,
4061 gettext_noop ("# duplicate HELLO (peer known)"),
4066 if (chvc->ve_count == 0)
4068 GNUNET_CONTAINER_DLL_remove (chvc_head,
4077 #if DEBUG_TRANSPORT_HELLO
4078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4079 "PEERINFO had `%s' message for peer `%4s', validating only new addresses.\n",
4083 chvc->hello_known = GNUNET_YES;
4084 n = find_neighbour (peer);
4087 #if DEBUG_TRANSPORT_HELLO
4088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4089 "Calling hello_iterate_addresses for %s!\n",
4092 GNUNET_HELLO_iterate_addresses (h,
4094 &add_to_foreign_address_list,
4096 try_transmission_to_peer (n);
4100 #if DEBUG_TRANSPORT_HELLO
4101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4102 "No existing neighbor record for %s!\n",
4105 GNUNET_STATISTICS_update (stats,
4106 gettext_noop ("# no existing neighbour record (validating HELLO)"),
4110 GNUNET_STATISTICS_update (stats,
4111 gettext_noop ("# HELLO validations (update case)"),
4114 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
4116 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
4123 * Process HELLO-message.
4125 * @param plugin transport involved, may be NULL
4126 * @param message the actual message
4127 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
4130 process_hello (struct TransportPlugin *plugin,
4131 const struct GNUNET_MessageHeader *message)
4134 struct GNUNET_PeerIdentity target;
4135 const struct GNUNET_HELLO_Message *hello;
4136 struct CheckHelloValidatedContext *chvc;
4137 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
4138 #if DEBUG_TRANSPORT_HELLO > 2
4141 hsize = ntohs (message->size);
4142 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
4143 (hsize < sizeof (struct GNUNET_MessageHeader)))
4146 return GNUNET_SYSERR;
4148 GNUNET_STATISTICS_update (stats,
4149 gettext_noop ("# HELLOs received for validation"),
4153 /* first, check if load is too high */
4154 if (GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
4156 GNUNET_STATISTICS_update (stats,
4157 gettext_noop ("# HELLOs ignored due to high load"),
4160 #if DEBUG_TRANSPORT_HELLO
4161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4162 "Ignoring `%s' for `%4s', load too high.\n",
4164 GNUNET_i2s (&target));
4168 hello = (const struct GNUNET_HELLO_Message *) message;
4169 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
4171 #if DEBUG_TRANSPORT_HELLO
4172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4173 "Unable to get public key from `%s' for `%4s'!\n",
4175 GNUNET_i2s (&target));
4177 GNUNET_break_op (0);
4178 return GNUNET_SYSERR;
4181 GNUNET_CRYPTO_hash (&publicKey,
4182 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4183 &target.hashPubKey);
4185 #if DEBUG_TRANSPORT_HELLO
4186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4187 "Received `%s' message for `%4s'\n",
4189 GNUNET_i2s (&target));
4192 if (0 == memcmp (&my_identity,
4194 sizeof (struct GNUNET_PeerIdentity)))
4196 GNUNET_STATISTICS_update (stats,
4197 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
4203 while (NULL != chvc)
4205 if (GNUNET_HELLO_equals (hello,
4207 GNUNET_TIME_absolute_get ()).abs_value > 0)
4209 #if DEBUG_TRANSPORT_HELLO > 2
4210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4211 "Received duplicate `%s' message for `%4s'; ignored\n",
4213 GNUNET_i2s (&target));
4215 return GNUNET_OK; /* validation already pending */
4217 if (GNUNET_HELLO_size(hello) == GNUNET_HELLO_size (chvc->hello))
4218 GNUNET_break (0 != memcmp (hello, chvc->hello,
4219 GNUNET_HELLO_size(hello)));
4224 struct NeighbourList *temp_neighbor = find_neighbour(&target);
4225 if ((NULL != temp_neighbor))
4227 fprintf(stderr, "Already know peer, ignoring hello\n");
4232 #if DEBUG_TRANSPORT_HELLO > 2
4235 my_id = GNUNET_strdup(GNUNET_i2s(plugin->env.my_identity));
4236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4237 "%s: Starting validation of `%s' message for `%4s' via '%s' of size %u\n",
4240 GNUNET_i2s (&target),
4242 GNUNET_HELLO_size(hello));
4246 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
4248 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
4249 memcpy (&chvc[1], hello, hsize);
4250 GNUNET_CONTAINER_DLL_insert (chvc_head,
4253 /* finally, check if HELLO was previously validated
4254 (continuation will then schedule actual validation) */
4255 GNUNET_STATISTICS_update (stats,
4256 gettext_noop ("# peerinfo process hello iterate requests"),
4259 GNUNET_STATISTICS_update (stats,
4260 gettext_noop ("# outstanding peerinfo iterate requests"),
4263 chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
4265 HELLO_VERIFICATION_TIMEOUT,
4266 &check_hello_validated, chvc);
4272 * The peer specified by the given neighbour has timed-out or a plugin
4273 * has disconnected. We may either need to do nothing (other plugins
4274 * still up), or trigger a full disconnect and clean up. This
4275 * function updates our state and does the necessary notifications.
4276 * Also notifies our clients that the neighbour is now officially
4279 * @param n the neighbour list entry for the peer
4280 * @param check GNUNET_YES to check if ALL addresses for this peer
4281 * are gone, GNUNET_NO to force a disconnect of the peer
4282 * regardless of whether other addresses exist.
4285 disconnect_neighbour (struct NeighbourList *n, int check)
4287 struct ReadyList *rpos;
4288 struct NeighbourList *npos;
4289 struct NeighbourList *nprev;
4290 struct MessageQueue *mq;
4291 struct ForeignAddressList *peer_addresses;
4292 struct ForeignAddressList *peer_pos;
4294 if (GNUNET_YES == check)
4297 while (NULL != rpos)
4299 peer_addresses = rpos->addresses;
4300 while (peer_addresses != NULL)
4302 if (GNUNET_YES == peer_addresses->connected)
4304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4305 "NOT Disconnecting from `%4s', still have live addresses!\n",
4306 GNUNET_i2s (&n->id));
4307 return; /* still connected */
4309 peer_addresses = peer_addresses->next;
4315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4316 "Disconnecting from `%4s'\n",
4317 GNUNET_i2s (&n->id));
4319 /* remove n from neighbours list */
4322 while ((npos != NULL) && (npos != n))
4327 GNUNET_assert (npos != NULL);
4329 neighbours = n->next;
4331 nprev->next = n->next;
4333 /* notify all clients about disconnect */
4334 if (GNUNET_YES == n->received_pong)
4335 notify_clients_disconnect (&n->id);
4337 /* clean up all plugins, cancel connections and pending transmissions */
4338 while (NULL != (rpos = n->plugins))
4340 n->plugins = rpos->next;
4341 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
4342 while (rpos->addresses != NULL)
4344 peer_pos = rpos->addresses;
4345 rpos->addresses = peer_pos->next;
4346 if (peer_pos->connected == GNUNET_YES)
4347 GNUNET_STATISTICS_update (stats,
4348 gettext_noop ("# connected addresses"),
4351 if (GNUNET_YES == peer_pos->validated)
4352 GNUNET_STATISTICS_update (stats,
4353 gettext_noop ("# peer addresses considered valid"),
4356 if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
4358 GNUNET_SCHEDULER_cancel (peer_pos->revalidate_task);
4359 peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
4361 GNUNET_free(peer_pos);
4366 /* free all messages on the queue */
4367 while (NULL != (mq = n->messages_head))
4369 GNUNET_STATISTICS_update (stats,
4370 gettext_noop ("# bytes in message queue for other peers"),
4371 - (int64_t) mq->message_buf_size,
4373 GNUNET_STATISTICS_update (stats,
4374 gettext_noop ("# bytes discarded due to disconnect"),
4375 mq->message_buf_size,
4377 GNUNET_CONTAINER_DLL_remove (n->messages_head,
4380 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4382 sizeof(struct GNUNET_PeerIdentity)));
4385 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
4387 GNUNET_SCHEDULER_cancel (n->timeout_task);
4388 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4390 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4392 GNUNET_SCHEDULER_cancel (n->retry_task);
4393 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4395 if (n->piter != NULL)
4397 GNUNET_PEERINFO_iterate_cancel (n->piter);
4398 GNUNET_STATISTICS_update (stats,
4399 gettext_noop ("# outstanding peerinfo iterate requests"),
4404 /* finally, free n itself */
4405 GNUNET_STATISTICS_update (stats,
4406 gettext_noop ("# active neighbours"),
4409 GNUNET_free_non_null (n->pre_connect_message_buffer);
4415 * We have received a PING message from someone. Need to send a PONG message
4416 * in response to the peer by any means necessary.
4419 handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
4420 const struct GNUNET_PeerIdentity *peer,
4421 struct Session *session,
4422 const char *sender_address,
4423 uint16_t sender_address_len)
4425 struct TransportPlugin *plugin = cls;
4426 struct SessionHeader *session_header = (struct SessionHeader*) session;
4427 struct TransportPingMessage *ping;
4428 struct TransportPongMessage *pong;
4429 struct NeighbourList *n;
4430 struct ReadyList *rl;
4431 struct ForeignAddressList *fal;
4432 struct OwnAddressList *oal;
4437 if (ntohs (message->size) < sizeof (struct TransportPingMessage))
4439 GNUNET_break_op (0);
4440 return GNUNET_SYSERR;
4443 ping = (struct TransportPingMessage *) message;
4444 if (0 != memcmp (&ping->target,
4445 plugin->env.my_identity,
4446 sizeof (struct GNUNET_PeerIdentity)))
4448 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4449 _("Received `%s' message from `%s' destined for `%s' which is not me!\n"),
4451 (sender_address != NULL)
4452 ? a2s (plugin->short_name,
4453 (const struct sockaddr *)sender_address,
4456 GNUNET_i2s (&ping->target));
4457 return GNUNET_SYSERR;
4460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4461 "Processing `%s' from `%s'\n",
4463 (sender_address != NULL)
4464 ? a2s (plugin->short_name,
4465 (const struct sockaddr *)sender_address,
4469 GNUNET_STATISTICS_update (stats,
4470 gettext_noop ("# PING messages received"),
4473 addr = (const char*) &ping[1];
4474 alen = ntohs (message->size) - sizeof (struct TransportPingMessage);
4475 slen = strlen (plugin->short_name) + 1;
4478 /* peer wants to confirm that we have an outbound connection to him */
4479 if (session == NULL)
4481 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4482 _("Refusing to create PONG since I do not have a session with `%s'.\n"),
4484 return GNUNET_SYSERR;
4486 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len + slen);
4487 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len + slen);
4488 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
4489 pong->purpose.size =
4490 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4492 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
4493 sizeof (struct GNUNET_PeerIdentity) + sender_address_len + slen);
4494 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING);
4495 pong->challenge = ping->challenge;
4496 pong->addrlen = htonl(sender_address_len + slen);
4499 sizeof(struct GNUNET_PeerIdentity));
4503 memcpy (&((char*)&pong[1])[slen],
4505 sender_address_len);
4506 if (GNUNET_TIME_absolute_get_remaining (session_header->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
4508 /* create / update cached sig */
4510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4511 "Creating PONG signature to indicate active connection.\n");
4513 session_header->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
4514 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
4515 GNUNET_assert (GNUNET_OK ==
4516 GNUNET_CRYPTO_rsa_sign (my_private_key,
4518 &session_header->pong_signature));
4522 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
4524 memcpy (&pong->signature,
4525 &session_header->pong_signature,
4526 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4532 /* peer wants to confirm that this is one of our addresses */
4536 plugin->api->check_address (plugin->api->cls,
4540 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4541 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
4542 a2s (plugin->short_name,
4547 oal = plugin->addresses;
4550 if ( (oal->addrlen == alen) &&
4557 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
4558 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
4559 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
4560 pong->purpose.size =
4561 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4563 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
4564 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
4565 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
4566 pong->challenge = ping->challenge;
4567 pong->addrlen = htonl(alen + slen);
4570 sizeof(struct GNUNET_PeerIdentity));
4571 memcpy (&pong[1], plugin->short_name, slen);
4572 memcpy (&((char*)&pong[1])[slen], addr, alen);
4573 if ( (oal != NULL) &&
4574 (GNUNET_TIME_absolute_get_remaining (oal->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4) )
4576 /* create / update cached sig */
4578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4579 "Creating PONG signature to indicate ownership.\n");
4581 oal->pong_sig_expires = GNUNET_TIME_absolute_min (oal->expires,
4582 GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
4583 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
4584 GNUNET_assert (GNUNET_OK ==
4585 GNUNET_CRYPTO_rsa_sign (my_private_key,
4587 &oal->pong_signature));
4588 memcpy (&pong->signature,
4589 &oal->pong_signature,
4590 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4592 else if (oal == NULL)
4594 /* not using cache (typically DV-only) */
4595 pong->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
4596 GNUNET_assert (GNUNET_OK ==
4597 GNUNET_CRYPTO_rsa_sign (my_private_key,
4603 /* can used cached version */
4604 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
4605 memcpy (&pong->signature,
4606 &oal->pong_signature,
4607 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4610 n = find_neighbour(peer);
4611 GNUNET_assert (n != NULL);
4612 /* first try reliable response transmission */
4616 fal = rl->addresses;
4619 if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
4622 ntohs (pong->header.size),
4623 TRANSPORT_PONG_PRIORITY,
4624 HELLO_VERIFICATION_TIMEOUT,
4632 GNUNET_STATISTICS_update (stats,
4633 gettext_noop ("# PONGs unicast via reliable transport"),
4643 /* no reliable method found, do multicast */
4644 GNUNET_STATISTICS_update (stats,
4645 gettext_noop ("# PONGs multicast to all available addresses"),
4651 fal = rl->addresses;
4654 transmit_to_peer(NULL, fal,
4655 TRANSPORT_PONG_PRIORITY,
4656 HELLO_VERIFICATION_TIMEOUT,
4658 ntohs(pong->header.size),
4671 * Function called by the plugin for each received message.
4672 * Update data volumes, possibly notify plugins about
4673 * reducing the rate at which they read from the socket
4674 * and generally forward to our receive callback.
4676 * @param cls the "struct TransportPlugin *" we gave to the plugin
4677 * @param peer (claimed) identity of the other peer
4678 * @param message the message, NULL if we only care about
4679 * learning about the delay until we should receive again
4680 * @param distance in overlay hops; use 1 unless DV (or 0 if message == NULL)
4681 * @param session identifier used for this session (can be NULL)
4682 * @param sender_address binary address of the sender (if observed)
4683 * @param sender_address_len number of bytes in sender_address
4684 * @return how long in ms the plugin should wait until receiving more data
4685 * (plugins that do not support this, can ignore the return value)
4687 static struct GNUNET_TIME_Relative
4688 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
4689 const struct GNUNET_MessageHeader *message,
4691 struct Session *session,
4692 const char *sender_address,
4693 uint16_t sender_address_len)
4695 struct TransportPlugin *plugin = cls;
4696 struct ReadyList *service_context;
4697 struct ForeignAddressList *peer_address;
4699 struct NeighbourList *n;
4700 struct GNUNET_TIME_Relative ret;
4701 if (is_blacklisted (peer, plugin))
4702 return GNUNET_TIME_UNIT_FOREVER_REL;
4704 n = find_neighbour (peer);
4706 n = setup_new_neighbour (peer, GNUNET_YES);
4707 service_context = n->plugins;
4708 while ((service_context != NULL) && (plugin != service_context->plugin))
4709 service_context = service_context->next;
4710 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
4711 peer_address = NULL;
4712 if (message != NULL)
4714 if ( (session != NULL) ||
4715 (sender_address != NULL) )
4716 peer_address = add_peer_address (n,
4720 sender_address_len);
4721 if (peer_address != NULL)
4723 peer_address->distance = distance;
4724 if (GNUNET_YES == peer_address->validated)
4725 mark_address_connected (peer_address);
4726 peer_address->timeout
4728 GNUNET_TIME_relative_to_absolute
4729 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
4730 schedule_next_ping (peer_address);
4732 /* update traffic received amount ... */
4733 msize = ntohs (message->size);
4734 GNUNET_STATISTICS_update (stats,
4735 gettext_noop ("# bytes received from other peers"),
4738 n->distance = distance;
4740 GNUNET_TIME_relative_to_absolute
4741 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
4742 GNUNET_SCHEDULER_cancel (n->timeout_task);
4744 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
4745 &neighbour_timeout_task, n);
4746 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
4748 /* dropping message due to frequent inbound volume violations! */
4749 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
4750 GNUNET_ERROR_TYPE_BULK,
4752 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
4753 n->in_tracker.available_bytes_per_s__,
4754 n->quota_violation_count);
4755 GNUNET_STATISTICS_update (stats,
4756 gettext_noop ("# bandwidth quota violations by other peers"),
4759 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
4763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4764 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
4765 ntohs (message->type),
4766 ntohs (message->size),
4769 switch (ntohs (message->type))
4771 case GNUNET_MESSAGE_TYPE_HELLO:
4772 GNUNET_STATISTICS_update (stats,
4773 gettext_noop ("# HELLO messages received from other peers"),
4776 process_hello (plugin, message);
4778 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
4779 handle_ping (plugin, message, peer, session, sender_address, sender_address_len);
4781 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
4782 handle_pong (plugin, message, peer, sender_address, sender_address_len);
4785 handle_payload_message (message, n);
4789 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
4790 if (ret.rel_value > 0)
4792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4793 "Throttling read (%llu bytes excess at %u b/s), waiting %llums before reading more.\n",
4794 (unsigned long long) n->in_tracker.consumption_since_last_update__,
4795 (unsigned int) n->in_tracker.available_bytes_per_s__,
4796 (unsigned long long) ret.rel_value);
4797 GNUNET_STATISTICS_update (stats,
4798 gettext_noop ("# ms throttling suggested"),
4799 (int64_t) ret.rel_value,
4806 * Handle START-message. This is the first message sent to us
4807 * by any client which causes us to add it to our list.
4809 * @param cls closure (always NULL)
4810 * @param client identification of the client
4811 * @param message the actual message
4814 handle_start (void *cls,
4815 struct GNUNET_SERVER_Client *client,
4816 const struct GNUNET_MessageHeader *message)
4818 const struct StartMessage *start;
4819 struct TransportClient *c;
4820 struct ConnectInfoMessage cim;
4821 struct NeighbourList *n;
4823 start = (const struct StartMessage*) message;
4825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4826 "Received `%s' request from client\n", "START");
4831 if (c->client == client)
4833 /* client already on our list! */
4835 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4840 if ( (GNUNET_NO != ntohl (start->do_check)) &&
4841 (0 != memcmp (&start->self,
4843 sizeof (struct GNUNET_PeerIdentity))) )
4845 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4846 _("Rejecting control connection from peer `%s', which is not me!\n"),
4847 GNUNET_i2s (&start->self));
4848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4851 c = GNUNET_malloc (sizeof (struct TransportClient));
4855 if (our_hello != NULL)
4858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4859 "Sending our own `%s' to new client\n", "HELLO");
4861 transmit_to_client (c,
4862 (const struct GNUNET_MessageHeader *) our_hello,
4864 /* tell new client about all existing connections */
4865 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
4866 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
4867 cim.ats_count = htonl(0);
4868 cim.ats.type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
4869 cim.ats.value = htonl (0);
4873 if (GNUNET_YES == n->received_pong)
4876 transmit_to_client (c, &cim.header, GNUNET_NO);
4881 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4886 * Handle HELLO-message.
4888 * @param cls closure (always NULL)
4889 * @param client identification of the client
4890 * @param message the actual message
4893 handle_hello (void *cls,
4894 struct GNUNET_SERVER_Client *client,
4895 const struct GNUNET_MessageHeader *message)
4899 GNUNET_STATISTICS_update (stats,
4900 gettext_noop ("# HELLOs received from clients"),
4903 ret = process_hello (NULL, message);
4904 GNUNET_SERVER_receive_done (client, ret);
4909 * Closure for 'transmit_client_message'; followed by
4910 * 'msize' bytes of the actual message.
4912 struct TransmitClientMessageContext
4915 * Client on whom's behalf we are sending.
4917 struct GNUNET_SERVER_Client *client;
4920 * Timeout for the transmission.
4922 struct GNUNET_TIME_Absolute timeout;
4930 * Size of the message in bytes.
4937 * Schedule transmission of a message we got from a client to a peer.
4939 * @param cls the 'struct TransmitClientMessageContext*'
4940 * @param n destination, or NULL on error (in that case, drop the message)
4943 transmit_client_message (void *cls,
4944 struct NeighbourList *n)
4946 struct TransmitClientMessageContext *tcmc = cls;
4947 struct TransportClient *tc;
4950 while ((tc != NULL) && (tc->client != tcmc->client))
4955 transmit_to_peer (tc, NULL, tcmc->priority,
4956 GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
4958 tcmc->msize, GNUNET_NO, n);
4960 GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
4961 GNUNET_SERVER_client_drop (tcmc->client);
4967 * Handle SEND-message.
4969 * @param cls closure (always NULL)
4970 * @param client identification of the client
4971 * @param message the actual message
4974 handle_send (void *cls,
4975 struct GNUNET_SERVER_Client *client,
4976 const struct GNUNET_MessageHeader *message)
4978 const struct OutboundMessage *obm;
4979 const struct GNUNET_MessageHeader *obmm;
4980 struct TransmitClientMessageContext *tcmc;
4984 size = ntohs (message->size);
4986 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
4989 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4992 GNUNET_STATISTICS_update (stats,
4993 gettext_noop ("# payload received for other peers"),
4996 obm = (const struct OutboundMessage *) message;
4997 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
4998 msize = size - sizeof (struct OutboundMessage);
5000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5001 "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
5002 "SEND", GNUNET_i2s (&obm->peer),
5006 tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
5007 tcmc->client = client;
5008 tcmc->priority = ntohl (obm->priority);
5009 tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
5010 tcmc->msize = msize;
5011 /* FIXME: this memcpy can be up to 7% of our total runtime */
5012 memcpy (&tcmc[1], obmm, msize);
5013 GNUNET_SERVER_client_keep (client);
5014 setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
5015 &transmit_client_message,
5021 * Handle request connect message
5023 * @param cls closure (always NULL)
5024 * @param client identification of the client
5025 * @param message the actual message
5028 handle_request_connect (void *cls,
5029 struct GNUNET_SERVER_Client *client,
5030 const struct GNUNET_MessageHeader *message)
5032 const struct TransportRequestConnectMessage *trcm =
5033 (const struct TransportRequestConnectMessage *) message;
5035 GNUNET_STATISTICS_update (stats,
5036 gettext_noop ("# REQUEST CONNECT messages received"),
5039 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a request connect message for peer %s\n", GNUNET_i2s(&trcm->peer));
5040 setup_peer_check_blacklist (&trcm->peer, GNUNET_YES,
5042 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5046 * Handle SET_QUOTA-message.
5048 * @param cls closure (always NULL)
5049 * @param client identification of the client
5050 * @param message the actual message
5053 handle_set_quota (void *cls,
5054 struct GNUNET_SERVER_Client *client,
5055 const struct GNUNET_MessageHeader *message)
5057 const struct QuotaSetMessage *qsm =
5058 (const struct QuotaSetMessage *) message;
5059 struct NeighbourList *n;
5061 GNUNET_STATISTICS_update (stats,
5062 gettext_noop ("# SET QUOTA messages received"),
5065 n = find_neighbour (&qsm->peer);
5068 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5069 GNUNET_STATISTICS_update (stats,
5070 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
5076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5077 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
5079 (unsigned int) ntohl (qsm->quota.value__),
5080 (unsigned int) n->in_tracker.available_bytes_per_s__,
5081 GNUNET_i2s (&qsm->peer));
5083 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
5085 if (0 == ntohl (qsm->quota.value__))
5087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5088 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&n->id),
5090 disconnect_neighbour (n, GNUNET_NO);
5092 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5097 * Take the given address and append it to the set of results sent back to
5100 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
5101 * @param address the resolved name, NULL to indicate the last response
5104 transmit_address_to_client (void *cls, const char *address)
5106 struct GNUNET_SERVER_TransmitContext *tc = cls;
5109 if (NULL == address)
5112 slen = strlen (address) + 1;
5114 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
5115 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5116 if (NULL == address)
5117 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
5122 * Handle AddressLookup-message.
5124 * @param cls closure (always NULL)
5125 * @param client identification of the client
5126 * @param message the actual message
5129 handle_address_lookup (void *cls,
5130 struct GNUNET_SERVER_Client *client,
5131 const struct GNUNET_MessageHeader *message)
5133 const struct AddressLookupMessage *alum;
5134 struct TransportPlugin *lsPlugin;
5135 const char *nameTransport;
5136 const char *address;
5138 struct GNUNET_SERVER_TransmitContext *tc;
5139 struct GNUNET_TIME_Absolute timeout;
5140 struct GNUNET_TIME_Relative rtimeout;
5143 size = ntohs (message->size);
5144 if (size < sizeof (struct AddressLookupMessage))
5146 GNUNET_break_op (0);
5147 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5150 alum = (const struct AddressLookupMessage *) message;
5151 uint32_t addressLen = ntohl (alum->addrlen);
5152 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
5154 GNUNET_break_op (0);
5155 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5158 address = (const char *) &alum[1];
5159 nameTransport = (const char *) &address[addressLen];
5161 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
5163 GNUNET_break_op (0);
5164 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5167 timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
5168 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5169 numeric = ntohl (alum->numeric_only);
5170 lsPlugin = find_transport (nameTransport);
5171 if (NULL == lsPlugin)
5173 tc = GNUNET_SERVER_transmit_context_create (client);
5174 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5175 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5176 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
5179 tc = GNUNET_SERVER_transmit_context_create (client);
5180 lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
5182 address, addressLen,
5185 &transmit_address_to_client, tc);
5190 * Setup the environment for this plugin.
5193 create_environment (struct TransportPlugin *plug)
5195 plug->env.cfg = cfg;
5196 plug->env.my_identity = &my_identity;
5197 plug->env.our_hello = &our_hello;
5198 plug->env.cls = plug;
5199 plug->env.receive = &plugin_env_receive;
5200 plug->env.notify_address = &plugin_env_notify_address;
5201 plug->env.session_end = &plugin_env_session_end;
5202 plug->env.max_connections = max_connect_per_transport;
5203 plug->env.stats = stats;
5208 * Start the specified transport (load the plugin).
5211 start_transport (struct GNUNET_SERVER_Handle *server,
5214 struct TransportPlugin *plug;
5217 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5218 _("Loading `%s' transport plugin\n"), name);
5219 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
5220 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
5221 create_environment (plug);
5222 plug->short_name = GNUNET_strdup (name);
5223 plug->lib_name = libname;
5224 plug->next = plugins;
5226 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
5227 if (plug->api == NULL)
5229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5230 _("Failed to load transport plugin for `%s'\n"), name);
5231 GNUNET_free (plug->short_name);
5232 plugins = plug->next;
5233 GNUNET_free (libname);
5240 * Called whenever a client is disconnected. Frees our
5241 * resources associated with that client.
5243 * @param cls closure
5244 * @param client identification of the client
5247 client_disconnect_notification (void *cls,
5248 struct GNUNET_SERVER_Client *client)
5250 struct TransportClient *pos;
5251 struct TransportClient *prev;
5252 struct ClientMessageQueueEntry *mqe;
5253 struct Blacklisters *bl;
5254 struct BlacklistCheck *bc;
5259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
5260 "Client disconnected, cleaning up.\n");
5262 /* clean up blacklister */
5266 if (bl->client == client)
5271 if (bc->bl_pos == bl)
5273 bc->bl_pos = bl->next;
5276 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
5279 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
5280 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
5286 GNUNET_CONTAINER_DLL_remove (bl_head,
5289 GNUNET_SERVER_client_drop (bl->client);
5295 /* clean up 'normal' clients */
5298 while ((pos != NULL) && (pos->client != client))
5305 while (NULL != (mqe = pos->message_queue_head))
5307 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
5308 pos->message_queue_tail,
5310 pos->message_count--;
5314 clients = pos->next;
5316 prev->next = pos->next;
5317 if (GNUNET_YES == pos->tcs_pending)
5322 if (pos->th != NULL)
5324 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
5327 GNUNET_break (0 == pos->message_count);
5333 * Function called when the service shuts down. Unloads our plugins
5334 * and cancels pending validations.
5336 * @param cls closure, unused
5337 * @param tc task context (unused)
5340 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5342 struct TransportPlugin *plug;
5343 struct OwnAddressList *al;
5344 struct CheckHelloValidatedContext *chvc;
5346 while (neighbours != NULL)
5348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5349 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&neighbours->id),
5351 disconnect_neighbour (neighbours, GNUNET_NO);
5354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5355 "Transport service is unloading plugins...\n");
5357 while (NULL != (plug = plugins))
5359 plugins = plug->next;
5360 if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
5362 GNUNET_SCHEDULER_cancel (plug->address_update_task);
5363 plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
5365 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
5366 GNUNET_free (plug->lib_name);
5367 GNUNET_free (plug->short_name);
5368 while (NULL != (al = plug->addresses))
5370 plug->addresses = al->next;
5375 if (my_private_key != NULL)
5376 GNUNET_CRYPTO_rsa_key_free (my_private_key);
5377 GNUNET_free_non_null (our_hello);
5379 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
5382 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
5383 validation_map = NULL;
5385 /* free 'chvc' data structure */
5386 while (NULL != (chvc = chvc_head))
5388 chvc_head = chvc->next;
5389 if (chvc->piter != NULL)
5391 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
5392 GNUNET_STATISTICS_update (stats,
5393 gettext_noop ("# outstanding peerinfo iterate requests"),
5399 GNUNET_assert (chvc->ve_count == 0);
5406 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
5409 if (peerinfo != NULL)
5411 GNUNET_PEERINFO_disconnect (peerinfo);
5414 /* Can we assume those are gone by now, or do we need to clean up
5416 GNUNET_break (bl_head == NULL);
5417 GNUNET_break (bc_head == NULL);
5422 * Initiate transport service.
5424 * @param cls closure
5425 * @param server the initialized server
5426 * @param c configuration to use
5430 struct GNUNET_SERVER_Handle *server,
5431 const struct GNUNET_CONFIGURATION_Handle *c)
5433 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
5434 {&handle_start, NULL,
5435 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
5436 {&handle_hello, NULL,
5437 GNUNET_MESSAGE_TYPE_HELLO, 0},
5438 {&handle_send, NULL,
5439 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
5440 {&handle_request_connect, NULL,
5441 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT, sizeof(struct TransportRequestConnectMessage)},
5442 {&handle_set_quota, NULL,
5443 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
5444 {&handle_address_lookup, NULL,
5445 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
5447 {&handle_blacklist_init, NULL,
5448 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
5449 {&handle_blacklist_reply, NULL,
5450 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
5456 unsigned long long tneigh;
5460 stats = GNUNET_STATISTICS_create ("transport", cfg);
5461 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
5462 /* parse configuration */
5464 GNUNET_CONFIGURATION_get_value_number (c,
5469 GNUNET_CONFIGURATION_get_value_filename (c,
5471 "HOSTKEY", &keyfile)))
5473 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5475 ("Transport service is lacking key configuration settings. Exiting.\n"));
5476 GNUNET_SCHEDULER_shutdown ();
5479 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
5482 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
5483 validation_map = NULL;
5486 max_connect_per_transport = (uint32_t) tneigh;
5487 peerinfo = GNUNET_PEERINFO_connect (cfg);
5488 if (peerinfo == NULL)
5490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5491 _("Could not access PEERINFO service. Exiting.\n"));
5492 GNUNET_SCHEDULER_shutdown ();
5495 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
5498 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
5499 validation_map = NULL;
5500 GNUNET_free (keyfile);
5503 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
5504 GNUNET_free (keyfile);
5505 if (my_private_key == NULL)
5507 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5509 ("Transport service could not access hostkey. Exiting.\n"));
5510 GNUNET_SCHEDULER_shutdown ();
5513 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
5516 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
5517 validation_map = NULL;
5520 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
5521 GNUNET_CRYPTO_hash (&my_public_key,
5522 sizeof (my_public_key), &my_identity.hashPubKey);
5523 /* setup notification */
5524 GNUNET_SERVER_disconnect_notify (server,
5525 &client_disconnect_notification, NULL);
5526 /* load plugins... */
5529 GNUNET_CONFIGURATION_get_value_string (c,
5530 "TRANSPORT", "PLUGINS", &plugs))
5532 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5533 _("Starting transport plugins `%s'\n"), plugs);
5534 pos = strtok (plugs, " ");
5537 start_transport (server, pos);
5539 pos = strtok (NULL, " ");
5541 GNUNET_free (plugs);
5543 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
5544 &shutdown_task, NULL);
5549 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport service ready.\n"));
5551 /* If we have a blacklist file, read from it */
5552 read_blacklist_file(cfg);
5553 /* process client requests */
5554 GNUNET_SERVER_add_handlers (server, handlers);
5559 * The main function for the transport service.
5561 * @param argc number of arguments from the command line
5562 * @param argv command line arguments
5563 * @return 0 ok, 1 on error
5566 main (int argc, char *const *argv)
5568 a2s (NULL, NULL, 0); /* make compiler happy */
5569 return (GNUNET_OK ==
5570 GNUNET_SERVICE_run (argc,
5573 GNUNET_SERVICE_OPTION_NONE,
5574 &run, NULL)) ? 0 : 1;
5577 /* end of gnunet-service-transport.c */