2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/gnunet-service-transport.c
23 * @brief low-level P2P messaging
24 * @author Christian Grothoff
28 #include "gnunet_client_lib.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "gnunet_plugin_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_service_lib.h"
38 #include "gnunet_signatures.h"
39 #include "gnunet_transport_plugin.h"
40 #include "transport.h"
41 #include "transport_ats.h"
44 #define DEBUG_BLACKLIST GNUNET_NO
46 #define DEBUG_PING_PONG GNUNET_NO
48 #define DEBUG_TRANSPORT_HELLO GNUNET_NO
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)
151 * List of addresses of other peers
153 struct ForeignAddressList
156 * This is a linked list.
158 struct ForeignAddressList *next;
161 * Which ready list does this entry belong to.
163 struct ReadyList *ready_list;
166 * How long until we auto-expire this address (unless it is
167 * re-confirmed by the transport)?
169 struct GNUNET_TIME_Absolute expires;
172 * Task used to re-validate addresses, updates latencies and
175 GNUNET_SCHEDULER_TaskIdentifier revalidate_task;
183 * Session (or NULL if no valid session currently exists or if the
184 * plugin does not use sessions).
186 struct Session *session;
188 struct ATS_ressource_entry * ressources;
190 struct ATS_quality_entry * quality;
193 * What was the last latency observed for this address, plugin and peer?
195 struct GNUNET_TIME_Relative latency;
198 * If we did not successfully transmit a message to the given peer
199 * via this connection during the specified time, we should consider
200 * the connection to be dead. This is used in the case that a TCP
201 * transport simply stalls writing to the stream but does not
202 * formerly get a signal that the other peer died.
204 struct GNUNET_TIME_Absolute timeout;
207 * How often have we tried to connect using this plugin? Used to
208 * discriminate against addresses that do not work well.
209 * FIXME: not yet used, but should be!
211 unsigned int connect_attempts;
214 * DV distance to this peer (1 if no DV is used).
215 * FIXME: need to set this from transport plugins!
225 * Have we ever estimated the latency of this address? Used to
226 * ensure that the first time we add an address, we immediately
232 * Are we currently connected via this address? The first time we
233 * successfully transmit or receive data to a peer via a particular
234 * address, we set this to GNUNET_YES. If we later get an error
235 * (disconnect notification, transmission failure, timeout), we set
236 * it back to GNUNET_NO.
241 * Is this plugin currently busy transmitting to the specific target?
242 * GNUNET_NO if not (initial, default state is GNUNET_NO). Internal
243 * messages do not count as 'in transmit'.
248 * Has this address been validated yet?
256 * Entry in linked list of network addresses for ourselves. Also
257 * includes a cached signature for 'struct TransportPongMessage's.
259 struct OwnAddressList
262 * This is a linked list.
264 struct OwnAddressList *next;
267 * How long until the current signature expires? (ZERO if the
268 * signature was never created).
270 struct GNUNET_TIME_Absolute pong_sig_expires;
273 * Signature for a 'struct TransportPongMessage' for this address.
275 struct GNUNET_CRYPTO_RsaSignature pong_signature;
286 * Entry in linked list of all of our plugins.
288 struct TransportPlugin
291 * This is a linked list.
293 struct TransportPlugin *next;
296 * API of the transport as returned by the plugin's
297 * initialization function.
299 struct GNUNET_TRANSPORT_PluginFunctions *api;
302 * Short name for the plugin (i.e. "tcp").
307 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
312 * List of our known addresses for this transport.
314 struct OwnAddressList *addresses;
317 * Environment this transport service is using
320 struct GNUNET_TRANSPORT_PluginEnvironment env;
323 * ID of task that is used to clean up expired addresses.
325 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
328 * Set to GNUNET_YES if we need to scrap the existing list of
329 * "addresses" and start fresh when we receive the next address
330 * update from a transport. Set to GNUNET_NO if we should just add
331 * the new address to the list and wait for the commit call.
335 struct ATS_plugin * rc;
338 * Hashmap of blacklisted peers for this particular transport.
340 struct GNUNET_CONTAINER_MultiHashMap *blacklist;
343 struct NeighbourList;
346 * For each neighbour we keep a list of messages
347 * that we still want to transmit to the neighbour.
353 * This is a doubly linked list.
355 struct MessageQueue *next;
358 * This is a doubly linked list.
360 struct MessageQueue *prev;
363 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
364 * stuck together in memory. Allocated at the end of this struct.
366 const char *message_buf;
369 * Size of the message buf
371 size_t message_buf_size;
374 * Client responsible for queueing the message;
375 * used to check that a client has no two messages
376 * pending for the same target. Can be NULL.
378 struct TransportClient *client;
381 * Using which specific address should we send this message?
383 struct ForeignAddressList *specific_address;
386 * Peer ID of the Neighbour this entry belongs to.
388 struct GNUNET_PeerIdentity neighbour_id;
391 * Plugin that we used for the transmission.
392 * NULL until we scheduled a transmission.
394 struct TransportPlugin *plugin;
397 * At what time should we fail?
399 struct GNUNET_TIME_Absolute timeout;
402 * Internal message of the transport system that should not be
403 * included in the usual SEND-SEND_OK transmission confirmation
404 * traffic management scheme. Typically, "internal_msg" will
405 * be set whenever "client" is NULL (but it is not strictly
411 * How important is the message?
413 unsigned int priority;
419 * For a given Neighbour, which plugins are available
420 * to talk to this peer and what are their costs?
425 * This is a linked list.
427 struct ReadyList *next;
430 * Which of our transport plugins does this entry
433 struct TransportPlugin *plugin;
436 * Transport addresses, latency, and readiness for
437 * this particular plugin.
439 struct ForeignAddressList *addresses;
442 * To which neighbour does this ready list belong to?
444 struct NeighbourList *neighbour;
449 * Entry in linked list of all of our current neighbours.
455 * This is a linked list.
457 struct NeighbourList *next;
460 * Which of our transports is connected to this peer
461 * and what is their status?
463 struct ReadyList *plugins;
466 * Head of list of messages we would like to send to this peer;
467 * must contain at most one message per client.
469 struct MessageQueue *messages_head;
472 * Tail of list of messages we would like to send to this peer; must
473 * contain at most one message per client.
475 struct MessageQueue *messages_tail;
478 * Head of list of messages of messages we expected the continuation
479 * to be called to destroy the message
481 struct MessageQueue *cont_head;
484 * Tail of list of messages of messages we expected the continuation
485 * to be called to destroy the message
487 struct MessageQueue *cont_tail;
490 * Buffer for at most one payload message used when we receive
491 * payload data before our PING-PONG has succeeded. We then
492 * store such messages in this intermediary buffer until the
493 * connection is fully up.
495 struct GNUNET_MessageHeader *pre_connect_message_buffer;
498 * Context for peerinfo iteration.
499 * NULL after we are done processing peerinfo's information.
501 struct GNUNET_PEERINFO_IteratorContext *piter;
504 * Public key for this peer. Valid only if the respective flag is set below.
506 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
509 * Identity of this neighbour.
511 struct GNUNET_PeerIdentity id;
514 * ID of task scheduled to run when this peer is about to
515 * time out (will free resources associated with the peer).
517 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
520 * ID of task scheduled to run when we should retry transmitting
521 * the head of the message queue. Actually triggered when the
522 * transmission is timing out (we trigger instantly when we have
523 * a chance of success).
525 GNUNET_SCHEDULER_TaskIdentifier retry_task;
528 * How long until we should consider this peer dead
529 * (if we don't receive another message in the
532 struct GNUNET_TIME_Absolute peer_timeout;
535 * Tracker for inbound bandwidth.
537 struct GNUNET_BANDWIDTH_Tracker in_tracker;
540 * The latency we have seen for this particular address for
541 * this particular peer. This latency may have been calculated
542 * over multiple transports. This value reflects how long it took
543 * us to receive a response when SENDING via this particular
544 * transport/neighbour/address combination!
546 * FIXME: we need to periodically send PINGs to update this
547 * latency (at least more often than the current "huge" (11h?)
550 struct GNUNET_TIME_Relative latency;
553 * How often has the other peer (recently) violated the
554 * inbound traffic limit? Incremented by 10 per violation,
555 * decremented by 1 per non-violation (for each
558 unsigned int quota_violation_count;
561 * DV distance to this peer (1 if no DV is used).
566 * Have we seen an PONG from this neighbour in the past (and
567 * not had a disconnect since)?
572 * Do we have a valid public key for this neighbour?
574 int public_key_valid;
577 * Performance data for the peer.
579 struct GNUNET_TRANSPORT_ATS_Information *ats;
583 * Message used to ask a peer to validate receipt (to check an address
584 * from a HELLO). Followed by the address we are trying to validate,
585 * or an empty address if we are just sending a PING to confirm that a
586 * connection which the receiver (of the PING) initiated is still valid.
588 struct TransportPingMessage
592 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
594 struct GNUNET_MessageHeader header;
597 * Challenge code (to ensure fresh reply).
599 uint32_t challenge GNUNET_PACKED;
602 * Who is the intended recipient?
604 struct GNUNET_PeerIdentity target;
610 * Message used to validate a HELLO. The challenge is included in the
611 * confirmation to make matching of replies to requests possible. The
612 * signature signs our public key, an expiration time and our address.<p>
614 * This message is followed by our transport address that the PING tried
615 * to confirm (if we liked it). The address can be empty (zero bytes)
616 * if the PING had not address either (and we received the request via
617 * a connection that we initiated).
619 struct TransportPongMessage
623 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
625 struct GNUNET_MessageHeader header;
628 * Challenge code from PING (showing freshness). Not part of what
629 * is signed so that we can re-use signatures.
631 uint32_t challenge GNUNET_PACKED;
636 struct GNUNET_CRYPTO_RsaSignature signature;
639 * What are we signing and why? Two possible reason codes can be here:
640 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
641 * plausible address for this peer (pid is set to identity of signer); or
642 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING to confirm that this is
643 * an address we used to connect to the peer with the given pid.
645 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
648 * When does this signature expire?
650 struct GNUNET_TIME_AbsoluteNBO expiration;
653 * Either the identity of the peer Who signed this message, or the
654 * identity of the peer that we're connected to using the given
655 * address (depending on purpose.type).
657 struct GNUNET_PeerIdentity pid;
660 * Size of address appended to this message (part of what is
661 * being signed, hence not redundant).
669 * Linked list of messages to be transmitted to the client. Each
670 * entry is followed by the actual message.
672 struct ClientMessageQueueEntry
675 * This is a doubly-linked list.
677 struct ClientMessageQueueEntry *next;
680 * This is a doubly-linked list.
682 struct ClientMessageQueueEntry *prev;
687 * Client connected to the transport service.
689 struct TransportClient
693 * This is a linked list.
695 struct TransportClient *next;
698 * Handle to the client.
700 struct GNUNET_SERVER_Client *client;
703 * Linked list of messages yet to be transmitted to
706 struct ClientMessageQueueEntry *message_queue_head;
709 * Tail of linked list of messages yet to be transmitted to the
712 struct ClientMessageQueueEntry *message_queue_tail;
715 * Current transmit request handle.
717 struct GNUNET_CONNECTION_TransmitHandle *th;
720 * Is a call to "transmit_send_continuation" pending? If so, we
721 * must not free this struct (even if the corresponding client
722 * disconnects) and instead only remove it from the linked list and
723 * set the "client" field to NULL.
728 * Length of the list of messages pending for this client.
730 unsigned int message_count;
736 * Context of currently active requests to peerinfo
737 * for validation of HELLOs.
739 struct CheckHelloValidatedContext;
743 * Entry in map of all HELLOs awaiting validation.
745 struct ValidationEntry
749 * NULL if this entry is not part of a larger HELLO validation.
751 struct CheckHelloValidatedContext *chvc;
754 * The address, actually a pointer to the end
755 * of this struct. Do not free!
760 * Name of the transport.
762 char *transport_name;
765 * The public key of the peer.
767 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
770 * ID of task that will clean up this entry if we don't succeed
771 * with the validation first.
773 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
776 * At what time did we send this validation?
778 struct GNUNET_TIME_Absolute send_time;
781 * Session being validated (or NULL for none).
783 struct Session *session;
786 * Challenge number we used.
799 * Context of currently active requests to peerinfo
800 * for validation of HELLOs.
802 struct CheckHelloValidatedContext
806 * This is a doubly-linked list.
808 struct CheckHelloValidatedContext *next;
811 * This is a doubly-linked list.
813 struct CheckHelloValidatedContext *prev;
816 * Hello that we are validating.
818 const struct GNUNET_HELLO_Message *hello;
821 * Context for peerinfo iteration.
822 * NULL after we are done processing peerinfo's information.
824 struct GNUNET_PEERINFO_IteratorContext *piter;
827 * Was a HELLO known for this peer to peerinfo?
832 * Number of validation entries currently referring to this
835 unsigned int ve_count;
842 static struct GNUNET_HELLO_Message *our_hello;
847 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
852 static struct GNUNET_PeerIdentity my_identity;
857 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
862 const struct GNUNET_CONFIGURATION_Handle *cfg;
865 * Linked list of all clients to this service.
867 static struct TransportClient *clients;
870 * All loaded plugins.
872 static struct TransportPlugin *plugins;
875 * Handle to peerinfo service.
877 static struct GNUNET_PEERINFO_Handle *peerinfo;
880 * All known neighbours and their HELLOs.
882 static struct NeighbourList *neighbours;
885 * Number of neighbours we'd like to have.
887 static uint32_t max_connect_per_transport;
890 * Head of linked list.
892 static struct CheckHelloValidatedContext *chvc_head;
895 * Tail of linked list.
897 static struct CheckHelloValidatedContext *chvc_tail;
900 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
901 * of the given peer that we are currently validating).
903 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
906 * Handle for reporting statistics.
908 static struct GNUNET_STATISTICS_Handle *stats;
911 * Identifier of 'refresh_hello' task.
913 static GNUNET_SCHEDULER_TaskIdentifier hello_task;
916 * Identifier of ats scheduler task.
918 static GNUNET_SCHEDULER_TaskIdentifier ats_task;
921 * Is transport service shutting down ?
923 static int shutdown_in_progress;
926 * Handle for ats information
928 static struct ATS_Handle *ats;
931 * Time of last ats execution
933 struct GNUNET_TIME_Absolute last_ats_execution;
935 * Minimum interval between two ATS executions
937 struct GNUNET_TIME_Relative ats_minimum_interval;
939 * Regular interval when ATS execution is triggered
941 struct GNUNET_TIME_Relative ats_regular_interval;
944 * The peer specified by the given neighbour has timed-out or a plugin
945 * has disconnected. We may either need to do nothing (other plugins
946 * still up), or trigger a full disconnect and clean up. This
947 * function updates our state and do the necessary notifications.
948 * Also notifies our clients that the neighbour is now officially
951 * @param n the neighbour list entry for the peer
952 * @param check should we just check if all plugins
953 * disconnected or must we ask all plugins to
956 static void disconnect_neighbour (struct NeighbourList *n, int check);
959 * Check the ready list for the given neighbour and if a plugin is
960 * ready for transmission (and if we have a message), do so!
962 * @param nexi target peer for which to transmit
964 static void try_transmission_to_peer (struct NeighbourList *n);
966 struct ForeignAddressList * get_preferred_ats_address (
967 struct NeighbourList *n);
970 * Find an entry in the neighbour list for a particular peer.
972 * @return NULL if not found.
974 static struct NeighbourList *
975 find_neighbour (const struct GNUNET_PeerIdentity *key)
977 struct NeighbourList *head = neighbours;
979 while ((head != NULL) &&
980 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
985 static int update_addr_value (struct ForeignAddressList *fal, uint32_t value , int ats_index)
989 for (c=0; c<available_quality_metrics; c++)
991 if (ats_index == qm[c].atis_index)
993 fal->quality[c].values[0] = fal->quality[c].values[1];
994 fal->quality[c].values[1] = fal->quality[c].values[2];
995 fal->quality[c].values[2] = value;
997 ats_modify_problem_state (ats, ATS_QUALITY_UPDATED);
1000 if (set == GNUNET_NO)
1002 for (c=0; c<available_ressources; c++)
1004 if (ats_index == ressources[c].atis_index)
1006 fal->ressources[c].c = value;
1008 ats_modify_problem_state (ats, ATS_COST_UPDATED);
1016 update_addr_ats (struct ForeignAddressList *fal,
1017 const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
1022 for (c1=0; c1<ats_count; c1++)
1024 set = update_addr_value(fal, ntohl(ats_data[c1].value), ntohl(ats_data[c1].type));
1030 * Find an entry in the transport list for a particular transport.
1032 * @return NULL if not found.
1034 static struct TransportPlugin *
1035 find_transport (const char *short_name)
1037 struct TransportPlugin *head = plugins;
1038 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
1044 * Is a particular peer blacklisted for a particular transport?
1046 * @param peer the peer to check for
1047 * @param plugin the plugin used to connect to the peer
1049 * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
1052 is_blacklisted (const struct GNUNET_PeerIdentity *peer, struct TransportPlugin *plugin)
1055 if (plugin->blacklist != NULL)
1057 if (GNUNET_CONTAINER_multihashmap_contains (plugin->blacklist, &peer->hashPubKey) == GNUNET_YES)
1060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1061 "Peer `%s:%s' is blacklisted!\n",
1062 plugin->short_name, GNUNET_i2s (peer));
1065 GNUNET_STATISTICS_update (stats, "# blacklisted peers refused", 1, GNUNET_NO);
1075 add_peer_to_blacklist (struct GNUNET_PeerIdentity *peer,
1076 char *transport_name)
1078 struct TransportPlugin *plugin;
1080 plugin = find_transport(transport_name);
1081 if (plugin == NULL) /* Nothing to do */
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1085 "Adding peer `%s' with plugin `%s' to blacklist\n",
1089 if (plugin->blacklist == NULL)
1090 plugin->blacklist = GNUNET_CONTAINER_multihashmap_create(TRANSPORT_BLACKLIST_HT_SIZE);
1091 GNUNET_assert(plugin->blacklist != NULL);
1092 GNUNET_CONTAINER_multihashmap_put(plugin->blacklist, &peer->hashPubKey,
1094 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1099 * Read the blacklist file, containing transport:peer entries.
1100 * Provided the transport is loaded, set up hashmap with these
1101 * entries to blacklist peers by transport.
1105 read_blacklist_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1112 struct GNUNET_PeerIdentity pid;
1114 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1115 unsigned int entries_found;
1116 char *transport_name;
1119 GNUNET_CONFIGURATION_get_value_filename (cfg,
1125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1126 "Option `%s' in section `%s' not specified!\n",
1132 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1133 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1134 | GNUNET_DISK_PERM_USER_WRITE);
1135 if (0 != STAT (fn, &frstat))
1137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1138 _("Could not read blacklist file `%s'\n"), fn);
1142 if (frstat.st_size == 0)
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 _("Blacklist file `%s' is empty.\n"),
1152 /* FIXME: use mmap */
1153 data = GNUNET_malloc_large (frstat.st_size);
1154 GNUNET_assert(data != NULL);
1155 if (frstat.st_size !=
1156 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1159 _("Failed to read blacklist from `%s'\n"), fn);
1166 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1168 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1169 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1172 while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') && !isspace ( (unsigned char) data[colon_pos]))
1175 if (colon_pos >= frstat.st_size)
1177 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1178 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1179 (unsigned long long) colon_pos);
1185 if (isspace( (unsigned char) data[colon_pos]))
1187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1188 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1189 (unsigned long long) colon_pos);
1191 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1195 tsize = colon_pos - pos;
1196 if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size) || (tsize == 0))
1198 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1199 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1200 (unsigned long long) colon_pos);
1209 transport_name = GNUNET_malloc(tsize + 1);
1210 memcpy(transport_name, &data[pos], tsize);
1211 pos = colon_pos + 1;
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1214 "Read transport name %s in blacklist file.\n",
1217 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1218 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1220 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1221 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1222 (unsigned long long) pos);
1224 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1226 GNUNET_free_non_null(transport_name);
1229 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1230 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1232 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1233 _("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
1234 (unsigned long long) pos,
1239 if (0 != memcmp (&pid,
1241 sizeof (struct GNUNET_PeerIdentity)))
1244 add_peer_to_blacklist (&pid,
1249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1250 _("Found myself `%s' in blacklist (useless, ignored)\n"),
1254 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1255 GNUNET_free_non_null(transport_name);
1256 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1259 GNUNET_STATISTICS_update (stats, "# Transport entries blacklisted", entries_found, GNUNET_NO);
1266 * Function called to notify a client about the socket being ready to
1267 * queue more data. "buf" will be NULL and "size" zero if the socket
1268 * was closed for writing in the meantime.
1270 * @param cls closure
1271 * @param size number of bytes available in buf
1272 * @param buf where the callee should write the message
1273 * @return number of bytes written to buf
1276 transmit_to_client_callback (void *cls, size_t size, void *buf)
1278 struct TransportClient *client = cls;
1279 struct ClientMessageQueueEntry *q;
1282 const struct GNUNET_MessageHeader *msg;
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "Transmission to client failed, closing connection.\n");
1292 /* fatal error with client, free message queue! */
1293 while (NULL != (q = client->message_queue_head))
1295 GNUNET_STATISTICS_update (stats,
1296 gettext_noop ("# bytes discarded (could not transmit to client)"),
1297 ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
1299 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1300 client->message_queue_tail,
1304 client->message_count = 0;
1309 while (NULL != (q = client->message_queue_head))
1311 msg = (const struct GNUNET_MessageHeader *) &q[1];
1312 msize = ntohs (msg->size);
1313 if (msize + tsize > size)
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1317 "Transmitting message of type %u to client.\n",
1320 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1321 client->message_queue_tail,
1323 memcpy (&cbuf[tsize], msg, msize);
1326 client->message_count--;
1330 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1331 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1333 GNUNET_TIME_UNIT_FOREVER_REL,
1334 &transmit_to_client_callback,
1336 GNUNET_assert (client->th != NULL);
1343 * Convert an address to a string.
1345 * @param plugin name of the plugin responsible for the address
1346 * @param addr binary address
1347 * @param addr_len number of bytes in addr
1348 * @return NULL on error, otherwise address string
1351 a2s (const char *plugin,
1355 struct TransportPlugin *p;
1359 p = find_transport (plugin);
1360 if ((p == NULL) || (addr_len == 0) || (addr == NULL))
1363 return p->api->address_to_string (NULL,
1373 * Iterator to free entries in the validation_map.
1375 * @param cls closure (unused)
1376 * @param key current key code
1377 * @param value value in the hash map (validation to abort)
1378 * @return GNUNET_YES (always)
1381 abort_validation (void *cls,
1382 const GNUNET_HashCode * key,
1385 struct ValidationEntry *va = value;
1387 if (GNUNET_SCHEDULER_NO_TASK != va->timeout_task)
1388 GNUNET_SCHEDULER_cancel (va->timeout_task);
1389 GNUNET_free (va->transport_name);
1390 if (va->chvc != NULL)
1392 va->chvc->ve_count--;
1393 if (va->chvc->ve_count == 0)
1395 GNUNET_CONTAINER_DLL_remove (chvc_head,
1398 GNUNET_free (va->chvc);
1408 * HELLO validation cleanup task (validation failed).
1410 * @param cls the 'struct ValidationEntry' that failed
1411 * @param tc scheduler context (unused)
1414 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1416 struct ValidationEntry *va = cls;
1417 struct GNUNET_PeerIdentity pid;
1419 va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1420 GNUNET_STATISTICS_update (stats,
1421 gettext_noop ("# address validation timeouts"),
1424 GNUNET_CRYPTO_hash (&va->publicKey,
1426 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1428 GNUNET_break (GNUNET_OK ==
1429 GNUNET_CONTAINER_multihashmap_remove (validation_map,
1432 abort_validation (NULL, NULL, va);
1438 * Send the specified message to the specified client. Since multiple
1439 * messages may be pending for the same client at a time, this code
1440 * makes sure that no message is lost.
1442 * @param client client to transmit the message to
1443 * @param msg the message to send
1444 * @param may_drop can this message be dropped if the
1445 * message queue for this client is getting far too large?
1448 transmit_to_client (struct TransportClient *client,
1449 const struct GNUNET_MessageHeader *msg, int may_drop)
1451 struct ClientMessageQueueEntry *q;
1454 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1456 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1458 ("Dropping message of type %u and size %u, have %u messages pending (%u is the soft limit)\n"),
1461 client->message_count,
1463 GNUNET_STATISTICS_update (stats,
1464 gettext_noop ("# messages dropped due to slow client"),
1469 msize = ntohs (msg->size);
1470 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1471 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1472 memcpy (&q[1], msg, msize);
1473 GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1474 client->message_queue_tail,
1475 client->message_queue_tail,
1477 client->message_count++;
1478 if (client->th == NULL)
1480 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1482 GNUNET_TIME_UNIT_FOREVER_REL,
1483 &transmit_to_client_callback,
1485 GNUNET_assert (client->th != NULL);
1491 * Transmit a 'SEND_OK' notification to the given client for the
1494 * @param client who to notify
1495 * @param n neighbour to notify about, can be NULL (on failure)
1496 * @param target target of the transmission
1497 * @param result status code for the transmission request
1500 transmit_send_ok (struct TransportClient *client,
1501 struct NeighbourList *n,
1502 const struct GNUNET_PeerIdentity *target,
1505 struct SendOkMessage send_ok_msg;
1507 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1508 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1509 send_ok_msg.success = htonl (result);
1511 send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1513 send_ok_msg.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
1514 send_ok_msg.peer = *target;
1515 transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1520 * Mark the given FAL entry as 'connected' (and hence preferred for
1521 * sending); also mark all others for the same peer as 'not connected'
1522 * (since only one can be preferred).
1524 * @param fal address to set to 'connected'
1527 mark_address_connected (struct ForeignAddressList *fal);
1532 * We should re-try transmitting to the given peer,
1533 * hopefully we've learned something in the meantime.
1536 retry_transmission_task (void *cls,
1537 const struct GNUNET_SCHEDULER_TaskContext *tc)
1539 struct NeighbourList *n = cls;
1541 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1542 try_transmission_to_peer (n);
1547 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1548 * upon "completion" of a send request. This tells the API
1549 * that it is now legal to send another message to the given
1552 * @param cls closure, identifies the entry on the
1553 * message queue that was transmitted and the
1554 * client responsible for queuing the message
1555 * @param target the peer receiving the message
1556 * @param result GNUNET_OK on success, if the transmission
1557 * failed, we should not tell the client to transmit
1561 transmit_send_continuation (void *cls,
1562 const struct GNUNET_PeerIdentity *target,
1565 struct MessageQueue *mq = cls;
1566 struct NeighbourList *n;
1568 GNUNET_STATISTICS_update (stats,
1569 gettext_noop ("# bytes pending with plugins"),
1570 - (int64_t) mq->message_buf_size,
1572 if (result == GNUNET_OK)
1574 GNUNET_STATISTICS_update (stats,
1575 gettext_noop ("# bytes successfully transmitted by plugins"),
1576 mq->message_buf_size,
1581 GNUNET_STATISTICS_update (stats,
1582 gettext_noop ("# bytes with transmission failure by plugins"),
1583 mq->message_buf_size,
1586 if (mq->specific_address != NULL)
1588 if (result == GNUNET_OK)
1590 mq->specific_address->timeout =
1591 GNUNET_TIME_relative_to_absolute
1592 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1593 if (mq->specific_address->validated == GNUNET_YES)
1594 mark_address_connected (mq->specific_address);
1598 if (mq->specific_address->connected != GNUNET_NO)
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1602 "Marking address `%s' as no longer connected (due to transmission problem)\n",
1603 a2s (mq->specific_address->ready_list->plugin->short_name,
1604 mq->specific_address->addr,
1605 mq->specific_address->addrlen));
1607 GNUNET_STATISTICS_update (stats,
1608 gettext_noop ("# connected addresses"),
1611 mq->specific_address->connected = GNUNET_NO;
1614 if (! mq->internal_msg)
1615 mq->specific_address->in_transmit = GNUNET_NO;
1617 n = find_neighbour (&mq->neighbour_id);
1618 if (mq->client != NULL)
1619 transmit_send_ok (mq->client, n, target, result);
1622 GNUNET_CONTAINER_DLL_remove (n->cont_head,
1629 if (result == GNUNET_OK)
1630 try_transmission_to_peer (n);
1631 else if (GNUNET_SCHEDULER_NO_TASK == n->retry_task)
1632 n->retry_task = GNUNET_SCHEDULER_add_now (&retry_transmission_task,
1639 * Check the ready list for the given neighbour and if a plugin is
1640 * ready for transmission (and if we have a message), do so!
1642 * @param neighbour target peer for which to transmit
1645 try_transmission_to_peer (struct NeighbourList *n)
1647 struct ReadyList *rl;
1648 struct MessageQueue *mq;
1649 struct GNUNET_TIME_Relative timeout;
1653 if (n->messages_head == NULL)
1656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1657 "Transmission queue for `%4s' is empty\n",
1658 GNUNET_i2s (&n->id));
1660 return; /* nothing to do */
1663 mq = n->messages_head;
1664 force_address = GNUNET_YES;
1665 if (mq->specific_address == NULL)
1668 mq->specific_address = get_preferred_ats_address(n);
1669 GNUNET_STATISTICS_update (stats,
1670 gettext_noop ("# transport selected peer address freely"),
1673 force_address = GNUNET_NO;
1675 if (mq->specific_address == NULL)
1677 GNUNET_STATISTICS_update (stats,
1678 gettext_noop ("# transport failed to selected peer address"),
1681 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1682 if (timeout.rel_value == 0)
1685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1686 "No destination address available to transmit message of size %u to peer `%4s'\n",
1687 mq->message_buf_size,
1688 GNUNET_i2s (&mq->neighbour_id));
1690 GNUNET_STATISTICS_update (stats,
1691 gettext_noop ("# bytes in message queue for other peers"),
1692 - (int64_t) mq->message_buf_size,
1694 GNUNET_STATISTICS_update (stats,
1695 gettext_noop ("# bytes discarded (no destination address available)"),
1696 mq->message_buf_size,
1698 if (mq->client != NULL)
1699 transmit_send_ok (mq->client, n, &n->id, GNUNET_NO);
1700 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1704 return; /* nobody ready */
1706 GNUNET_STATISTICS_update (stats,
1707 gettext_noop ("# message delivery deferred (no address)"),
1710 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
1711 GNUNET_SCHEDULER_cancel (n->retry_task);
1712 n->retry_task = GNUNET_SCHEDULER_add_delayed (timeout,
1713 &retry_transmission_task,
1716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1717 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
1718 mq->message_buf_size,
1719 GNUNET_i2s (&mq->neighbour_id),
1722 /* FIXME: might want to trigger peerinfo lookup here
1723 (unless that's already pending...) */
1726 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1729 if (mq->specific_address->connected == GNUNET_NO)
1730 mq->specific_address->connect_attempts++;
1731 rl = mq->specific_address->ready_list;
1732 mq->plugin = rl->plugin;
1733 if (!mq->internal_msg)
1734 mq->specific_address->in_transmit = GNUNET_YES;
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1737 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
1738 mq->message_buf_size,
1739 GNUNET_i2s (&n->id),
1740 (mq->specific_address->addr != NULL)
1741 ? a2s (mq->plugin->short_name,
1742 mq->specific_address->addr,
1743 mq->specific_address->addrlen)
1745 rl->plugin->short_name);
1747 GNUNET_STATISTICS_update (stats,
1748 gettext_noop ("# bytes in message queue for other peers"),
1749 - (int64_t) mq->message_buf_size,
1751 GNUNET_STATISTICS_update (stats,
1752 gettext_noop ("# bytes pending with plugins"),
1753 mq->message_buf_size,
1756 GNUNET_CONTAINER_DLL_insert (n->cont_head,
1760 ret = rl->plugin->api->send (rl->plugin->api->cls,
1763 mq->message_buf_size,
1765 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1766 mq->specific_address->session,
1767 mq->specific_address->addr,
1768 mq->specific_address->addrlen,
1770 &transmit_send_continuation, mq);
1773 /* failure, but 'send' would not call continuation in this case,
1774 so we need to do it here! */
1775 transmit_send_continuation (mq,
1783 * Send the specified message to the specified peer.
1785 * @param client source of the transmission request (can be NULL)
1786 * @param peer_address ForeignAddressList where we should send this message
1787 * @param priority how important is the message
1788 * @param timeout how long do we have to transmit?
1789 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
1790 * @param message_buf_size total size of all messages in message_buf
1791 * @param is_internal is this an internal message; these are pre-pended and
1792 * also do not count for plugins being "ready" to transmit
1793 * @param neighbour handle to the neighbour for transmission
1796 transmit_to_peer (struct TransportClient *client,
1797 struct ForeignAddressList *peer_address,
1798 unsigned int priority,
1799 struct GNUNET_TIME_Relative timeout,
1800 const char *message_buf,
1801 size_t message_buf_size,
1802 int is_internal, struct NeighbourList *neighbour)
1804 struct MessageQueue *mq;
1809 /* check for duplicate submission */
1810 mq = neighbour->messages_head;
1813 if (mq->client == client)
1815 /* client transmitted to same peer twice
1816 before getting SEND_OK! */
1824 GNUNET_STATISTICS_update (stats,
1825 gettext_noop ("# bytes in message queue for other peers"),
1828 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
1829 mq->specific_address = peer_address;
1830 mq->client = client;
1831 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1832 memcpy (&mq[1], message_buf, message_buf_size);
1833 mq->message_buf = (const char*) &mq[1];
1834 mq->message_buf_size = message_buf_size;
1835 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
1836 mq->internal_msg = is_internal;
1837 mq->priority = priority;
1838 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1840 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
1841 neighbour->messages_tail,
1844 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
1845 neighbour->messages_tail,
1846 neighbour->messages_tail,
1848 try_transmission_to_peer (neighbour);
1853 * Send a plain PING (without address or our HELLO) to the given
1854 * foreign address to try to establish a connection (and validate
1855 * that the other peer is really who he claimed he is).
1857 * @param n neighbour to PING
1860 transmit_plain_ping (struct NeighbourList *n)
1862 struct ValidationEntry *ve;
1863 struct TransportPingMessage ping;
1864 struct ReadyList *rl;
1865 struct TransportPlugin *plugin;
1866 struct ForeignAddressList *fal;
1868 if (! n->public_key_valid)
1870 /* This should not happen since the other peer
1871 should send us a HELLO prior to sending his
1873 GNUNET_break_op (0);
1874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1875 "Could not transmit plain PING to `%s': public key not known\n",
1876 GNUNET_i2s (&n->id));
1879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1880 "Looking for addresses to transmit plain PING to `%s'\n",
1881 GNUNET_i2s (&n->id));
1882 for (rl = n->plugins; rl != NULL; rl = rl->next)
1884 plugin = rl->plugin;
1885 for (fal = rl->addresses; fal != NULL; fal = fal->next)
1887 if (! fal->connected)
1889 ve = GNUNET_malloc (sizeof (struct ValidationEntry));
1890 ve->transport_name = GNUNET_strdup (plugin->short_name);
1891 ve->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1893 ve->send_time = GNUNET_TIME_absolute_get();
1894 ve->session = fal->session;
1895 memcpy(&ve->publicKey,
1897 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1898 ve->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
1899 &timeout_hello_validation,
1901 GNUNET_CONTAINER_multihashmap_put (validation_map,
1904 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1905 ping.header.size = htons(sizeof(struct TransportPingMessage));
1906 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
1907 ping.challenge = htonl(ve->challenge);
1908 memcpy(&ping.target, &n->id, sizeof(struct GNUNET_PeerIdentity));
1909 GNUNET_STATISTICS_update (stats,
1910 gettext_noop ("# PING without HELLO messages sent"),
1913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1914 "Transmitting plain PING to `%s'\n",
1915 GNUNET_i2s (&n->id));
1916 transmit_to_peer (NULL,
1918 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1919 HELLO_VERIFICATION_TIMEOUT,
1920 (const char*) &ping, sizeof (ping),
1928 * Mark the given FAL entry as 'connected' (and hence preferred for
1929 * sending); also mark all others for the same peer as 'not connected'
1930 * (since only one can be preferred).
1932 * @param fal address to set to 'connected'
1935 mark_address_connected (struct ForeignAddressList *fal)
1937 struct ForeignAddressList *pos;
1940 GNUNET_assert (GNUNET_YES == fal->validated);
1941 if (fal->connected == GNUNET_YES)
1942 return; /* nothing to do */
1944 pos = fal->ready_list->addresses;
1947 if (GNUNET_YES == pos->connected)
1950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1951 "Marking address `%s' as no longer connected (due to connect on other address)\n",
1952 a2s (pos->ready_list->plugin->short_name,
1956 GNUNET_break (cnt == GNUNET_YES);
1958 pos->connected = GNUNET_NO;
1959 GNUNET_STATISTICS_update (stats,
1960 gettext_noop ("# connected addresses"),
1966 fal->connected = GNUNET_YES;
1967 if (GNUNET_YES == cnt)
1969 GNUNET_STATISTICS_update (stats,
1970 gettext_noop ("# connected addresses"),
1978 * Find an address in any of the available transports for
1979 * the given neighbour that would be good for message
1980 * transmission. This is essentially the transport selection
1983 * @param neighbour for whom to select an address
1984 * @return selected address, NULL if we have none
1986 struct ForeignAddressList *
1987 find_ready_address(struct NeighbourList *neighbour)
1989 struct ReadyList *head = neighbour->plugins;
1990 struct ForeignAddressList *addresses;
1991 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1992 struct ForeignAddressList *best_address;
1994 /* Hack to prefer unix domain sockets */
1995 struct ForeignAddressList *unix_address = NULL;
1997 best_address = NULL;
1998 while (head != NULL)
2000 addresses = head->addresses;
2001 while (addresses != NULL)
2003 if ( (addresses->timeout.abs_value < now.abs_value) &&
2004 (addresses->connected == GNUNET_YES) )
2007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2008 "Marking long-time inactive connection to `%4s' as down.\n",
2009 GNUNET_i2s (&neighbour->id));
2011 GNUNET_STATISTICS_update (stats,
2012 gettext_noop ("# connected addresses"),
2015 addresses->connected = GNUNET_NO;
2017 addresses = addresses->next;
2020 addresses = head->addresses;
2021 while (addresses != NULL)
2024 if (addresses->addr != NULL)
2025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2026 "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
2027 a2s (head->plugin->short_name,
2029 addresses->addrlen),
2030 GNUNET_i2s (&neighbour->id),
2031 addresses->connected,
2032 addresses->in_transmit,
2033 addresses->validated,
2034 addresses->connect_attempts,
2035 (unsigned long long) addresses->timeout.abs_value,
2036 (unsigned int) addresses->distance);
2038 if (0==strcmp(head->plugin->short_name,"unix"))
2040 if ( (unix_address == NULL) ||
2041 ( (unix_address != NULL) &&
2042 (addresses->latency.rel_value < unix_address->latency.rel_value) ) )
2043 unix_address = addresses;
2045 if ( ( (best_address == NULL) ||
2046 (addresses->connected == GNUNET_YES) ||
2047 (best_address->connected == GNUNET_NO) ) &&
2048 (addresses->in_transmit == GNUNET_NO) &&
2049 ( (best_address == NULL) ||
2050 (addresses->latency.rel_value < best_address->latency.rel_value)) )
2051 best_address = addresses;
2052 /* FIXME: also give lower-latency addresses that are not
2053 connected a chance some times... */
2054 addresses = addresses->next;
2056 if (unix_address != NULL)
2060 if (unix_address != NULL)
2062 best_address = unix_address;
2064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2065 "Found UNIX address, forced this address\n");
2068 if (best_address != NULL)
2071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2072 "Best address found (`%s') has latency of %llu ms.\n",
2073 (best_address->addrlen > 0)
2074 ? a2s (best_address->ready_list->plugin->short_name,
2076 best_address->addrlen)
2078 best_address->latency.rel_value);
2083 GNUNET_STATISTICS_update (stats,
2084 gettext_noop ("# transmission attempts failed (no address)"),
2089 return best_address;
2097 struct GeneratorContext
2099 struct TransportPlugin *plug_pos;
2100 struct OwnAddressList *addr_pos;
2101 struct GNUNET_TIME_Absolute expiration;
2109 address_generator (void *cls, size_t max, void *buf)
2111 struct GeneratorContext *gc = cls;
2114 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
2116 gc->plug_pos = gc->plug_pos->next;
2117 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
2119 if (NULL == gc->plug_pos)
2124 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
2127 gc->addr_pos->addrlen, buf, max);
2128 gc->addr_pos = gc->addr_pos->next;
2134 * Construct our HELLO message from all of the addresses of
2135 * all of the transports.
2138 * @param tc scheduler context
2141 refresh_hello_task (void *cls,
2142 const struct GNUNET_SCHEDULER_TaskContext *tc)
2144 struct GNUNET_HELLO_Message *hello;
2145 struct TransportClient *cpos;
2146 struct NeighbourList *npos;
2147 struct GeneratorContext gc;
2149 hello_task = GNUNET_SCHEDULER_NO_TASK;
2150 gc.plug_pos = plugins;
2151 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
2152 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2153 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
2155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2156 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
2158 GNUNET_STATISTICS_update (stats,
2159 gettext_noop ("# refreshed my HELLO"),
2163 while (cpos != NULL)
2165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2166 "Transmitting my HELLO to client!\n");
2167 transmit_to_client (cpos,
2168 (const struct GNUNET_MessageHeader *) hello,
2173 GNUNET_free_non_null (our_hello);
2175 GNUNET_PEERINFO_add_peer (peerinfo, our_hello);
2176 for (npos = neighbours; npos != NULL; npos = npos->next)
2178 if (! npos->received_pong)
2181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2182 "Transmitting updated `%s' to neighbour `%4s'\n",
2183 "HELLO", GNUNET_i2s (&npos->id));
2185 GNUNET_STATISTICS_update (stats,
2186 gettext_noop ("# transmitted my HELLO to other peers"),
2189 transmit_to_peer (NULL, NULL, 0,
2190 HELLO_ADDRESS_EXPIRATION,
2191 (const char *) our_hello,
2192 GNUNET_HELLO_size(our_hello),
2199 * Schedule task to refresh hello (unless such a
2200 * task exists already).
2205 #if DEBUG_TRANSPORT_HELLO
2206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2207 "refresh_hello() called!\n");
2209 if (hello_task != GNUNET_SCHEDULER_NO_TASK)
2212 = GNUNET_SCHEDULER_add_now (&refresh_hello_task,
2218 * Iterator over hash map entries that NULLs the session of validation
2219 * entries that match the given session.
2221 * @param cls closure (the 'struct Session*' to match against)
2222 * @param key current key code (peer ID, not used)
2223 * @param value value in the hash map ('struct ValidationEntry*')
2224 * @return GNUNET_YES (we should continue to iterate)
2227 remove_session_validations (void *cls,
2228 const GNUNET_HashCode * key,
2231 struct Session *session = cls;
2232 struct ValidationEntry *ve = value;
2234 if (session == ve->session)
2241 * We've been disconnected from the other peer (for some
2242 * connection-oriented transport). Either quickly
2243 * re-establish the connection or signal the disconnect
2246 * Only signal CORE level disconnect if ALL addresses
2247 * for the peer are exhausted.
2249 * @param p overall plugin context
2250 * @param nl neighbour that was disconnected
2253 try_fast_reconnect (struct TransportPlugin *p,
2254 struct NeighbourList *nl)
2256 /* FIXME-MW: fast reconnect / transport switching not implemented... */
2257 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2258 "try_fast_reconnect not implemented!\n");
2259 /* Note: the idea here is to hide problems with transports (or
2260 switching between plugins) from the core to eliminate the need to
2261 re-negotiate session keys and the like; OTOH, we should tell core
2262 quickly (much faster than timeout) `if a connection was lost and
2263 could not be re-established (i.e. other peer went down or is
2264 unable / refuses to communicate);
2266 So we should consider:
2267 1) ideally: our own willingness / need to connect
2268 2) prior failures to connect to this peer (by plugin)
2269 3) ideally: reasons why other peer terminated (as far as knowable)
2271 Most importantly, it must be POSSIBLE for another peer to terminate
2272 a connection for a while (without us instantly re-establishing it).
2273 Similarly, if another peer is gone we should quickly notify CORE.
2274 OTOH, if there was a minor glitch (i.e. crash of gnunet-service-transport
2275 on the other end), we should reconnect in such a way that BOTH CORE
2276 services never even notice.
2277 Furthermore, the same mechanism (or small variation) could be used
2278 to switch to a better-performing plugin (ATS).
2280 Finally, this needs to be tested throughly... */
2283 * GNUNET_NO in the call below makes transport disconnect the peer,
2284 * even if only a single address (out of say, six) went away. This
2285 * function must be careful to ONLY disconnect if the peer is gone,
2286 * not just a specific address.
2288 * More specifically, half the places it was used had it WRONG.
2291 /* No reconnect, signal disconnect instead! */
2294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2295 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2296 "try_fast_reconnect");
2298 GNUNET_STATISTICS_update (stats,
2299 gettext_noop ("# disconnects due to try_fast_reconnect"),
2303 disconnect_neighbour (nl, GNUNET_YES);
2309 * Function that will be called whenever the plugin internally
2310 * cleans up a session pointer and hence the service needs to
2311 * discard all of those sessions as well. Plugins that do not
2312 * use sessions can simply omit calling this function and always
2313 * use NULL wherever a session pointer is needed.
2315 * @param cls closure
2316 * @param peer which peer was the session for
2317 * @param session which session is being destoyed
2320 plugin_env_session_end (void *cls,
2321 const struct GNUNET_PeerIdentity *peer,
2322 struct Session *session)
2324 struct TransportPlugin *p = cls;
2325 struct NeighbourList *nl;
2326 struct ReadyList *rl;
2327 struct ForeignAddressList *pos;
2328 struct ForeignAddressList *prev;
2331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2332 "Session ended with peer `%4s', %s\n",
2334 "plugin_env_session_end");
2336 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2337 &remove_session_validations,
2339 nl = find_neighbour (peer);
2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2344 "No neighbour record found for peer `%4s'\n",
2347 return; /* was never marked as connected */
2352 if (rl->plugin == p)
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2360 "Plugin was associated with peer `%4s'\n",
2363 GNUNET_STATISTICS_update (stats,
2364 gettext_noop ("# disconnects due to session end"),
2367 disconnect_neighbour (nl, GNUNET_YES);
2371 pos = rl->addresses;
2372 while ( (pos != NULL) &&
2373 (pos->session != session) )
2381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2382 "Session was never marked as ready for peer `%4s'\n",
2386 int validations_pending = GNUNET_CONTAINER_multihashmap_contains (validation_map, &peer->hashPubKey);
2388 /* No session was marked as ready, but we have pending validations so do not disconnect from neighbour */
2389 if (validations_pending ==GNUNET_YES)
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2393 "Not disconnecting from peer `%4s due to pending address validations\n", GNUNET_i2s(peer));
2398 //FIXME: This conflicts with inbound tcp connections and tcp nat ... debugging in progress
2399 GNUNET_STATISTICS_update (stats,
2400 gettext_noop ("# disconnects due to unready session"),
2404 disconnect_neighbour (nl, GNUNET_YES);
2405 return; /* was never marked as connected */
2407 pos->session = NULL;
2408 pos->connected = GNUNET_NO;
2409 if (pos->addrlen != 0)
2411 if (nl->received_pong != GNUNET_NO)
2413 GNUNET_STATISTICS_update (stats,
2414 gettext_noop ("# try_fast_reconnect thanks to plugin_env_session_end"),
2417 if (GNUNET_YES == pos->connected)
2418 try_fast_reconnect (p, nl);
2422 GNUNET_STATISTICS_update (stats,
2423 gettext_noop ("# disconnects due to missing pong"),
2426 /* FIXME this is never true?! See: line 2416*/
2427 if (GNUNET_YES == pos->connected)
2428 disconnect_neighbour (nl, GNUNET_YES);
2433 GNUNET_STATISTICS_update (stats,
2434 gettext_noop ("# connected addresses"),
2438 /* was inbound connection, free 'pos' */
2440 rl->addresses = pos->next;
2442 prev->next = pos->next;
2443 if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2445 GNUNET_SCHEDULER_cancel (pos->revalidate_task);
2446 pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2448 GNUNET_free_non_null(pos->ressources);
2449 GNUNET_free_non_null(pos->quality);
2450 ats_modify_problem_state (ats, ATS_MODIFIED);
2452 if (GNUNET_YES != pos->connected)
2454 /* nothing else to do, connection was never up... */
2460 if (nl->received_pong == GNUNET_NO)
2462 GNUNET_STATISTICS_update (stats,
2463 gettext_noop ("# disconnects due to NO pong"),
2466 disconnect_neighbour (nl, GNUNET_YES);
2467 return; /* nothing to do, never connected... */
2469 /* check if we have any validated addresses left */
2470 pos = rl->addresses;
2473 if (GNUNET_YES == pos->validated)
2475 GNUNET_STATISTICS_update (stats,
2476 gettext_noop ("# try_fast_reconnect thanks to validated_address"),
2479 try_fast_reconnect (p, nl);
2484 /* no valid addresses left, signal disconnect! */
2487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2488 "Disconnecting peer `%4s', %s\n",
2490 "plugin_env_session_end");
2492 /* FIXME: This doesn't mean there are no addresses left for this PEER,
2493 * it means there aren't any left for this PLUGIN/PEER combination! So
2494 * calling disconnect_neighbour here with GNUNET_NO forces disconnect
2495 * when it isn't necessary. Using GNUNET_YES at least checks to see
2496 * if there are any addresses that work first, so as not to overdo it.
2499 GNUNET_STATISTICS_update (stats,
2500 gettext_noop ("# disconnects due to plugin_env_session_end"),
2503 disconnect_neighbour (nl, GNUNET_YES);
2508 * Function that must be called by each plugin to notify the
2509 * transport service about the addresses under which the transport
2510 * provided by the plugin can be reached.
2512 * @param cls closure
2513 * @param add_remove GNUNET_YES to add, GNUNET_NO to remove the address
2514 * @param addr one of the addresses of the host, NULL for the last address
2515 * the specific address format depends on the transport
2516 * @param addrlen length of the address
2519 plugin_env_notify_address (void *cls,
2524 struct TransportPlugin *p = cls;
2525 struct OwnAddressList *al;
2526 struct OwnAddressList *prev;
2528 GNUNET_assert (p->api != NULL);
2530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2531 (add_remove == GNUNET_YES)
2532 ? "Adding `%s':%s to the set of our addresses\n"
2533 : "Removing `%s':%s from the set of our addresses\n",
2538 GNUNET_assert (addr != NULL);
2539 if (GNUNET_NO == add_remove)
2545 if ( (addrlen == al->addrlen) &&
2546 (0 == memcmp (addr, &al[1], addrlen)) )
2549 p->addresses = al->next;
2551 prev->next = al->next;
2562 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2563 al->next = p->addresses;
2565 al->addrlen = addrlen;
2566 memcpy (&al[1], addr, addrlen);
2572 * Notify all of our clients about a peer connecting.
2575 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2576 struct GNUNET_TIME_Relative latency,
2579 struct ConnectInfoMessage * cim;
2580 struct TransportClient *cpos;
2584 if (0 == memcmp (peer,
2586 sizeof (struct GNUNET_PeerIdentity)))
2592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2593 "Notifying clients about connection with `%s'\n",
2596 GNUNET_STATISTICS_update (stats,
2597 gettext_noop ("# peers connected"),
2602 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
2603 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2607 cim = GNUNET_malloc (size);
2608 cim->header.size = htons (size);
2609 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2610 cim->ats_count = htonl(2);
2611 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2612 (&(cim->ats))[0].value = htonl (distance);
2613 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
2614 (&(cim->ats))[1].value = htonl ((uint32_t) latency.rel_value);
2615 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2616 (&(cim->ats))[2].value = htonl (0);
2617 memcpy (&cim->id, peer, sizeof (struct GNUNET_PeerIdentity));
2619 /* notify ats about connecting peer */
2620 /* notify ats about connecting peer */
2621 if ((ats != NULL) && (shutdown_in_progress == GNUNET_NO))
2623 ats_modify_problem_state(ats, ATS_MODIFIED);
2624 ats_calculate_bandwidth_distribution (ats, stats);
2629 while (cpos != NULL)
2631 transmit_to_client (cpos, &(cim->header), GNUNET_NO);
2640 * Notify all of our clients about a peer disconnecting.
2643 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2645 struct DisconnectInfoMessage dim;
2646 struct TransportClient *cpos;
2648 if (0 == memcmp (peer,
2650 sizeof (struct GNUNET_PeerIdentity)))
2656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2657 "Notifying clients about lost connection to `%s'\n",
2660 GNUNET_STATISTICS_update (stats,
2661 gettext_noop ("# peers connected"),
2664 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2665 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2666 dim.reserved = htonl (0);
2667 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2669 /* notify ats about connecting peer */
2670 if ((ats != NULL) && (shutdown_in_progress == GNUNET_NO))
2672 ats_modify_problem_state(ats, ATS_MODIFIED);
2673 ats_calculate_bandwidth_distribution (ats, stats);
2677 while (cpos != NULL)
2679 transmit_to_client (cpos, &dim.header, GNUNET_NO);
2686 * Find a ForeignAddressList entry for the given neighbour
2687 * that matches the given address and transport.
2689 * @param neighbour which peer we care about
2690 * @param tname name of the transport plugin
2691 * @param session session to look for, NULL for 'any'; otherwise
2692 * can be used for the service to "learn" this session ID
2694 * @param addr binary address
2695 * @param addrlen length of addr
2696 * @return NULL if no such entry exists
2698 static struct ForeignAddressList *
2699 find_peer_address(struct NeighbourList *neighbour,
2701 struct Session *session,
2705 struct ReadyList *head;
2706 struct ForeignAddressList *pos;
2708 head = neighbour->plugins;
2709 while (head != NULL)
2711 if (0 == strcmp (tname, head->plugin->short_name))
2717 pos = head->addresses;
2718 while ( (pos != NULL) &&
2719 ( (pos->addrlen != addrlen) ||
2720 (memcmp(pos->addr, addr, addrlen) != 0) ) )
2722 if ( (session != NULL) &&
2723 (pos->session == session) )
2727 if ( (session != NULL) && (pos != NULL) )
2728 pos->session = session; /* learn it! */
2734 * Get the peer address struct for the given neighbour and
2735 * address. If it doesn't yet exist, create it.
2737 * @param neighbour which peer we care about
2738 * @param tname name of the transport plugin
2739 * @param session session of the plugin, or NULL for none
2740 * @param addr binary address
2741 * @param addrlen length of addr
2742 * @return NULL if we do not have a transport plugin for 'tname'
2744 static struct ForeignAddressList *
2745 add_peer_address (struct NeighbourList *neighbour,
2747 struct Session *session,
2751 struct ReadyList *head;
2752 struct ForeignAddressList *ret;
2755 ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2758 head = neighbour->plugins;
2760 while (head != NULL)
2762 if (0 == strcmp (tname, head->plugin->short_name))
2768 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2769 ret->session = session;
2770 if ((addrlen > 0) && (addr != NULL))
2772 ret->addr = (const char*) &ret[1];
2773 memcpy (&ret[1], addr, addrlen);
2780 ret->ressources = GNUNET_malloc(available_ressources * sizeof (struct ATS_ressource_entry));
2781 for (c=0; c<available_ressources; c++)
2783 struct ATS_ressource_entry *r = ret->ressources;
2785 r[c].atis_index = ressources[c].atis_index;
2786 if (0 == strcmp(neighbour->plugins->plugin->short_name,"unix"))
2788 r[c].c = ressources[c].c_unix;
2790 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"udp"))
2792 r[c].c = ressources[c].c_udp;
2794 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"tcp"))
2796 r[c].c = ressources[c].c_tcp;
2798 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"http"))
2800 r[c].c = ressources[c].c_http;
2802 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"https"))
2804 r[c].c = ressources[c].c_https;
2806 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"wlan"))
2808 r[c].c = ressources[c].c_wlan;
2812 r[c].c = ressources[c].c_default;
2813 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2814 "Assigning default cost to peer `%s' addr plugin `%s'! This should not happen!\n",
2815 GNUNET_i2s(&neighbour->id),
2816 neighbour->plugins->plugin->short_name);
2820 ret->quality = GNUNET_malloc (available_quality_metrics * sizeof (struct ATS_quality_entry));
2821 ret->addrlen = addrlen;
2822 ret->expires = GNUNET_TIME_relative_to_absolute
2823 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2824 ret->latency = GNUNET_TIME_relative_get_forever();
2826 ret->timeout = GNUNET_TIME_relative_to_absolute
2827 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2828 ret->ready_list = head;
2829 ret->next = head->addresses;
2830 head->addresses = ret;
2836 * Closure for 'add_validated_address'.
2838 struct AddValidatedAddressContext
2841 * Entry that has been validated.
2843 const struct ValidationEntry *ve;
2846 * Flag set after we have added the address so
2847 * that we terminate the iteration next time.
2854 * Callback function used to fill a buffer of max bytes with a list of
2855 * addresses in the format used by HELLOs. Should use
2856 * "GNUNET_HELLO_add_address" as a helper function.
2858 * @param cls the 'struct AddValidatedAddressContext' with the validated address
2859 * @param max maximum number of bytes that can be written to buf
2860 * @param buf where to write the address information
2861 * @return number of bytes written, 0 to signal the
2862 * end of the iteration.
2865 add_validated_address (void *cls,
2866 size_t max, void *buf)
2868 struct AddValidatedAddressContext *avac = cls;
2869 const struct ValidationEntry *ve = avac->ve;
2871 if (GNUNET_YES == avac->done)
2873 avac->done = GNUNET_YES;
2874 return GNUNET_HELLO_add_address (ve->transport_name,
2875 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2885 * Closure for 'check_address_exists'.
2887 struct CheckAddressExistsClosure
2890 * Address to check for.
2895 * Name of the transport.
2902 struct Session *session;
2905 * Set to GNUNET_YES if the address exists.
2918 * Iterator over hash map entries. Checks if the given
2919 * validation entry is for the same address as what is given
2922 * @param cls the 'struct CheckAddressExistsClosure*'
2923 * @param key current key code (ignored)
2924 * @param value value in the hash map ('struct ValidationEntry')
2925 * @return GNUNET_YES if we should continue to
2926 * iterate (mismatch), GNUNET_NO if not (entry matched)
2929 check_address_exists (void *cls,
2930 const GNUNET_HashCode * key,
2933 struct CheckAddressExistsClosure *caec = cls;
2934 struct ValidationEntry *ve = value;
2936 if ( (0 == strcmp (caec->tname,
2937 ve->transport_name)) &&
2938 (caec->addrlen == ve->addrlen) &&
2939 (0 == memcmp (caec->addr,
2943 caec->exists = GNUNET_YES;
2946 if ( (ve->session != NULL) &&
2947 (caec->session == ve->session) )
2949 caec->exists = GNUNET_YES;
2957 neighbour_timeout_task (void *cls,
2958 const struct GNUNET_SCHEDULER_TaskContext *tc)
2960 struct NeighbourList *n = cls;
2963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2964 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
2966 GNUNET_STATISTICS_update (stats,
2967 gettext_noop ("# disconnects due to timeout"),
2970 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2971 disconnect_neighbour (n, GNUNET_NO);
2976 * Schedule the job that will cause us to send a PING to the
2977 * foreign address to evaluate its validity and latency.
2979 * @param fal address to PING
2982 schedule_next_ping (struct ForeignAddressList *fal);
2986 * Add the given address to the list of foreign addresses
2987 * available for the given peer (check for duplicates).
2989 * @param cls the respective 'struct NeighbourList' to update
2990 * @param tname name of the transport
2991 * @param expiration expiration time
2992 * @param addr the address
2993 * @param addrlen length of the address
2994 * @return GNUNET_OK (always)
2997 add_to_foreign_address_list (void *cls,
2999 struct GNUNET_TIME_Absolute expiration,
3003 struct NeighbourList *n = cls;
3004 struct ForeignAddressList *fal;
3007 GNUNET_STATISTICS_update (stats,
3008 gettext_noop ("# valid peer addresses returned by PEERINFO"),
3012 fal = find_peer_address (n, tname, NULL, addr, addrlen);
3015 #if DEBUG_TRANSPORT_HELLO
3016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3017 "Adding address `%s' (%s) for peer `%4s' due to PEERINFO data for %llums.\n",
3018 a2s (tname, addr, addrlen),
3020 GNUNET_i2s (&n->id),
3021 expiration.abs_value);
3023 fal = add_peer_address (n, tname, NULL, addr, addrlen);
3026 GNUNET_STATISTICS_update (stats,
3027 gettext_noop ("# previously validated addresses lacking transport"),
3033 fal->expires = GNUNET_TIME_absolute_max (expiration,
3035 schedule_next_ping (fal);
3041 fal->expires = GNUNET_TIME_absolute_max (expiration,
3047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3048 "Failed to add new address for `%4s'\n",
3049 GNUNET_i2s (&n->id));
3053 if (fal->validated == GNUNET_NO)
3055 fal->validated = GNUNET_YES;
3056 GNUNET_STATISTICS_update (stats,
3057 gettext_noop ("# peer addresses considered valid"),
3061 if (try == GNUNET_YES)
3064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3065 "Have new addresses, will try to trigger transmissions.\n");
3067 try_transmission_to_peer (n);
3074 * Add addresses in validated HELLO "h" to the set of addresses
3075 * we have for this peer.
3077 * @param cls closure ('struct NeighbourList*')
3078 * @param peer id of the peer, NULL for last call
3079 * @param h hello message for the peer (can be NULL)
3080 * @param err_msg NULL if successful, otherwise contains error message
3083 add_hello_for_peer (void *cls,
3084 const struct GNUNET_PeerIdentity *peer,
3085 const struct GNUNET_HELLO_Message *h,
3086 const char *err_msg)
3088 struct NeighbourList *n = cls;
3090 if (err_msg != NULL)
3093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3094 _("Error in communication with PEERINFO service: %s\n"),
3101 GNUNET_STATISTICS_update (stats,
3102 gettext_noop ("# outstanding peerinfo iterate requests"),
3109 return; /* no HELLO available */
3111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3112 "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
3116 if (GNUNET_YES != n->public_key_valid)
3118 GNUNET_HELLO_get_key (h, &n->publicKey);
3119 n->public_key_valid = GNUNET_YES;
3121 GNUNET_HELLO_iterate_addresses (h,
3123 &add_to_foreign_address_list,
3129 * Create a fresh entry in our neighbour list for the given peer.
3130 * Will try to transmit our current HELLO to the new neighbour.
3131 * Do not call this function directly, use 'setup_peer_check_blacklist.
3133 * @param peer the peer for which we create the entry
3134 * @param do_hello should we schedule transmitting a HELLO
3135 * @return the new neighbour list entry
3137 static struct NeighbourList *
3138 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
3141 struct NeighbourList *n;
3142 struct TransportPlugin *tp;
3143 struct ReadyList *rl;
3145 GNUNET_assert (0 != memcmp (peer,
3147 sizeof (struct GNUNET_PeerIdentity)));
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3150 "Setting up state for neighbour `%4s'\n",
3153 GNUNET_STATISTICS_update (stats,
3154 gettext_noop ("# active neighbours"),
3157 n = GNUNET_malloc (sizeof (struct NeighbourList));
3158 n->next = neighbours;
3162 GNUNET_TIME_relative_to_absolute
3163 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3164 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
3165 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3166 MAX_BANDWIDTH_CARRY_S);
3170 if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
3172 rl = GNUNET_malloc (sizeof (struct ReadyList));
3174 rl->next = n->plugins;
3177 rl->addresses = NULL;
3181 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
3183 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
3184 &neighbour_timeout_task, n);
3187 GNUNET_STATISTICS_update (stats,
3188 gettext_noop ("# peerinfo new neighbor iterate requests"),
3191 GNUNET_STATISTICS_update (stats,
3192 gettext_noop ("# outstanding peerinfo iterate requests"),
3195 n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
3196 GNUNET_TIME_UNIT_FOREVER_REL,
3197 &add_hello_for_peer, n);
3199 GNUNET_STATISTICS_update (stats,
3200 gettext_noop ("# HELLO's sent to new neighbors"),
3203 if (NULL != our_hello)
3204 transmit_to_peer (NULL, NULL, 0,
3205 HELLO_ADDRESS_EXPIRATION,
3206 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
3214 * Function called after we have checked if communicating
3215 * with a given peer is acceptable.
3217 * @param cls closure
3218 * @param n NULL if communication is not acceptable
3220 typedef void (*SetupContinuation)(void *cls,
3221 struct NeighbourList *n);
3225 * Information kept for each client registered to perform
3231 * This is a linked list.
3233 struct Blacklisters *next;
3236 * This is a linked list.
3238 struct Blacklisters *prev;
3241 * Client responsible for this entry.
3243 struct GNUNET_SERVER_Client *client;
3246 * Blacklist check that we're currently performing.
3248 struct BlacklistCheck *bc;
3254 * Head of DLL of blacklisting clients.
3256 static struct Blacklisters *bl_head;
3259 * Tail of DLL of blacklisting clients.
3261 static struct Blacklisters *bl_tail;
3265 * Context we use when performing a blacklist check.
3267 struct BlacklistCheck
3271 * This is a linked list.
3273 struct BlacklistCheck *next;
3276 * This is a linked list.
3278 struct BlacklistCheck *prev;
3281 * Peer being checked.
3283 struct GNUNET_PeerIdentity peer;
3286 * Option for setup neighbour afterwards.
3291 * Continuation to call with the result.
3293 SetupContinuation cont;
3301 * Current transmission request handle for this client, or NULL if no
3302 * request is pending.
3304 struct GNUNET_CONNECTION_TransmitHandle *th;
3307 * Our current position in the blacklisters list.
3309 struct Blacklisters *bl_pos;
3312 * Current task performing the check.
3314 GNUNET_SCHEDULER_TaskIdentifier task;
3319 * Head of DLL of active blacklisting queries.
3321 static struct BlacklistCheck *bc_head;
3324 * Tail of DLL of active blacklisting queries.
3326 static struct BlacklistCheck *bc_tail;
3330 * Perform next action in the blacklist check.
3332 * @param cls the 'struct BlacklistCheck*'
3336 do_blacklist_check (void *cls,
3337 const struct GNUNET_SCHEDULER_TaskContext *tc);
3340 * Transmit blacklist query to the client.
3342 * @param cls the 'struct BlacklistCheck'
3343 * @param size number of bytes allowed
3344 * @param buf where to copy the message
3345 * @return number of bytes copied to buf
3348 transmit_blacklist_message (void *cls,
3352 struct BlacklistCheck *bc = cls;
3353 struct Blacklisters *bl;
3354 struct BlacklistMessage bm;
3359 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
3360 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3362 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3363 "Failed to send blacklist test for peer `%s' to client\n",
3364 GNUNET_i2s (&bc->peer));
3368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3369 "Sending blacklist test for peer `%s' to client\n",
3370 GNUNET_i2s (&bc->peer));
3373 bm.header.size = htons (sizeof (struct BlacklistMessage));
3374 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
3375 bm.is_allowed = htonl (0);
3377 memcpy (buf, &bm, sizeof (bm));
3378 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
3384 * Perform next action in the blacklist check.
3386 * @param cls the 'struct BlacklistCheck*'
3390 do_blacklist_check (void *cls,
3391 const struct GNUNET_SCHEDULER_TaskContext *tc)
3393 struct BlacklistCheck *bc = cls;
3394 struct Blacklisters *bl;
3396 bc->task = GNUNET_SCHEDULER_NO_TASK;
3401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3402 "No blacklist clients active, will now setup neighbour record for peer `%s'\n",
3403 GNUNET_i2s (&bc->peer));
3405 bc->cont (bc->cont_cls,
3406 setup_new_neighbour (&bc->peer, bc->do_hello));
3413 bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
3414 sizeof (struct BlacklistMessage),
3415 GNUNET_TIME_UNIT_FOREVER_REL,
3416 &transmit_blacklist_message,
3423 * Obtain a 'struct NeighbourList' for the given peer. If such an entry
3424 * does not yet exist, check the blacklist. If the blacklist says creating
3425 * one is acceptable, create one and call the continuation; otherwise
3426 * call the continuation with NULL.
3428 * @param peer peer to setup or look up a struct NeighbourList for
3429 * @param do_hello should we also schedule sending our HELLO to the peer
3430 * if this is a new record
3431 * @param cont function to call with the 'struct NeigbhbourList*'
3432 * @param cont_cls closure for cont
3435 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
3437 SetupContinuation cont,
3440 struct NeighbourList *n;
3441 struct BlacklistCheck *bc;
3443 n = find_neighbour(peer);
3447 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3448 "Neighbour record exists for peer `%s'\n",
3455 if (bl_head == NULL)
3458 cont (cont_cls, setup_new_neighbour (peer, do_hello));
3460 setup_new_neighbour(peer, do_hello);
3463 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3464 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3466 bc->do_hello = do_hello;
3468 bc->cont_cls = cont_cls;
3469 bc->bl_pos = bl_head;
3470 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3476 * Function called with the result of querying a new blacklister about
3477 * it being allowed (or not) to continue to talk to an existing neighbour.
3479 * @param cls the original 'struct NeighbourList'
3480 * @param n NULL if we need to disconnect
3483 confirm_or_drop_neighbour (void *cls,
3484 struct NeighbourList *n)
3486 struct NeighbourList * orig = cls;
3491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3492 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&orig->id),
3493 "confirm_or_drop_neighboUr");
3495 GNUNET_STATISTICS_update (stats,
3496 gettext_noop ("# disconnects due to blacklist"),
3499 disconnect_neighbour (orig, GNUNET_NO);
3505 * Handle a request to start a blacklist.
3507 * @param cls closure (always NULL)
3508 * @param client identification of the client
3509 * @param message the actual message
3512 handle_blacklist_init (void *cls,
3513 struct GNUNET_SERVER_Client *client,
3514 const struct GNUNET_MessageHeader *message)
3516 struct Blacklisters *bl;
3517 struct BlacklistCheck *bc;
3518 struct NeighbourList *n;
3523 if (bl->client == client)
3526 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3531 bl = GNUNET_malloc (sizeof (struct Blacklisters));
3532 bl->client = client;
3533 GNUNET_SERVER_client_keep (client);
3534 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
3535 /* confirm that all existing connections are OK! */
3539 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3540 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3542 bc->do_hello = GNUNET_NO;
3543 bc->cont = &confirm_or_drop_neighbour;
3546 if (n == neighbours) /* all would wait for the same client, no need to
3547 create more than just the first task right now */
3548 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3556 * Handle a request to blacklist a peer.
3558 * @param cls closure (always NULL)
3559 * @param client identification of the client
3560 * @param message the actual message
3563 handle_blacklist_reply (void *cls,
3564 struct GNUNET_SERVER_Client *client,
3565 const struct GNUNET_MessageHeader *message)
3567 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
3568 struct Blacklisters *bl;
3569 struct BlacklistCheck *bc;
3572 while ( (bl != NULL) &&
3573 (bl->client != client) )
3578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3579 "Blacklist client disconnected\n");
3581 /* FIXME: other error handling here!? */
3582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3587 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
3590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3591 "Blacklist check failed, peer not allowed\n");
3593 bc->cont (bc->cont_cls, NULL);
3594 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
3600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3601 "Blacklist check succeeded, continuing with checks\n");
3603 bc->bl_pos = bc->bl_pos->next;
3604 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3607 /* check if any other bc's are waiting for this blacklister */
3611 if ( (bc->bl_pos == bl) &&
3612 (GNUNET_SCHEDULER_NO_TASK == bc->task) )
3613 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3621 * Send periodic PING messages to a given foreign address.
3623 * @param cls our 'struct PeriodicValidationContext*'
3624 * @param tc task context
3627 send_periodic_ping (void *cls,
3628 const struct GNUNET_SCHEDULER_TaskContext *tc)
3630 struct ForeignAddressList *peer_address = cls;
3631 struct TransportPlugin *tp;
3632 struct ValidationEntry *va;
3633 struct NeighbourList *neighbour;
3634 struct TransportPingMessage ping;
3635 struct CheckAddressExistsClosure caec;
3637 uint16_t hello_size;
3641 peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3642 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3644 tp = peer_address->ready_list->plugin;
3645 neighbour = peer_address->ready_list->neighbour;
3646 if (GNUNET_YES != neighbour->public_key_valid)
3648 /* no public key yet, try again later */
3649 schedule_next_ping (peer_address);
3652 caec.addr = peer_address->addr;
3653 caec.addrlen = peer_address->addrlen;
3654 caec.tname = tp->short_name;
3655 caec.session = peer_address->session;
3656 caec.exists = GNUNET_NO;
3657 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3658 &check_address_exists,
3660 if (caec.exists == GNUNET_YES)
3662 /* During validation attempts we will likely trigger the other
3663 peer trying to validate our address which in turn will cause
3664 it to send us its HELLO, so we expect to hit this case rather
3665 frequently. Only print something if we are very verbose. */
3666 #if DEBUG_TRANSPORT > 1
3667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3668 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3669 (peer_address->addr != NULL)
3670 ? a2s (tp->short_name,
3672 peer_address->addrlen)
3675 GNUNET_i2s (&neighbour->id));
3677 schedule_next_ping (peer_address);
3680 va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3681 va->transport_name = GNUNET_strdup (tp->short_name);
3682 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
3684 va->send_time = GNUNET_TIME_absolute_get();
3685 va->session = peer_address->session;
3686 if (peer_address->addr != NULL)
3688 va->addr = (const void*) &va[1];
3689 memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3690 va->addrlen = peer_address->addrlen;
3692 memcpy(&va->publicKey,
3693 &neighbour->publicKey,
3694 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3696 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
3697 &timeout_hello_validation,
3699 GNUNET_CONTAINER_multihashmap_put (validation_map,
3700 &neighbour->id.hashPubKey,
3702 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3704 if (peer_address->validated != GNUNET_YES)
3705 hello_size = GNUNET_HELLO_size(our_hello);
3709 tsize = sizeof(struct TransportPingMessage) + hello_size;
3711 if (peer_address->addr != NULL)
3713 slen = strlen (tp->short_name) + 1;
3714 tsize += slen + peer_address->addrlen;
3718 slen = 0; /* make gcc happy */
3720 message_buf = GNUNET_malloc(tsize);
3721 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3722 ping.challenge = htonl(va->challenge);
3723 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3724 if (peer_address->validated != GNUNET_YES)
3726 memcpy(message_buf, our_hello, hello_size);
3729 if (peer_address->addr != NULL)
3731 ping.header.size = htons(sizeof(struct TransportPingMessage) +
3732 peer_address->addrlen +
3734 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3737 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3739 peer_address->addrlen);
3743 ping.header.size = htons(sizeof(struct TransportPingMessage));
3746 memcpy(&message_buf[hello_size],
3748 sizeof(struct TransportPingMessage));
3750 #if DEBUG_TRANSPORT_REVALIDATION
3751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3752 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s'\n",
3753 (peer_address->addr != NULL)
3754 ? a2s (peer_address->plugin->short_name,
3756 peer_address->addrlen)
3759 GNUNET_i2s (&neighbour->id),
3760 "HELLO", hello_size,
3763 if (peer_address->validated != GNUNET_YES)
3764 GNUNET_STATISTICS_update (stats,
3765 gettext_noop ("# PING with HELLO messages sent"),
3769 GNUNET_STATISTICS_update (stats,
3770 gettext_noop ("# PING without HELLO messages sent"),
3773 GNUNET_STATISTICS_update (stats,
3774 gettext_noop ("# PING messages sent for re-validation"),
3777 transmit_to_peer (NULL, peer_address,
3778 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3779 HELLO_VERIFICATION_TIMEOUT,
3781 GNUNET_YES, neighbour);
3782 GNUNET_free(message_buf);
3783 schedule_next_ping (peer_address);
3788 * Schedule the job that will cause us to send a PING to the
3789 * foreign address to evaluate its validity and latency.
3791 * @param fal address to PING
3794 schedule_next_ping (struct ForeignAddressList *fal)
3796 struct GNUNET_TIME_Relative delay;
3798 if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3800 delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3801 delay.rel_value /= 2; /* do before expiration */
3802 delay = GNUNET_TIME_relative_min (delay,
3803 LATENCY_EVALUATION_MAX_DELAY);
3804 if (GNUNET_YES != fal->estimated)
3806 delay = GNUNET_TIME_UNIT_ZERO;
3807 fal->estimated = GNUNET_YES;
3809 if (GNUNET_YES == fal->connected)
3811 delay = GNUNET_TIME_relative_min (delay,
3812 CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3814 /* FIXME: also adjust delay based on how close the last
3815 observed latency is to the latency of the best alternative */
3816 /* bound how fast we can go */
3817 delay = GNUNET_TIME_relative_max (delay,
3818 GNUNET_TIME_UNIT_SECONDS);
3819 /* randomize a bit (to avoid doing all at the same time) */
3820 delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3821 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(delay,
3822 &send_periodic_ping,
3830 * Function that will be called if we receive some payload
3831 * from another peer.
3833 * @param message the payload
3834 * @param n peer who claimed to be the sender
3837 handle_payload_message (const struct GNUNET_MessageHeader *message,
3838 struct NeighbourList *n)
3840 struct InboundMessage *im;
3841 struct TransportClient *cpos;
3844 msize = ntohs (message->size);
3845 if (n->received_pong == GNUNET_NO)
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3849 "Received message of type %u and size %u from `%4s', but no pong yet!\n",
3850 ntohs (message->type),
3851 ntohs (message->size),
3852 GNUNET_i2s (&n->id));
3854 GNUNET_free_non_null (n->pre_connect_message_buffer);
3855 n->pre_connect_message_buffer = GNUNET_malloc (msize);
3856 memcpy (n->pre_connect_message_buffer, message, msize);
3861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3862 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
3863 ntohs (message->type),
3864 ntohs (message->size),
3865 GNUNET_i2s (&n->id));
3867 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3870 n->quota_violation_count++;
3872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3873 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
3874 n->in_tracker.available_bytes_per_s__,
3875 n->quota_violation_count);
3877 /* Discount 32k per violation */
3878 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3883 if (n->quota_violation_count > 0)
3885 /* try to add 32k back */
3886 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3888 n->quota_violation_count--;
3891 GNUNET_STATISTICS_update (stats,
3892 gettext_noop ("# payload received from other peers"),
3895 /* transmit message to all clients */
3896 uint32_t ats_count = 2;
3897 size_t size = sizeof (struct InboundMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) + msize;
3898 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
3901 im = GNUNET_malloc (size);
3902 im->header.size = htons (size);
3903 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3905 im->ats_count = htonl(ats_count);
3906 /* Setting ATS data */
3907 (&(im->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
3908 (&(im->ats))[0].value = htonl (n->distance);
3909 (&(im->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
3910 (&(im->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
3911 (&(im->ats))[ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
3912 (&(im->ats))[ats_count].value = htonl (0);
3914 memcpy (&((&(im->ats))[ats_count+1]), message, msize);
3916 while (cpos != NULL)
3918 transmit_to_client (cpos, &im->header, GNUNET_YES);
3926 * Iterator over hash map entries. Checks if the given validation
3927 * entry is for the same challenge as what is given in the PONG.
3929 * @param cls the 'struct TransportPongMessage*'
3930 * @param key peer identity
3931 * @param value value in the hash map ('struct ValidationEntry')
3932 * @return GNUNET_YES if we should continue to
3933 * iterate (mismatch), GNUNET_NO if not (entry matched)
3936 check_pending_validation (void *cls,
3937 const GNUNET_HashCode * key,
3940 const struct TransportPongMessage *pong = cls;
3941 struct ValidationEntry *ve = value;
3942 struct AddValidatedAddressContext avac;
3943 unsigned int challenge = ntohl(pong->challenge);
3944 struct GNUNET_HELLO_Message *hello;
3945 struct GNUNET_PeerIdentity target;
3946 struct NeighbourList *n;
3947 struct ForeignAddressList *fal;
3948 struct OwnAddressList *oal;
3949 struct TransportPlugin *tp;
3950 struct GNUNET_MessageHeader *prem;
3956 ps = ntohs (pong->header.size);
3957 if (ps < sizeof (struct TransportPongMessage))
3959 GNUNET_break_op (0);
3962 addr = (const char*) &pong[1];
3963 slen = strlen (ve->transport_name) + 1;
3964 if ( (ps - sizeof (struct TransportPongMessage) < slen) ||
3965 (ve->challenge != challenge) ||
3966 (addr[slen-1] != '\0') ||
3967 (0 != strcmp (addr, ve->transport_name)) ||
3968 (ntohl (pong->purpose.size)
3969 != sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3971 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
3972 sizeof (struct GNUNET_PeerIdentity) + ps - sizeof (struct TransportPongMessage)) )
3977 alen = ps - sizeof (struct TransportPongMessage) - slen;
3978 switch (ntohl (pong->purpose.purpose))
3980 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN:
3981 if ( (ve->addrlen + slen != ntohl (pong->addrlen)) ||
3982 (0 != memcmp (&addr[slen],
3986 return GNUNET_YES; /* different entry, keep trying! */
3988 if (0 != memcmp (&pong->pid,
3990 sizeof (struct GNUNET_PeerIdentity)))
3992 GNUNET_break_op (0);
3996 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
4001 GNUNET_break_op (0);
4006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4007 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
4009 a2s (ve->transport_name,
4010 (const struct sockaddr *) ve->addr,
4012 ve->transport_name);
4015 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING:
4016 if (0 != memcmp (&pong->pid,
4018 sizeof (struct GNUNET_PeerIdentity)))
4022 GNUNET_asprintf(&peer, "%s",GNUNET_i2s (&pong->pid));
4024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4025 "Received PONG for different identity: I am `%s', PONG identity: `%s'\n",
4026 GNUNET_i2s (&my_identity),
4032 if (ve->addrlen != 0)
4034 /* must have been for a different validation entry */
4037 tp = find_transport (ve->transport_name);
4043 oal = tp->addresses;
4046 if ( (oal->addrlen == alen) &&
4047 (0 == memcmp (&oal[1],
4055 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4056 _("Not accepting PONG from `%s' with address `%s' since I cannot confirm using this address.\n"),
4057 GNUNET_i2s (&pong->pid),
4058 a2s (ve->transport_name,
4061 /* FIXME: since the sender of the PONG currently uses the
4062 wrong address (see FIMXE there!), we cannot run a
4063 proper check here... */
4069 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING,
4074 GNUNET_break_op (0);
4079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4080 "Confirmed that peer `%4s' is talking to us using address `%s' (%s) for us.\n",
4082 a2s (ve->transport_name,
4085 ve->transport_name);
4089 GNUNET_break_op (0);
4092 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
4094 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4095 _("Received expired signature. Check system time.\n"));
4098 GNUNET_STATISTICS_update (stats,
4099 gettext_noop ("# address validation successes"),
4102 /* create the updated HELLO */
4103 GNUNET_CRYPTO_hash (&ve->publicKey,
4104 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4105 &target.hashPubKey);
4106 if (ve->addr != NULL)
4108 avac.done = GNUNET_NO;
4110 hello = GNUNET_HELLO_create (&ve->publicKey,
4111 &add_validated_address,
4113 GNUNET_PEERINFO_add_peer (peerinfo,
4115 GNUNET_free (hello);
4117 n = find_neighbour (&target);
4120 n->publicKey = ve->publicKey;
4121 n->public_key_valid = GNUNET_YES;
4122 fal = add_peer_address (n,
4127 GNUNET_assert (fal != NULL);
4128 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
4129 fal->validated = GNUNET_YES;
4130 mark_address_connected (fal);
4131 GNUNET_STATISTICS_update (stats,
4132 gettext_noop ("# peer addresses considered valid"),
4135 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
4136 update_addr_value (fal, GNUNET_TIME_absolute_get_duration (ve->send_time).rel_value, GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
4138 schedule_next_ping (fal);
4139 if (n->latency.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
4140 n->latency = fal->latency;
4142 n->latency.rel_value = (fal->latency.rel_value + n->latency.rel_value) / 2;
4144 n->distance = fal->distance;
4145 if (GNUNET_NO == n->received_pong)
4147 n->received_pong = GNUNET_YES;
4148 notify_clients_connect (&target, n->latency, n->distance);
4149 if (NULL != (prem = n->pre_connect_message_buffer))
4151 n->pre_connect_message_buffer = NULL;
4152 handle_payload_message (prem, n);
4156 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4158 GNUNET_SCHEDULER_cancel (n->retry_task);
4159 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4160 try_transmission_to_peer (n);
4164 /* clean up validation entry */
4165 GNUNET_assert (GNUNET_YES ==
4166 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4169 abort_validation (NULL, NULL, ve);
4175 * Function that will be called if we receive a validation
4176 * of an address challenge that we transmitted to another
4177 * peer. Note that the validation should only be considered
4178 * acceptable if the challenge matches AND if the sender
4179 * address is at least a plausible address for this peer
4180 * (otherwise we may be seeing a MiM attack).
4182 * @param cls closure
4183 * @param message the pong message
4184 * @param peer who responded to our challenge
4185 * @param sender_address string describing our sender address (as observed
4186 * by the other peer in binary format)
4187 * @param sender_address_len number of bytes in 'sender_address'
4190 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
4191 const struct GNUNET_PeerIdentity *peer,
4192 const char *sender_address,
4193 size_t sender_address_len)
4195 if (0 == memcmp (peer,
4197 sizeof (struct GNUNET_PeerIdentity)))
4199 /* PONG send to self, ignore */
4200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4201 "Receiving `%s' message from myself\n",
4205 #if DEBUG_TRANSPORT > 1
4206 /* we get tons of these that just get discarded, only log
4207 if we are quite verbose */
4208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4209 "Receiving `%s' message from `%4s'.\n", "PONG",
4212 GNUNET_STATISTICS_update (stats,
4213 gettext_noop ("# PONG messages received"),
4216 if (GNUNET_SYSERR !=
4217 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
4219 &check_pending_validation,
4222 /* This is *expected* to happen a lot since we send
4223 PONGs to *all* known addresses of the sender of
4224 the PING, so most likely we get multiple PONGs
4225 per PING, and all but the first PONG will end up
4226 here. So really we should not print anything here
4227 unless we want to be very, very verbose... */
4228 #if DEBUG_TRANSPORT > 2
4229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4230 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
4242 * Try to validate a neighbour's address by sending him our HELLO and a PING.
4244 * @param cls the 'struct ValidationEntry*'
4245 * @param neighbour neighbour to validate, NULL if validation failed
4248 transmit_hello_and_ping (void *cls,
4249 struct NeighbourList *neighbour)
4251 struct ValidationEntry *va = cls;
4252 struct ForeignAddressList *peer_address;
4253 struct TransportPingMessage ping;
4254 uint16_t hello_size;
4257 struct GNUNET_PeerIdentity id;
4260 GNUNET_CRYPTO_hash (&va->publicKey,
4261 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4263 if (neighbour == NULL)
4265 /* FIXME: stats... */
4266 GNUNET_break (GNUNET_OK ==
4267 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4270 abort_validation (NULL, NULL, va);
4273 neighbour->publicKey = va->publicKey;
4274 neighbour->public_key_valid = GNUNET_YES;
4275 peer_address = add_peer_address (neighbour,
4276 va->transport_name, NULL,
4277 (const void*) &va[1],
4279 if (peer_address == NULL)
4281 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4282 "Failed to add peer `%4s' for plugin `%s'\n",
4283 GNUNET_i2s (&neighbour->id),
4284 va->transport_name);
4285 GNUNET_break (GNUNET_OK ==
4286 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4289 abort_validation (NULL, NULL, va);
4292 if (NULL == our_hello)
4293 refresh_hello_task (NULL, NULL);
4294 hello_size = GNUNET_HELLO_size(our_hello);
4295 slen = strlen(va->transport_name) + 1;
4296 tsize = sizeof(struct TransportPingMessage) + hello_size + va->addrlen + slen;
4297 message_buf = GNUNET_malloc(tsize);
4298 ping.challenge = htonl(va->challenge);
4299 ping.header.size = htons(sizeof(struct TransportPingMessage) + slen + va->addrlen);
4300 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
4301 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
4302 memcpy(message_buf, our_hello, hello_size);
4303 memcpy(&message_buf[hello_size],
4305 sizeof(struct TransportPingMessage));
4306 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
4309 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
4313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4314 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
4317 : a2s (va->transport_name,
4318 (const void*) &va[1], va->addrlen),
4320 GNUNET_i2s (&neighbour->id),
4321 "HELLO", hello_size,
4322 "PING", sizeof (struct TransportPingMessage) + va->addrlen + slen);
4325 GNUNET_STATISTICS_update (stats,
4326 gettext_noop ("# PING messages sent for initial validation"),
4329 transmit_to_peer (NULL, peer_address,
4330 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
4331 HELLO_VERIFICATION_TIMEOUT,
4333 GNUNET_YES, neighbour);
4334 GNUNET_free(message_buf);
4339 * Check if the given address is already being validated; if not,
4340 * append the given address to the list of entries that are being be
4341 * validated and initiate validation.
4343 * @param cls closure ('struct CheckHelloValidatedContext *')
4344 * @param tname name of the transport
4345 * @param expiration expiration time
4346 * @param addr the address
4347 * @param addrlen length of the address
4348 * @return GNUNET_OK (always)
4351 run_validation (void *cls,
4353 struct GNUNET_TIME_Absolute expiration,
4357 struct CheckHelloValidatedContext *chvc = cls;
4358 struct GNUNET_PeerIdentity id;
4359 struct TransportPlugin *tp;
4360 struct ValidationEntry *va;
4361 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4362 struct CheckAddressExistsClosure caec;
4363 struct OwnAddressList *oal;
4365 GNUNET_assert (addr != NULL);
4367 GNUNET_STATISTICS_update (stats,
4368 gettext_noop ("# peer addresses scheduled for validation"),
4371 tp = find_transport (tname);
4374 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
4375 GNUNET_ERROR_TYPE_BULK,
4377 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
4379 GNUNET_STATISTICS_update (stats,
4380 gettext_noop ("# peer addresses not validated (plugin not available)"),
4385 /* check if this is one of our own addresses */
4386 oal = tp->addresses;
4389 if ( (oal->addrlen == addrlen) &&
4390 (0 == memcmp (&oal[1],
4394 /* not plausible, this address is equivalent to our own address! */
4395 GNUNET_STATISTICS_update (stats,
4396 gettext_noop ("# peer addresses not validated (loopback)"),
4403 GNUNET_HELLO_get_key (chvc->hello, &pk);
4404 GNUNET_CRYPTO_hash (&pk,
4406 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4409 if (is_blacklisted(&id, tp))
4412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4413 "Attempted to validate blacklisted peer `%s' using `%s'!\n",
4421 caec.addrlen = addrlen;
4422 caec.session = NULL;
4424 caec.exists = GNUNET_NO;
4425 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
4426 &check_address_exists,
4428 if (caec.exists == GNUNET_YES)
4430 /* During validation attempts we will likely trigger the other
4431 peer trying to validate our address which in turn will cause
4432 it to send us its HELLO, so we expect to hit this case rather
4433 frequently. Only print something if we are very verbose. */
4434 #if DEBUG_TRANSPORT > 1
4435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4436 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
4437 a2s (tname, addr, addrlen),
4441 GNUNET_STATISTICS_update (stats,
4442 gettext_noop ("# peer addresses not validated (in progress)"),
4447 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
4450 va->transport_name = GNUNET_strdup (tname);
4451 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
4453 va->send_time = GNUNET_TIME_absolute_get();
4454 va->addr = (const void*) &va[1];
4455 memcpy (&va[1], addr, addrlen);
4456 va->addrlen = addrlen;
4457 GNUNET_HELLO_get_key (chvc->hello,
4459 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
4460 &timeout_hello_validation,
4462 GNUNET_CONTAINER_multihashmap_put (validation_map,
4465 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4466 setup_peer_check_blacklist (&id, GNUNET_NO,
4467 &transmit_hello_and_ping,
4474 * Check if addresses in validated hello "h" overlap with
4475 * those in "chvc->hello" and validate the rest.
4477 * @param cls closure
4478 * @param peer id of the peer, NULL for last call
4479 * @param h hello message for the peer (can be NULL)
4480 * @param err_msg NULL if successful, otherwise contains error message
4483 check_hello_validated (void *cls,
4484 const struct GNUNET_PeerIdentity *peer,
4485 const struct GNUNET_HELLO_Message *h,
4486 const char *err_msg)
4488 struct CheckHelloValidatedContext *chvc = cls;
4489 struct GNUNET_HELLO_Message *plain_hello;
4490 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4491 struct GNUNET_PeerIdentity target;
4492 struct NeighbourList *n;
4494 if (err_msg != NULL)
4497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4498 _("Error in communication with PEERINFO service: %s\n"),
4506 GNUNET_STATISTICS_update (stats,
4507 gettext_noop ("# outstanding peerinfo iterate requests"),
4511 if (GNUNET_NO == chvc->hello_known)
4513 /* notify PEERINFO about the peer now, so that we at least
4514 have the public key if some other component needs it */
4515 GNUNET_HELLO_get_key (chvc->hello, &pk);
4516 GNUNET_CRYPTO_hash (&pk,
4517 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4518 &target.hashPubKey);
4519 plain_hello = GNUNET_HELLO_create (&pk,
4522 GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
4523 GNUNET_free (plain_hello);
4524 #if DEBUG_TRANSPORT_HELLO
4525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4526 "PEERINFO had no `%s' message for peer `%4s', full validation needed.\n",
4528 GNUNET_i2s (&target));
4530 GNUNET_STATISTICS_update (stats,
4531 gettext_noop ("# new HELLOs requiring full validation"),
4534 GNUNET_HELLO_iterate_addresses (chvc->hello,
4541 GNUNET_STATISTICS_update (stats,
4542 gettext_noop ("# duplicate HELLO (peer known)"),
4547 if (chvc->ve_count == 0)
4549 GNUNET_CONTAINER_DLL_remove (chvc_head,
4558 #if DEBUG_TRANSPORT_HELLO
4559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4560 "PEERINFO had `%s' message for peer `%4s', validating only new addresses.\n",
4564 chvc->hello_known = GNUNET_YES;
4565 n = find_neighbour (peer);
4568 #if DEBUG_TRANSPORT_HELLO
4569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4570 "Calling hello_iterate_addresses for %s!\n",
4573 GNUNET_HELLO_iterate_addresses (h,
4575 &add_to_foreign_address_list,
4577 try_transmission_to_peer (n);
4581 #if DEBUG_TRANSPORT_HELLO
4582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4583 "No existing neighbor record for %s!\n",
4586 GNUNET_STATISTICS_update (stats,
4587 gettext_noop ("# no existing neighbour record (validating HELLO)"),
4591 GNUNET_STATISTICS_update (stats,
4592 gettext_noop ("# HELLO validations (update case)"),
4595 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
4597 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
4604 * Process HELLO-message.
4606 * @param plugin transport involved, may be NULL
4607 * @param message the actual message
4608 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
4611 process_hello (struct TransportPlugin *plugin,
4612 const struct GNUNET_MessageHeader *message)
4615 struct GNUNET_PeerIdentity target;
4616 const struct GNUNET_HELLO_Message *hello;
4617 struct CheckHelloValidatedContext *chvc;
4618 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
4619 struct NeighbourList *n;
4620 #if DEBUG_TRANSPORT_HELLO > 2
4624 hsize = ntohs (message->size);
4625 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
4626 (hsize < sizeof (struct GNUNET_MessageHeader)))
4629 return GNUNET_SYSERR;
4631 GNUNET_STATISTICS_update (stats,
4632 gettext_noop ("# HELLOs received for validation"),
4636 hello = (const struct GNUNET_HELLO_Message *) message;
4637 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
4639 #if DEBUG_TRANSPORT_HELLO
4640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4641 "Unable to get public key from `%s' for `%4s'!\n",
4643 GNUNET_i2s (&target));
4645 GNUNET_break_op (0);
4646 return GNUNET_SYSERR;
4648 GNUNET_CRYPTO_hash (&publicKey,
4649 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4650 &target.hashPubKey);
4652 #if DEBUG_TRANSPORT_HELLO
4653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4654 "Received `%s' message for `%4s'\n",
4656 GNUNET_i2s (&target));
4658 if (0 == memcmp (&my_identity,
4660 sizeof (struct GNUNET_PeerIdentity)))
4662 GNUNET_STATISTICS_update (stats,
4663 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
4668 n = find_neighbour (&target);
4670 (! n->public_key_valid) )
4672 GNUNET_HELLO_get_key (hello, &n->publicKey);
4673 n->public_key_valid = GNUNET_YES;
4676 /* check if load is too high before doing expensive stuff */
4677 if (GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
4679 GNUNET_STATISTICS_update (stats,
4680 gettext_noop ("# HELLOs ignored due to high load"),
4683 #if DEBUG_TRANSPORT_HELLO
4684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4685 "Ignoring `%s' for `%4s', load too high.\n",
4687 GNUNET_i2s (&target));
4694 while (NULL != chvc)
4696 if (GNUNET_HELLO_equals (hello,
4698 GNUNET_TIME_absolute_get ()).abs_value > 0)
4700 #if DEBUG_TRANSPORT_HELLO > 2
4701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4702 "Received duplicate `%s' message for `%4s'; ignored\n",
4704 GNUNET_i2s (&target));
4706 return GNUNET_OK; /* validation already pending */
4708 if (GNUNET_HELLO_size (hello) == GNUNET_HELLO_size (chvc->hello))
4709 GNUNET_break (0 != memcmp (hello, chvc->hello,
4710 GNUNET_HELLO_size(hello)));
4715 struct NeighbourList *temp_neighbor = find_neighbour(&target);
4716 if ((NULL != temp_neighbor))
4718 fprintf(stderr, "Already know peer, ignoring hello\n");
4723 #if DEBUG_TRANSPORT_HELLO > 2
4726 my_id = GNUNET_strdup(GNUNET_i2s(plugin->env.my_identity));
4728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4729 "%s: Starting validation of `%s' message for `%4s' via '%s' of size %u\n",
4732 GNUNET_i2s (&target),
4734 GNUNET_HELLO_size(hello));
4739 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
4741 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
4742 memcpy (&chvc[1], hello, hsize);
4743 GNUNET_CONTAINER_DLL_insert (chvc_head,
4746 /* finally, check if HELLO was previously validated
4747 (continuation will then schedule actual validation) */
4748 GNUNET_STATISTICS_update (stats,
4749 gettext_noop ("# peerinfo process hello iterate requests"),
4752 GNUNET_STATISTICS_update (stats,
4753 gettext_noop ("# outstanding peerinfo iterate requests"),
4756 chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
4758 HELLO_VERIFICATION_TIMEOUT,
4759 &check_hello_validated, chvc);
4765 * The peer specified by the given neighbour has timed-out or a plugin
4766 * has disconnected. We may either need to do nothing (other plugins
4767 * still up), or trigger a full disconnect and clean up. This
4768 * function updates our state and does the necessary notifications.
4769 * Also notifies our clients that the neighbour is now officially
4772 * @param n the neighbour list entry for the peer
4773 * @param check GNUNET_YES to check if ALL addresses for this peer
4774 * are gone, GNUNET_NO to force a disconnect of the peer
4775 * regardless of whether other addresses exist.
4778 disconnect_neighbour (struct NeighbourList *n, int check)
4780 struct ReadyList *rpos;
4781 struct NeighbourList *npos;
4782 struct NeighbourList *nprev;
4783 struct MessageQueue *mq;
4784 struct ForeignAddressList *peer_addresses;
4785 struct ForeignAddressList *peer_pos;
4787 if (GNUNET_YES == check)
4790 while (NULL != rpos)
4792 peer_addresses = rpos->addresses;
4793 while (peer_addresses != NULL)
4795 // Do not disconnect if: an address is connected or an inbound address exists
4796 if ((GNUNET_YES == peer_addresses->connected) || (peer_addresses->addrlen == 0))
4799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4800 "NOT Disconnecting from `%4s', still have live address `%s'!\n",
4801 GNUNET_i2s (&n->id),
4802 a2s (peer_addresses->ready_list->plugin->short_name,
4803 peer_addresses->addr,
4804 peer_addresses->addrlen));
4806 return; /* still connected */
4808 peer_addresses = peer_addresses->next;
4814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4815 "Disconnecting from `%4s'\n",
4816 GNUNET_i2s (&n->id));
4819 /* remove n from neighbours list */
4822 while ((npos != NULL) && (npos != n))
4827 GNUNET_assert (npos != NULL);
4829 neighbours = n->next;
4831 nprev->next = n->next;
4833 /* notify all clients about disconnect */
4834 if (GNUNET_YES == n->received_pong)
4835 notify_clients_disconnect (&n->id);
4837 ats_modify_problem_state(ats, ATS_MODIFIED);
4839 /* clean up all plugins, cancel connections and pending transmissions */
4840 while (NULL != (rpos = n->plugins))
4842 n->plugins = rpos->next;
4843 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
4844 while (rpos->addresses != NULL)
4846 peer_pos = rpos->addresses;
4847 rpos->addresses = peer_pos->next;
4848 if (peer_pos->connected == GNUNET_YES)
4849 GNUNET_STATISTICS_update (stats,
4850 gettext_noop ("# connected addresses"),
4853 if (GNUNET_YES == peer_pos->validated)
4854 GNUNET_STATISTICS_update (stats,
4855 gettext_noop ("# peer addresses considered valid"),
4858 if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
4860 GNUNET_SCHEDULER_cancel (peer_pos->revalidate_task);
4861 peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
4863 GNUNET_free(peer_pos->ressources);
4864 peer_pos->ressources = NULL;
4865 GNUNET_free(peer_pos->quality);
4866 peer_pos->ressources = NULL;
4867 GNUNET_free(peer_pos);
4872 /* free all messages on the queue */
4873 while (NULL != (mq = n->messages_head))
4875 GNUNET_STATISTICS_update (stats,
4876 gettext_noop ("# bytes in message queue for other peers"),
4877 - (int64_t) mq->message_buf_size,
4879 GNUNET_STATISTICS_update (stats,
4880 gettext_noop ("# bytes discarded due to disconnect"),
4881 mq->message_buf_size,
4883 GNUNET_CONTAINER_DLL_remove (n->messages_head,
4886 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4888 sizeof(struct GNUNET_PeerIdentity)));
4892 while (NULL != (mq = n->cont_head))
4895 GNUNET_CONTAINER_DLL_remove (n->cont_head,
4898 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4900 sizeof(struct GNUNET_PeerIdentity)));
4904 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
4906 GNUNET_SCHEDULER_cancel (n->timeout_task);
4907 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4909 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4911 GNUNET_SCHEDULER_cancel (n->retry_task);
4912 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4914 if (n->piter != NULL)
4916 GNUNET_PEERINFO_iterate_cancel (n->piter);
4917 GNUNET_STATISTICS_update (stats,
4918 gettext_noop ("# outstanding peerinfo iterate requests"),
4923 /* finally, free n itself */
4924 GNUNET_STATISTICS_update (stats,
4925 gettext_noop ("# active neighbours"),
4928 GNUNET_free_non_null (n->pre_connect_message_buffer);
4934 * We have received a PING message from someone. Need to send a PONG message
4935 * in response to the peer by any means necessary.
4938 handle_ping (void *cls, const struct GNUNET_MessageHeader *message,
4939 const struct GNUNET_PeerIdentity *peer,
4940 struct Session *session,
4941 const char *sender_address,
4942 uint16_t sender_address_len)
4944 struct TransportPlugin *plugin = cls;
4945 struct SessionHeader *session_header = (struct SessionHeader*) session;
4946 struct TransportPingMessage *ping;
4947 struct TransportPongMessage *pong;
4948 struct NeighbourList *n;
4949 struct ReadyList *rl;
4950 struct ForeignAddressList *fal;
4951 struct OwnAddressList *oal;
4957 if (ntohs (message->size) < sizeof (struct TransportPingMessage))
4959 GNUNET_break_op (0);
4960 return GNUNET_SYSERR;
4963 ping = (struct TransportPingMessage *) message;
4964 if (0 != memcmp (&ping->target,
4965 plugin->env.my_identity,
4966 sizeof (struct GNUNET_PeerIdentity)))
4969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4970 _("Received `%s' message from `%s' destined for `%s' which is not me!\n"),
4972 (sender_address != NULL)
4973 ? a2s (plugin->short_name,
4974 (const struct sockaddr *)sender_address,
4977 GNUNET_i2s (&ping->target));
4979 return GNUNET_SYSERR;
4982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4983 "Processing `%s' from `%s'\n",
4985 (sender_address != NULL)
4986 ? a2s (plugin->short_name,
4987 (const struct sockaddr *)sender_address,
4991 GNUNET_STATISTICS_update (stats,
4992 gettext_noop ("# PING messages received"),
4995 addr = (const char*) &ping[1];
4996 alen = ntohs (message->size) - sizeof (struct TransportPingMessage);
4997 slen = strlen (plugin->short_name) + 1;
5000 /* peer wants to confirm that we have an outbound connection to him */
5001 if (session == NULL)
5003 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5004 _("Refusing to create PONG since I do not have a session with `%s'.\n"),
5006 return GNUNET_SYSERR;
5008 /* FIXME-urg: the use of 'sender_address' in the code below is doubly-wrong:
5009 1) it is NULL when we need to have a real value
5010 2) it is documented to be the address of the sender (source-IP), where
5011 what we actually want is our LISTEN IP (what we 'bound' to); which we don't even
5014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5015 "Creating PONG indicating that we received a connection at our address `%s' from `%s'.\n",
5016 a2s (plugin->short_name,
5018 sender_address_len),
5021 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len + slen);
5022 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len + slen);
5023 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
5024 pong->purpose.size =
5025 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
5027 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
5028 sizeof (struct GNUNET_PeerIdentity) + sender_address_len + slen);
5029 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING);
5030 pong->challenge = ping->challenge;
5031 pong->addrlen = htonl(sender_address_len + slen);
5034 sizeof(struct GNUNET_PeerIdentity));
5038 if ((sender_address!=NULL) && (sender_address_len > 0))
5039 memcpy (&((char*)&pong[1])[slen],
5041 sender_address_len);
5042 if (GNUNET_TIME_absolute_get_remaining (session_header->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
5044 /* create / update cached sig */
5046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5047 "Creating PONG signature to indicate active connection.\n");
5049 session_header->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
5050 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
5051 GNUNET_assert (GNUNET_OK ==
5052 GNUNET_CRYPTO_rsa_sign (my_private_key,
5054 &session_header->pong_signature));
5058 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
5060 memcpy (&pong->signature,
5061 &session_header->pong_signature,
5062 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5068 /* peer wants to confirm that this is one of our addresses */
5072 plugin->api->check_address (plugin->api->cls,
5076 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5077 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
5078 a2s (plugin->short_name,
5083 oal = plugin->addresses;
5086 if ( (oal->addrlen == alen) &&
5093 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
5094 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
5095 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
5096 pong->purpose.size =
5097 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
5099 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
5100 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
5101 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
5102 pong->challenge = ping->challenge;
5103 pong->addrlen = htonl(alen + slen);
5106 sizeof(struct GNUNET_PeerIdentity));
5107 memcpy (&pong[1], plugin->short_name, slen);
5108 memcpy (&((char*)&pong[1])[slen], addr, alen);
5109 if ( (oal != NULL) &&
5110 (GNUNET_TIME_absolute_get_remaining (oal->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4) )
5112 /* create / update cached sig */
5114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5115 "Creating PONG signature to indicate ownership.\n");
5117 oal->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
5118 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5119 GNUNET_assert (GNUNET_OK ==
5120 GNUNET_CRYPTO_rsa_sign (my_private_key,
5122 &oal->pong_signature));
5123 memcpy (&pong->signature,
5124 &oal->pong_signature,
5125 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5127 else if (oal == NULL)
5129 /* not using cache (typically DV-only) */
5130 pong->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
5131 GNUNET_assert (GNUNET_OK ==
5132 GNUNET_CRYPTO_rsa_sign (my_private_key,
5138 /* can used cached version */
5139 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5140 memcpy (&pong->signature,
5141 &oal->pong_signature,
5142 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5145 n = find_neighbour(peer);
5146 GNUNET_assert (n != NULL);
5147 did_pong = GNUNET_NO;
5148 /* first try reliable response transmission */
5152 fal = rl->addresses;
5155 if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
5158 ntohs (pong->header.size),
5159 TRANSPORT_PONG_PRIORITY,
5160 HELLO_VERIFICATION_TIMEOUT,
5167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5168 "Transmitted PONG to `%s' via reliable mechanism\n",
5171 GNUNET_STATISTICS_update (stats,
5172 gettext_noop ("# PONGs unicast via reliable transport"),
5178 did_pong = GNUNET_YES;
5183 /* no reliable method found, do multicast */
5184 GNUNET_STATISTICS_update (stats,
5185 gettext_noop ("# PONGs multicast to all available addresses"),
5191 fal = rl->addresses;
5194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5195 "Transmitting PONG to `%s' via unreliable mechanism `%s':%s\n",
5197 a2s (rl->plugin->short_name,
5200 rl->plugin->short_name);
5201 transmit_to_peer(NULL, fal,
5202 TRANSPORT_PONG_PRIORITY,
5203 HELLO_VERIFICATION_TIMEOUT,
5205 ntohs(pong->header.size),
5208 did_pong = GNUNET_YES;
5214 if (GNUNET_YES != did_pong)
5215 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5216 _("Could not send PONG to `%s': no address available\n"),
5223 * Function called by the plugin for each received message. Update
5224 * data volumes, possibly notify plugins about reducing the rate at
5225 * which they read from the socket and generally forward to our
5228 * @param cls the "struct TransportPlugin *" we gave to the plugin
5229 * @param peer (claimed) identity of the other peer
5230 * @param message the message, NULL if we only care about
5231 * learning about the delay until we should receive again
5232 * @param ats_data information for automatic transport selection
5233 * @param ats_count number of elements in ats not including 0-terminator
5234 * @param session identifier used for this session (can be NULL)
5235 * @param sender_address binary address of the sender (if observed)
5236 * @param sender_address_len number of bytes in sender_address
5237 * @return how long in ms the plugin should wait until receiving more data
5238 * (plugins that do not support this, can ignore the return value)
5240 static struct GNUNET_TIME_Relative
5241 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
5242 const struct GNUNET_MessageHeader *message,
5243 const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
5245 struct Session *session,
5246 const char *sender_address,
5247 uint16_t sender_address_len)
5249 struct TransportPlugin *plugin = cls;
5250 struct ReadyList *service_context;
5251 struct ForeignAddressList *peer_address;
5253 struct NeighbourList *n;
5254 struct GNUNET_TIME_Relative ret;
5258 if (0 == memcmp (peer,
5260 sizeof (struct GNUNET_PeerIdentity)))
5262 /* refuse to receive from myself */
5264 return GNUNET_TIME_UNIT_FOREVER_REL;
5266 if (is_blacklisted (peer, plugin))
5267 return GNUNET_TIME_UNIT_FOREVER_REL;
5268 n = find_neighbour (peer);
5270 n = setup_new_neighbour (peer, GNUNET_YES);
5271 service_context = n->plugins;
5272 while ((service_context != NULL) && (plugin != service_context->plugin))
5273 service_context = service_context->next;
5274 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
5275 peer_address = NULL;
5278 for (c=0; c<ats_count; c++)
5279 if (ntohl(ats_data[c].type) == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
5280 distance = ntohl(ats_data[c].value);
5283 if (message != NULL)
5285 if ( (session != NULL) ||
5286 (sender_address != NULL) )
5287 peer_address = add_peer_address (n,
5291 sender_address_len);
5292 if (peer_address != NULL)
5294 update_addr_ats(peer_address, ats_data, ats_count);
5295 update_addr_value(peer_address, distance, GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5297 peer_address->distance = distance;
5298 if (GNUNET_YES == peer_address->validated)
5299 mark_address_connected (peer_address);
5300 peer_address->timeout
5301 = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5302 schedule_next_ping (peer_address);
5304 /* update traffic received amount ... */
5305 msize = ntohs (message->size);
5307 GNUNET_STATISTICS_update (stats,
5308 gettext_noop ("# bytes received from other peers"),
5311 n->distance = distance;
5313 GNUNET_TIME_relative_to_absolute
5314 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5315 GNUNET_SCHEDULER_cancel (n->timeout_task);
5317 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
5318 &neighbour_timeout_task, n);
5319 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
5321 /* dropping message due to frequent inbound volume violations! */
5322 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
5323 GNUNET_ERROR_TYPE_BULK,
5325 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
5326 n->in_tracker.available_bytes_per_s__,
5327 n->quota_violation_count);
5328 GNUNET_STATISTICS_update (stats,
5329 gettext_noop ("# bandwidth quota violations by other peers"),
5332 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
5334 if ((ntohs(message->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ATS) &&
5335 (ntohs(message->size) == (sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t))))
5337 uint32_t value = ntohl(*((uint32_t *) &message[1]));
5338 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GNUNET_MESSAGE_TYPE_TRANSPORT_ATS: %i \n", value);
5339 /* Force ressource and quality update */
5340 if ((value == 4) && (ats != NULL))
5341 ats_modify_problem_state(ats, ATS_QUALITY_COST_UPDATED);
5342 /* Force cost update */
5343 if ((value == 3) && (ats != NULL))
5344 ats_modify_problem_state(ats, ATS_COST_UPDATED);
5345 /* Force quality update */
5346 if ((value == 2) && (ats != NULL))
5347 ats_modify_problem_state(ats, ATS_QUALITY_UPDATED);
5348 /* Force full rebuild */
5349 if ((value == 1) && (ats != NULL))
5350 ats_modify_problem_state(ats, ATS_MODIFIED);
5354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5355 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
5356 ntohs (message->type),
5357 ntohs (message->size),
5360 switch (ntohs (message->type))
5362 case GNUNET_MESSAGE_TYPE_HELLO:
5363 GNUNET_STATISTICS_update (stats,
5364 gettext_noop ("# HELLO messages received from other peers"),
5367 process_hello (plugin, message);
5369 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
5370 handle_ping (plugin, message, peer, session, sender_address, sender_address_len);
5371 if (! n->received_pong)
5372 transmit_plain_ping (n);
5374 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
5375 handle_pong (plugin, message, peer, sender_address, sender_address_len);
5377 case GNUNET_MESSAGE_TYPE_TRANSPORT_ATS:
5380 handle_payload_message (message, n);
5384 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
5385 if (ret.rel_value > 0)
5388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5389 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
5390 (unsigned long long) n->in_tracker.consumption_since_last_update__,
5391 (unsigned int) n->in_tracker.available_bytes_per_s__,
5392 (unsigned long long) ret.rel_value);
5394 GNUNET_STATISTICS_update (stats,
5395 gettext_noop ("# ms throttling suggested"),
5396 (int64_t) ret.rel_value,
5403 * Handle START-message. This is the first message sent to us
5404 * by any client which causes us to add it to our list.
5406 * @param cls closure (always NULL)
5407 * @param client identification of the client
5408 * @param message the actual message
5411 handle_start (void *cls,
5412 struct GNUNET_SERVER_Client *client,
5413 const struct GNUNET_MessageHeader *message)
5415 const struct StartMessage *start;
5416 struct TransportClient *c;
5417 struct ConnectInfoMessage * cim;
5418 struct NeighbourList *n;
5422 start = (const struct StartMessage*) message;
5424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5425 "Received `%s' request from client\n", "START");
5430 if (c->client == client)
5432 /* client already on our list! */
5434 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5439 if ( (GNUNET_NO != ntohl (start->do_check)) &&
5440 (0 != memcmp (&start->self,
5442 sizeof (struct GNUNET_PeerIdentity))) )
5444 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5445 _("Rejecting control connection from peer `%s', which is not me!\n"),
5446 GNUNET_i2s (&start->self));
5447 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5450 c = GNUNET_malloc (sizeof (struct TransportClient));
5454 if (our_hello != NULL)
5457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5458 "Sending our own `%s' to new client\n", "HELLO");
5460 transmit_to_client (c,
5461 (const struct GNUNET_MessageHeader *) our_hello,
5463 /* tell new client about all existing connections */
5465 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
5466 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
5470 cim = GNUNET_malloc (size);
5471 cim->header.size = htons (size);
5472 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
5473 cim->ats_count = htonl(ats_count);
5474 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
5475 (&(cim->ats))[2].value = htonl (0);
5479 if (GNUNET_YES == n->received_pong)
5481 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5482 (&(cim->ats))[0].value = htonl (n->distance);
5483 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
5484 (&(cim->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
5486 transmit_to_client (c, &cim->header, GNUNET_NO);
5494 #if DEBUG_TRANSPORT_HELLO
5495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5496 "No HELLO created yet, will transmit HELLO to client later!\n");
5500 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5505 * Handle HELLO-message.
5507 * @param cls closure (always NULL)
5508 * @param client identification of the client
5509 * @param message the actual message
5512 handle_hello (void *cls,
5513 struct GNUNET_SERVER_Client *client,
5514 const struct GNUNET_MessageHeader *message)
5518 GNUNET_STATISTICS_update (stats,
5519 gettext_noop ("# HELLOs received from clients"),
5522 ret = process_hello (NULL, message);
5523 GNUNET_SERVER_receive_done (client, ret);
5528 * Closure for 'transmit_client_message'; followed by
5529 * 'msize' bytes of the actual message.
5531 struct TransmitClientMessageContext
5534 * Client on whom's behalf we are sending.
5536 struct GNUNET_SERVER_Client *client;
5539 * Timeout for the transmission.
5541 struct GNUNET_TIME_Absolute timeout;
5549 * Size of the message in bytes.
5556 * Schedule transmission of a message we got from a client to a peer.
5558 * @param cls the 'struct TransmitClientMessageContext*'
5559 * @param n destination, or NULL on error (in that case, drop the message)
5562 transmit_client_message (void *cls,
5563 struct NeighbourList *n)
5565 struct TransmitClientMessageContext *tcmc = cls;
5566 struct TransportClient *tc;
5569 while ((tc != NULL) && (tc->client != tcmc->client))
5574 transmit_to_peer (tc, NULL, tcmc->priority,
5575 GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
5577 tcmc->msize, GNUNET_NO, n);
5579 GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
5580 GNUNET_SERVER_client_drop (tcmc->client);
5586 * Handle SEND-message.
5588 * @param cls closure (always NULL)
5589 * @param client identification of the client
5590 * @param message the actual message
5593 handle_send (void *cls,
5594 struct GNUNET_SERVER_Client *client,
5595 const struct GNUNET_MessageHeader *message)
5597 const struct OutboundMessage *obm;
5598 const struct GNUNET_MessageHeader *obmm;
5599 struct TransmitClientMessageContext *tcmc;
5603 size = ntohs (message->size);
5605 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
5608 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5611 GNUNET_STATISTICS_update (stats,
5612 gettext_noop ("# payload received for other peers"),
5615 obm = (const struct OutboundMessage *) message;
5616 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
5617 msize = size - sizeof (struct OutboundMessage);
5619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5620 "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
5621 "SEND", GNUNET_i2s (&obm->peer),
5625 tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
5626 tcmc->client = client;
5627 tcmc->priority = ntohl (obm->priority);
5628 tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
5629 tcmc->msize = msize;
5630 /* FIXME: this memcpy can be up to 7% of our total runtime */
5631 memcpy (&tcmc[1], obmm, msize);
5632 GNUNET_SERVER_client_keep (client);
5633 setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
5634 &transmit_client_message,
5640 * Handle request connect message
5642 * @param cls closure (always NULL)
5643 * @param client identification of the client
5644 * @param message the actual message
5647 handle_request_connect (void *cls,
5648 struct GNUNET_SERVER_Client *client,
5649 const struct GNUNET_MessageHeader *message)
5651 const struct TransportRequestConnectMessage *trcm =
5652 (const struct TransportRequestConnectMessage *) message;
5654 GNUNET_STATISTICS_update (stats,
5655 gettext_noop ("# REQUEST CONNECT messages received"),
5659 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
5660 "Received a request connect message for peer `%s'\n",
5661 GNUNET_i2s(&trcm->peer));
5663 setup_peer_check_blacklist (&trcm->peer, GNUNET_YES,
5665 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5670 * Handle SET_QUOTA-message.
5672 * @param cls closure (always NULL)
5673 * @param client identification of the client
5674 * @param message the actual message
5677 handle_set_quota (void *cls,
5678 struct GNUNET_SERVER_Client *client,
5679 const struct GNUNET_MessageHeader *message)
5681 const struct QuotaSetMessage *qsm =
5682 (const struct QuotaSetMessage *) message;
5683 struct NeighbourList *n;
5685 GNUNET_STATISTICS_update (stats,
5686 gettext_noop ("# SET QUOTA messages received"),
5689 n = find_neighbour (&qsm->peer);
5692 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5693 GNUNET_STATISTICS_update (stats,
5694 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
5700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5701 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
5703 (unsigned int) ntohl (qsm->quota.value__),
5704 (unsigned int) n->in_tracker.available_bytes_per_s__,
5705 GNUNET_i2s (&qsm->peer));
5707 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
5709 if (0 == ntohl (qsm->quota.value__))
5712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5713 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&n->id),
5716 GNUNET_STATISTICS_update (stats,
5717 gettext_noop ("# disconnects due to quota of 0"),
5720 disconnect_neighbour (n, GNUNET_NO);
5722 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5727 * Take the given address and append it to the set of results sent back to
5730 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
5731 * @param address the resolved name, NULL to indicate the last response
5734 transmit_address_to_client (void *cls, const char *address)
5736 struct GNUNET_SERVER_TransmitContext *tc = cls;
5739 if (NULL != address)
5741 slen = strlen (address) + 1;
5742 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
5743 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5747 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
5753 * Handle AddressLookup-message.
5755 * @param cls closure (always NULL)
5756 * @param client identification of the client
5757 * @param message the actual message
5760 handle_address_lookup (void *cls,
5761 struct GNUNET_SERVER_Client *client,
5762 const struct GNUNET_MessageHeader *message)
5764 const struct AddressLookupMessage *alum;
5765 struct TransportPlugin *lsPlugin;
5766 const char *nameTransport;
5767 const char *address;
5769 struct GNUNET_SERVER_TransmitContext *tc;
5770 struct GNUNET_TIME_Absolute timeout;
5771 struct GNUNET_TIME_Relative rtimeout;
5774 size = ntohs (message->size);
5775 if (size < sizeof (struct AddressLookupMessage))
5777 GNUNET_break_op (0);
5778 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5781 alum = (const struct AddressLookupMessage *) message;
5782 uint32_t addressLen = ntohl (alum->addrlen);
5783 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
5785 GNUNET_break_op (0);
5786 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5789 address = (const char *) &alum[1];
5790 nameTransport = (const char *) &address[addressLen];
5792 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
5794 GNUNET_break_op (0);
5795 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5798 timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
5799 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5800 numeric = ntohl (alum->numeric_only);
5801 lsPlugin = find_transport (nameTransport);
5802 if (NULL == lsPlugin)
5804 tc = GNUNET_SERVER_transmit_context_create (client);
5805 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5806 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5807 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
5810 GNUNET_SERVER_disable_receive_done_warning (client);
5811 tc = GNUNET_SERVER_transmit_context_create (client);
5812 lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
5814 address, addressLen,
5817 &transmit_address_to_client, tc);
5821 * Handle PeerAddressLookupMessage.
5823 * @param cls closure (always NULL)
5824 * @param client identification of the client
5825 * @param message the actual message
5828 handle_peer_address_lookup (void *cls,
5829 struct GNUNET_SERVER_Client *client,
5830 const struct GNUNET_MessageHeader *message)
5832 const struct PeerAddressLookupMessage *peer_address_lookup;
5833 struct NeighbourList *neighbor_iterator;
5834 struct ReadyList *ready_iterator;
5835 struct ForeignAddressList *foreign_address_iterator;
5836 struct TransportPlugin *transport_plugin;
5839 struct GNUNET_SERVER_TransmitContext *tc;
5840 struct GNUNET_TIME_Absolute timeout;
5841 struct GNUNET_TIME_Relative rtimeout;
5844 size = ntohs (message->size);
5845 if (size < sizeof (struct PeerAddressLookupMessage))
5847 GNUNET_break_op (0);
5848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5851 peer_address_lookup = (const struct PeerAddressLookupMessage *) message;
5853 timeout = GNUNET_TIME_absolute_ntoh (peer_address_lookup->timeout);
5854 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5856 neighbor_iterator = neighbours;
5857 while (neighbor_iterator != NULL)
5859 if (0 == memcmp(&neighbor_iterator->id, &peer_address_lookup->peer, sizeof(struct GNUNET_PeerIdentity)))
5861 neighbor_iterator = neighbor_iterator->next;
5864 /* Found no neighbor matching this peer id (shouldn't be possible, but...) */
5865 if (neighbor_iterator == NULL)
5868 tc = GNUNET_SERVER_transmit_context_create (client);
5869 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5870 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5871 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
5875 ready_iterator = neighbor_iterator->plugins;
5876 GNUNET_SERVER_disable_receive_done_warning (client);
5877 tc = GNUNET_SERVER_transmit_context_create (client);
5878 while(ready_iterator != NULL)
5880 foreign_address_iterator = ready_iterator->addresses;
5881 while (foreign_address_iterator != NULL)
5883 transport_plugin = foreign_address_iterator->ready_list->plugin;
5884 if (foreign_address_iterator->addr != NULL)
5886 GNUNET_asprintf (&addr_buf, "%s --- %s",
5887 a2s (transport_plugin->short_name,
5888 foreign_address_iterator->addr,
5889 foreign_address_iterator->addrlen),
5890 (foreign_address_iterator->connected
5891 == GNUNET_YES) ? "CONNECTED"
5893 transmit_address_to_client(tc, addr_buf);
5894 GNUNET_free(addr_buf);
5896 else if (foreign_address_iterator->addrlen == 0)
5898 GNUNET_asprintf (&addr_buf, "%s --- %s", "<inbound>",
5899 (foreign_address_iterator->connected
5900 == GNUNET_YES) ? "CONNECTED"
5902 transmit_address_to_client (tc, addr_buf);
5903 GNUNET_free(addr_buf);
5906 foreign_address_iterator = foreign_address_iterator->next;
5908 ready_iterator = ready_iterator->next;
5910 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5911 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5912 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
5916 * Handle AddressIterateMessage
5918 * @param cls closure (always NULL)
5919 * @param client identification of the client
5920 * @param message the actual message
5923 handle_address_iterate (void *cls,
5924 struct GNUNET_SERVER_Client *client,
5925 const struct GNUNET_MessageHeader *message)
5927 const struct AddressIterateMessage *address_iterate;
5928 struct NeighbourList *neighbor_iterator;
5929 struct ReadyList *ready_iterator;
5930 struct ForeignAddressList *foreign_address_iterator;
5931 struct TransportPlugin *transport_plugin;
5934 struct GNUNET_SERVER_TransmitContext *tc;
5935 struct GNUNET_TIME_Absolute timeout;
5936 struct GNUNET_TIME_Relative rtimeout;
5939 size = ntohs (message->size);
5940 if (size < sizeof (struct AddressIterateMessage))
5942 GNUNET_break_op (0);
5943 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5946 address_iterate = (const struct AddressIterateMessage *) message;
5948 timeout = GNUNET_TIME_absolute_ntoh (address_iterate->timeout);
5949 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5951 GNUNET_SERVER_disable_receive_done_warning (client);
5952 tc = GNUNET_SERVER_transmit_context_create (client);
5954 neighbor_iterator = neighbours;
5955 while (neighbor_iterator != NULL)
5957 ready_iterator = neighbor_iterator->plugins;
5958 while (ready_iterator != NULL)
5960 foreign_address_iterator = ready_iterator->addresses;
5961 while (foreign_address_iterator != NULL)
5963 transport_plugin = foreign_address_iterator->ready_list->plugin;
5964 if (foreign_address_iterator->addr != NULL)
5966 GNUNET_asprintf (&addr_buf, "%s:%s --- %s",
5967 GNUNET_i2s(&neighbor_iterator->id),
5968 a2s (transport_plugin->short_name,
5969 foreign_address_iterator->addr,
5970 foreign_address_iterator->addrlen),
5971 (foreign_address_iterator->connected
5972 == GNUNET_YES) ? "CONNECTED"
5974 transmit_address_to_client (tc, addr_buf);
5975 GNUNET_free(addr_buf);
5977 else if (foreign_address_iterator->addrlen == 0)
5979 GNUNET_asprintf (&addr_buf, "%s:%s --- %s",
5980 GNUNET_i2s (&neighbor_iterator->id),
5982 (foreign_address_iterator->connected
5983 == GNUNET_YES) ? "CONNECTED"
5985 transmit_address_to_client (tc, addr_buf);
5986 GNUNET_free(addr_buf);
5989 foreign_address_iterator = foreign_address_iterator->next;
5991 ready_iterator = ready_iterator->next;
5993 neighbor_iterator = neighbor_iterator->next;
5996 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5997 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5998 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
6003 * Setup the environment for this plugin.
6006 create_environment (struct TransportPlugin *plug)
6008 plug->env.cfg = cfg;
6009 plug->env.my_identity = &my_identity;
6010 plug->env.our_hello = &our_hello;
6011 plug->env.cls = plug;
6012 plug->env.receive = &plugin_env_receive;
6013 plug->env.notify_address = &plugin_env_notify_address;
6014 plug->env.session_end = &plugin_env_session_end;
6015 plug->env.max_connections = max_connect_per_transport;
6016 plug->env.stats = stats;
6021 * Start the specified transport (load the plugin).
6024 start_transport (struct GNUNET_SERVER_Handle *server,
6027 struct TransportPlugin *plug;
6030 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6031 _("Loading `%s' transport plugin\n"), name);
6032 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
6033 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
6034 create_environment (plug);
6035 plug->short_name = GNUNET_strdup (name);
6036 plug->lib_name = libname;
6037 plug->next = plugins;
6039 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
6040 if (plug->api == NULL)
6042 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6043 _("Failed to load transport plugin for `%s'\n"), name);
6044 GNUNET_free (plug->short_name);
6045 plugins = plug->next;
6046 GNUNET_free (libname);
6053 * Called whenever a client is disconnected. Frees our
6054 * resources associated with that client.
6056 * @param cls closure
6057 * @param client identification of the client
6060 client_disconnect_notification (void *cls,
6061 struct GNUNET_SERVER_Client *client)
6063 struct TransportClient *pos;
6064 struct TransportClient *prev;
6065 struct ClientMessageQueueEntry *mqe;
6066 struct Blacklisters *bl;
6067 struct BlacklistCheck *bc;
6072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
6073 "Client disconnected, cleaning up.\n");
6075 /* clean up blacklister */
6079 if (bl->client == client)
6084 if (bc->bl_pos == bl)
6086 bc->bl_pos = bl->next;
6089 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
6092 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
6093 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
6099 GNUNET_CONTAINER_DLL_remove (bl_head,
6102 GNUNET_SERVER_client_drop (bl->client);
6108 /* clean up 'normal' clients */
6111 while ((pos != NULL) && (pos->client != client))
6118 while (NULL != (mqe = pos->message_queue_head))
6120 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
6121 pos->message_queue_tail,
6123 pos->message_count--;
6127 clients = pos->next;
6129 prev->next = pos->next;
6130 if (GNUNET_YES == pos->tcs_pending)
6135 if (pos->th != NULL)
6137 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
6140 GNUNET_break (0 == pos->message_count);
6146 * Function called when the service shuts down. Unloads our plugins
6147 * and cancels pending validations.
6149 * @param cls closure, unused
6150 * @param tc task context (unused)
6153 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6155 struct TransportPlugin *plug;
6156 struct TransportPlugin *tmp;
6157 struct OwnAddressList *al;
6158 struct CheckHelloValidatedContext *chvc;
6160 shutdown_in_progress = GNUNET_YES;
6161 while (neighbours != NULL)
6164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6165 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&neighbours->id),
6168 disconnect_neighbour (neighbours, GNUNET_NO);
6171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6172 "Transport service is unloading plugins...\n");
6175 while (plug != NULL)
6177 if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
6179 GNUNET_SCHEDULER_cancel (plug->address_update_task);
6180 plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
6182 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
6183 GNUNET_free (plug->lib_name);
6184 GNUNET_free (plug->short_name);
6185 while (NULL != (al = plug->addresses))
6187 plug->addresses = al->next;
6194 if (my_private_key != NULL)
6195 GNUNET_CRYPTO_rsa_key_free (my_private_key);
6196 GNUNET_free_non_null (our_hello);
6198 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
6201 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6202 validation_map = NULL;
6205 if (ats_task != GNUNET_SCHEDULER_NO_TASK)
6207 GNUNET_SCHEDULER_cancel(ats_task);
6208 ats_task = GNUNET_SCHEDULER_NO_TASK;
6213 /* free 'chvc' data structure */
6214 while (NULL != (chvc = chvc_head))
6216 chvc_head = chvc->next;
6217 if (chvc->piter != NULL)
6219 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
6220 GNUNET_STATISTICS_update (stats,
6221 gettext_noop ("# outstanding peerinfo iterate requests"),
6228 GNUNET_assert (chvc->ve_count == 0);
6235 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6238 if (peerinfo != NULL)
6240 GNUNET_PEERINFO_disconnect (peerinfo);
6243 if (GNUNET_SCHEDULER_NO_TASK != hello_task)
6245 GNUNET_SCHEDULER_cancel (hello_task);
6246 hello_task = GNUNET_SCHEDULER_NO_TASK;
6248 /* Can we assume those are gone by now, or do we need to clean up
6250 GNUNET_break (bl_head == NULL);
6251 GNUNET_break (bc_head == NULL);
6255 void ats_result_cb ()
6257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6258 "ATS Result callback\n");
6262 void create_ats_information ( struct ATS_peer **p,
6264 struct ATS_mechanism ** m,
6268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6269 "ATS requires clean address information\n");
6271 struct ATS_mechanism * mechanisms;
6272 struct ATS_peer *peers;
6274 int connected_addresses = 0;
6277 struct NeighbourList *next = neighbours;
6281 int found_addresses = GNUNET_NO;
6282 struct ReadyList *r_next = next->plugins;
6283 while (r_next != NULL)
6285 struct ForeignAddressList * a_next = r_next->addresses;
6286 while (a_next != NULL)
6289 found_addresses = GNUNET_YES;
6290 a_next = a_next->next;
6292 r_next = r_next->next;
6294 if (found_addresses) c_peers++;
6299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6300 "Found %u peers with % u transport mechanisms\n", c_peers, c_mechs);
6303 if ((c_peers == 0) && (c_mechs == 0))
6312 mechanisms = GNUNET_malloc((1+c_mechs) * sizeof (struct ATS_mechanism));
6313 peers = GNUNET_malloc((1+c_peers) * sizeof (struct ATS_peer));
6321 int found_addresses = GNUNET_NO;
6322 struct ReadyList *r_next = next->plugins;
6323 while (r_next != NULL)
6325 struct ForeignAddressList * a_next = r_next->addresses;
6326 while (a_next != NULL)
6328 if (a_next->connected == GNUNET_YES)
6329 connected_addresses ++;
6330 if (found_addresses == GNUNET_NO)
6332 peers[c_peers].peer = next->id;
6333 peers[c_peers].m_head = NULL;
6334 peers[c_peers].m_tail = NULL;
6335 peers[c_peers].f = 1.0 / c_mechs;
6338 mechanisms[c_mechs].addr = a_next;
6339 mechanisms[c_mechs].col_index = c_mechs;
6340 mechanisms[c_mechs].peer = &peers[c_peers];
6341 mechanisms[c_mechs].next = NULL;
6342 mechanisms[c_mechs].plugin = r_next->plugin;
6343 mechanisms[c_mechs].ressources = a_next->ressources;
6344 mechanisms[c_mechs].quality = a_next->quality;
6346 GNUNET_CONTAINER_DLL_insert_tail(peers[c_peers].m_head,
6347 peers[c_peers].m_tail,
6348 &mechanisms[c_mechs]);
6349 found_addresses = GNUNET_YES;
6352 a_next = a_next->next;
6354 r_next = r_next->next;
6356 if (found_addresses == GNUNET_YES)
6367 GNUNET_STATISTICS_set(stats,
6368 gettext_noop ("# connected addresses"),
6369 connected_addresses,
6374 schedule_ats (void *cls,
6375 const struct GNUNET_SCHEDULER_TaskContext *tc)
6377 struct ATS_Handle *ats = (struct ATS_Handle *) cls;
6381 ats_task = GNUNET_SCHEDULER_NO_TASK;
6382 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
6385 if (shutdown_in_progress == GNUNET_YES)
6388 struct GNUNET_TIME_Relative delta =
6389 GNUNET_TIME_absolute_get_difference (last_ats_execution, GNUNET_TIME_absolute_get());
6390 if (delta.rel_value < ats_minimum_interval.rel_value)
6393 GNUNET_log (GNUNET_ERROR_TYPE_BULK,
6394 "Minimum time between cycles not reached\n");
6400 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running scheduled calculation\n");
6402 ats_calculate_bandwidth_distribution (ats, stats);
6403 last_ats_execution = GNUNET_TIME_absolute_get();
6405 ats_task = GNUNET_SCHEDULER_add_delayed (ats_regular_interval,
6406 &schedule_ats, ats);
6409 struct ForeignAddressList * get_preferred_ats_address (
6410 struct NeighbourList *n)
6412 // TODO get ATS prefered address
6413 return find_ready_address(n);
6417 * Initiate transport service.
6419 * @param cls closure
6420 * @param server the initialized server
6421 * @param c configuration to use
6425 struct GNUNET_SERVER_Handle *server,
6426 const struct GNUNET_CONFIGURATION_Handle *c)
6428 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
6429 {&handle_start, NULL,
6430 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
6431 {&handle_hello, NULL,
6432 GNUNET_MESSAGE_TYPE_HELLO, 0},
6433 {&handle_send, NULL,
6434 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
6435 {&handle_request_connect, NULL,
6436 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT, sizeof(struct TransportRequestConnectMessage)},
6437 {&handle_set_quota, NULL,
6438 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
6439 {&handle_address_lookup, NULL,
6440 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
6442 {&handle_peer_address_lookup, NULL,
6443 GNUNET_MESSAGE_TYPE_TRANSPORT_PEER_ADDRESS_LOOKUP,
6445 {&handle_address_iterate, NULL,
6446 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE,
6448 {&handle_blacklist_init, NULL,
6449 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
6450 {&handle_blacklist_reply, NULL,
6451 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
6457 unsigned long long tneigh;
6460 shutdown_in_progress = GNUNET_NO;
6462 stats = GNUNET_STATISTICS_create ("transport", cfg);
6463 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
6464 /* parse configuration */
6466 GNUNET_CONFIGURATION_get_value_number (c,
6471 GNUNET_CONFIGURATION_get_value_filename (c,
6473 "HOSTKEY", &keyfile)))
6475 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6477 ("Transport service is lacking key configuration settings. Exiting.\n"));
6478 GNUNET_SCHEDULER_shutdown ();
6481 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6484 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6485 validation_map = NULL;
6489 max_connect_per_transport = (uint32_t) tneigh;
6490 peerinfo = GNUNET_PEERINFO_connect (cfg);
6491 if (peerinfo == NULL)
6493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6494 _("Could not access PEERINFO service. Exiting.\n"));
6495 GNUNET_SCHEDULER_shutdown ();
6498 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6501 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6502 validation_map = NULL;
6503 GNUNET_free (keyfile);
6506 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6507 GNUNET_free (keyfile);
6508 if (my_private_key == NULL)
6510 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6512 ("Transport service could not access hostkey. Exiting.\n"));
6513 GNUNET_SCHEDULER_shutdown ();
6516 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6519 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6520 validation_map = NULL;
6523 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6524 GNUNET_CRYPTO_hash (&my_public_key,
6525 sizeof (my_public_key), &my_identity.hashPubKey);
6526 /* setup notification */
6527 GNUNET_SERVER_disconnect_notify (server,
6528 &client_disconnect_notification, NULL);
6529 /* load plugins... */
6532 GNUNET_CONFIGURATION_get_value_string (c,
6533 "TRANSPORT", "PLUGINS", &plugs))
6535 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6536 _("Starting transport plugins `%s'\n"), plugs);
6537 pos = strtok (plugs, " ");
6540 start_transport (server, pos);
6542 pos = strtok (NULL, " ");
6544 GNUNET_free (plugs);
6546 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
6547 &shutdown_task, NULL);
6551 /* Initializing ATS */
6554 unsigned long long value;
6559 int v_b_min = 64000;
6562 ats_minimum_interval = ATS_MIN_INTERVAL;
6563 ats_regular_interval = ATS_EXEC_INTERVAL;
6565 /* loading cost ressources */
6566 for (co=0; co<available_ressources; co++)
6568 GNUNET_asprintf(§ion,"%s_UP",ressources[co].cfg_param);
6569 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
6571 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg,
6577 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6578 "Found ressource cost: [%s] = %llu\n",
6581 ressources[co].c_max = value;
6584 GNUNET_free (section);
6585 GNUNET_asprintf(§ion,"%s_DOWN",ressources[co].cfg_param);
6586 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
6588 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg,
6594 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6595 "Found ressource cost: [%s] = %llu\n",
6598 ressources[co].c_min = value;
6601 GNUNET_free (section);
6604 ats = ats_init (D, U, R, v_b_min, v_n_min,
6605 ATS_MAX_ITERATIONS, ATS_MAX_EXEC_DURATION,
6606 create_ats_information,
6609 int log_problem = GNUNET_NO;
6610 int log_solution = GNUNET_NO;
6611 int overwrite_dump = GNUNET_NO;
6612 int minimum_peers = 0;
6613 int minimum_addresses = 0;
6615 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_MLP"))
6616 log_problem = GNUNET_CONFIGURATION_get_value_yesno (cfg,
6617 "transport","DUMP_MLP");
6619 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_SOLUTION"))
6620 log_solution = GNUNET_CONFIGURATION_get_value_yesno (cfg,
6621 "transport","DUMP_SOLUTION");
6622 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_OVERWRITE"))
6623 overwrite_dump = GNUNET_CONFIGURATION_get_value_yesno (cfg,
6624 "transport","DUMP_OVERWRITE");
6625 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_MIN_PEERS"))
6627 GNUNET_CONFIGURATION_get_value_number(cfg,
6628 "transport","DUMP_MIN_PEERS", &value);
6629 minimum_peers = value;
6631 if (GNUNET_CONFIGURATION_have_value(cfg,
6632 "transport", "DUMP_MIN_ADDRS"))
6634 GNUNET_CONFIGURATION_get_value_number(cfg,
6635 "transport","DUMP_MIN_ADDRS", &value);
6636 minimum_addresses= value;
6638 if (GNUNET_CONFIGURATION_have_value(cfg,
6639 "transport", "DUMP_OVERWRITE"))
6641 GNUNET_CONFIGURATION_get_value_number(cfg,
6642 "transport","DUMP_OVERWRITE", &value);
6643 overwrite_dump = value;
6646 if (GNUNET_CONFIGURATION_have_value(cfg,
6647 "transport", "ATS_MIN_INTERVAL"))
6649 GNUNET_CONFIGURATION_get_value_number(cfg,
6650 "transport","ATS_MIN_INTERVAL", &value);
6651 ats_minimum_interval.rel_value = value;
6654 if (GNUNET_CONFIGURATION_have_value(cfg,
6655 "transport", "ATS_EXEC_INTERVAL"))
6657 GNUNET_CONFIGURATION_get_value_number(cfg,
6658 "transport","ATS_EXEC_INTERVAL", &value);
6659 ats_regular_interval.rel_value = value;
6661 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "ATS_MIN_INTERVAL"))
6663 GNUNET_CONFIGURATION_get_value_number(cfg,
6664 "transport","ATS_MIN_INTERVAL", &value);
6665 ats_minimum_interval.rel_value = value;
6668 ats_set_logging_options (ats,
6676 ats_task = GNUNET_SCHEDULER_add_now (&schedule_ats, ats);
6682 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6683 _("Transport service ready.\n"));
6685 /* If we have a blacklist file, read from it */
6686 read_blacklist_file(cfg);
6687 /* process client requests */
6688 GNUNET_SERVER_add_handlers (server, handlers);
6693 * The main function for the transport service.
6695 * @param argc number of arguments from the command line
6696 * @param argv command line arguments
6697 * @return 0 ok, 1 on error
6700 main (int argc, char *const *argv)
6702 a2s (NULL, NULL, 0); /* make compiler happy */
6703 return (GNUNET_OK ==
6704 GNUNET_SERVICE_run (argc,
6707 GNUNET_SERVICE_OPTION_NONE,
6708 &run, NULL)) ? 0 : 1;
6711 /* end of gnunet-service-transport.c */