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 * Identifier of ats scheduler task.
925 static GNUNET_SCHEDULER_TaskIdentifier ats_task;
928 * Is transport service shutting down ?
930 static int shutdown_in_progress;
933 * Handle for ats information
935 static struct ATS_Handle *ats;
938 * The peer specified by the given neighbour has timed-out or a plugin
939 * has disconnected. We may either need to do nothing (other plugins
940 * still up), or trigger a full disconnect and clean up. This
941 * function updates our state and do the necessary notifications.
942 * Also notifies our clients that the neighbour is now officially
945 * @param n the neighbour list entry for the peer
946 * @param check should we just check if all plugins
947 * disconnected or must we ask all plugins to
950 static void disconnect_neighbour (struct NeighbourList *n, int check);
953 * Check the ready list for the given neighbour and if a plugin is
954 * ready for transmission (and if we have a message), do so!
956 * @param nexi target peer for which to transmit
958 static void try_transmission_to_peer (struct NeighbourList *n);
960 struct ForeignAddressList * get_preferred_ats_address (
961 struct NeighbourList *n);
964 * Find an entry in the neighbour list for a particular peer.
966 * @return NULL if not found.
968 static struct NeighbourList *
969 find_neighbour (const struct GNUNET_PeerIdentity *key)
971 struct NeighbourList *head = neighbours;
973 while ((head != NULL) &&
974 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
979 static int update_addr_value (struct ForeignAddressList *fal, uint32_t value , int ats_index)
983 for (c=0; c<available_quality_metrics; c++)
985 if (ats_index == qm[c].atis_index)
987 fal->quality[c].values[0] = fal->quality[c].values[1];
988 fal->quality[c].values[1] = fal->quality[c].values[2];
989 fal->quality[c].values[2] = value;
991 ats_modify_problem_state (ats, ATS_QUALITY_UPDATED);
994 if (set == GNUNET_NO)
996 for (c=0; c<available_ressources; c++)
998 if (ats_index == ressources[c].atis_index)
1000 fal->ressources[c].c = value;
1002 ats_modify_problem_state (ats, ATS_COST_UPDATED);
1010 update_addr_ats (struct ForeignAddressList *fal,
1011 const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
1016 for (c1=0; c1<ats_count; c1++)
1018 set = update_addr_value(fal, ntohl(ats_data[c1].value), ntohl(ats_data[c1].type));
1024 * Find an entry in the transport list for a particular transport.
1026 * @return NULL if not found.
1028 static struct TransportPlugin *
1029 find_transport (const char *short_name)
1031 struct TransportPlugin *head = plugins;
1032 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
1038 * Is a particular peer blacklisted for a particular transport?
1040 * @param peer the peer to check for
1041 * @param plugin the plugin used to connect to the peer
1043 * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
1046 is_blacklisted (const struct GNUNET_PeerIdentity *peer, struct TransportPlugin *plugin)
1049 if (plugin->blacklist != NULL)
1051 if (GNUNET_CONTAINER_multihashmap_contains (plugin->blacklist, &peer->hashPubKey) == GNUNET_YES)
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "Peer `%s:%s' is blacklisted!\n",
1056 plugin->short_name, GNUNET_i2s (peer));
1059 GNUNET_STATISTICS_update (stats, "# blacklisted peers refused", 1, GNUNET_NO);
1069 add_peer_to_blacklist (struct GNUNET_PeerIdentity *peer,
1070 char *transport_name)
1072 struct TransportPlugin *plugin;
1074 plugin = find_transport(transport_name);
1075 if (plugin == NULL) /* Nothing to do */
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "Adding peer `%s' with plugin `%s' to blacklist\n",
1083 if (plugin->blacklist == NULL)
1084 plugin->blacklist = GNUNET_CONTAINER_multihashmap_create(TRANSPORT_BLACKLIST_HT_SIZE);
1085 GNUNET_assert(plugin->blacklist != NULL);
1086 GNUNET_CONTAINER_multihashmap_put(plugin->blacklist, &peer->hashPubKey,
1088 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1093 * Read the blacklist file, containing transport:peer entries.
1094 * Provided the transport is loaded, set up hashmap with these
1095 * entries to blacklist peers by transport.
1099 read_blacklist_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1106 struct GNUNET_PeerIdentity pid;
1108 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1109 unsigned int entries_found;
1110 char *transport_name;
1113 GNUNET_CONFIGURATION_get_value_filename (cfg,
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1120 "Option `%s' in section `%s' not specified!\n",
1126 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1127 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1128 | GNUNET_DISK_PERM_USER_WRITE);
1129 if (0 != STAT (fn, &frstat))
1131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1132 _("Could not read blacklist file `%s'\n"), fn);
1136 if (frstat.st_size == 0)
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 _("Blacklist file `%s' is empty.\n"),
1146 /* FIXME: use mmap */
1147 data = GNUNET_malloc_large (frstat.st_size);
1148 GNUNET_assert(data != NULL);
1149 if (frstat.st_size !=
1150 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1153 _("Failed to read blacklist from `%s'\n"), fn);
1160 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1162 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1163 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1166 while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') && !isspace ( (unsigned char) data[colon_pos]))
1169 if (colon_pos >= frstat.st_size)
1171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1172 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1173 (unsigned long long) colon_pos);
1179 if (isspace( (unsigned char) data[colon_pos]))
1181 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1182 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1183 (unsigned long long) colon_pos);
1185 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1189 tsize = colon_pos - pos;
1190 if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size) || (tsize == 0))
1192 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1193 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1194 (unsigned long long) colon_pos);
1203 transport_name = GNUNET_malloc(tsize + 1);
1204 memcpy(transport_name, &data[pos], tsize);
1205 pos = colon_pos + 1;
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "Read transport name %s in blacklist file.\n",
1211 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1212 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1214 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1215 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1216 (unsigned long long) pos);
1218 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1220 GNUNET_free_non_null(transport_name);
1223 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1224 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1226 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1227 _("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
1228 (unsigned long long) pos,
1233 if (0 != memcmp (&pid,
1235 sizeof (struct GNUNET_PeerIdentity)))
1238 add_peer_to_blacklist (&pid,
1243 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1244 _("Found myself `%s' in blacklist (useless, ignored)\n"),
1248 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1249 GNUNET_free_non_null(transport_name);
1250 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1253 GNUNET_STATISTICS_update (stats, "# Transport entries blacklisted", entries_found, GNUNET_NO);
1260 * Function called to notify a client about the socket being ready to
1261 * queue more data. "buf" will be NULL and "size" zero if the socket
1262 * was closed for writing in the meantime.
1264 * @param cls closure
1265 * @param size number of bytes available in buf
1266 * @param buf where the callee should write the message
1267 * @return number of bytes written to buf
1270 transmit_to_client_callback (void *cls, size_t size, void *buf)
1272 struct TransportClient *client = cls;
1273 struct ClientMessageQueueEntry *q;
1276 const struct GNUNET_MessageHeader *msg;
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1284 "Transmission to client failed, closing connection.\n");
1286 /* fatal error with client, free message queue! */
1287 while (NULL != (q = client->message_queue_head))
1289 GNUNET_STATISTICS_update (stats,
1290 gettext_noop ("# bytes discarded (could not transmit to client)"),
1291 ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
1293 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1294 client->message_queue_tail,
1298 client->message_count = 0;
1303 while (NULL != (q = client->message_queue_head))
1305 msg = (const struct GNUNET_MessageHeader *) &q[1];
1306 msize = ntohs (msg->size);
1307 if (msize + tsize > size)
1310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1311 "Transmitting message of type %u to client.\n",
1314 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1315 client->message_queue_tail,
1317 memcpy (&cbuf[tsize], msg, msize);
1320 client->message_count--;
1324 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1325 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1327 GNUNET_TIME_UNIT_FOREVER_REL,
1328 &transmit_to_client_callback,
1330 GNUNET_assert (client->th != NULL);
1337 * Convert an address to a string.
1339 * @param plugin name of the plugin responsible for the address
1340 * @param addr binary address
1341 * @param addr_len number of bytes in addr
1342 * @return NULL on error, otherwise address string
1345 a2s (const char *plugin,
1349 struct TransportPlugin *p;
1353 p = find_transport (plugin);
1354 if ((p == NULL) || (addr_len == 0) || (addr == NULL))
1357 return p->api->address_to_string (NULL,
1367 * Iterator to free entries in the validation_map.
1369 * @param cls closure (unused)
1370 * @param key current key code
1371 * @param value value in the hash map (validation to abort)
1372 * @return GNUNET_YES (always)
1375 abort_validation (void *cls,
1376 const GNUNET_HashCode * key,
1379 struct ValidationEntry *va = value;
1381 if (GNUNET_SCHEDULER_NO_TASK != va->timeout_task)
1382 GNUNET_SCHEDULER_cancel (va->timeout_task);
1383 GNUNET_free (va->transport_name);
1384 if (va->chvc != NULL)
1386 va->chvc->ve_count--;
1387 if (va->chvc->ve_count == 0)
1389 GNUNET_CONTAINER_DLL_remove (chvc_head,
1392 GNUNET_free (va->chvc);
1402 * HELLO validation cleanup task (validation failed).
1404 * @param cls the 'struct ValidationEntry' that failed
1405 * @param tc scheduler context (unused)
1408 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1410 struct ValidationEntry *va = cls;
1411 struct GNUNET_PeerIdentity pid;
1413 va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1414 GNUNET_STATISTICS_update (stats,
1415 gettext_noop ("# address validation timeouts"),
1418 GNUNET_CRYPTO_hash (&va->publicKey,
1420 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1422 GNUNET_break (GNUNET_OK ==
1423 GNUNET_CONTAINER_multihashmap_remove (validation_map,
1426 abort_validation (NULL, NULL, va);
1432 * Send the specified message to the specified client. Since multiple
1433 * messages may be pending for the same client at a time, this code
1434 * makes sure that no message is lost.
1436 * @param client client to transmit the message to
1437 * @param msg the message to send
1438 * @param may_drop can this message be dropped if the
1439 * message queue for this client is getting far too large?
1442 transmit_to_client (struct TransportClient *client,
1443 const struct GNUNET_MessageHeader *msg, int may_drop)
1445 struct ClientMessageQueueEntry *q;
1448 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1450 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1452 ("Dropping message of type %u and size %u, have %u messages pending (%u is the soft limit)\n"),
1455 client->message_count,
1457 GNUNET_STATISTICS_update (stats,
1458 gettext_noop ("# messages dropped due to slow client"),
1463 msize = ntohs (msg->size);
1464 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1465 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1466 memcpy (&q[1], msg, msize);
1467 GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1468 client->message_queue_tail,
1469 client->message_queue_tail,
1471 client->message_count++;
1472 if (client->th == NULL)
1474 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1476 GNUNET_TIME_UNIT_FOREVER_REL,
1477 &transmit_to_client_callback,
1479 GNUNET_assert (client->th != NULL);
1485 * Transmit a 'SEND_OK' notification to the given client for the
1488 * @param client who to notify
1489 * @param n neighbour to notify about, can be NULL (on failure)
1490 * @param target target of the transmission
1491 * @param result status code for the transmission request
1494 transmit_send_ok (struct TransportClient *client,
1495 struct NeighbourList *n,
1496 const struct GNUNET_PeerIdentity *target,
1499 struct SendOkMessage send_ok_msg;
1501 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1502 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1503 send_ok_msg.success = htonl (result);
1505 send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1507 send_ok_msg.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
1508 send_ok_msg.peer = *target;
1509 transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1514 * Mark the given FAL entry as 'connected' (and hence preferred for
1515 * sending); also mark all others for the same peer as 'not connected'
1516 * (since only one can be preferred).
1518 * @param fal address to set to 'connected'
1521 mark_address_connected (struct ForeignAddressList *fal);
1526 * We should re-try transmitting to the given peer,
1527 * hopefully we've learned something in the meantime.
1530 retry_transmission_task (void *cls,
1531 const struct GNUNET_SCHEDULER_TaskContext *tc)
1533 struct NeighbourList *n = cls;
1535 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1536 try_transmission_to_peer (n);
1541 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1542 * upon "completion" of a send request. This tells the API
1543 * that it is now legal to send another message to the given
1546 * @param cls closure, identifies the entry on the
1547 * message queue that was transmitted and the
1548 * client responsible for queuing the message
1549 * @param target the peer receiving the message
1550 * @param result GNUNET_OK on success, if the transmission
1551 * failed, we should not tell the client to transmit
1555 transmit_send_continuation (void *cls,
1556 const struct GNUNET_PeerIdentity *target,
1559 struct MessageQueue *mq = cls;
1560 struct NeighbourList *n;
1562 GNUNET_STATISTICS_update (stats,
1563 gettext_noop ("# bytes pending with plugins"),
1564 - (int64_t) mq->message_buf_size,
1566 if (result == GNUNET_OK)
1568 GNUNET_STATISTICS_update (stats,
1569 gettext_noop ("# bytes successfully transmitted by plugins"),
1570 mq->message_buf_size,
1575 GNUNET_STATISTICS_update (stats,
1576 gettext_noop ("# bytes with transmission failure by plugins"),
1577 mq->message_buf_size,
1580 if (mq->specific_address != NULL)
1582 if (result == GNUNET_OK)
1584 mq->specific_address->timeout =
1585 GNUNET_TIME_relative_to_absolute
1586 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1587 if (mq->specific_address->validated == GNUNET_YES)
1588 mark_address_connected (mq->specific_address);
1592 if (mq->specific_address->connected != GNUNET_NO)
1595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1596 "Marking address `%s' as no longer connected (due to transmission problem)\n",
1597 a2s (mq->specific_address->ready_list->plugin->short_name,
1598 mq->specific_address->addr,
1599 mq->specific_address->addrlen));
1601 GNUNET_STATISTICS_update (stats,
1602 gettext_noop ("# connected addresses"),
1605 mq->specific_address->connected = GNUNET_NO;
1608 if (! mq->internal_msg)
1609 mq->specific_address->in_transmit = GNUNET_NO;
1611 n = find_neighbour (&mq->neighbour_id);
1612 if (mq->client != NULL)
1613 transmit_send_ok (mq->client, n, target, result);
1616 GNUNET_CONTAINER_DLL_remove (n->cont_head,
1623 if (result == GNUNET_OK)
1624 try_transmission_to_peer (n);
1625 else if (GNUNET_SCHEDULER_NO_TASK == n->retry_task)
1626 n->retry_task = GNUNET_SCHEDULER_add_now (&retry_transmission_task,
1633 * Check the ready list for the given neighbour and if a plugin is
1634 * ready for transmission (and if we have a message), do so!
1636 * @param neighbour target peer for which to transmit
1639 try_transmission_to_peer (struct NeighbourList *n)
1641 struct ReadyList *rl;
1642 struct MessageQueue *mq;
1643 struct GNUNET_TIME_Relative timeout;
1647 if (n->messages_head == NULL)
1650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1651 "Transmission queue for `%4s' is empty\n",
1652 GNUNET_i2s (&n->id));
1654 return; /* nothing to do */
1657 mq = n->messages_head;
1658 force_address = GNUNET_YES;
1659 if (mq->specific_address == NULL)
1662 mq->specific_address = get_preferred_ats_address(n);
1663 GNUNET_STATISTICS_update (stats,
1664 gettext_noop ("# transport selected peer address freely"),
1667 force_address = GNUNET_NO;
1669 if (mq->specific_address == NULL)
1671 GNUNET_STATISTICS_update (stats,
1672 gettext_noop ("# transport failed to selected peer address"),
1675 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1676 if (timeout.rel_value == 0)
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1680 "No destination address available to transmit message of size %u to peer `%4s'\n",
1681 mq->message_buf_size,
1682 GNUNET_i2s (&mq->neighbour_id));
1684 GNUNET_STATISTICS_update (stats,
1685 gettext_noop ("# bytes in message queue for other peers"),
1686 - (int64_t) mq->message_buf_size,
1688 GNUNET_STATISTICS_update (stats,
1689 gettext_noop ("# bytes discarded (no destination address available)"),
1690 mq->message_buf_size,
1692 if (mq->client != NULL)
1693 transmit_send_ok (mq->client, n, &n->id, GNUNET_NO);
1694 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1698 return; /* nobody ready */
1700 GNUNET_STATISTICS_update (stats,
1701 gettext_noop ("# message delivery deferred (no address)"),
1704 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
1705 GNUNET_SCHEDULER_cancel (n->retry_task);
1706 n->retry_task = GNUNET_SCHEDULER_add_delayed (timeout,
1707 &retry_transmission_task,
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1711 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
1712 mq->message_buf_size,
1713 GNUNET_i2s (&mq->neighbour_id),
1716 /* FIXME: might want to trigger peerinfo lookup here
1717 (unless that's already pending...) */
1720 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1723 if (mq->specific_address->connected == GNUNET_NO)
1724 mq->specific_address->connect_attempts++;
1725 rl = mq->specific_address->ready_list;
1726 mq->plugin = rl->plugin;
1727 if (!mq->internal_msg)
1728 mq->specific_address->in_transmit = GNUNET_YES;
1730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1731 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
1732 mq->message_buf_size,
1733 GNUNET_i2s (&n->id),
1734 (mq->specific_address->addr != NULL)
1735 ? a2s (mq->plugin->short_name,
1736 mq->specific_address->addr,
1737 mq->specific_address->addrlen)
1739 rl->plugin->short_name);
1741 GNUNET_STATISTICS_update (stats,
1742 gettext_noop ("# bytes in message queue for other peers"),
1743 - (int64_t) mq->message_buf_size,
1745 GNUNET_STATISTICS_update (stats,
1746 gettext_noop ("# bytes pending with plugins"),
1747 mq->message_buf_size,
1750 GNUNET_CONTAINER_DLL_insert (n->cont_head,
1754 ret = rl->plugin->api->send (rl->plugin->api->cls,
1757 mq->message_buf_size,
1759 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1760 mq->specific_address->session,
1761 mq->specific_address->addr,
1762 mq->specific_address->addrlen,
1764 &transmit_send_continuation, mq);
1767 /* failure, but 'send' would not call continuation in this case,
1768 so we need to do it here! */
1769 transmit_send_continuation (mq,
1777 * Send the specified message to the specified peer.
1779 * @param client source of the transmission request (can be NULL)
1780 * @param peer_address ForeignAddressList where we should send this message
1781 * @param priority how important is the message
1782 * @param timeout how long do we have to transmit?
1783 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
1784 * @param message_buf_size total size of all messages in message_buf
1785 * @param is_internal is this an internal message; these are pre-pended and
1786 * also do not count for plugins being "ready" to transmit
1787 * @param neighbour handle to the neighbour for transmission
1790 transmit_to_peer (struct TransportClient *client,
1791 struct ForeignAddressList *peer_address,
1792 unsigned int priority,
1793 struct GNUNET_TIME_Relative timeout,
1794 const char *message_buf,
1795 size_t message_buf_size,
1796 int is_internal, struct NeighbourList *neighbour)
1798 struct MessageQueue *mq;
1803 /* check for duplicate submission */
1804 mq = neighbour->messages_head;
1807 if (mq->client == client)
1809 /* client transmitted to same peer twice
1810 before getting SEND_OK! */
1818 GNUNET_STATISTICS_update (stats,
1819 gettext_noop ("# bytes in message queue for other peers"),
1822 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
1823 mq->specific_address = peer_address;
1824 mq->client = client;
1825 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1826 memcpy (&mq[1], message_buf, message_buf_size);
1827 mq->message_buf = (const char*) &mq[1];
1828 mq->message_buf_size = message_buf_size;
1829 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
1830 mq->internal_msg = is_internal;
1831 mq->priority = priority;
1832 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1834 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
1835 neighbour->messages_tail,
1838 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
1839 neighbour->messages_tail,
1840 neighbour->messages_tail,
1842 try_transmission_to_peer (neighbour);
1847 * Send a plain PING (without address or our HELLO) to the given
1848 * foreign address to try to establish a connection (and validate
1849 * that the other peer is really who he claimed he is).
1851 * @param n neighbour to PING
1854 transmit_plain_ping (struct NeighbourList *n)
1856 struct ValidationEntry *ve;
1857 struct TransportPingMessage ping;
1858 struct ReadyList *rl;
1859 struct TransportPlugin *plugin;
1860 struct ForeignAddressList *fal;
1862 if (! n->public_key_valid)
1864 /* This should not happen since the other peer
1865 should send us a HELLO prior to sending his
1867 GNUNET_break_op (0);
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1869 "Could not transmit plain PING to `%s': public key not known\n",
1870 GNUNET_i2s (&n->id));
1873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1874 "Looking for addresses to transmit plain PING to `%s'\n",
1875 GNUNET_i2s (&n->id));
1876 for (rl = n->plugins; rl != NULL; rl = rl->next)
1878 plugin = rl->plugin;
1879 for (fal = rl->addresses; fal != NULL; fal = fal->next)
1881 if (! fal->connected)
1883 ve = GNUNET_malloc (sizeof (struct ValidationEntry));
1884 ve->transport_name = GNUNET_strdup (plugin->short_name);
1885 ve->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1887 ve->send_time = GNUNET_TIME_absolute_get();
1888 ve->session = fal->session;
1889 memcpy(&ve->publicKey,
1891 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1892 ve->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
1893 &timeout_hello_validation,
1895 GNUNET_CONTAINER_multihashmap_put (validation_map,
1898 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1899 ping.header.size = htons(sizeof(struct TransportPingMessage));
1900 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
1901 ping.challenge = htonl(ve->challenge);
1902 memcpy(&ping.target, &n->id, sizeof(struct GNUNET_PeerIdentity));
1903 GNUNET_STATISTICS_update (stats,
1904 gettext_noop ("# PING without HELLO messages sent"),
1907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1908 "Transmitting plain PING to `%s'\n",
1909 GNUNET_i2s (&n->id));
1910 transmit_to_peer (NULL,
1912 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1913 HELLO_VERIFICATION_TIMEOUT,
1914 (const char*) &ping, sizeof (ping),
1922 * Mark the given FAL entry as 'connected' (and hence preferred for
1923 * sending); also mark all others for the same peer as 'not connected'
1924 * (since only one can be preferred).
1926 * @param fal address to set to 'connected'
1929 mark_address_connected (struct ForeignAddressList *fal)
1931 struct ForeignAddressList *pos;
1934 GNUNET_assert (GNUNET_YES == fal->validated);
1935 if (fal->connected == GNUNET_YES)
1936 return; /* nothing to do */
1938 pos = fal->ready_list->addresses;
1941 if (GNUNET_YES == pos->connected)
1944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1945 "Marking address `%s' as no longer connected (due to connect on other address)\n",
1946 a2s (pos->ready_list->plugin->short_name,
1950 GNUNET_break (cnt == GNUNET_YES);
1952 pos->connected = GNUNET_NO;
1953 GNUNET_STATISTICS_update (stats,
1954 gettext_noop ("# connected addresses"),
1960 fal->connected = GNUNET_YES;
1961 if (GNUNET_YES == cnt)
1963 GNUNET_STATISTICS_update (stats,
1964 gettext_noop ("# connected addresses"),
1972 * Find an address in any of the available transports for
1973 * the given neighbour that would be good for message
1974 * transmission. This is essentially the transport selection
1977 * @param neighbour for whom to select an address
1978 * @return selected address, NULL if we have none
1980 struct ForeignAddressList *
1981 find_ready_address(struct NeighbourList *neighbour)
1983 struct ReadyList *head = neighbour->plugins;
1984 struct ForeignAddressList *addresses;
1985 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1986 struct ForeignAddressList *best_address;
1988 /* Hack to prefer unix domain sockets */
1989 struct ForeignAddressList *unix_address = NULL;
1991 best_address = NULL;
1992 while (head != NULL)
1994 addresses = head->addresses;
1995 while (addresses != NULL)
1997 if ( (addresses->timeout.abs_value < now.abs_value) &&
1998 (addresses->connected == GNUNET_YES) )
2001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2002 "Marking long-time inactive connection to `%4s' as down.\n",
2003 GNUNET_i2s (&neighbour->id));
2005 GNUNET_STATISTICS_update (stats,
2006 gettext_noop ("# connected addresses"),
2009 addresses->connected = GNUNET_NO;
2011 addresses = addresses->next;
2014 addresses = head->addresses;
2015 while (addresses != NULL)
2018 if (addresses->addr != NULL)
2019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2020 "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
2021 a2s (head->plugin->short_name,
2023 addresses->addrlen),
2024 GNUNET_i2s (&neighbour->id),
2025 addresses->connected,
2026 addresses->in_transmit,
2027 addresses->validated,
2028 addresses->connect_attempts,
2029 (unsigned long long) addresses->timeout.abs_value,
2030 (unsigned int) addresses->distance);
2032 if (0==strcmp(head->plugin->short_name,"unix"))
2034 if ( (unix_address == NULL) ||
2035 ( (unix_address != NULL) &&
2036 (addresses->latency.rel_value < unix_address->latency.rel_value) ) )
2037 unix_address = addresses;
2039 if ( ( (best_address == NULL) ||
2040 (addresses->connected == GNUNET_YES) ||
2041 (best_address->connected == GNUNET_NO) ) &&
2042 (addresses->in_transmit == GNUNET_NO) &&
2043 ( (best_address == NULL) ||
2044 (addresses->latency.rel_value < best_address->latency.rel_value)) )
2045 best_address = addresses;
2046 /* FIXME: also give lower-latency addresses that are not
2047 connected a chance some times... */
2048 addresses = addresses->next;
2050 if (unix_address != NULL)
2054 if (unix_address != NULL)
2056 best_address = unix_address;
2058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2059 "Found UNIX address, forced this address\n");
2062 if (best_address != NULL)
2065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2066 "Best address found (`%s') has latency of %llu ms.\n",
2067 (best_address->addrlen > 0)
2068 ? a2s (best_address->ready_list->plugin->short_name,
2070 best_address->addrlen)
2072 best_address->latency.rel_value);
2077 GNUNET_STATISTICS_update (stats,
2078 gettext_noop ("# transmission attempts failed (no address)"),
2083 return best_address;
2091 struct GeneratorContext
2093 struct TransportPlugin *plug_pos;
2094 struct OwnAddressList *addr_pos;
2095 struct GNUNET_TIME_Absolute expiration;
2103 address_generator (void *cls, size_t max, void *buf)
2105 struct GeneratorContext *gc = cls;
2108 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
2110 gc->plug_pos = gc->plug_pos->next;
2111 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
2113 if (NULL == gc->plug_pos)
2118 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
2121 gc->addr_pos->addrlen, buf, max);
2122 gc->addr_pos = gc->addr_pos->next;
2128 * Construct our HELLO message from all of the addresses of
2129 * all of the transports.
2132 * @param tc scheduler context
2135 refresh_hello_task (void *cls,
2136 const struct GNUNET_SCHEDULER_TaskContext *tc)
2138 struct GNUNET_HELLO_Message *hello;
2139 struct TransportClient *cpos;
2140 struct NeighbourList *npos;
2141 struct GeneratorContext gc;
2143 hello_task = GNUNET_SCHEDULER_NO_TASK;
2144 gc.plug_pos = plugins;
2145 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
2146 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2147 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2150 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
2152 GNUNET_STATISTICS_update (stats,
2153 gettext_noop ("# refreshed my HELLO"),
2157 while (cpos != NULL)
2159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2160 "Transmitting my HELLO to client!\n");
2161 transmit_to_client (cpos,
2162 (const struct GNUNET_MessageHeader *) hello,
2167 GNUNET_free_non_null (our_hello);
2169 GNUNET_PEERINFO_add_peer (peerinfo, our_hello);
2170 for (npos = neighbours; npos != NULL; npos = npos->next)
2172 if (! npos->received_pong)
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2176 "Transmitting updated `%s' to neighbour `%4s'\n",
2177 "HELLO", GNUNET_i2s (&npos->id));
2179 GNUNET_STATISTICS_update (stats,
2180 gettext_noop ("# transmitted my HELLO to other peers"),
2183 transmit_to_peer (NULL, NULL, 0,
2184 HELLO_ADDRESS_EXPIRATION,
2185 (const char *) our_hello,
2186 GNUNET_HELLO_size(our_hello),
2193 * Schedule task to refresh hello (unless such a
2194 * task exists already).
2199 #if DEBUG_TRANSPORT_HELLO
2200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2201 "refresh_hello() called!\n");
2203 if (hello_task != GNUNET_SCHEDULER_NO_TASK)
2206 = GNUNET_SCHEDULER_add_now (&refresh_hello_task,
2212 * Iterator over hash map entries that NULLs the session of validation
2213 * entries that match the given session.
2215 * @param cls closure (the 'struct Session*' to match against)
2216 * @param key current key code (peer ID, not used)
2217 * @param value value in the hash map ('struct ValidationEntry*')
2218 * @return GNUNET_YES (we should continue to iterate)
2221 remove_session_validations (void *cls,
2222 const GNUNET_HashCode * key,
2225 struct Session *session = cls;
2226 struct ValidationEntry *ve = value;
2228 if (session == ve->session)
2235 * We've been disconnected from the other peer (for some
2236 * connection-oriented transport). Either quickly
2237 * re-establish the connection or signal the disconnect
2240 * Only signal CORE level disconnect if ALL addresses
2241 * for the peer are exhausted.
2243 * @param p overall plugin context
2244 * @param nl neighbour that was disconnected
2247 try_fast_reconnect (struct TransportPlugin *p,
2248 struct NeighbourList *nl)
2250 /* FIXME-MW: fast reconnect / transport switching not implemented... */
2251 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2252 "try_fast_reconnect not implemented!\n");
2253 /* Note: the idea here is to hide problems with transports (or
2254 switching between plugins) from the core to eliminate the need to
2255 re-negotiate session keys and the like; OTOH, we should tell core
2256 quickly (much faster than timeout) `if a connection was lost and
2257 could not be re-established (i.e. other peer went down or is
2258 unable / refuses to communicate);
2260 So we should consider:
2261 1) ideally: our own willingness / need to connect
2262 2) prior failures to connect to this peer (by plugin)
2263 3) ideally: reasons why other peer terminated (as far as knowable)
2265 Most importantly, it must be POSSIBLE for another peer to terminate
2266 a connection for a while (without us instantly re-establishing it).
2267 Similarly, if another peer is gone we should quickly notify CORE.
2268 OTOH, if there was a minor glitch (i.e. crash of gnunet-service-transport
2269 on the other end), we should reconnect in such a way that BOTH CORE
2270 services never even notice.
2271 Furthermore, the same mechanism (or small variation) could be used
2272 to switch to a better-performing plugin (ATS).
2274 Finally, this needs to be tested throughly... */
2277 * GNUNET_NO in the call below makes transport disconnect the peer,
2278 * even if only a single address (out of say, six) went away. This
2279 * function must be careful to ONLY disconnect if the peer is gone,
2280 * not just a specific address.
2282 * More specifically, half the places it was used had it WRONG.
2285 /* No reconnect, signal disconnect instead! */
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2288 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2289 "try_fast_reconnect");
2291 GNUNET_STATISTICS_update (stats,
2292 gettext_noop ("# disconnects due to try_fast_reconnect"),
2296 disconnect_neighbour (nl, GNUNET_YES);
2302 * Function that will be called whenever the plugin internally
2303 * cleans up a session pointer and hence the service needs to
2304 * discard all of those sessions as well. Plugins that do not
2305 * use sessions can simply omit calling this function and always
2306 * use NULL wherever a session pointer is needed.
2308 * @param cls closure
2309 * @param peer which peer was the session for
2310 * @param session which session is being destoyed
2313 plugin_env_session_end (void *cls,
2314 const struct GNUNET_PeerIdentity *peer,
2315 struct Session *session)
2317 struct TransportPlugin *p = cls;
2318 struct NeighbourList *nl;
2319 struct ReadyList *rl;
2320 struct ForeignAddressList *pos;
2321 struct ForeignAddressList *prev;
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 "Session ended with peer `%4s', %s\n",
2327 "plugin_env_session_end");
2329 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2330 &remove_session_validations,
2332 nl = find_neighbour (peer);
2336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2337 "No neighbour record found for peer `%4s'\n",
2340 return; /* was never marked as connected */
2345 if (rl->plugin == p)
2352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2353 "Plugin was associated with peer `%4s'\n",
2356 GNUNET_STATISTICS_update (stats,
2357 gettext_noop ("# disconnects due to session end"),
2360 disconnect_neighbour (nl, GNUNET_YES);
2364 pos = rl->addresses;
2365 while ( (pos != NULL) &&
2366 (pos->session != session) )
2374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2375 "Session was never marked as ready for peer `%4s'\n",
2379 int validations_pending = GNUNET_CONTAINER_multihashmap_contains (validation_map, &peer->hashPubKey);
2381 /* No session was marked as ready, but we have pending validations so do not disconnect from neighbour */
2382 if (validations_pending ==GNUNET_YES)
2385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2386 "Not disconnecting from peer `%4s due to pending address validations\n", GNUNET_i2s(peer));
2391 //FIXME: This conflicts with inbound tcp connections and tcp nat ... debugging in progress
2392 GNUNET_STATISTICS_update (stats,
2393 gettext_noop ("# disconnects due to unready session"),
2397 disconnect_neighbour (nl, GNUNET_YES);
2398 return; /* was never marked as connected */
2400 pos->session = NULL;
2401 pos->connected = GNUNET_NO;
2402 if (pos->addrlen != 0)
2404 if (nl->received_pong != GNUNET_NO)
2406 GNUNET_STATISTICS_update (stats,
2407 gettext_noop ("# try_fast_reconnect thanks to plugin_env_session_end"),
2410 if (GNUNET_YES == pos->connected)
2411 try_fast_reconnect (p, nl);
2415 GNUNET_STATISTICS_update (stats,
2416 gettext_noop ("# disconnects due to missing pong"),
2419 if (GNUNET_YES == pos->connected)
2420 disconnect_neighbour (nl, GNUNET_YES);
2424 /* was inbound connection, free 'pos' */
2426 rl->addresses = pos->next;
2428 prev->next = pos->next;
2429 if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2431 GNUNET_SCHEDULER_cancel (pos->revalidate_task);
2432 pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2434 GNUNET_free_non_null(pos->ressources);
2435 GNUNET_free_non_null(pos->quality);
2436 ats_modify_problem_state (ats, ATS_MODIFIED);
2438 if (GNUNET_YES != pos->connected)
2440 /* nothing else to do, connection was never up... */
2446 if (nl->received_pong == GNUNET_NO)
2448 GNUNET_STATISTICS_update (stats,
2449 gettext_noop ("# disconnects due to NO pong"),
2452 disconnect_neighbour (nl, GNUNET_YES);
2453 return; /* nothing to do, never connected... */
2455 /* check if we have any validated addresses left */
2456 pos = rl->addresses;
2459 if (GNUNET_YES == pos->validated)
2461 GNUNET_STATISTICS_update (stats,
2462 gettext_noop ("# try_fast_reconnect thanks to validated_address"),
2465 try_fast_reconnect (p, nl);
2470 /* no valid addresses left, signal disconnect! */
2473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2474 "Disconnecting peer `%4s', %s\n",
2476 "plugin_env_session_end");
2478 /* FIXME: This doesn't mean there are no addresses left for this PEER,
2479 * it means there aren't any left for this PLUGIN/PEER combination! So
2480 * calling disconnect_neighbour here with GNUNET_NO forces disconnect
2481 * when it isn't necessary. Using GNUNET_YES at least checks to see
2482 * if there are any addresses that work first, so as not to overdo it.
2485 GNUNET_STATISTICS_update (stats,
2486 gettext_noop ("# disconnects due to plugin_env_session_end"),
2489 disconnect_neighbour (nl, GNUNET_YES);
2494 * Function that must be called by each plugin to notify the
2495 * transport service about the addresses under which the transport
2496 * provided by the plugin can be reached.
2498 * @param cls closure
2499 * @param add_remove GNUNET_YES to add, GNUNET_NO to remove the address
2500 * @param addr one of the addresses of the host, NULL for the last address
2501 * the specific address format depends on the transport
2502 * @param addrlen length of the address
2505 plugin_env_notify_address (void *cls,
2510 struct TransportPlugin *p = cls;
2511 struct OwnAddressList *al;
2512 struct OwnAddressList *prev;
2514 GNUNET_assert (p->api != NULL);
2516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2517 (add_remove == GNUNET_YES)
2518 ? "Adding `%s':%s to the set of our addresses\n"
2519 : "Removing `%s':%s from the set of our addresses\n",
2524 GNUNET_assert (addr != NULL);
2525 if (GNUNET_NO == add_remove)
2531 if ( (addrlen == al->addrlen) &&
2532 (0 == memcmp (addr, &al[1], addrlen)) )
2535 p->addresses = al->next;
2537 prev->next = al->next;
2548 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2549 al->next = p->addresses;
2551 al->addrlen = addrlen;
2552 memcpy (&al[1], addr, addrlen);
2558 * Notify all of our clients about a peer connecting.
2561 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2562 struct GNUNET_TIME_Relative latency,
2565 struct ConnectInfoMessage * cim;
2566 struct TransportClient *cpos;
2570 if (0 == memcmp (peer,
2572 sizeof (struct GNUNET_PeerIdentity)))
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2579 "Notifying clients about connection with `%s'\n",
2582 GNUNET_STATISTICS_update (stats,
2583 gettext_noop ("# peers connected"),
2588 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
2589 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2593 cim = GNUNET_malloc (size);
2594 cim->header.size = htons (size);
2595 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2596 cim->ats_count = htonl(2);
2597 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2598 (&(cim->ats))[0].value = htonl (distance);
2599 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
2600 (&(cim->ats))[1].value = htonl ((uint32_t) latency.rel_value);
2601 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2602 (&(cim->ats))[2].value = htonl (0);
2603 memcpy (&cim->id, peer, sizeof (struct GNUNET_PeerIdentity));
2605 /* notify ats about connecting peer */
2606 /* notify ats about connecting peer */
2607 if ((ats != NULL) && (shutdown_in_progress == GNUNET_NO))
2609 ats_modify_problem_state(ats, ATS_MODIFIED);
2610 ats_calculate_bandwidth_distribution (ats, stats, neighbours);
2615 while (cpos != NULL)
2617 transmit_to_client (cpos, &(cim->header), GNUNET_NO);
2626 * Notify all of our clients about a peer disconnecting.
2629 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2631 struct DisconnectInfoMessage dim;
2632 struct TransportClient *cpos;
2634 if (0 == memcmp (peer,
2636 sizeof (struct GNUNET_PeerIdentity)))
2642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2643 "Notifying clients about lost connection to `%s'\n",
2646 GNUNET_STATISTICS_update (stats,
2647 gettext_noop ("# peers connected"),
2650 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2651 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2652 dim.reserved = htonl (0);
2653 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2655 /* notify ats about connecting peer */
2656 if ((ats != NULL) && (shutdown_in_progress == GNUNET_NO))
2658 ats_modify_problem_state(ats, ATS_MODIFIED);
2659 ats_calculate_bandwidth_distribution (ats, stats, neighbours);
2663 while (cpos != NULL)
2665 transmit_to_client (cpos, &dim.header, GNUNET_NO);
2672 * Find a ForeignAddressList entry for the given neighbour
2673 * that matches the given address and transport.
2675 * @param neighbour which peer we care about
2676 * @param tname name of the transport plugin
2677 * @param session session to look for, NULL for 'any'; otherwise
2678 * can be used for the service to "learn" this session ID
2680 * @param addr binary address
2681 * @param addrlen length of addr
2682 * @return NULL if no such entry exists
2684 static struct ForeignAddressList *
2685 find_peer_address(struct NeighbourList *neighbour,
2687 struct Session *session,
2691 struct ReadyList *head;
2692 struct ForeignAddressList *pos;
2694 head = neighbour->plugins;
2695 while (head != NULL)
2697 if (0 == strcmp (tname, head->plugin->short_name))
2703 pos = head->addresses;
2704 while ( (pos != NULL) &&
2705 ( (pos->addrlen != addrlen) ||
2706 (memcmp(pos->addr, addr, addrlen) != 0) ) )
2708 if ( (session != NULL) &&
2709 (pos->session == session) )
2713 if ( (session != NULL) && (pos != NULL) )
2714 pos->session = session; /* learn it! */
2720 * Get the peer address struct for the given neighbour and
2721 * address. If it doesn't yet exist, create it.
2723 * @param neighbour which peer we care about
2724 * @param tname name of the transport plugin
2725 * @param session session of the plugin, or NULL for none
2726 * @param addr binary address
2727 * @param addrlen length of addr
2728 * @return NULL if we do not have a transport plugin for 'tname'
2730 static struct ForeignAddressList *
2731 add_peer_address (struct NeighbourList *neighbour,
2733 struct Session *session,
2737 struct ReadyList *head;
2738 struct ForeignAddressList *ret;
2741 ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2744 head = neighbour->plugins;
2746 while (head != NULL)
2748 if (0 == strcmp (tname, head->plugin->short_name))
2754 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2755 ret->session = session;
2756 if ((addrlen > 0) && (addr != NULL))
2758 ret->addr = (const char*) &ret[1];
2759 memcpy (&ret[1], addr, addrlen);
2766 ret->ressources = GNUNET_malloc(available_ressources * sizeof (struct ATS_ressource_entry));
2767 for (c=0; c<available_ressources; c++)
2769 struct ATS_ressource_entry *r = ret->ressources;
2771 r[c].atis_index = ressources[c].atis_index;
2772 if (0 == strcmp(neighbour->plugins->plugin->short_name,"unix"))
2774 r[c].c = ressources[c].c_unix;
2776 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"udp"))
2778 r[c].c = ressources[c].c_udp;
2780 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"tcp"))
2782 r[c].c = ressources[c].c_tcp;
2784 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"http"))
2786 r[c].c = ressources[c].c_http;
2788 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"https"))
2790 r[c].c = ressources[c].c_https;
2792 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"wlan"))
2794 r[c].c = ressources[c].c_wlan;
2798 r[c].c = ressources[c].c_default;
2799 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2800 "Assigning default cost to peer `%s' addr plugin `%s'! This should not happen!\n",
2801 GNUNET_i2s(&neighbour->peer),
2802 neighbour->plugins->plugin->short_name);
2806 ret->quality = GNUNET_malloc (available_quality_metrics * sizeof (struct ATS_quality_entry));
2807 ret->addrlen = addrlen;
2808 ret->expires = GNUNET_TIME_relative_to_absolute
2809 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2810 ret->latency = GNUNET_TIME_relative_get_forever();
2812 ret->timeout = GNUNET_TIME_relative_to_absolute
2813 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2814 ret->ready_list = head;
2815 ret->next = head->addresses;
2816 head->addresses = ret;
2822 * Closure for 'add_validated_address'.
2824 struct AddValidatedAddressContext
2827 * Entry that has been validated.
2829 const struct ValidationEntry *ve;
2832 * Flag set after we have added the address so
2833 * that we terminate the iteration next time.
2840 * Callback function used to fill a buffer of max bytes with a list of
2841 * addresses in the format used by HELLOs. Should use
2842 * "GNUNET_HELLO_add_address" as a helper function.
2844 * @param cls the 'struct AddValidatedAddressContext' with the validated address
2845 * @param max maximum number of bytes that can be written to buf
2846 * @param buf where to write the address information
2847 * @return number of bytes written, 0 to signal the
2848 * end of the iteration.
2851 add_validated_address (void *cls,
2852 size_t max, void *buf)
2854 struct AddValidatedAddressContext *avac = cls;
2855 const struct ValidationEntry *ve = avac->ve;
2857 if (GNUNET_YES == avac->done)
2859 avac->done = GNUNET_YES;
2860 return GNUNET_HELLO_add_address (ve->transport_name,
2861 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2871 * Closure for 'check_address_exists'.
2873 struct CheckAddressExistsClosure
2876 * Address to check for.
2881 * Name of the transport.
2888 struct Session *session;
2891 * Set to GNUNET_YES if the address exists.
2904 * Iterator over hash map entries. Checks if the given
2905 * validation entry is for the same address as what is given
2908 * @param cls the 'struct CheckAddressExistsClosure*'
2909 * @param key current key code (ignored)
2910 * @param value value in the hash map ('struct ValidationEntry')
2911 * @return GNUNET_YES if we should continue to
2912 * iterate (mismatch), GNUNET_NO if not (entry matched)
2915 check_address_exists (void *cls,
2916 const GNUNET_HashCode * key,
2919 struct CheckAddressExistsClosure *caec = cls;
2920 struct ValidationEntry *ve = value;
2922 if ( (0 == strcmp (caec->tname,
2923 ve->transport_name)) &&
2924 (caec->addrlen == ve->addrlen) &&
2925 (0 == memcmp (caec->addr,
2929 caec->exists = GNUNET_YES;
2932 if ( (ve->session != NULL) &&
2933 (caec->session == ve->session) )
2935 caec->exists = GNUNET_YES;
2943 neighbour_timeout_task (void *cls,
2944 const struct GNUNET_SCHEDULER_TaskContext *tc)
2946 struct NeighbourList *n = cls;
2949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2950 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
2952 GNUNET_STATISTICS_update (stats,
2953 gettext_noop ("# disconnects due to timeout"),
2956 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2957 disconnect_neighbour (n, GNUNET_NO);
2962 * Schedule the job that will cause us to send a PING to the
2963 * foreign address to evaluate its validity and latency.
2965 * @param fal address to PING
2968 schedule_next_ping (struct ForeignAddressList *fal);
2972 * Add the given address to the list of foreign addresses
2973 * available for the given peer (check for duplicates).
2975 * @param cls the respective 'struct NeighbourList' to update
2976 * @param tname name of the transport
2977 * @param expiration expiration time
2978 * @param addr the address
2979 * @param addrlen length of the address
2980 * @return GNUNET_OK (always)
2983 add_to_foreign_address_list (void *cls,
2985 struct GNUNET_TIME_Absolute expiration,
2989 struct NeighbourList *n = cls;
2990 struct ForeignAddressList *fal;
2993 GNUNET_STATISTICS_update (stats,
2994 gettext_noop ("# valid peer addresses returned by PEERINFO"),
2998 fal = find_peer_address (n, tname, NULL, addr, addrlen);
3001 #if DEBUG_TRANSPORT_HELLO
3002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3003 "Adding address `%s' (%s) for peer `%4s' due to PEERINFO data for %llums.\n",
3004 a2s (tname, addr, addrlen),
3006 GNUNET_i2s (&n->id),
3007 expiration.abs_value);
3009 fal = add_peer_address (n, tname, NULL, addr, addrlen);
3012 GNUNET_STATISTICS_update (stats,
3013 gettext_noop ("# previously validated addresses lacking transport"),
3019 fal->expires = GNUNET_TIME_absolute_max (expiration,
3021 schedule_next_ping (fal);
3027 fal->expires = GNUNET_TIME_absolute_max (expiration,
3033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3034 "Failed to add new address for `%4s'\n",
3035 GNUNET_i2s (&n->id));
3039 if (fal->validated == GNUNET_NO)
3041 fal->validated = GNUNET_YES;
3042 GNUNET_STATISTICS_update (stats,
3043 gettext_noop ("# peer addresses considered valid"),
3047 if (try == GNUNET_YES)
3050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3051 "Have new addresses, will try to trigger transmissions.\n");
3053 try_transmission_to_peer (n);
3060 * Add addresses in validated HELLO "h" to the set of addresses
3061 * we have for this peer.
3063 * @param cls closure ('struct NeighbourList*')
3064 * @param peer id of the peer, NULL for last call
3065 * @param h hello message for the peer (can be NULL)
3066 * @param err_msg NULL if successful, otherwise contains error message
3069 add_hello_for_peer (void *cls,
3070 const struct GNUNET_PeerIdentity *peer,
3071 const struct GNUNET_HELLO_Message *h,
3072 const char *err_msg)
3074 struct NeighbourList *n = cls;
3076 if (err_msg != NULL)
3079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3080 _("Error in communication with PEERINFO service: %s\n"),
3087 GNUNET_STATISTICS_update (stats,
3088 gettext_noop ("# outstanding peerinfo iterate requests"),
3095 return; /* no HELLO available */
3097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3098 "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
3102 if (GNUNET_YES != n->public_key_valid)
3104 GNUNET_HELLO_get_key (h, &n->publicKey);
3105 n->public_key_valid = GNUNET_YES;
3107 GNUNET_HELLO_iterate_addresses (h,
3109 &add_to_foreign_address_list,
3115 * Create a fresh entry in our neighbour list for the given peer.
3116 * Will try to transmit our current HELLO to the new neighbour.
3117 * Do not call this function directly, use 'setup_peer_check_blacklist.
3119 * @param peer the peer for which we create the entry
3120 * @param do_hello should we schedule transmitting a HELLO
3121 * @return the new neighbour list entry
3123 static struct NeighbourList *
3124 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
3127 struct NeighbourList *n;
3128 struct TransportPlugin *tp;
3129 struct ReadyList *rl;
3131 GNUNET_assert (0 != memcmp (peer,
3133 sizeof (struct GNUNET_PeerIdentity)));
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3136 "Setting up state for neighbour `%4s'\n",
3139 GNUNET_STATISTICS_update (stats,
3140 gettext_noop ("# active neighbours"),
3143 n = GNUNET_malloc (sizeof (struct NeighbourList));
3144 n->next = neighbours;
3148 GNUNET_TIME_relative_to_absolute
3149 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3150 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
3151 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3152 MAX_BANDWIDTH_CARRY_S);
3156 if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
3158 rl = GNUNET_malloc (sizeof (struct ReadyList));
3160 rl->next = n->plugins;
3163 rl->addresses = NULL;
3167 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
3169 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
3170 &neighbour_timeout_task, n);
3173 GNUNET_STATISTICS_update (stats,
3174 gettext_noop ("# peerinfo new neighbor iterate requests"),
3177 GNUNET_STATISTICS_update (stats,
3178 gettext_noop ("# outstanding peerinfo iterate requests"),
3181 n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
3182 GNUNET_TIME_UNIT_FOREVER_REL,
3183 &add_hello_for_peer, n);
3185 GNUNET_STATISTICS_update (stats,
3186 gettext_noop ("# HELLO's sent to new neighbors"),
3189 if (NULL != our_hello)
3190 transmit_to_peer (NULL, NULL, 0,
3191 HELLO_ADDRESS_EXPIRATION,
3192 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
3200 * Function called after we have checked if communicating
3201 * with a given peer is acceptable.
3203 * @param cls closure
3204 * @param n NULL if communication is not acceptable
3206 typedef void (*SetupContinuation)(void *cls,
3207 struct NeighbourList *n);
3211 * Information kept for each client registered to perform
3217 * This is a linked list.
3219 struct Blacklisters *next;
3222 * This is a linked list.
3224 struct Blacklisters *prev;
3227 * Client responsible for this entry.
3229 struct GNUNET_SERVER_Client *client;
3232 * Blacklist check that we're currently performing.
3234 struct BlacklistCheck *bc;
3240 * Head of DLL of blacklisting clients.
3242 static struct Blacklisters *bl_head;
3245 * Tail of DLL of blacklisting clients.
3247 static struct Blacklisters *bl_tail;
3251 * Context we use when performing a blacklist check.
3253 struct BlacklistCheck
3257 * This is a linked list.
3259 struct BlacklistCheck *next;
3262 * This is a linked list.
3264 struct BlacklistCheck *prev;
3267 * Peer being checked.
3269 struct GNUNET_PeerIdentity peer;
3272 * Option for setup neighbour afterwards.
3277 * Continuation to call with the result.
3279 SetupContinuation cont;
3287 * Current transmission request handle for this client, or NULL if no
3288 * request is pending.
3290 struct GNUNET_CONNECTION_TransmitHandle *th;
3293 * Our current position in the blacklisters list.
3295 struct Blacklisters *bl_pos;
3298 * Current task performing the check.
3300 GNUNET_SCHEDULER_TaskIdentifier task;
3305 * Head of DLL of active blacklisting queries.
3307 static struct BlacklistCheck *bc_head;
3310 * Tail of DLL of active blacklisting queries.
3312 static struct BlacklistCheck *bc_tail;
3316 * Perform next action in the blacklist check.
3318 * @param cls the 'struct BlacklistCheck*'
3322 do_blacklist_check (void *cls,
3323 const struct GNUNET_SCHEDULER_TaskContext *tc);
3326 * Transmit blacklist query to the client.
3328 * @param cls the 'struct BlacklistCheck'
3329 * @param size number of bytes allowed
3330 * @param buf where to copy the message
3331 * @return number of bytes copied to buf
3334 transmit_blacklist_message (void *cls,
3338 struct BlacklistCheck *bc = cls;
3339 struct Blacklisters *bl;
3340 struct BlacklistMessage bm;
3345 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
3346 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3348 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3349 "Failed to send blacklist test for peer `%s' to client\n",
3350 GNUNET_i2s (&bc->peer));
3354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3355 "Sending blacklist test for peer `%s' to client\n",
3356 GNUNET_i2s (&bc->peer));
3359 bm.header.size = htons (sizeof (struct BlacklistMessage));
3360 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
3361 bm.is_allowed = htonl (0);
3363 memcpy (buf, &bm, sizeof (bm));
3364 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
3370 * Perform next action in the blacklist check.
3372 * @param cls the 'struct BlacklistCheck*'
3376 do_blacklist_check (void *cls,
3377 const struct GNUNET_SCHEDULER_TaskContext *tc)
3379 struct BlacklistCheck *bc = cls;
3380 struct Blacklisters *bl;
3382 bc->task = GNUNET_SCHEDULER_NO_TASK;
3387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3388 "No blacklist clients active, will now setup neighbour record for peer `%s'\n",
3389 GNUNET_i2s (&bc->peer));
3391 bc->cont (bc->cont_cls,
3392 setup_new_neighbour (&bc->peer, bc->do_hello));
3399 bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
3400 sizeof (struct BlacklistMessage),
3401 GNUNET_TIME_UNIT_FOREVER_REL,
3402 &transmit_blacklist_message,
3409 * Obtain a 'struct NeighbourList' for the given peer. If such an entry
3410 * does not yet exist, check the blacklist. If the blacklist says creating
3411 * one is acceptable, create one and call the continuation; otherwise
3412 * call the continuation with NULL.
3414 * @param peer peer to setup or look up a struct NeighbourList for
3415 * @param do_hello should we also schedule sending our HELLO to the peer
3416 * if this is a new record
3417 * @param cont function to call with the 'struct NeigbhbourList*'
3418 * @param cont_cls closure for cont
3421 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
3423 SetupContinuation cont,
3426 struct NeighbourList *n;
3427 struct BlacklistCheck *bc;
3429 n = find_neighbour(peer);
3433 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3434 "Neighbour record exists for peer `%s'\n",
3441 if (bl_head == NULL)
3444 cont (cont_cls, setup_new_neighbour (peer, do_hello));
3446 setup_new_neighbour(peer, do_hello);
3449 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3450 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3452 bc->do_hello = do_hello;
3454 bc->cont_cls = cont_cls;
3455 bc->bl_pos = bl_head;
3456 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3462 * Function called with the result of querying a new blacklister about
3463 * it being allowed (or not) to continue to talk to an existing neighbour.
3465 * @param cls the original 'struct NeighbourList'
3466 * @param n NULL if we need to disconnect
3469 confirm_or_drop_neighbour (void *cls,
3470 struct NeighbourList *n)
3472 struct NeighbourList * orig = cls;
3477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3478 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&orig->id),
3479 "confirm_or_drop_neighboUr");
3481 GNUNET_STATISTICS_update (stats,
3482 gettext_noop ("# disconnects due to blacklist"),
3485 disconnect_neighbour (orig, GNUNET_NO);
3491 * Handle a request to start a blacklist.
3493 * @param cls closure (always NULL)
3494 * @param client identification of the client
3495 * @param message the actual message
3498 handle_blacklist_init (void *cls,
3499 struct GNUNET_SERVER_Client *client,
3500 const struct GNUNET_MessageHeader *message)
3502 struct Blacklisters *bl;
3503 struct BlacklistCheck *bc;
3504 struct NeighbourList *n;
3509 if (bl->client == client)
3512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3517 bl = GNUNET_malloc (sizeof (struct Blacklisters));
3518 bl->client = client;
3519 GNUNET_SERVER_client_keep (client);
3520 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
3521 /* confirm that all existing connections are OK! */
3525 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3526 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3528 bc->do_hello = GNUNET_NO;
3529 bc->cont = &confirm_or_drop_neighbour;
3532 if (n == neighbours) /* all would wait for the same client, no need to
3533 create more than just the first task right now */
3534 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3542 * Handle a request to blacklist a peer.
3544 * @param cls closure (always NULL)
3545 * @param client identification of the client
3546 * @param message the actual message
3549 handle_blacklist_reply (void *cls,
3550 struct GNUNET_SERVER_Client *client,
3551 const struct GNUNET_MessageHeader *message)
3553 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
3554 struct Blacklisters *bl;
3555 struct BlacklistCheck *bc;
3558 while ( (bl != NULL) &&
3559 (bl->client != client) )
3564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3565 "Blacklist client disconnected\n");
3567 /* FIXME: other error handling here!? */
3568 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3573 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
3576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3577 "Blacklist check failed, peer not allowed\n");
3579 bc->cont (bc->cont_cls, NULL);
3580 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
3586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3587 "Blacklist check succeeded, continuing with checks\n");
3589 bc->bl_pos = bc->bl_pos->next;
3590 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3593 /* check if any other bc's are waiting for this blacklister */
3597 if ( (bc->bl_pos == bl) &&
3598 (GNUNET_SCHEDULER_NO_TASK == bc->task) )
3599 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3607 * Send periodic PING messages to a given foreign address.
3609 * @param cls our 'struct PeriodicValidationContext*'
3610 * @param tc task context
3613 send_periodic_ping (void *cls,
3614 const struct GNUNET_SCHEDULER_TaskContext *tc)
3616 struct ForeignAddressList *peer_address = cls;
3617 struct TransportPlugin *tp;
3618 struct ValidationEntry *va;
3619 struct NeighbourList *neighbour;
3620 struct TransportPingMessage ping;
3621 struct CheckAddressExistsClosure caec;
3623 uint16_t hello_size;
3627 peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3628 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3630 tp = peer_address->ready_list->plugin;
3631 neighbour = peer_address->ready_list->neighbour;
3632 if (GNUNET_YES != neighbour->public_key_valid)
3634 /* no public key yet, try again later */
3635 schedule_next_ping (peer_address);
3638 caec.addr = peer_address->addr;
3639 caec.addrlen = peer_address->addrlen;
3640 caec.tname = tp->short_name;
3641 caec.session = peer_address->session;
3642 caec.exists = GNUNET_NO;
3643 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3644 &check_address_exists,
3646 if (caec.exists == GNUNET_YES)
3648 /* During validation attempts we will likely trigger the other
3649 peer trying to validate our address which in turn will cause
3650 it to send us its HELLO, so we expect to hit this case rather
3651 frequently. Only print something if we are very verbose. */
3652 #if DEBUG_TRANSPORT > 1
3653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3654 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3655 (peer_address->addr != NULL)
3656 ? a2s (tp->short_name,
3658 peer_address->addrlen)
3661 GNUNET_i2s (&neighbour->id));
3663 schedule_next_ping (peer_address);
3666 va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3667 va->transport_name = GNUNET_strdup (tp->short_name);
3668 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
3670 va->send_time = GNUNET_TIME_absolute_get();
3671 va->session = peer_address->session;
3672 if (peer_address->addr != NULL)
3674 va->addr = (const void*) &va[1];
3675 memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3676 va->addrlen = peer_address->addrlen;
3678 memcpy(&va->publicKey,
3679 &neighbour->publicKey,
3680 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3682 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
3683 &timeout_hello_validation,
3685 GNUNET_CONTAINER_multihashmap_put (validation_map,
3686 &neighbour->id.hashPubKey,
3688 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3690 if (peer_address->validated != GNUNET_YES)
3691 hello_size = GNUNET_HELLO_size(our_hello);
3695 tsize = sizeof(struct TransportPingMessage) + hello_size;
3697 if (peer_address->addr != NULL)
3699 slen = strlen (tp->short_name) + 1;
3700 tsize += slen + peer_address->addrlen;
3704 slen = 0; /* make gcc happy */
3706 message_buf = GNUNET_malloc(tsize);
3707 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3708 ping.challenge = htonl(va->challenge);
3709 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3710 if (peer_address->validated != GNUNET_YES)
3712 memcpy(message_buf, our_hello, hello_size);
3715 if (peer_address->addr != NULL)
3717 ping.header.size = htons(sizeof(struct TransportPingMessage) +
3718 peer_address->addrlen +
3720 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3723 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3725 peer_address->addrlen);
3729 ping.header.size = htons(sizeof(struct TransportPingMessage));
3732 memcpy(&message_buf[hello_size],
3734 sizeof(struct TransportPingMessage));
3736 #if DEBUG_TRANSPORT_REVALIDATION
3737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3738 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s'\n",
3739 (peer_address->addr != NULL)
3740 ? a2s (peer_address->plugin->short_name,
3742 peer_address->addrlen)
3745 GNUNET_i2s (&neighbour->id),
3746 "HELLO", hello_size,
3749 if (peer_address->validated != GNUNET_YES)
3750 GNUNET_STATISTICS_update (stats,
3751 gettext_noop ("# PING with HELLO messages sent"),
3755 GNUNET_STATISTICS_update (stats,
3756 gettext_noop ("# PING without HELLO messages sent"),
3759 GNUNET_STATISTICS_update (stats,
3760 gettext_noop ("# PING messages sent for re-validation"),
3763 transmit_to_peer (NULL, peer_address,
3764 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3765 HELLO_VERIFICATION_TIMEOUT,
3767 GNUNET_YES, neighbour);
3768 GNUNET_free(message_buf);
3769 schedule_next_ping (peer_address);
3774 * Schedule the job that will cause us to send a PING to the
3775 * foreign address to evaluate its validity and latency.
3777 * @param fal address to PING
3780 schedule_next_ping (struct ForeignAddressList *fal)
3782 struct GNUNET_TIME_Relative delay;
3784 if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3786 delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3787 delay.rel_value /= 2; /* do before expiration */
3788 delay = GNUNET_TIME_relative_min (delay,
3789 LATENCY_EVALUATION_MAX_DELAY);
3790 if (GNUNET_YES != fal->estimated)
3792 delay = GNUNET_TIME_UNIT_ZERO;
3793 fal->estimated = GNUNET_YES;
3795 if (GNUNET_YES == fal->connected)
3797 delay = GNUNET_TIME_relative_min (delay,
3798 CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3800 /* FIXME: also adjust delay based on how close the last
3801 observed latency is to the latency of the best alternative */
3802 /* bound how fast we can go */
3803 delay = GNUNET_TIME_relative_max (delay,
3804 GNUNET_TIME_UNIT_SECONDS);
3805 /* randomize a bit (to avoid doing all at the same time) */
3806 delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3807 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(delay,
3808 &send_periodic_ping,
3816 * Function that will be called if we receive some payload
3817 * from another peer.
3819 * @param message the payload
3820 * @param n peer who claimed to be the sender
3823 handle_payload_message (const struct GNUNET_MessageHeader *message,
3824 struct NeighbourList *n)
3826 struct InboundMessage *im;
3827 struct TransportClient *cpos;
3830 msize = ntohs (message->size);
3831 if (n->received_pong == GNUNET_NO)
3834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3835 "Received message of type %u and size %u from `%4s', but no pong yet!\n",
3836 ntohs (message->type),
3837 ntohs (message->size),
3838 GNUNET_i2s (&n->id));
3840 GNUNET_free_non_null (n->pre_connect_message_buffer);
3841 n->pre_connect_message_buffer = GNUNET_malloc (msize);
3842 memcpy (n->pre_connect_message_buffer, message, msize);
3847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3848 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
3849 ntohs (message->type),
3850 ntohs (message->size),
3851 GNUNET_i2s (&n->id));
3853 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3856 n->quota_violation_count++;
3858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3859 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
3860 n->in_tracker.available_bytes_per_s__,
3861 n->quota_violation_count);
3863 /* Discount 32k per violation */
3864 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3869 if (n->quota_violation_count > 0)
3871 /* try to add 32k back */
3872 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3874 n->quota_violation_count--;
3877 GNUNET_STATISTICS_update (stats,
3878 gettext_noop ("# payload received from other peers"),
3881 /* transmit message to all clients */
3882 uint32_t ats_count = 2;
3883 size_t size = sizeof (struct InboundMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) + msize;
3884 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
3887 im = GNUNET_malloc (size);
3888 im->header.size = htons (size);
3889 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3891 im->ats_count = htonl(ats_count);
3892 /* Setting ATS data */
3893 (&(im->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
3894 (&(im->ats))[0].value = htonl (n->distance);
3895 (&(im->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
3896 (&(im->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
3897 (&(im->ats))[ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
3898 (&(im->ats))[ats_count].value = htonl (0);
3900 memcpy (&((&(im->ats))[ats_count+1]), message, msize);
3902 while (cpos != NULL)
3904 transmit_to_client (cpos, &im->header, GNUNET_YES);
3912 * Iterator over hash map entries. Checks if the given validation
3913 * entry is for the same challenge as what is given in the PONG.
3915 * @param cls the 'struct TransportPongMessage*'
3916 * @param key peer identity
3917 * @param value value in the hash map ('struct ValidationEntry')
3918 * @return GNUNET_YES if we should continue to
3919 * iterate (mismatch), GNUNET_NO if not (entry matched)
3922 check_pending_validation (void *cls,
3923 const GNUNET_HashCode * key,
3926 const struct TransportPongMessage *pong = cls;
3927 struct ValidationEntry *ve = value;
3928 struct AddValidatedAddressContext avac;
3929 unsigned int challenge = ntohl(pong->challenge);
3930 struct GNUNET_HELLO_Message *hello;
3931 struct GNUNET_PeerIdentity target;
3932 struct NeighbourList *n;
3933 struct ForeignAddressList *fal;
3934 struct OwnAddressList *oal;
3935 struct TransportPlugin *tp;
3936 struct GNUNET_MessageHeader *prem;
3942 ps = ntohs (pong->header.size);
3943 if (ps < sizeof (struct TransportPongMessage))
3945 GNUNET_break_op (0);
3948 addr = (const char*) &pong[1];
3949 slen = strlen (ve->transport_name) + 1;
3950 if ( (ps - sizeof (struct TransportPongMessage) < slen) ||
3951 (ve->challenge != challenge) ||
3952 (addr[slen-1] != '\0') ||
3953 (0 != strcmp (addr, ve->transport_name)) ||
3954 (ntohl (pong->purpose.size)
3955 != sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3957 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
3958 sizeof (struct GNUNET_PeerIdentity) + ps - sizeof (struct TransportPongMessage)) )
3963 alen = ps - sizeof (struct TransportPongMessage) - slen;
3964 switch (ntohl (pong->purpose.purpose))
3966 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN:
3967 if ( (ve->addrlen + slen != ntohl (pong->addrlen)) ||
3968 (0 != memcmp (&addr[slen],
3972 return GNUNET_YES; /* different entry, keep trying! */
3974 if (0 != memcmp (&pong->pid,
3976 sizeof (struct GNUNET_PeerIdentity)))
3978 GNUNET_break_op (0);
3982 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
3987 GNUNET_break_op (0);
3992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3993 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
3995 a2s (ve->transport_name,
3996 (const struct sockaddr *) ve->addr,
3998 ve->transport_name);
4001 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING:
4002 if (0 != memcmp (&pong->pid,
4004 sizeof (struct GNUNET_PeerIdentity)))
4008 GNUNET_asprintf(&peer, "%s",GNUNET_i2s (&pong->pid));
4010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4011 "Received PONG for different identity: I am `%s', PONG identity: `%s'\n",
4012 GNUNET_i2s (&my_identity),
4018 if (ve->addrlen != 0)
4020 /* must have been for a different validation entry */
4023 tp = find_transport (ve->transport_name);
4029 oal = tp->addresses;
4032 if ( (oal->addrlen == alen) &&
4033 (0 == memcmp (&oal[1],
4041 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4042 _("Not accepting PONG from `%s' with address `%s' since I cannot confirm using this address.\n"),
4043 GNUNET_i2s (&pong->pid),
4044 a2s (ve->transport_name,
4047 /* FIXME: since the sender of the PONG currently uses the
4048 wrong address (see FIMXE there!), we cannot run a
4049 proper check here... */
4055 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING,
4060 GNUNET_break_op (0);
4065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4066 "Confirmed that peer `%4s' is talking to us using address `%s' (%s) for us.\n",
4068 a2s (ve->transport_name,
4071 ve->transport_name);
4075 GNUNET_break_op (0);
4078 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
4080 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4081 _("Received expired signature. Check system time.\n"));
4084 GNUNET_STATISTICS_update (stats,
4085 gettext_noop ("# address validation successes"),
4088 /* create the updated HELLO */
4089 GNUNET_CRYPTO_hash (&ve->publicKey,
4090 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4091 &target.hashPubKey);
4092 if (ve->addr != NULL)
4094 avac.done = GNUNET_NO;
4096 hello = GNUNET_HELLO_create (&ve->publicKey,
4097 &add_validated_address,
4099 GNUNET_PEERINFO_add_peer (peerinfo,
4101 GNUNET_free (hello);
4103 n = find_neighbour (&target);
4106 n->publicKey = ve->publicKey;
4107 n->public_key_valid = GNUNET_YES;
4108 fal = add_peer_address (n,
4113 GNUNET_assert (fal != NULL);
4114 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
4115 fal->validated = GNUNET_YES;
4116 mark_address_connected (fal);
4117 GNUNET_STATISTICS_update (stats,
4118 gettext_noop ("# peer addresses considered valid"),
4121 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
4122 update_addr_value (fal, GNUNET_TIME_absolute_get_duration (ve->send_time).rel_value, GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
4124 schedule_next_ping (fal);
4125 if (n->latency.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
4126 n->latency = fal->latency;
4128 n->latency.rel_value = (fal->latency.rel_value + n->latency.rel_value) / 2;
4130 n->distance = fal->distance;
4131 if (GNUNET_NO == n->received_pong)
4133 n->received_pong = GNUNET_YES;
4134 notify_clients_connect (&target, n->latency, n->distance);
4135 if (NULL != (prem = n->pre_connect_message_buffer))
4137 n->pre_connect_message_buffer = NULL;
4138 handle_payload_message (prem, n);
4142 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4144 GNUNET_SCHEDULER_cancel (n->retry_task);
4145 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4146 try_transmission_to_peer (n);
4150 /* clean up validation entry */
4151 GNUNET_assert (GNUNET_YES ==
4152 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4155 abort_validation (NULL, NULL, ve);
4161 * Function that will be called if we receive a validation
4162 * of an address challenge that we transmitted to another
4163 * peer. Note that the validation should only be considered
4164 * acceptable if the challenge matches AND if the sender
4165 * address is at least a plausible address for this peer
4166 * (otherwise we may be seeing a MiM attack).
4168 * @param cls closure
4169 * @param message the pong message
4170 * @param peer who responded to our challenge
4171 * @param sender_address string describing our sender address (as observed
4172 * by the other peer in binary format)
4173 * @param sender_address_len number of bytes in 'sender_address'
4176 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
4177 const struct GNUNET_PeerIdentity *peer,
4178 const char *sender_address,
4179 size_t sender_address_len)
4181 if (0 == memcmp (peer,
4183 sizeof (struct GNUNET_PeerIdentity)))
4185 /* PONG send to self, ignore */
4186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4187 "Receiving `%s' message from myself\n",
4191 #if DEBUG_TRANSPORT > 1
4192 /* we get tons of these that just get discarded, only log
4193 if we are quite verbose */
4194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4195 "Receiving `%s' message from `%4s'.\n", "PONG",
4198 GNUNET_STATISTICS_update (stats,
4199 gettext_noop ("# PONG messages received"),
4202 if (GNUNET_SYSERR !=
4203 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
4205 &check_pending_validation,
4208 /* This is *expected* to happen a lot since we send
4209 PONGs to *all* known addresses of the sender of
4210 the PING, so most likely we get multiple PONGs
4211 per PING, and all but the first PONG will end up
4212 here. So really we should not print anything here
4213 unless we want to be very, very verbose... */
4214 #if DEBUG_TRANSPORT > 2
4215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4216 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
4228 * Try to validate a neighbour's address by sending him our HELLO and a PING.
4230 * @param cls the 'struct ValidationEntry*'
4231 * @param neighbour neighbour to validate, NULL if validation failed
4234 transmit_hello_and_ping (void *cls,
4235 struct NeighbourList *neighbour)
4237 struct ValidationEntry *va = cls;
4238 struct ForeignAddressList *peer_address;
4239 struct TransportPingMessage ping;
4240 uint16_t hello_size;
4243 struct GNUNET_PeerIdentity id;
4246 GNUNET_CRYPTO_hash (&va->publicKey,
4247 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4249 if (neighbour == NULL)
4251 /* FIXME: stats... */
4252 GNUNET_break (GNUNET_OK ==
4253 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4256 abort_validation (NULL, NULL, va);
4259 neighbour->publicKey = va->publicKey;
4260 neighbour->public_key_valid = GNUNET_YES;
4261 peer_address = add_peer_address (neighbour,
4262 va->transport_name, NULL,
4263 (const void*) &va[1],
4265 if (peer_address == NULL)
4267 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4268 "Failed to add peer `%4s' for plugin `%s'\n",
4269 GNUNET_i2s (&neighbour->id),
4270 va->transport_name);
4271 GNUNET_break (GNUNET_OK ==
4272 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4275 abort_validation (NULL, NULL, va);
4278 if (NULL == our_hello)
4279 refresh_hello_task (NULL, NULL);
4280 hello_size = GNUNET_HELLO_size(our_hello);
4281 slen = strlen(va->transport_name) + 1;
4282 tsize = sizeof(struct TransportPingMessage) + hello_size + va->addrlen + slen;
4283 message_buf = GNUNET_malloc(tsize);
4284 ping.challenge = htonl(va->challenge);
4285 ping.header.size = htons(sizeof(struct TransportPingMessage) + slen + va->addrlen);
4286 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
4287 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
4288 memcpy(message_buf, our_hello, hello_size);
4289 memcpy(&message_buf[hello_size],
4291 sizeof(struct TransportPingMessage));
4292 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
4295 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
4299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4300 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
4303 : a2s (va->transport_name,
4304 (const void*) &va[1], va->addrlen),
4306 GNUNET_i2s (&neighbour->id),
4307 "HELLO", hello_size,
4308 "PING", sizeof (struct TransportPingMessage) + va->addrlen + slen);
4311 GNUNET_STATISTICS_update (stats,
4312 gettext_noop ("# PING messages sent for initial validation"),
4315 transmit_to_peer (NULL, peer_address,
4316 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
4317 HELLO_VERIFICATION_TIMEOUT,
4319 GNUNET_YES, neighbour);
4320 GNUNET_free(message_buf);
4325 * Check if the given address is already being validated; if not,
4326 * append the given address to the list of entries that are being be
4327 * validated and initiate validation.
4329 * @param cls closure ('struct CheckHelloValidatedContext *')
4330 * @param tname name of the transport
4331 * @param expiration expiration time
4332 * @param addr the address
4333 * @param addrlen length of the address
4334 * @return GNUNET_OK (always)
4337 run_validation (void *cls,
4339 struct GNUNET_TIME_Absolute expiration,
4343 struct CheckHelloValidatedContext *chvc = cls;
4344 struct GNUNET_PeerIdentity id;
4345 struct TransportPlugin *tp;
4346 struct ValidationEntry *va;
4347 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4348 struct CheckAddressExistsClosure caec;
4349 struct OwnAddressList *oal;
4351 GNUNET_assert (addr != NULL);
4353 GNUNET_STATISTICS_update (stats,
4354 gettext_noop ("# peer addresses scheduled for validation"),
4357 tp = find_transport (tname);
4360 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
4361 GNUNET_ERROR_TYPE_BULK,
4363 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
4365 GNUNET_STATISTICS_update (stats,
4366 gettext_noop ("# peer addresses not validated (plugin not available)"),
4371 /* check if this is one of our own addresses */
4372 oal = tp->addresses;
4375 if ( (oal->addrlen == addrlen) &&
4376 (0 == memcmp (&oal[1],
4380 /* not plausible, this address is equivalent to our own address! */
4381 GNUNET_STATISTICS_update (stats,
4382 gettext_noop ("# peer addresses not validated (loopback)"),
4389 GNUNET_HELLO_get_key (chvc->hello, &pk);
4390 GNUNET_CRYPTO_hash (&pk,
4392 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4395 if (is_blacklisted(&id, tp))
4398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4399 "Attempted to validate blacklisted peer `%s' using `%s'!\n",
4407 caec.addrlen = addrlen;
4408 caec.session = NULL;
4410 caec.exists = GNUNET_NO;
4411 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
4412 &check_address_exists,
4414 if (caec.exists == GNUNET_YES)
4416 /* During validation attempts we will likely trigger the other
4417 peer trying to validate our address which in turn will cause
4418 it to send us its HELLO, so we expect to hit this case rather
4419 frequently. Only print something if we are very verbose. */
4420 #if DEBUG_TRANSPORT > 1
4421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4422 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
4423 a2s (tname, addr, addrlen),
4427 GNUNET_STATISTICS_update (stats,
4428 gettext_noop ("# peer addresses not validated (in progress)"),
4433 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
4436 va->transport_name = GNUNET_strdup (tname);
4437 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
4439 va->send_time = GNUNET_TIME_absolute_get();
4440 va->addr = (const void*) &va[1];
4441 memcpy (&va[1], addr, addrlen);
4442 va->addrlen = addrlen;
4443 GNUNET_HELLO_get_key (chvc->hello,
4445 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
4446 &timeout_hello_validation,
4448 GNUNET_CONTAINER_multihashmap_put (validation_map,
4451 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4452 setup_peer_check_blacklist (&id, GNUNET_NO,
4453 &transmit_hello_and_ping,
4460 * Check if addresses in validated hello "h" overlap with
4461 * those in "chvc->hello" and validate the rest.
4463 * @param cls closure
4464 * @param peer id of the peer, NULL for last call
4465 * @param h hello message for the peer (can be NULL)
4466 * @param err_msg NULL if successful, otherwise contains error message
4469 check_hello_validated (void *cls,
4470 const struct GNUNET_PeerIdentity *peer,
4471 const struct GNUNET_HELLO_Message *h,
4472 const char *err_msg)
4474 struct CheckHelloValidatedContext *chvc = cls;
4475 struct GNUNET_HELLO_Message *plain_hello;
4476 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4477 struct GNUNET_PeerIdentity target;
4478 struct NeighbourList *n;
4480 if (err_msg != NULL)
4483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4484 _("Error in communication with PEERINFO service: %s\n"),
4492 GNUNET_STATISTICS_update (stats,
4493 gettext_noop ("# outstanding peerinfo iterate requests"),
4497 if (GNUNET_NO == chvc->hello_known)
4499 /* notify PEERINFO about the peer now, so that we at least
4500 have the public key if some other component needs it */
4501 GNUNET_HELLO_get_key (chvc->hello, &pk);
4502 GNUNET_CRYPTO_hash (&pk,
4503 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4504 &target.hashPubKey);
4505 plain_hello = GNUNET_HELLO_create (&pk,
4508 GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
4509 GNUNET_free (plain_hello);
4510 #if DEBUG_TRANSPORT_HELLO
4511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4512 "PEERINFO had no `%s' message for peer `%4s', full validation needed.\n",
4514 GNUNET_i2s (&target));
4516 GNUNET_STATISTICS_update (stats,
4517 gettext_noop ("# new HELLOs requiring full validation"),
4520 GNUNET_HELLO_iterate_addresses (chvc->hello,
4527 GNUNET_STATISTICS_update (stats,
4528 gettext_noop ("# duplicate HELLO (peer known)"),
4533 if (chvc->ve_count == 0)
4535 GNUNET_CONTAINER_DLL_remove (chvc_head,
4544 #if DEBUG_TRANSPORT_HELLO
4545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4546 "PEERINFO had `%s' message for peer `%4s', validating only new addresses.\n",
4550 chvc->hello_known = GNUNET_YES;
4551 n = find_neighbour (peer);
4554 #if DEBUG_TRANSPORT_HELLO
4555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4556 "Calling hello_iterate_addresses for %s!\n",
4559 GNUNET_HELLO_iterate_addresses (h,
4561 &add_to_foreign_address_list,
4563 try_transmission_to_peer (n);
4567 #if DEBUG_TRANSPORT_HELLO
4568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4569 "No existing neighbor record for %s!\n",
4572 GNUNET_STATISTICS_update (stats,
4573 gettext_noop ("# no existing neighbour record (validating HELLO)"),
4577 GNUNET_STATISTICS_update (stats,
4578 gettext_noop ("# HELLO validations (update case)"),
4581 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
4583 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
4590 * Process HELLO-message.
4592 * @param plugin transport involved, may be NULL
4593 * @param message the actual message
4594 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
4597 process_hello (struct TransportPlugin *plugin,
4598 const struct GNUNET_MessageHeader *message)
4601 struct GNUNET_PeerIdentity target;
4602 const struct GNUNET_HELLO_Message *hello;
4603 struct CheckHelloValidatedContext *chvc;
4604 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
4605 struct NeighbourList *n;
4606 #if DEBUG_TRANSPORT_HELLO > 2
4610 hsize = ntohs (message->size);
4611 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
4612 (hsize < sizeof (struct GNUNET_MessageHeader)))
4615 return GNUNET_SYSERR;
4617 GNUNET_STATISTICS_update (stats,
4618 gettext_noop ("# HELLOs received for validation"),
4622 hello = (const struct GNUNET_HELLO_Message *) message;
4623 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
4625 #if DEBUG_TRANSPORT_HELLO
4626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4627 "Unable to get public key from `%s' for `%4s'!\n",
4629 GNUNET_i2s (&target));
4631 GNUNET_break_op (0);
4632 return GNUNET_SYSERR;
4634 GNUNET_CRYPTO_hash (&publicKey,
4635 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4636 &target.hashPubKey);
4638 #if DEBUG_TRANSPORT_HELLO
4639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4640 "Received `%s' message for `%4s'\n",
4642 GNUNET_i2s (&target));
4644 if (0 == memcmp (&my_identity,
4646 sizeof (struct GNUNET_PeerIdentity)))
4648 GNUNET_STATISTICS_update (stats,
4649 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
4654 n = find_neighbour (&target);
4656 (! n->public_key_valid) )
4658 GNUNET_HELLO_get_key (hello, &n->publicKey);
4659 n->public_key_valid = GNUNET_YES;
4662 /* check if load is too high before doing expensive stuff */
4663 if (GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
4665 GNUNET_STATISTICS_update (stats,
4666 gettext_noop ("# HELLOs ignored due to high load"),
4669 #if DEBUG_TRANSPORT_HELLO
4670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4671 "Ignoring `%s' for `%4s', load too high.\n",
4673 GNUNET_i2s (&target));
4680 while (NULL != chvc)
4682 if (GNUNET_HELLO_equals (hello,
4684 GNUNET_TIME_absolute_get ()).abs_value > 0)
4686 #if DEBUG_TRANSPORT_HELLO > 2
4687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4688 "Received duplicate `%s' message for `%4s'; ignored\n",
4690 GNUNET_i2s (&target));
4692 return GNUNET_OK; /* validation already pending */
4694 if (GNUNET_HELLO_size (hello) == GNUNET_HELLO_size (chvc->hello))
4695 GNUNET_break (0 != memcmp (hello, chvc->hello,
4696 GNUNET_HELLO_size(hello)));
4701 struct NeighbourList *temp_neighbor = find_neighbour(&target);
4702 if ((NULL != temp_neighbor))
4704 fprintf(stderr, "Already know peer, ignoring hello\n");
4709 #if DEBUG_TRANSPORT_HELLO > 2
4712 my_id = GNUNET_strdup(GNUNET_i2s(plugin->env.my_identity));
4714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4715 "%s: Starting validation of `%s' message for `%4s' via '%s' of size %u\n",
4718 GNUNET_i2s (&target),
4720 GNUNET_HELLO_size(hello));
4725 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
4727 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
4728 memcpy (&chvc[1], hello, hsize);
4729 GNUNET_CONTAINER_DLL_insert (chvc_head,
4732 /* finally, check if HELLO was previously validated
4733 (continuation will then schedule actual validation) */
4734 GNUNET_STATISTICS_update (stats,
4735 gettext_noop ("# peerinfo process hello iterate requests"),
4738 GNUNET_STATISTICS_update (stats,
4739 gettext_noop ("# outstanding peerinfo iterate requests"),
4742 chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
4744 HELLO_VERIFICATION_TIMEOUT,
4745 &check_hello_validated, chvc);
4751 * The peer specified by the given neighbour has timed-out or a plugin
4752 * has disconnected. We may either need to do nothing (other plugins
4753 * still up), or trigger a full disconnect and clean up. This
4754 * function updates our state and does the necessary notifications.
4755 * Also notifies our clients that the neighbour is now officially
4758 * @param n the neighbour list entry for the peer
4759 * @param check GNUNET_YES to check if ALL addresses for this peer
4760 * are gone, GNUNET_NO to force a disconnect of the peer
4761 * regardless of whether other addresses exist.
4764 disconnect_neighbour (struct NeighbourList *n, int check)
4766 struct ReadyList *rpos;
4767 struct NeighbourList *npos;
4768 struct NeighbourList *nprev;
4769 struct MessageQueue *mq;
4770 struct ForeignAddressList *peer_addresses;
4771 struct ForeignAddressList *peer_pos;
4773 if (GNUNET_YES == check)
4776 while (NULL != rpos)
4778 peer_addresses = rpos->addresses;
4779 while (peer_addresses != NULL)
4781 // Do not disconnect if: an address is connected or an inbound address exists
4782 if ((GNUNET_YES == peer_addresses->connected) || (peer_addresses->addrlen == 0))
4785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4786 "NOT Disconnecting from `%4s', still have live address `%s'!\n",
4787 GNUNET_i2s (&n->id),
4788 a2s (peer_addresses->ready_list->plugin->short_name,
4789 peer_addresses->addr,
4790 peer_addresses->addrlen));
4792 return; /* still connected */
4794 peer_addresses = peer_addresses->next;
4800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4801 "Disconnecting from `%4s'\n",
4802 GNUNET_i2s (&n->id));
4805 /* remove n from neighbours list */
4808 while ((npos != NULL) && (npos != n))
4813 GNUNET_assert (npos != NULL);
4815 neighbours = n->next;
4817 nprev->next = n->next;
4819 /* notify all clients about disconnect */
4820 if (GNUNET_YES == n->received_pong)
4821 notify_clients_disconnect (&n->id);
4823 ats_modify_problem_state(ats, ATS_QUALITY_COST_UPDATED);
4825 /* clean up all plugins, cancel connections and pending transmissions */
4826 while (NULL != (rpos = n->plugins))
4828 n->plugins = rpos->next;
4829 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
4830 while (rpos->addresses != NULL)
4832 peer_pos = rpos->addresses;
4833 rpos->addresses = peer_pos->next;
4834 if (peer_pos->connected == GNUNET_YES)
4835 GNUNET_STATISTICS_update (stats,
4836 gettext_noop ("# connected addresses"),
4839 if (GNUNET_YES == peer_pos->validated)
4840 GNUNET_STATISTICS_update (stats,
4841 gettext_noop ("# peer addresses considered valid"),
4844 if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
4846 GNUNET_SCHEDULER_cancel (peer_pos->revalidate_task);
4847 peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
4849 GNUNET_free(peer_pos->ressources);
4850 peer_pos->ressources = NULL;
4851 GNUNET_free(peer_pos->quality);
4852 peer_pos->ressources = NULL;
4853 GNUNET_free(peer_pos);
4858 /* free all messages on the queue */
4859 while (NULL != (mq = n->messages_head))
4861 GNUNET_STATISTICS_update (stats,
4862 gettext_noop ("# bytes in message queue for other peers"),
4863 - (int64_t) mq->message_buf_size,
4865 GNUNET_STATISTICS_update (stats,
4866 gettext_noop ("# bytes discarded due to disconnect"),
4867 mq->message_buf_size,
4869 GNUNET_CONTAINER_DLL_remove (n->messages_head,
4872 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4874 sizeof(struct GNUNET_PeerIdentity)));
4878 while (NULL != (mq = n->cont_head))
4881 GNUNET_CONTAINER_DLL_remove (n->cont_head,
4884 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4886 sizeof(struct GNUNET_PeerIdentity)));
4890 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
4892 GNUNET_SCHEDULER_cancel (n->timeout_task);
4893 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4895 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4897 GNUNET_SCHEDULER_cancel (n->retry_task);
4898 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4900 if (n->piter != NULL)
4902 GNUNET_PEERINFO_iterate_cancel (n->piter);
4903 GNUNET_STATISTICS_update (stats,
4904 gettext_noop ("# outstanding peerinfo iterate requests"),
4909 /* finally, free n itself */
4910 GNUNET_STATISTICS_update (stats,
4911 gettext_noop ("# active neighbours"),
4914 GNUNET_free_non_null (n->pre_connect_message_buffer);
4920 * We have received a PING message from someone. Need to send a PONG message
4921 * in response to the peer by any means necessary.
4924 handle_ping (void *cls, const struct GNUNET_MessageHeader *message,
4925 const struct GNUNET_PeerIdentity *peer,
4926 struct Session *session,
4927 const char *sender_address,
4928 uint16_t sender_address_len)
4930 struct TransportPlugin *plugin = cls;
4931 struct SessionHeader *session_header = (struct SessionHeader*) session;
4932 struct TransportPingMessage *ping;
4933 struct TransportPongMessage *pong;
4934 struct NeighbourList *n;
4935 struct ReadyList *rl;
4936 struct ForeignAddressList *fal;
4937 struct OwnAddressList *oal;
4943 if (ntohs (message->size) < sizeof (struct TransportPingMessage))
4945 GNUNET_break_op (0);
4946 return GNUNET_SYSERR;
4949 ping = (struct TransportPingMessage *) message;
4950 if (0 != memcmp (&ping->target,
4951 plugin->env.my_identity,
4952 sizeof (struct GNUNET_PeerIdentity)))
4955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4956 _("Received `%s' message from `%s' destined for `%s' which is not me!\n"),
4958 (sender_address != NULL)
4959 ? a2s (plugin->short_name,
4960 (const struct sockaddr *)sender_address,
4963 GNUNET_i2s (&ping->target));
4965 return GNUNET_SYSERR;
4968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4969 "Processing `%s' from `%s'\n",
4971 (sender_address != NULL)
4972 ? a2s (plugin->short_name,
4973 (const struct sockaddr *)sender_address,
4977 GNUNET_STATISTICS_update (stats,
4978 gettext_noop ("# PING messages received"),
4981 addr = (const char*) &ping[1];
4982 alen = ntohs (message->size) - sizeof (struct TransportPingMessage);
4983 slen = strlen (plugin->short_name) + 1;
4986 /* peer wants to confirm that we have an outbound connection to him */
4987 if (session == NULL)
4989 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4990 _("Refusing to create PONG since I do not have a session with `%s'.\n"),
4992 return GNUNET_SYSERR;
4994 /* FIXME-urg: the use of 'sender_address' in the code below is doubly-wrong:
4995 1) it is NULL when we need to have a real value
4996 2) it is documented to be the address of the sender (source-IP), where
4997 what we actually want is our LISTEN IP (what we 'bound' to); which we don't even
5000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5001 "Creating PONG indicating that we received a connection at our address `%s' from `%s'.\n",
5002 a2s (plugin->short_name,
5004 sender_address_len),
5007 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len + slen);
5008 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len + slen);
5009 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
5010 pong->purpose.size =
5011 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
5013 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
5014 sizeof (struct GNUNET_PeerIdentity) + sender_address_len + slen);
5015 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING);
5016 pong->challenge = ping->challenge;
5017 pong->addrlen = htonl(sender_address_len + slen);
5020 sizeof(struct GNUNET_PeerIdentity));
5024 if ((sender_address!=NULL) && (sender_address_len > 0))
5025 memcpy (&((char*)&pong[1])[slen],
5027 sender_address_len);
5028 if (GNUNET_TIME_absolute_get_remaining (session_header->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
5030 /* create / update cached sig */
5032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5033 "Creating PONG signature to indicate active connection.\n");
5035 session_header->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
5036 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
5037 GNUNET_assert (GNUNET_OK ==
5038 GNUNET_CRYPTO_rsa_sign (my_private_key,
5040 &session_header->pong_signature));
5044 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
5046 memcpy (&pong->signature,
5047 &session_header->pong_signature,
5048 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5054 /* peer wants to confirm that this is one of our addresses */
5058 plugin->api->check_address (plugin->api->cls,
5062 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5063 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
5064 a2s (plugin->short_name,
5069 oal = plugin->addresses;
5072 if ( (oal->addrlen == alen) &&
5079 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
5080 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
5081 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
5082 pong->purpose.size =
5083 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
5085 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
5086 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
5087 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
5088 pong->challenge = ping->challenge;
5089 pong->addrlen = htonl(alen + slen);
5092 sizeof(struct GNUNET_PeerIdentity));
5093 memcpy (&pong[1], plugin->short_name, slen);
5094 memcpy (&((char*)&pong[1])[slen], addr, alen);
5095 if ( (oal != NULL) &&
5096 (GNUNET_TIME_absolute_get_remaining (oal->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4) )
5098 /* create / update cached sig */
5100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5101 "Creating PONG signature to indicate ownership.\n");
5103 oal->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
5104 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5105 GNUNET_assert (GNUNET_OK ==
5106 GNUNET_CRYPTO_rsa_sign (my_private_key,
5108 &oal->pong_signature));
5109 memcpy (&pong->signature,
5110 &oal->pong_signature,
5111 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5113 else if (oal == NULL)
5115 /* not using cache (typically DV-only) */
5116 pong->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
5117 GNUNET_assert (GNUNET_OK ==
5118 GNUNET_CRYPTO_rsa_sign (my_private_key,
5124 /* can used cached version */
5125 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5126 memcpy (&pong->signature,
5127 &oal->pong_signature,
5128 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5131 n = find_neighbour(peer);
5132 GNUNET_assert (n != NULL);
5133 did_pong = GNUNET_NO;
5134 /* first try reliable response transmission */
5138 fal = rl->addresses;
5141 if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
5144 ntohs (pong->header.size),
5145 TRANSPORT_PONG_PRIORITY,
5146 HELLO_VERIFICATION_TIMEOUT,
5153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5154 "Transmitted PONG to `%s' via reliable mechanism\n",
5157 GNUNET_STATISTICS_update (stats,
5158 gettext_noop ("# PONGs unicast via reliable transport"),
5164 did_pong = GNUNET_YES;
5169 /* no reliable method found, do multicast */
5170 GNUNET_STATISTICS_update (stats,
5171 gettext_noop ("# PONGs multicast to all available addresses"),
5177 fal = rl->addresses;
5180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5181 "Transmitting PONG to `%s' via unreliable mechanism `%s':%s\n",
5183 a2s (rl->plugin->short_name,
5186 rl->plugin->short_name);
5187 transmit_to_peer(NULL, fal,
5188 TRANSPORT_PONG_PRIORITY,
5189 HELLO_VERIFICATION_TIMEOUT,
5191 ntohs(pong->header.size),
5194 did_pong = GNUNET_YES;
5200 if (GNUNET_YES != did_pong)
5201 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5202 _("Could not send PONG to `%s': no address available\n"),
5209 * Function called by the plugin for each received message. Update
5210 * data volumes, possibly notify plugins about reducing the rate at
5211 * which they read from the socket and generally forward to our
5214 * @param cls the "struct TransportPlugin *" we gave to the plugin
5215 * @param peer (claimed) identity of the other peer
5216 * @param message the message, NULL if we only care about
5217 * learning about the delay until we should receive again
5218 * @param ats_data information for automatic transport selection
5219 * @param ats_count number of elements in ats not including 0-terminator
5220 * @param session identifier used for this session (can be NULL)
5221 * @param sender_address binary address of the sender (if observed)
5222 * @param sender_address_len number of bytes in sender_address
5223 * @return how long in ms the plugin should wait until receiving more data
5224 * (plugins that do not support this, can ignore the return value)
5226 static struct GNUNET_TIME_Relative
5227 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
5228 const struct GNUNET_MessageHeader *message,
5229 const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
5231 struct Session *session,
5232 const char *sender_address,
5233 uint16_t sender_address_len)
5235 struct TransportPlugin *plugin = cls;
5236 struct ReadyList *service_context;
5237 struct ForeignAddressList *peer_address;
5239 struct NeighbourList *n;
5240 struct GNUNET_TIME_Relative ret;
5244 if (0 == memcmp (peer,
5246 sizeof (struct GNUNET_PeerIdentity)))
5248 /* refuse to receive from myself */
5250 return GNUNET_TIME_UNIT_FOREVER_REL;
5252 if (is_blacklisted (peer, plugin))
5253 return GNUNET_TIME_UNIT_FOREVER_REL;
5254 n = find_neighbour (peer);
5256 n = setup_new_neighbour (peer, GNUNET_YES);
5257 service_context = n->plugins;
5258 while ((service_context != NULL) && (plugin != service_context->plugin))
5259 service_context = service_context->next;
5260 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
5261 peer_address = NULL;
5264 for (c=0; c<ats_count; c++)
5265 if (ntohl(ats_data[c].type) == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
5266 distance = ntohl(ats_data[c].value);
5269 if (message != NULL)
5271 if ( (session != NULL) ||
5272 (sender_address != NULL) )
5273 peer_address = add_peer_address (n,
5277 sender_address_len);
5278 if (peer_address != NULL)
5280 update_addr_ats(peer_address, ats_data, ats_count);
5281 update_addr_value(peer_address, distance, GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5283 peer_address->distance = distance;
5284 if (GNUNET_YES == peer_address->validated)
5285 mark_address_connected (peer_address);
5286 peer_address->timeout
5287 = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5288 schedule_next_ping (peer_address);
5290 /* update traffic received amount ... */
5291 msize = ntohs (message->size);
5293 GNUNET_STATISTICS_update (stats,
5294 gettext_noop ("# bytes received from other peers"),
5297 n->distance = distance;
5299 GNUNET_TIME_relative_to_absolute
5300 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5301 GNUNET_SCHEDULER_cancel (n->timeout_task);
5303 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
5304 &neighbour_timeout_task, n);
5305 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
5307 /* dropping message due to frequent inbound volume violations! */
5308 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
5309 GNUNET_ERROR_TYPE_BULK,
5311 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
5312 n->in_tracker.available_bytes_per_s__,
5313 n->quota_violation_count);
5314 GNUNET_STATISTICS_update (stats,
5315 gettext_noop ("# bandwidth quota violations by other peers"),
5318 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
5320 if ((ntohs(message->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ATS) &&
5321 (ntohs(message->size) == (sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t))))
5323 uint32_t value = ntohl(*((uint32_t *) &message[1]));
5324 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GNUNET_MESSAGE_TYPE_TRANSPORT_ATS: %i \n", value);
5325 /* Force ressource and quality update */
5326 if ((value == 4) && (ats != NULL))
5327 ats_modify_problem_state(ats, ATS_QUALITY_COST_UPDATED);
5328 /* Force cost update */
5329 if ((value == 3) && (ats != NULL))
5330 ats_modify_problem_state(ats, ATS_COST_UPDATED);
5331 /* Force quality update */
5332 if ((value == 2) && (ats != NULL))
5333 ats_modify_problem_state(ats, ATS_QUALITY_UPDATED);
5334 /* Force full rebuild */
5335 if ((value == 1) && (ats != NULL))
5336 ats_modify_problem_state(ats, ATS_MODIFIED);
5340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5341 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
5342 ntohs (message->type),
5343 ntohs (message->size),
5346 switch (ntohs (message->type))
5348 case GNUNET_MESSAGE_TYPE_HELLO:
5349 GNUNET_STATISTICS_update (stats,
5350 gettext_noop ("# HELLO messages received from other peers"),
5353 process_hello (plugin, message);
5355 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
5356 handle_ping (plugin, message, peer, session, sender_address, sender_address_len);
5357 if (! n->received_pong)
5358 transmit_plain_ping (n);
5360 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
5361 handle_pong (plugin, message, peer, sender_address, sender_address_len);
5363 case GNUNET_MESSAGE_TYPE_TRANSPORT_ATS:
5366 handle_payload_message (message, n);
5370 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
5371 if (ret.rel_value > 0)
5374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5375 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
5376 (unsigned long long) n->in_tracker.consumption_since_last_update__,
5377 (unsigned int) n->in_tracker.available_bytes_per_s__,
5378 (unsigned long long) ret.rel_value);
5380 GNUNET_STATISTICS_update (stats,
5381 gettext_noop ("# ms throttling suggested"),
5382 (int64_t) ret.rel_value,
5389 * Handle START-message. This is the first message sent to us
5390 * by any client which causes us to add it to our list.
5392 * @param cls closure (always NULL)
5393 * @param client identification of the client
5394 * @param message the actual message
5397 handle_start (void *cls,
5398 struct GNUNET_SERVER_Client *client,
5399 const struct GNUNET_MessageHeader *message)
5401 const struct StartMessage *start;
5402 struct TransportClient *c;
5403 struct ConnectInfoMessage * cim;
5404 struct NeighbourList *n;
5408 start = (const struct StartMessage*) message;
5410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5411 "Received `%s' request from client\n", "START");
5416 if (c->client == client)
5418 /* client already on our list! */
5420 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5425 if ( (GNUNET_NO != ntohl (start->do_check)) &&
5426 (0 != memcmp (&start->self,
5428 sizeof (struct GNUNET_PeerIdentity))) )
5430 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5431 _("Rejecting control connection from peer `%s', which is not me!\n"),
5432 GNUNET_i2s (&start->self));
5433 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5436 c = GNUNET_malloc (sizeof (struct TransportClient));
5440 if (our_hello != NULL)
5443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5444 "Sending our own `%s' to new client\n", "HELLO");
5446 transmit_to_client (c,
5447 (const struct GNUNET_MessageHeader *) our_hello,
5449 /* tell new client about all existing connections */
5451 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
5452 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
5456 cim = GNUNET_malloc (size);
5457 cim->header.size = htons (size);
5458 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
5459 cim->ats_count = htonl(ats_count);
5460 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
5461 (&(cim->ats))[2].value = htonl (0);
5465 if (GNUNET_YES == n->received_pong)
5467 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5468 (&(cim->ats))[0].value = htonl (n->distance);
5469 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
5470 (&(cim->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
5472 transmit_to_client (c, &cim->header, GNUNET_NO);
5480 #if DEBUG_TRANSPORT_HELLO
5481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5482 "No HELLO created yet, will transmit HELLO to client later!\n");
5486 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5491 * Handle HELLO-message.
5493 * @param cls closure (always NULL)
5494 * @param client identification of the client
5495 * @param message the actual message
5498 handle_hello (void *cls,
5499 struct GNUNET_SERVER_Client *client,
5500 const struct GNUNET_MessageHeader *message)
5504 GNUNET_STATISTICS_update (stats,
5505 gettext_noop ("# HELLOs received from clients"),
5508 ret = process_hello (NULL, message);
5509 GNUNET_SERVER_receive_done (client, ret);
5514 * Closure for 'transmit_client_message'; followed by
5515 * 'msize' bytes of the actual message.
5517 struct TransmitClientMessageContext
5520 * Client on whom's behalf we are sending.
5522 struct GNUNET_SERVER_Client *client;
5525 * Timeout for the transmission.
5527 struct GNUNET_TIME_Absolute timeout;
5535 * Size of the message in bytes.
5542 * Schedule transmission of a message we got from a client to a peer.
5544 * @param cls the 'struct TransmitClientMessageContext*'
5545 * @param n destination, or NULL on error (in that case, drop the message)
5548 transmit_client_message (void *cls,
5549 struct NeighbourList *n)
5551 struct TransmitClientMessageContext *tcmc = cls;
5552 struct TransportClient *tc;
5555 while ((tc != NULL) && (tc->client != tcmc->client))
5560 transmit_to_peer (tc, NULL, tcmc->priority,
5561 GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
5563 tcmc->msize, GNUNET_NO, n);
5565 GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
5566 GNUNET_SERVER_client_drop (tcmc->client);
5572 * Handle SEND-message.
5574 * @param cls closure (always NULL)
5575 * @param client identification of the client
5576 * @param message the actual message
5579 handle_send (void *cls,
5580 struct GNUNET_SERVER_Client *client,
5581 const struct GNUNET_MessageHeader *message)
5583 const struct OutboundMessage *obm;
5584 const struct GNUNET_MessageHeader *obmm;
5585 struct TransmitClientMessageContext *tcmc;
5589 size = ntohs (message->size);
5591 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
5594 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5597 GNUNET_STATISTICS_update (stats,
5598 gettext_noop ("# payload received for other peers"),
5601 obm = (const struct OutboundMessage *) message;
5602 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
5603 msize = size - sizeof (struct OutboundMessage);
5605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5606 "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
5607 "SEND", GNUNET_i2s (&obm->peer),
5611 tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
5612 tcmc->client = client;
5613 tcmc->priority = ntohl (obm->priority);
5614 tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
5615 tcmc->msize = msize;
5616 /* FIXME: this memcpy can be up to 7% of our total runtime */
5617 memcpy (&tcmc[1], obmm, msize);
5618 GNUNET_SERVER_client_keep (client);
5619 setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
5620 &transmit_client_message,
5626 * Handle request connect message
5628 * @param cls closure (always NULL)
5629 * @param client identification of the client
5630 * @param message the actual message
5633 handle_request_connect (void *cls,
5634 struct GNUNET_SERVER_Client *client,
5635 const struct GNUNET_MessageHeader *message)
5637 const struct TransportRequestConnectMessage *trcm =
5638 (const struct TransportRequestConnectMessage *) message;
5640 GNUNET_STATISTICS_update (stats,
5641 gettext_noop ("# REQUEST CONNECT messages received"),
5645 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
5646 "Received a request connect message for peer `%s'\n",
5647 GNUNET_i2s(&trcm->peer));
5649 setup_peer_check_blacklist (&trcm->peer, GNUNET_YES,
5651 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5656 * Handle SET_QUOTA-message.
5658 * @param cls closure (always NULL)
5659 * @param client identification of the client
5660 * @param message the actual message
5663 handle_set_quota (void *cls,
5664 struct GNUNET_SERVER_Client *client,
5665 const struct GNUNET_MessageHeader *message)
5667 const struct QuotaSetMessage *qsm =
5668 (const struct QuotaSetMessage *) message;
5669 struct NeighbourList *n;
5671 GNUNET_STATISTICS_update (stats,
5672 gettext_noop ("# SET QUOTA messages received"),
5675 n = find_neighbour (&qsm->peer);
5678 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5679 GNUNET_STATISTICS_update (stats,
5680 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
5686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5687 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
5689 (unsigned int) ntohl (qsm->quota.value__),
5690 (unsigned int) n->in_tracker.available_bytes_per_s__,
5691 GNUNET_i2s (&qsm->peer));
5693 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
5695 if (0 == ntohl (qsm->quota.value__))
5698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5699 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&n->id),
5702 GNUNET_STATISTICS_update (stats,
5703 gettext_noop ("# disconnects due to quota of 0"),
5706 disconnect_neighbour (n, GNUNET_NO);
5708 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5713 * Take the given address and append it to the set of results sent back to
5716 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
5717 * @param address the resolved name, NULL to indicate the last response
5720 transmit_address_to_client (void *cls, const char *address)
5722 struct GNUNET_SERVER_TransmitContext *tc = cls;
5725 if (NULL != address)
5727 slen = strlen (address) + 1;
5728 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
5729 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5733 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
5739 * Handle AddressLookup-message.
5741 * @param cls closure (always NULL)
5742 * @param client identification of the client
5743 * @param message the actual message
5746 handle_address_lookup (void *cls,
5747 struct GNUNET_SERVER_Client *client,
5748 const struct GNUNET_MessageHeader *message)
5750 const struct AddressLookupMessage *alum;
5751 struct TransportPlugin *lsPlugin;
5752 const char *nameTransport;
5753 const char *address;
5755 struct GNUNET_SERVER_TransmitContext *tc;
5756 struct GNUNET_TIME_Absolute timeout;
5757 struct GNUNET_TIME_Relative rtimeout;
5760 size = ntohs (message->size);
5761 if (size < sizeof (struct AddressLookupMessage))
5763 GNUNET_break_op (0);
5764 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5767 alum = (const struct AddressLookupMessage *) message;
5768 uint32_t addressLen = ntohl (alum->addrlen);
5769 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
5771 GNUNET_break_op (0);
5772 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5775 address = (const char *) &alum[1];
5776 nameTransport = (const char *) &address[addressLen];
5778 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
5780 GNUNET_break_op (0);
5781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5784 timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
5785 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5786 numeric = ntohl (alum->numeric_only);
5787 lsPlugin = find_transport (nameTransport);
5788 if (NULL == lsPlugin)
5790 tc = GNUNET_SERVER_transmit_context_create (client);
5791 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5792 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5793 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
5796 GNUNET_SERVER_disable_receive_done_warning (client);
5797 tc = GNUNET_SERVER_transmit_context_create (client);
5798 lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
5800 address, addressLen,
5803 &transmit_address_to_client, tc);
5808 * Setup the environment for this plugin.
5811 create_environment (struct TransportPlugin *plug)
5813 plug->env.cfg = cfg;
5814 plug->env.my_identity = &my_identity;
5815 plug->env.our_hello = &our_hello;
5816 plug->env.cls = plug;
5817 plug->env.receive = &plugin_env_receive;
5818 plug->env.notify_address = &plugin_env_notify_address;
5819 plug->env.session_end = &plugin_env_session_end;
5820 plug->env.max_connections = max_connect_per_transport;
5821 plug->env.stats = stats;
5826 * Start the specified transport (load the plugin).
5829 start_transport (struct GNUNET_SERVER_Handle *server,
5832 struct TransportPlugin *plug;
5835 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5836 _("Loading `%s' transport plugin\n"), name);
5837 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
5838 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
5839 create_environment (plug);
5840 plug->short_name = GNUNET_strdup (name);
5841 plug->lib_name = libname;
5842 plug->next = plugins;
5844 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
5845 if (plug->api == NULL)
5847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5848 _("Failed to load transport plugin for `%s'\n"), name);
5849 GNUNET_free (plug->short_name);
5850 plugins = plug->next;
5851 GNUNET_free (libname);
5858 * Called whenever a client is disconnected. Frees our
5859 * resources associated with that client.
5861 * @param cls closure
5862 * @param client identification of the client
5865 client_disconnect_notification (void *cls,
5866 struct GNUNET_SERVER_Client *client)
5868 struct TransportClient *pos;
5869 struct TransportClient *prev;
5870 struct ClientMessageQueueEntry *mqe;
5871 struct Blacklisters *bl;
5872 struct BlacklistCheck *bc;
5877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
5878 "Client disconnected, cleaning up.\n");
5880 /* clean up blacklister */
5884 if (bl->client == client)
5889 if (bc->bl_pos == bl)
5891 bc->bl_pos = bl->next;
5894 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
5897 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
5898 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
5904 GNUNET_CONTAINER_DLL_remove (bl_head,
5907 GNUNET_SERVER_client_drop (bl->client);
5913 /* clean up 'normal' clients */
5916 while ((pos != NULL) && (pos->client != client))
5923 while (NULL != (mqe = pos->message_queue_head))
5925 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
5926 pos->message_queue_tail,
5928 pos->message_count--;
5932 clients = pos->next;
5934 prev->next = pos->next;
5935 if (GNUNET_YES == pos->tcs_pending)
5940 if (pos->th != NULL)
5942 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
5945 GNUNET_break (0 == pos->message_count);
5951 * Function called when the service shuts down. Unloads our plugins
5952 * and cancels pending validations.
5954 * @param cls closure, unused
5955 * @param tc task context (unused)
5958 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5960 struct TransportPlugin *plug;
5961 struct TransportPlugin *tmp;
5962 struct OwnAddressList *al;
5963 struct CheckHelloValidatedContext *chvc;
5965 shutdown_in_progress = GNUNET_YES;
5966 while (neighbours != NULL)
5969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5970 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&neighbours->id),
5973 disconnect_neighbour (neighbours, GNUNET_NO);
5976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5977 "Transport service is unloading plugins...\n");
5980 while (plug != NULL)
5982 if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
5984 GNUNET_SCHEDULER_cancel (plug->address_update_task);
5985 plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
5987 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
5988 GNUNET_free (plug->lib_name);
5989 GNUNET_free (plug->short_name);
5990 while (NULL != (al = plug->addresses))
5992 plug->addresses = al->next;
5999 if (my_private_key != NULL)
6000 GNUNET_CRYPTO_rsa_key_free (my_private_key);
6001 GNUNET_free_non_null (our_hello);
6003 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
6006 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6007 validation_map = NULL;
6009 if (ats_task != GNUNET_SCHEDULER_NO_TASK)
6011 GNUNET_SCHEDULER_cancel(ats_task);
6012 ats_task = GNUNET_SCHEDULER_NO_TASK;
6019 /* free 'chvc' data structure */
6020 while (NULL != (chvc = chvc_head))
6022 chvc_head = chvc->next;
6023 if (chvc->piter != NULL)
6025 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
6026 GNUNET_STATISTICS_update (stats,
6027 gettext_noop ("# outstanding peerinfo iterate requests"),
6034 GNUNET_assert (chvc->ve_count == 0);
6041 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6044 if (peerinfo != NULL)
6046 GNUNET_PEERINFO_disconnect (peerinfo);
6049 if (GNUNET_SCHEDULER_NO_TASK != hello_task)
6051 GNUNET_SCHEDULER_cancel (hello_task);
6052 hello_task = GNUNET_SCHEDULER_NO_TASK;
6054 /* Can we assume those are gone by now, or do we need to clean up
6056 GNUNET_break (bl_head == NULL);
6057 GNUNET_break (bc_head == NULL);
6062 schedule_ats (void *cls,
6063 const struct GNUNET_SCHEDULER_TaskContext *tc)
6065 struct ATS_Handle *ats = (struct ATS_Handle *) cls;
6069 ats_task = GNUNET_SCHEDULER_NO_TASK;
6070 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
6072 if (shutdown_in_progress == GNUNET_YES)
6075 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running scheduled calculation\n");
6077 ats_calculate_bandwidth_distribution (ats, stats, neighbours);
6078 ats_task = GNUNET_SCHEDULER_add_delayed (ats->exec_interval,
6079 &schedule_ats, ats);
6082 struct ForeignAddressList * get_preferred_ats_address (
6083 struct NeighbourList *n)
6085 // TODO get ATS prefered address
6086 return find_ready_address(n);
6090 * Initiate transport service.
6092 * @param cls closure
6093 * @param server the initialized server
6094 * @param c configuration to use
6098 struct GNUNET_SERVER_Handle *server,
6099 const struct GNUNET_CONFIGURATION_Handle *c)
6101 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
6102 {&handle_start, NULL,
6103 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
6104 {&handle_hello, NULL,
6105 GNUNET_MESSAGE_TYPE_HELLO, 0},
6106 {&handle_send, NULL,
6107 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
6108 {&handle_request_connect, NULL,
6109 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT, sizeof(struct TransportRequestConnectMessage)},
6110 {&handle_set_quota, NULL,
6111 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
6112 {&handle_address_lookup, NULL,
6113 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
6115 {&handle_blacklist_init, NULL,
6116 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
6117 {&handle_blacklist_reply, NULL,
6118 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
6124 unsigned long long tneigh;
6127 shutdown_in_progress = GNUNET_NO;
6129 stats = GNUNET_STATISTICS_create ("transport", cfg);
6130 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
6131 /* parse configuration */
6133 GNUNET_CONFIGURATION_get_value_number (c,
6138 GNUNET_CONFIGURATION_get_value_filename (c,
6140 "HOSTKEY", &keyfile)))
6142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6144 ("Transport service is lacking key configuration settings. Exiting.\n"));
6145 GNUNET_SCHEDULER_shutdown ();
6148 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6151 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6152 validation_map = NULL;
6156 max_connect_per_transport = (uint32_t) tneigh;
6157 peerinfo = GNUNET_PEERINFO_connect (cfg);
6158 if (peerinfo == NULL)
6160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6161 _("Could not access PEERINFO service. Exiting.\n"));
6162 GNUNET_SCHEDULER_shutdown ();
6165 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6168 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6169 validation_map = NULL;
6170 GNUNET_free (keyfile);
6173 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6174 GNUNET_free (keyfile);
6175 if (my_private_key == NULL)
6177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6179 ("Transport service could not access hostkey. Exiting.\n"));
6180 GNUNET_SCHEDULER_shutdown ();
6183 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6186 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6187 validation_map = NULL;
6190 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6191 GNUNET_CRYPTO_hash (&my_public_key,
6192 sizeof (my_public_key), &my_identity.hashPubKey);
6193 /* setup notification */
6194 GNUNET_SERVER_disconnect_notify (server,
6195 &client_disconnect_notification, NULL);
6196 /* load plugins... */
6199 GNUNET_CONFIGURATION_get_value_string (c,
6200 "TRANSPORT", "PLUGINS", &plugs))
6202 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6203 _("Starting transport plugins `%s'\n"), plugs);
6204 pos = strtok (plugs, " ");
6207 start_transport (server, pos);
6209 pos = strtok (NULL, " ");
6211 GNUNET_free (plugs);
6213 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
6214 &shutdown_task, NULL);
6218 ats = ats_init (cfg);
6220 ats_task = GNUNET_SCHEDULER_add_now (&schedule_ats, ats);
6224 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6225 _("Transport service ready.\n"));
6227 /* If we have a blacklist file, read from it */
6228 read_blacklist_file(cfg);
6229 /* process client requests */
6230 GNUNET_SERVER_add_handlers (server, handlers);
6235 * The main function for the transport service.
6237 * @param argc number of arguments from the command line
6238 * @param argv command line arguments
6239 * @return 0 ok, 1 on error
6242 main (int argc, char *const *argv)
6244 a2s (NULL, NULL, 0); /* make compiler happy */
6245 return (GNUNET_OK ==
6246 GNUNET_SERVICE_run (argc,
6249 GNUNET_SERVICE_OPTION_NONE,
6250 &run, NULL)) ? 0 : 1;
6253 /* end of gnunet-service-transport.c */