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
292 * This is a linked list.
294 struct TransportPlugin *next;
297 * API of the transport as returned by the plugin's
298 * initialization function.
300 struct GNUNET_TRANSPORT_PluginFunctions *api;
303 * Short name for the plugin (i.e. "tcp").
308 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
313 * List of our known addresses for this transport.
315 struct OwnAddressList *addresses;
318 * Environment this transport service is using
321 struct GNUNET_TRANSPORT_PluginEnvironment env;
324 * ID of task that is used to clean up expired addresses.
326 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
329 * Set to GNUNET_YES if we need to scrap the existing list of
330 * "addresses" and start fresh when we receive the next address
331 * update from a transport. Set to GNUNET_NO if we should just add
332 * the new address to the list and wait for the commit call.
336 struct ATS_plugin * rc;
339 * Hashmap of blacklisted peers for this particular transport.
341 struct GNUNET_CONTAINER_MultiHashMap *blacklist;
344 struct NeighbourList;
347 * For each neighbour we keep a list of messages
348 * that we still want to transmit to the neighbour.
354 * This is a doubly linked list.
356 struct MessageQueue *next;
359 * This is a doubly linked list.
361 struct MessageQueue *prev;
364 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
365 * stuck together in memory. Allocated at the end of this struct.
367 const char *message_buf;
370 * Size of the message buf
372 size_t message_buf_size;
375 * Client responsible for queueing the message;
376 * used to check that a client has no two messages
377 * pending for the same target. Can be NULL.
379 struct TransportClient *client;
382 * Using which specific address should we send this message?
384 struct ForeignAddressList *specific_address;
387 * Peer ID of the Neighbour this entry belongs to.
389 struct GNUNET_PeerIdentity neighbour_id;
392 * Plugin that we used for the transmission.
393 * NULL until we scheduled a transmission.
395 struct TransportPlugin *plugin;
398 * At what time should we fail?
400 struct GNUNET_TIME_Absolute timeout;
403 * Internal message of the transport system that should not be
404 * included in the usual SEND-SEND_OK transmission confirmation
405 * traffic management scheme. Typically, "internal_msg" will
406 * be set whenever "client" is NULL (but it is not strictly
412 * How important is the message?
414 unsigned int priority;
420 * For a given Neighbour, which plugins are available
421 * to talk to this peer and what are their costs?
426 * This is a linked list.
428 struct ReadyList *next;
431 * Which of our transport plugins does this entry
434 struct TransportPlugin *plugin;
437 * Transport addresses, latency, and readiness for
438 * this particular plugin.
440 struct ForeignAddressList *addresses;
443 * To which neighbour does this ready list belong to?
445 struct NeighbourList *neighbour;
450 * Entry in linked list of all of our current neighbours.
456 * This is a linked list.
458 struct NeighbourList *next;
461 * Which of our transports is connected to this peer
462 * and what is their status?
464 struct ReadyList *plugins;
467 * Head of list of messages we would like to send to this peer;
468 * must contain at most one message per client.
470 struct MessageQueue *messages_head;
473 * Tail of list of messages we would like to send to this peer; must
474 * contain at most one message per client.
476 struct MessageQueue *messages_tail;
479 * Head of list of messages of messages we expected the continuation
480 * to be called to destroy the message
482 struct MessageQueue *cont_head;
485 * Tail of list of messages of messages we expected the continuation
486 * to be called to destroy the message
488 struct MessageQueue *cont_tail;
491 * Buffer for at most one payload message used when we receive
492 * payload data before our PING-PONG has succeeded. We then
493 * store such messages in this intermediary buffer until the
494 * connection is fully up.
496 struct GNUNET_MessageHeader *pre_connect_message_buffer;
499 * Context for peerinfo iteration.
500 * NULL after we are done processing peerinfo's information.
502 struct GNUNET_PEERINFO_IteratorContext *piter;
505 * Public key for this peer. Valid only if the respective flag is set below.
507 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
510 * Identity of this neighbour.
512 struct GNUNET_PeerIdentity id;
515 * ID of task scheduled to run when this peer is about to
516 * time out (will free resources associated with the peer).
518 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
521 * ID of task scheduled to run when we should retry transmitting
522 * the head of the message queue. Actually triggered when the
523 * transmission is timing out (we trigger instantly when we have
524 * a chance of success).
526 GNUNET_SCHEDULER_TaskIdentifier retry_task;
529 * How long until we should consider this peer dead
530 * (if we don't receive another message in the
533 struct GNUNET_TIME_Absolute peer_timeout;
536 * Tracker for inbound bandwidth.
538 struct GNUNET_BANDWIDTH_Tracker in_tracker;
541 * The latency we have seen for this particular address for
542 * this particular peer. This latency may have been calculated
543 * over multiple transports. This value reflects how long it took
544 * us to receive a response when SENDING via this particular
545 * transport/neighbour/address combination!
547 * FIXME: we need to periodically send PINGs to update this
548 * latency (at least more often than the current "huge" (11h?)
551 struct GNUNET_TIME_Relative latency;
554 * How often has the other peer (recently) violated the
555 * inbound traffic limit? Incremented by 10 per violation,
556 * decremented by 1 per non-violation (for each
559 unsigned int quota_violation_count;
562 * DV distance to this peer (1 if no DV is used).
567 * Have we seen an PONG from this neighbour in the past (and
568 * not had a disconnect since)?
573 * Do we have a valid public key for this neighbour?
575 int public_key_valid;
578 * Performance data for the peer.
580 struct GNUNET_TRANSPORT_ATS_Information *ats;
583 * Identity of the neighbour.
585 struct GNUNET_PeerIdentity peer;
590 * Message used to ask a peer to validate receipt (to check an address
591 * from a HELLO). Followed by the address we are trying to validate,
592 * or an empty address if we are just sending a PING to confirm that a
593 * connection which the receiver (of the PING) initiated is still valid.
595 struct TransportPingMessage
599 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
601 struct GNUNET_MessageHeader header;
604 * Challenge code (to ensure fresh reply).
606 uint32_t challenge GNUNET_PACKED;
609 * Who is the intended recipient?
611 struct GNUNET_PeerIdentity target;
617 * Message used to validate a HELLO. The challenge is included in the
618 * confirmation to make matching of replies to requests possible. The
619 * signature signs our public key, an expiration time and our address.<p>
621 * This message is followed by our transport address that the PING tried
622 * to confirm (if we liked it). The address can be empty (zero bytes)
623 * if the PING had not address either (and we received the request via
624 * a connection that we initiated).
626 struct TransportPongMessage
630 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
632 struct GNUNET_MessageHeader header;
635 * Challenge code from PING (showing freshness). Not part of what
636 * is signed so that we can re-use signatures.
638 uint32_t challenge GNUNET_PACKED;
643 struct GNUNET_CRYPTO_RsaSignature signature;
646 * What are we signing and why? Two possible reason codes can be here:
647 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
648 * plausible address for this peer (pid is set to identity of signer); or
649 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING to confirm that this is
650 * an address we used to connect to the peer with the given pid.
652 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
655 * When does this signature expire?
657 struct GNUNET_TIME_AbsoluteNBO expiration;
660 * Either the identity of the peer Who signed this message, or the
661 * identity of the peer that we're connected to using the given
662 * address (depending on purpose.type).
664 struct GNUNET_PeerIdentity pid;
667 * Size of address appended to this message (part of what is
668 * being signed, hence not redundant).
676 * Linked list of messages to be transmitted to the client. Each
677 * entry is followed by the actual message.
679 struct ClientMessageQueueEntry
682 * This is a doubly-linked list.
684 struct ClientMessageQueueEntry *next;
687 * This is a doubly-linked list.
689 struct ClientMessageQueueEntry *prev;
694 * Client connected to the transport service.
696 struct TransportClient
700 * This is a linked list.
702 struct TransportClient *next;
705 * Handle to the client.
707 struct GNUNET_SERVER_Client *client;
710 * Linked list of messages yet to be transmitted to
713 struct ClientMessageQueueEntry *message_queue_head;
716 * Tail of linked list of messages yet to be transmitted to the
719 struct ClientMessageQueueEntry *message_queue_tail;
722 * Current transmit request handle.
724 struct GNUNET_CONNECTION_TransmitHandle *th;
727 * Is a call to "transmit_send_continuation" pending? If so, we
728 * must not free this struct (even if the corresponding client
729 * disconnects) and instead only remove it from the linked list and
730 * set the "client" field to NULL.
735 * Length of the list of messages pending for this client.
737 unsigned int message_count;
743 * Context of currently active requests to peerinfo
744 * for validation of HELLOs.
746 struct CheckHelloValidatedContext;
750 * Entry in map of all HELLOs awaiting validation.
752 struct ValidationEntry
756 * NULL if this entry is not part of a larger HELLO validation.
758 struct CheckHelloValidatedContext *chvc;
761 * The address, actually a pointer to the end
762 * of this struct. Do not free!
767 * Name of the transport.
769 char *transport_name;
772 * The public key of the peer.
774 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
777 * ID of task that will clean up this entry if we don't succeed
778 * with the validation first.
780 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
783 * At what time did we send this validation?
785 struct GNUNET_TIME_Absolute send_time;
788 * Session being validated (or NULL for none).
790 struct Session *session;
793 * Challenge number we used.
806 * Context of currently active requests to peerinfo
807 * for validation of HELLOs.
809 struct CheckHelloValidatedContext
813 * This is a doubly-linked list.
815 struct CheckHelloValidatedContext *next;
818 * This is a doubly-linked list.
820 struct CheckHelloValidatedContext *prev;
823 * Hello that we are validating.
825 const struct GNUNET_HELLO_Message *hello;
828 * Context for peerinfo iteration.
829 * NULL after we are done processing peerinfo's information.
831 struct GNUNET_PEERINFO_IteratorContext *piter;
834 * Was a HELLO known for this peer to peerinfo?
839 * Number of validation entries currently referring to this
842 unsigned int ve_count;
849 static struct GNUNET_HELLO_Message *our_hello;
854 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
859 static struct GNUNET_PeerIdentity my_identity;
864 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
869 const struct GNUNET_CONFIGURATION_Handle *cfg;
872 * Linked list of all clients to this service.
874 static struct TransportClient *clients;
877 * All loaded plugins.
879 static struct TransportPlugin *plugins;
882 * Handle to peerinfo service.
884 static struct GNUNET_PEERINFO_Handle *peerinfo;
887 * All known neighbours and their HELLOs.
889 static struct NeighbourList *neighbours;
892 * Number of neighbours we'd like to have.
894 static uint32_t max_connect_per_transport;
897 * Head of linked list.
899 static struct CheckHelloValidatedContext *chvc_head;
902 * Tail of linked list.
904 static struct CheckHelloValidatedContext *chvc_tail;
907 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
908 * of the given peer that we are currently validating).
910 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
913 * Handle for reporting statistics.
915 static struct GNUNET_STATISTICS_Handle *stats;
918 * Identifier of 'refresh_hello' task.
920 static GNUNET_SCHEDULER_TaskIdentifier hello_task;
923 * Is transport service shutting down ?
925 static int shutdown_in_progress;
928 * Handle for ats information
930 static struct ATS_Handle *ats;
933 * The peer specified by the given neighbour has timed-out or a plugin
934 * has disconnected. We may either need to do nothing (other plugins
935 * still up), or trigger a full disconnect and clean up. This
936 * function updates our state and do the necessary notifications.
937 * Also notifies our clients that the neighbour is now officially
940 * @param n the neighbour list entry for the peer
941 * @param check should we just check if all plugins
942 * disconnected or must we ask all plugins to
945 static void disconnect_neighbour (struct NeighbourList *n, int check);
948 * Check the ready list for the given neighbour and if a plugin is
949 * ready for transmission (and if we have a message), do so!
951 * @param nexi target peer for which to transmit
953 static void try_transmission_to_peer (struct NeighbourList *n);
955 struct ForeignAddressList * get_preferred_ats_address (
956 struct NeighbourList *n);
959 * Find an entry in the neighbour list for a particular peer.
961 * @return NULL if not found.
963 static struct NeighbourList *
964 find_neighbour (const struct GNUNET_PeerIdentity *key)
966 struct NeighbourList *head = neighbours;
968 while ((head != NULL) &&
969 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
974 static int update_addr_value (struct ForeignAddressList *fal, uint32_t value , int ats_index)
978 for (c=0; c<available_quality_metrics; c++)
980 if (ats_index == qm[c].atis_index)
982 fal->quality[c].values[0] = fal->quality[c].values[1];
983 fal->quality[c].values[1] = fal->quality[c].values[2];
984 fal->quality[c].values[2] = value;
987 ats->stat.modified_quality = GNUNET_YES;
990 if (set == GNUNET_NO)
992 for (c=0; c<available_ressources; c++)
994 if (ats_index == ressources[c].atis_index)
996 fal->ressources[c].c = value;
999 ats->stat.modified_resources = GNUNET_YES;
1007 update_addr_ats (struct ForeignAddressList *fal,
1008 const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
1013 for (c1=0; c1<ats_count; c1++)
1015 set = update_addr_value(fal, ntohl(ats_data[c1].value), ntohl(ats_data[c1].type));
1021 * Find an entry in the transport list for a particular transport.
1023 * @return NULL if not found.
1025 static struct TransportPlugin *
1026 find_transport (const char *short_name)
1028 struct TransportPlugin *head = plugins;
1029 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
1035 * Is a particular peer blacklisted for a particular transport?
1037 * @param peer the peer to check for
1038 * @param plugin the plugin used to connect to the peer
1040 * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
1043 is_blacklisted (const struct GNUNET_PeerIdentity *peer, struct TransportPlugin *plugin)
1046 if (plugin->blacklist != NULL)
1048 if (GNUNET_CONTAINER_multihashmap_contains (plugin->blacklist, &peer->hashPubKey) == GNUNET_YES)
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 "Peer `%s:%s' is blacklisted!\n",
1053 plugin->short_name, GNUNET_i2s (peer));
1056 GNUNET_STATISTICS_update (stats, "# blacklisted peers refused", 1, GNUNET_NO);
1066 add_peer_to_blacklist (struct GNUNET_PeerIdentity *peer,
1067 char *transport_name)
1069 struct TransportPlugin *plugin;
1071 plugin = find_transport(transport_name);
1072 if (plugin == NULL) /* Nothing to do */
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 "Adding peer `%s' with plugin `%s' to blacklist\n",
1080 if (plugin->blacklist == NULL)
1081 plugin->blacklist = GNUNET_CONTAINER_multihashmap_create(TRANSPORT_BLACKLIST_HT_SIZE);
1082 GNUNET_assert(plugin->blacklist != NULL);
1083 GNUNET_CONTAINER_multihashmap_put(plugin->blacklist, &peer->hashPubKey,
1085 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1090 * Read the blacklist file, containing transport:peer entries.
1091 * Provided the transport is loaded, set up hashmap with these
1092 * entries to blacklist peers by transport.
1096 read_blacklist_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1103 struct GNUNET_PeerIdentity pid;
1105 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1106 unsigned int entries_found;
1107 char *transport_name;
1110 GNUNET_CONFIGURATION_get_value_filename (cfg,
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 "Option `%s' in section `%s' not specified!\n",
1123 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1124 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1125 | GNUNET_DISK_PERM_USER_WRITE);
1126 if (0 != STAT (fn, &frstat))
1128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1129 _("Could not read blacklist file `%s'\n"), fn);
1133 if (frstat.st_size == 0)
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1137 _("Blacklist file `%s' is empty.\n"),
1143 /* FIXME: use mmap */
1144 data = GNUNET_malloc_large (frstat.st_size);
1145 GNUNET_assert(data != NULL);
1146 if (frstat.st_size !=
1147 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1150 _("Failed to read blacklist from `%s'\n"), fn);
1157 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1159 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1160 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1163 while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') && !isspace ( (unsigned char) data[colon_pos]))
1166 if (colon_pos >= frstat.st_size)
1168 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1169 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1170 (unsigned long long) colon_pos);
1176 if (isspace( (unsigned char) data[colon_pos]))
1178 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1179 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1180 (unsigned long long) colon_pos);
1182 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1186 tsize = colon_pos - pos;
1187 if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size) || (tsize == 0))
1189 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1190 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1191 (unsigned long long) colon_pos);
1200 transport_name = GNUNET_malloc(tsize + 1);
1201 memcpy(transport_name, &data[pos], tsize);
1202 pos = colon_pos + 1;
1204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1205 "Read transport name %s in blacklist file.\n",
1208 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1209 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1211 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1212 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1213 (unsigned long long) pos);
1215 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1217 GNUNET_free_non_null(transport_name);
1220 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1221 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1223 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1224 _("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
1225 (unsigned long long) pos,
1230 if (0 != memcmp (&pid,
1232 sizeof (struct GNUNET_PeerIdentity)))
1235 add_peer_to_blacklist (&pid,
1240 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1241 _("Found myself `%s' in blacklist (useless, ignored)\n"),
1245 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1246 GNUNET_free_non_null(transport_name);
1247 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1250 GNUNET_STATISTICS_update (stats, "# Transport entries blacklisted", entries_found, GNUNET_NO);
1257 * Function called to notify a client about the socket being ready to
1258 * queue more data. "buf" will be NULL and "size" zero if the socket
1259 * was closed for writing in the meantime.
1261 * @param cls closure
1262 * @param size number of bytes available in buf
1263 * @param buf where the callee should write the message
1264 * @return number of bytes written to buf
1267 transmit_to_client_callback (void *cls, size_t size, void *buf)
1269 struct TransportClient *client = cls;
1270 struct ClientMessageQueueEntry *q;
1273 const struct GNUNET_MessageHeader *msg;
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1281 "Transmission to client failed, closing connection.\n");
1283 /* fatal error with client, free message queue! */
1284 while (NULL != (q = client->message_queue_head))
1286 GNUNET_STATISTICS_update (stats,
1287 gettext_noop ("# bytes discarded (could not transmit to client)"),
1288 ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
1290 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1291 client->message_queue_tail,
1295 client->message_count = 0;
1300 while (NULL != (q = client->message_queue_head))
1302 msg = (const struct GNUNET_MessageHeader *) &q[1];
1303 msize = ntohs (msg->size);
1304 if (msize + tsize > size)
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1308 "Transmitting message of type %u to client.\n",
1311 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1312 client->message_queue_tail,
1314 memcpy (&cbuf[tsize], msg, msize);
1317 client->message_count--;
1321 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1322 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1324 GNUNET_TIME_UNIT_FOREVER_REL,
1325 &transmit_to_client_callback,
1327 GNUNET_assert (client->th != NULL);
1334 * Convert an address to a string.
1336 * @param plugin name of the plugin responsible for the address
1337 * @param addr binary address
1338 * @param addr_len number of bytes in addr
1339 * @return NULL on error, otherwise address string
1342 a2s (const char *plugin,
1346 struct TransportPlugin *p;
1350 p = find_transport (plugin);
1351 if ((p == NULL) || (addr_len == 0) || (addr == NULL))
1354 return p->api->address_to_string (NULL,
1364 * Iterator to free entries in the validation_map.
1366 * @param cls closure (unused)
1367 * @param key current key code
1368 * @param value value in the hash map (validation to abort)
1369 * @return GNUNET_YES (always)
1372 abort_validation (void *cls,
1373 const GNUNET_HashCode * key,
1376 struct ValidationEntry *va = value;
1378 if (GNUNET_SCHEDULER_NO_TASK != va->timeout_task)
1379 GNUNET_SCHEDULER_cancel (va->timeout_task);
1380 GNUNET_free (va->transport_name);
1381 if (va->chvc != NULL)
1383 va->chvc->ve_count--;
1384 if (va->chvc->ve_count == 0)
1386 GNUNET_CONTAINER_DLL_remove (chvc_head,
1389 GNUNET_free (va->chvc);
1399 * HELLO validation cleanup task (validation failed).
1401 * @param cls the 'struct ValidationEntry' that failed
1402 * @param tc scheduler context (unused)
1405 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1407 struct ValidationEntry *va = cls;
1408 struct GNUNET_PeerIdentity pid;
1410 va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1411 GNUNET_STATISTICS_update (stats,
1412 gettext_noop ("# address validation timeouts"),
1415 GNUNET_CRYPTO_hash (&va->publicKey,
1417 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1419 GNUNET_break (GNUNET_OK ==
1420 GNUNET_CONTAINER_multihashmap_remove (validation_map,
1423 abort_validation (NULL, NULL, va);
1429 * Send the specified message to the specified client. Since multiple
1430 * messages may be pending for the same client at a time, this code
1431 * makes sure that no message is lost.
1433 * @param client client to transmit the message to
1434 * @param msg the message to send
1435 * @param may_drop can this message be dropped if the
1436 * message queue for this client is getting far too large?
1439 transmit_to_client (struct TransportClient *client,
1440 const struct GNUNET_MessageHeader *msg, int may_drop)
1442 struct ClientMessageQueueEntry *q;
1445 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1447 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1449 ("Dropping message of type %u and size %u, have %u messages pending (%u is the soft limit)\n"),
1452 client->message_count,
1454 GNUNET_STATISTICS_update (stats,
1455 gettext_noop ("# messages dropped due to slow client"),
1460 msize = ntohs (msg->size);
1461 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1462 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1463 memcpy (&q[1], msg, msize);
1464 GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1465 client->message_queue_tail,
1466 client->message_queue_tail,
1468 client->message_count++;
1469 if (client->th == NULL)
1471 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1473 GNUNET_TIME_UNIT_FOREVER_REL,
1474 &transmit_to_client_callback,
1476 GNUNET_assert (client->th != NULL);
1482 * Transmit a 'SEND_OK' notification to the given client for the
1485 * @param client who to notify
1486 * @param n neighbour to notify about, can be NULL (on failure)
1487 * @param target target of the transmission
1488 * @param result status code for the transmission request
1491 transmit_send_ok (struct TransportClient *client,
1492 struct NeighbourList *n,
1493 const struct GNUNET_PeerIdentity *target,
1496 struct SendOkMessage send_ok_msg;
1498 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1499 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1500 send_ok_msg.success = htonl (result);
1502 send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1504 send_ok_msg.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
1505 send_ok_msg.peer = *target;
1506 transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1511 * Mark the given FAL entry as 'connected' (and hence preferred for
1512 * sending); also mark all others for the same peer as 'not connected'
1513 * (since only one can be preferred).
1515 * @param fal address to set to 'connected'
1518 mark_address_connected (struct ForeignAddressList *fal);
1523 * We should re-try transmitting to the given peer,
1524 * hopefully we've learned something in the meantime.
1527 retry_transmission_task (void *cls,
1528 const struct GNUNET_SCHEDULER_TaskContext *tc)
1530 struct NeighbourList *n = cls;
1532 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1533 try_transmission_to_peer (n);
1538 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1539 * upon "completion" of a send request. This tells the API
1540 * that it is now legal to send another message to the given
1543 * @param cls closure, identifies the entry on the
1544 * message queue that was transmitted and the
1545 * client responsible for queuing the message
1546 * @param target the peer receiving the message
1547 * @param result GNUNET_OK on success, if the transmission
1548 * failed, we should not tell the client to transmit
1552 transmit_send_continuation (void *cls,
1553 const struct GNUNET_PeerIdentity *target,
1556 struct MessageQueue *mq = cls;
1557 struct NeighbourList *n;
1559 GNUNET_STATISTICS_update (stats,
1560 gettext_noop ("# bytes pending with plugins"),
1561 - (int64_t) mq->message_buf_size,
1563 if (result == GNUNET_OK)
1565 GNUNET_STATISTICS_update (stats,
1566 gettext_noop ("# bytes successfully transmitted by plugins"),
1567 mq->message_buf_size,
1572 GNUNET_STATISTICS_update (stats,
1573 gettext_noop ("# bytes with transmission failure by plugins"),
1574 mq->message_buf_size,
1577 if (mq->specific_address != NULL)
1579 if (result == GNUNET_OK)
1581 mq->specific_address->timeout =
1582 GNUNET_TIME_relative_to_absolute
1583 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1584 if (mq->specific_address->validated == GNUNET_YES)
1585 mark_address_connected (mq->specific_address);
1589 if (mq->specific_address->connected != GNUNET_NO)
1592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1593 "Marking address `%s' as no longer connected (due to transmission problem)\n",
1594 a2s (mq->specific_address->ready_list->plugin->short_name,
1595 mq->specific_address->addr,
1596 mq->specific_address->addrlen));
1598 GNUNET_STATISTICS_update (stats,
1599 gettext_noop ("# connected addresses"),
1602 mq->specific_address->connected = GNUNET_NO;
1605 if (! mq->internal_msg)
1606 mq->specific_address->in_transmit = GNUNET_NO;
1608 n = find_neighbour (&mq->neighbour_id);
1609 if (mq->client != NULL)
1610 transmit_send_ok (mq->client, n, target, result);
1613 GNUNET_CONTAINER_DLL_remove (n->cont_head,
1620 if (result == GNUNET_OK)
1621 try_transmission_to_peer (n);
1622 else if (GNUNET_SCHEDULER_NO_TASK == n->retry_task)
1623 n->retry_task = GNUNET_SCHEDULER_add_now (&retry_transmission_task,
1630 * Check the ready list for the given neighbour and if a plugin is
1631 * ready for transmission (and if we have a message), do so!
1633 * @param neighbour target peer for which to transmit
1636 try_transmission_to_peer (struct NeighbourList *n)
1638 struct ReadyList *rl;
1639 struct MessageQueue *mq;
1640 struct GNUNET_TIME_Relative timeout;
1644 if (n->messages_head == NULL)
1647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1648 "Transmission queue for `%4s' is empty\n",
1649 GNUNET_i2s (&n->id));
1651 return; /* nothing to do */
1654 mq = n->messages_head;
1655 force_address = GNUNET_YES;
1656 if (mq->specific_address == NULL)
1659 mq->specific_address = get_preferred_ats_address(n);
1660 GNUNET_STATISTICS_update (stats,
1661 gettext_noop ("# transport selected peer address freely"),
1664 force_address = GNUNET_NO;
1666 if (mq->specific_address == NULL)
1668 GNUNET_STATISTICS_update (stats,
1669 gettext_noop ("# transport failed to selected peer address"),
1672 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1673 if (timeout.rel_value == 0)
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1677 "No destination address available to transmit message of size %u to peer `%4s'\n",
1678 mq->message_buf_size,
1679 GNUNET_i2s (&mq->neighbour_id));
1681 GNUNET_STATISTICS_update (stats,
1682 gettext_noop ("# bytes in message queue for other peers"),
1683 - (int64_t) mq->message_buf_size,
1685 GNUNET_STATISTICS_update (stats,
1686 gettext_noop ("# bytes discarded (no destination address available)"),
1687 mq->message_buf_size,
1689 if (mq->client != NULL)
1690 transmit_send_ok (mq->client, n, &n->id, GNUNET_NO);
1691 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1695 return; /* nobody ready */
1697 GNUNET_STATISTICS_update (stats,
1698 gettext_noop ("# message delivery deferred (no address)"),
1701 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
1702 GNUNET_SCHEDULER_cancel (n->retry_task);
1703 n->retry_task = GNUNET_SCHEDULER_add_delayed (timeout,
1704 &retry_transmission_task,
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1708 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
1709 mq->message_buf_size,
1710 GNUNET_i2s (&mq->neighbour_id),
1713 /* FIXME: might want to trigger peerinfo lookup here
1714 (unless that's already pending...) */
1717 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1720 if (mq->specific_address->connected == GNUNET_NO)
1721 mq->specific_address->connect_attempts++;
1722 rl = mq->specific_address->ready_list;
1723 mq->plugin = rl->plugin;
1724 if (!mq->internal_msg)
1725 mq->specific_address->in_transmit = GNUNET_YES;
1727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1728 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
1729 mq->message_buf_size,
1730 GNUNET_i2s (&n->id),
1731 (mq->specific_address->addr != NULL)
1732 ? a2s (mq->plugin->short_name,
1733 mq->specific_address->addr,
1734 mq->specific_address->addrlen)
1736 rl->plugin->short_name);
1738 GNUNET_STATISTICS_update (stats,
1739 gettext_noop ("# bytes in message queue for other peers"),
1740 - (int64_t) mq->message_buf_size,
1742 GNUNET_STATISTICS_update (stats,
1743 gettext_noop ("# bytes pending with plugins"),
1744 mq->message_buf_size,
1747 GNUNET_CONTAINER_DLL_insert (n->cont_head,
1751 ret = rl->plugin->api->send (rl->plugin->api->cls,
1754 mq->message_buf_size,
1756 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1757 mq->specific_address->session,
1758 mq->specific_address->addr,
1759 mq->specific_address->addrlen,
1761 &transmit_send_continuation, mq);
1764 /* failure, but 'send' would not call continuation in this case,
1765 so we need to do it here! */
1766 transmit_send_continuation (mq,
1774 * Send the specified message to the specified peer.
1776 * @param client source of the transmission request (can be NULL)
1777 * @param peer_address ForeignAddressList where we should send this message
1778 * @param priority how important is the message
1779 * @param timeout how long do we have to transmit?
1780 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
1781 * @param message_buf_size total size of all messages in message_buf
1782 * @param is_internal is this an internal message; these are pre-pended and
1783 * also do not count for plugins being "ready" to transmit
1784 * @param neighbour handle to the neighbour for transmission
1787 transmit_to_peer (struct TransportClient *client,
1788 struct ForeignAddressList *peer_address,
1789 unsigned int priority,
1790 struct GNUNET_TIME_Relative timeout,
1791 const char *message_buf,
1792 size_t message_buf_size,
1793 int is_internal, struct NeighbourList *neighbour)
1795 struct MessageQueue *mq;
1800 /* check for duplicate submission */
1801 mq = neighbour->messages_head;
1804 if (mq->client == client)
1806 /* client transmitted to same peer twice
1807 before getting SEND_OK! */
1815 GNUNET_STATISTICS_update (stats,
1816 gettext_noop ("# bytes in message queue for other peers"),
1819 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
1820 mq->specific_address = peer_address;
1821 mq->client = client;
1822 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1823 memcpy (&mq[1], message_buf, message_buf_size);
1824 mq->message_buf = (const char*) &mq[1];
1825 mq->message_buf_size = message_buf_size;
1826 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
1827 mq->internal_msg = is_internal;
1828 mq->priority = priority;
1829 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1831 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
1832 neighbour->messages_tail,
1835 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
1836 neighbour->messages_tail,
1837 neighbour->messages_tail,
1839 try_transmission_to_peer (neighbour);
1844 * Send a plain PING (without address or our HELLO) to the given
1845 * foreign address to try to establish a connection (and validate
1846 * that the other peer is really who he claimed he is).
1848 * @param n neighbour to PING
1851 transmit_plain_ping (struct NeighbourList *n)
1853 struct ValidationEntry *ve;
1854 struct TransportPingMessage ping;
1855 struct ReadyList *rl;
1856 struct TransportPlugin *plugin;
1857 struct ForeignAddressList *fal;
1859 if (! n->public_key_valid)
1861 /* This should not happen since the other peer
1862 should send us a HELLO prior to sending his
1864 GNUNET_break_op (0);
1865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1866 "Could not transmit plain PING to `%s': public key not known\n",
1867 GNUNET_i2s (&n->id));
1870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1871 "Looking for addresses to transmit plain PING to `%s'\n",
1872 GNUNET_i2s (&n->id));
1873 for (rl = n->plugins; rl != NULL; rl = rl->next)
1875 plugin = rl->plugin;
1876 for (fal = rl->addresses; fal != NULL; fal = fal->next)
1878 if (! fal->connected)
1880 ve = GNUNET_malloc (sizeof (struct ValidationEntry));
1881 ve->transport_name = GNUNET_strdup (plugin->short_name);
1882 ve->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1884 ve->send_time = GNUNET_TIME_absolute_get();
1885 ve->session = fal->session;
1886 memcpy(&ve->publicKey,
1888 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1889 ve->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
1890 &timeout_hello_validation,
1892 GNUNET_CONTAINER_multihashmap_put (validation_map,
1895 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1896 ping.header.size = htons(sizeof(struct TransportPingMessage));
1897 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
1898 ping.challenge = htonl(ve->challenge);
1899 memcpy(&ping.target, &n->id, sizeof(struct GNUNET_PeerIdentity));
1900 GNUNET_STATISTICS_update (stats,
1901 gettext_noop ("# PING without HELLO messages sent"),
1904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1905 "Transmitting plain PING to `%s'\n",
1906 GNUNET_i2s (&n->id));
1907 transmit_to_peer (NULL,
1909 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1910 HELLO_VERIFICATION_TIMEOUT,
1911 (const char*) &ping, sizeof (ping),
1919 * Mark the given FAL entry as 'connected' (and hence preferred for
1920 * sending); also mark all others for the same peer as 'not connected'
1921 * (since only one can be preferred).
1923 * @param fal address to set to 'connected'
1926 mark_address_connected (struct ForeignAddressList *fal)
1928 struct ForeignAddressList *pos;
1931 GNUNET_assert (GNUNET_YES == fal->validated);
1932 if (fal->connected == GNUNET_YES)
1933 return; /* nothing to do */
1935 pos = fal->ready_list->addresses;
1938 if (GNUNET_YES == pos->connected)
1941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1942 "Marking address `%s' as no longer connected (due to connect on other address)\n",
1943 a2s (pos->ready_list->plugin->short_name,
1947 GNUNET_break (cnt == GNUNET_YES);
1949 pos->connected = GNUNET_NO;
1950 GNUNET_STATISTICS_update (stats,
1951 gettext_noop ("# connected addresses"),
1957 fal->connected = GNUNET_YES;
1958 if (GNUNET_YES == cnt)
1960 GNUNET_STATISTICS_update (stats,
1961 gettext_noop ("# connected addresses"),
1969 * Find an address in any of the available transports for
1970 * the given neighbour that would be good for message
1971 * transmission. This is essentially the transport selection
1974 * @param neighbour for whom to select an address
1975 * @return selected address, NULL if we have none
1977 struct ForeignAddressList *
1978 find_ready_address(struct NeighbourList *neighbour)
1980 struct ReadyList *head = neighbour->plugins;
1981 struct ForeignAddressList *addresses;
1982 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1983 struct ForeignAddressList *best_address;
1985 /* Hack to prefer unix domain sockets */
1986 struct ForeignAddressList *unix_address = NULL;
1988 best_address = NULL;
1989 while (head != NULL)
1991 addresses = head->addresses;
1992 while (addresses != NULL)
1994 if ( (addresses->timeout.abs_value < now.abs_value) &&
1995 (addresses->connected == GNUNET_YES) )
1998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1999 "Marking long-time inactive connection to `%4s' as down.\n",
2000 GNUNET_i2s (&neighbour->id));
2002 GNUNET_STATISTICS_update (stats,
2003 gettext_noop ("# connected addresses"),
2006 addresses->connected = GNUNET_NO;
2008 addresses = addresses->next;
2011 addresses = head->addresses;
2012 while (addresses != NULL)
2015 if (addresses->addr != NULL)
2016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2017 "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
2018 a2s (head->plugin->short_name,
2020 addresses->addrlen),
2021 GNUNET_i2s (&neighbour->id),
2022 addresses->connected,
2023 addresses->in_transmit,
2024 addresses->validated,
2025 addresses->connect_attempts,
2026 (unsigned long long) addresses->timeout.abs_value,
2027 (unsigned int) addresses->distance);
2029 if (0==strcmp(head->plugin->short_name,"unix"))
2031 if ( (unix_address == NULL) ||
2032 ( (unix_address != NULL) &&
2033 (addresses->latency.rel_value < unix_address->latency.rel_value) ) )
2034 unix_address = addresses;
2036 if ( ( (best_address == NULL) ||
2037 (addresses->connected == GNUNET_YES) ||
2038 (best_address->connected == GNUNET_NO) ) &&
2039 (addresses->in_transmit == GNUNET_NO) &&
2040 ( (best_address == NULL) ||
2041 (addresses->latency.rel_value < best_address->latency.rel_value)) )
2042 best_address = addresses;
2043 /* FIXME: also give lower-latency addresses that are not
2044 connected a chance some times... */
2045 addresses = addresses->next;
2047 if (unix_address != NULL)
2051 if (unix_address != NULL)
2053 best_address = unix_address;
2055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2056 "Found UNIX address, forced this address\n");
2059 if (best_address != NULL)
2062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2063 "Best address found (`%s') has latency of %llu ms.\n",
2064 (best_address->addrlen > 0)
2065 ? a2s (best_address->ready_list->plugin->short_name,
2067 best_address->addrlen)
2069 best_address->latency.rel_value);
2074 GNUNET_STATISTICS_update (stats,
2075 gettext_noop ("# transmission attempts failed (no address)"),
2080 return best_address;
2088 struct GeneratorContext
2090 struct TransportPlugin *plug_pos;
2091 struct OwnAddressList *addr_pos;
2092 struct GNUNET_TIME_Absolute expiration;
2100 address_generator (void *cls, size_t max, void *buf)
2102 struct GeneratorContext *gc = cls;
2105 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
2107 gc->plug_pos = gc->plug_pos->next;
2108 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
2110 if (NULL == gc->plug_pos)
2115 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
2118 gc->addr_pos->addrlen, buf, max);
2119 gc->addr_pos = gc->addr_pos->next;
2125 * Construct our HELLO message from all of the addresses of
2126 * all of the transports.
2129 * @param tc scheduler context
2132 refresh_hello_task (void *cls,
2133 const struct GNUNET_SCHEDULER_TaskContext *tc)
2135 struct GNUNET_HELLO_Message *hello;
2136 struct TransportClient *cpos;
2137 struct NeighbourList *npos;
2138 struct GeneratorContext gc;
2140 hello_task = GNUNET_SCHEDULER_NO_TASK;
2141 gc.plug_pos = plugins;
2142 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
2143 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2144 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2147 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
2149 GNUNET_STATISTICS_update (stats,
2150 gettext_noop ("# refreshed my HELLO"),
2154 while (cpos != NULL)
2156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2157 "Transmitting my HELLO to client!\n");
2158 transmit_to_client (cpos,
2159 (const struct GNUNET_MessageHeader *) hello,
2164 GNUNET_free_non_null (our_hello);
2166 GNUNET_PEERINFO_add_peer (peerinfo, our_hello);
2167 for (npos = neighbours; npos != NULL; npos = npos->next)
2169 if (! npos->received_pong)
2172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2173 "Transmitting updated `%s' to neighbour `%4s'\n",
2174 "HELLO", GNUNET_i2s (&npos->id));
2176 GNUNET_STATISTICS_update (stats,
2177 gettext_noop ("# transmitted my HELLO to other peers"),
2180 transmit_to_peer (NULL, NULL, 0,
2181 HELLO_ADDRESS_EXPIRATION,
2182 (const char *) our_hello,
2183 GNUNET_HELLO_size(our_hello),
2190 * Schedule task to refresh hello (unless such a
2191 * task exists already).
2196 #if DEBUG_TRANSPORT_HELLO
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 "refresh_hello() called!\n");
2200 if (hello_task != GNUNET_SCHEDULER_NO_TASK)
2203 = GNUNET_SCHEDULER_add_now (&refresh_hello_task,
2209 * Iterator over hash map entries that NULLs the session of validation
2210 * entries that match the given session.
2212 * @param cls closure (the 'struct Session*' to match against)
2213 * @param key current key code (peer ID, not used)
2214 * @param value value in the hash map ('struct ValidationEntry*')
2215 * @return GNUNET_YES (we should continue to iterate)
2218 remove_session_validations (void *cls,
2219 const GNUNET_HashCode * key,
2222 struct Session *session = cls;
2223 struct ValidationEntry *ve = value;
2225 if (session == ve->session)
2232 * We've been disconnected from the other peer (for some
2233 * connection-oriented transport). Either quickly
2234 * re-establish the connection or signal the disconnect
2237 * Only signal CORE level disconnect if ALL addresses
2238 * for the peer are exhausted.
2240 * @param p overall plugin context
2241 * @param nl neighbour that was disconnected
2244 try_fast_reconnect (struct TransportPlugin *p,
2245 struct NeighbourList *nl)
2247 /* FIXME-MW: fast reconnect / transport switching not implemented... */
2248 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2249 "try_fast_reconnect not implemented!\n");
2250 /* Note: the idea here is to hide problems with transports (or
2251 switching between plugins) from the core to eliminate the need to
2252 re-negotiate session keys and the like; OTOH, we should tell core
2253 quickly (much faster than timeout) `if a connection was lost and
2254 could not be re-established (i.e. other peer went down or is
2255 unable / refuses to communicate);
2257 So we should consider:
2258 1) ideally: our own willingness / need to connect
2259 2) prior failures to connect to this peer (by plugin)
2260 3) ideally: reasons why other peer terminated (as far as knowable)
2262 Most importantly, it must be POSSIBLE for another peer to terminate
2263 a connection for a while (without us instantly re-establishing it).
2264 Similarly, if another peer is gone we should quickly notify CORE.
2265 OTOH, if there was a minor glitch (i.e. crash of gnunet-service-transport
2266 on the other end), we should reconnect in such a way that BOTH CORE
2267 services never even notice.
2268 Furthermore, the same mechanism (or small variation) could be used
2269 to switch to a better-performing plugin (ATS).
2271 Finally, this needs to be tested throughly... */
2274 * GNUNET_NO in the call below makes transport disconnect the peer,
2275 * even if only a single address (out of say, six) went away. This
2276 * function must be careful to ONLY disconnect if the peer is gone,
2277 * not just a specific address.
2279 * More specifically, half the places it was used had it WRONG.
2282 /* No reconnect, signal disconnect instead! */
2284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2285 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2286 "try_fast_reconnect");
2288 GNUNET_STATISTICS_update (stats,
2289 gettext_noop ("# disconnects due to try_fast_reconnect"),
2293 disconnect_neighbour (nl, GNUNET_YES);
2299 * Function that will be called whenever the plugin internally
2300 * cleans up a session pointer and hence the service needs to
2301 * discard all of those sessions as well. Plugins that do not
2302 * use sessions can simply omit calling this function and always
2303 * use NULL wherever a session pointer is needed.
2305 * @param cls closure
2306 * @param peer which peer was the session for
2307 * @param session which session is being destoyed
2310 plugin_env_session_end (void *cls,
2311 const struct GNUNET_PeerIdentity *peer,
2312 struct Session *session)
2314 struct TransportPlugin *p = cls;
2315 struct NeighbourList *nl;
2316 struct ReadyList *rl;
2317 struct ForeignAddressList *pos;
2318 struct ForeignAddressList *prev;
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2322 "Session ended with peer `%4s', %s\n",
2324 "plugin_env_session_end");
2326 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2327 &remove_session_validations,
2329 nl = find_neighbour (peer);
2333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2334 "No neighbour record found for peer `%4s'\n",
2337 return; /* was never marked as connected */
2342 if (rl->plugin == p)
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2350 "Plugin was associated with peer `%4s'\n",
2353 GNUNET_STATISTICS_update (stats,
2354 gettext_noop ("# disconnects due to session end"),
2357 disconnect_neighbour (nl, GNUNET_YES);
2361 pos = rl->addresses;
2362 while ( (pos != NULL) &&
2363 (pos->session != session) )
2371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2372 "Session was never marked as ready for peer `%4s'\n",
2376 int validations_pending = GNUNET_CONTAINER_multihashmap_contains (validation_map, &peer->hashPubKey);
2378 /* No session was marked as ready, but we have pending validations so do not disconnect from neighbour */
2379 if (validations_pending ==GNUNET_YES)
2382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2383 "Not disconnecting from peer `%4s due to pending address validations\n", GNUNET_i2s(peer));
2388 //FIXME: This conflicts with inbound tcp connections and tcp nat ... debugging in progress
2389 GNUNET_STATISTICS_update (stats,
2390 gettext_noop ("# disconnects due to unready session"),
2394 disconnect_neighbour (nl, GNUNET_YES);
2395 return; /* was never marked as connected */
2397 pos->session = NULL;
2398 pos->connected = GNUNET_NO;
2399 if (pos->addrlen != 0)
2401 if (nl->received_pong != GNUNET_NO)
2403 GNUNET_STATISTICS_update (stats,
2404 gettext_noop ("# try_fast_reconnect thanks to plugin_env_session_end"),
2407 if (GNUNET_YES == pos->connected)
2408 try_fast_reconnect (p, nl);
2412 GNUNET_STATISTICS_update (stats,
2413 gettext_noop ("# disconnects due to missing pong"),
2416 if (GNUNET_YES == pos->connected)
2417 disconnect_neighbour (nl, GNUNET_YES);
2421 /* was inbound connection, free 'pos' */
2423 rl->addresses = pos->next;
2425 prev->next = pos->next;
2426 if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2428 GNUNET_SCHEDULER_cancel (pos->revalidate_task);
2429 pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2431 GNUNET_free_non_null(pos->ressources);
2432 GNUNET_free_non_null(pos->quality);
2433 if (GNUNET_YES != pos->connected)
2435 /* nothing else to do, connection was never up... */
2441 ats->stat.recreate_problem = GNUNET_YES;
2442 if (nl->received_pong == GNUNET_NO)
2444 GNUNET_STATISTICS_update (stats,
2445 gettext_noop ("# disconnects due to NO pong"),
2448 disconnect_neighbour (nl, GNUNET_YES);
2449 return; /* nothing to do, never connected... */
2451 /* check if we have any validated addresses left */
2452 pos = rl->addresses;
2455 if (GNUNET_YES == pos->validated)
2457 GNUNET_STATISTICS_update (stats,
2458 gettext_noop ("# try_fast_reconnect thanks to validated_address"),
2461 try_fast_reconnect (p, nl);
2466 /* no valid addresses left, signal disconnect! */
2469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2470 "Disconnecting peer `%4s', %s\n",
2472 "plugin_env_session_end");
2474 /* FIXME: This doesn't mean there are no addresses left for this PEER,
2475 * it means there aren't any left for this PLUGIN/PEER combination! So
2476 * calling disconnect_neighbour here with GNUNET_NO forces disconnect
2477 * when it isn't necessary. Using GNUNET_YES at least checks to see
2478 * if there are any addresses that work first, so as not to overdo it.
2481 GNUNET_STATISTICS_update (stats,
2482 gettext_noop ("# disconnects due to plugin_env_session_end"),
2485 disconnect_neighbour (nl, GNUNET_YES);
2490 * Function that must be called by each plugin to notify the
2491 * transport service about the addresses under which the transport
2492 * provided by the plugin can be reached.
2494 * @param cls closure
2495 * @param add_remove GNUNET_YES to add, GNUNET_NO to remove the address
2496 * @param addr one of the addresses of the host, NULL for the last address
2497 * the specific address format depends on the transport
2498 * @param addrlen length of the address
2501 plugin_env_notify_address (void *cls,
2506 struct TransportPlugin *p = cls;
2507 struct OwnAddressList *al;
2508 struct OwnAddressList *prev;
2510 GNUNET_assert (p->api != NULL);
2512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2513 (add_remove == GNUNET_YES)
2514 ? "Adding `%s':%s to the set of our addresses\n"
2515 : "Removing `%s':%s from the set of our addresses\n",
2520 GNUNET_assert (addr != NULL);
2521 if (GNUNET_NO == add_remove)
2527 if ( (addrlen == al->addrlen) &&
2528 (0 == memcmp (addr, &al[1], addrlen)) )
2531 p->addresses = al->next;
2533 prev->next = al->next;
2544 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2545 al->next = p->addresses;
2547 al->addrlen = addrlen;
2548 memcpy (&al[1], addr, addrlen);
2554 * Notify all of our clients about a peer connecting.
2557 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2558 struct GNUNET_TIME_Relative latency,
2561 struct ConnectInfoMessage * cim;
2562 struct TransportClient *cpos;
2566 if (0 == memcmp (peer,
2568 sizeof (struct GNUNET_PeerIdentity)))
2574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2575 "Notifying clients about connection with `%s'\n",
2578 GNUNET_STATISTICS_update (stats,
2579 gettext_noop ("# peers connected"),
2584 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
2585 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2589 cim = GNUNET_malloc (size);
2590 cim->header.size = htons (size);
2591 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2592 cim->ats_count = htonl(2);
2593 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2594 (&(cim->ats))[0].value = htonl (distance);
2595 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
2596 (&(cim->ats))[1].value = htonl ((uint32_t) latency.rel_value);
2597 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2598 (&(cim->ats))[2].value = htonl (0);
2599 memcpy (&cim->id, peer, sizeof (struct GNUNET_PeerIdentity));
2601 /* notify ats about connecting peer */
2602 /* notify ats about connecting peer */
2603 if ((ats != NULL) && (shutdown_in_progress == GNUNET_NO))
2605 ats->stat.recreate_problem = GNUNET_YES;
2606 ats_calculate_bandwidth_distribution (ats, stats, neighbours);
2611 while (cpos != NULL)
2613 transmit_to_client (cpos, &(cim->header), GNUNET_NO);
2622 * Notify all of our clients about a peer disconnecting.
2625 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2627 struct DisconnectInfoMessage dim;
2628 struct TransportClient *cpos;
2630 if (0 == memcmp (peer,
2632 sizeof (struct GNUNET_PeerIdentity)))
2638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2639 "Notifying clients about lost connection to `%s'\n",
2642 GNUNET_STATISTICS_update (stats,
2643 gettext_noop ("# peers connected"),
2646 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2647 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2648 dim.reserved = htonl (0);
2649 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2651 /* notify ats about connecting peer */
2652 if ((ats != NULL) && (shutdown_in_progress == GNUNET_NO))
2654 ats->stat.recreate_problem = GNUNET_YES;
2655 ats_calculate_bandwidth_distribution (ats, stats, neighbours);
2659 while (cpos != NULL)
2661 transmit_to_client (cpos, &dim.header, GNUNET_NO);
2668 * Find a ForeignAddressList entry for the given neighbour
2669 * that matches the given address and transport.
2671 * @param neighbour which peer we care about
2672 * @param tname name of the transport plugin
2673 * @param session session to look for, NULL for 'any'; otherwise
2674 * can be used for the service to "learn" this session ID
2676 * @param addr binary address
2677 * @param addrlen length of addr
2678 * @return NULL if no such entry exists
2680 static struct ForeignAddressList *
2681 find_peer_address(struct NeighbourList *neighbour,
2683 struct Session *session,
2687 struct ReadyList *head;
2688 struct ForeignAddressList *pos;
2690 head = neighbour->plugins;
2691 while (head != NULL)
2693 if (0 == strcmp (tname, head->plugin->short_name))
2699 pos = head->addresses;
2700 while ( (pos != NULL) &&
2701 ( (pos->addrlen != addrlen) ||
2702 (memcmp(pos->addr, addr, addrlen) != 0) ) )
2704 if ( (session != NULL) &&
2705 (pos->session == session) )
2709 if ( (session != NULL) && (pos != NULL) )
2710 pos->session = session; /* learn it! */
2716 * Get the peer address struct for the given neighbour and
2717 * address. If it doesn't yet exist, create it.
2719 * @param neighbour which peer we care about
2720 * @param tname name of the transport plugin
2721 * @param session session of the plugin, or NULL for none
2722 * @param addr binary address
2723 * @param addrlen length of addr
2724 * @return NULL if we do not have a transport plugin for 'tname'
2726 static struct ForeignAddressList *
2727 add_peer_address (struct NeighbourList *neighbour,
2729 struct Session *session,
2733 struct ReadyList *head;
2734 struct ForeignAddressList *ret;
2737 ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2740 head = neighbour->plugins;
2742 while (head != NULL)
2744 if (0 == strcmp (tname, head->plugin->short_name))
2750 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2751 ret->session = session;
2752 if ((addrlen > 0) && (addr != NULL))
2754 ret->addr = (const char*) &ret[1];
2755 memcpy (&ret[1], addr, addrlen);
2762 ret->ressources = GNUNET_malloc(available_ressources * sizeof (struct ATS_ressource_entry));
2763 for (c=0; c<available_ressources; c++)
2765 struct ATS_ressource_entry *r = ret->ressources;
2767 r[c].atis_index = ressources[c].atis_index;
2768 if (0 == strcmp(neighbour->plugins->plugin->short_name,"unix"))
2770 r[c].c = ressources[c].c_unix;
2772 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"udp"))
2774 r[c].c = ressources[c].c_udp;
2776 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"tcp"))
2778 r[c].c = ressources[c].c_tcp;
2780 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"http"))
2782 r[c].c = ressources[c].c_http;
2784 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"https"))
2786 r[c].c = ressources[c].c_https;
2788 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"wlan"))
2790 r[c].c = ressources[c].c_wlan;
2794 r[c].c = ressources[c].c_default;
2795 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2796 "Assigning default cost to peer `%s' addr plugin `%s'! This should not happen!\n",
2797 GNUNET_i2s(&neighbour->peer),
2798 neighbour->plugins->plugin->short_name);
2802 ret->quality = GNUNET_malloc (available_quality_metrics * sizeof (struct ATS_quality_entry));
2803 ret->addrlen = addrlen;
2804 ret->expires = GNUNET_TIME_relative_to_absolute
2805 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2806 ret->latency = GNUNET_TIME_relative_get_forever();
2808 ret->timeout = GNUNET_TIME_relative_to_absolute
2809 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2810 ret->ready_list = head;
2811 ret->next = head->addresses;
2812 head->addresses = ret;
2818 * Closure for 'add_validated_address'.
2820 struct AddValidatedAddressContext
2823 * Entry that has been validated.
2825 const struct ValidationEntry *ve;
2828 * Flag set after we have added the address so
2829 * that we terminate the iteration next time.
2836 * Callback function used to fill a buffer of max bytes with a list of
2837 * addresses in the format used by HELLOs. Should use
2838 * "GNUNET_HELLO_add_address" as a helper function.
2840 * @param cls the 'struct AddValidatedAddressContext' with the validated address
2841 * @param max maximum number of bytes that can be written to buf
2842 * @param buf where to write the address information
2843 * @return number of bytes written, 0 to signal the
2844 * end of the iteration.
2847 add_validated_address (void *cls,
2848 size_t max, void *buf)
2850 struct AddValidatedAddressContext *avac = cls;
2851 const struct ValidationEntry *ve = avac->ve;
2853 if (GNUNET_YES == avac->done)
2855 avac->done = GNUNET_YES;
2856 return GNUNET_HELLO_add_address (ve->transport_name,
2857 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2867 * Closure for 'check_address_exists'.
2869 struct CheckAddressExistsClosure
2872 * Address to check for.
2877 * Name of the transport.
2884 struct Session *session;
2887 * Set to GNUNET_YES if the address exists.
2900 * Iterator over hash map entries. Checks if the given
2901 * validation entry is for the same address as what is given
2904 * @param cls the 'struct CheckAddressExistsClosure*'
2905 * @param key current key code (ignored)
2906 * @param value value in the hash map ('struct ValidationEntry')
2907 * @return GNUNET_YES if we should continue to
2908 * iterate (mismatch), GNUNET_NO if not (entry matched)
2911 check_address_exists (void *cls,
2912 const GNUNET_HashCode * key,
2915 struct CheckAddressExistsClosure *caec = cls;
2916 struct ValidationEntry *ve = value;
2918 if ( (0 == strcmp (caec->tname,
2919 ve->transport_name)) &&
2920 (caec->addrlen == ve->addrlen) &&
2921 (0 == memcmp (caec->addr,
2925 caec->exists = GNUNET_YES;
2928 if ( (ve->session != NULL) &&
2929 (caec->session == ve->session) )
2931 caec->exists = GNUNET_YES;
2939 neighbour_timeout_task (void *cls,
2940 const struct GNUNET_SCHEDULER_TaskContext *tc)
2942 struct NeighbourList *n = cls;
2945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2946 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
2948 GNUNET_STATISTICS_update (stats,
2949 gettext_noop ("# disconnects due to timeout"),
2952 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2953 disconnect_neighbour (n, GNUNET_NO);
2958 * Schedule the job that will cause us to send a PING to the
2959 * foreign address to evaluate its validity and latency.
2961 * @param fal address to PING
2964 schedule_next_ping (struct ForeignAddressList *fal);
2968 * Add the given address to the list of foreign addresses
2969 * available for the given peer (check for duplicates).
2971 * @param cls the respective 'struct NeighbourList' to update
2972 * @param tname name of the transport
2973 * @param expiration expiration time
2974 * @param addr the address
2975 * @param addrlen length of the address
2976 * @return GNUNET_OK (always)
2979 add_to_foreign_address_list (void *cls,
2981 struct GNUNET_TIME_Absolute expiration,
2985 struct NeighbourList *n = cls;
2986 struct ForeignAddressList *fal;
2989 GNUNET_STATISTICS_update (stats,
2990 gettext_noop ("# valid peer addresses returned by PEERINFO"),
2994 fal = find_peer_address (n, tname, NULL, addr, addrlen);
2997 #if DEBUG_TRANSPORT_HELLO
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2999 "Adding address `%s' (%s) for peer `%4s' due to PEERINFO data for %llums.\n",
3000 a2s (tname, addr, addrlen),
3002 GNUNET_i2s (&n->id),
3003 expiration.abs_value);
3005 fal = add_peer_address (n, tname, NULL, addr, addrlen);
3008 GNUNET_STATISTICS_update (stats,
3009 gettext_noop ("# previously validated addresses lacking transport"),
3015 fal->expires = GNUNET_TIME_absolute_max (expiration,
3017 schedule_next_ping (fal);
3023 fal->expires = GNUNET_TIME_absolute_max (expiration,
3029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3030 "Failed to add new address for `%4s'\n",
3031 GNUNET_i2s (&n->id));
3035 if (fal->validated == GNUNET_NO)
3037 fal->validated = GNUNET_YES;
3038 GNUNET_STATISTICS_update (stats,
3039 gettext_noop ("# peer addresses considered valid"),
3043 if (try == GNUNET_YES)
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3047 "Have new addresses, will try to trigger transmissions.\n");
3049 try_transmission_to_peer (n);
3056 * Add addresses in validated HELLO "h" to the set of addresses
3057 * we have for this peer.
3059 * @param cls closure ('struct NeighbourList*')
3060 * @param peer id of the peer, NULL for last call
3061 * @param h hello message for the peer (can be NULL)
3062 * @param err_msg NULL if successful, otherwise contains error message
3065 add_hello_for_peer (void *cls,
3066 const struct GNUNET_PeerIdentity *peer,
3067 const struct GNUNET_HELLO_Message *h,
3068 const char *err_msg)
3070 struct NeighbourList *n = cls;
3072 if (err_msg != NULL)
3075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3076 _("Error in communication with PEERINFO service: %s\n"),
3083 GNUNET_STATISTICS_update (stats,
3084 gettext_noop ("# outstanding peerinfo iterate requests"),
3091 return; /* no HELLO available */
3093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3094 "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
3098 if (GNUNET_YES != n->public_key_valid)
3100 GNUNET_HELLO_get_key (h, &n->publicKey);
3101 n->public_key_valid = GNUNET_YES;
3103 GNUNET_HELLO_iterate_addresses (h,
3105 &add_to_foreign_address_list,
3111 * Create a fresh entry in our neighbour list for the given peer.
3112 * Will try to transmit our current HELLO to the new neighbour.
3113 * Do not call this function directly, use 'setup_peer_check_blacklist.
3115 * @param peer the peer for which we create the entry
3116 * @param do_hello should we schedule transmitting a HELLO
3117 * @return the new neighbour list entry
3119 static struct NeighbourList *
3120 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
3123 struct NeighbourList *n;
3124 struct TransportPlugin *tp;
3125 struct ReadyList *rl;
3127 GNUNET_assert (0 != memcmp (peer,
3129 sizeof (struct GNUNET_PeerIdentity)));
3131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3132 "Setting up state for neighbour `%4s'\n",
3135 GNUNET_STATISTICS_update (stats,
3136 gettext_noop ("# active neighbours"),
3139 n = GNUNET_malloc (sizeof (struct NeighbourList));
3140 n->next = neighbours;
3144 GNUNET_TIME_relative_to_absolute
3145 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3146 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
3147 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3148 MAX_BANDWIDTH_CARRY_S);
3152 if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
3154 rl = GNUNET_malloc (sizeof (struct ReadyList));
3156 rl->next = n->plugins;
3159 rl->addresses = NULL;
3163 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
3165 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
3166 &neighbour_timeout_task, n);
3169 GNUNET_STATISTICS_update (stats,
3170 gettext_noop ("# peerinfo new neighbor iterate requests"),
3173 GNUNET_STATISTICS_update (stats,
3174 gettext_noop ("# outstanding peerinfo iterate requests"),
3177 n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
3178 GNUNET_TIME_UNIT_FOREVER_REL,
3179 &add_hello_for_peer, n);
3181 GNUNET_STATISTICS_update (stats,
3182 gettext_noop ("# HELLO's sent to new neighbors"),
3185 if (NULL != our_hello)
3186 transmit_to_peer (NULL, NULL, 0,
3187 HELLO_ADDRESS_EXPIRATION,
3188 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
3196 * Function called after we have checked if communicating
3197 * with a given peer is acceptable.
3199 * @param cls closure
3200 * @param n NULL if communication is not acceptable
3202 typedef void (*SetupContinuation)(void *cls,
3203 struct NeighbourList *n);
3207 * Information kept for each client registered to perform
3213 * This is a linked list.
3215 struct Blacklisters *next;
3218 * This is a linked list.
3220 struct Blacklisters *prev;
3223 * Client responsible for this entry.
3225 struct GNUNET_SERVER_Client *client;
3228 * Blacklist check that we're currently performing.
3230 struct BlacklistCheck *bc;
3236 * Head of DLL of blacklisting clients.
3238 static struct Blacklisters *bl_head;
3241 * Tail of DLL of blacklisting clients.
3243 static struct Blacklisters *bl_tail;
3247 * Context we use when performing a blacklist check.
3249 struct BlacklistCheck
3253 * This is a linked list.
3255 struct BlacklistCheck *next;
3258 * This is a linked list.
3260 struct BlacklistCheck *prev;
3263 * Peer being checked.
3265 struct GNUNET_PeerIdentity peer;
3268 * Option for setup neighbour afterwards.
3273 * Continuation to call with the result.
3275 SetupContinuation cont;
3283 * Current transmission request handle for this client, or NULL if no
3284 * request is pending.
3286 struct GNUNET_CONNECTION_TransmitHandle *th;
3289 * Our current position in the blacklisters list.
3291 struct Blacklisters *bl_pos;
3294 * Current task performing the check.
3296 GNUNET_SCHEDULER_TaskIdentifier task;
3301 * Head of DLL of active blacklisting queries.
3303 static struct BlacklistCheck *bc_head;
3306 * Tail of DLL of active blacklisting queries.
3308 static struct BlacklistCheck *bc_tail;
3312 * Perform next action in the blacklist check.
3314 * @param cls the 'struct BlacklistCheck*'
3318 do_blacklist_check (void *cls,
3319 const struct GNUNET_SCHEDULER_TaskContext *tc);
3322 * Transmit blacklist query to the client.
3324 * @param cls the 'struct BlacklistCheck'
3325 * @param size number of bytes allowed
3326 * @param buf where to copy the message
3327 * @return number of bytes copied to buf
3330 transmit_blacklist_message (void *cls,
3334 struct BlacklistCheck *bc = cls;
3335 struct Blacklisters *bl;
3336 struct BlacklistMessage bm;
3341 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
3342 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3344 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3345 "Failed to send blacklist test for peer `%s' to client\n",
3346 GNUNET_i2s (&bc->peer));
3350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3351 "Sending blacklist test for peer `%s' to client\n",
3352 GNUNET_i2s (&bc->peer));
3355 bm.header.size = htons (sizeof (struct BlacklistMessage));
3356 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
3357 bm.is_allowed = htonl (0);
3359 memcpy (buf, &bm, sizeof (bm));
3360 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
3366 * Perform next action in the blacklist check.
3368 * @param cls the 'struct BlacklistCheck*'
3372 do_blacklist_check (void *cls,
3373 const struct GNUNET_SCHEDULER_TaskContext *tc)
3375 struct BlacklistCheck *bc = cls;
3376 struct Blacklisters *bl;
3378 bc->task = GNUNET_SCHEDULER_NO_TASK;
3383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3384 "No blacklist clients active, will now setup neighbour record for peer `%s'\n",
3385 GNUNET_i2s (&bc->peer));
3387 bc->cont (bc->cont_cls,
3388 setup_new_neighbour (&bc->peer, bc->do_hello));
3395 bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
3396 sizeof (struct BlacklistMessage),
3397 GNUNET_TIME_UNIT_FOREVER_REL,
3398 &transmit_blacklist_message,
3405 * Obtain a 'struct NeighbourList' for the given peer. If such an entry
3406 * does not yet exist, check the blacklist. If the blacklist says creating
3407 * one is acceptable, create one and call the continuation; otherwise
3408 * call the continuation with NULL.
3410 * @param peer peer to setup or look up a struct NeighbourList for
3411 * @param do_hello should we also schedule sending our HELLO to the peer
3412 * if this is a new record
3413 * @param cont function to call with the 'struct NeigbhbourList*'
3414 * @param cont_cls closure for cont
3417 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
3419 SetupContinuation cont,
3422 struct NeighbourList *n;
3423 struct BlacklistCheck *bc;
3425 n = find_neighbour(peer);
3429 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3430 "Neighbour record exists for peer `%s'\n",
3437 if (bl_head == NULL)
3440 cont (cont_cls, setup_new_neighbour (peer, do_hello));
3442 setup_new_neighbour(peer, do_hello);
3445 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3446 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3448 bc->do_hello = do_hello;
3450 bc->cont_cls = cont_cls;
3451 bc->bl_pos = bl_head;
3452 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3458 * Function called with the result of querying a new blacklister about
3459 * it being allowed (or not) to continue to talk to an existing neighbour.
3461 * @param cls the original 'struct NeighbourList'
3462 * @param n NULL if we need to disconnect
3465 confirm_or_drop_neighbour (void *cls,
3466 struct NeighbourList *n)
3468 struct NeighbourList * orig = cls;
3473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3474 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&orig->id),
3475 "confirm_or_drop_neighboUr");
3477 GNUNET_STATISTICS_update (stats,
3478 gettext_noop ("# disconnects due to blacklist"),
3481 disconnect_neighbour (orig, GNUNET_NO);
3487 * Handle a request to start a blacklist.
3489 * @param cls closure (always NULL)
3490 * @param client identification of the client
3491 * @param message the actual message
3494 handle_blacklist_init (void *cls,
3495 struct GNUNET_SERVER_Client *client,
3496 const struct GNUNET_MessageHeader *message)
3498 struct Blacklisters *bl;
3499 struct BlacklistCheck *bc;
3500 struct NeighbourList *n;
3505 if (bl->client == client)
3508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3513 bl = GNUNET_malloc (sizeof (struct Blacklisters));
3514 bl->client = client;
3515 GNUNET_SERVER_client_keep (client);
3516 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
3517 /* confirm that all existing connections are OK! */
3521 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3522 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3524 bc->do_hello = GNUNET_NO;
3525 bc->cont = &confirm_or_drop_neighbour;
3528 if (n == neighbours) /* all would wait for the same client, no need to
3529 create more than just the first task right now */
3530 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3538 * Handle a request to blacklist a peer.
3540 * @param cls closure (always NULL)
3541 * @param client identification of the client
3542 * @param message the actual message
3545 handle_blacklist_reply (void *cls,
3546 struct GNUNET_SERVER_Client *client,
3547 const struct GNUNET_MessageHeader *message)
3549 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
3550 struct Blacklisters *bl;
3551 struct BlacklistCheck *bc;
3554 while ( (bl != NULL) &&
3555 (bl->client != client) )
3560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3561 "Blacklist client disconnected\n");
3563 /* FIXME: other error handling here!? */
3564 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3569 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
3572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3573 "Blacklist check failed, peer not allowed\n");
3575 bc->cont (bc->cont_cls, NULL);
3576 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
3582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3583 "Blacklist check succeeded, continuing with checks\n");
3585 bc->bl_pos = bc->bl_pos->next;
3586 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3589 /* check if any other bc's are waiting for this blacklister */
3593 if ( (bc->bl_pos == bl) &&
3594 (GNUNET_SCHEDULER_NO_TASK == bc->task) )
3595 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3603 * Send periodic PING messages to a given foreign address.
3605 * @param cls our 'struct PeriodicValidationContext*'
3606 * @param tc task context
3609 send_periodic_ping (void *cls,
3610 const struct GNUNET_SCHEDULER_TaskContext *tc)
3612 struct ForeignAddressList *peer_address = cls;
3613 struct TransportPlugin *tp;
3614 struct ValidationEntry *va;
3615 struct NeighbourList *neighbour;
3616 struct TransportPingMessage ping;
3617 struct CheckAddressExistsClosure caec;
3619 uint16_t hello_size;
3623 peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3624 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3626 tp = peer_address->ready_list->plugin;
3627 neighbour = peer_address->ready_list->neighbour;
3628 if (GNUNET_YES != neighbour->public_key_valid)
3630 /* no public key yet, try again later */
3631 schedule_next_ping (peer_address);
3634 caec.addr = peer_address->addr;
3635 caec.addrlen = peer_address->addrlen;
3636 caec.tname = tp->short_name;
3637 caec.session = peer_address->session;
3638 caec.exists = GNUNET_NO;
3639 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3640 &check_address_exists,
3642 if (caec.exists == GNUNET_YES)
3644 /* During validation attempts we will likely trigger the other
3645 peer trying to validate our address which in turn will cause
3646 it to send us its HELLO, so we expect to hit this case rather
3647 frequently. Only print something if we are very verbose. */
3648 #if DEBUG_TRANSPORT > 1
3649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3650 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3651 (peer_address->addr != NULL)
3652 ? a2s (tp->short_name,
3654 peer_address->addrlen)
3657 GNUNET_i2s (&neighbour->id));
3659 schedule_next_ping (peer_address);
3662 va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3663 va->transport_name = GNUNET_strdup (tp->short_name);
3664 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
3666 va->send_time = GNUNET_TIME_absolute_get();
3667 va->session = peer_address->session;
3668 if (peer_address->addr != NULL)
3670 va->addr = (const void*) &va[1];
3671 memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3672 va->addrlen = peer_address->addrlen;
3674 memcpy(&va->publicKey,
3675 &neighbour->publicKey,
3676 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3678 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
3679 &timeout_hello_validation,
3681 GNUNET_CONTAINER_multihashmap_put (validation_map,
3682 &neighbour->id.hashPubKey,
3684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3686 if (peer_address->validated != GNUNET_YES)
3687 hello_size = GNUNET_HELLO_size(our_hello);
3691 tsize = sizeof(struct TransportPingMessage) + hello_size;
3693 if (peer_address->addr != NULL)
3695 slen = strlen (tp->short_name) + 1;
3696 tsize += slen + peer_address->addrlen;
3700 slen = 0; /* make gcc happy */
3702 message_buf = GNUNET_malloc(tsize);
3703 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3704 ping.challenge = htonl(va->challenge);
3705 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3706 if (peer_address->validated != GNUNET_YES)
3708 memcpy(message_buf, our_hello, hello_size);
3711 if (peer_address->addr != NULL)
3713 ping.header.size = htons(sizeof(struct TransportPingMessage) +
3714 peer_address->addrlen +
3716 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3719 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3721 peer_address->addrlen);
3725 ping.header.size = htons(sizeof(struct TransportPingMessage));
3728 memcpy(&message_buf[hello_size],
3730 sizeof(struct TransportPingMessage));
3732 #if DEBUG_TRANSPORT_REVALIDATION
3733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3734 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s'\n",
3735 (peer_address->addr != NULL)
3736 ? a2s (peer_address->plugin->short_name,
3738 peer_address->addrlen)
3741 GNUNET_i2s (&neighbour->id),
3742 "HELLO", hello_size,
3745 if (peer_address->validated != GNUNET_YES)
3746 GNUNET_STATISTICS_update (stats,
3747 gettext_noop ("# PING with HELLO messages sent"),
3751 GNUNET_STATISTICS_update (stats,
3752 gettext_noop ("# PING without HELLO messages sent"),
3755 GNUNET_STATISTICS_update (stats,
3756 gettext_noop ("# PING messages sent for re-validation"),
3759 transmit_to_peer (NULL, peer_address,
3760 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3761 HELLO_VERIFICATION_TIMEOUT,
3763 GNUNET_YES, neighbour);
3764 GNUNET_free(message_buf);
3765 schedule_next_ping (peer_address);
3770 * Schedule the job that will cause us to send a PING to the
3771 * foreign address to evaluate its validity and latency.
3773 * @param fal address to PING
3776 schedule_next_ping (struct ForeignAddressList *fal)
3778 struct GNUNET_TIME_Relative delay;
3780 if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3782 delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3783 delay.rel_value /= 2; /* do before expiration */
3784 delay = GNUNET_TIME_relative_min (delay,
3785 LATENCY_EVALUATION_MAX_DELAY);
3786 if (GNUNET_YES != fal->estimated)
3788 delay = GNUNET_TIME_UNIT_ZERO;
3789 fal->estimated = GNUNET_YES;
3791 if (GNUNET_YES == fal->connected)
3793 delay = GNUNET_TIME_relative_min (delay,
3794 CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3796 /* FIXME: also adjust delay based on how close the last
3797 observed latency is to the latency of the best alternative */
3798 /* bound how fast we can go */
3799 delay = GNUNET_TIME_relative_max (delay,
3800 GNUNET_TIME_UNIT_SECONDS);
3801 /* randomize a bit (to avoid doing all at the same time) */
3802 delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3803 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(delay,
3804 &send_periodic_ping,
3812 * Function that will be called if we receive some payload
3813 * from another peer.
3815 * @param message the payload
3816 * @param n peer who claimed to be the sender
3819 handle_payload_message (const struct GNUNET_MessageHeader *message,
3820 struct NeighbourList *n)
3822 struct InboundMessage *im;
3823 struct TransportClient *cpos;
3826 msize = ntohs (message->size);
3827 if (n->received_pong == GNUNET_NO)
3830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3831 "Received message of type %u and size %u from `%4s', but no pong yet!\n",
3832 ntohs (message->type),
3833 ntohs (message->size),
3834 GNUNET_i2s (&n->id));
3836 GNUNET_free_non_null (n->pre_connect_message_buffer);
3837 n->pre_connect_message_buffer = GNUNET_malloc (msize);
3838 memcpy (n->pre_connect_message_buffer, message, msize);
3843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3844 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
3845 ntohs (message->type),
3846 ntohs (message->size),
3847 GNUNET_i2s (&n->id));
3849 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3852 n->quota_violation_count++;
3854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3855 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
3856 n->in_tracker.available_bytes_per_s__,
3857 n->quota_violation_count);
3859 /* Discount 32k per violation */
3860 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3865 if (n->quota_violation_count > 0)
3867 /* try to add 32k back */
3868 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3870 n->quota_violation_count--;
3873 GNUNET_STATISTICS_update (stats,
3874 gettext_noop ("# payload received from other peers"),
3877 /* transmit message to all clients */
3878 uint32_t ats_count = 2;
3879 size_t size = sizeof (struct InboundMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) + msize;
3880 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
3883 im = GNUNET_malloc (size);
3884 im->header.size = htons (size);
3885 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3887 im->ats_count = htonl(ats_count);
3888 /* Setting ATS data */
3889 (&(im->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
3890 (&(im->ats))[0].value = htonl (n->distance);
3891 (&(im->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
3892 (&(im->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
3893 (&(im->ats))[ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
3894 (&(im->ats))[ats_count].value = htonl (0);
3896 memcpy (&((&(im->ats))[ats_count+1]), message, msize);
3898 while (cpos != NULL)
3900 transmit_to_client (cpos, &im->header, GNUNET_YES);
3908 * Iterator over hash map entries. Checks if the given validation
3909 * entry is for the same challenge as what is given in the PONG.
3911 * @param cls the 'struct TransportPongMessage*'
3912 * @param key peer identity
3913 * @param value value in the hash map ('struct ValidationEntry')
3914 * @return GNUNET_YES if we should continue to
3915 * iterate (mismatch), GNUNET_NO if not (entry matched)
3918 check_pending_validation (void *cls,
3919 const GNUNET_HashCode * key,
3922 const struct TransportPongMessage *pong = cls;
3923 struct ValidationEntry *ve = value;
3924 struct AddValidatedAddressContext avac;
3925 unsigned int challenge = ntohl(pong->challenge);
3926 struct GNUNET_HELLO_Message *hello;
3927 struct GNUNET_PeerIdentity target;
3928 struct NeighbourList *n;
3929 struct ForeignAddressList *fal;
3930 struct OwnAddressList *oal;
3931 struct TransportPlugin *tp;
3932 struct GNUNET_MessageHeader *prem;
3938 ps = ntohs (pong->header.size);
3939 if (ps < sizeof (struct TransportPongMessage))
3941 GNUNET_break_op (0);
3944 addr = (const char*) &pong[1];
3945 slen = strlen (ve->transport_name) + 1;
3946 if ( (ps - sizeof (struct TransportPongMessage) < slen) ||
3947 (ve->challenge != challenge) ||
3948 (addr[slen-1] != '\0') ||
3949 (0 != strcmp (addr, ve->transport_name)) ||
3950 (ntohl (pong->purpose.size)
3951 != sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3953 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
3954 sizeof (struct GNUNET_PeerIdentity) + ps - sizeof (struct TransportPongMessage)) )
3959 alen = ps - sizeof (struct TransportPongMessage) - slen;
3960 switch (ntohl (pong->purpose.purpose))
3962 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN:
3963 if ( (ve->addrlen + slen != ntohl (pong->addrlen)) ||
3964 (0 != memcmp (&addr[slen],
3968 return GNUNET_YES; /* different entry, keep trying! */
3970 if (0 != memcmp (&pong->pid,
3972 sizeof (struct GNUNET_PeerIdentity)))
3974 GNUNET_break_op (0);
3978 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
3983 GNUNET_break_op (0);
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3989 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
3991 a2s (ve->transport_name,
3992 (const struct sockaddr *) ve->addr,
3994 ve->transport_name);
3997 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING:
3998 if (0 != memcmp (&pong->pid,
4000 sizeof (struct GNUNET_PeerIdentity)))
4004 GNUNET_asprintf(&peer, "%s",GNUNET_i2s (&pong->pid));
4006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4007 "Received PONG for different identity: I am `%s', PONG identity: `%s'\n",
4008 GNUNET_i2s (&my_identity),
4014 if (ve->addrlen != 0)
4016 /* must have been for a different validation entry */
4019 tp = find_transport (ve->transport_name);
4025 oal = tp->addresses;
4028 if ( (oal->addrlen == alen) &&
4029 (0 == memcmp (&oal[1],
4037 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4038 _("Not accepting PONG from `%s' with address `%s' since I cannot confirm using this address.\n"),
4039 GNUNET_i2s (&pong->pid),
4040 a2s (ve->transport_name,
4043 /* FIXME: since the sender of the PONG currently uses the
4044 wrong address (see FIMXE there!), we cannot run a
4045 proper check here... */
4051 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING,
4056 GNUNET_break_op (0);
4061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4062 "Confirmed that peer `%4s' is talking to us using address `%s' (%s) for us.\n",
4064 a2s (ve->transport_name,
4067 ve->transport_name);
4071 GNUNET_break_op (0);
4074 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
4076 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4077 _("Received expired signature. Check system time.\n"));
4080 GNUNET_STATISTICS_update (stats,
4081 gettext_noop ("# address validation successes"),
4084 /* create the updated HELLO */
4085 GNUNET_CRYPTO_hash (&ve->publicKey,
4086 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4087 &target.hashPubKey);
4088 if (ve->addr != NULL)
4090 avac.done = GNUNET_NO;
4092 hello = GNUNET_HELLO_create (&ve->publicKey,
4093 &add_validated_address,
4095 GNUNET_PEERINFO_add_peer (peerinfo,
4097 GNUNET_free (hello);
4099 n = find_neighbour (&target);
4102 n->publicKey = ve->publicKey;
4103 n->public_key_valid = GNUNET_YES;
4104 fal = add_peer_address (n,
4109 GNUNET_assert (fal != NULL);
4110 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
4111 fal->validated = GNUNET_YES;
4112 mark_address_connected (fal);
4113 GNUNET_STATISTICS_update (stats,
4114 gettext_noop ("# peer addresses considered valid"),
4117 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
4118 update_addr_value (fal, GNUNET_TIME_absolute_get_duration (ve->send_time).rel_value, GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
4120 schedule_next_ping (fal);
4121 if (n->latency.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
4122 n->latency = fal->latency;
4124 n->latency.rel_value = (fal->latency.rel_value + n->latency.rel_value) / 2;
4126 n->distance = fal->distance;
4127 if (GNUNET_NO == n->received_pong)
4129 n->received_pong = GNUNET_YES;
4130 notify_clients_connect (&target, n->latency, n->distance);
4131 if (NULL != (prem = n->pre_connect_message_buffer))
4133 n->pre_connect_message_buffer = NULL;
4134 handle_payload_message (prem, n);
4138 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4140 GNUNET_SCHEDULER_cancel (n->retry_task);
4141 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4142 try_transmission_to_peer (n);
4146 /* clean up validation entry */
4147 GNUNET_assert (GNUNET_YES ==
4148 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4151 abort_validation (NULL, NULL, ve);
4157 * Function that will be called if we receive a validation
4158 * of an address challenge that we transmitted to another
4159 * peer. Note that the validation should only be considered
4160 * acceptable if the challenge matches AND if the sender
4161 * address is at least a plausible address for this peer
4162 * (otherwise we may be seeing a MiM attack).
4164 * @param cls closure
4165 * @param message the pong message
4166 * @param peer who responded to our challenge
4167 * @param sender_address string describing our sender address (as observed
4168 * by the other peer in binary format)
4169 * @param sender_address_len number of bytes in 'sender_address'
4172 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
4173 const struct GNUNET_PeerIdentity *peer,
4174 const char *sender_address,
4175 size_t sender_address_len)
4177 if (0 == memcmp (peer,
4179 sizeof (struct GNUNET_PeerIdentity)))
4181 /* PONG send to self, ignore */
4182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4183 "Receiving `%s' message from myself\n",
4187 #if DEBUG_TRANSPORT > 1
4188 /* we get tons of these that just get discarded, only log
4189 if we are quite verbose */
4190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4191 "Receiving `%s' message from `%4s'.\n", "PONG",
4194 GNUNET_STATISTICS_update (stats,
4195 gettext_noop ("# PONG messages received"),
4198 if (GNUNET_SYSERR !=
4199 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
4201 &check_pending_validation,
4204 /* This is *expected* to happen a lot since we send
4205 PONGs to *all* known addresses of the sender of
4206 the PING, so most likely we get multiple PONGs
4207 per PING, and all but the first PONG will end up
4208 here. So really we should not print anything here
4209 unless we want to be very, very verbose... */
4210 #if DEBUG_TRANSPORT > 2
4211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4212 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
4224 * Try to validate a neighbour's address by sending him our HELLO and a PING.
4226 * @param cls the 'struct ValidationEntry*'
4227 * @param neighbour neighbour to validate, NULL if validation failed
4230 transmit_hello_and_ping (void *cls,
4231 struct NeighbourList *neighbour)
4233 struct ValidationEntry *va = cls;
4234 struct ForeignAddressList *peer_address;
4235 struct TransportPingMessage ping;
4236 uint16_t hello_size;
4239 struct GNUNET_PeerIdentity id;
4242 GNUNET_CRYPTO_hash (&va->publicKey,
4243 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4245 if (neighbour == NULL)
4247 /* FIXME: stats... */
4248 GNUNET_break (GNUNET_OK ==
4249 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4252 abort_validation (NULL, NULL, va);
4255 neighbour->publicKey = va->publicKey;
4256 neighbour->public_key_valid = GNUNET_YES;
4257 peer_address = add_peer_address (neighbour,
4258 va->transport_name, NULL,
4259 (const void*) &va[1],
4261 if (peer_address == NULL)
4263 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4264 "Failed to add peer `%4s' for plugin `%s'\n",
4265 GNUNET_i2s (&neighbour->id),
4266 va->transport_name);
4267 GNUNET_break (GNUNET_OK ==
4268 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4271 abort_validation (NULL, NULL, va);
4274 if (NULL == our_hello)
4275 refresh_hello_task (NULL, NULL);
4276 hello_size = GNUNET_HELLO_size(our_hello);
4277 slen = strlen(va->transport_name) + 1;
4278 tsize = sizeof(struct TransportPingMessage) + hello_size + va->addrlen + slen;
4279 message_buf = GNUNET_malloc(tsize);
4280 ping.challenge = htonl(va->challenge);
4281 ping.header.size = htons(sizeof(struct TransportPingMessage) + slen + va->addrlen);
4282 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
4283 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
4284 memcpy(message_buf, our_hello, hello_size);
4285 memcpy(&message_buf[hello_size],
4287 sizeof(struct TransportPingMessage));
4288 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
4291 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
4295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4296 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
4299 : a2s (va->transport_name,
4300 (const void*) &va[1], va->addrlen),
4302 GNUNET_i2s (&neighbour->id),
4303 "HELLO", hello_size,
4304 "PING", sizeof (struct TransportPingMessage) + va->addrlen + slen);
4307 GNUNET_STATISTICS_update (stats,
4308 gettext_noop ("# PING messages sent for initial validation"),
4311 transmit_to_peer (NULL, peer_address,
4312 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
4313 HELLO_VERIFICATION_TIMEOUT,
4315 GNUNET_YES, neighbour);
4316 GNUNET_free(message_buf);
4321 * Check if the given address is already being validated; if not,
4322 * append the given address to the list of entries that are being be
4323 * validated and initiate validation.
4325 * @param cls closure ('struct CheckHelloValidatedContext *')
4326 * @param tname name of the transport
4327 * @param expiration expiration time
4328 * @param addr the address
4329 * @param addrlen length of the address
4330 * @return GNUNET_OK (always)
4333 run_validation (void *cls,
4335 struct GNUNET_TIME_Absolute expiration,
4339 struct CheckHelloValidatedContext *chvc = cls;
4340 struct GNUNET_PeerIdentity id;
4341 struct TransportPlugin *tp;
4342 struct ValidationEntry *va;
4343 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4344 struct CheckAddressExistsClosure caec;
4345 struct OwnAddressList *oal;
4347 GNUNET_assert (addr != NULL);
4349 GNUNET_STATISTICS_update (stats,
4350 gettext_noop ("# peer addresses scheduled for validation"),
4353 tp = find_transport (tname);
4356 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
4357 GNUNET_ERROR_TYPE_BULK,
4359 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
4361 GNUNET_STATISTICS_update (stats,
4362 gettext_noop ("# peer addresses not validated (plugin not available)"),
4367 /* check if this is one of our own addresses */
4368 oal = tp->addresses;
4371 if ( (oal->addrlen == addrlen) &&
4372 (0 == memcmp (&oal[1],
4376 /* not plausible, this address is equivalent to our own address! */
4377 GNUNET_STATISTICS_update (stats,
4378 gettext_noop ("# peer addresses not validated (loopback)"),
4385 GNUNET_HELLO_get_key (chvc->hello, &pk);
4386 GNUNET_CRYPTO_hash (&pk,
4388 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4391 if (is_blacklisted(&id, tp))
4394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4395 "Attempted to validate blacklisted peer `%s' using `%s'!\n",
4403 caec.addrlen = addrlen;
4404 caec.session = NULL;
4406 caec.exists = GNUNET_NO;
4407 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
4408 &check_address_exists,
4410 if (caec.exists == GNUNET_YES)
4412 /* During validation attempts we will likely trigger the other
4413 peer trying to validate our address which in turn will cause
4414 it to send us its HELLO, so we expect to hit this case rather
4415 frequently. Only print something if we are very verbose. */
4416 #if DEBUG_TRANSPORT > 1
4417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4418 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
4419 a2s (tname, addr, addrlen),
4423 GNUNET_STATISTICS_update (stats,
4424 gettext_noop ("# peer addresses not validated (in progress)"),
4429 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
4432 va->transport_name = GNUNET_strdup (tname);
4433 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
4435 va->send_time = GNUNET_TIME_absolute_get();
4436 va->addr = (const void*) &va[1];
4437 memcpy (&va[1], addr, addrlen);
4438 va->addrlen = addrlen;
4439 GNUNET_HELLO_get_key (chvc->hello,
4441 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
4442 &timeout_hello_validation,
4444 GNUNET_CONTAINER_multihashmap_put (validation_map,
4447 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4448 setup_peer_check_blacklist (&id, GNUNET_NO,
4449 &transmit_hello_and_ping,
4456 * Check if addresses in validated hello "h" overlap with
4457 * those in "chvc->hello" and validate the rest.
4459 * @param cls closure
4460 * @param peer id of the peer, NULL for last call
4461 * @param h hello message for the peer (can be NULL)
4462 * @param err_msg NULL if successful, otherwise contains error message
4465 check_hello_validated (void *cls,
4466 const struct GNUNET_PeerIdentity *peer,
4467 const struct GNUNET_HELLO_Message *h,
4468 const char *err_msg)
4470 struct CheckHelloValidatedContext *chvc = cls;
4471 struct GNUNET_HELLO_Message *plain_hello;
4472 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4473 struct GNUNET_PeerIdentity target;
4474 struct NeighbourList *n;
4476 if (err_msg != NULL)
4479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4480 _("Error in communication with PEERINFO service: %s\n"),
4488 GNUNET_STATISTICS_update (stats,
4489 gettext_noop ("# outstanding peerinfo iterate requests"),
4493 if (GNUNET_NO == chvc->hello_known)
4495 /* notify PEERINFO about the peer now, so that we at least
4496 have the public key if some other component needs it */
4497 GNUNET_HELLO_get_key (chvc->hello, &pk);
4498 GNUNET_CRYPTO_hash (&pk,
4499 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4500 &target.hashPubKey);
4501 plain_hello = GNUNET_HELLO_create (&pk,
4504 GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
4505 GNUNET_free (plain_hello);
4506 #if DEBUG_TRANSPORT_HELLO
4507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4508 "PEERINFO had no `%s' message for peer `%4s', full validation needed.\n",
4510 GNUNET_i2s (&target));
4512 GNUNET_STATISTICS_update (stats,
4513 gettext_noop ("# new HELLOs requiring full validation"),
4516 GNUNET_HELLO_iterate_addresses (chvc->hello,
4523 GNUNET_STATISTICS_update (stats,
4524 gettext_noop ("# duplicate HELLO (peer known)"),
4529 if (chvc->ve_count == 0)
4531 GNUNET_CONTAINER_DLL_remove (chvc_head,
4540 #if DEBUG_TRANSPORT_HELLO
4541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4542 "PEERINFO had `%s' message for peer `%4s', validating only new addresses.\n",
4546 chvc->hello_known = GNUNET_YES;
4547 n = find_neighbour (peer);
4550 #if DEBUG_TRANSPORT_HELLO
4551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4552 "Calling hello_iterate_addresses for %s!\n",
4555 GNUNET_HELLO_iterate_addresses (h,
4557 &add_to_foreign_address_list,
4559 try_transmission_to_peer (n);
4563 #if DEBUG_TRANSPORT_HELLO
4564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4565 "No existing neighbor record for %s!\n",
4568 GNUNET_STATISTICS_update (stats,
4569 gettext_noop ("# no existing neighbour record (validating HELLO)"),
4573 GNUNET_STATISTICS_update (stats,
4574 gettext_noop ("# HELLO validations (update case)"),
4577 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
4579 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
4586 * Process HELLO-message.
4588 * @param plugin transport involved, may be NULL
4589 * @param message the actual message
4590 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
4593 process_hello (struct TransportPlugin *plugin,
4594 const struct GNUNET_MessageHeader *message)
4597 struct GNUNET_PeerIdentity target;
4598 const struct GNUNET_HELLO_Message *hello;
4599 struct CheckHelloValidatedContext *chvc;
4600 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
4601 struct NeighbourList *n;
4602 #if DEBUG_TRANSPORT_HELLO > 2
4606 hsize = ntohs (message->size);
4607 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
4608 (hsize < sizeof (struct GNUNET_MessageHeader)))
4611 return GNUNET_SYSERR;
4613 GNUNET_STATISTICS_update (stats,
4614 gettext_noop ("# HELLOs received for validation"),
4618 hello = (const struct GNUNET_HELLO_Message *) message;
4619 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
4621 #if DEBUG_TRANSPORT_HELLO
4622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4623 "Unable to get public key from `%s' for `%4s'!\n",
4625 GNUNET_i2s (&target));
4627 GNUNET_break_op (0);
4628 return GNUNET_SYSERR;
4630 GNUNET_CRYPTO_hash (&publicKey,
4631 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4632 &target.hashPubKey);
4634 #if DEBUG_TRANSPORT_HELLO
4635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4636 "Received `%s' message for `%4s'\n",
4638 GNUNET_i2s (&target));
4640 if (0 == memcmp (&my_identity,
4642 sizeof (struct GNUNET_PeerIdentity)))
4644 GNUNET_STATISTICS_update (stats,
4645 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
4650 n = find_neighbour (&target);
4652 (! n->public_key_valid) )
4654 GNUNET_HELLO_get_key (hello, &n->publicKey);
4655 n->public_key_valid = GNUNET_YES;
4658 /* check if load is too high before doing expensive stuff */
4659 if (GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
4661 GNUNET_STATISTICS_update (stats,
4662 gettext_noop ("# HELLOs ignored due to high load"),
4665 #if DEBUG_TRANSPORT_HELLO
4666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4667 "Ignoring `%s' for `%4s', load too high.\n",
4669 GNUNET_i2s (&target));
4676 while (NULL != chvc)
4678 if (GNUNET_HELLO_equals (hello,
4680 GNUNET_TIME_absolute_get ()).abs_value > 0)
4682 #if DEBUG_TRANSPORT_HELLO > 2
4683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4684 "Received duplicate `%s' message for `%4s'; ignored\n",
4686 GNUNET_i2s (&target));
4688 return GNUNET_OK; /* validation already pending */
4690 if (GNUNET_HELLO_size (hello) == GNUNET_HELLO_size (chvc->hello))
4691 GNUNET_break (0 != memcmp (hello, chvc->hello,
4692 GNUNET_HELLO_size(hello)));
4697 struct NeighbourList *temp_neighbor = find_neighbour(&target);
4698 if ((NULL != temp_neighbor))
4700 fprintf(stderr, "Already know peer, ignoring hello\n");
4705 #if DEBUG_TRANSPORT_HELLO > 2
4708 my_id = GNUNET_strdup(GNUNET_i2s(plugin->env.my_identity));
4710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4711 "%s: Starting validation of `%s' message for `%4s' via '%s' of size %u\n",
4714 GNUNET_i2s (&target),
4716 GNUNET_HELLO_size(hello));
4721 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
4723 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
4724 memcpy (&chvc[1], hello, hsize);
4725 GNUNET_CONTAINER_DLL_insert (chvc_head,
4728 /* finally, check if HELLO was previously validated
4729 (continuation will then schedule actual validation) */
4730 GNUNET_STATISTICS_update (stats,
4731 gettext_noop ("# peerinfo process hello iterate requests"),
4734 GNUNET_STATISTICS_update (stats,
4735 gettext_noop ("# outstanding peerinfo iterate requests"),
4738 chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
4740 HELLO_VERIFICATION_TIMEOUT,
4741 &check_hello_validated, chvc);
4747 * The peer specified by the given neighbour has timed-out or a plugin
4748 * has disconnected. We may either need to do nothing (other plugins
4749 * still up), or trigger a full disconnect and clean up. This
4750 * function updates our state and does the necessary notifications.
4751 * Also notifies our clients that the neighbour is now officially
4754 * @param n the neighbour list entry for the peer
4755 * @param check GNUNET_YES to check if ALL addresses for this peer
4756 * are gone, GNUNET_NO to force a disconnect of the peer
4757 * regardless of whether other addresses exist.
4760 disconnect_neighbour (struct NeighbourList *n, int check)
4762 struct ReadyList *rpos;
4763 struct NeighbourList *npos;
4764 struct NeighbourList *nprev;
4765 struct MessageQueue *mq;
4766 struct ForeignAddressList *peer_addresses;
4767 struct ForeignAddressList *peer_pos;
4769 if (GNUNET_YES == check)
4772 while (NULL != rpos)
4774 peer_addresses = rpos->addresses;
4775 while (peer_addresses != NULL)
4777 // Do not disconnect if: an address is connected or an inbound address exists
4778 if ((GNUNET_YES == peer_addresses->connected) || (peer_addresses->addrlen == 0))
4781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4782 "NOT Disconnecting from `%4s', still have live address `%s'!\n",
4783 GNUNET_i2s (&n->id),
4784 a2s (peer_addresses->ready_list->plugin->short_name,
4785 peer_addresses->addr,
4786 peer_addresses->addrlen));
4788 return; /* still connected */
4790 peer_addresses = peer_addresses->next;
4796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4797 "Disconnecting from `%4s'\n",
4798 GNUNET_i2s (&n->id));
4801 /* remove n from neighbours list */
4804 while ((npos != NULL) && (npos != n))
4809 GNUNET_assert (npos != NULL);
4811 neighbours = n->next;
4813 nprev->next = n->next;
4815 /* notify all clients about disconnect */
4816 if (GNUNET_YES == n->received_pong)
4817 notify_clients_disconnect (&n->id);
4819 /* clean up all plugins, cancel connections and pending transmissions */
4820 while (NULL != (rpos = n->plugins))
4822 n->plugins = rpos->next;
4823 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
4824 while (rpos->addresses != NULL)
4826 peer_pos = rpos->addresses;
4827 rpos->addresses = peer_pos->next;
4828 if (peer_pos->connected == GNUNET_YES)
4829 GNUNET_STATISTICS_update (stats,
4830 gettext_noop ("# connected addresses"),
4833 if (GNUNET_YES == peer_pos->validated)
4834 GNUNET_STATISTICS_update (stats,
4835 gettext_noop ("# peer addresses considered valid"),
4838 if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
4840 GNUNET_SCHEDULER_cancel (peer_pos->revalidate_task);
4841 peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
4843 GNUNET_free(peer_pos->ressources);
4844 peer_pos->ressources = NULL;
4845 GNUNET_free(peer_pos->quality);
4846 peer_pos->ressources = NULL;
4847 GNUNET_free(peer_pos);
4849 ats->stat.recreate_problem = GNUNET_YES;
4854 /* free all messages on the queue */
4855 while (NULL != (mq = n->messages_head))
4857 GNUNET_STATISTICS_update (stats,
4858 gettext_noop ("# bytes in message queue for other peers"),
4859 - (int64_t) mq->message_buf_size,
4861 GNUNET_STATISTICS_update (stats,
4862 gettext_noop ("# bytes discarded due to disconnect"),
4863 mq->message_buf_size,
4865 GNUNET_CONTAINER_DLL_remove (n->messages_head,
4868 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4870 sizeof(struct GNUNET_PeerIdentity)));
4874 while (NULL != (mq = n->cont_head))
4877 GNUNET_CONTAINER_DLL_remove (n->cont_head,
4880 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4882 sizeof(struct GNUNET_PeerIdentity)));
4886 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
4888 GNUNET_SCHEDULER_cancel (n->timeout_task);
4889 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4891 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4893 GNUNET_SCHEDULER_cancel (n->retry_task);
4894 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4896 if (n->piter != NULL)
4898 GNUNET_PEERINFO_iterate_cancel (n->piter);
4899 GNUNET_STATISTICS_update (stats,
4900 gettext_noop ("# outstanding peerinfo iterate requests"),
4905 /* finally, free n itself */
4906 GNUNET_STATISTICS_update (stats,
4907 gettext_noop ("# active neighbours"),
4910 GNUNET_free_non_null (n->pre_connect_message_buffer);
4916 * We have received a PING message from someone. Need to send a PONG message
4917 * in response to the peer by any means necessary.
4920 handle_ping (void *cls, const struct GNUNET_MessageHeader *message,
4921 const struct GNUNET_PeerIdentity *peer,
4922 struct Session *session,
4923 const char *sender_address,
4924 uint16_t sender_address_len)
4926 struct TransportPlugin *plugin = cls;
4927 struct SessionHeader *session_header = (struct SessionHeader*) session;
4928 struct TransportPingMessage *ping;
4929 struct TransportPongMessage *pong;
4930 struct NeighbourList *n;
4931 struct ReadyList *rl;
4932 struct ForeignAddressList *fal;
4933 struct OwnAddressList *oal;
4939 if (ntohs (message->size) < sizeof (struct TransportPingMessage))
4941 GNUNET_break_op (0);
4942 return GNUNET_SYSERR;
4945 ping = (struct TransportPingMessage *) message;
4946 if (0 != memcmp (&ping->target,
4947 plugin->env.my_identity,
4948 sizeof (struct GNUNET_PeerIdentity)))
4951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4952 _("Received `%s' message from `%s' destined for `%s' which is not me!\n"),
4954 (sender_address != NULL)
4955 ? a2s (plugin->short_name,
4956 (const struct sockaddr *)sender_address,
4959 GNUNET_i2s (&ping->target));
4961 return GNUNET_SYSERR;
4964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4965 "Processing `%s' from `%s'\n",
4967 (sender_address != NULL)
4968 ? a2s (plugin->short_name,
4969 (const struct sockaddr *)sender_address,
4973 GNUNET_STATISTICS_update (stats,
4974 gettext_noop ("# PING messages received"),
4977 addr = (const char*) &ping[1];
4978 alen = ntohs (message->size) - sizeof (struct TransportPingMessage);
4979 slen = strlen (plugin->short_name) + 1;
4982 /* peer wants to confirm that we have an outbound connection to him */
4983 if (session == NULL)
4985 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4986 _("Refusing to create PONG since I do not have a session with `%s'.\n"),
4988 return GNUNET_SYSERR;
4990 /* FIXME-urg: the use of 'sender_address' in the code below is doubly-wrong:
4991 1) it is NULL when we need to have a real value
4992 2) it is documented to be the address of the sender (source-IP), where
4993 what we actually want is our LISTEN IP (what we 'bound' to); which we don't even
4996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4997 "Creating PONG indicating that we received a connection at our address `%s' from `%s'.\n",
4998 a2s (plugin->short_name,
5000 sender_address_len),
5003 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len + slen);
5004 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len + slen);
5005 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
5006 pong->purpose.size =
5007 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
5009 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
5010 sizeof (struct GNUNET_PeerIdentity) + sender_address_len + slen);
5011 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING);
5012 pong->challenge = ping->challenge;
5013 pong->addrlen = htonl(sender_address_len + slen);
5016 sizeof(struct GNUNET_PeerIdentity));
5020 if ((sender_address!=NULL) && (sender_address_len > 0))
5021 memcpy (&((char*)&pong[1])[slen],
5023 sender_address_len);
5024 if (GNUNET_TIME_absolute_get_remaining (session_header->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
5026 /* create / update cached sig */
5028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5029 "Creating PONG signature to indicate active connection.\n");
5031 session_header->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
5032 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
5033 GNUNET_assert (GNUNET_OK ==
5034 GNUNET_CRYPTO_rsa_sign (my_private_key,
5036 &session_header->pong_signature));
5040 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
5042 memcpy (&pong->signature,
5043 &session_header->pong_signature,
5044 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5050 /* peer wants to confirm that this is one of our addresses */
5054 plugin->api->check_address (plugin->api->cls,
5058 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5059 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
5060 a2s (plugin->short_name,
5065 oal = plugin->addresses;
5068 if ( (oal->addrlen == alen) &&
5075 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
5076 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
5077 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
5078 pong->purpose.size =
5079 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
5081 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
5082 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
5083 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
5084 pong->challenge = ping->challenge;
5085 pong->addrlen = htonl(alen + slen);
5088 sizeof(struct GNUNET_PeerIdentity));
5089 memcpy (&pong[1], plugin->short_name, slen);
5090 memcpy (&((char*)&pong[1])[slen], addr, alen);
5091 if ( (oal != NULL) &&
5092 (GNUNET_TIME_absolute_get_remaining (oal->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4) )
5094 /* create / update cached sig */
5096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5097 "Creating PONG signature to indicate ownership.\n");
5099 oal->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
5100 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5101 GNUNET_assert (GNUNET_OK ==
5102 GNUNET_CRYPTO_rsa_sign (my_private_key,
5104 &oal->pong_signature));
5105 memcpy (&pong->signature,
5106 &oal->pong_signature,
5107 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5109 else if (oal == NULL)
5111 /* not using cache (typically DV-only) */
5112 pong->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
5113 GNUNET_assert (GNUNET_OK ==
5114 GNUNET_CRYPTO_rsa_sign (my_private_key,
5120 /* can used cached version */
5121 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5122 memcpy (&pong->signature,
5123 &oal->pong_signature,
5124 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5127 n = find_neighbour(peer);
5128 GNUNET_assert (n != NULL);
5129 did_pong = GNUNET_NO;
5130 /* first try reliable response transmission */
5134 fal = rl->addresses;
5137 if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
5140 ntohs (pong->header.size),
5141 TRANSPORT_PONG_PRIORITY,
5142 HELLO_VERIFICATION_TIMEOUT,
5149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5150 "Transmitted PONG to `%s' via reliable mechanism\n",
5153 GNUNET_STATISTICS_update (stats,
5154 gettext_noop ("# PONGs unicast via reliable transport"),
5160 did_pong = GNUNET_YES;
5165 /* no reliable method found, do multicast */
5166 GNUNET_STATISTICS_update (stats,
5167 gettext_noop ("# PONGs multicast to all available addresses"),
5173 fal = rl->addresses;
5176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5177 "Transmitting PONG to `%s' via unreliable mechanism `%s':%s\n",
5179 a2s (rl->plugin->short_name,
5182 rl->plugin->short_name);
5183 transmit_to_peer(NULL, fal,
5184 TRANSPORT_PONG_PRIORITY,
5185 HELLO_VERIFICATION_TIMEOUT,
5187 ntohs(pong->header.size),
5190 did_pong = GNUNET_YES;
5196 if (GNUNET_YES != did_pong)
5197 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5198 _("Could not send PONG to `%s': no address available\n"),
5205 * Function called by the plugin for each received message. Update
5206 * data volumes, possibly notify plugins about reducing the rate at
5207 * which they read from the socket and generally forward to our
5210 * @param cls the "struct TransportPlugin *" we gave to the plugin
5211 * @param peer (claimed) identity of the other peer
5212 * @param message the message, NULL if we only care about
5213 * learning about the delay until we should receive again
5214 * @param ats_data information for automatic transport selection
5215 * @param ats_count number of elements in ats not including 0-terminator
5216 * @param session identifier used for this session (can be NULL)
5217 * @param sender_address binary address of the sender (if observed)
5218 * @param sender_address_len number of bytes in sender_address
5219 * @return how long in ms the plugin should wait until receiving more data
5220 * (plugins that do not support this, can ignore the return value)
5222 static struct GNUNET_TIME_Relative
5223 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
5224 const struct GNUNET_MessageHeader *message,
5225 const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
5227 struct Session *session,
5228 const char *sender_address,
5229 uint16_t sender_address_len)
5231 struct TransportPlugin *plugin = cls;
5232 struct ReadyList *service_context;
5233 struct ForeignAddressList *peer_address;
5235 struct NeighbourList *n;
5236 struct GNUNET_TIME_Relative ret;
5240 if (0 == memcmp (peer,
5242 sizeof (struct GNUNET_PeerIdentity)))
5244 /* refuse to receive from myself */
5246 return GNUNET_TIME_UNIT_FOREVER_REL;
5248 if (is_blacklisted (peer, plugin))
5249 return GNUNET_TIME_UNIT_FOREVER_REL;
5250 n = find_neighbour (peer);
5252 n = setup_new_neighbour (peer, GNUNET_YES);
5253 service_context = n->plugins;
5254 while ((service_context != NULL) && (plugin != service_context->plugin))
5255 service_context = service_context->next;
5256 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
5257 peer_address = NULL;
5260 for (c=0; c<ats_count; c++)
5261 if (ntohl(ats_data[c].type) == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
5262 distance = ntohl(ats_data[c].value);
5265 if (message != NULL)
5267 if ( (session != NULL) ||
5268 (sender_address != NULL) )
5269 peer_address = add_peer_address (n,
5273 sender_address_len);
5274 if (peer_address != NULL)
5276 update_addr_ats(peer_address, ats_data, ats_count);
5277 update_addr_value(peer_address, distance, GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5279 peer_address->distance = distance;
5280 if (GNUNET_YES == peer_address->validated)
5281 mark_address_connected (peer_address);
5282 peer_address->timeout
5283 = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5284 schedule_next_ping (peer_address);
5286 /* update traffic received amount ... */
5287 msize = ntohs (message->size);
5289 GNUNET_STATISTICS_update (stats,
5290 gettext_noop ("# bytes received from other peers"),
5293 n->distance = distance;
5295 GNUNET_TIME_relative_to_absolute
5296 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5297 GNUNET_SCHEDULER_cancel (n->timeout_task);
5299 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
5300 &neighbour_timeout_task, n);
5301 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
5303 /* dropping message due to frequent inbound volume violations! */
5304 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
5305 GNUNET_ERROR_TYPE_BULK,
5307 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
5308 n->in_tracker.available_bytes_per_s__,
5309 n->quota_violation_count);
5310 GNUNET_STATISTICS_update (stats,
5311 gettext_noop ("# bandwidth quota violations by other peers"),
5314 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
5316 if ((ntohs(message->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ATS) &&
5317 (ntohs(message->size) == (sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t))))
5319 uint32_t value = ntohl(*((uint32_t *) &message[1]));
5320 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GNUNET_MESSAGE_TYPE_TRANSPORT_ATS: %i \n", value);
5321 /* Force ressource and quality update */
5322 if ((value == 4) && (ats != NULL))
5324 ats->stat.modified_resources = GNUNET_YES;
5325 ats->stat.modified_quality = GNUNET_YES;
5327 /* Force cost update */
5328 if ((value == 3) && (ats != NULL))
5329 ats->stat.modified_resources = GNUNET_YES;
5330 /* Force quality update */
5331 if ((value == 2) && (ats != NULL))
5332 ats->stat.modified_quality = GNUNET_YES;
5333 /* Force full rebuild */
5334 if ((value == 1) && (ats != NULL))
5335 ats->stat.recreate_problem = GNUNET_YES;
5339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5340 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
5341 ntohs (message->type),
5342 ntohs (message->size),
5345 switch (ntohs (message->type))
5347 case GNUNET_MESSAGE_TYPE_HELLO:
5348 GNUNET_STATISTICS_update (stats,
5349 gettext_noop ("# HELLO messages received from other peers"),
5352 process_hello (plugin, message);
5354 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
5355 handle_ping (plugin, message, peer, session, sender_address, sender_address_len);
5356 if (! n->received_pong)
5357 transmit_plain_ping (n);
5359 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
5360 handle_pong (plugin, message, peer, sender_address, sender_address_len);
5362 case GNUNET_MESSAGE_TYPE_TRANSPORT_ATS:
5365 handle_payload_message (message, n);
5369 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
5370 if (ret.rel_value > 0)
5373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5374 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
5375 (unsigned long long) n->in_tracker.consumption_since_last_update__,
5376 (unsigned int) n->in_tracker.available_bytes_per_s__,
5377 (unsigned long long) ret.rel_value);
5379 GNUNET_STATISTICS_update (stats,
5380 gettext_noop ("# ms throttling suggested"),
5381 (int64_t) ret.rel_value,
5388 * Handle START-message. This is the first message sent to us
5389 * by any client which causes us to add it to our list.
5391 * @param cls closure (always NULL)
5392 * @param client identification of the client
5393 * @param message the actual message
5396 handle_start (void *cls,
5397 struct GNUNET_SERVER_Client *client,
5398 const struct GNUNET_MessageHeader *message)
5400 const struct StartMessage *start;
5401 struct TransportClient *c;
5402 struct ConnectInfoMessage * cim;
5403 struct NeighbourList *n;
5407 start = (const struct StartMessage*) message;
5409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5410 "Received `%s' request from client\n", "START");
5415 if (c->client == client)
5417 /* client already on our list! */
5419 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5424 if ( (GNUNET_NO != ntohl (start->do_check)) &&
5425 (0 != memcmp (&start->self,
5427 sizeof (struct GNUNET_PeerIdentity))) )
5429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5430 _("Rejecting control connection from peer `%s', which is not me!\n"),
5431 GNUNET_i2s (&start->self));
5432 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5435 c = GNUNET_malloc (sizeof (struct TransportClient));
5439 if (our_hello != NULL)
5442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5443 "Sending our own `%s' to new client\n", "HELLO");
5445 transmit_to_client (c,
5446 (const struct GNUNET_MessageHeader *) our_hello,
5448 /* tell new client about all existing connections */
5450 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
5451 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
5455 cim = GNUNET_malloc (size);
5456 cim->header.size = htons (size);
5457 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
5458 cim->ats_count = htonl(ats_count);
5459 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
5460 (&(cim->ats))[2].value = htonl (0);
5464 if (GNUNET_YES == n->received_pong)
5466 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5467 (&(cim->ats))[0].value = htonl (n->distance);
5468 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
5469 (&(cim->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
5471 transmit_to_client (c, &cim->header, GNUNET_NO);
5479 #if DEBUG_TRANSPORT_HELLO
5480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5481 "No HELLO created yet, will transmit HELLO to client later!\n");
5485 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5490 * Handle HELLO-message.
5492 * @param cls closure (always NULL)
5493 * @param client identification of the client
5494 * @param message the actual message
5497 handle_hello (void *cls,
5498 struct GNUNET_SERVER_Client *client,
5499 const struct GNUNET_MessageHeader *message)
5503 GNUNET_STATISTICS_update (stats,
5504 gettext_noop ("# HELLOs received from clients"),
5507 ret = process_hello (NULL, message);
5508 GNUNET_SERVER_receive_done (client, ret);
5513 * Closure for 'transmit_client_message'; followed by
5514 * 'msize' bytes of the actual message.
5516 struct TransmitClientMessageContext
5519 * Client on whom's behalf we are sending.
5521 struct GNUNET_SERVER_Client *client;
5524 * Timeout for the transmission.
5526 struct GNUNET_TIME_Absolute timeout;
5534 * Size of the message in bytes.
5541 * Schedule transmission of a message we got from a client to a peer.
5543 * @param cls the 'struct TransmitClientMessageContext*'
5544 * @param n destination, or NULL on error (in that case, drop the message)
5547 transmit_client_message (void *cls,
5548 struct NeighbourList *n)
5550 struct TransmitClientMessageContext *tcmc = cls;
5551 struct TransportClient *tc;
5554 while ((tc != NULL) && (tc->client != tcmc->client))
5559 transmit_to_peer (tc, NULL, tcmc->priority,
5560 GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
5562 tcmc->msize, GNUNET_NO, n);
5564 GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
5565 GNUNET_SERVER_client_drop (tcmc->client);
5571 * Handle SEND-message.
5573 * @param cls closure (always NULL)
5574 * @param client identification of the client
5575 * @param message the actual message
5578 handle_send (void *cls,
5579 struct GNUNET_SERVER_Client *client,
5580 const struct GNUNET_MessageHeader *message)
5582 const struct OutboundMessage *obm;
5583 const struct GNUNET_MessageHeader *obmm;
5584 struct TransmitClientMessageContext *tcmc;
5588 size = ntohs (message->size);
5590 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
5593 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5596 GNUNET_STATISTICS_update (stats,
5597 gettext_noop ("# payload received for other peers"),
5600 obm = (const struct OutboundMessage *) message;
5601 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
5602 msize = size - sizeof (struct OutboundMessage);
5604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5605 "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
5606 "SEND", GNUNET_i2s (&obm->peer),
5610 tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
5611 tcmc->client = client;
5612 tcmc->priority = ntohl (obm->priority);
5613 tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
5614 tcmc->msize = msize;
5615 /* FIXME: this memcpy can be up to 7% of our total runtime */
5616 memcpy (&tcmc[1], obmm, msize);
5617 GNUNET_SERVER_client_keep (client);
5618 setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
5619 &transmit_client_message,
5625 * Handle request connect message
5627 * @param cls closure (always NULL)
5628 * @param client identification of the client
5629 * @param message the actual message
5632 handle_request_connect (void *cls,
5633 struct GNUNET_SERVER_Client *client,
5634 const struct GNUNET_MessageHeader *message)
5636 const struct TransportRequestConnectMessage *trcm =
5637 (const struct TransportRequestConnectMessage *) message;
5639 GNUNET_STATISTICS_update (stats,
5640 gettext_noop ("# REQUEST CONNECT messages received"),
5644 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
5645 "Received a request connect message for peer `%s'\n",
5646 GNUNET_i2s(&trcm->peer));
5648 setup_peer_check_blacklist (&trcm->peer, GNUNET_YES,
5650 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5655 * Handle SET_QUOTA-message.
5657 * @param cls closure (always NULL)
5658 * @param client identification of the client
5659 * @param message the actual message
5662 handle_set_quota (void *cls,
5663 struct GNUNET_SERVER_Client *client,
5664 const struct GNUNET_MessageHeader *message)
5666 const struct QuotaSetMessage *qsm =
5667 (const struct QuotaSetMessage *) message;
5668 struct NeighbourList *n;
5670 GNUNET_STATISTICS_update (stats,
5671 gettext_noop ("# SET QUOTA messages received"),
5674 n = find_neighbour (&qsm->peer);
5677 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5678 GNUNET_STATISTICS_update (stats,
5679 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
5685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5686 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
5688 (unsigned int) ntohl (qsm->quota.value__),
5689 (unsigned int) n->in_tracker.available_bytes_per_s__,
5690 GNUNET_i2s (&qsm->peer));
5692 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
5694 if (0 == ntohl (qsm->quota.value__))
5697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5698 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&n->id),
5701 GNUNET_STATISTICS_update (stats,
5702 gettext_noop ("# disconnects due to quota of 0"),
5705 disconnect_neighbour (n, GNUNET_NO);
5707 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5712 * Take the given address and append it to the set of results sent back to
5715 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
5716 * @param address the resolved name, NULL to indicate the last response
5719 transmit_address_to_client (void *cls, const char *address)
5721 struct GNUNET_SERVER_TransmitContext *tc = cls;
5724 if (NULL != address)
5726 slen = strlen (address) + 1;
5727 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
5728 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5732 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
5738 * Handle AddressLookup-message.
5740 * @param cls closure (always NULL)
5741 * @param client identification of the client
5742 * @param message the actual message
5745 handle_address_lookup (void *cls,
5746 struct GNUNET_SERVER_Client *client,
5747 const struct GNUNET_MessageHeader *message)
5749 const struct AddressLookupMessage *alum;
5750 struct TransportPlugin *lsPlugin;
5751 const char *nameTransport;
5752 const char *address;
5754 struct GNUNET_SERVER_TransmitContext *tc;
5755 struct GNUNET_TIME_Absolute timeout;
5756 struct GNUNET_TIME_Relative rtimeout;
5759 size = ntohs (message->size);
5760 if (size < sizeof (struct AddressLookupMessage))
5762 GNUNET_break_op (0);
5763 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5766 alum = (const struct AddressLookupMessage *) message;
5767 uint32_t addressLen = ntohl (alum->addrlen);
5768 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
5770 GNUNET_break_op (0);
5771 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5774 address = (const char *) &alum[1];
5775 nameTransport = (const char *) &address[addressLen];
5777 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
5779 GNUNET_break_op (0);
5780 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5783 timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
5784 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5785 numeric = ntohl (alum->numeric_only);
5786 lsPlugin = find_transport (nameTransport);
5787 if (NULL == lsPlugin)
5789 tc = GNUNET_SERVER_transmit_context_create (client);
5790 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5791 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5792 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
5795 GNUNET_SERVER_disable_receive_done_warning (client);
5796 tc = GNUNET_SERVER_transmit_context_create (client);
5797 lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
5799 address, addressLen,
5802 &transmit_address_to_client, tc);
5807 * Setup the environment for this plugin.
5810 create_environment (struct TransportPlugin *plug)
5812 plug->env.cfg = cfg;
5813 plug->env.my_identity = &my_identity;
5814 plug->env.our_hello = &our_hello;
5815 plug->env.cls = plug;
5816 plug->env.receive = &plugin_env_receive;
5817 plug->env.notify_address = &plugin_env_notify_address;
5818 plug->env.session_end = &plugin_env_session_end;
5819 plug->env.max_connections = max_connect_per_transport;
5820 plug->env.stats = stats;
5825 * Start the specified transport (load the plugin).
5828 start_transport (struct GNUNET_SERVER_Handle *server,
5831 struct TransportPlugin *plug;
5834 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5835 _("Loading `%s' transport plugin\n"), name);
5836 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
5837 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
5838 create_environment (plug);
5839 plug->short_name = GNUNET_strdup (name);
5840 plug->lib_name = libname;
5841 plug->next = plugins;
5843 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
5844 if (plug->api == NULL)
5846 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5847 _("Failed to load transport plugin for `%s'\n"), name);
5848 GNUNET_free (plug->short_name);
5849 plugins = plug->next;
5850 GNUNET_free (libname);
5857 * Called whenever a client is disconnected. Frees our
5858 * resources associated with that client.
5860 * @param cls closure
5861 * @param client identification of the client
5864 client_disconnect_notification (void *cls,
5865 struct GNUNET_SERVER_Client *client)
5867 struct TransportClient *pos;
5868 struct TransportClient *prev;
5869 struct ClientMessageQueueEntry *mqe;
5870 struct Blacklisters *bl;
5871 struct BlacklistCheck *bc;
5876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
5877 "Client disconnected, cleaning up.\n");
5879 /* clean up blacklister */
5883 if (bl->client == client)
5888 if (bc->bl_pos == bl)
5890 bc->bl_pos = bl->next;
5893 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
5896 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
5897 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
5903 GNUNET_CONTAINER_DLL_remove (bl_head,
5906 GNUNET_SERVER_client_drop (bl->client);
5912 /* clean up 'normal' clients */
5915 while ((pos != NULL) && (pos->client != client))
5922 while (NULL != (mqe = pos->message_queue_head))
5924 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
5925 pos->message_queue_tail,
5927 pos->message_count--;
5931 clients = pos->next;
5933 prev->next = pos->next;
5934 if (GNUNET_YES == pos->tcs_pending)
5939 if (pos->th != NULL)
5941 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
5944 GNUNET_break (0 == pos->message_count);
5950 * Function called when the service shuts down. Unloads our plugins
5951 * and cancels pending validations.
5953 * @param cls closure, unused
5954 * @param tc task context (unused)
5957 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5959 struct TransportPlugin *plug;
5960 struct TransportPlugin *tmp;
5961 struct OwnAddressList *al;
5962 struct CheckHelloValidatedContext *chvc;
5964 shutdown_in_progress = GNUNET_YES;
5965 while (neighbours != NULL)
5968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5969 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&neighbours->id),
5972 disconnect_neighbour (neighbours, GNUNET_NO);
5975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5976 "Transport service is unloading plugins...\n");
5979 while (plug != NULL)
5981 if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
5983 GNUNET_SCHEDULER_cancel (plug->address_update_task);
5984 plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
5986 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
5987 GNUNET_free (plug->lib_name);
5988 GNUNET_free (plug->short_name);
5989 while (NULL != (al = plug->addresses))
5991 plug->addresses = al->next;
5998 if (my_private_key != NULL)
5999 GNUNET_CRYPTO_rsa_key_free (my_private_key);
6000 GNUNET_free_non_null (our_hello);
6002 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
6005 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6006 validation_map = NULL;
6011 /* free 'chvc' data structure */
6012 while (NULL != (chvc = chvc_head))
6014 chvc_head = chvc->next;
6015 if (chvc->piter != NULL)
6017 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
6018 GNUNET_STATISTICS_update (stats,
6019 gettext_noop ("# outstanding peerinfo iterate requests"),
6026 GNUNET_assert (chvc->ve_count == 0);
6033 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6036 if (peerinfo != NULL)
6038 GNUNET_PEERINFO_disconnect (peerinfo);
6041 if (GNUNET_SCHEDULER_NO_TASK != hello_task)
6043 GNUNET_SCHEDULER_cancel (hello_task);
6044 hello_task = GNUNET_SCHEDULER_NO_TASK;
6046 /* Can we assume those are gone by now, or do we need to clean up
6048 GNUNET_break (bl_head == NULL);
6049 GNUNET_break (bc_head == NULL);
6054 schedule_ats (void *cls,
6055 const struct GNUNET_SCHEDULER_TaskContext *tc)
6057 struct ATS_Handle *ats = (struct ATS_Handle *) cls;
6061 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6062 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
6064 if (shutdown_in_progress == GNUNET_YES)
6067 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running scheduled calculation\n");
6069 ats_calculate_bandwidth_distribution (ats, stats, neighbours);
6070 ats->ats_task = GNUNET_SCHEDULER_add_delayed (ats->exec_interval,
6071 &schedule_ats, ats);
6074 struct ForeignAddressList * get_preferred_ats_address (
6075 struct NeighbourList *n)
6077 // TODO get ATS prefered address
6078 return find_ready_address(n);
6082 * Initiate transport service.
6084 * @param cls closure
6085 * @param server the initialized server
6086 * @param c configuration to use
6090 struct GNUNET_SERVER_Handle *server,
6091 const struct GNUNET_CONFIGURATION_Handle *c)
6093 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
6094 {&handle_start, NULL,
6095 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
6096 {&handle_hello, NULL,
6097 GNUNET_MESSAGE_TYPE_HELLO, 0},
6098 {&handle_send, NULL,
6099 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
6100 {&handle_request_connect, NULL,
6101 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT, sizeof(struct TransportRequestConnectMessage)},
6102 {&handle_set_quota, NULL,
6103 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
6104 {&handle_address_lookup, NULL,
6105 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
6107 {&handle_blacklist_init, NULL,
6108 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
6109 {&handle_blacklist_reply, NULL,
6110 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
6116 unsigned long long tneigh;
6119 shutdown_in_progress = GNUNET_NO;
6121 stats = GNUNET_STATISTICS_create ("transport", cfg);
6122 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
6123 /* parse configuration */
6125 GNUNET_CONFIGURATION_get_value_number (c,
6130 GNUNET_CONFIGURATION_get_value_filename (c,
6132 "HOSTKEY", &keyfile)))
6134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6136 ("Transport service is lacking key configuration settings. Exiting.\n"));
6137 GNUNET_SCHEDULER_shutdown ();
6140 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6143 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6144 validation_map = NULL;
6148 max_connect_per_transport = (uint32_t) tneigh;
6149 peerinfo = GNUNET_PEERINFO_connect (cfg);
6150 if (peerinfo == NULL)
6152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6153 _("Could not access PEERINFO service. Exiting.\n"));
6154 GNUNET_SCHEDULER_shutdown ();
6157 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6160 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6161 validation_map = NULL;
6162 GNUNET_free (keyfile);
6165 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6166 GNUNET_free (keyfile);
6167 if (my_private_key == NULL)
6169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6171 ("Transport service could not access hostkey. Exiting.\n"));
6172 GNUNET_SCHEDULER_shutdown ();
6175 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6178 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6179 validation_map = NULL;
6182 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6183 GNUNET_CRYPTO_hash (&my_public_key,
6184 sizeof (my_public_key), &my_identity.hashPubKey);
6185 /* setup notification */
6186 GNUNET_SERVER_disconnect_notify (server,
6187 &client_disconnect_notification, NULL);
6188 /* load plugins... */
6191 GNUNET_CONFIGURATION_get_value_string (c,
6192 "TRANSPORT", "PLUGINS", &plugs))
6194 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6195 _("Starting transport plugins `%s'\n"), plugs);
6196 pos = strtok (plugs, " ");
6199 start_transport (server, pos);
6201 pos = strtok (NULL, " ");
6203 GNUNET_free (plugs);
6205 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
6206 &shutdown_task, NULL);
6210 ats = ats_init (cfg);
6212 ats->ats_task = GNUNET_SCHEDULER_add_now (&schedule_ats, ats);
6216 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6217 _("Transport service ready.\n"));
6219 /* If we have a blacklist file, read from it */
6220 read_blacklist_file(cfg);
6221 /* process client requests */
6222 GNUNET_SERVER_add_handlers (server, handlers);
6227 * The main function for the transport service.
6229 * @param argc number of arguments from the command line
6230 * @param argv command line arguments
6231 * @return 0 ok, 1 on error
6234 main (int argc, char *const *argv)
6236 a2s (NULL, NULL, 0); /* make compiler happy */
6237 return (GNUNET_OK ==
6238 GNUNET_SERVICE_run (argc,
6241 GNUNET_SERVICE_OPTION_NONE,
6242 &run, NULL)) ? 0 : 1;
6245 /* end of gnunet-service-transport.c */