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 "plugin_transport.h"
40 #include "transport.h"
42 #define DEBUG_BLACKLIST GNUNET_NO
44 #define DEBUG_PING_PONG GNUNET_NO
46 #define SIGN_USELESS GNUNET_NO
48 #define DEBUG_TRANSPORT_HELLO GNUNET_YES
51 * Should we do some additional checks (to validate behavior
54 #define EXTRA_CHECKS GNUNET_YES
57 * How many messages can we have pending for a given client process
58 * before we start to drop incoming messages? We typically should
59 * have only one client and so this would be the primary buffer for
60 * messages, so the number should be chosen rather generously.
62 * The expectation here is that most of the time the queue is large
63 * enough so that a drop is virtually never required. Note that
64 * this value must be about as large as 'TOTAL_MSGS' in the
65 * 'test_transport_api_reliability.c', otherwise that testcase may
68 #define MAX_PENDING (128 * 1024)
71 * Size of the per-transport blacklist hash maps.
73 #define TRANSPORT_BLACKLIST_HT_SIZE 16
76 * How often should we try to reconnect to a peer using a particular
77 * transport plugin before giving up? Note that the plugin may be
78 * added back to the list after PLUGIN_RETRY_FREQUENCY expires.
80 #define MAX_CONNECT_RETRY 3
83 * Limit on the number of ready-to-run tasks when validating
84 * HELLOs. If more tasks are ready to run, we will drop
85 * HELLOs instead of validating them.
87 #define MAX_HELLO_LOAD 4
90 * How often must a peer violate bandwidth quotas before we start
91 * to simply drop its messages?
93 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
96 * How long until a HELLO verification attempt should time out?
97 * Must be rather small, otherwise a partially successful HELLO
98 * validation (some addresses working) might not be available
99 * before a client's request for a connection fails for good.
100 * Besides, if a single request to an address takes a long time,
101 * then the peer is unlikely worthwhile anyway.
103 #define HELLO_VERIFICATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
106 * How long is a PONG signature valid? We'll recycle a signature until
107 * 1/4 of this time is remaining. PONGs should expire so that if our
108 * external addresses change an adversary cannot replay them indefinitely.
109 * OTOH, we don't want to spend too much time generating PONG signatures,
110 * so they must have some lifetime to reduce our CPU usage.
112 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
115 * Priority to use for PONG messages.
117 #define TRANSPORT_PONG_PRIORITY 4
120 * How often do we re-add (cheaper) plugins to our list of plugins
121 * to try for a given connected peer?
123 #define PLUGIN_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
126 * After how long do we expire an address in a HELLO that we just
127 * validated? This value is also used for our own addresses when we
130 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
134 * How long before an existing address expires should we again try to
135 * validate it? Must be (significantly) smaller than
136 * HELLO_ADDRESS_EXPIRATION.
138 #define HELLO_REVALIDATION_START_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
141 * Maximum frequency for re-evaluating latencies for all transport addresses.
143 #define LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
146 * Maximum frequency for re-evaluating latencies for connected addresses.
148 #define CONNECTED_LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
152 * List of addresses of other peers
154 struct ForeignAddressList
157 * This is a linked list.
159 struct ForeignAddressList *next;
162 * Which ready list does this entry belong to.
164 struct ReadyList *ready_list;
167 * How long until we auto-expire this address (unless it is
168 * re-confirmed by the transport)?
170 struct GNUNET_TIME_Absolute expires;
173 * Task used to re-validate addresses, updates latencies and
176 GNUNET_SCHEDULER_TaskIdentifier revalidate_task;
184 * Session (or NULL if no valid session currently exists or if the
185 * plugin does not use sessions).
187 struct Session *session;
190 * What was the last latency observed for this address, plugin and peer?
192 struct GNUNET_TIME_Relative latency;
195 * If we did not successfully transmit a message to the given peer
196 * via this connection during the specified time, we should consider
197 * the connection to be dead. This is used in the case that a TCP
198 * transport simply stalls writing to the stream but does not
199 * formerly get a signal that the other peer died.
201 struct GNUNET_TIME_Absolute timeout;
204 * How often have we tried to connect using this plugin? Used to
205 * discriminate against addresses that do not work well.
206 * FIXME: not yet used, but should be!
208 unsigned int connect_attempts;
211 * DV distance to this peer (1 if no DV is used).
212 * FIXME: need to set this from transport plugins!
222 * Have we ever estimated the latency of this address? Used to
223 * ensure that the first time we add an address, we immediately
229 * Are we currently connected via this address? The first time we
230 * successfully transmit or receive data to a peer via a particular
231 * address, we set this to GNUNET_YES. If we later get an error
232 * (disconnect notification, transmission failure, timeout), we set
233 * it back to GNUNET_NO.
238 * Is this plugin currently busy transmitting to the specific target?
239 * GNUNET_NO if not (initial, default state is GNUNET_NO). Internal
240 * messages do not count as 'in transmit'.
245 * Has this address been validated yet?
253 * Entry in linked list of network addresses for ourselves. Also
254 * includes a cached signature for 'struct TransportPongMessage's.
256 struct OwnAddressList
259 * This is a linked list.
261 struct OwnAddressList *next;
264 * How long until we actually auto-expire this address (unless it is
265 * re-confirmed by the transport)?
267 struct GNUNET_TIME_Absolute expires;
270 * How long until the current signature expires? (ZERO if the
271 * signature was never created).
273 struct GNUNET_TIME_Absolute pong_sig_expires;
276 * Signature for a 'struct TransportPongMessage' for this address.
278 struct GNUNET_CRYPTO_RsaSignature pong_signature;
289 * Entry in linked list of all of our plugins.
291 struct TransportPlugin
295 * This is a linked list.
297 struct TransportPlugin *next;
300 * API of the transport as returned by the plugin's
301 * initialization function.
303 struct GNUNET_TRANSPORT_PluginFunctions *api;
306 * Short name for the plugin (i.e. "tcp").
311 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
316 * List of our known addresses for this transport.
318 struct OwnAddressList *addresses;
321 * Environment this transport service is using
324 struct GNUNET_TRANSPORT_PluginEnvironment env;
327 * ID of task that is used to clean up expired addresses.
329 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
332 * Set to GNUNET_YES if we need to scrap the existing list of
333 * "addresses" and start fresh when we receive the next address
334 * update from a transport. Set to GNUNET_NO if we should just add
335 * the new address to the list and wait for the commit call.
340 * Hashmap of blacklisted peers for this particular transport.
342 struct GNUNET_CONTAINER_MultiHashMap *blacklist;
345 struct NeighbourList;
348 * For each neighbour we keep a list of messages
349 * that we still want to transmit to the neighbour.
355 * This is a doubly linked list.
357 struct MessageQueue *next;
360 * This is a doubly linked list.
362 struct MessageQueue *prev;
365 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
366 * stuck together in memory. Allocated at the end of this struct.
368 const char *message_buf;
371 * Size of the message buf
373 size_t message_buf_size;
376 * Client responsible for queueing the message;
377 * used to check that a client has no two messages
378 * pending for the same target. Can be NULL.
380 struct TransportClient *client;
383 * Using which specific address should we send this message?
385 struct ForeignAddressList *specific_address;
388 * Peer ID of the Neighbour this entry belongs to.
390 struct GNUNET_PeerIdentity neighbour_id;
393 * Plugin that we used for the transmission.
394 * NULL until we scheduled a transmission.
396 struct TransportPlugin *plugin;
399 * At what time should we fail?
401 struct GNUNET_TIME_Absolute timeout;
404 * Internal message of the transport system that should not be
405 * included in the usual SEND-SEND_OK transmission confirmation
406 * traffic management scheme. Typically, "internal_msg" will
407 * be set whenever "client" is NULL (but it is not strictly
413 * How important is the message?
415 unsigned int priority;
421 * For a given Neighbour, which plugins are available
422 * to talk to this peer and what are their costs?
427 * This is a linked list.
429 struct ReadyList *next;
432 * Which of our transport plugins does this entry
435 struct TransportPlugin *plugin;
438 * Transport addresses, latency, and readiness for
439 * this particular plugin.
441 struct ForeignAddressList *addresses;
444 * To which neighbour does this ready list belong to?
446 struct NeighbourList *neighbour;
452 * Entry in linked list of all of our current neighbours.
458 * This is a linked list.
460 struct NeighbourList *next;
463 * Which of our transports is connected to this peer
464 * and what is their status?
466 struct ReadyList *plugins;
469 * Head of list of messages we would like to send to this peer;
470 * must contain at most one message per client.
472 struct MessageQueue *messages_head;
475 * Tail of list of messages we would like to send to this peer; must
476 * contain at most one message per client.
478 struct MessageQueue *messages_tail;
481 * Buffer for at most one payload message used when we receive
482 * payload data before our PING-PONG has succeeded. We then
483 * store such messages in this intermediary buffer until the
484 * connection is fully up.
486 struct GNUNET_MessageHeader *pre_connect_message_buffer;
489 * Context for peerinfo iteration.
490 * NULL after we are done processing peerinfo's information.
492 struct GNUNET_PEERINFO_IteratorContext *piter;
495 * Public key for this peer. Valid only if the respective flag is set below.
497 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
500 * Identity of this neighbour.
502 struct GNUNET_PeerIdentity id;
505 * ID of task scheduled to run when this peer is about to
506 * time out (will free resources associated with the peer).
508 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
511 * ID of task scheduled to run when we should retry transmitting
512 * the head of the message queue. Actually triggered when the
513 * transmission is timing out (we trigger instantly when we have
514 * a chance of success).
516 GNUNET_SCHEDULER_TaskIdentifier retry_task;
519 * How long until we should consider this peer dead
520 * (if we don't receive another message in the
523 struct GNUNET_TIME_Absolute peer_timeout;
526 * Tracker for inbound bandwidth.
528 struct GNUNET_BANDWIDTH_Tracker in_tracker;
531 * The latency we have seen for this particular address for
532 * this particular peer. This latency may have been calculated
533 * over multiple transports. This value reflects how long it took
534 * us to receive a response when SENDING via this particular
535 * transport/neighbour/address combination!
537 * FIXME: we need to periodically send PINGs to update this
538 * latency (at least more often than the current "huge" (11h?)
541 struct GNUNET_TIME_Relative latency;
544 * How often has the other peer (recently) violated the
545 * inbound traffic limit? Incremented by 10 per violation,
546 * decremented by 1 per non-violation (for each
549 unsigned int quota_violation_count;
552 * DV distance to this peer (1 if no DV is used).
557 * Have we seen an PONG from this neighbour in the past (and
558 * not had a disconnect since)?
563 * Do we have a valid public key for this neighbour?
565 int public_key_valid;
570 * Message used to ask a peer to validate receipt (to check an address
571 * from a HELLO). Followed by the address we are trying to validate,
572 * or an empty address if we are just sending a PING to confirm that a
573 * connection which the receiver (of the PING) initiated is still valid.
575 struct TransportPingMessage
579 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
581 struct GNUNET_MessageHeader header;
584 * Challenge code (to ensure fresh reply).
586 uint32_t challenge GNUNET_PACKED;
589 * Who is the intended recipient?
591 struct GNUNET_PeerIdentity target;
597 * Message used to validate a HELLO. The challenge is included in the
598 * confirmation to make matching of replies to requests possible. The
599 * signature signs our public key, an expiration time and our address.<p>
601 * This message is followed by our transport address that the PING tried
602 * to confirm (if we liked it). The address can be empty (zero bytes)
603 * if the PING had not address either (and we received the request via
604 * a connection that we initiated).
606 struct TransportPongMessage
610 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
612 struct GNUNET_MessageHeader header;
615 * Challenge code from PING (showing freshness). Not part of what
616 * is signed so that we can re-use signatures.
618 uint32_t challenge GNUNET_PACKED;
623 struct GNUNET_CRYPTO_RsaSignature signature;
626 * What are we signing and why? Two possible reason codes can be here:
627 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
628 * plausible address for this peer (pid is set to identity of signer); or
629 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING to confirm that this is
630 * an address we used to connect to the peer with the given pid.
632 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
635 * When does this signature expire?
637 struct GNUNET_TIME_AbsoluteNBO expiration;
640 * Either the identity of the peer Who signed this message, or the
641 * identity of the peer that we're connected to using the given
642 * address (depending on purpose.type).
644 struct GNUNET_PeerIdentity pid;
647 * Size of address appended to this message (part of what is
648 * being signed, hence not redundant).
656 * Linked list of messages to be transmitted to the client. Each
657 * entry is followed by the actual message.
659 struct ClientMessageQueueEntry
662 * This is a doubly-linked list.
664 struct ClientMessageQueueEntry *next;
667 * This is a doubly-linked list.
669 struct ClientMessageQueueEntry *prev;
674 * Client connected to the transport service.
676 struct TransportClient
680 * This is a linked list.
682 struct TransportClient *next;
685 * Handle to the client.
687 struct GNUNET_SERVER_Client *client;
690 * Linked list of messages yet to be transmitted to
693 struct ClientMessageQueueEntry *message_queue_head;
696 * Tail of linked list of messages yet to be transmitted to the
699 struct ClientMessageQueueEntry *message_queue_tail;
702 * Current transmit request handle.
704 struct GNUNET_CONNECTION_TransmitHandle *th;
707 * Is a call to "transmit_send_continuation" pending? If so, we
708 * must not free this struct (even if the corresponding client
709 * disconnects) and instead only remove it from the linked list and
710 * set the "client" field to NULL.
715 * Length of the list of messages pending for this client.
717 unsigned int message_count;
723 * Context of currently active requests to peerinfo
724 * for validation of HELLOs.
726 struct CheckHelloValidatedContext;
730 * Entry in map of all HELLOs awaiting validation.
732 struct ValidationEntry
736 * NULL if this entry is not part of a larger HELLO validation.
738 struct CheckHelloValidatedContext *chvc;
741 * The address, actually a pointer to the end
742 * of this struct. Do not free!
747 * Name of the transport.
749 char *transport_name;
752 * The public key of the peer.
754 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
757 * ID of task that will clean up this entry if we don't succeed
758 * with the validation first.
760 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
763 * At what time did we send this validation?
765 struct GNUNET_TIME_Absolute send_time;
768 * Session being validated (or NULL for none).
770 struct Session *session;
773 * Challenge number we used.
786 * Context of currently active requests to peerinfo
787 * for validation of HELLOs.
789 struct CheckHelloValidatedContext
793 * This is a doubly-linked list.
795 struct CheckHelloValidatedContext *next;
798 * This is a doubly-linked list.
800 struct CheckHelloValidatedContext *prev;
803 * Hello that we are validating.
805 const struct GNUNET_HELLO_Message *hello;
808 * Context for peerinfo iteration.
809 * NULL after we are done processing peerinfo's information.
811 struct GNUNET_PEERINFO_IteratorContext *piter;
814 * Was a HELLO known for this peer to peerinfo?
819 * Number of validation entries currently referring to this
822 unsigned int ve_count;
830 static struct GNUNET_HELLO_Message *our_hello;
835 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
840 static struct GNUNET_PeerIdentity my_identity;
845 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
850 struct GNUNET_SCHEDULER_Handle *sched;
855 const struct GNUNET_CONFIGURATION_Handle *cfg;
858 * Linked list of all clients to this service.
860 static struct TransportClient *clients;
863 * All loaded plugins.
865 static struct TransportPlugin *plugins;
868 * Handle to peerinfo service.
870 static struct GNUNET_PEERINFO_Handle *peerinfo;
873 * All known neighbours and their HELLOs.
875 static struct NeighbourList *neighbours;
878 * Number of neighbours we'd like to have.
880 static uint32_t max_connect_per_transport;
883 * Head of linked list.
885 static struct CheckHelloValidatedContext *chvc_head;
888 * Tail of linked list.
890 static struct CheckHelloValidatedContext *chvc_tail;
893 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
894 * of the given peer that we are currently validating).
896 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
899 * Handle for reporting statistics.
901 static struct GNUNET_STATISTICS_Handle *stats;
904 * The peer specified by the given neighbour has timed-out or a plugin
905 * has disconnected. We may either need to do nothing (other plugins
906 * still up), or trigger a full disconnect and clean up. This
907 * function updates our state and do the necessary notifications.
908 * Also notifies our clients that the neighbour is now officially
911 * @param n the neighbour list entry for the peer
912 * @param check should we just check if all plugins
913 * disconnected or must we ask all plugins to
916 static void disconnect_neighbour (struct NeighbourList *n, int check);
919 * Check the ready list for the given neighbour and if a plugin is
920 * ready for transmission (and if we have a message), do so!
922 * @param neighbour target peer for which to transmit
924 static void try_transmission_to_peer (struct NeighbourList *neighbour);
928 * Find an entry in the neighbour list for a particular peer.
930 * @return NULL if not found.
932 static struct NeighbourList *
933 find_neighbour (const struct GNUNET_PeerIdentity *key)
935 struct NeighbourList *head = neighbours;
937 while ((head != NULL) &&
938 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
945 * Find an entry in the transport list for a particular transport.
947 * @return NULL if not found.
949 static struct TransportPlugin *
950 find_transport (const char *short_name)
952 struct TransportPlugin *head = plugins;
953 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
959 * Is a particular peer blacklisted for a particular transport?
961 * @param peer the peer to check for
962 * @param plugin the plugin used to connect to the peer
964 * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
967 is_blacklisted (const struct GNUNET_PeerIdentity *peer, struct TransportPlugin *plugin)
970 if (plugin->blacklist != NULL)
972 if (GNUNET_CONTAINER_multihashmap_contains(plugin->blacklist, &peer->hashPubKey) == GNUNET_YES)
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
976 _("Peer `%s:%s' is blacklisted!\n"),
977 plugin->short_name, GNUNET_i2s (peer));
988 add_peer_to_blacklist (struct GNUNET_PeerIdentity *peer, char *transport_name)
990 struct TransportPlugin *plugin;
992 plugin = find_transport(transport_name);
993 if (plugin == NULL) /* Nothing to do */
995 if (plugin->blacklist == NULL)
996 plugin->blacklist = GNUNET_CONTAINER_multihashmap_create(TRANSPORT_BLACKLIST_HT_SIZE);
997 GNUNET_assert(plugin->blacklist != NULL);
998 GNUNET_CONTAINER_multihashmap_put(plugin->blacklist, &peer->hashPubKey,
1000 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1005 * Read the blacklist file, containing transport:peer entries.
1006 * Provided the transport is loaded, set up hashmap with these
1007 * entries to blacklist peers by transport.
1011 read_blacklist_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1018 struct GNUNET_PeerIdentity pid;
1020 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1021 unsigned int entries_found;
1022 char *transport_name;
1025 GNUNET_CONFIGURATION_get_value_filename (cfg,
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 _("Option `%s' in section `%s' not specified!\n"),
1038 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1039 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1040 | GNUNET_DISK_PERM_USER_WRITE);
1041 if (0 != STAT (fn, &frstat))
1043 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1044 _("Could not read blacklist file `%s'\n"), fn);
1048 if (frstat.st_size == 0)
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 _("Blacklist file `%s' is empty.\n"),
1058 /* FIXME: use mmap */
1059 data = GNUNET_malloc_large (frstat.st_size);
1060 GNUNET_assert(data != NULL);
1061 if (frstat.st_size !=
1062 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1064 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1065 _("Failed to read blacklist from `%s'\n"), fn);
1072 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1074 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1075 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1078 while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') && !isspace ( (unsigned char) data[colon_pos]))
1081 if (colon_pos >= frstat.st_size)
1083 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1084 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1085 (unsigned long long) colon_pos);
1091 if (isspace( (unsigned char) data[colon_pos]))
1093 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1094 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1095 (unsigned long long) colon_pos);
1097 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1101 tsize = colon_pos - pos;
1102 if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size) || (tsize == 0))
1104 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1105 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1106 (unsigned long long) colon_pos);
1115 transport_name = GNUNET_malloc(tsize + 1);
1116 memcpy(transport_name, &data[pos], tsize);
1117 pos = colon_pos + 1;
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1120 _("Read transport name %s in blacklist file.\n"),
1123 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1124 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1126 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1127 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1128 (unsigned long long) pos);
1130 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1132 GNUNET_free_non_null(transport_name);
1135 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1136 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1138 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1139 _("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
1140 (unsigned long long) pos,
1145 if (0 != memcmp (&pid,
1147 sizeof (struct GNUNET_PeerIdentity)))
1150 add_peer_to_blacklist (&pid,
1155 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1156 _("Found myself `%s' in blacklist (useless, ignored)\n"),
1160 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1161 GNUNET_free_non_null(transport_name);
1162 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1171 * Function called to notify a client about the socket being ready to
1172 * queue more data. "buf" will be NULL and "size" zero if the socket
1173 * was closed for writing in the meantime.
1175 * @param cls closure
1176 * @param size number of bytes available in buf
1177 * @param buf where the callee should write the message
1178 * @return number of bytes written to buf
1181 transmit_to_client_callback (void *cls, size_t size, void *buf)
1183 struct TransportClient *client = cls;
1184 struct ClientMessageQueueEntry *q;
1187 const struct GNUNET_MessageHeader *msg;
1193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1194 "Transmission to client failed, closing connection.\n");
1195 /* fatal error with client, free message queue! */
1196 while (NULL != (q = client->message_queue_head))
1198 GNUNET_STATISTICS_update (stats,
1199 gettext_noop ("# bytes discarded (could not transmit to client)"),
1200 ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
1202 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1203 client->message_queue_tail,
1207 client->message_count = 0;
1212 while (NULL != (q = client->message_queue_head))
1214 msg = (const struct GNUNET_MessageHeader *) &q[1];
1215 msize = ntohs (msg->size);
1216 if (msize + tsize > size)
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1220 "Transmitting message of type %u to client.\n",
1223 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1224 client->message_queue_tail,
1226 memcpy (&cbuf[tsize], msg, msize);
1229 client->message_count--;
1233 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1234 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1236 GNUNET_TIME_UNIT_FOREVER_REL,
1237 &transmit_to_client_callback,
1239 GNUNET_assert (client->th != NULL);
1246 * Convert an address to a string.
1248 * @param plugin name of the plugin responsible for the address
1249 * @param addr binary address
1250 * @param addr_len number of bytes in addr
1251 * @return NULL on error, otherwise address string
1254 a2s (const char *plugin,
1258 struct TransportPlugin *p;
1262 p = find_transport (plugin);
1265 return p->api->address_to_string (p->api->cls,
1272 * Mark the given FAL entry as 'connected' (and hence preferred for
1273 * sending); also mark all others for the same peer as 'not connected'
1274 * (since only one can be preferred).
1276 * @param fal address to set to 'connected'
1279 mark_address_connected (struct ForeignAddressList *fal)
1281 struct ForeignAddressList *pos;
1284 GNUNET_assert (GNUNET_YES == fal->validated);
1285 if (fal->connected == GNUNET_YES)
1286 return; /* nothing to do */
1288 pos = fal->ready_list->addresses;
1291 if (GNUNET_YES == pos->connected)
1294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1295 "Marking address `%s' as no longer connected (due to connect on other address)\n",
1296 a2s (pos->ready_list->plugin->short_name,
1300 GNUNET_break (cnt == GNUNET_YES);
1302 pos->connected = GNUNET_NO;
1303 GNUNET_STATISTICS_update (stats,
1304 gettext_noop ("# connected addresses"),
1310 fal->connected = GNUNET_YES;
1311 if (GNUNET_YES == cnt)
1313 GNUNET_STATISTICS_update (stats,
1314 gettext_noop ("# connected addresses"),
1322 * Send the specified message to the specified client. Since multiple
1323 * messages may be pending for the same client at a time, this code
1324 * makes sure that no message is lost.
1326 * @param client client to transmit the message to
1327 * @param msg the message to send
1328 * @param may_drop can this message be dropped if the
1329 * message queue for this client is getting far too large?
1332 transmit_to_client (struct TransportClient *client,
1333 const struct GNUNET_MessageHeader *msg, int may_drop)
1335 struct ClientMessageQueueEntry *q;
1338 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1340 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1342 ("Dropping message of type %u and size %u, have %u messages pending (%u is the soft limit)\n"),
1345 client->message_count,
1347 GNUNET_STATISTICS_update (stats,
1348 gettext_noop ("# messages dropped due to slow client"),
1353 msize = ntohs (msg->size);
1354 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1355 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1356 memcpy (&q[1], msg, msize);
1357 GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1358 client->message_queue_tail,
1359 client->message_queue_tail,
1361 client->message_count++;
1362 if (client->th == NULL)
1364 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1366 GNUNET_TIME_UNIT_FOREVER_REL,
1367 &transmit_to_client_callback,
1369 GNUNET_assert (client->th != NULL);
1375 * Transmit a 'SEND_OK' notification to the given client for the
1378 * @param client who to notify
1379 * @param n neighbour to notify about
1380 * @param result status code for the transmission request
1383 transmit_send_ok (struct TransportClient *client,
1384 struct NeighbourList *n,
1387 struct SendOkMessage send_ok_msg;
1389 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1390 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1391 send_ok_msg.success = htonl (result);
1392 send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1393 send_ok_msg.peer = n->id;
1394 transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1399 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1400 * upon "completion" of a send request. This tells the API
1401 * that it is now legal to send another message to the given
1404 * @param cls closure, identifies the entry on the
1405 * message queue that was transmitted and the
1406 * client responsible for queueing the message
1407 * @param target the peer receiving the message
1408 * @param result GNUNET_OK on success, if the transmission
1409 * failed, we should not tell the client to transmit
1413 transmit_send_continuation (void *cls,
1414 const struct GNUNET_PeerIdentity *target,
1417 struct MessageQueue *mq = cls;
1418 struct NeighbourList *n;
1420 GNUNET_STATISTICS_update (stats,
1421 gettext_noop ("# bytes pending with plugins"),
1422 - (int64_t) mq->message_buf_size,
1424 if (result == GNUNET_OK)
1426 GNUNET_STATISTICS_update (stats,
1427 gettext_noop ("# bytes successfully transmitted by plugins"),
1428 mq->message_buf_size,
1433 GNUNET_STATISTICS_update (stats,
1434 gettext_noop ("# bytes with transmission failure by plugins"),
1435 mq->message_buf_size,
1438 n = find_neighbour(&mq->neighbour_id);
1439 GNUNET_assert (n != NULL);
1440 if (mq->specific_address != NULL)
1442 if (result == GNUNET_OK)
1444 mq->specific_address->timeout =
1445 GNUNET_TIME_relative_to_absolute
1446 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1447 if (mq->specific_address->validated == GNUNET_YES)
1448 mark_address_connected (mq->specific_address);
1452 if (mq->specific_address->connected != GNUNET_NO)
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 "Marking address `%s' as no longer connected (due to transmission problem)\n",
1457 a2s (mq->specific_address->ready_list->plugin->short_name,
1458 mq->specific_address->addr,
1459 mq->specific_address->addrlen));
1461 GNUNET_STATISTICS_update (stats,
1462 gettext_noop ("# connected addresses"),
1465 mq->specific_address->connected = GNUNET_NO;
1468 if (! mq->internal_msg)
1469 mq->specific_address->in_transmit = GNUNET_NO;
1471 if (mq->client != NULL)
1472 transmit_send_ok (mq->client, n, result);
1474 try_transmission_to_peer (n);
1479 * Find an address in any of the available transports for
1480 * the given neighbour that would be good for message
1481 * transmission. This is essentially the transport selection
1484 * @param neighbour for whom to select an address
1485 * @return selected address, NULL if we have none
1487 struct ForeignAddressList *
1488 find_ready_address(struct NeighbourList *neighbour)
1490 struct ReadyList *head = neighbour->plugins;
1491 struct ForeignAddressList *addresses;
1492 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1493 struct ForeignAddressList *best_address;
1495 best_address = NULL;
1496 while (head != NULL)
1498 addresses = head->addresses;
1499 while (addresses != NULL)
1501 if ( (addresses->timeout.value < now.value) &&
1502 (addresses->connected == GNUNET_YES) )
1505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1506 "Marking long-time inactive connection to `%4s' as down.\n",
1507 GNUNET_i2s (&neighbour->id));
1509 GNUNET_STATISTICS_update (stats,
1510 gettext_noop ("# connected addresses"),
1513 addresses->connected = GNUNET_NO;
1515 addresses = addresses->next;
1518 addresses = head->addresses;
1519 while (addresses != NULL)
1521 #if DEBUG_TRANSPORT > 1
1522 if (addresses->addr != NULL)
1523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1524 "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
1525 a2s (head->plugin->short_name,
1527 addresses->addrlen),
1528 GNUNET_i2s (&neighbour->id),
1529 addresses->connected,
1530 addresses->in_transmit,
1531 addresses->validated,
1532 addresses->connect_attempts,
1533 (unsigned long long) addresses->timeout.value,
1534 (unsigned int) addresses->distance);
1536 if ( ( (best_address == NULL) ||
1537 (addresses->connected == GNUNET_YES) ||
1538 (best_address->connected == GNUNET_NO) ) &&
1539 (addresses->in_transmit == GNUNET_NO) &&
1540 ( (best_address == NULL) ||
1541 (addresses->latency.value < best_address->latency.value)) )
1542 best_address = addresses;
1543 /* FIXME: also give lower-latency addresses that are not
1544 connected a chance some times... */
1545 addresses = addresses->next;
1549 if (best_address != NULL)
1552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1553 "Best address found (`%s') has latency of %llu ms.\n",
1554 (best_address->addrlen > 0)
1555 ? a2s (best_address->ready_list->plugin->short_name,
1557 best_address->addrlen)
1559 best_address->latency.value);
1564 GNUNET_STATISTICS_update (stats,
1565 gettext_noop ("# transmission attempts failed (no address)"),
1569 return best_address;
1575 * We should re-try transmitting to the given peer,
1576 * hopefully we've learned something in the meantime.
1579 retry_transmission_task (void *cls,
1580 const struct GNUNET_SCHEDULER_TaskContext *tc)
1582 struct NeighbourList *n = cls;
1584 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1585 try_transmission_to_peer (n);
1590 * Check the ready list for the given neighbour and if a plugin is
1591 * ready for transmission (and if we have a message), do so!
1593 * @param neighbour target peer for which to transmit
1596 try_transmission_to_peer (struct NeighbourList *neighbour)
1598 struct ReadyList *rl;
1599 struct MessageQueue *mq;
1600 struct GNUNET_TIME_Relative timeout;
1604 if (neighbour->messages_head == NULL)
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1608 "Transmission queue for `%4s' is empty\n",
1609 GNUNET_i2s (&neighbour->id));
1611 return; /* nothing to do */
1614 mq = neighbour->messages_head;
1615 force_address = GNUNET_YES;
1616 if (mq->specific_address == NULL)
1618 mq->specific_address = find_ready_address(neighbour);
1619 GNUNET_STATISTICS_update (stats,
1620 gettext_noop ("# transport selected peer address freely"),
1623 force_address = GNUNET_NO;
1625 if (mq->specific_address == NULL)
1627 GNUNET_STATISTICS_update (stats,
1628 gettext_noop ("# transport failed to selected peer address"),
1631 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1632 if (timeout.value == 0)
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1636 "No destination address available to transmit message of size %u to peer `%4s'\n",
1637 mq->message_buf_size,
1638 GNUNET_i2s (&mq->neighbour_id));
1640 GNUNET_STATISTICS_update (stats,
1641 gettext_noop ("# bytes in message queue for other peers"),
1642 - (int64_t) mq->message_buf_size,
1644 GNUNET_STATISTICS_update (stats,
1645 gettext_noop ("# bytes discarded (no destination address available)"),
1646 mq->message_buf_size,
1648 if (mq->client != NULL)
1649 transmit_send_ok (mq->client, neighbour, GNUNET_NO);
1650 GNUNET_CONTAINER_DLL_remove (neighbour->messages_head,
1651 neighbour->messages_tail,
1654 return; /* nobody ready */
1656 GNUNET_STATISTICS_update (stats,
1657 gettext_noop ("# message delivery deferred (no address)"),
1660 if (neighbour->retry_task != GNUNET_SCHEDULER_NO_TASK)
1661 GNUNET_SCHEDULER_cancel (sched,
1662 neighbour->retry_task);
1663 neighbour->retry_task = GNUNET_SCHEDULER_add_delayed (sched,
1665 &retry_transmission_task,
1668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1669 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
1670 mq->message_buf_size,
1671 GNUNET_i2s (&mq->neighbour_id),
1674 /* FIXME: might want to trigger peerinfo lookup here
1675 (unless that's already pending...) */
1678 GNUNET_CONTAINER_DLL_remove (neighbour->messages_head,
1679 neighbour->messages_tail,
1681 if (mq->specific_address->connected == GNUNET_NO)
1682 mq->specific_address->connect_attempts++;
1683 rl = mq->specific_address->ready_list;
1684 mq->plugin = rl->plugin;
1685 if (!mq->internal_msg)
1686 mq->specific_address->in_transmit = GNUNET_YES;
1688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1689 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
1690 mq->message_buf_size,
1691 GNUNET_i2s (&neighbour->id),
1692 (mq->specific_address->addr != NULL)
1693 ? a2s (mq->plugin->short_name,
1694 mq->specific_address->addr,
1695 mq->specific_address->addrlen)
1697 rl->plugin->short_name);
1699 GNUNET_STATISTICS_update (stats,
1700 gettext_noop ("# bytes in message queue for other peers"),
1701 - (int64_t) mq->message_buf_size,
1703 GNUNET_STATISTICS_update (stats,
1704 gettext_noop ("# bytes pending with plugins"),
1705 mq->message_buf_size,
1707 ret = rl->plugin->api->send (rl->plugin->api->cls,
1710 mq->message_buf_size,
1712 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1713 mq->specific_address->session,
1714 mq->specific_address->addr,
1715 mq->specific_address->addrlen,
1717 &transmit_send_continuation, mq);
1720 /* failure, but 'send' would not call continuation in this case,
1721 so we need to do it here! */
1722 transmit_send_continuation (mq,
1730 * Send the specified message to the specified peer.
1732 * @param client source of the transmission request (can be NULL)
1733 * @param peer_address ForeignAddressList where we should send this message
1734 * @param priority how important is the message
1735 * @param timeout how long do we have to transmit?
1736 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
1737 * @param message_buf_size total size of all messages in message_buf
1738 * @param is_internal is this an internal message; these are pre-pended and
1739 * also do not count for plugins being "ready" to transmit
1740 * @param neighbour handle to the neighbour for transmission
1743 transmit_to_peer (struct TransportClient *client,
1744 struct ForeignAddressList *peer_address,
1745 unsigned int priority,
1746 struct GNUNET_TIME_Relative timeout,
1747 const char *message_buf,
1748 size_t message_buf_size,
1749 int is_internal, struct NeighbourList *neighbour)
1751 struct MessageQueue *mq;
1756 /* check for duplicate submission */
1757 mq = neighbour->messages_head;
1760 if (mq->client == client)
1762 /* client transmitted to same peer twice
1763 before getting SEND_OK! */
1771 GNUNET_STATISTICS_update (stats,
1772 gettext_noop ("# bytes in message queue for other peers"),
1775 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
1776 mq->specific_address = peer_address;
1777 mq->client = client;
1778 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1779 memcpy (&mq[1], message_buf, message_buf_size);
1780 mq->message_buf = (const char*) &mq[1];
1781 mq->message_buf_size = message_buf_size;
1782 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
1783 mq->internal_msg = is_internal;
1784 mq->priority = priority;
1785 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1787 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
1788 neighbour->messages_tail,
1791 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
1792 neighbour->messages_tail,
1793 neighbour->messages_tail,
1795 try_transmission_to_peer (neighbour);
1802 struct GeneratorContext
1804 struct TransportPlugin *plug_pos;
1805 struct OwnAddressList *addr_pos;
1806 struct GNUNET_TIME_Absolute expiration;
1814 address_generator (void *cls, size_t max, void *buf)
1816 struct GeneratorContext *gc = cls;
1819 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
1821 gc->plug_pos = gc->plug_pos->next;
1822 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
1824 if (NULL == gc->plug_pos)
1829 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
1832 gc->addr_pos->addrlen, buf, max);
1833 gc->addr_pos = gc->addr_pos->next;
1839 * Construct our HELLO message from all of the addresses of
1840 * all of the transports.
1845 struct GNUNET_HELLO_Message *hello;
1846 struct TransportClient *cpos;
1847 struct NeighbourList *npos;
1848 struct GeneratorContext gc;
1850 gc.plug_pos = plugins;
1851 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
1852 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1853 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
1855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1856 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
1858 GNUNET_STATISTICS_update (stats,
1859 gettext_noop ("# refreshed my HELLO"),
1863 while (cpos != NULL)
1865 transmit_to_client (cpos,
1866 (const struct GNUNET_MessageHeader *) hello,
1871 GNUNET_free_non_null (our_hello);
1873 GNUNET_PEERINFO_add_peer (peerinfo, our_hello);
1875 while (npos != NULL)
1878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1879 "Transmitting updated `%s' to neighbour `%4s'\n",
1880 "HELLO", GNUNET_i2s (&npos->id));
1882 GNUNET_STATISTICS_update (stats,
1883 gettext_noop ("# transmitted my HELLO to other peers"),
1886 transmit_to_peer (NULL, NULL, 0,
1887 HELLO_ADDRESS_EXPIRATION,
1888 (const char *) our_hello,
1889 GNUNET_HELLO_size(our_hello),
1897 * Task used to clean up expired addresses for a plugin.
1899 * @param cls closure
1903 expire_address_task (void *cls,
1904 const struct GNUNET_SCHEDULER_TaskContext *tc);
1908 * Update the list of addresses for this plugin,
1909 * expiring those that are past their expiration date.
1911 * @param plugin addresses of which plugin should be recomputed?
1912 * @param fresh set to GNUNET_YES if a new address was added
1913 * and we need to regenerate the HELLO even if nobody
1917 update_addresses (struct TransportPlugin *plugin, int fresh)
1919 static struct GNUNET_TIME_Absolute last_update;
1920 struct GNUNET_TIME_Relative min_remaining;
1921 struct GNUNET_TIME_Relative remaining;
1922 struct GNUNET_TIME_Absolute now;
1923 struct OwnAddressList *pos;
1924 struct OwnAddressList *prev;
1925 struct OwnAddressList *next;
1928 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_TASK)
1929 GNUNET_SCHEDULER_cancel (plugin->env.sched, plugin->address_update_task);
1930 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1931 now = GNUNET_TIME_absolute_get ();
1932 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
1933 expired = (GNUNET_TIME_absolute_get_duration (last_update).value > (HELLO_ADDRESS_EXPIRATION.value / 4));
1935 pos = plugin->addresses;
1939 if (pos->expires.value < now.value)
1941 expired = GNUNET_YES;
1943 plugin->addresses = pos->next;
1945 prev->next = pos->next;
1950 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
1951 if (remaining.value < min_remaining.value)
1952 min_remaining = remaining;
1958 if (expired || fresh)
1963 min_remaining = GNUNET_TIME_relative_min (min_remaining,
1964 GNUNET_TIME_relative_divide (HELLO_ADDRESS_EXPIRATION,
1966 plugin->address_update_task
1967 = GNUNET_SCHEDULER_add_delayed (plugin->env.sched,
1969 &expire_address_task, plugin);
1974 * Task used to clean up expired addresses for a plugin.
1976 * @param cls closure
1980 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1982 struct TransportPlugin *plugin = cls;
1984 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1985 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1986 update_addresses (plugin, GNUNET_NO);
1991 * Iterator over hash map entries that NULLs the session of validation
1992 * entries that match the given session.
1994 * @param cls closure (the 'struct Session*' to match against)
1995 * @param key current key code (peer ID, not used)
1996 * @param value value in the hash map ('struct ValidationEntry*')
1997 * @return GNUNET_YES (we should continue to iterate)
2000 remove_session_validations (void *cls,
2001 const GNUNET_HashCode * key,
2004 struct Session *session = cls;
2005 struct ValidationEntry *ve = value;
2007 if (session == ve->session)
2014 * Function that will be called whenever the plugin internally
2015 * cleans up a session pointer and hence the service needs to
2016 * discard all of those sessions as well. Plugins that do not
2017 * use sessions can simply omit calling this function and always
2018 * use NULL wherever a session pointer is needed.
2020 * @param cls closure
2021 * @param peer which peer was the session for
2022 * @param session which session is being destoyed
2025 plugin_env_session_end (void *cls,
2026 const struct GNUNET_PeerIdentity *peer,
2027 struct Session *session)
2029 struct TransportPlugin *p = cls;
2030 struct NeighbourList *nl;
2031 struct ReadyList *rl;
2032 struct ForeignAddressList *pos;
2033 struct ForeignAddressList *prev;
2035 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2036 &remove_session_validations,
2038 nl = find_neighbour (peer);
2044 if (rl->plugin == p)
2051 pos = rl->addresses;
2052 while ( (pos != NULL) &&
2053 (pos->session != session) )
2060 pos->session = NULL;
2061 if (pos->addrlen != 0)
2064 rl->addresses = pos->next;
2066 prev->next = pos->next;
2067 if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2069 GNUNET_SCHEDULER_cancel (sched,
2070 pos->revalidate_task);
2071 pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2074 if (nl->received_pong == GNUNET_NO)
2075 return; /* nothing to do */
2076 /* check if we have any validated addresses left */
2077 pos = rl->addresses;
2084 /* no valid addresses left, signal disconnect! */
2085 disconnect_neighbour (nl, GNUNET_NO);
2090 * Function that must be called by each plugin to notify the
2091 * transport service about the addresses under which the transport
2092 * provided by the plugin can be reached.
2094 * @param cls closure
2095 * @param name name of the transport that generated the address
2096 * @param addr one of the addresses of the host, NULL for the last address
2097 * the specific address format depends on the transport
2098 * @param addrlen length of the address
2099 * @param expires when should this address automatically expire?
2102 plugin_env_notify_address (void *cls,
2106 struct GNUNET_TIME_Relative expires)
2108 struct TransportPlugin *p = cls;
2109 struct OwnAddressList *al;
2110 struct GNUNET_TIME_Absolute abex;
2112 GNUNET_assert (addr != NULL);
2113 abex = GNUNET_TIME_relative_to_absolute (expires);
2114 GNUNET_assert (p == find_transport (name));
2118 if ((addrlen == al->addrlen) && (0 == memcmp (addr, &al[1], addrlen)))
2120 if (al->expires.value < abex.value)
2127 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2128 al->next = p->addresses;
2131 al->addrlen = addrlen;
2132 memcpy (&al[1], addr, addrlen);
2133 update_addresses (p, GNUNET_YES);
2138 * Notify all of our clients about a peer connecting.
2141 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2142 struct GNUNET_TIME_Relative latency,
2145 struct ConnectInfoMessage cim;
2146 struct TransportClient *cpos;
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2150 "Notifying clients about connection from `%s'\n",
2153 GNUNET_STATISTICS_update (stats,
2154 gettext_noop ("# peers connected"),
2157 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
2158 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2159 cim.distance = htonl (distance);
2160 cim.latency = GNUNET_TIME_relative_hton (latency);
2161 memcpy (&cim.id, peer, sizeof (struct GNUNET_PeerIdentity));
2163 while (cpos != NULL)
2165 transmit_to_client (cpos, &cim.header, GNUNET_NO);
2172 * Notify all of our clients about a peer disconnecting.
2175 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2177 struct DisconnectInfoMessage dim;
2178 struct TransportClient *cpos;
2181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2182 "Notifying clients about lost connection to `%s'\n",
2185 GNUNET_STATISTICS_update (stats,
2186 gettext_noop ("# peers connected"),
2189 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2190 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2191 dim.reserved = htonl (0);
2192 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2194 while (cpos != NULL)
2196 transmit_to_client (cpos, &dim.header, GNUNET_NO);
2203 * Find a ForeignAddressList entry for the given neighbour
2204 * that matches the given address and transport.
2206 * @param neighbour which peer we care about
2207 * @param tname name of the transport plugin
2208 * @param session session to look for, NULL for 'any'; otherwise
2209 * can be used for the service to "learn" this session ID
2211 * @param addr binary address
2212 * @param addrlen length of addr
2213 * @return NULL if no such entry exists
2215 static struct ForeignAddressList *
2216 find_peer_address(struct NeighbourList *neighbour,
2218 struct Session *session,
2222 struct ReadyList *head;
2223 struct ForeignAddressList *pos;
2225 head = neighbour->plugins;
2226 while (head != NULL)
2228 if (0 == strcmp (tname, head->plugin->short_name))
2234 pos = head->addresses;
2235 while ( (pos != NULL) &&
2236 ( (pos->addrlen != addrlen) ||
2237 (memcmp(pos->addr, addr, addrlen) != 0) ) )
2239 if ( (session != NULL) &&
2240 (pos->session == session) )
2244 if ( (session != NULL) && (pos != NULL) )
2245 pos->session = session; /* learn it! */
2251 * Get the peer address struct for the given neighbour and
2252 * address. If it doesn't yet exist, create it.
2254 * @param neighbour which peer we care about
2255 * @param tname name of the transport plugin
2256 * @param session session of the plugin, or NULL for none
2257 * @param addr binary address
2258 * @param addrlen length of addr
2259 * @return NULL if we do not have a transport plugin for 'tname'
2261 static struct ForeignAddressList *
2262 add_peer_address (struct NeighbourList *neighbour,
2264 struct Session *session,
2268 struct ReadyList *head;
2269 struct ForeignAddressList *ret;
2271 ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2274 head = neighbour->plugins;
2276 while (head != NULL)
2278 if (0 == strcmp (tname, head->plugin->short_name))
2284 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2285 ret->session = session;
2288 ret->addr = (const char*) &ret[1];
2289 memcpy (&ret[1], addr, addrlen);
2295 ret->addrlen = addrlen;
2296 ret->expires = GNUNET_TIME_relative_to_absolute
2297 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2298 ret->latency = GNUNET_TIME_relative_get_forever();
2300 ret->timeout = GNUNET_TIME_relative_to_absolute
2301 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2302 ret->ready_list = head;
2303 ret->next = head->addresses;
2304 head->addresses = ret;
2310 * Closure for 'add_validated_address'.
2312 struct AddValidatedAddressContext
2315 * Entry that has been validated.
2317 const struct ValidationEntry *ve;
2320 * Flag set after we have added the address so
2321 * that we terminate the iteration next time.
2328 * Callback function used to fill a buffer of max bytes with a list of
2329 * addresses in the format used by HELLOs. Should use
2330 * "GNUNET_HELLO_add_address" as a helper function.
2332 * @param cls the 'struct AddValidatedAddressContext' with the validated address
2333 * @param max maximum number of bytes that can be written to buf
2334 * @param buf where to write the address information
2335 * @return number of bytes written, 0 to signal the
2336 * end of the iteration.
2339 add_validated_address (void *cls,
2340 size_t max, void *buf)
2342 struct AddValidatedAddressContext *avac = cls;
2343 const struct ValidationEntry *ve = avac->ve;
2345 if (GNUNET_YES == avac->done)
2347 avac->done = GNUNET_YES;
2348 return GNUNET_HELLO_add_address (ve->transport_name,
2349 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2359 * Closure for 'check_address_exists'.
2361 struct CheckAddressExistsClosure
2364 * Address to check for.
2369 * Name of the transport.
2376 struct Session *session;
2379 * Set to GNUNET_YES if the address exists.
2392 * Iterator over hash map entries. Checks if the given
2393 * validation entry is for the same address as what is given
2396 * @param cls the 'struct CheckAddressExistsClosure*'
2397 * @param key current key code (ignored)
2398 * @param value value in the hash map ('struct ValidationEntry')
2399 * @return GNUNET_YES if we should continue to
2400 * iterate (mismatch), GNUNET_NO if not (entry matched)
2403 check_address_exists (void *cls,
2404 const GNUNET_HashCode * key,
2407 struct CheckAddressExistsClosure *caec = cls;
2408 struct ValidationEntry *ve = value;
2410 if ( (0 == strcmp (caec->tname,
2411 ve->transport_name)) &&
2412 (caec->addrlen == ve->addrlen) &&
2413 (0 == memcmp (caec->addr,
2417 caec->exists = GNUNET_YES;
2420 if ( (ve->session != NULL) &&
2421 (caec->session == ve->session) )
2423 caec->exists = GNUNET_YES;
2432 * Iterator to free entries in the validation_map.
2434 * @param cls closure (unused)
2435 * @param key current key code
2436 * @param value value in the hash map (validation to abort)
2437 * @return GNUNET_YES (always)
2440 abort_validation (void *cls,
2441 const GNUNET_HashCode * key,
2444 struct ValidationEntry *va = value;
2446 if (GNUNET_SCHEDULER_NO_TASK != va->timeout_task)
2447 GNUNET_SCHEDULER_cancel (sched, va->timeout_task);
2448 GNUNET_free (va->transport_name);
2449 if (va->chvc != NULL)
2451 va->chvc->ve_count--;
2452 if (va->chvc->ve_count == 0)
2454 GNUNET_CONTAINER_DLL_remove (chvc_head,
2457 GNUNET_free (va->chvc);
2467 * HELLO validation cleanup task (validation failed).
2469 * @param cls the 'struct ValidationEntry' that failed
2470 * @param tc scheduler context (unused)
2473 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2475 struct ValidationEntry *va = cls;
2476 struct GNUNET_PeerIdentity pid;
2478 va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2479 GNUNET_STATISTICS_update (stats,
2480 gettext_noop ("# address validation timeouts"),
2483 GNUNET_CRYPTO_hash (&va->publicKey,
2485 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2487 GNUNET_break (GNUNET_OK ==
2488 GNUNET_CONTAINER_multihashmap_remove (validation_map,
2491 abort_validation (NULL, NULL, va);
2496 neighbour_timeout_task (void *cls,
2497 const struct GNUNET_SCHEDULER_TaskContext *tc)
2499 struct NeighbourList *n = cls;
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2503 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
2505 GNUNET_STATISTICS_update (stats,
2506 gettext_noop ("# disconnects due to timeout"),
2509 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2510 disconnect_neighbour (n, GNUNET_NO);
2515 * Schedule the job that will cause us to send a PING to the
2516 * foreign address to evaluate its validity and latency.
2518 * @param fal address to PING
2521 schedule_next_ping (struct ForeignAddressList *fal);
2525 * Add the given address to the list of foreign addresses
2526 * available for the given peer (check for duplicates).
2528 * @param cls the respective 'struct NeighbourList' to update
2529 * @param tname name of the transport
2530 * @param expiration expiration time
2531 * @param addr the address
2532 * @param addrlen length of the address
2533 * @return GNUNET_OK (always)
2536 add_to_foreign_address_list (void *cls,
2538 struct GNUNET_TIME_Absolute expiration,
2542 struct NeighbourList *n = cls;
2543 struct ForeignAddressList *fal;
2546 GNUNET_STATISTICS_update (stats,
2547 gettext_noop ("# valid peer addresses returned by PEERINFO"),
2551 fal = find_peer_address (n, tname, NULL, addr, addrlen);
2555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2556 "Adding address `%s' (%s) for peer `%4s' due to PEERINFO data for %llums.\n",
2557 a2s (tname, addr, addrlen),
2559 GNUNET_i2s (&n->id),
2562 fal = add_peer_address (n, tname, NULL, addr, addrlen);
2565 GNUNET_STATISTICS_update (stats,
2566 gettext_noop ("# previously validated addresses lacking transport"),
2572 fal->expires = GNUNET_TIME_absolute_max (expiration,
2574 schedule_next_ping (fal);
2580 fal->expires = GNUNET_TIME_absolute_max (expiration,
2585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2586 "Failed to add new address for `%4s'\n",
2587 GNUNET_i2s (&n->id));
2590 if (fal->validated == GNUNET_NO)
2592 fal->validated = GNUNET_YES;
2593 GNUNET_STATISTICS_update (stats,
2594 gettext_noop ("# peer addresses considered valid"),
2598 if (try == GNUNET_YES)
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2601 "Have new addresses, will try to trigger transmissions.\n");
2602 try_transmission_to_peer (n);
2609 * Add addresses in validated HELLO "h" to the set of addresses
2610 * we have for this peer.
2612 * @param cls closure ('struct NeighbourList*')
2613 * @param peer id of the peer, NULL for last call
2614 * @param h hello message for the peer (can be NULL)
2615 * @param trust amount of trust we have in the peer (not used)
2618 add_hello_for_peer (void *cls,
2619 const struct GNUNET_PeerIdentity *peer,
2620 const struct GNUNET_HELLO_Message *h,
2623 struct NeighbourList *n = cls;
2631 return; /* no HELLO available */
2633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2634 "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
2638 if (GNUNET_YES != n->public_key_valid)
2640 GNUNET_HELLO_get_key (h, &n->publicKey);
2641 n->public_key_valid = GNUNET_YES;
2643 GNUNET_HELLO_iterate_addresses (h,
2645 &add_to_foreign_address_list,
2651 * Create a fresh entry in our neighbour list for the given peer.
2652 * Will try to transmit our current HELLO to the new neighbour.
2653 * Do not call this function directly, use 'setup_peer_check_blacklist.
2655 * @param peer the peer for which we create the entry
2656 * @param do_hello should we schedule transmitting a HELLO
2657 * @return the new neighbour list entry
2659 static struct NeighbourList *
2660 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
2663 struct NeighbourList *n;
2664 struct TransportPlugin *tp;
2665 struct ReadyList *rl;
2668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2669 "Setting up state for neighbour `%4s'\n",
2672 GNUNET_assert (our_hello != NULL);
2673 GNUNET_STATISTICS_update (stats,
2674 gettext_noop ("# active neighbours"),
2677 n = GNUNET_malloc (sizeof (struct NeighbourList));
2678 n->next = neighbours;
2682 GNUNET_TIME_relative_to_absolute
2683 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2684 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2685 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2686 MAX_BANDWIDTH_CARRY_S);
2690 if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
2692 rl = GNUNET_malloc (sizeof (struct ReadyList));
2694 rl->next = n->plugins;
2697 rl->addresses = NULL;
2701 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2703 n->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
2704 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2705 &neighbour_timeout_task, n);
2708 n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
2709 0, GNUNET_TIME_UNIT_FOREVER_REL,
2710 &add_hello_for_peer, n);
2711 transmit_to_peer (NULL, NULL, 0,
2712 HELLO_ADDRESS_EXPIRATION,
2713 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
2721 * Function called after we have checked if communicating
2722 * with a given peer is acceptable.
2724 * @param cls closure
2725 * @param n NULL if communication is not acceptable
2727 typedef void (*SetupContinuation)(void *cls,
2728 struct NeighbourList *n);
2732 * Information kept for each client registered to perform
2738 * This is a linked list.
2740 struct Blacklisters *next;
2743 * This is a linked list.
2745 struct Blacklisters *prev;
2748 * Client responsible for this entry.
2750 struct GNUNET_SERVER_Client *client;
2753 * Blacklist check that we're currently performing.
2755 struct BlacklistCheck *bc;
2761 * Head of DLL of blacklisting clients.
2763 static struct Blacklisters *bl_head;
2766 * Tail of DLL of blacklisting clients.
2768 static struct Blacklisters *bl_tail;
2772 * Context we use when performing a blacklist check.
2774 struct BlacklistCheck
2778 * This is a linked list.
2780 struct BlacklistCheck *next;
2783 * This is a linked list.
2785 struct BlacklistCheck *prev;
2788 * Peer being checked.
2790 struct GNUNET_PeerIdentity peer;
2793 * Option for setup neighbour afterwards.
2798 * Continuation to call with the result.
2800 SetupContinuation cont;
2808 * Current transmission request handle for this client, or NULL if no
2809 * request is pending.
2811 struct GNUNET_CONNECTION_TransmitHandle *th;
2814 * Our current position in the blacklisters list.
2816 struct Blacklisters *bl_pos;
2819 * Current task performing the check.
2821 GNUNET_SCHEDULER_TaskIdentifier task;
2826 * Head of DLL of active blacklisting queries.
2828 static struct BlacklistCheck *bc_head;
2831 * Tail of DLL of active blacklisting queries.
2833 static struct BlacklistCheck *bc_tail;
2837 * Perform next action in the blacklist check.
2839 * @param cls the 'struct BlacklistCheck*'
2843 do_blacklist_check (void *cls,
2844 const struct GNUNET_SCHEDULER_TaskContext *tc);
2848 * Transmit blacklist query to the client.
2850 * @param cls the 'struct BlacklistCheck'
2851 * @param size number of bytes allowed
2852 * @param buf where to copy the message
2853 * @return number of bytes copied to buf
2856 transmit_blacklist_message (void *cls,
2860 struct BlacklistCheck *bc = cls;
2861 struct Blacklisters *bl;
2862 struct BlacklistMessage bm;
2867 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
2868 bc->task = GNUNET_SCHEDULER_add_now (sched,
2869 &do_blacklist_check,
2874 bm.header.size = htons (sizeof (struct BlacklistMessage));
2875 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
2876 bm.is_allowed = htonl (0);
2878 memcpy (buf, &bm, sizeof (bm));
2879 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
2885 * Perform next action in the blacklist check.
2887 * @param cls the 'struct BlacklistCheck*'
2891 do_blacklist_check (void *cls,
2892 const struct GNUNET_SCHEDULER_TaskContext *tc)
2894 struct BlacklistCheck *bc = cls;
2895 struct Blacklisters *bl;
2897 bc->task = GNUNET_SCHEDULER_NO_TASK;
2901 bc->cont (bc->cont_cls,
2902 setup_new_neighbour (&bc->peer, bc->do_hello));
2909 bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
2910 sizeof (struct BlacklistMessage),
2911 GNUNET_TIME_UNIT_FOREVER_REL,
2912 &transmit_blacklist_message,
2919 * Obtain a 'struct NeighbourList' for the given peer. If such an entry
2920 * does not yet exist, check the blacklist. If the blacklist says creating
2921 * one is acceptable, create one and call the continuation; otherwise
2922 * call the continuation with NULL.
2924 * @param peer peer to setup or look up a struct NeighbourList for
2925 * @param do_hello should we also schedule sending our HELLO to the peer
2926 * if this is a new record
2927 * @param cont function to call with the 'struct NeigbhbourList*'
2928 * @param cont_cls closure for cont
2931 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
2933 SetupContinuation cont,
2936 struct NeighbourList *n;
2937 struct BlacklistCheck *bc;
2939 n = find_neighbour(peer);
2945 if (bl_head == NULL)
2948 setup_new_neighbour (peer, do_hello));
2951 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
2952 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
2954 bc->do_hello = do_hello;
2956 bc->cont_cls = cont_cls;
2957 bc->bl_pos = bl_head;
2958 bc->task = GNUNET_SCHEDULER_add_now (sched,
2959 &do_blacklist_check,
2965 * Function called with the result of querying a new blacklister about
2966 * it being allowed (or not) to continue to talk to an existing neighbour.
2968 * @param cls the original 'struct NeighbourList'
2969 * @param n NULL if we need to disconnect
2972 confirm_or_drop_neighbour (void *cls,
2973 struct NeighbourList *n)
2975 struct NeighbourList * orig = cls;
2978 disconnect_neighbour (orig, GNUNET_NO);
2983 * Handle a request to start a blacklist.
2985 * @param cls closure (always NULL)
2986 * @param client identification of the client
2987 * @param message the actual message
2990 handle_blacklist_init (void *cls,
2991 struct GNUNET_SERVER_Client *client,
2992 const struct GNUNET_MessageHeader *message)
2994 struct Blacklisters *bl;
2995 struct BlacklistCheck *bc;
2996 struct NeighbourList *n;
3001 if (bl->client == client)
3004 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3009 bl = GNUNET_malloc (sizeof (struct Blacklisters));
3010 bl->client = client;
3011 GNUNET_SERVER_client_keep (client);
3012 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
3013 /* confirm that all existing connections are OK! */
3017 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3018 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3020 bc->do_hello = GNUNET_NO;
3021 bc->cont = &confirm_or_drop_neighbour;
3024 if (n == neighbours) /* all would wait for the same client, no need to
3025 create more than just the first task right now */
3026 bc->task = GNUNET_SCHEDULER_add_now (sched,
3027 &do_blacklist_check,
3035 * Handle a request to blacklist a peer.
3037 * @param cls closure (always NULL)
3038 * @param client identification of the client
3039 * @param message the actual message
3042 handle_blacklist_reply (void *cls,
3043 struct GNUNET_SERVER_Client *client,
3044 const struct GNUNET_MessageHeader *message)
3046 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
3047 struct Blacklisters *bl;
3048 struct BlacklistCheck *bc;
3051 while ( (bl != NULL) &&
3052 (bl->client != client) )
3056 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3061 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
3063 bc->cont (bc->cont_cls, NULL);
3064 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
3069 bc->bl_pos = bc->bl_pos->next;
3070 bc->task = GNUNET_SCHEDULER_add_now (sched,
3071 &do_blacklist_check,
3074 /* check if any other bc's are waiting for this blacklister */
3078 if ( (bc->bl_pos == bl) &&
3079 (GNUNET_SCHEDULER_NO_TASK == bc->task) )
3080 bc->task = GNUNET_SCHEDULER_add_now (sched,
3081 &do_blacklist_check,
3089 * Send periodic PING messages to a given foreign address.
3091 * @param cls our 'struct PeriodicValidationContext*'
3092 * @param tc task context
3095 send_periodic_ping (void *cls,
3096 const struct GNUNET_SCHEDULER_TaskContext *tc)
3098 struct ForeignAddressList *peer_address = cls;
3099 struct TransportPlugin *tp;
3100 struct ValidationEntry *va;
3101 struct NeighbourList *neighbour;
3102 struct TransportPingMessage ping;
3103 struct CheckAddressExistsClosure caec;
3105 uint16_t hello_size;
3109 peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3110 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3112 tp = peer_address->ready_list->plugin;
3113 neighbour = peer_address->ready_list->neighbour;
3114 if (GNUNET_YES != neighbour->public_key_valid)
3116 /* no public key yet, try again later */
3117 schedule_next_ping (peer_address);
3120 caec.addr = peer_address->addr;
3121 caec.addrlen = peer_address->addrlen;
3122 caec.tname = tp->short_name;
3123 caec.session = peer_address->session;
3124 caec.exists = GNUNET_NO;
3125 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3126 &check_address_exists,
3128 if (caec.exists == GNUNET_YES)
3130 /* During validation attempts we will likely trigger the other
3131 peer trying to validate our address which in turn will cause
3132 it to send us its HELLO, so we expect to hit this case rather
3133 frequently. Only print something if we are very verbose. */
3134 #if DEBUG_TRANSPORT > 1
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3136 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3137 (peer_address->addr != NULL)
3138 ? a2s (tp->short_name,
3140 peer_address->addrlen)
3143 GNUNET_i2s (&neighbour->id));
3145 schedule_next_ping (peer_address);
3148 va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3149 va->transport_name = GNUNET_strdup (tp->short_name);
3150 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3152 va->send_time = GNUNET_TIME_absolute_get();
3153 va->session = peer_address->session;
3154 if (peer_address->addr != NULL)
3156 va->addr = (const void*) &va[1];
3157 memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3158 va->addrlen = peer_address->addrlen;
3160 memcpy(&va->publicKey,
3161 &neighbour->publicKey,
3162 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3164 va->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
3165 HELLO_VERIFICATION_TIMEOUT,
3166 &timeout_hello_validation,
3168 GNUNET_CONTAINER_multihashmap_put (validation_map,
3169 &neighbour->id.hashPubKey,
3171 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3172 hello_size = GNUNET_HELLO_size(our_hello);
3173 tsize = sizeof(struct TransportPingMessage) + hello_size;
3174 if (peer_address->addr != NULL)
3176 slen = strlen (tp->short_name) + 1;
3177 tsize += slen + peer_address->addrlen;
3181 slen = 0; /* make gcc happy */
3183 message_buf = GNUNET_malloc(tsize);
3184 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3185 ping.challenge = htonl(va->challenge);
3186 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3187 memcpy(message_buf, our_hello, hello_size);
3188 if (peer_address->addr != NULL)
3190 ping.header.size = htons(sizeof(struct TransportPingMessage) +
3191 peer_address->addrlen +
3193 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3196 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3198 peer_address->addrlen);
3202 ping.header.size = htons(sizeof(struct TransportPingMessage));
3204 memcpy(&message_buf[hello_size],
3206 sizeof(struct TransportPingMessage));
3208 #if DEBUG_TRANSPORT_REVALIDATION
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3210 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s'\n",
3211 (peer_address->addr != NULL)
3212 ? a2s (peer_address->plugin->short_name,
3214 peer_address->addrlen)
3217 GNUNET_i2s (&neighbour->id),
3218 "HELLO", hello_size,
3221 GNUNET_STATISTICS_update (stats,
3222 gettext_noop ("# PING messages sent for re-validation"),
3225 transmit_to_peer (NULL, peer_address,
3226 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3227 HELLO_VERIFICATION_TIMEOUT,
3229 GNUNET_YES, neighbour);
3230 GNUNET_free(message_buf);
3231 schedule_next_ping (peer_address);
3236 * Schedule the job that will cause us to send a PING to the
3237 * foreign address to evaluate its validity and latency.
3239 * @param fal address to PING
3242 schedule_next_ping (struct ForeignAddressList *fal)
3244 struct GNUNET_TIME_Relative delay;
3246 if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3248 delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3249 delay.value /= 2; /* do before expiration */
3250 delay = GNUNET_TIME_relative_min (delay,
3251 LATENCY_EVALUATION_MAX_DELAY);
3252 if (GNUNET_YES != fal->estimated)
3254 delay = GNUNET_TIME_UNIT_ZERO;
3255 fal->estimated = GNUNET_YES;
3257 if (GNUNET_YES == fal->connected)
3259 delay = GNUNET_TIME_relative_min (delay,
3260 CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3262 /* FIXME: also adjust delay based on how close the last
3263 observed latency is to the latency of the best alternative */
3264 /* bound how fast we can go */
3265 delay = GNUNET_TIME_relative_max (delay,
3266 GNUNET_TIME_UNIT_SECONDS);
3267 /* randomize a bit (to avoid doing all at the same time) */
3268 delay.value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3269 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(sched,
3271 &send_periodic_ping,
3279 * Function that will be called if we receive some payload
3280 * from another peer.
3282 * @param message the payload
3283 * @param n peer who claimed to be the sender
3286 handle_payload_message (const struct GNUNET_MessageHeader *message,
3287 struct NeighbourList *n)
3289 struct InboundMessage *im;
3290 struct TransportClient *cpos;
3293 msize = ntohs (message->size);
3294 if (n->received_pong == GNUNET_NO)
3296 GNUNET_free_non_null (n->pre_connect_message_buffer);
3297 n->pre_connect_message_buffer = GNUNET_malloc (msize);
3298 memcpy (n->pre_connect_message_buffer, message, msize);
3302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3303 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
3304 ntohs (message->type),
3305 ntohs (message->size),
3306 GNUNET_i2s (&n->id));
3308 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3311 n->quota_violation_count++;
3313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3314 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
3315 n->in_tracker.available_bytes_per_s__,
3316 n->quota_violation_count);
3318 /* Discount 32k per violation */
3319 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3324 if (n->quota_violation_count > 0)
3326 /* try to add 32k back */
3327 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3329 n->quota_violation_count--;
3332 GNUNET_STATISTICS_update (stats,
3333 gettext_noop ("# payload received from other peers"),
3336 /* transmit message to all clients */
3337 im = GNUNET_malloc (sizeof (struct InboundMessage) + msize);
3338 im->header.size = htons (sizeof (struct InboundMessage) + msize);
3339 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3340 im->latency = GNUNET_TIME_relative_hton (n->latency);
3342 im->distance = ntohl(n->distance);
3343 memcpy (&im[1], message, msize);
3345 while (cpos != NULL)
3347 transmit_to_client (cpos, &im->header, GNUNET_YES);
3355 * Iterator over hash map entries. Checks if the given validation
3356 * entry is for the same challenge as what is given in the PONG.
3358 * @param cls the 'struct TransportPongMessage*'
3359 * @param key peer identity
3360 * @param value value in the hash map ('struct ValidationEntry')
3361 * @return GNUNET_YES if we should continue to
3362 * iterate (mismatch), GNUNET_NO if not (entry matched)
3365 check_pending_validation (void *cls,
3366 const GNUNET_HashCode * key,
3369 const struct TransportPongMessage *pong = cls;
3370 struct ValidationEntry *ve = value;
3371 struct AddValidatedAddressContext avac;
3372 unsigned int challenge = ntohl(pong->challenge);
3373 struct GNUNET_HELLO_Message *hello;
3374 struct GNUNET_PeerIdentity target;
3375 struct NeighbourList *n;
3376 struct ForeignAddressList *fal;
3377 struct OwnAddressList *oal;
3378 struct TransportPlugin *tp;
3379 struct GNUNET_MessageHeader *prem;
3385 ps = ntohs (pong->header.size);
3386 if (ps < sizeof (struct TransportPongMessage))
3388 GNUNET_break_op (0);
3391 addr = (const char*) &pong[1];
3392 slen = strlen (ve->transport_name) + 1;
3393 if ( (ps - sizeof (struct TransportPongMessage) != ve->addrlen + slen) ||
3394 (ve->challenge != challenge) ||
3395 (addr[slen-1] != '\0') ||
3396 (0 != strcmp (addr, ve->transport_name)) ||
3397 (ntohl (pong->purpose.size)
3398 != sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3400 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
3401 sizeof (struct GNUNET_PeerIdentity) + ve->addrlen + slen) )
3403 alen = ps - sizeof (struct TransportPongMessage) - slen;
3404 switch (ntohl (pong->purpose.purpose))
3406 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN:
3407 if ( (ve->addrlen + slen != ntohl (pong->addrlen)) ||
3408 (0 != memcmp (&addr[slen],
3411 return GNUNET_YES; /* different entry, keep trying! */
3412 if (0 != memcmp (&pong->pid,
3414 sizeof (struct GNUNET_PeerIdentity)))
3416 GNUNET_break_op (0);
3420 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
3425 GNUNET_break_op (0);
3429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3430 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
3432 a2s (ve->transport_name,
3433 (const struct sockaddr *) ve->addr,
3435 ve->transport_name);
3438 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING:
3439 if (ve->addrlen != 0)
3440 return GNUNET_YES; /* different entry, keep trying */
3441 if ( (0 != memcmp (&pong->pid,
3443 sizeof (struct GNUNET_PeerIdentity))) ||
3444 (ve->addrlen != 0) )
3446 GNUNET_break_op (0);
3449 tp = find_transport (ve->transport_name);
3455 oal = tp->addresses;
3458 if ( (oal->addrlen == alen) &&
3459 (0 == memcmp (&oal[1],
3467 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3468 _("Not accepting PONG with address `%s' since I cannot confirm having this address.\n"),
3469 a2s (ve->transport_name,
3475 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING,
3480 GNUNET_break_op (0);
3484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3485 "Confirmed that peer `%4s' is talking to us using address `%s' (%s) for us.\n",
3487 a2s (ve->transport_name,
3490 ve->transport_name);
3494 GNUNET_break_op (0);
3497 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).value == 0)
3499 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3500 _("Received expired signature. Check system time.\n"));
3503 GNUNET_STATISTICS_update (stats,
3504 gettext_noop ("# address validation successes"),
3507 /* create the updated HELLO */
3508 GNUNET_CRYPTO_hash (&ve->publicKey,
3509 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3510 &target.hashPubKey);
3511 if (ve->addr != NULL)
3513 avac.done = GNUNET_NO;
3515 hello = GNUNET_HELLO_create (&ve->publicKey,
3516 &add_validated_address,
3518 GNUNET_PEERINFO_add_peer (peerinfo,
3520 GNUNET_free (hello);
3522 n = find_neighbour (&target);
3525 n->publicKey = ve->publicKey;
3526 n->public_key_valid = GNUNET_YES;
3527 fal = add_peer_address (n,
3532 GNUNET_assert (fal != NULL);
3533 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
3534 fal->validated = GNUNET_YES;
3535 mark_address_connected (fal);
3536 GNUNET_STATISTICS_update (stats,
3537 gettext_noop ("# peer addresses considered valid"),
3540 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
3541 schedule_next_ping (fal);
3542 if (n->latency.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
3543 n->latency = fal->latency;
3545 n->latency.value = (fal->latency.value + n->latency.value) / 2;
3547 n->distance = fal->distance;
3548 if (GNUNET_NO == n->received_pong)
3550 n->received_pong = GNUNET_YES;
3551 notify_clients_connect (&target, n->latency, n->distance);
3552 if (NULL != (prem = n->pre_connect_message_buffer))
3554 n->pre_connect_message_buffer = NULL;
3555 handle_payload_message (prem, n);
3559 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
3561 GNUNET_SCHEDULER_cancel (sched,
3563 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
3564 try_transmission_to_peer (n);
3568 /* clean up validation entry */
3569 GNUNET_assert (GNUNET_YES ==
3570 GNUNET_CONTAINER_multihashmap_remove (validation_map,
3573 abort_validation (NULL, NULL, ve);
3579 * Function that will be called if we receive a validation
3580 * of an address challenge that we transmitted to another
3581 * peer. Note that the validation should only be considered
3582 * acceptable if the challenge matches AND if the sender
3583 * address is at least a plausible address for this peer
3584 * (otherwise we may be seeing a MiM attack).
3586 * @param cls closure
3587 * @param message the pong message
3588 * @param peer who responded to our challenge
3589 * @param sender_address string describing our sender address (as observed
3590 * by the other peer in binary format)
3591 * @param sender_address_len number of bytes in 'sender_address'
3594 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
3595 const struct GNUNET_PeerIdentity *peer,
3596 const char *sender_address,
3597 size_t sender_address_len)
3599 #if DEBUG_TRANSPORT > 1
3600 /* we get tons of these that just get discarded, only log
3601 if we are quite verbose */
3602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3603 "Receiving `%s' message from `%4s'.\n", "PONG",
3606 GNUNET_STATISTICS_update (stats,
3607 gettext_noop ("# PONG messages received"),
3610 if (GNUNET_SYSERR !=
3611 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
3613 &check_pending_validation,
3616 /* This is *expected* to happen a lot since we send
3617 PONGs to *all* known addresses of the sender of
3618 the PING, so most likely we get multiple PONGs
3619 per PING, and all but the first PONG will end up
3620 here. So really we should not print anything here
3621 unless we want to be very, very verbose... */
3622 #if DEBUG_TRANSPORT > 2
3623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3624 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
3636 * Try to validate a neighbour's address by sending him our HELLO and a PING.
3638 * @param cls the 'struct ValidationEntry*'
3639 * @param neighbour neighbour to validate, NULL if validation failed
3642 transmit_hello_and_ping (void *cls,
3643 struct NeighbourList *neighbour)
3645 struct ValidationEntry *va = cls;
3646 struct ForeignAddressList *peer_address;
3647 struct TransportPingMessage ping;
3648 uint16_t hello_size;
3651 struct GNUNET_PeerIdentity id;
3654 GNUNET_CRYPTO_hash (&va->publicKey,
3655 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3657 if (neighbour == NULL)
3659 /* FIXME: stats... */
3660 GNUNET_break (GNUNET_OK ==
3661 GNUNET_CONTAINER_multihashmap_remove (validation_map,
3664 abort_validation (NULL, NULL, va);
3667 neighbour->publicKey = va->publicKey;
3668 neighbour->public_key_valid = GNUNET_YES;
3669 peer_address = add_peer_address (neighbour,
3670 va->transport_name, NULL,
3671 (const void*) &va[1],
3673 if (peer_address == NULL)
3675 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3676 "Failed to add peer `%4s' for plugin `%s'\n",
3677 GNUNET_i2s (&neighbour->id),
3678 va->transport_name);
3679 GNUNET_break (GNUNET_OK ==
3680 GNUNET_CONTAINER_multihashmap_remove (validation_map,
3683 abort_validation (NULL, NULL, va);
3686 hello_size = GNUNET_HELLO_size(our_hello);
3687 slen = strlen(va->transport_name) + 1;
3688 tsize = sizeof(struct TransportPingMessage) + hello_size + va->addrlen + slen;
3689 message_buf = GNUNET_malloc(tsize);
3690 ping.challenge = htonl(va->challenge);
3691 ping.header.size = htons(sizeof(struct TransportPingMessage) + slen + va->addrlen);
3692 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3693 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3694 memcpy(message_buf, our_hello, hello_size);
3695 memcpy(&message_buf[hello_size],
3697 sizeof(struct TransportPingMessage));
3698 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3701 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3706 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
3709 : a2s (va->transport_name,
3710 (const void*) &va[1], va->addrlen),
3712 GNUNET_i2s (&neighbour->id),
3713 "HELLO", hello_size,
3714 "PING", sizeof (struct TransportPingMessage) + va->addrlen + slen);
3717 GNUNET_STATISTICS_update (stats,
3718 gettext_noop ("# PING messages sent for initial validation"),
3721 transmit_to_peer (NULL, peer_address,
3722 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3723 HELLO_VERIFICATION_TIMEOUT,
3725 GNUNET_YES, neighbour);
3726 GNUNET_free(message_buf);
3731 * Check if the given address is already being validated; if not,
3732 * append the given address to the list of entries that are being be
3733 * validated and initiate validation.
3735 * @param cls closure ('struct CheckHelloValidatedContext *')
3736 * @param tname name of the transport
3737 * @param expiration expiration time
3738 * @param addr the address
3739 * @param addrlen length of the address
3740 * @return GNUNET_OK (always)
3743 run_validation (void *cls,
3745 struct GNUNET_TIME_Absolute expiration,
3749 struct CheckHelloValidatedContext *chvc = cls;
3750 struct GNUNET_PeerIdentity id;
3751 struct TransportPlugin *tp;
3752 struct ValidationEntry *va;
3753 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
3754 struct CheckAddressExistsClosure caec;
3755 struct OwnAddressList *oal;
3757 GNUNET_assert (addr != NULL);
3759 GNUNET_STATISTICS_update (stats,
3760 gettext_noop ("# peer addresses scheduled for validation"),
3763 tp = find_transport (tname);
3766 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
3767 GNUNET_ERROR_TYPE_BULK,
3769 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
3771 GNUNET_STATISTICS_update (stats,
3772 gettext_noop ("# peer addresses not validated (plugin not available)"),
3777 /* check if this is one of our own addresses */
3778 oal = tp->addresses;
3781 if ( (oal->addrlen == addrlen) &&
3782 (0 == memcmp (&oal[1],
3786 /* not plausible, this address is equivalent to our own address! */
3787 GNUNET_STATISTICS_update (stats,
3788 gettext_noop ("# peer addresses not validated (loopback)"),
3795 GNUNET_HELLO_get_key (chvc->hello, &pk);
3796 GNUNET_CRYPTO_hash (&pk,
3798 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3801 if (is_blacklisted(&id, tp))
3804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3805 "Attempted to validate blacklisted peer `%s' using `%s'!\n",
3813 caec.addrlen = addrlen;
3814 caec.session = NULL;
3816 caec.exists = GNUNET_NO;
3817 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3818 &check_address_exists,
3820 if (caec.exists == GNUNET_YES)
3822 /* During validation attempts we will likely trigger the other
3823 peer trying to validate our address which in turn will cause
3824 it to send us its HELLO, so we expect to hit this case rather
3825 frequently. Only print something if we are very verbose. */
3826 #if DEBUG_TRANSPORT > 1
3827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3828 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3829 a2s (tname, addr, addrlen),
3833 GNUNET_STATISTICS_update (stats,
3834 gettext_noop ("# peer addresses not validated (in progress)"),
3839 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
3842 va->transport_name = GNUNET_strdup (tname);
3843 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3845 va->send_time = GNUNET_TIME_absolute_get();
3846 va->addr = (const void*) &va[1];
3847 memcpy (&va[1], addr, addrlen);
3848 va->addrlen = addrlen;
3849 GNUNET_HELLO_get_key (chvc->hello,
3851 va->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
3852 HELLO_VERIFICATION_TIMEOUT,
3853 &timeout_hello_validation,
3855 GNUNET_CONTAINER_multihashmap_put (validation_map,
3858 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3859 setup_peer_check_blacklist (&id, GNUNET_NO,
3860 &transmit_hello_and_ping,
3867 * Check if addresses in validated hello "h" overlap with
3868 * those in "chvc->hello" and validate the rest.
3870 * @param cls closure
3871 * @param peer id of the peer, NULL for last call
3872 * @param h hello message for the peer (can be NULL)
3873 * @param trust amount of trust we have in the peer (not used)
3876 check_hello_validated (void *cls,
3877 const struct GNUNET_PeerIdentity *peer,
3878 const struct GNUNET_HELLO_Message *h,
3881 struct CheckHelloValidatedContext *chvc = cls;
3882 struct GNUNET_HELLO_Message *plain_hello;
3883 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
3884 struct GNUNET_PeerIdentity target;
3885 struct NeighbourList *n;
3890 if (GNUNET_NO == chvc->hello_known)
3892 /* notify PEERINFO about the peer now, so that we at least
3893 have the public key if some other component needs it */
3894 GNUNET_HELLO_get_key (chvc->hello, &pk);
3895 GNUNET_CRYPTO_hash (&pk,
3896 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3897 &target.hashPubKey);
3898 plain_hello = GNUNET_HELLO_create (&pk,
3901 GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
3902 GNUNET_free (plain_hello);
3904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3905 "PEERINFO had no `%s' message for peer `%4s', full validation needed.\n",
3907 GNUNET_i2s (&target));
3909 GNUNET_STATISTICS_update (stats,
3910 gettext_noop ("# new HELLOs requiring full validation"),
3913 GNUNET_HELLO_iterate_addresses (chvc->hello,
3920 GNUNET_STATISTICS_update (stats,
3921 gettext_noop ("# duplicate HELLO (peer known)"),
3926 if (chvc->ve_count == 0)
3928 GNUNET_CONTAINER_DLL_remove (chvc_head,
3938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3939 "PEERINFO had `%s' message for peer `%4s', validating only new addresses.\n",
3943 chvc->hello_known = GNUNET_YES;
3944 n = find_neighbour (peer);
3947 GNUNET_HELLO_iterate_addresses (h,
3949 &add_to_foreign_address_list,
3951 try_transmission_to_peer (n);
3955 GNUNET_STATISTICS_update (stats,
3956 gettext_noop ("# no existing neighbour record (validating HELLO)"),
3960 GNUNET_STATISTICS_update (stats,
3961 gettext_noop ("# HELLO validations (update case)"),
3964 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
3966 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
3973 * Process HELLO-message.
3975 * @param plugin transport involved, may be NULL
3976 * @param message the actual message
3977 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
3980 process_hello (struct TransportPlugin *plugin,
3981 const struct GNUNET_MessageHeader *message)
3984 struct GNUNET_PeerIdentity target;
3985 const struct GNUNET_HELLO_Message *hello;
3986 struct CheckHelloValidatedContext *chvc;
3987 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
3988 #if DEBUG_TRANSPORT_HELLO
3991 hsize = ntohs (message->size);
3992 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
3993 (hsize < sizeof (struct GNUNET_MessageHeader)))
3996 return GNUNET_SYSERR;
3998 GNUNET_STATISTICS_update (stats,
3999 gettext_noop ("# HELLOs received for validation"),
4003 /* first, check if load is too high */
4004 if (GNUNET_SCHEDULER_get_load (sched,
4005 GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
4007 GNUNET_STATISTICS_update (stats,
4008 gettext_noop ("# HELLOs ignored due to high load"),
4011 #if DEBUG_TRANSPORT_HELLO
4012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4013 "Ignoring `%s' for `%4s', load too high.\n",
4015 GNUNET_i2s (&target));
4019 hello = (const struct GNUNET_HELLO_Message *) message;
4020 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
4022 #if DEBUG_TRANSPORT_HELLO
4023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4024 "Unable to get public key from `%s' for `%4s'!\n",
4026 GNUNET_i2s (&target));
4028 GNUNET_break_op (0);
4029 return GNUNET_SYSERR;
4032 GNUNET_CRYPTO_hash (&publicKey,
4033 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4034 &target.hashPubKey);
4036 if (0 == memcmp (&my_identity,
4038 sizeof (struct GNUNET_PeerIdentity)))
4040 GNUNET_STATISTICS_update (stats,
4041 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
4047 while (NULL != chvc)
4049 if (GNUNET_HELLO_equals (hello,
4051 GNUNET_TIME_absolute_get ()).value > 0)
4053 #if DEBUG_TRANSPORT_HELLO
4054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4055 "Received duplicate `%s' message for `%4s'; ignored\n",
4057 GNUNET_i2s (&target));
4059 return GNUNET_OK; /* validation already pending */
4061 if (GNUNET_HELLO_size(hello) == GNUNET_HELLO_size (chvc->hello))
4062 GNUNET_break (0 != memcmp (hello, chvc->hello,
4063 GNUNET_HELLO_size(hello)));
4066 #if DEBUG_TRANSPORT_HELLO
4069 my_id = GNUNET_strdup(GNUNET_i2s(plugin->env.my_identity));
4070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4071 "%s: Starting validation of `%s' message for `%4s' via '%s' of size %u\n",
4074 GNUNET_i2s (&target),
4076 GNUNET_HELLO_size(hello));
4080 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
4082 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
4083 memcpy (&chvc[1], hello, hsize);
4084 GNUNET_CONTAINER_DLL_insert (chvc_head,
4087 /* finally, check if HELLO was previously validated
4088 (continuation will then schedule actual validation) */
4089 chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
4092 HELLO_VERIFICATION_TIMEOUT,
4093 &check_hello_validated, chvc);
4099 * The peer specified by the given neighbour has timed-out or a plugin
4100 * has disconnected. We may either need to do nothing (other plugins
4101 * still up), or trigger a full disconnect and clean up. This
4102 * function updates our state and does the necessary notifications.
4103 * Also notifies our clients that the neighbour is now officially
4106 * @param n the neighbour list entry for the peer
4107 * @param check should we just check if all plugins
4108 * disconnected or must we ask all plugins to
4112 disconnect_neighbour (struct NeighbourList *n, int check)
4114 struct ReadyList *rpos;
4115 struct NeighbourList *npos;
4116 struct NeighbourList *nprev;
4117 struct MessageQueue *mq;
4118 struct ForeignAddressList *peer_addresses;
4119 struct ForeignAddressList *peer_pos;
4121 if (GNUNET_YES == check)
4124 while (NULL != rpos)
4126 peer_addresses = rpos->addresses;
4127 while (peer_addresses != NULL)
4129 if (GNUNET_YES == peer_addresses->connected)
4130 return; /* still connected */
4131 peer_addresses = peer_addresses->next;
4137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4138 "Disconnecting from `%4s'\n",
4139 GNUNET_i2s (&n->id));
4141 /* remove n from neighbours list */
4144 while ((npos != NULL) && (npos != n))
4149 GNUNET_assert (npos != NULL);
4151 neighbours = n->next;
4153 nprev->next = n->next;
4155 /* notify all clients about disconnect */
4156 if (GNUNET_YES == n->received_pong)
4157 notify_clients_disconnect (&n->id);
4159 /* clean up all plugins, cancel connections and pending transmissions */
4160 while (NULL != (rpos = n->plugins))
4162 n->plugins = rpos->next;
4163 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
4164 while (rpos->addresses != NULL)
4166 peer_pos = rpos->addresses;
4167 rpos->addresses = peer_pos->next;
4168 if (peer_pos->connected == GNUNET_YES)
4169 GNUNET_STATISTICS_update (stats,
4170 gettext_noop ("# connected addresses"),
4173 if (GNUNET_YES == peer_pos->validated)
4174 GNUNET_STATISTICS_update (stats,
4175 gettext_noop ("# peer addresses considered valid"),
4178 if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
4180 GNUNET_SCHEDULER_cancel (sched,
4181 peer_pos->revalidate_task);
4182 peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
4184 GNUNET_free(peer_pos);
4189 /* free all messages on the queue */
4190 while (NULL != (mq = n->messages_head))
4192 GNUNET_STATISTICS_update (stats,
4193 gettext_noop ("# bytes in message queue for other peers"),
4194 - (int64_t) mq->message_buf_size,
4196 GNUNET_STATISTICS_update (stats,
4197 gettext_noop ("# bytes discarded due to disconnect"),
4198 mq->message_buf_size,
4200 GNUNET_CONTAINER_DLL_remove (n->messages_head,
4203 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4205 sizeof(struct GNUNET_PeerIdentity)));
4208 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
4210 GNUNET_SCHEDULER_cancel (sched, n->timeout_task);
4211 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4213 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4215 GNUNET_SCHEDULER_cancel (sched, n->retry_task);
4216 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4218 if (n->piter != NULL)
4220 GNUNET_PEERINFO_iterate_cancel (n->piter);
4223 /* finally, free n itself */
4224 GNUNET_STATISTICS_update (stats,
4225 gettext_noop ("# active neighbours"),
4228 GNUNET_free_non_null (n->pre_connect_message_buffer);
4234 * We have received a PING message from someone. Need to send a PONG message
4235 * in response to the peer by any means necessary.
4238 handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
4239 const struct GNUNET_PeerIdentity *peer,
4240 struct Session *session,
4241 const char *sender_address,
4242 uint16_t sender_address_len)
4244 struct TransportPlugin *plugin = cls;
4245 struct SessionHeader *session_header = (struct SessionHeader*) session;
4246 struct TransportPingMessage *ping;
4247 struct TransportPongMessage *pong;
4248 struct NeighbourList *n;
4249 struct ReadyList *rl;
4250 struct ForeignAddressList *fal;
4251 struct OwnAddressList *oal;
4256 if (ntohs (message->size) < sizeof (struct TransportPingMessage))
4258 GNUNET_break_op (0);
4259 return GNUNET_SYSERR;
4262 ping = (struct TransportPingMessage *) message;
4263 if (0 != memcmp (&ping->target,
4264 plugin->env.my_identity,
4265 sizeof (struct GNUNET_PeerIdentity)))
4267 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4268 _("Received `%s' message not destined for me!\n"),
4270 return GNUNET_SYSERR;
4273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4274 "Processing `%s' from `%s'\n",
4276 (sender_address != NULL)
4277 ? a2s (plugin->short_name,
4278 (const struct sockaddr *)sender_address,
4282 GNUNET_STATISTICS_update (stats,
4283 gettext_noop ("# PING messages received"),
4286 addr = (const char*) &ping[1];
4287 alen = ntohs (message->size) - sizeof (struct TransportPingMessage);
4288 slen = strlen (plugin->short_name) + 1;
4291 /* peer wants to confirm that we have an outbound connection to him */
4292 if (session == NULL)
4294 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4295 _("Refusing to create PONG since I do not have a session with `%s'.\n"),
4297 return GNUNET_SYSERR;
4299 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len + slen);
4300 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len + slen);
4301 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
4302 pong->purpose.size =
4303 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4305 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
4306 sizeof (struct GNUNET_PeerIdentity) + sender_address_len + slen);
4307 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING);
4308 pong->challenge = ping->challenge;
4309 pong->addrlen = htonl(sender_address_len + slen);
4312 sizeof(struct GNUNET_PeerIdentity));
4316 memcpy (&((char*)&pong[1])[slen],
4318 sender_address_len);
4319 if (GNUNET_TIME_absolute_get_remaining (session_header->pong_sig_expires).value < PONG_SIGNATURE_LIFETIME.value / 4)
4321 /* create / update cached sig */
4323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4324 "Creating PONG signature to indicate active connection.\n");
4326 session_header->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
4327 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
4328 GNUNET_assert (GNUNET_OK ==
4329 GNUNET_CRYPTO_rsa_sign (my_private_key,
4331 &session_header->pong_signature));
4335 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
4337 memcpy (&pong->signature,
4338 &session_header->pong_signature,
4339 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4345 /* peer wants to confirm that this is one of our addresses */
4349 plugin->api->check_address (plugin->api->cls,
4353 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4354 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
4355 a2s (plugin->short_name,
4360 oal = plugin->addresses;
4363 if ( (oal->addrlen == alen) &&
4370 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
4371 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
4372 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
4373 pong->purpose.size =
4374 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4376 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
4377 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
4378 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
4379 pong->challenge = ping->challenge;
4380 pong->addrlen = htonl(alen + slen);
4383 sizeof(struct GNUNET_PeerIdentity));
4384 memcpy (&pong[1], plugin->short_name, slen);
4385 memcpy (&((char*)&pong[1])[slen], addr, alen);
4386 if ( (oal != NULL) &&
4387 (GNUNET_TIME_absolute_get_remaining (oal->pong_sig_expires).value < PONG_SIGNATURE_LIFETIME.value / 4) )
4389 /* create / update cached sig */
4391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4392 "Creating PONG signature to indicate ownership.\n");
4394 oal->pong_sig_expires = GNUNET_TIME_absolute_min (oal->expires,
4395 GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
4396 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
4397 GNUNET_assert (GNUNET_OK ==
4398 GNUNET_CRYPTO_rsa_sign (my_private_key,
4400 &oal->pong_signature));
4401 memcpy (&pong->signature,
4402 &oal->pong_signature,
4403 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4405 else if (oal == NULL)
4407 /* not using cache (typically DV-only) */
4408 pong->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
4409 GNUNET_assert (GNUNET_OK ==
4410 GNUNET_CRYPTO_rsa_sign (my_private_key,
4416 /* can used cached version */
4417 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
4418 memcpy (&pong->signature,
4419 &oal->pong_signature,
4420 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4423 n = find_neighbour(peer);
4424 GNUNET_assert (n != NULL);
4425 /* first try reliable response transmission */
4429 fal = rl->addresses;
4432 if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
4435 ntohs (pong->header.size),
4436 TRANSPORT_PONG_PRIORITY,
4437 HELLO_VERIFICATION_TIMEOUT,
4445 GNUNET_STATISTICS_update (stats,
4446 gettext_noop ("# PONGs unicast via reliable transport"),
4456 /* no reliable method found, do multicast */
4457 GNUNET_STATISTICS_update (stats,
4458 gettext_noop ("# PONGs multicast to all available addresses"),
4464 fal = rl->addresses;
4467 transmit_to_peer(NULL, fal,
4468 TRANSPORT_PONG_PRIORITY,
4469 HELLO_VERIFICATION_TIMEOUT,
4471 ntohs(pong->header.size),
4484 * Function called by the plugin for each received message.
4485 * Update data volumes, possibly notify plugins about
4486 * reducing the rate at which they read from the socket
4487 * and generally forward to our receive callback.
4489 * @param cls the "struct TransportPlugin *" we gave to the plugin
4490 * @param peer (claimed) identity of the other peer
4491 * @param message the message, NULL if we only care about
4492 * learning about the delay until we should receive again
4493 * @param distance in overlay hops; use 1 unless DV (or 0 if message == NULL)
4494 * @param session identifier used for this session (can be NULL)
4495 * @param sender_address binary address of the sender (if observed)
4496 * @param sender_address_len number of bytes in sender_address
4497 * @return how long the plugin should wait until receiving more data
4498 * (plugins that do not support this, can ignore the return value)
4500 static struct GNUNET_TIME_Relative
4501 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
4502 const struct GNUNET_MessageHeader *message,
4504 struct Session *session,
4505 const char *sender_address,
4506 uint16_t sender_address_len)
4508 struct TransportPlugin *plugin = cls;
4509 struct ReadyList *service_context;
4510 struct ForeignAddressList *peer_address;
4512 struct NeighbourList *n;
4513 struct GNUNET_TIME_Relative ret;
4515 if (is_blacklisted (peer, plugin))
4516 return GNUNET_TIME_UNIT_FOREVER_REL;
4518 n = find_neighbour (peer);
4520 n = setup_new_neighbour (peer, GNUNET_YES);
4521 service_context = n->plugins;
4522 while ((service_context != NULL) && (plugin != service_context->plugin))
4523 service_context = service_context->next;
4524 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
4525 peer_address = NULL;
4526 if (message != NULL)
4528 if ( (session != NULL) ||
4529 (sender_address != NULL) )
4530 peer_address = add_peer_address (n,
4534 sender_address_len);
4535 if (peer_address != NULL)
4537 peer_address->distance = distance;
4538 if (GNUNET_YES == peer_address->validated)
4539 mark_address_connected (peer_address);
4540 peer_address->timeout
4542 GNUNET_TIME_relative_to_absolute
4543 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
4544 schedule_next_ping (peer_address);
4546 /* update traffic received amount ... */
4547 msize = ntohs (message->size);
4548 GNUNET_STATISTICS_update (stats,
4549 gettext_noop ("# bytes received from other peers"),
4552 n->distance = distance;
4554 GNUNET_TIME_relative_to_absolute
4555 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
4556 GNUNET_SCHEDULER_cancel (sched,
4559 GNUNET_SCHEDULER_add_delayed (sched,
4560 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
4561 &neighbour_timeout_task, n);
4562 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
4564 /* dropping message due to frequent inbound volume violations! */
4565 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
4566 GNUNET_ERROR_TYPE_BULK,
4568 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
4569 n->in_tracker.available_bytes_per_s__,
4570 n->quota_violation_count);
4571 GNUNET_STATISTICS_update (stats,
4572 gettext_noop ("# bandwidth quota violations by other peers"),
4575 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
4578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4579 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
4580 ntohs (message->type),
4581 ntohs (message->size),
4584 switch (ntohs (message->type))
4586 case GNUNET_MESSAGE_TYPE_HELLO:
4587 GNUNET_STATISTICS_update (stats,
4588 gettext_noop ("# HELLO messages received from other peers"),
4591 process_hello (plugin, message);
4593 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
4594 handle_ping (plugin, message, peer, session, sender_address, sender_address_len);
4596 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
4597 handle_pong (plugin, message, peer, sender_address, sender_address_len);
4600 handle_payload_message (message, n);
4604 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
4607 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4608 "Throttling read (%llu bytes excess at %u b/s), waiting %llums before reading more.\n",
4609 (unsigned long long) n->in_tracker.consumption_since_last_update__,
4610 (unsigned int) n->in_tracker.available_bytes_per_s__,
4611 (unsigned long long) ret.value);
4612 GNUNET_STATISTICS_update (stats,
4613 gettext_noop ("# ms throttling suggested"),
4614 (int64_t) ret.value,
4621 * Handle START-message. This is the first message sent to us
4622 * by any client which causes us to add it to our list.
4624 * @param cls closure (always NULL)
4625 * @param client identification of the client
4626 * @param message the actual message
4629 handle_start (void *cls,
4630 struct GNUNET_SERVER_Client *client,
4631 const struct GNUNET_MessageHeader *message)
4633 struct TransportClient *c;
4634 struct ConnectInfoMessage cim;
4635 struct NeighbourList *n;
4638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4639 "Received `%s' request from client\n", "START");
4644 if (c->client == client)
4646 /* client already on our list! */
4648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4653 c = GNUNET_malloc (sizeof (struct TransportClient));
4657 if (our_hello != NULL)
4660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4661 "Sending our own `%s' to new client\n", "HELLO");
4663 transmit_to_client (c,
4664 (const struct GNUNET_MessageHeader *) our_hello,
4666 /* tell new client about all existing connections */
4667 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
4668 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
4672 if (GNUNET_YES == n->received_pong)
4675 cim.latency = GNUNET_TIME_relative_hton (n->latency);
4676 cim.distance = htonl (n->distance);
4677 transmit_to_client (c, &cim.header, GNUNET_NO);
4682 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4687 * Handle HELLO-message.
4689 * @param cls closure (always NULL)
4690 * @param client identification of the client
4691 * @param message the actual message
4694 handle_hello (void *cls,
4695 struct GNUNET_SERVER_Client *client,
4696 const struct GNUNET_MessageHeader *message)
4700 GNUNET_STATISTICS_update (stats,
4701 gettext_noop ("# HELLOs received from clients"),
4704 ret = process_hello (NULL, message);
4705 GNUNET_SERVER_receive_done (client, ret);
4710 * Closure for 'transmit_client_message'; followed by
4711 * 'msize' bytes of the actual message.
4713 struct TransmitClientMessageContext
4716 * Client on whom's behalf we are sending.
4718 struct GNUNET_SERVER_Client *client;
4721 * Timeout for the transmission.
4723 struct GNUNET_TIME_Absolute timeout;
4731 * Size of the message in bytes.
4738 * Schedule transmission of a message we got from a client to a peer.
4740 * @param cls the 'struct TransmitClientMessageContext*'
4741 * @param n destination, or NULL on error (in that case, drop the message)
4744 transmit_client_message (void *cls,
4745 struct NeighbourList *n)
4747 struct TransmitClientMessageContext *tcmc = cls;
4748 struct TransportClient *tc;
4751 while ((tc != NULL) && (tc->client != tcmc->client))
4756 transmit_to_peer (tc, NULL, tcmc->priority,
4757 GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
4759 tcmc->msize, GNUNET_NO, n);
4761 GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
4762 GNUNET_SERVER_client_drop (tcmc->client);
4768 * Handle SEND-message.
4770 * @param cls closure (always NULL)
4771 * @param client identification of the client
4772 * @param message the actual message
4775 handle_send (void *cls,
4776 struct GNUNET_SERVER_Client *client,
4777 const struct GNUNET_MessageHeader *message)
4779 const struct OutboundMessage *obm;
4780 const struct GNUNET_MessageHeader *obmm;
4781 struct TransmitClientMessageContext *tcmc;
4785 size = ntohs (message->size);
4787 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
4790 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4793 GNUNET_STATISTICS_update (stats,
4794 gettext_noop ("# payload received for other peers"),
4797 obm = (const struct OutboundMessage *) message;
4798 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
4799 msize = size - sizeof (struct OutboundMessage);
4801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4802 "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
4803 "SEND", GNUNET_i2s (&obm->peer),
4807 tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
4808 tcmc->client = client;
4809 tcmc->priority = ntohl (obm->priority);
4810 tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
4811 tcmc->msize = msize;
4812 /* FIXME: this memcpy can be up to 7% of our total runtime */
4813 memcpy (&tcmc[1], obmm, msize);
4814 GNUNET_SERVER_client_keep (client);
4815 setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
4816 &transmit_client_message,
4822 * Handle SET_QUOTA-message.
4824 * @param cls closure (always NULL)
4825 * @param client identification of the client
4826 * @param message the actual message
4829 handle_set_quota (void *cls,
4830 struct GNUNET_SERVER_Client *client,
4831 const struct GNUNET_MessageHeader *message)
4833 const struct QuotaSetMessage *qsm =
4834 (const struct QuotaSetMessage *) message;
4835 struct NeighbourList *n;
4837 GNUNET_STATISTICS_update (stats,
4838 gettext_noop ("# SET QUOTA messages received"),
4841 n = find_neighbour (&qsm->peer);
4844 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4845 GNUNET_STATISTICS_update (stats,
4846 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
4852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4853 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
4855 (unsigned int) ntohl (qsm->quota.value__),
4856 (unsigned int) n->in_tracker.available_bytes_per_s__,
4857 GNUNET_i2s (&qsm->peer));
4859 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
4861 if (0 == ntohl (qsm->quota.value__))
4862 disconnect_neighbour (n, GNUNET_NO);
4863 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4868 * Take the given address and append it to the set of results sent back to
4871 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
4872 * @param address the resolved name, NULL to indicate the last response
4875 transmit_address_to_client (void *cls, const char *address)
4877 struct GNUNET_SERVER_TransmitContext *tc = cls;
4880 if (NULL == address)
4883 slen = strlen (address) + 1;
4885 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
4886 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
4887 if (NULL == address)
4888 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
4893 * Handle AddressLookup-message.
4895 * @param cls closure (always NULL)
4896 * @param client identification of the client
4897 * @param message the actual message
4900 handle_address_lookup (void *cls,
4901 struct GNUNET_SERVER_Client *client,
4902 const struct GNUNET_MessageHeader *message)
4904 const struct AddressLookupMessage *alum;
4905 struct TransportPlugin *lsPlugin;
4906 const char *nameTransport;
4907 const char *address;
4909 struct GNUNET_SERVER_TransmitContext *tc;
4910 struct GNUNET_TIME_Absolute timeout;
4911 struct GNUNET_TIME_Relative rtimeout;
4914 size = ntohs (message->size);
4915 if (size < sizeof (struct AddressLookupMessage))
4917 GNUNET_break_op (0);
4918 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4921 alum = (const struct AddressLookupMessage *) message;
4922 uint32_t addressLen = ntohl (alum->addrlen);
4923 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
4925 GNUNET_break_op (0);
4926 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4929 address = (const char *) &alum[1];
4930 nameTransport = (const char *) &address[addressLen];
4932 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
4934 GNUNET_break_op (0);
4935 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4938 timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
4939 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
4940 numeric = ntohl (alum->numeric_only);
4941 lsPlugin = find_transport (nameTransport);
4942 if (NULL == lsPlugin)
4944 tc = GNUNET_SERVER_transmit_context_create (client);
4945 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
4946 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
4947 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
4950 tc = GNUNET_SERVER_transmit_context_create (client);
4951 lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
4953 address, addressLen,
4956 &transmit_address_to_client, tc);
4961 * Setup the environment for this plugin.
4964 create_environment (struct TransportPlugin *plug)
4966 plug->env.cfg = cfg;
4967 plug->env.sched = sched;
4968 plug->env.my_identity = &my_identity;
4969 plug->env.cls = plug;
4970 plug->env.receive = &plugin_env_receive;
4971 plug->env.notify_address = &plugin_env_notify_address;
4972 plug->env.session_end = &plugin_env_session_end;
4973 plug->env.max_connections = max_connect_per_transport;
4974 plug->env.stats = stats;
4979 * Start the specified transport (load the plugin).
4982 start_transport (struct GNUNET_SERVER_Handle *server,
4985 struct TransportPlugin *plug;
4988 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4989 _("Loading `%s' transport plugin\n"), name);
4990 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
4991 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
4992 create_environment (plug);
4993 plug->short_name = GNUNET_strdup (name);
4994 plug->lib_name = libname;
4995 plug->next = plugins;
4997 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
4998 if (plug->api == NULL)
5000 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5001 _("Failed to load transport plugin for `%s'\n"), name);
5002 GNUNET_free (plug->short_name);
5003 plugins = plug->next;
5004 GNUNET_free (libname);
5011 * Called whenever a client is disconnected. Frees our
5012 * resources associated with that client.
5014 * @param cls closure
5015 * @param client identification of the client
5018 client_disconnect_notification (void *cls,
5019 struct GNUNET_SERVER_Client *client)
5021 struct TransportClient *pos;
5022 struct TransportClient *prev;
5023 struct ClientMessageQueueEntry *mqe;
5024 struct Blacklisters *bl;
5025 struct BlacklistCheck *bc;
5030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
5031 "Client disconnected, cleaning up.\n");
5033 /* clean up blacklister */
5037 if (bl->client == client)
5042 if (bc->bl_pos == bl)
5044 bc->bl_pos = bl->next;
5047 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
5050 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
5051 bc->task = GNUNET_SCHEDULER_add_now (sched,
5052 &do_blacklist_check,
5058 GNUNET_CONTAINER_DLL_remove (bl_head,
5061 GNUNET_SERVER_client_drop (bl->client);
5067 /* clean up 'normal' clients */
5070 while ((pos != NULL) && (pos->client != client))
5077 while (NULL != (mqe = pos->message_queue_head))
5079 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
5080 pos->message_queue_tail,
5082 pos->message_count--;
5086 clients = pos->next;
5088 prev->next = pos->next;
5089 if (GNUNET_YES == pos->tcs_pending)
5094 if (pos->th != NULL)
5096 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
5099 GNUNET_break (0 == pos->message_count);
5105 * Function called when the service shuts down. Unloads our plugins
5106 * and cancels pending validations.
5108 * @param cls closure, unused
5109 * @param tc task context (unused)
5112 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5114 struct TransportPlugin *plug;
5115 struct OwnAddressList *al;
5116 struct CheckHelloValidatedContext *chvc;
5118 while (neighbours != NULL)
5119 disconnect_neighbour (neighbours, GNUNET_NO);
5121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5122 "Transport service is unloading plugins...\n");
5124 while (NULL != (plug = plugins))
5126 plugins = plug->next;
5127 if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
5129 GNUNET_SCHEDULER_cancel (plug->env.sched,
5130 plug->address_update_task);
5131 plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
5133 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
5134 GNUNET_free (plug->lib_name);
5135 GNUNET_free (plug->short_name);
5136 while (NULL != (al = plug->addresses))
5138 plug->addresses = al->next;
5143 if (my_private_key != NULL)
5144 GNUNET_CRYPTO_rsa_key_free (my_private_key);
5145 GNUNET_free_non_null (our_hello);
5147 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
5150 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
5151 validation_map = NULL;
5153 /* free 'chvc' data structure */
5154 while (NULL != (chvc = chvc_head))
5156 chvc_head = chvc->next;
5157 if (chvc->piter != NULL)
5158 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
5161 GNUNET_assert (chvc->ve_count == 0);
5168 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
5171 if (peerinfo != NULL)
5173 GNUNET_PEERINFO_disconnect (peerinfo);
5176 /* Can we assume those are gone by now, or do we need to clean up
5178 GNUNET_break (bl_head == NULL);
5179 GNUNET_break (bc_head == NULL);
5184 * Initiate transport service.
5186 * @param cls closure
5187 * @param s scheduler to use
5188 * @param server the initialized server
5189 * @param c configuration to use
5193 struct GNUNET_SCHEDULER_Handle *s,
5194 struct GNUNET_SERVER_Handle *server,
5195 const struct GNUNET_CONFIGURATION_Handle *c)
5197 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
5198 {&handle_start, NULL,
5199 GNUNET_MESSAGE_TYPE_TRANSPORT_START, 0},
5200 {&handle_hello, NULL,
5201 GNUNET_MESSAGE_TYPE_HELLO, 0},
5202 {&handle_send, NULL,
5203 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
5204 {&handle_set_quota, NULL,
5205 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
5206 {&handle_address_lookup, NULL,
5207 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
5209 {&handle_blacklist_init, NULL,
5210 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
5211 {&handle_blacklist_reply, NULL,
5212 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
5218 unsigned long long tneigh;
5223 stats = GNUNET_STATISTICS_create (sched, "transport", cfg);
5224 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
5225 /* parse configuration */
5227 GNUNET_CONFIGURATION_get_value_number (c,
5232 GNUNET_CONFIGURATION_get_value_filename (c,
5234 "HOSTKEY", &keyfile)))
5236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5238 ("Transport service is lacking key configuration settings. Exiting.\n"));
5239 GNUNET_SCHEDULER_shutdown (s);
5242 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
5245 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
5246 validation_map = NULL;
5249 max_connect_per_transport = (uint32_t) tneigh;
5250 peerinfo = GNUNET_PEERINFO_connect (sched, cfg);
5251 if (peerinfo == NULL)
5253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5254 _("Could not access PEERINFO service. Exiting.\n"));
5255 GNUNET_SCHEDULER_shutdown (s);
5258 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
5261 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
5262 validation_map = NULL;
5263 GNUNET_free (keyfile);
5266 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
5267 GNUNET_free (keyfile);
5268 if (my_private_key == NULL)
5270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5272 ("Transport service could not access hostkey. Exiting.\n"));
5273 GNUNET_SCHEDULER_shutdown (s);
5276 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
5279 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
5280 validation_map = NULL;
5283 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
5284 GNUNET_CRYPTO_hash (&my_public_key,
5285 sizeof (my_public_key), &my_identity.hashPubKey);
5286 /* setup notification */
5287 GNUNET_SERVER_disconnect_notify (server,
5288 &client_disconnect_notification, NULL);
5289 /* load plugins... */
5292 GNUNET_CONFIGURATION_get_value_string (c,
5293 "TRANSPORT", "PLUGINS", &plugs))
5295 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5296 _("Starting transport plugins `%s'\n"), plugs);
5297 pos = strtok (plugs, " ");
5300 start_transport (server, pos);
5302 pos = strtok (NULL, " ");
5304 GNUNET_free (plugs);
5306 GNUNET_SCHEDULER_add_delayed (sched,
5307 GNUNET_TIME_UNIT_FOREVER_REL,
5308 &shutdown_task, NULL);
5313 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport service ready.\n"));
5315 /* If we have a blacklist file, read from it */
5316 read_blacklist_file(cfg);
5317 /* process client requests */
5318 GNUNET_SERVER_add_handlers (server, handlers);
5323 * The main function for the transport service.
5325 * @param argc number of arguments from the command line
5326 * @param argv command line arguments
5327 * @return 0 ok, 1 on error
5330 main (int argc, char *const *argv)
5332 a2s (NULL, NULL, 0); /* make compiler happy */
5333 return (GNUNET_OK ==
5334 GNUNET_SERVICE_run (argc,
5337 GNUNET_SERVICE_OPTION_NONE,
5338 &run, NULL)) ? 0 : 1;
5341 /* end of gnunet-service-transport.c */