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);
2434 ats->stat.recreate_problem = GNUNET_YES;
2435 if (GNUNET_YES != pos->connected)
2437 /* nothing else to do, connection was never up... */
2443 if (nl->received_pong == GNUNET_NO)
2445 GNUNET_STATISTICS_update (stats,
2446 gettext_noop ("# disconnects due to NO pong"),
2449 disconnect_neighbour (nl, GNUNET_YES);
2450 return; /* nothing to do, never connected... */
2452 /* check if we have any validated addresses left */
2453 pos = rl->addresses;
2456 if (GNUNET_YES == pos->validated)
2458 GNUNET_STATISTICS_update (stats,
2459 gettext_noop ("# try_fast_reconnect thanks to validated_address"),
2462 try_fast_reconnect (p, nl);
2467 /* no valid addresses left, signal disconnect! */
2470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2471 "Disconnecting peer `%4s', %s\n",
2473 "plugin_env_session_end");
2475 /* FIXME: This doesn't mean there are no addresses left for this PEER,
2476 * it means there aren't any left for this PLUGIN/PEER combination! So
2477 * calling disconnect_neighbour here with GNUNET_NO forces disconnect
2478 * when it isn't necessary. Using GNUNET_YES at least checks to see
2479 * if there are any addresses that work first, so as not to overdo it.
2482 GNUNET_STATISTICS_update (stats,
2483 gettext_noop ("# disconnects due to plugin_env_session_end"),
2486 disconnect_neighbour (nl, GNUNET_YES);
2491 * Function that must be called by each plugin to notify the
2492 * transport service about the addresses under which the transport
2493 * provided by the plugin can be reached.
2495 * @param cls closure
2496 * @param add_remove GNUNET_YES to add, GNUNET_NO to remove the address
2497 * @param addr one of the addresses of the host, NULL for the last address
2498 * the specific address format depends on the transport
2499 * @param addrlen length of the address
2502 plugin_env_notify_address (void *cls,
2507 struct TransportPlugin *p = cls;
2508 struct OwnAddressList *al;
2509 struct OwnAddressList *prev;
2511 GNUNET_assert (p->api != NULL);
2513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2514 (add_remove == GNUNET_YES)
2515 ? "Adding `%s':%s to the set of our addresses\n"
2516 : "Removing `%s':%s from the set of our addresses\n",
2521 GNUNET_assert (addr != NULL);
2522 if (GNUNET_NO == add_remove)
2528 if ( (addrlen == al->addrlen) &&
2529 (0 == memcmp (addr, &al[1], addrlen)) )
2532 p->addresses = al->next;
2534 prev->next = al->next;
2545 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2546 al->next = p->addresses;
2548 al->addrlen = addrlen;
2549 memcpy (&al[1], addr, addrlen);
2555 * Notify all of our clients about a peer connecting.
2558 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2559 struct GNUNET_TIME_Relative latency,
2562 struct ConnectInfoMessage * cim;
2563 struct TransportClient *cpos;
2567 if (0 == memcmp (peer,
2569 sizeof (struct GNUNET_PeerIdentity)))
2575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2576 "Notifying clients about connection with `%s'\n",
2579 GNUNET_STATISTICS_update (stats,
2580 gettext_noop ("# peers connected"),
2585 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
2586 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2590 cim = GNUNET_malloc (size);
2591 cim->header.size = htons (size);
2592 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2593 cim->ats_count = htonl(2);
2594 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2595 (&(cim->ats))[0].value = htonl (distance);
2596 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
2597 (&(cim->ats))[1].value = htonl ((uint32_t) latency.rel_value);
2598 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2599 (&(cim->ats))[2].value = htonl (0);
2600 memcpy (&cim->id, peer, sizeof (struct GNUNET_PeerIdentity));
2602 /* notify ats about connecting peer */
2603 /* notify ats about connecting peer */
2604 if ((ats != NULL) && (shutdown_in_progress == GNUNET_NO))
2606 ats->stat.recreate_problem = GNUNET_YES;
2607 ats_calculate_bandwidth_distribution (ats, stats, neighbours);
2612 while (cpos != NULL)
2614 transmit_to_client (cpos, &(cim->header), GNUNET_NO);
2623 * Notify all of our clients about a peer disconnecting.
2626 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2628 struct DisconnectInfoMessage dim;
2629 struct TransportClient *cpos;
2631 if (0 == memcmp (peer,
2633 sizeof (struct GNUNET_PeerIdentity)))
2639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2640 "Notifying clients about lost connection to `%s'\n",
2643 GNUNET_STATISTICS_update (stats,
2644 gettext_noop ("# peers connected"),
2647 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2648 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2649 dim.reserved = htonl (0);
2650 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2652 /* notify ats about connecting peer */
2653 if ((ats != NULL) && (shutdown_in_progress == GNUNET_NO))
2655 ats->stat.recreate_problem = GNUNET_YES;
2656 ats_calculate_bandwidth_distribution (ats, stats, neighbours);
2660 while (cpos != NULL)
2662 transmit_to_client (cpos, &dim.header, GNUNET_NO);
2669 * Find a ForeignAddressList entry for the given neighbour
2670 * that matches the given address and transport.
2672 * @param neighbour which peer we care about
2673 * @param tname name of the transport plugin
2674 * @param session session to look for, NULL for 'any'; otherwise
2675 * can be used for the service to "learn" this session ID
2677 * @param addr binary address
2678 * @param addrlen length of addr
2679 * @return NULL if no such entry exists
2681 static struct ForeignAddressList *
2682 find_peer_address(struct NeighbourList *neighbour,
2684 struct Session *session,
2688 struct ReadyList *head;
2689 struct ForeignAddressList *pos;
2691 head = neighbour->plugins;
2692 while (head != NULL)
2694 if (0 == strcmp (tname, head->plugin->short_name))
2700 pos = head->addresses;
2701 while ( (pos != NULL) &&
2702 ( (pos->addrlen != addrlen) ||
2703 (memcmp(pos->addr, addr, addrlen) != 0) ) )
2705 if ( (session != NULL) &&
2706 (pos->session == session) )
2710 if ( (session != NULL) && (pos != NULL) )
2711 pos->session = session; /* learn it! */
2717 * Get the peer address struct for the given neighbour and
2718 * address. If it doesn't yet exist, create it.
2720 * @param neighbour which peer we care about
2721 * @param tname name of the transport plugin
2722 * @param session session of the plugin, or NULL for none
2723 * @param addr binary address
2724 * @param addrlen length of addr
2725 * @return NULL if we do not have a transport plugin for 'tname'
2727 static struct ForeignAddressList *
2728 add_peer_address (struct NeighbourList *neighbour,
2730 struct Session *session,
2734 struct ReadyList *head;
2735 struct ForeignAddressList *ret;
2738 ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2741 head = neighbour->plugins;
2743 while (head != NULL)
2745 if (0 == strcmp (tname, head->plugin->short_name))
2751 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2752 ret->session = session;
2753 if ((addrlen > 0) && (addr != NULL))
2755 ret->addr = (const char*) &ret[1];
2756 memcpy (&ret[1], addr, addrlen);
2763 ret->ressources = GNUNET_malloc(available_ressources * sizeof (struct ATS_ressource_entry));
2764 for (c=0; c<available_ressources; c++)
2766 struct ATS_ressource_entry *r = ret->ressources;
2768 r[c].atis_index = ressources[c].atis_index;
2769 if (0 == strcmp(neighbour->plugins->plugin->short_name,"unix"))
2771 r[c].c = ressources[c].c_unix;
2773 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"udp"))
2775 r[c].c = ressources[c].c_udp;
2777 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"tcp"))
2779 r[c].c = ressources[c].c_tcp;
2781 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"http"))
2783 r[c].c = ressources[c].c_http;
2785 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"https"))
2787 r[c].c = ressources[c].c_https;
2789 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"wlan"))
2791 r[c].c = ressources[c].c_wlan;
2795 r[c].c = ressources[c].c_default;
2796 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2797 "Assigning default cost to peer `%s' addr plugin `%s'! This should not happen!\n",
2798 GNUNET_i2s(&neighbour->peer),
2799 neighbour->plugins->plugin->short_name);
2803 ret->quality = GNUNET_malloc (available_quality_metrics * sizeof (struct ATS_quality_entry));
2804 ret->addrlen = addrlen;
2805 ret->expires = GNUNET_TIME_relative_to_absolute
2806 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2807 ret->latency = GNUNET_TIME_relative_get_forever();
2809 ret->timeout = GNUNET_TIME_relative_to_absolute
2810 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2811 ret->ready_list = head;
2812 ret->next = head->addresses;
2813 head->addresses = ret;
2819 * Closure for 'add_validated_address'.
2821 struct AddValidatedAddressContext
2824 * Entry that has been validated.
2826 const struct ValidationEntry *ve;
2829 * Flag set after we have added the address so
2830 * that we terminate the iteration next time.
2837 * Callback function used to fill a buffer of max bytes with a list of
2838 * addresses in the format used by HELLOs. Should use
2839 * "GNUNET_HELLO_add_address" as a helper function.
2841 * @param cls the 'struct AddValidatedAddressContext' with the validated address
2842 * @param max maximum number of bytes that can be written to buf
2843 * @param buf where to write the address information
2844 * @return number of bytes written, 0 to signal the
2845 * end of the iteration.
2848 add_validated_address (void *cls,
2849 size_t max, void *buf)
2851 struct AddValidatedAddressContext *avac = cls;
2852 const struct ValidationEntry *ve = avac->ve;
2854 if (GNUNET_YES == avac->done)
2856 avac->done = GNUNET_YES;
2857 return GNUNET_HELLO_add_address (ve->transport_name,
2858 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2868 * Closure for 'check_address_exists'.
2870 struct CheckAddressExistsClosure
2873 * Address to check for.
2878 * Name of the transport.
2885 struct Session *session;
2888 * Set to GNUNET_YES if the address exists.
2901 * Iterator over hash map entries. Checks if the given
2902 * validation entry is for the same address as what is given
2905 * @param cls the 'struct CheckAddressExistsClosure*'
2906 * @param key current key code (ignored)
2907 * @param value value in the hash map ('struct ValidationEntry')
2908 * @return GNUNET_YES if we should continue to
2909 * iterate (mismatch), GNUNET_NO if not (entry matched)
2912 check_address_exists (void *cls,
2913 const GNUNET_HashCode * key,
2916 struct CheckAddressExistsClosure *caec = cls;
2917 struct ValidationEntry *ve = value;
2919 if ( (0 == strcmp (caec->tname,
2920 ve->transport_name)) &&
2921 (caec->addrlen == ve->addrlen) &&
2922 (0 == memcmp (caec->addr,
2926 caec->exists = GNUNET_YES;
2929 if ( (ve->session != NULL) &&
2930 (caec->session == ve->session) )
2932 caec->exists = GNUNET_YES;
2940 neighbour_timeout_task (void *cls,
2941 const struct GNUNET_SCHEDULER_TaskContext *tc)
2943 struct NeighbourList *n = cls;
2946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2947 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
2949 GNUNET_STATISTICS_update (stats,
2950 gettext_noop ("# disconnects due to timeout"),
2953 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2954 disconnect_neighbour (n, GNUNET_NO);
2959 * Schedule the job that will cause us to send a PING to the
2960 * foreign address to evaluate its validity and latency.
2962 * @param fal address to PING
2965 schedule_next_ping (struct ForeignAddressList *fal);
2969 * Add the given address to the list of foreign addresses
2970 * available for the given peer (check for duplicates).
2972 * @param cls the respective 'struct NeighbourList' to update
2973 * @param tname name of the transport
2974 * @param expiration expiration time
2975 * @param addr the address
2976 * @param addrlen length of the address
2977 * @return GNUNET_OK (always)
2980 add_to_foreign_address_list (void *cls,
2982 struct GNUNET_TIME_Absolute expiration,
2986 struct NeighbourList *n = cls;
2987 struct ForeignAddressList *fal;
2990 GNUNET_STATISTICS_update (stats,
2991 gettext_noop ("# valid peer addresses returned by PEERINFO"),
2995 fal = find_peer_address (n, tname, NULL, addr, addrlen);
2998 #if DEBUG_TRANSPORT_HELLO
2999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3000 "Adding address `%s' (%s) for peer `%4s' due to PEERINFO data for %llums.\n",
3001 a2s (tname, addr, addrlen),
3003 GNUNET_i2s (&n->id),
3004 expiration.abs_value);
3006 fal = add_peer_address (n, tname, NULL, addr, addrlen);
3009 GNUNET_STATISTICS_update (stats,
3010 gettext_noop ("# previously validated addresses lacking transport"),
3016 fal->expires = GNUNET_TIME_absolute_max (expiration,
3018 schedule_next_ping (fal);
3024 fal->expires = GNUNET_TIME_absolute_max (expiration,
3030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3031 "Failed to add new address for `%4s'\n",
3032 GNUNET_i2s (&n->id));
3036 if (fal->validated == GNUNET_NO)
3038 fal->validated = GNUNET_YES;
3039 GNUNET_STATISTICS_update (stats,
3040 gettext_noop ("# peer addresses considered valid"),
3044 if (try == GNUNET_YES)
3047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3048 "Have new addresses, will try to trigger transmissions.\n");
3050 try_transmission_to_peer (n);
3057 * Add addresses in validated HELLO "h" to the set of addresses
3058 * we have for this peer.
3060 * @param cls closure ('struct NeighbourList*')
3061 * @param peer id of the peer, NULL for last call
3062 * @param h hello message for the peer (can be NULL)
3063 * @param err_msg NULL if successful, otherwise contains error message
3066 add_hello_for_peer (void *cls,
3067 const struct GNUNET_PeerIdentity *peer,
3068 const struct GNUNET_HELLO_Message *h,
3069 const char *err_msg)
3071 struct NeighbourList *n = cls;
3073 if (err_msg != NULL)
3076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3077 _("Error in communication with PEERINFO service: %s\n"),
3084 GNUNET_STATISTICS_update (stats,
3085 gettext_noop ("# outstanding peerinfo iterate requests"),
3092 return; /* no HELLO available */
3094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3095 "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
3099 if (GNUNET_YES != n->public_key_valid)
3101 GNUNET_HELLO_get_key (h, &n->publicKey);
3102 n->public_key_valid = GNUNET_YES;
3104 GNUNET_HELLO_iterate_addresses (h,
3106 &add_to_foreign_address_list,
3112 * Create a fresh entry in our neighbour list for the given peer.
3113 * Will try to transmit our current HELLO to the new neighbour.
3114 * Do not call this function directly, use 'setup_peer_check_blacklist.
3116 * @param peer the peer for which we create the entry
3117 * @param do_hello should we schedule transmitting a HELLO
3118 * @return the new neighbour list entry
3120 static struct NeighbourList *
3121 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
3124 struct NeighbourList *n;
3125 struct TransportPlugin *tp;
3126 struct ReadyList *rl;
3128 GNUNET_assert (0 != memcmp (peer,
3130 sizeof (struct GNUNET_PeerIdentity)));
3132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3133 "Setting up state for neighbour `%4s'\n",
3136 GNUNET_STATISTICS_update (stats,
3137 gettext_noop ("# active neighbours"),
3140 n = GNUNET_malloc (sizeof (struct NeighbourList));
3141 n->next = neighbours;
3145 GNUNET_TIME_relative_to_absolute
3146 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3147 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
3148 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3149 MAX_BANDWIDTH_CARRY_S);
3153 if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
3155 rl = GNUNET_malloc (sizeof (struct ReadyList));
3157 rl->next = n->plugins;
3160 rl->addresses = NULL;
3164 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
3166 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
3167 &neighbour_timeout_task, n);
3170 GNUNET_STATISTICS_update (stats,
3171 gettext_noop ("# peerinfo new neighbor iterate requests"),
3174 GNUNET_STATISTICS_update (stats,
3175 gettext_noop ("# outstanding peerinfo iterate requests"),
3178 n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
3179 GNUNET_TIME_UNIT_FOREVER_REL,
3180 &add_hello_for_peer, n);
3182 GNUNET_STATISTICS_update (stats,
3183 gettext_noop ("# HELLO's sent to new neighbors"),
3186 if (NULL != our_hello)
3187 transmit_to_peer (NULL, NULL, 0,
3188 HELLO_ADDRESS_EXPIRATION,
3189 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
3197 * Function called after we have checked if communicating
3198 * with a given peer is acceptable.
3200 * @param cls closure
3201 * @param n NULL if communication is not acceptable
3203 typedef void (*SetupContinuation)(void *cls,
3204 struct NeighbourList *n);
3208 * Information kept for each client registered to perform
3214 * This is a linked list.
3216 struct Blacklisters *next;
3219 * This is a linked list.
3221 struct Blacklisters *prev;
3224 * Client responsible for this entry.
3226 struct GNUNET_SERVER_Client *client;
3229 * Blacklist check that we're currently performing.
3231 struct BlacklistCheck *bc;
3237 * Head of DLL of blacklisting clients.
3239 static struct Blacklisters *bl_head;
3242 * Tail of DLL of blacklisting clients.
3244 static struct Blacklisters *bl_tail;
3248 * Context we use when performing a blacklist check.
3250 struct BlacklistCheck
3254 * This is a linked list.
3256 struct BlacklistCheck *next;
3259 * This is a linked list.
3261 struct BlacklistCheck *prev;
3264 * Peer being checked.
3266 struct GNUNET_PeerIdentity peer;
3269 * Option for setup neighbour afterwards.
3274 * Continuation to call with the result.
3276 SetupContinuation cont;
3284 * Current transmission request handle for this client, or NULL if no
3285 * request is pending.
3287 struct GNUNET_CONNECTION_TransmitHandle *th;
3290 * Our current position in the blacklisters list.
3292 struct Blacklisters *bl_pos;
3295 * Current task performing the check.
3297 GNUNET_SCHEDULER_TaskIdentifier task;
3302 * Head of DLL of active blacklisting queries.
3304 static struct BlacklistCheck *bc_head;
3307 * Tail of DLL of active blacklisting queries.
3309 static struct BlacklistCheck *bc_tail;
3313 * Perform next action in the blacklist check.
3315 * @param cls the 'struct BlacklistCheck*'
3319 do_blacklist_check (void *cls,
3320 const struct GNUNET_SCHEDULER_TaskContext *tc);
3323 * Transmit blacklist query to the client.
3325 * @param cls the 'struct BlacklistCheck'
3326 * @param size number of bytes allowed
3327 * @param buf where to copy the message
3328 * @return number of bytes copied to buf
3331 transmit_blacklist_message (void *cls,
3335 struct BlacklistCheck *bc = cls;
3336 struct Blacklisters *bl;
3337 struct BlacklistMessage bm;
3342 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
3343 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3346 "Failed to send blacklist test for peer `%s' to client\n",
3347 GNUNET_i2s (&bc->peer));
3351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3352 "Sending blacklist test for peer `%s' to client\n",
3353 GNUNET_i2s (&bc->peer));
3356 bm.header.size = htons (sizeof (struct BlacklistMessage));
3357 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
3358 bm.is_allowed = htonl (0);
3360 memcpy (buf, &bm, sizeof (bm));
3361 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
3367 * Perform next action in the blacklist check.
3369 * @param cls the 'struct BlacklistCheck*'
3373 do_blacklist_check (void *cls,
3374 const struct GNUNET_SCHEDULER_TaskContext *tc)
3376 struct BlacklistCheck *bc = cls;
3377 struct Blacklisters *bl;
3379 bc->task = GNUNET_SCHEDULER_NO_TASK;
3384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3385 "No blacklist clients active, will now setup neighbour record for peer `%s'\n",
3386 GNUNET_i2s (&bc->peer));
3388 bc->cont (bc->cont_cls,
3389 setup_new_neighbour (&bc->peer, bc->do_hello));
3396 bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
3397 sizeof (struct BlacklistMessage),
3398 GNUNET_TIME_UNIT_FOREVER_REL,
3399 &transmit_blacklist_message,
3406 * Obtain a 'struct NeighbourList' for the given peer. If such an entry
3407 * does not yet exist, check the blacklist. If the blacklist says creating
3408 * one is acceptable, create one and call the continuation; otherwise
3409 * call the continuation with NULL.
3411 * @param peer peer to setup or look up a struct NeighbourList for
3412 * @param do_hello should we also schedule sending our HELLO to the peer
3413 * if this is a new record
3414 * @param cont function to call with the 'struct NeigbhbourList*'
3415 * @param cont_cls closure for cont
3418 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
3420 SetupContinuation cont,
3423 struct NeighbourList *n;
3424 struct BlacklistCheck *bc;
3426 n = find_neighbour(peer);
3430 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3431 "Neighbour record exists for peer `%s'\n",
3438 if (bl_head == NULL)
3441 cont (cont_cls, setup_new_neighbour (peer, do_hello));
3443 setup_new_neighbour(peer, do_hello);
3446 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3447 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3449 bc->do_hello = do_hello;
3451 bc->cont_cls = cont_cls;
3452 bc->bl_pos = bl_head;
3453 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3459 * Function called with the result of querying a new blacklister about
3460 * it being allowed (or not) to continue to talk to an existing neighbour.
3462 * @param cls the original 'struct NeighbourList'
3463 * @param n NULL if we need to disconnect
3466 confirm_or_drop_neighbour (void *cls,
3467 struct NeighbourList *n)
3469 struct NeighbourList * orig = cls;
3474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3475 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&orig->id),
3476 "confirm_or_drop_neighboUr");
3478 GNUNET_STATISTICS_update (stats,
3479 gettext_noop ("# disconnects due to blacklist"),
3482 disconnect_neighbour (orig, GNUNET_NO);
3488 * Handle a request to start a blacklist.
3490 * @param cls closure (always NULL)
3491 * @param client identification of the client
3492 * @param message the actual message
3495 handle_blacklist_init (void *cls,
3496 struct GNUNET_SERVER_Client *client,
3497 const struct GNUNET_MessageHeader *message)
3499 struct Blacklisters *bl;
3500 struct BlacklistCheck *bc;
3501 struct NeighbourList *n;
3506 if (bl->client == client)
3509 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3514 bl = GNUNET_malloc (sizeof (struct Blacklisters));
3515 bl->client = client;
3516 GNUNET_SERVER_client_keep (client);
3517 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
3518 /* confirm that all existing connections are OK! */
3522 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3523 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3525 bc->do_hello = GNUNET_NO;
3526 bc->cont = &confirm_or_drop_neighbour;
3529 if (n == neighbours) /* all would wait for the same client, no need to
3530 create more than just the first task right now */
3531 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3539 * Handle a request to blacklist a peer.
3541 * @param cls closure (always NULL)
3542 * @param client identification of the client
3543 * @param message the actual message
3546 handle_blacklist_reply (void *cls,
3547 struct GNUNET_SERVER_Client *client,
3548 const struct GNUNET_MessageHeader *message)
3550 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
3551 struct Blacklisters *bl;
3552 struct BlacklistCheck *bc;
3555 while ( (bl != NULL) &&
3556 (bl->client != client) )
3561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3562 "Blacklist client disconnected\n");
3564 /* FIXME: other error handling here!? */
3565 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3570 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
3573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3574 "Blacklist check failed, peer not allowed\n");
3576 bc->cont (bc->cont_cls, NULL);
3577 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
3583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3584 "Blacklist check succeeded, continuing with checks\n");
3586 bc->bl_pos = bc->bl_pos->next;
3587 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3590 /* check if any other bc's are waiting for this blacklister */
3594 if ( (bc->bl_pos == bl) &&
3595 (GNUNET_SCHEDULER_NO_TASK == bc->task) )
3596 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3604 * Send periodic PING messages to a given foreign address.
3606 * @param cls our 'struct PeriodicValidationContext*'
3607 * @param tc task context
3610 send_periodic_ping (void *cls,
3611 const struct GNUNET_SCHEDULER_TaskContext *tc)
3613 struct ForeignAddressList *peer_address = cls;
3614 struct TransportPlugin *tp;
3615 struct ValidationEntry *va;
3616 struct NeighbourList *neighbour;
3617 struct TransportPingMessage ping;
3618 struct CheckAddressExistsClosure caec;
3620 uint16_t hello_size;
3624 peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3625 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3627 tp = peer_address->ready_list->plugin;
3628 neighbour = peer_address->ready_list->neighbour;
3629 if (GNUNET_YES != neighbour->public_key_valid)
3631 /* no public key yet, try again later */
3632 schedule_next_ping (peer_address);
3635 caec.addr = peer_address->addr;
3636 caec.addrlen = peer_address->addrlen;
3637 caec.tname = tp->short_name;
3638 caec.session = peer_address->session;
3639 caec.exists = GNUNET_NO;
3640 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3641 &check_address_exists,
3643 if (caec.exists == GNUNET_YES)
3645 /* During validation attempts we will likely trigger the other
3646 peer trying to validate our address which in turn will cause
3647 it to send us its HELLO, so we expect to hit this case rather
3648 frequently. Only print something if we are very verbose. */
3649 #if DEBUG_TRANSPORT > 1
3650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3651 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3652 (peer_address->addr != NULL)
3653 ? a2s (tp->short_name,
3655 peer_address->addrlen)
3658 GNUNET_i2s (&neighbour->id));
3660 schedule_next_ping (peer_address);
3663 va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3664 va->transport_name = GNUNET_strdup (tp->short_name);
3665 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
3667 va->send_time = GNUNET_TIME_absolute_get();
3668 va->session = peer_address->session;
3669 if (peer_address->addr != NULL)
3671 va->addr = (const void*) &va[1];
3672 memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3673 va->addrlen = peer_address->addrlen;
3675 memcpy(&va->publicKey,
3676 &neighbour->publicKey,
3677 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3679 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
3680 &timeout_hello_validation,
3682 GNUNET_CONTAINER_multihashmap_put (validation_map,
3683 &neighbour->id.hashPubKey,
3685 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3687 if (peer_address->validated != GNUNET_YES)
3688 hello_size = GNUNET_HELLO_size(our_hello);
3692 tsize = sizeof(struct TransportPingMessage) + hello_size;
3694 if (peer_address->addr != NULL)
3696 slen = strlen (tp->short_name) + 1;
3697 tsize += slen + peer_address->addrlen;
3701 slen = 0; /* make gcc happy */
3703 message_buf = GNUNET_malloc(tsize);
3704 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3705 ping.challenge = htonl(va->challenge);
3706 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3707 if (peer_address->validated != GNUNET_YES)
3709 memcpy(message_buf, our_hello, hello_size);
3712 if (peer_address->addr != NULL)
3714 ping.header.size = htons(sizeof(struct TransportPingMessage) +
3715 peer_address->addrlen +
3717 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3720 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3722 peer_address->addrlen);
3726 ping.header.size = htons(sizeof(struct TransportPingMessage));
3729 memcpy(&message_buf[hello_size],
3731 sizeof(struct TransportPingMessage));
3733 #if DEBUG_TRANSPORT_REVALIDATION
3734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3735 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s'\n",
3736 (peer_address->addr != NULL)
3737 ? a2s (peer_address->plugin->short_name,
3739 peer_address->addrlen)
3742 GNUNET_i2s (&neighbour->id),
3743 "HELLO", hello_size,
3746 if (peer_address->validated != GNUNET_YES)
3747 GNUNET_STATISTICS_update (stats,
3748 gettext_noop ("# PING with HELLO messages sent"),
3752 GNUNET_STATISTICS_update (stats,
3753 gettext_noop ("# PING without HELLO messages sent"),
3756 GNUNET_STATISTICS_update (stats,
3757 gettext_noop ("# PING messages sent for re-validation"),
3760 transmit_to_peer (NULL, peer_address,
3761 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3762 HELLO_VERIFICATION_TIMEOUT,
3764 GNUNET_YES, neighbour);
3765 GNUNET_free(message_buf);
3766 schedule_next_ping (peer_address);
3771 * Schedule the job that will cause us to send a PING to the
3772 * foreign address to evaluate its validity and latency.
3774 * @param fal address to PING
3777 schedule_next_ping (struct ForeignAddressList *fal)
3779 struct GNUNET_TIME_Relative delay;
3781 if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3783 delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3784 delay.rel_value /= 2; /* do before expiration */
3785 delay = GNUNET_TIME_relative_min (delay,
3786 LATENCY_EVALUATION_MAX_DELAY);
3787 if (GNUNET_YES != fal->estimated)
3789 delay = GNUNET_TIME_UNIT_ZERO;
3790 fal->estimated = GNUNET_YES;
3792 if (GNUNET_YES == fal->connected)
3794 delay = GNUNET_TIME_relative_min (delay,
3795 CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3797 /* FIXME: also adjust delay based on how close the last
3798 observed latency is to the latency of the best alternative */
3799 /* bound how fast we can go */
3800 delay = GNUNET_TIME_relative_max (delay,
3801 GNUNET_TIME_UNIT_SECONDS);
3802 /* randomize a bit (to avoid doing all at the same time) */
3803 delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3804 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(delay,
3805 &send_periodic_ping,
3813 * Function that will be called if we receive some payload
3814 * from another peer.
3816 * @param message the payload
3817 * @param n peer who claimed to be the sender
3820 handle_payload_message (const struct GNUNET_MessageHeader *message,
3821 struct NeighbourList *n)
3823 struct InboundMessage *im;
3824 struct TransportClient *cpos;
3827 msize = ntohs (message->size);
3828 if (n->received_pong == GNUNET_NO)
3831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3832 "Received message of type %u and size %u from `%4s', but no pong yet!\n",
3833 ntohs (message->type),
3834 ntohs (message->size),
3835 GNUNET_i2s (&n->id));
3837 GNUNET_free_non_null (n->pre_connect_message_buffer);
3838 n->pre_connect_message_buffer = GNUNET_malloc (msize);
3839 memcpy (n->pre_connect_message_buffer, message, msize);
3844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3845 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
3846 ntohs (message->type),
3847 ntohs (message->size),
3848 GNUNET_i2s (&n->id));
3850 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3853 n->quota_violation_count++;
3855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3856 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
3857 n->in_tracker.available_bytes_per_s__,
3858 n->quota_violation_count);
3860 /* Discount 32k per violation */
3861 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3866 if (n->quota_violation_count > 0)
3868 /* try to add 32k back */
3869 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3871 n->quota_violation_count--;
3874 GNUNET_STATISTICS_update (stats,
3875 gettext_noop ("# payload received from other peers"),
3878 /* transmit message to all clients */
3879 uint32_t ats_count = 2;
3880 size_t size = sizeof (struct InboundMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) + msize;
3881 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
3884 im = GNUNET_malloc (size);
3885 im->header.size = htons (size);
3886 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3888 im->ats_count = htonl(ats_count);
3889 /* Setting ATS data */
3890 (&(im->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
3891 (&(im->ats))[0].value = htonl (n->distance);
3892 (&(im->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
3893 (&(im->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
3894 (&(im->ats))[ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
3895 (&(im->ats))[ats_count].value = htonl (0);
3897 memcpy (&((&(im->ats))[ats_count+1]), message, msize);
3899 while (cpos != NULL)
3901 transmit_to_client (cpos, &im->header, GNUNET_YES);
3909 * Iterator over hash map entries. Checks if the given validation
3910 * entry is for the same challenge as what is given in the PONG.
3912 * @param cls the 'struct TransportPongMessage*'
3913 * @param key peer identity
3914 * @param value value in the hash map ('struct ValidationEntry')
3915 * @return GNUNET_YES if we should continue to
3916 * iterate (mismatch), GNUNET_NO if not (entry matched)
3919 check_pending_validation (void *cls,
3920 const GNUNET_HashCode * key,
3923 const struct TransportPongMessage *pong = cls;
3924 struct ValidationEntry *ve = value;
3925 struct AddValidatedAddressContext avac;
3926 unsigned int challenge = ntohl(pong->challenge);
3927 struct GNUNET_HELLO_Message *hello;
3928 struct GNUNET_PeerIdentity target;
3929 struct NeighbourList *n;
3930 struct ForeignAddressList *fal;
3931 struct OwnAddressList *oal;
3932 struct TransportPlugin *tp;
3933 struct GNUNET_MessageHeader *prem;
3939 ps = ntohs (pong->header.size);
3940 if (ps < sizeof (struct TransportPongMessage))
3942 GNUNET_break_op (0);
3945 addr = (const char*) &pong[1];
3946 slen = strlen (ve->transport_name) + 1;
3947 if ( (ps - sizeof (struct TransportPongMessage) < slen) ||
3948 (ve->challenge != challenge) ||
3949 (addr[slen-1] != '\0') ||
3950 (0 != strcmp (addr, ve->transport_name)) ||
3951 (ntohl (pong->purpose.size)
3952 != sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3954 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
3955 sizeof (struct GNUNET_PeerIdentity) + ps - sizeof (struct TransportPongMessage)) )
3960 alen = ps - sizeof (struct TransportPongMessage) - slen;
3961 switch (ntohl (pong->purpose.purpose))
3963 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN:
3964 if ( (ve->addrlen + slen != ntohl (pong->addrlen)) ||
3965 (0 != memcmp (&addr[slen],
3969 return GNUNET_YES; /* different entry, keep trying! */
3971 if (0 != memcmp (&pong->pid,
3973 sizeof (struct GNUNET_PeerIdentity)))
3975 GNUNET_break_op (0);
3979 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
3984 GNUNET_break_op (0);
3989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3990 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
3992 a2s (ve->transport_name,
3993 (const struct sockaddr *) ve->addr,
3995 ve->transport_name);
3998 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING:
3999 if (0 != memcmp (&pong->pid,
4001 sizeof (struct GNUNET_PeerIdentity)))
4005 GNUNET_asprintf(&peer, "%s",GNUNET_i2s (&pong->pid));
4007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4008 "Received PONG for different identity: I am `%s', PONG identity: `%s'\n",
4009 GNUNET_i2s (&my_identity),
4015 if (ve->addrlen != 0)
4017 /* must have been for a different validation entry */
4020 tp = find_transport (ve->transport_name);
4026 oal = tp->addresses;
4029 if ( (oal->addrlen == alen) &&
4030 (0 == memcmp (&oal[1],
4038 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4039 _("Not accepting PONG from `%s' with address `%s' since I cannot confirm using this address.\n"),
4040 GNUNET_i2s (&pong->pid),
4041 a2s (ve->transport_name,
4044 /* FIXME: since the sender of the PONG currently uses the
4045 wrong address (see FIMXE there!), we cannot run a
4046 proper check here... */
4052 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING,
4057 GNUNET_break_op (0);
4062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4063 "Confirmed that peer `%4s' is talking to us using address `%s' (%s) for us.\n",
4065 a2s (ve->transport_name,
4068 ve->transport_name);
4072 GNUNET_break_op (0);
4075 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
4077 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4078 _("Received expired signature. Check system time.\n"));
4081 GNUNET_STATISTICS_update (stats,
4082 gettext_noop ("# address validation successes"),
4085 /* create the updated HELLO */
4086 GNUNET_CRYPTO_hash (&ve->publicKey,
4087 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4088 &target.hashPubKey);
4089 if (ve->addr != NULL)
4091 avac.done = GNUNET_NO;
4093 hello = GNUNET_HELLO_create (&ve->publicKey,
4094 &add_validated_address,
4096 GNUNET_PEERINFO_add_peer (peerinfo,
4098 GNUNET_free (hello);
4100 n = find_neighbour (&target);
4103 n->publicKey = ve->publicKey;
4104 n->public_key_valid = GNUNET_YES;
4105 fal = add_peer_address (n,
4110 GNUNET_assert (fal != NULL);
4111 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
4112 fal->validated = GNUNET_YES;
4113 mark_address_connected (fal);
4114 GNUNET_STATISTICS_update (stats,
4115 gettext_noop ("# peer addresses considered valid"),
4118 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
4119 update_addr_value (fal, GNUNET_TIME_absolute_get_duration (ve->send_time).rel_value, GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
4121 schedule_next_ping (fal);
4122 if (n->latency.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
4123 n->latency = fal->latency;
4125 n->latency.rel_value = (fal->latency.rel_value + n->latency.rel_value) / 2;
4127 n->distance = fal->distance;
4128 if (GNUNET_NO == n->received_pong)
4130 n->received_pong = GNUNET_YES;
4131 notify_clients_connect (&target, n->latency, n->distance);
4132 if (NULL != (prem = n->pre_connect_message_buffer))
4134 n->pre_connect_message_buffer = NULL;
4135 handle_payload_message (prem, n);
4139 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4141 GNUNET_SCHEDULER_cancel (n->retry_task);
4142 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4143 try_transmission_to_peer (n);
4147 /* clean up validation entry */
4148 GNUNET_assert (GNUNET_YES ==
4149 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4152 abort_validation (NULL, NULL, ve);
4158 * Function that will be called if we receive a validation
4159 * of an address challenge that we transmitted to another
4160 * peer. Note that the validation should only be considered
4161 * acceptable if the challenge matches AND if the sender
4162 * address is at least a plausible address for this peer
4163 * (otherwise we may be seeing a MiM attack).
4165 * @param cls closure
4166 * @param message the pong message
4167 * @param peer who responded to our challenge
4168 * @param sender_address string describing our sender address (as observed
4169 * by the other peer in binary format)
4170 * @param sender_address_len number of bytes in 'sender_address'
4173 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
4174 const struct GNUNET_PeerIdentity *peer,
4175 const char *sender_address,
4176 size_t sender_address_len)
4178 if (0 == memcmp (peer,
4180 sizeof (struct GNUNET_PeerIdentity)))
4182 /* PONG send to self, ignore */
4183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4184 "Receiving `%s' message from myself\n",
4188 #if DEBUG_TRANSPORT > 1
4189 /* we get tons of these that just get discarded, only log
4190 if we are quite verbose */
4191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4192 "Receiving `%s' message from `%4s'.\n", "PONG",
4195 GNUNET_STATISTICS_update (stats,
4196 gettext_noop ("# PONG messages received"),
4199 if (GNUNET_SYSERR !=
4200 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
4202 &check_pending_validation,
4205 /* This is *expected* to happen a lot since we send
4206 PONGs to *all* known addresses of the sender of
4207 the PING, so most likely we get multiple PONGs
4208 per PING, and all but the first PONG will end up
4209 here. So really we should not print anything here
4210 unless we want to be very, very verbose... */
4211 #if DEBUG_TRANSPORT > 2
4212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4213 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
4225 * Try to validate a neighbour's address by sending him our HELLO and a PING.
4227 * @param cls the 'struct ValidationEntry*'
4228 * @param neighbour neighbour to validate, NULL if validation failed
4231 transmit_hello_and_ping (void *cls,
4232 struct NeighbourList *neighbour)
4234 struct ValidationEntry *va = cls;
4235 struct ForeignAddressList *peer_address;
4236 struct TransportPingMessage ping;
4237 uint16_t hello_size;
4240 struct GNUNET_PeerIdentity id;
4243 GNUNET_CRYPTO_hash (&va->publicKey,
4244 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4246 if (neighbour == NULL)
4248 /* FIXME: stats... */
4249 GNUNET_break (GNUNET_OK ==
4250 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4253 abort_validation (NULL, NULL, va);
4256 neighbour->publicKey = va->publicKey;
4257 neighbour->public_key_valid = GNUNET_YES;
4258 peer_address = add_peer_address (neighbour,
4259 va->transport_name, NULL,
4260 (const void*) &va[1],
4262 if (peer_address == NULL)
4264 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4265 "Failed to add peer `%4s' for plugin `%s'\n",
4266 GNUNET_i2s (&neighbour->id),
4267 va->transport_name);
4268 GNUNET_break (GNUNET_OK ==
4269 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4272 abort_validation (NULL, NULL, va);
4275 if (NULL == our_hello)
4276 refresh_hello_task (NULL, NULL);
4277 hello_size = GNUNET_HELLO_size(our_hello);
4278 slen = strlen(va->transport_name) + 1;
4279 tsize = sizeof(struct TransportPingMessage) + hello_size + va->addrlen + slen;
4280 message_buf = GNUNET_malloc(tsize);
4281 ping.challenge = htonl(va->challenge);
4282 ping.header.size = htons(sizeof(struct TransportPingMessage) + slen + va->addrlen);
4283 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
4284 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
4285 memcpy(message_buf, our_hello, hello_size);
4286 memcpy(&message_buf[hello_size],
4288 sizeof(struct TransportPingMessage));
4289 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
4292 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
4296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4297 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
4300 : a2s (va->transport_name,
4301 (const void*) &va[1], va->addrlen),
4303 GNUNET_i2s (&neighbour->id),
4304 "HELLO", hello_size,
4305 "PING", sizeof (struct TransportPingMessage) + va->addrlen + slen);
4308 GNUNET_STATISTICS_update (stats,
4309 gettext_noop ("# PING messages sent for initial validation"),
4312 transmit_to_peer (NULL, peer_address,
4313 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
4314 HELLO_VERIFICATION_TIMEOUT,
4316 GNUNET_YES, neighbour);
4317 GNUNET_free(message_buf);
4322 * Check if the given address is already being validated; if not,
4323 * append the given address to the list of entries that are being be
4324 * validated and initiate validation.
4326 * @param cls closure ('struct CheckHelloValidatedContext *')
4327 * @param tname name of the transport
4328 * @param expiration expiration time
4329 * @param addr the address
4330 * @param addrlen length of the address
4331 * @return GNUNET_OK (always)
4334 run_validation (void *cls,
4336 struct GNUNET_TIME_Absolute expiration,
4340 struct CheckHelloValidatedContext *chvc = cls;
4341 struct GNUNET_PeerIdentity id;
4342 struct TransportPlugin *tp;
4343 struct ValidationEntry *va;
4344 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4345 struct CheckAddressExistsClosure caec;
4346 struct OwnAddressList *oal;
4348 GNUNET_assert (addr != NULL);
4350 GNUNET_STATISTICS_update (stats,
4351 gettext_noop ("# peer addresses scheduled for validation"),
4354 tp = find_transport (tname);
4357 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
4358 GNUNET_ERROR_TYPE_BULK,
4360 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
4362 GNUNET_STATISTICS_update (stats,
4363 gettext_noop ("# peer addresses not validated (plugin not available)"),
4368 /* check if this is one of our own addresses */
4369 oal = tp->addresses;
4372 if ( (oal->addrlen == addrlen) &&
4373 (0 == memcmp (&oal[1],
4377 /* not plausible, this address is equivalent to our own address! */
4378 GNUNET_STATISTICS_update (stats,
4379 gettext_noop ("# peer addresses not validated (loopback)"),
4386 GNUNET_HELLO_get_key (chvc->hello, &pk);
4387 GNUNET_CRYPTO_hash (&pk,
4389 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4392 if (is_blacklisted(&id, tp))
4395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4396 "Attempted to validate blacklisted peer `%s' using `%s'!\n",
4404 caec.addrlen = addrlen;
4405 caec.session = NULL;
4407 caec.exists = GNUNET_NO;
4408 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
4409 &check_address_exists,
4411 if (caec.exists == GNUNET_YES)
4413 /* During validation attempts we will likely trigger the other
4414 peer trying to validate our address which in turn will cause
4415 it to send us its HELLO, so we expect to hit this case rather
4416 frequently. Only print something if we are very verbose. */
4417 #if DEBUG_TRANSPORT > 1
4418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4419 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
4420 a2s (tname, addr, addrlen),
4424 GNUNET_STATISTICS_update (stats,
4425 gettext_noop ("# peer addresses not validated (in progress)"),
4430 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
4433 va->transport_name = GNUNET_strdup (tname);
4434 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
4436 va->send_time = GNUNET_TIME_absolute_get();
4437 va->addr = (const void*) &va[1];
4438 memcpy (&va[1], addr, addrlen);
4439 va->addrlen = addrlen;
4440 GNUNET_HELLO_get_key (chvc->hello,
4442 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
4443 &timeout_hello_validation,
4445 GNUNET_CONTAINER_multihashmap_put (validation_map,
4448 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4449 setup_peer_check_blacklist (&id, GNUNET_NO,
4450 &transmit_hello_and_ping,
4457 * Check if addresses in validated hello "h" overlap with
4458 * those in "chvc->hello" and validate the rest.
4460 * @param cls closure
4461 * @param peer id of the peer, NULL for last call
4462 * @param h hello message for the peer (can be NULL)
4463 * @param err_msg NULL if successful, otherwise contains error message
4466 check_hello_validated (void *cls,
4467 const struct GNUNET_PeerIdentity *peer,
4468 const struct GNUNET_HELLO_Message *h,
4469 const char *err_msg)
4471 struct CheckHelloValidatedContext *chvc = cls;
4472 struct GNUNET_HELLO_Message *plain_hello;
4473 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4474 struct GNUNET_PeerIdentity target;
4475 struct NeighbourList *n;
4477 if (err_msg != NULL)
4480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4481 _("Error in communication with PEERINFO service: %s\n"),
4489 GNUNET_STATISTICS_update (stats,
4490 gettext_noop ("# outstanding peerinfo iterate requests"),
4494 if (GNUNET_NO == chvc->hello_known)
4496 /* notify PEERINFO about the peer now, so that we at least
4497 have the public key if some other component needs it */
4498 GNUNET_HELLO_get_key (chvc->hello, &pk);
4499 GNUNET_CRYPTO_hash (&pk,
4500 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4501 &target.hashPubKey);
4502 plain_hello = GNUNET_HELLO_create (&pk,
4505 GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
4506 GNUNET_free (plain_hello);
4507 #if DEBUG_TRANSPORT_HELLO
4508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4509 "PEERINFO had no `%s' message for peer `%4s', full validation needed.\n",
4511 GNUNET_i2s (&target));
4513 GNUNET_STATISTICS_update (stats,
4514 gettext_noop ("# new HELLOs requiring full validation"),
4517 GNUNET_HELLO_iterate_addresses (chvc->hello,
4524 GNUNET_STATISTICS_update (stats,
4525 gettext_noop ("# duplicate HELLO (peer known)"),
4530 if (chvc->ve_count == 0)
4532 GNUNET_CONTAINER_DLL_remove (chvc_head,
4541 #if DEBUG_TRANSPORT_HELLO
4542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4543 "PEERINFO had `%s' message for peer `%4s', validating only new addresses.\n",
4547 chvc->hello_known = GNUNET_YES;
4548 n = find_neighbour (peer);
4551 #if DEBUG_TRANSPORT_HELLO
4552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4553 "Calling hello_iterate_addresses for %s!\n",
4556 GNUNET_HELLO_iterate_addresses (h,
4558 &add_to_foreign_address_list,
4560 try_transmission_to_peer (n);
4564 #if DEBUG_TRANSPORT_HELLO
4565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4566 "No existing neighbor record for %s!\n",
4569 GNUNET_STATISTICS_update (stats,
4570 gettext_noop ("# no existing neighbour record (validating HELLO)"),
4574 GNUNET_STATISTICS_update (stats,
4575 gettext_noop ("# HELLO validations (update case)"),
4578 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
4580 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
4587 * Process HELLO-message.
4589 * @param plugin transport involved, may be NULL
4590 * @param message the actual message
4591 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
4594 process_hello (struct TransportPlugin *plugin,
4595 const struct GNUNET_MessageHeader *message)
4598 struct GNUNET_PeerIdentity target;
4599 const struct GNUNET_HELLO_Message *hello;
4600 struct CheckHelloValidatedContext *chvc;
4601 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
4602 struct NeighbourList *n;
4603 #if DEBUG_TRANSPORT_HELLO > 2
4607 hsize = ntohs (message->size);
4608 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
4609 (hsize < sizeof (struct GNUNET_MessageHeader)))
4612 return GNUNET_SYSERR;
4614 GNUNET_STATISTICS_update (stats,
4615 gettext_noop ("# HELLOs received for validation"),
4619 hello = (const struct GNUNET_HELLO_Message *) message;
4620 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
4622 #if DEBUG_TRANSPORT_HELLO
4623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4624 "Unable to get public key from `%s' for `%4s'!\n",
4626 GNUNET_i2s (&target));
4628 GNUNET_break_op (0);
4629 return GNUNET_SYSERR;
4631 GNUNET_CRYPTO_hash (&publicKey,
4632 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4633 &target.hashPubKey);
4635 #if DEBUG_TRANSPORT_HELLO
4636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4637 "Received `%s' message for `%4s'\n",
4639 GNUNET_i2s (&target));
4641 if (0 == memcmp (&my_identity,
4643 sizeof (struct GNUNET_PeerIdentity)))
4645 GNUNET_STATISTICS_update (stats,
4646 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
4651 n = find_neighbour (&target);
4653 (! n->public_key_valid) )
4655 GNUNET_HELLO_get_key (hello, &n->publicKey);
4656 n->public_key_valid = GNUNET_YES;
4659 /* check if load is too high before doing expensive stuff */
4660 if (GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
4662 GNUNET_STATISTICS_update (stats,
4663 gettext_noop ("# HELLOs ignored due to high load"),
4666 #if DEBUG_TRANSPORT_HELLO
4667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4668 "Ignoring `%s' for `%4s', load too high.\n",
4670 GNUNET_i2s (&target));
4677 while (NULL != chvc)
4679 if (GNUNET_HELLO_equals (hello,
4681 GNUNET_TIME_absolute_get ()).abs_value > 0)
4683 #if DEBUG_TRANSPORT_HELLO > 2
4684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4685 "Received duplicate `%s' message for `%4s'; ignored\n",
4687 GNUNET_i2s (&target));
4689 return GNUNET_OK; /* validation already pending */
4691 if (GNUNET_HELLO_size (hello) == GNUNET_HELLO_size (chvc->hello))
4692 GNUNET_break (0 != memcmp (hello, chvc->hello,
4693 GNUNET_HELLO_size(hello)));
4698 struct NeighbourList *temp_neighbor = find_neighbour(&target);
4699 if ((NULL != temp_neighbor))
4701 fprintf(stderr, "Already know peer, ignoring hello\n");
4706 #if DEBUG_TRANSPORT_HELLO > 2
4709 my_id = GNUNET_strdup(GNUNET_i2s(plugin->env.my_identity));
4711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4712 "%s: Starting validation of `%s' message for `%4s' via '%s' of size %u\n",
4715 GNUNET_i2s (&target),
4717 GNUNET_HELLO_size(hello));
4722 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
4724 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
4725 memcpy (&chvc[1], hello, hsize);
4726 GNUNET_CONTAINER_DLL_insert (chvc_head,
4729 /* finally, check if HELLO was previously validated
4730 (continuation will then schedule actual validation) */
4731 GNUNET_STATISTICS_update (stats,
4732 gettext_noop ("# peerinfo process hello iterate requests"),
4735 GNUNET_STATISTICS_update (stats,
4736 gettext_noop ("# outstanding peerinfo iterate requests"),
4739 chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
4741 HELLO_VERIFICATION_TIMEOUT,
4742 &check_hello_validated, chvc);
4748 * The peer specified by the given neighbour has timed-out or a plugin
4749 * has disconnected. We may either need to do nothing (other plugins
4750 * still up), or trigger a full disconnect and clean up. This
4751 * function updates our state and does the necessary notifications.
4752 * Also notifies our clients that the neighbour is now officially
4755 * @param n the neighbour list entry for the peer
4756 * @param check GNUNET_YES to check if ALL addresses for this peer
4757 * are gone, GNUNET_NO to force a disconnect of the peer
4758 * regardless of whether other addresses exist.
4761 disconnect_neighbour (struct NeighbourList *n, int check)
4763 struct ReadyList *rpos;
4764 struct NeighbourList *npos;
4765 struct NeighbourList *nprev;
4766 struct MessageQueue *mq;
4767 struct ForeignAddressList *peer_addresses;
4768 struct ForeignAddressList *peer_pos;
4770 if (GNUNET_YES == check)
4773 while (NULL != rpos)
4775 peer_addresses = rpos->addresses;
4776 while (peer_addresses != NULL)
4778 // Do not disconnect if: an address is connected or an inbound address exists
4779 if ((GNUNET_YES == peer_addresses->connected) || (peer_addresses->addrlen == 0))
4782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4783 "NOT Disconnecting from `%4s', still have live address `%s'!\n",
4784 GNUNET_i2s (&n->id),
4785 a2s (peer_addresses->ready_list->plugin->short_name,
4786 peer_addresses->addr,
4787 peer_addresses->addrlen));
4789 return; /* still connected */
4791 peer_addresses = peer_addresses->next;
4797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4798 "Disconnecting from `%4s'\n",
4799 GNUNET_i2s (&n->id));
4802 /* remove n from neighbours list */
4805 while ((npos != NULL) && (npos != n))
4810 GNUNET_assert (npos != NULL);
4812 neighbours = n->next;
4814 nprev->next = n->next;
4816 /* notify all clients about disconnect */
4817 if (GNUNET_YES == n->received_pong)
4818 notify_clients_disconnect (&n->id);
4821 ats->stat.recreate_problem = GNUNET_YES;
4823 /* clean up all plugins, cancel connections and pending transmissions */
4824 while (NULL != (rpos = n->plugins))
4826 n->plugins = rpos->next;
4827 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
4828 while (rpos->addresses != NULL)
4830 peer_pos = rpos->addresses;
4831 rpos->addresses = peer_pos->next;
4832 if (peer_pos->connected == GNUNET_YES)
4833 GNUNET_STATISTICS_update (stats,
4834 gettext_noop ("# connected addresses"),
4837 if (GNUNET_YES == peer_pos->validated)
4838 GNUNET_STATISTICS_update (stats,
4839 gettext_noop ("# peer addresses considered valid"),
4842 if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
4844 GNUNET_SCHEDULER_cancel (peer_pos->revalidate_task);
4845 peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
4847 GNUNET_free(peer_pos->ressources);
4848 peer_pos->ressources = NULL;
4849 GNUNET_free(peer_pos->quality);
4850 peer_pos->ressources = NULL;
4851 GNUNET_free(peer_pos);
4856 /* free all messages on the queue */
4857 while (NULL != (mq = n->messages_head))
4859 GNUNET_STATISTICS_update (stats,
4860 gettext_noop ("# bytes in message queue for other peers"),
4861 - (int64_t) mq->message_buf_size,
4863 GNUNET_STATISTICS_update (stats,
4864 gettext_noop ("# bytes discarded due to disconnect"),
4865 mq->message_buf_size,
4867 GNUNET_CONTAINER_DLL_remove (n->messages_head,
4870 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4872 sizeof(struct GNUNET_PeerIdentity)));
4876 while (NULL != (mq = n->cont_head))
4879 GNUNET_CONTAINER_DLL_remove (n->cont_head,
4882 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4884 sizeof(struct GNUNET_PeerIdentity)));
4888 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
4890 GNUNET_SCHEDULER_cancel (n->timeout_task);
4891 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4893 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4895 GNUNET_SCHEDULER_cancel (n->retry_task);
4896 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4898 if (n->piter != NULL)
4900 GNUNET_PEERINFO_iterate_cancel (n->piter);
4901 GNUNET_STATISTICS_update (stats,
4902 gettext_noop ("# outstanding peerinfo iterate requests"),
4907 /* finally, free n itself */
4908 GNUNET_STATISTICS_update (stats,
4909 gettext_noop ("# active neighbours"),
4912 GNUNET_free_non_null (n->pre_connect_message_buffer);
4918 * We have received a PING message from someone. Need to send a PONG message
4919 * in response to the peer by any means necessary.
4922 handle_ping (void *cls, const struct GNUNET_MessageHeader *message,
4923 const struct GNUNET_PeerIdentity *peer,
4924 struct Session *session,
4925 const char *sender_address,
4926 uint16_t sender_address_len)
4928 struct TransportPlugin *plugin = cls;
4929 struct SessionHeader *session_header = (struct SessionHeader*) session;
4930 struct TransportPingMessage *ping;
4931 struct TransportPongMessage *pong;
4932 struct NeighbourList *n;
4933 struct ReadyList *rl;
4934 struct ForeignAddressList *fal;
4935 struct OwnAddressList *oal;
4941 if (ntohs (message->size) < sizeof (struct TransportPingMessage))
4943 GNUNET_break_op (0);
4944 return GNUNET_SYSERR;
4947 ping = (struct TransportPingMessage *) message;
4948 if (0 != memcmp (&ping->target,
4949 plugin->env.my_identity,
4950 sizeof (struct GNUNET_PeerIdentity)))
4953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4954 _("Received `%s' message from `%s' destined for `%s' which is not me!\n"),
4956 (sender_address != NULL)
4957 ? a2s (plugin->short_name,
4958 (const struct sockaddr *)sender_address,
4961 GNUNET_i2s (&ping->target));
4963 return GNUNET_SYSERR;
4966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4967 "Processing `%s' from `%s'\n",
4969 (sender_address != NULL)
4970 ? a2s (plugin->short_name,
4971 (const struct sockaddr *)sender_address,
4975 GNUNET_STATISTICS_update (stats,
4976 gettext_noop ("# PING messages received"),
4979 addr = (const char*) &ping[1];
4980 alen = ntohs (message->size) - sizeof (struct TransportPingMessage);
4981 slen = strlen (plugin->short_name) + 1;
4984 /* peer wants to confirm that we have an outbound connection to him */
4985 if (session == NULL)
4987 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4988 _("Refusing to create PONG since I do not have a session with `%s'.\n"),
4990 return GNUNET_SYSERR;
4992 /* FIXME-urg: the use of 'sender_address' in the code below is doubly-wrong:
4993 1) it is NULL when we need to have a real value
4994 2) it is documented to be the address of the sender (source-IP), where
4995 what we actually want is our LISTEN IP (what we 'bound' to); which we don't even
4998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4999 "Creating PONG indicating that we received a connection at our address `%s' from `%s'.\n",
5000 a2s (plugin->short_name,
5002 sender_address_len),
5005 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len + slen);
5006 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len + slen);
5007 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
5008 pong->purpose.size =
5009 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
5011 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
5012 sizeof (struct GNUNET_PeerIdentity) + sender_address_len + slen);
5013 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING);
5014 pong->challenge = ping->challenge;
5015 pong->addrlen = htonl(sender_address_len + slen);
5018 sizeof(struct GNUNET_PeerIdentity));
5022 if ((sender_address!=NULL) && (sender_address_len > 0))
5023 memcpy (&((char*)&pong[1])[slen],
5025 sender_address_len);
5026 if (GNUNET_TIME_absolute_get_remaining (session_header->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
5028 /* create / update cached sig */
5030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5031 "Creating PONG signature to indicate active connection.\n");
5033 session_header->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
5034 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
5035 GNUNET_assert (GNUNET_OK ==
5036 GNUNET_CRYPTO_rsa_sign (my_private_key,
5038 &session_header->pong_signature));
5042 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
5044 memcpy (&pong->signature,
5045 &session_header->pong_signature,
5046 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5052 /* peer wants to confirm that this is one of our addresses */
5056 plugin->api->check_address (plugin->api->cls,
5060 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5061 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
5062 a2s (plugin->short_name,
5067 oal = plugin->addresses;
5070 if ( (oal->addrlen == alen) &&
5077 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
5078 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
5079 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
5080 pong->purpose.size =
5081 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
5083 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
5084 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
5085 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
5086 pong->challenge = ping->challenge;
5087 pong->addrlen = htonl(alen + slen);
5090 sizeof(struct GNUNET_PeerIdentity));
5091 memcpy (&pong[1], plugin->short_name, slen);
5092 memcpy (&((char*)&pong[1])[slen], addr, alen);
5093 if ( (oal != NULL) &&
5094 (GNUNET_TIME_absolute_get_remaining (oal->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4) )
5096 /* create / update cached sig */
5098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5099 "Creating PONG signature to indicate ownership.\n");
5101 oal->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
5102 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5103 GNUNET_assert (GNUNET_OK ==
5104 GNUNET_CRYPTO_rsa_sign (my_private_key,
5106 &oal->pong_signature));
5107 memcpy (&pong->signature,
5108 &oal->pong_signature,
5109 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5111 else if (oal == NULL)
5113 /* not using cache (typically DV-only) */
5114 pong->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
5115 GNUNET_assert (GNUNET_OK ==
5116 GNUNET_CRYPTO_rsa_sign (my_private_key,
5122 /* can used cached version */
5123 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5124 memcpy (&pong->signature,
5125 &oal->pong_signature,
5126 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5129 n = find_neighbour(peer);
5130 GNUNET_assert (n != NULL);
5131 did_pong = GNUNET_NO;
5132 /* first try reliable response transmission */
5136 fal = rl->addresses;
5139 if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
5142 ntohs (pong->header.size),
5143 TRANSPORT_PONG_PRIORITY,
5144 HELLO_VERIFICATION_TIMEOUT,
5151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5152 "Transmitted PONG to `%s' via reliable mechanism\n",
5155 GNUNET_STATISTICS_update (stats,
5156 gettext_noop ("# PONGs unicast via reliable transport"),
5162 did_pong = GNUNET_YES;
5167 /* no reliable method found, do multicast */
5168 GNUNET_STATISTICS_update (stats,
5169 gettext_noop ("# PONGs multicast to all available addresses"),
5175 fal = rl->addresses;
5178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5179 "Transmitting PONG to `%s' via unreliable mechanism `%s':%s\n",
5181 a2s (rl->plugin->short_name,
5184 rl->plugin->short_name);
5185 transmit_to_peer(NULL, fal,
5186 TRANSPORT_PONG_PRIORITY,
5187 HELLO_VERIFICATION_TIMEOUT,
5189 ntohs(pong->header.size),
5192 did_pong = GNUNET_YES;
5198 if (GNUNET_YES != did_pong)
5199 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5200 _("Could not send PONG to `%s': no address available\n"),
5207 * Function called by the plugin for each received message. Update
5208 * data volumes, possibly notify plugins about reducing the rate at
5209 * which they read from the socket and generally forward to our
5212 * @param cls the "struct TransportPlugin *" we gave to the plugin
5213 * @param peer (claimed) identity of the other peer
5214 * @param message the message, NULL if we only care about
5215 * learning about the delay until we should receive again
5216 * @param ats_data information for automatic transport selection
5217 * @param ats_count number of elements in ats not including 0-terminator
5218 * @param session identifier used for this session (can be NULL)
5219 * @param sender_address binary address of the sender (if observed)
5220 * @param sender_address_len number of bytes in sender_address
5221 * @return how long in ms the plugin should wait until receiving more data
5222 * (plugins that do not support this, can ignore the return value)
5224 static struct GNUNET_TIME_Relative
5225 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
5226 const struct GNUNET_MessageHeader *message,
5227 const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
5229 struct Session *session,
5230 const char *sender_address,
5231 uint16_t sender_address_len)
5233 struct TransportPlugin *plugin = cls;
5234 struct ReadyList *service_context;
5235 struct ForeignAddressList *peer_address;
5237 struct NeighbourList *n;
5238 struct GNUNET_TIME_Relative ret;
5242 if (0 == memcmp (peer,
5244 sizeof (struct GNUNET_PeerIdentity)))
5246 /* refuse to receive from myself */
5248 return GNUNET_TIME_UNIT_FOREVER_REL;
5250 if (is_blacklisted (peer, plugin))
5251 return GNUNET_TIME_UNIT_FOREVER_REL;
5252 n = find_neighbour (peer);
5254 n = setup_new_neighbour (peer, GNUNET_YES);
5255 service_context = n->plugins;
5256 while ((service_context != NULL) && (plugin != service_context->plugin))
5257 service_context = service_context->next;
5258 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
5259 peer_address = NULL;
5262 for (c=0; c<ats_count; c++)
5263 if (ntohl(ats_data[c].type) == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
5264 distance = ntohl(ats_data[c].value);
5267 if (message != NULL)
5269 if ( (session != NULL) ||
5270 (sender_address != NULL) )
5271 peer_address = add_peer_address (n,
5275 sender_address_len);
5276 if (peer_address != NULL)
5278 update_addr_ats(peer_address, ats_data, ats_count);
5279 update_addr_value(peer_address, distance, GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5281 peer_address->distance = distance;
5282 if (GNUNET_YES == peer_address->validated)
5283 mark_address_connected (peer_address);
5284 peer_address->timeout
5285 = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5286 schedule_next_ping (peer_address);
5288 /* update traffic received amount ... */
5289 msize = ntohs (message->size);
5291 GNUNET_STATISTICS_update (stats,
5292 gettext_noop ("# bytes received from other peers"),
5295 n->distance = distance;
5297 GNUNET_TIME_relative_to_absolute
5298 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5299 GNUNET_SCHEDULER_cancel (n->timeout_task);
5301 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
5302 &neighbour_timeout_task, n);
5303 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
5305 /* dropping message due to frequent inbound volume violations! */
5306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
5307 GNUNET_ERROR_TYPE_BULK,
5309 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
5310 n->in_tracker.available_bytes_per_s__,
5311 n->quota_violation_count);
5312 GNUNET_STATISTICS_update (stats,
5313 gettext_noop ("# bandwidth quota violations by other peers"),
5316 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
5318 if ((ntohs(message->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ATS) &&
5319 (ntohs(message->size) == (sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t))))
5321 uint32_t value = ntohl(*((uint32_t *) &message[1]));
5322 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GNUNET_MESSAGE_TYPE_TRANSPORT_ATS: %i \n", value);
5323 /* Force ressource and quality update */
5324 if ((value == 4) && (ats != NULL))
5326 ats->stat.modified_resources = GNUNET_YES;
5327 ats->stat.modified_quality = GNUNET_YES;
5329 /* Force cost update */
5330 if ((value == 3) && (ats != NULL))
5331 ats->stat.modified_resources = GNUNET_YES;
5332 /* Force quality update */
5333 if ((value == 2) && (ats != NULL))
5334 ats->stat.modified_quality = GNUNET_YES;
5335 /* Force full rebuild */
5336 if ((value == 1) && (ats != NULL))
5337 ats->stat.recreate_problem = GNUNET_YES;
5341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5342 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
5343 ntohs (message->type),
5344 ntohs (message->size),
5347 switch (ntohs (message->type))
5349 case GNUNET_MESSAGE_TYPE_HELLO:
5350 GNUNET_STATISTICS_update (stats,
5351 gettext_noop ("# HELLO messages received from other peers"),
5354 process_hello (plugin, message);
5356 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
5357 handle_ping (plugin, message, peer, session, sender_address, sender_address_len);
5358 if (! n->received_pong)
5359 transmit_plain_ping (n);
5361 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
5362 handle_pong (plugin, message, peer, sender_address, sender_address_len);
5364 case GNUNET_MESSAGE_TYPE_TRANSPORT_ATS:
5367 handle_payload_message (message, n);
5371 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
5372 if (ret.rel_value > 0)
5375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5376 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
5377 (unsigned long long) n->in_tracker.consumption_since_last_update__,
5378 (unsigned int) n->in_tracker.available_bytes_per_s__,
5379 (unsigned long long) ret.rel_value);
5381 GNUNET_STATISTICS_update (stats,
5382 gettext_noop ("# ms throttling suggested"),
5383 (int64_t) ret.rel_value,
5390 * Handle START-message. This is the first message sent to us
5391 * by any client which causes us to add it to our list.
5393 * @param cls closure (always NULL)
5394 * @param client identification of the client
5395 * @param message the actual message
5398 handle_start (void *cls,
5399 struct GNUNET_SERVER_Client *client,
5400 const struct GNUNET_MessageHeader *message)
5402 const struct StartMessage *start;
5403 struct TransportClient *c;
5404 struct ConnectInfoMessage * cim;
5405 struct NeighbourList *n;
5409 start = (const struct StartMessage*) message;
5411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5412 "Received `%s' request from client\n", "START");
5417 if (c->client == client)
5419 /* client already on our list! */
5421 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5426 if ( (GNUNET_NO != ntohl (start->do_check)) &&
5427 (0 != memcmp (&start->self,
5429 sizeof (struct GNUNET_PeerIdentity))) )
5431 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5432 _("Rejecting control connection from peer `%s', which is not me!\n"),
5433 GNUNET_i2s (&start->self));
5434 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5437 c = GNUNET_malloc (sizeof (struct TransportClient));
5441 if (our_hello != NULL)
5444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5445 "Sending our own `%s' to new client\n", "HELLO");
5447 transmit_to_client (c,
5448 (const struct GNUNET_MessageHeader *) our_hello,
5450 /* tell new client about all existing connections */
5452 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
5453 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
5457 cim = GNUNET_malloc (size);
5458 cim->header.size = htons (size);
5459 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
5460 cim->ats_count = htonl(ats_count);
5461 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
5462 (&(cim->ats))[2].value = htonl (0);
5466 if (GNUNET_YES == n->received_pong)
5468 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5469 (&(cim->ats))[0].value = htonl (n->distance);
5470 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
5471 (&(cim->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
5473 transmit_to_client (c, &cim->header, GNUNET_NO);
5481 #if DEBUG_TRANSPORT_HELLO
5482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5483 "No HELLO created yet, will transmit HELLO to client later!\n");
5487 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5492 * Handle HELLO-message.
5494 * @param cls closure (always NULL)
5495 * @param client identification of the client
5496 * @param message the actual message
5499 handle_hello (void *cls,
5500 struct GNUNET_SERVER_Client *client,
5501 const struct GNUNET_MessageHeader *message)
5505 GNUNET_STATISTICS_update (stats,
5506 gettext_noop ("# HELLOs received from clients"),
5509 ret = process_hello (NULL, message);
5510 GNUNET_SERVER_receive_done (client, ret);
5515 * Closure for 'transmit_client_message'; followed by
5516 * 'msize' bytes of the actual message.
5518 struct TransmitClientMessageContext
5521 * Client on whom's behalf we are sending.
5523 struct GNUNET_SERVER_Client *client;
5526 * Timeout for the transmission.
5528 struct GNUNET_TIME_Absolute timeout;
5536 * Size of the message in bytes.
5543 * Schedule transmission of a message we got from a client to a peer.
5545 * @param cls the 'struct TransmitClientMessageContext*'
5546 * @param n destination, or NULL on error (in that case, drop the message)
5549 transmit_client_message (void *cls,
5550 struct NeighbourList *n)
5552 struct TransmitClientMessageContext *tcmc = cls;
5553 struct TransportClient *tc;
5556 while ((tc != NULL) && (tc->client != tcmc->client))
5561 transmit_to_peer (tc, NULL, tcmc->priority,
5562 GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
5564 tcmc->msize, GNUNET_NO, n);
5566 GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
5567 GNUNET_SERVER_client_drop (tcmc->client);
5573 * Handle SEND-message.
5575 * @param cls closure (always NULL)
5576 * @param client identification of the client
5577 * @param message the actual message
5580 handle_send (void *cls,
5581 struct GNUNET_SERVER_Client *client,
5582 const struct GNUNET_MessageHeader *message)
5584 const struct OutboundMessage *obm;
5585 const struct GNUNET_MessageHeader *obmm;
5586 struct TransmitClientMessageContext *tcmc;
5590 size = ntohs (message->size);
5592 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
5595 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5598 GNUNET_STATISTICS_update (stats,
5599 gettext_noop ("# payload received for other peers"),
5602 obm = (const struct OutboundMessage *) message;
5603 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
5604 msize = size - sizeof (struct OutboundMessage);
5606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5607 "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
5608 "SEND", GNUNET_i2s (&obm->peer),
5612 tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
5613 tcmc->client = client;
5614 tcmc->priority = ntohl (obm->priority);
5615 tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
5616 tcmc->msize = msize;
5617 /* FIXME: this memcpy can be up to 7% of our total runtime */
5618 memcpy (&tcmc[1], obmm, msize);
5619 GNUNET_SERVER_client_keep (client);
5620 setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
5621 &transmit_client_message,
5627 * Handle request connect message
5629 * @param cls closure (always NULL)
5630 * @param client identification of the client
5631 * @param message the actual message
5634 handle_request_connect (void *cls,
5635 struct GNUNET_SERVER_Client *client,
5636 const struct GNUNET_MessageHeader *message)
5638 const struct TransportRequestConnectMessage *trcm =
5639 (const struct TransportRequestConnectMessage *) message;
5641 GNUNET_STATISTICS_update (stats,
5642 gettext_noop ("# REQUEST CONNECT messages received"),
5646 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
5647 "Received a request connect message for peer `%s'\n",
5648 GNUNET_i2s(&trcm->peer));
5650 setup_peer_check_blacklist (&trcm->peer, GNUNET_YES,
5652 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5657 * Handle SET_QUOTA-message.
5659 * @param cls closure (always NULL)
5660 * @param client identification of the client
5661 * @param message the actual message
5664 handle_set_quota (void *cls,
5665 struct GNUNET_SERVER_Client *client,
5666 const struct GNUNET_MessageHeader *message)
5668 const struct QuotaSetMessage *qsm =
5669 (const struct QuotaSetMessage *) message;
5670 struct NeighbourList *n;
5672 GNUNET_STATISTICS_update (stats,
5673 gettext_noop ("# SET QUOTA messages received"),
5676 n = find_neighbour (&qsm->peer);
5679 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5680 GNUNET_STATISTICS_update (stats,
5681 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
5687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5688 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
5690 (unsigned int) ntohl (qsm->quota.value__),
5691 (unsigned int) n->in_tracker.available_bytes_per_s__,
5692 GNUNET_i2s (&qsm->peer));
5694 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
5696 if (0 == ntohl (qsm->quota.value__))
5699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5700 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&n->id),
5703 GNUNET_STATISTICS_update (stats,
5704 gettext_noop ("# disconnects due to quota of 0"),
5707 disconnect_neighbour (n, GNUNET_NO);
5709 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5714 * Take the given address and append it to the set of results sent back to
5717 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
5718 * @param address the resolved name, NULL to indicate the last response
5721 transmit_address_to_client (void *cls, const char *address)
5723 struct GNUNET_SERVER_TransmitContext *tc = cls;
5726 if (NULL != address)
5728 slen = strlen (address) + 1;
5729 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
5730 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5734 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
5740 * Handle AddressLookup-message.
5742 * @param cls closure (always NULL)
5743 * @param client identification of the client
5744 * @param message the actual message
5747 handle_address_lookup (void *cls,
5748 struct GNUNET_SERVER_Client *client,
5749 const struct GNUNET_MessageHeader *message)
5751 const struct AddressLookupMessage *alum;
5752 struct TransportPlugin *lsPlugin;
5753 const char *nameTransport;
5754 const char *address;
5756 struct GNUNET_SERVER_TransmitContext *tc;
5757 struct GNUNET_TIME_Absolute timeout;
5758 struct GNUNET_TIME_Relative rtimeout;
5761 size = ntohs (message->size);
5762 if (size < sizeof (struct AddressLookupMessage))
5764 GNUNET_break_op (0);
5765 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5768 alum = (const struct AddressLookupMessage *) message;
5769 uint32_t addressLen = ntohl (alum->addrlen);
5770 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
5772 GNUNET_break_op (0);
5773 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5776 address = (const char *) &alum[1];
5777 nameTransport = (const char *) &address[addressLen];
5779 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
5781 GNUNET_break_op (0);
5782 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5785 timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
5786 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5787 numeric = ntohl (alum->numeric_only);
5788 lsPlugin = find_transport (nameTransport);
5789 if (NULL == lsPlugin)
5791 tc = GNUNET_SERVER_transmit_context_create (client);
5792 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5793 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5794 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
5797 GNUNET_SERVER_disable_receive_done_warning (client);
5798 tc = GNUNET_SERVER_transmit_context_create (client);
5799 lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
5801 address, addressLen,
5804 &transmit_address_to_client, tc);
5809 * Setup the environment for this plugin.
5812 create_environment (struct TransportPlugin *plug)
5814 plug->env.cfg = cfg;
5815 plug->env.my_identity = &my_identity;
5816 plug->env.our_hello = &our_hello;
5817 plug->env.cls = plug;
5818 plug->env.receive = &plugin_env_receive;
5819 plug->env.notify_address = &plugin_env_notify_address;
5820 plug->env.session_end = &plugin_env_session_end;
5821 plug->env.max_connections = max_connect_per_transport;
5822 plug->env.stats = stats;
5827 * Start the specified transport (load the plugin).
5830 start_transport (struct GNUNET_SERVER_Handle *server,
5833 struct TransportPlugin *plug;
5836 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5837 _("Loading `%s' transport plugin\n"), name);
5838 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
5839 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
5840 create_environment (plug);
5841 plug->short_name = GNUNET_strdup (name);
5842 plug->lib_name = libname;
5843 plug->next = plugins;
5845 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
5846 if (plug->api == NULL)
5848 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5849 _("Failed to load transport plugin for `%s'\n"), name);
5850 GNUNET_free (plug->short_name);
5851 plugins = plug->next;
5852 GNUNET_free (libname);
5859 * Called whenever a client is disconnected. Frees our
5860 * resources associated with that client.
5862 * @param cls closure
5863 * @param client identification of the client
5866 client_disconnect_notification (void *cls,
5867 struct GNUNET_SERVER_Client *client)
5869 struct TransportClient *pos;
5870 struct TransportClient *prev;
5871 struct ClientMessageQueueEntry *mqe;
5872 struct Blacklisters *bl;
5873 struct BlacklistCheck *bc;
5878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
5879 "Client disconnected, cleaning up.\n");
5881 /* clean up blacklister */
5885 if (bl->client == client)
5890 if (bc->bl_pos == bl)
5892 bc->bl_pos = bl->next;
5895 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
5898 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
5899 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
5905 GNUNET_CONTAINER_DLL_remove (bl_head,
5908 GNUNET_SERVER_client_drop (bl->client);
5914 /* clean up 'normal' clients */
5917 while ((pos != NULL) && (pos->client != client))
5924 while (NULL != (mqe = pos->message_queue_head))
5926 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
5927 pos->message_queue_tail,
5929 pos->message_count--;
5933 clients = pos->next;
5935 prev->next = pos->next;
5936 if (GNUNET_YES == pos->tcs_pending)
5941 if (pos->th != NULL)
5943 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
5946 GNUNET_break (0 == pos->message_count);
5952 * Function called when the service shuts down. Unloads our plugins
5953 * and cancels pending validations.
5955 * @param cls closure, unused
5956 * @param tc task context (unused)
5959 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5961 struct TransportPlugin *plug;
5962 struct TransportPlugin *tmp;
5963 struct OwnAddressList *al;
5964 struct CheckHelloValidatedContext *chvc;
5966 shutdown_in_progress = GNUNET_YES;
5967 while (neighbours != NULL)
5970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5971 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&neighbours->id),
5974 disconnect_neighbour (neighbours, GNUNET_NO);
5977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5978 "Transport service is unloading plugins...\n");
5981 while (plug != NULL)
5983 if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
5985 GNUNET_SCHEDULER_cancel (plug->address_update_task);
5986 plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
5988 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
5989 GNUNET_free (plug->lib_name);
5990 GNUNET_free (plug->short_name);
5991 while (NULL != (al = plug->addresses))
5993 plug->addresses = al->next;
6000 if (my_private_key != NULL)
6001 GNUNET_CRYPTO_rsa_key_free (my_private_key);
6002 GNUNET_free_non_null (our_hello);
6004 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
6007 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6008 validation_map = NULL;
6013 /* free 'chvc' data structure */
6014 while (NULL != (chvc = chvc_head))
6016 chvc_head = chvc->next;
6017 if (chvc->piter != NULL)
6019 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
6020 GNUNET_STATISTICS_update (stats,
6021 gettext_noop ("# outstanding peerinfo iterate requests"),
6028 GNUNET_assert (chvc->ve_count == 0);
6035 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6038 if (peerinfo != NULL)
6040 GNUNET_PEERINFO_disconnect (peerinfo);
6043 if (GNUNET_SCHEDULER_NO_TASK != hello_task)
6045 GNUNET_SCHEDULER_cancel (hello_task);
6046 hello_task = GNUNET_SCHEDULER_NO_TASK;
6048 /* Can we assume those are gone by now, or do we need to clean up
6050 GNUNET_break (bl_head == NULL);
6051 GNUNET_break (bc_head == NULL);
6056 schedule_ats (void *cls,
6057 const struct GNUNET_SCHEDULER_TaskContext *tc)
6059 struct ATS_Handle *ats = (struct ATS_Handle *) cls;
6063 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6064 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
6066 if (shutdown_in_progress == GNUNET_YES)
6069 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running scheduled calculation\n");
6071 ats_calculate_bandwidth_distribution (ats, stats, neighbours);
6072 ats->ats_task = GNUNET_SCHEDULER_add_delayed (ats->exec_interval,
6073 &schedule_ats, ats);
6076 struct ForeignAddressList * get_preferred_ats_address (
6077 struct NeighbourList *n)
6079 // TODO get ATS prefered address
6080 return find_ready_address(n);
6084 * Initiate transport service.
6086 * @param cls closure
6087 * @param server the initialized server
6088 * @param c configuration to use
6092 struct GNUNET_SERVER_Handle *server,
6093 const struct GNUNET_CONFIGURATION_Handle *c)
6095 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
6096 {&handle_start, NULL,
6097 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
6098 {&handle_hello, NULL,
6099 GNUNET_MESSAGE_TYPE_HELLO, 0},
6100 {&handle_send, NULL,
6101 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
6102 {&handle_request_connect, NULL,
6103 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT, sizeof(struct TransportRequestConnectMessage)},
6104 {&handle_set_quota, NULL,
6105 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
6106 {&handle_address_lookup, NULL,
6107 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
6109 {&handle_blacklist_init, NULL,
6110 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
6111 {&handle_blacklist_reply, NULL,
6112 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
6118 unsigned long long tneigh;
6121 shutdown_in_progress = GNUNET_NO;
6123 stats = GNUNET_STATISTICS_create ("transport", cfg);
6124 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
6125 /* parse configuration */
6127 GNUNET_CONFIGURATION_get_value_number (c,
6132 GNUNET_CONFIGURATION_get_value_filename (c,
6134 "HOSTKEY", &keyfile)))
6136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6138 ("Transport service is lacking key configuration settings. Exiting.\n"));
6139 GNUNET_SCHEDULER_shutdown ();
6142 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6145 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6146 validation_map = NULL;
6150 max_connect_per_transport = (uint32_t) tneigh;
6151 peerinfo = GNUNET_PEERINFO_connect (cfg);
6152 if (peerinfo == NULL)
6154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6155 _("Could not access PEERINFO service. Exiting.\n"));
6156 GNUNET_SCHEDULER_shutdown ();
6159 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6162 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6163 validation_map = NULL;
6164 GNUNET_free (keyfile);
6167 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6168 GNUNET_free (keyfile);
6169 if (my_private_key == NULL)
6171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6173 ("Transport service could not access hostkey. Exiting.\n"));
6174 GNUNET_SCHEDULER_shutdown ();
6177 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6180 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6181 validation_map = NULL;
6184 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6185 GNUNET_CRYPTO_hash (&my_public_key,
6186 sizeof (my_public_key), &my_identity.hashPubKey);
6187 /* setup notification */
6188 GNUNET_SERVER_disconnect_notify (server,
6189 &client_disconnect_notification, NULL);
6190 /* load plugins... */
6193 GNUNET_CONFIGURATION_get_value_string (c,
6194 "TRANSPORT", "PLUGINS", &plugs))
6196 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6197 _("Starting transport plugins `%s'\n"), plugs);
6198 pos = strtok (plugs, " ");
6201 start_transport (server, pos);
6203 pos = strtok (NULL, " ");
6205 GNUNET_free (plugs);
6207 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
6208 &shutdown_task, NULL);
6212 ats = ats_init (cfg);
6214 ats->ats_task = GNUNET_SCHEDULER_add_now (&schedule_ats, ats);
6218 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6219 _("Transport service ready.\n"));
6221 /* If we have a blacklist file, read from it */
6222 read_blacklist_file(cfg);
6223 /* process client requests */
6224 GNUNET_SERVER_add_handlers (server, handlers);
6229 * The main function for the transport service.
6231 * @param argc number of arguments from the command line
6232 * @param argv command line arguments
6233 * @return 0 ok, 1 on error
6236 main (int argc, char *const *argv)
6238 a2s (NULL, NULL, 0); /* make compiler happy */
6239 return (GNUNET_OK ==
6240 GNUNET_SERVICE_run (argc,
6243 GNUNET_SERVICE_OPTION_NONE,
6244 &run, NULL)) ? 0 : 1;
6247 /* end of gnunet-service-transport.c */