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 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/gnunet-service-transport.c
23 * @brief low-level P2P messaging
24 * @author Christian Grothoff
28 #include "gnunet_client_lib.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "gnunet_plugin_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_service_lib.h"
38 #include "gnunet_signatures.h"
39 #include "plugin_transport.h"
40 #include "transport.h"
42 #define DEBUG_BLACKLIST GNUNET_NO
44 #define DEBUG_PING_PONG GNUNET_NO
46 #define SIGN_USELESS GNUNET_NO
49 * Should we do some additional checks (to validate behavior
52 #define EXTRA_CHECKS GNUNET_YES
55 * How many messages can we have pending for a given client process
56 * before we start to drop incoming messages? We typically should
57 * have only one client and so this would be the primary buffer for
58 * messages, so the number should be chosen rather generously.
60 * The expectation here is that most of the time the queue is large
61 * enough so that a drop is virtually never required. Note that
62 * this value must be about as large as 'TOTAL_MSGS' in the
63 * 'test_transport_api_reliability.c', otherwise that testcase may
66 #define MAX_PENDING (128 * 1024)
69 * Size of the per-transport blacklist hash maps.
71 #define TRANSPORT_BLACKLIST_HT_SIZE 16
74 * How often should we try to reconnect to a peer using a particular
75 * transport plugin before giving up? Note that the plugin may be
76 * added back to the list after PLUGIN_RETRY_FREQUENCY expires.
78 #define MAX_CONNECT_RETRY 3
81 * Limit on the number of ready-to-run tasks when validating
82 * HELLOs. If more tasks are ready to run, we will drop
83 * HELLOs instead of validating them.
85 #define MAX_HELLO_LOAD 4
88 * How often must a peer violate bandwidth quotas before we start
89 * to simply drop its messages?
91 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
94 * How long until a HELLO verification attempt should time out?
95 * Must be rather small, otherwise a partially successful HELLO
96 * validation (some addresses working) might not be available
97 * before a client's request for a connection fails for good.
98 * Besides, if a single request to an address takes a long time,
99 * then the peer is unlikely worthwhile anyway.
101 #define HELLO_VERIFICATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
104 * Priority to use for PONG messages.
106 #define TRANSPORT_PONG_PRIORITY 4
109 * How often do we re-add (cheaper) plugins to our list of plugins
110 * to try for a given connected peer?
112 #define PLUGIN_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
115 * After how long do we expire an address in a HELLO that we just
116 * validated? This value is also used for our own addresses when we
119 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
123 * How long before an existing address expires should we again try to
124 * validate it? Must be (significantly) smaller than
125 * HELLO_ADDRESS_EXPIRATION.
127 #define HELLO_REVALIDATION_START_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
130 * Maximum frequency for re-evaluating latencies for all transport addresses.
132 #define LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
135 * Maximum frequency for re-evaluating latencies for connected addresses.
137 #define CONNECTED_LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
141 * List of addresses of other peers
143 struct ForeignAddressList
146 * This is a linked list.
148 struct ForeignAddressList *next;
151 * Which ready list does this entry belong to.
153 struct ReadyList *ready_list;
156 * How long until we auto-expire this address (unless it is
157 * re-confirmed by the transport)?
159 struct GNUNET_TIME_Absolute expires;
162 * Task used to re-validate addresses, updates latencies and
165 GNUNET_SCHEDULER_TaskIdentifier revalidate_task;
173 * Session (or NULL if no valid session currently exists or if the
174 * plugin does not use sessions).
176 struct Session *session;
179 * What was the last latency observed for this address, plugin and peer?
181 struct GNUNET_TIME_Relative latency;
184 * If we did not successfully transmit a message to the given peer
185 * via this connection during the specified time, we should consider
186 * the connection to be dead. This is used in the case that a TCP
187 * transport simply stalls writing to the stream but does not
188 * formerly get a signal that the other peer died.
190 struct GNUNET_TIME_Absolute timeout;
193 * How often have we tried to connect using this plugin? Used to
194 * discriminate against addresses that do not work well.
195 * FIXME: not yet used, but should be!
197 unsigned int connect_attempts;
200 * DV distance to this peer (1 if no DV is used).
201 * FIXME: need to set this from transport plugins!
211 * Have we ever estimated the latency of this address? Used to
212 * ensure that the first time we add an address, we immediately
218 * Are we currently connected via this address? The first time we
219 * successfully transmit or receive data to a peer via a particular
220 * address, we set this to GNUNET_YES. If we later get an error
221 * (disconnect notification, transmission failure, timeout), we set
222 * it back to GNUNET_NO.
227 * Is this plugin currently busy transmitting to the specific target?
228 * GNUNET_NO if not (initial, default state is GNUNET_NO). Internal
229 * messages do not count as 'in transmit'.
234 * Has this address been validated yet?
242 * Entry in linked list of network addresses for ourselves.
244 struct OwnAddressList
247 * This is a linked list.
249 struct OwnAddressList *next;
252 * The address, actually a pointer to the end
253 * of this struct. Do not free!
258 * How long until we auto-expire this address (unless it is
259 * re-confirmed by the transport)?
261 struct GNUNET_TIME_Absolute expires;
272 * Entry in linked list of all of our plugins.
274 struct TransportPlugin
278 * This is a linked list.
280 struct TransportPlugin *next;
283 * API of the transport as returned by the plugin's
284 * initialization function.
286 struct GNUNET_TRANSPORT_PluginFunctions *api;
289 * Short name for the plugin (i.e. "tcp").
294 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
299 * List of our known addresses for this transport.
301 struct OwnAddressList *addresses;
304 * Environment this transport service is using
307 struct GNUNET_TRANSPORT_PluginEnvironment env;
310 * ID of task that is used to clean up expired addresses.
312 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
315 * Set to GNUNET_YES if we need to scrap the existing list of
316 * "addresses" and start fresh when we receive the next address
317 * update from a transport. Set to GNUNET_NO if we should just add
318 * the new address to the list and wait for the commit call.
323 * Hashmap of blacklisted peers for this particular transport.
325 struct GNUNET_CONTAINER_MultiHashMap *blacklist;
328 struct NeighbourList;
331 * For each neighbour we keep a list of messages
332 * that we still want to transmit to the neighbour.
338 * This is a doubly linked list.
340 struct MessageQueue *next;
343 * This is a doubly linked list.
345 struct MessageQueue *prev;
348 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
349 * stuck together in memory. Allocated at the end of this struct.
351 const char *message_buf;
354 * Size of the message buf
356 size_t message_buf_size;
359 * Client responsible for queueing the message;
360 * used to check that a client has no two messages
361 * pending for the same target. Can be NULL.
363 struct TransportClient *client;
366 * Using which specific address should we send this message?
368 struct ForeignAddressList *specific_address;
371 * Peer ID of the Neighbour this entry belongs to.
373 struct GNUNET_PeerIdentity neighbour_id;
376 * Plugin that we used for the transmission.
377 * NULL until we scheduled a transmission.
379 struct TransportPlugin *plugin;
382 * At what time should we fail?
384 struct GNUNET_TIME_Absolute timeout;
387 * Internal message of the transport system that should not be
388 * included in the usual SEND-SEND_OK transmission confirmation
389 * traffic management scheme. Typically, "internal_msg" will
390 * be set whenever "client" is NULL (but it is not strictly
396 * How important is the message?
398 unsigned int priority;
404 * For a given Neighbour, which plugins are available
405 * to talk to this peer and what are their costs?
410 * This is a linked list.
412 struct ReadyList *next;
415 * Which of our transport plugins does this entry
418 struct TransportPlugin *plugin;
421 * Transport addresses, latency, and readiness for
422 * this particular plugin.
424 struct ForeignAddressList *addresses;
427 * To which neighbour does this ready list belong to?
429 struct NeighbourList *neighbour;
435 * Entry in linked list of all of our current neighbours.
441 * This is a linked list.
443 struct NeighbourList *next;
446 * Which of our transports is connected to this peer
447 * and what is their status?
449 struct ReadyList *plugins;
452 * Head of list of messages we would like to send to this peer;
453 * must contain at most one message per client.
455 struct MessageQueue *messages_head;
458 * Tail of list of messages we would like to send to this peer; must
459 * contain at most one message per client.
461 struct MessageQueue *messages_tail;
464 * Buffer for at most one payload message used when we receive
465 * payload data before our PING-PONG has succeeded. We then
466 * store such messages in this intermediary buffer until the
467 * connection is fully up.
469 struct GNUNET_MessageHeader *pre_connect_message_buffer;
472 * Context for peerinfo iteration.
473 * NULL after we are done processing peerinfo's information.
475 struct GNUNET_PEERINFO_IteratorContext *piter;
478 * Public key for this peer. Valid only if the respective flag is set below.
480 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
483 * Identity of this neighbour.
485 struct GNUNET_PeerIdentity id;
488 * ID of task scheduled to run when this peer is about to
489 * time out (will free resources associated with the peer).
491 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
494 * ID of task scheduled to run when we should retry transmitting
495 * the head of the message queue. Actually triggered when the
496 * transmission is timing out (we trigger instantly when we have
497 * a chance of success).
499 GNUNET_SCHEDULER_TaskIdentifier retry_task;
502 * How long until we should consider this peer dead
503 * (if we don't receive another message in the
506 struct GNUNET_TIME_Absolute peer_timeout;
509 * Tracker for inbound bandwidth.
511 struct GNUNET_BANDWIDTH_Tracker in_tracker;
514 * The latency we have seen for this particular address for
515 * this particular peer. This latency may have been calculated
516 * over multiple transports. This value reflects how long it took
517 * us to receive a response when SENDING via this particular
518 * transport/neighbour/address combination!
520 * FIXME: we need to periodically send PINGs to update this
521 * latency (at least more often than the current "huge" (11h?)
524 struct GNUNET_TIME_Relative latency;
527 * How often has the other peer (recently) violated the
528 * inbound traffic limit? Incremented by 10 per violation,
529 * decremented by 1 per non-violation (for each
532 unsigned int quota_violation_count;
535 * DV distance to this peer (1 if no DV is used).
540 * Have we seen an PONG from this neighbour in the past (and
541 * not had a disconnect since)?
546 * Do we have a valid public key for this neighbour?
548 int public_key_valid;
553 * Message used to ask a peer to validate receipt (to check an address
556 struct TransportPingMessage
560 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
562 struct GNUNET_MessageHeader header;
565 * Random challenge number (in network byte order).
567 uint32_t challenge GNUNET_PACKED;
570 * Who is the intended recipient?
572 struct GNUNET_PeerIdentity target;
578 * Message used to validate a HELLO. The challenge is included in the
579 * confirmation to make matching of replies to requests possible. The
580 * signature signs the original challenge number, our public key, the
581 * sender's address (so that the sender can check that the address we
582 * saw is plausible for him and possibly detect a MiM attack) and a
583 * timestamp (to limit replay).<p>
585 * This message is followed by the address of the
586 * client that we are observing (which is part of what
589 struct TransportPongMessage
593 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
595 struct GNUNET_MessageHeader header;
598 * For padding, always zero.
600 uint32_t reserved GNUNET_PACKED;
605 struct GNUNET_CRYPTO_RsaSignature signature;
608 * What are we signing and why?
610 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
613 * Random challenge number (in network byte order).
615 uint32_t challenge GNUNET_PACKED;
618 * Who signed this message?
620 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded signer;
623 * Size of address appended to this message
631 * Linked list of messages to be transmitted to the client. Each
632 * entry is followed by the actual message.
634 struct ClientMessageQueueEntry
637 * This is a doubly-linked list.
639 struct ClientMessageQueueEntry *next;
642 * This is a doubly-linked list.
644 struct ClientMessageQueueEntry *prev;
649 * Client connected to the transport service.
651 struct TransportClient
655 * This is a linked list.
657 struct TransportClient *next;
660 * Handle to the client.
662 struct GNUNET_SERVER_Client *client;
665 * Linked list of messages yet to be transmitted to
668 struct ClientMessageQueueEntry *message_queue_head;
671 * Tail of linked list of messages yet to be transmitted to the
674 struct ClientMessageQueueEntry *message_queue_tail;
677 * Current transmit request handle.
679 struct GNUNET_CONNECTION_TransmitHandle *th;
682 * Is a call to "transmit_send_continuation" pending? If so, we
683 * must not free this struct (even if the corresponding client
684 * disconnects) and instead only remove it from the linked list and
685 * set the "client" field to NULL.
690 * Length of the list of messages pending for this client.
692 unsigned int message_count;
698 * Context of currently active requests to peerinfo
699 * for validation of HELLOs.
701 struct CheckHelloValidatedContext;
705 * Entry in map of all HELLOs awaiting validation.
707 struct ValidationEntry
711 * NULL if this entry is not part of a larger HELLO validation.
713 struct CheckHelloValidatedContext *chvc;
716 * The address, actually a pointer to the end
717 * of this struct. Do not free!
722 * Name of the transport.
724 char *transport_name;
727 * The public key of the peer.
729 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
732 * ID of task that will clean up this entry if we don't succeed
733 * with the validation first.
735 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
738 * At what time did we send this validation?
740 struct GNUNET_TIME_Absolute send_time;
743 * Session being validated (or NULL for none).
745 struct Session *session;
748 * Challenge number we used.
761 * Context of currently active requests to peerinfo
762 * for validation of HELLOs.
764 struct CheckHelloValidatedContext
768 * This is a doubly-linked list.
770 struct CheckHelloValidatedContext *next;
773 * This is a doubly-linked list.
775 struct CheckHelloValidatedContext *prev;
778 * Hello that we are validating.
780 const struct GNUNET_HELLO_Message *hello;
783 * Context for peerinfo iteration.
784 * NULL after we are done processing peerinfo's information.
786 struct GNUNET_PEERINFO_IteratorContext *piter;
789 * Was a HELLO known for this peer to peerinfo?
794 * Number of validation entries currently referring to this
797 unsigned int ve_count;
805 static struct GNUNET_HELLO_Message *our_hello;
808 * "version" of "our_hello". Used to see if a given neighbour has
809 * already been sent the latest version of our HELLO message.
811 static unsigned int our_hello_version;
816 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
821 static struct GNUNET_PeerIdentity my_identity;
826 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
831 struct GNUNET_SCHEDULER_Handle *sched;
836 const struct GNUNET_CONFIGURATION_Handle *cfg;
839 * Linked list of all clients to this service.
841 static struct TransportClient *clients;
844 * All loaded plugins.
846 static struct TransportPlugin *plugins;
851 static struct GNUNET_SERVER_Handle *server;
854 * Handle to peerinfo service.
856 static struct GNUNET_PEERINFO_Handle *peerinfo;
859 * All known neighbours and their HELLOs.
861 static struct NeighbourList *neighbours;
864 * Number of neighbours we'd like to have.
866 static uint32_t max_connect_per_transport;
869 * Head of linked list.
871 static struct CheckHelloValidatedContext *chvc_head;
874 * Tail of linked list.
876 static struct CheckHelloValidatedContext *chvc_tail;
879 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
880 * of the given peer that we are currently validating).
882 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
885 * Handle for reporting statistics.
887 static struct GNUNET_STATISTICS_Handle *stats;
891 * The peer specified by the given neighbour has timed-out or a plugin
892 * has disconnected. We may either need to do nothing (other plugins
893 * still up), or trigger a full disconnect and clean up. This
894 * function updates our state and do the necessary notifications.
895 * Also notifies our clients that the neighbour is now officially
898 * @param n the neighbour list entry for the peer
899 * @param check should we just check if all plugins
900 * disconnected or must we ask all plugins to
903 static void disconnect_neighbour (struct NeighbourList *n, int check);
906 * Check the ready list for the given neighbour and if a plugin is
907 * ready for transmission (and if we have a message), do so!
909 * @param neighbour target peer for which to transmit
911 static void try_transmission_to_peer (struct NeighbourList *neighbour);
915 * Find an entry in the neighbour list for a particular peer.
917 * @return NULL if not found.
919 static struct NeighbourList *
920 find_neighbour (const struct GNUNET_PeerIdentity *key)
922 struct NeighbourList *head = neighbours;
924 while ((head != NULL) &&
925 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
932 * Find an entry in the transport list for a particular transport.
934 * @return NULL if not found.
936 static struct TransportPlugin *
937 find_transport (const char *short_name)
939 struct TransportPlugin *head = plugins;
940 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
946 * Is a particular peer blacklisted for a particular transport?
948 * @param peer the peer to check for
949 * @param plugin the plugin used to connect to the peer
951 * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
954 is_blacklisted (const struct GNUNET_PeerIdentity *peer, struct TransportPlugin *plugin)
957 if (plugin->blacklist != NULL)
959 if (GNUNET_CONTAINER_multihashmap_contains(plugin->blacklist, &peer->hashPubKey) == GNUNET_YES)
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
963 _("Peer `%s:%s' is blacklisted!\n"),
964 plugin->short_name, GNUNET_i2s (peer));
975 add_peer_to_blacklist (struct GNUNET_PeerIdentity *peer, char *transport_name)
977 struct TransportPlugin *plugin;
979 plugin = find_transport(transport_name);
980 if (plugin == NULL) /* Nothing to do */
982 if (plugin->blacklist == NULL)
983 plugin->blacklist = GNUNET_CONTAINER_multihashmap_create(TRANSPORT_BLACKLIST_HT_SIZE);
984 GNUNET_assert(plugin->blacklist != NULL);
985 GNUNET_CONTAINER_multihashmap_put(plugin->blacklist, &peer->hashPubKey,
987 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
992 * Read the blacklist file, containing transport:peer entries.
993 * Provided the transport is loaded, set up hashmap with these
994 * entries to blacklist peers by transport.
998 read_blacklist_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1005 struct GNUNET_PeerIdentity pid;
1007 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1008 unsigned int entries_found;
1009 char *transport_name;
1012 GNUNET_CONFIGURATION_get_value_filename (cfg,
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019 _("Option `%s' in section `%s' not specified!\n"),
1025 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1026 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1027 | GNUNET_DISK_PERM_USER_WRITE);
1028 if (0 != STAT (fn, &frstat))
1030 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1031 _("Could not read blacklist file `%s'\n"), fn);
1035 if (frstat.st_size == 0)
1037 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1038 _("Blacklist file `%s' is empty.\n"),
1043 /* FIXME: use mmap */
1044 data = GNUNET_malloc_large (frstat.st_size);
1045 GNUNET_assert(data != NULL);
1046 if (frstat.st_size !=
1047 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1049 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1050 _("Failed to read blacklist from `%s'\n"), fn);
1057 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1059 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1060 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1063 while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') && !isspace ( (unsigned char) data[colon_pos]))
1066 if (colon_pos >= frstat.st_size)
1068 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1069 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1070 (unsigned long long) colon_pos);
1076 if (isspace( (unsigned char) data[colon_pos]))
1078 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1079 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1080 (unsigned long long) colon_pos);
1082 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1086 tsize = colon_pos - pos;
1087 if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size))
1089 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1090 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1091 (unsigned long long) colon_pos);
1097 transport_name = GNUNET_malloc(tsize);
1098 memcpy(transport_name, &data[pos], tsize);
1099 pos = colon_pos + 1;
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1103 _("Read transport name %s in blacklist file.\n"),
1106 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1107 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1109 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1110 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1111 (unsigned long long) pos);
1113 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1115 GNUNET_free_non_null(transport_name);
1118 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1119 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1121 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1122 _("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
1123 (unsigned long long) pos,
1128 if (0 != memcmp (&pid,
1130 sizeof (struct GNUNET_PeerIdentity)))
1133 add_peer_to_blacklist (&pid,
1135 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1136 _("Found blacklisted peer `%s:%s' in configuration\n"),
1137 transport_name, GNUNET_i2s (&pid));
1141 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1142 _("Found myself `%s' in blacklist (useless, ignored)\n"),
1146 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1147 GNUNET_free_non_null(transport_name);
1148 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1157 * Function called to notify a client about the socket being ready to
1158 * queue more data. "buf" will be NULL and "size" zero if the socket
1159 * was closed for writing in the meantime.
1161 * @param cls closure
1162 * @param size number of bytes available in buf
1163 * @param buf where the callee should write the message
1164 * @return number of bytes written to buf
1167 transmit_to_client_callback (void *cls, size_t size, void *buf)
1169 struct TransportClient *client = cls;
1170 struct ClientMessageQueueEntry *q;
1173 const struct GNUNET_MessageHeader *msg;
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180 "Transmission to client failed, closing connection.\n");
1181 /* fatal error with client, free message queue! */
1182 while (NULL != (q = client->message_queue_head))
1184 GNUNET_STATISTICS_update (stats,
1185 gettext_noop ("# bytes discarded (could not transmit to client)"),
1186 ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
1188 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1189 client->message_queue_tail,
1193 client->message_count = 0;
1198 while (NULL != (q = client->message_queue_head))
1200 msg = (const struct GNUNET_MessageHeader *) &q[1];
1201 msize = ntohs (msg->size);
1202 if (msize + tsize > size)
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1206 "Transmitting message of type %u to client.\n",
1209 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1210 client->message_queue_tail,
1212 memcpy (&cbuf[tsize], msg, msize);
1215 client->message_count--;
1219 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1220 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1222 GNUNET_TIME_UNIT_FOREVER_REL,
1223 &transmit_to_client_callback,
1225 GNUNET_assert (client->th != NULL);
1232 * Convert an address to a string.
1234 * @param plugin name of the plugin responsible for the address
1235 * @param addr binary address
1236 * @param addr_len number of bytes in addr
1237 * @return NULL on error, otherwise address string
1240 a2s (const char *plugin,
1244 struct TransportPlugin *p;
1248 p = find_transport (plugin);
1251 return p->api->address_to_string (p->api->cls,
1258 * Mark the given FAL entry as 'connected' (and hence preferred for
1259 * sending); also mark all others for the same peer as 'not connected'
1260 * (since only one can be preferred).
1262 * @param fal address to set to 'connected'
1265 mark_address_connected (struct ForeignAddressList *fal)
1267 struct ForeignAddressList *pos;
1270 GNUNET_assert (GNUNET_YES == fal->validated);
1271 if (fal->connected == GNUNET_YES)
1272 return; /* nothing to do */
1274 pos = fal->ready_list->addresses;
1277 if (GNUNET_YES == pos->connected)
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1281 "Marking address `%s' as no longer connected (due to connect on other address)\n",
1282 a2s (pos->ready_list->plugin->short_name,
1286 GNUNET_break (cnt == GNUNET_YES);
1288 pos->connected = GNUNET_NO;
1289 GNUNET_STATISTICS_update (stats,
1290 gettext_noop ("# connected addresses"),
1296 fal->connected = GNUNET_YES;
1297 if (GNUNET_YES == cnt)
1299 GNUNET_STATISTICS_update (stats,
1300 gettext_noop ("# connected addresses"),
1308 * Send the specified message to the specified client. Since multiple
1309 * messages may be pending for the same client at a time, this code
1310 * makes sure that no message is lost.
1312 * @param client client to transmit the message to
1313 * @param msg the message to send
1314 * @param may_drop can this message be dropped if the
1315 * message queue for this client is getting far too large?
1318 transmit_to_client (struct TransportClient *client,
1319 const struct GNUNET_MessageHeader *msg, int may_drop)
1321 struct ClientMessageQueueEntry *q;
1324 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1326 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1328 ("Dropping message of type %u and size %u, have %u messages pending (%u is the soft limit)\n"),
1331 client->message_count,
1333 GNUNET_STATISTICS_update (stats,
1334 gettext_noop ("# messages dropped due to slow client"),
1339 msize = ntohs (msg->size);
1340 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1341 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1342 memcpy (&q[1], msg, msize);
1343 GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1344 client->message_queue_tail,
1345 client->message_queue_tail,
1347 client->message_count++;
1348 if (client->th == NULL)
1350 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1352 GNUNET_TIME_UNIT_FOREVER_REL,
1353 &transmit_to_client_callback,
1355 GNUNET_assert (client->th != NULL);
1361 * Transmit a 'SEND_OK' notification to the given client for the
1364 * @param client who to notify
1365 * @param n neighbour to notify about
1366 * @param result status code for the transmission request
1369 transmit_send_ok (struct TransportClient *client,
1370 struct NeighbourList *n,
1373 struct SendOkMessage send_ok_msg;
1375 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1376 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1377 send_ok_msg.success = htonl (result);
1378 send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1379 send_ok_msg.peer = n->id;
1380 transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1385 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1386 * upon "completion" of a send request. This tells the API
1387 * that it is now legal to send another message to the given
1390 * @param cls closure, identifies the entry on the
1391 * message queue that was transmitted and the
1392 * client responsible for queueing the message
1393 * @param target the peer receiving the message
1394 * @param result GNUNET_OK on success, if the transmission
1395 * failed, we should not tell the client to transmit
1399 transmit_send_continuation (void *cls,
1400 const struct GNUNET_PeerIdentity *target,
1403 struct MessageQueue *mq = cls;
1404 struct NeighbourList *n;
1406 GNUNET_STATISTICS_update (stats,
1407 gettext_noop ("# bytes pending with plugins"),
1408 - (int64_t) mq->message_buf_size,
1410 if (result == GNUNET_OK)
1412 GNUNET_STATISTICS_update (stats,
1413 gettext_noop ("# bytes successfully transmitted by plugins"),
1414 mq->message_buf_size,
1419 GNUNET_STATISTICS_update (stats,
1420 gettext_noop ("# bytes with transmission failure by plugins"),
1421 mq->message_buf_size,
1424 n = find_neighbour(&mq->neighbour_id);
1425 GNUNET_assert (n != NULL);
1426 if (mq->specific_address != NULL)
1428 if (result == GNUNET_OK)
1430 mq->specific_address->timeout =
1431 GNUNET_TIME_relative_to_absolute
1432 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1433 if (mq->specific_address->validated == GNUNET_YES)
1434 mark_address_connected (mq->specific_address);
1438 if (mq->specific_address->connected != GNUNET_NO)
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442 "Marking address `%s' as no longer connected (due to transmission problem)\n",
1443 a2s (mq->specific_address->ready_list->plugin->short_name,
1444 mq->specific_address->addr,
1445 mq->specific_address->addrlen));
1447 GNUNET_STATISTICS_update (stats,
1448 gettext_noop ("# connected addresses"),
1451 mq->specific_address->connected = GNUNET_NO;
1454 if (! mq->internal_msg)
1455 mq->specific_address->in_transmit = GNUNET_NO;
1457 if (mq->client != NULL)
1458 transmit_send_ok (mq->client, n, result);
1460 try_transmission_to_peer (n);
1465 * Find an address in any of the available transports for
1466 * the given neighbour that would be good for message
1467 * transmission. This is essentially the transport selection
1470 * @param neighbour for whom to select an address
1471 * @return selected address, NULL if we have none
1473 struct ForeignAddressList *
1474 find_ready_address(struct NeighbourList *neighbour)
1476 struct ReadyList *head = neighbour->plugins;
1477 struct ForeignAddressList *addresses;
1478 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1479 struct ForeignAddressList *best_address;
1481 best_address = NULL;
1482 while (head != NULL)
1484 addresses = head->addresses;
1485 while (addresses != NULL)
1487 if ( (addresses->timeout.value < now.value) &&
1488 (addresses->connected == GNUNET_YES) )
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1492 "Marking long-time inactive connection to `%4s' as down.\n",
1493 GNUNET_i2s (&neighbour->id));
1495 GNUNET_STATISTICS_update (stats,
1496 gettext_noop ("# connected addresses"),
1499 addresses->connected = GNUNET_NO;
1501 addresses = addresses->next;
1504 addresses = head->addresses;
1505 while (addresses != NULL)
1507 #if DEBUG_TRANSPORT > 1
1508 if (addresses->addr != NULL)
1509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1510 "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
1511 a2s (head->plugin->short_name,
1513 addresses->addrlen),
1514 GNUNET_i2s (&neighbour->id),
1515 addresses->connected,
1516 addresses->in_transmit,
1517 addresses->validated,
1518 addresses->connect_attempts,
1519 (unsigned long long) addresses->timeout.value,
1520 (unsigned int) addresses->distance);
1522 if ( ( (best_address == NULL) ||
1523 (addresses->connected == GNUNET_YES) ||
1524 (best_address->connected == GNUNET_NO) ) &&
1525 (addresses->in_transmit == GNUNET_NO) &&
1526 ( (best_address == NULL) ||
1527 (addresses->latency.value < best_address->latency.value)) )
1528 best_address = addresses;
1529 /* FIXME: also give lower-latency addresses that are not
1530 connected a chance some times... */
1531 addresses = addresses->next;
1535 if (best_address != NULL)
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1539 "Best address found (`%s') has latency of %llu ms.\n",
1540 a2s (best_address->ready_list->plugin->short_name,
1542 best_address->addrlen),
1543 best_address->latency.value);
1548 GNUNET_STATISTICS_update (stats,
1549 gettext_noop ("# transmission attempts failed (no address)"),
1553 return best_address;
1559 * We should re-try transmitting to the given peer,
1560 * hopefully we've learned something in the meantime.
1563 retry_transmission_task (void *cls,
1564 const struct GNUNET_SCHEDULER_TaskContext *tc)
1566 struct NeighbourList *n = cls;
1568 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1569 try_transmission_to_peer (n);
1574 * Check the ready list for the given neighbour and if a plugin is
1575 * ready for transmission (and if we have a message), do so!
1577 * @param neighbour target peer for which to transmit
1580 try_transmission_to_peer (struct NeighbourList *neighbour)
1582 struct ReadyList *rl;
1583 struct MessageQueue *mq;
1584 struct GNUNET_TIME_Relative timeout;
1588 if (neighbour->messages_head == NULL)
1591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1592 "Transmission queue for `%4s' is empty\n",
1593 GNUNET_i2s (&neighbour->id));
1595 return; /* nothing to do */
1598 mq = neighbour->messages_head;
1599 force_address = GNUNET_YES;
1600 if (mq->specific_address == NULL)
1602 mq->specific_address = find_ready_address(neighbour);
1603 GNUNET_STATISTICS_update (stats,
1604 gettext_noop ("# transport selected peer address freely"),
1607 force_address = GNUNET_NO;
1609 if (mq->specific_address == NULL)
1611 GNUNET_STATISTICS_update (stats,
1612 gettext_noop ("# transport failed to selected peer address"),
1615 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1616 if (timeout.value == 0)
1619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1620 "No destination address available to transmit message of size %u to peer `%4s'\n",
1621 mq->message_buf_size,
1622 GNUNET_i2s (&mq->neighbour_id));
1624 GNUNET_STATISTICS_update (stats,
1625 gettext_noop ("# bytes in message queue for other peers"),
1626 - (int64_t) mq->message_buf_size,
1628 GNUNET_STATISTICS_update (stats,
1629 gettext_noop ("# bytes discarded (no destination address available)"),
1630 mq->message_buf_size,
1632 if (mq->client != NULL)
1633 transmit_send_ok (mq->client, neighbour, GNUNET_NO);
1634 GNUNET_CONTAINER_DLL_remove (neighbour->messages_head,
1635 neighbour->messages_tail,
1638 return; /* nobody ready */
1640 GNUNET_STATISTICS_update (stats,
1641 gettext_noop ("# message delivery deferred (no address)"),
1644 if (neighbour->retry_task != GNUNET_SCHEDULER_NO_TASK)
1645 GNUNET_SCHEDULER_cancel (sched,
1646 neighbour->retry_task);
1647 neighbour->retry_task = GNUNET_SCHEDULER_add_delayed (sched,
1649 &retry_transmission_task,
1652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1653 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
1654 mq->message_buf_size,
1655 GNUNET_i2s (&mq->neighbour_id),
1658 /* FIXME: might want to trigger peerinfo lookup here
1659 (unless that's already pending...) */
1662 GNUNET_CONTAINER_DLL_remove (neighbour->messages_head,
1663 neighbour->messages_tail,
1665 if (mq->specific_address->connected == GNUNET_NO)
1666 mq->specific_address->connect_attempts++;
1667 rl = mq->specific_address->ready_list;
1668 mq->plugin = rl->plugin;
1669 if (!mq->internal_msg)
1670 mq->specific_address->in_transmit = GNUNET_YES;
1672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1673 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
1674 mq->message_buf_size,
1675 GNUNET_i2s (&neighbour->id),
1676 (mq->specific_address->addr != NULL)
1677 ? a2s (mq->plugin->short_name,
1678 mq->specific_address->addr,
1679 mq->specific_address->addrlen)
1681 rl->plugin->short_name);
1683 GNUNET_STATISTICS_update (stats,
1684 gettext_noop ("# bytes in message queue for other peers"),
1685 - (int64_t) mq->message_buf_size,
1687 GNUNET_STATISTICS_update (stats,
1688 gettext_noop ("# bytes pending with plugins"),
1689 mq->message_buf_size,
1691 ret = rl->plugin->api->send (rl->plugin->api->cls,
1694 mq->message_buf_size,
1696 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1697 mq->specific_address->session,
1698 mq->specific_address->addr,
1699 mq->specific_address->addrlen,
1701 &transmit_send_continuation, mq);
1704 /* failure, but 'send' would not call continuation in this case,
1705 so we need to do it here! */
1706 transmit_send_continuation (mq,
1714 * Send the specified message to the specified peer.
1716 * @param client source of the transmission request (can be NULL)
1717 * @param peer_address ForeignAddressList where we should send this message
1718 * @param priority how important is the message
1719 * @param timeout how long do we have to transmit?
1720 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
1721 * @param message_buf_size total size of all messages in message_buf
1722 * @param is_internal is this an internal message; these are pre-pended and
1723 * also do not count for plugins being "ready" to transmit
1724 * @param neighbour handle to the neighbour for transmission
1727 transmit_to_peer (struct TransportClient *client,
1728 struct ForeignAddressList *peer_address,
1729 unsigned int priority,
1730 struct GNUNET_TIME_Relative timeout,
1731 const char *message_buf,
1732 size_t message_buf_size,
1733 int is_internal, struct NeighbourList *neighbour)
1735 struct MessageQueue *mq;
1740 /* check for duplicate submission */
1741 mq = neighbour->messages_head;
1744 if (mq->client == client)
1746 /* client transmitted to same peer twice
1747 before getting SEND_OK! */
1755 GNUNET_STATISTICS_update (stats,
1756 gettext_noop ("# bytes in message queue for other peers"),
1759 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
1760 mq->specific_address = peer_address;
1761 mq->client = client;
1762 memcpy (&mq[1], message_buf, message_buf_size);
1763 mq->message_buf = (const char*) &mq[1];
1764 mq->message_buf_size = message_buf_size;
1765 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
1766 mq->internal_msg = is_internal;
1767 mq->priority = priority;
1768 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1770 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
1771 neighbour->messages_tail,
1774 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
1775 neighbour->messages_tail,
1776 neighbour->messages_tail,
1778 try_transmission_to_peer (neighbour);
1785 struct GeneratorContext
1787 struct TransportPlugin *plug_pos;
1788 struct OwnAddressList *addr_pos;
1789 struct GNUNET_TIME_Absolute expiration;
1797 address_generator (void *cls, size_t max, void *buf)
1799 struct GeneratorContext *gc = cls;
1802 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
1804 gc->plug_pos = gc->plug_pos->next;
1805 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
1807 if (NULL == gc->plug_pos)
1812 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
1815 gc->addr_pos->addrlen, buf, max);
1816 gc->addr_pos = gc->addr_pos->next;
1822 * Construct our HELLO message from all of the addresses of
1823 * all of the transports.
1828 struct GNUNET_HELLO_Message *hello;
1829 struct TransportClient *cpos;
1830 struct NeighbourList *npos;
1831 struct GeneratorContext gc;
1833 gc.plug_pos = plugins;
1834 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
1835 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1836 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
1838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1839 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
1841 GNUNET_STATISTICS_update (stats,
1842 gettext_noop ("# refreshed my HELLO"),
1846 while (cpos != NULL)
1848 transmit_to_client (cpos,
1849 (const struct GNUNET_MessageHeader *) hello,
1854 GNUNET_free_non_null (our_hello);
1856 our_hello_version++;
1857 GNUNET_PEERINFO_add_peer (peerinfo, our_hello);
1859 while (npos != NULL)
1862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1863 "Transmitting updated `%s' to neighbour `%4s'\n",
1864 "HELLO", GNUNET_i2s (&npos->id));
1866 GNUNET_STATISTICS_update (stats,
1867 gettext_noop ("# transmitted my HELLO to other peers"),
1870 transmit_to_peer (NULL, NULL, 0,
1871 HELLO_ADDRESS_EXPIRATION,
1872 (const char *) our_hello,
1873 GNUNET_HELLO_size(our_hello),
1881 * Task used to clean up expired addresses for a plugin.
1883 * @param cls closure
1887 expire_address_task (void *cls,
1888 const struct GNUNET_SCHEDULER_TaskContext *tc);
1892 * Update the list of addresses for this plugin,
1893 * expiring those that are past their expiration date.
1895 * @param plugin addresses of which plugin should be recomputed?
1896 * @param fresh set to GNUNET_YES if a new address was added
1897 * and we need to regenerate the HELLO even if nobody
1901 update_addresses (struct TransportPlugin *plugin, int fresh)
1903 static struct GNUNET_TIME_Absolute last_update;
1904 struct GNUNET_TIME_Relative min_remaining;
1905 struct GNUNET_TIME_Relative remaining;
1906 struct GNUNET_TIME_Absolute now;
1907 struct OwnAddressList *pos;
1908 struct OwnAddressList *prev;
1909 struct OwnAddressList *next;
1912 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_TASK)
1913 GNUNET_SCHEDULER_cancel (plugin->env.sched, plugin->address_update_task);
1914 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1915 now = GNUNET_TIME_absolute_get ();
1916 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
1917 expired = (GNUNET_TIME_absolute_get_duration (last_update).value > (HELLO_ADDRESS_EXPIRATION.value / 4));
1919 pos = plugin->addresses;
1923 if (pos->expires.value < now.value)
1925 expired = GNUNET_YES;
1927 plugin->addresses = pos->next;
1929 prev->next = pos->next;
1934 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
1935 if (remaining.value < min_remaining.value)
1936 min_remaining = remaining;
1942 if (expired || fresh)
1947 min_remaining = GNUNET_TIME_relative_min (min_remaining,
1948 GNUNET_TIME_relative_divide (HELLO_ADDRESS_EXPIRATION,
1950 plugin->address_update_task
1951 = GNUNET_SCHEDULER_add_delayed (plugin->env.sched,
1953 &expire_address_task, plugin);
1958 * Task used to clean up expired addresses for a plugin.
1960 * @param cls closure
1964 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1966 struct TransportPlugin *plugin = cls;
1968 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1969 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1970 update_addresses (plugin, GNUNET_NO);
1975 * Iterator over hash map entries that NULLs the session of validation
1976 * entries that match the given session.
1978 * @param cls closure (the 'struct Session*' to match against)
1979 * @param key current key code (peer ID, not used)
1980 * @param value value in the hash map ('struct ValidationEntry*')
1981 * @return GNUNET_YES (we should continue to iterate)
1984 remove_session_validations (void *cls,
1985 const GNUNET_HashCode * key,
1988 struct Session *session = cls;
1989 struct ValidationEntry *ve = value;
1991 if (session == ve->session)
1998 * Function that will be called whenever the plugin internally
1999 * cleans up a session pointer and hence the service needs to
2000 * discard all of those sessions as well. Plugins that do not
2001 * use sessions can simply omit calling this function and always
2002 * use NULL wherever a session pointer is needed.
2004 * @param cls closure
2005 * @param peer which peer was the session for
2006 * @param session which session is being destoyed
2009 plugin_env_session_end (void *cls,
2010 const struct GNUNET_PeerIdentity *peer,
2011 struct Session *session)
2013 struct TransportPlugin *p = cls;
2014 struct NeighbourList *nl;
2015 struct ReadyList *rl;
2016 struct ForeignAddressList *pos;
2017 struct ForeignAddressList *prev;
2019 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2020 &remove_session_validations,
2022 nl = find_neighbour (peer);
2028 if (rl->plugin == p)
2035 pos = rl->addresses;
2036 while ( (pos != NULL) &&
2037 (pos->session != session) )
2044 pos->session = NULL;
2045 if (pos->addrlen != 0)
2048 rl->addresses = pos->next;
2050 prev->next = pos->next;
2051 if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2053 GNUNET_SCHEDULER_cancel (sched,
2054 pos->revalidate_task);
2055 pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2058 if (nl->received_pong == GNUNET_NO)
2059 return; /* nothing to do */
2060 /* check if we have any validated addresses left */
2061 pos = rl->addresses;
2068 /* no valid addresses left, signal disconnect! */
2069 disconnect_neighbour (nl, GNUNET_NO);
2074 * Function that must be called by each plugin to notify the
2075 * transport service about the addresses under which the transport
2076 * provided by the plugin can be reached.
2078 * @param cls closure
2079 * @param name name of the transport that generated the address
2080 * @param addr one of the addresses of the host, NULL for the last address
2081 * the specific address format depends on the transport
2082 * @param addrlen length of the address
2083 * @param expires when should this address automatically expire?
2086 plugin_env_notify_address (void *cls,
2090 struct GNUNET_TIME_Relative expires)
2092 struct TransportPlugin *p = cls;
2093 struct OwnAddressList *al;
2094 struct GNUNET_TIME_Absolute abex;
2096 GNUNET_assert (addr != NULL);
2097 abex = GNUNET_TIME_relative_to_absolute (expires);
2098 GNUNET_assert (p == find_transport (name));
2102 if ((addrlen == al->addrlen) && (0 == memcmp (addr, &al[1], addrlen)))
2104 if (al->expires.value < abex.value)
2111 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2113 al->next = p->addresses;
2116 al->addrlen = addrlen;
2117 memcpy (&al[1], addr, addrlen);
2118 update_addresses (p, GNUNET_YES);
2123 * Notify all of our clients about a peer connecting.
2126 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2127 struct GNUNET_TIME_Relative latency,
2130 struct ConnectInfoMessage cim;
2131 struct TransportClient *cpos;
2134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2135 "Notifying clients about connection from `%s'\n",
2138 GNUNET_STATISTICS_update (stats,
2139 gettext_noop ("# peers connected"),
2142 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
2143 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2144 cim.distance = htonl (distance);
2145 cim.latency = GNUNET_TIME_relative_hton (latency);
2146 memcpy (&cim.id, peer, sizeof (struct GNUNET_PeerIdentity));
2148 while (cpos != NULL)
2150 transmit_to_client (cpos, &cim.header, GNUNET_NO);
2157 * Notify all of our clients about a peer disconnecting.
2160 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2162 struct DisconnectInfoMessage dim;
2163 struct TransportClient *cpos;
2166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2167 "Notifying clients about lost connection to `%s'\n",
2170 GNUNET_STATISTICS_update (stats,
2171 gettext_noop ("# peers connected"),
2174 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2175 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2176 dim.reserved = htonl (0);
2177 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2179 while (cpos != NULL)
2181 transmit_to_client (cpos, &dim.header, GNUNET_NO);
2188 * Find a ForeignAddressList entry for the given neighbour
2189 * that matches the given address and transport.
2191 * @param neighbour which peer we care about
2192 * @param tname name of the transport plugin
2193 * @param session session to look for, NULL for 'any'; otherwise
2194 * can be used for the service to "learn" this session ID
2196 * @param addr binary address
2197 * @param addrlen length of addr
2198 * @return NULL if no such entry exists
2200 static struct ForeignAddressList *
2201 find_peer_address(struct NeighbourList *neighbour,
2203 struct Session *session,
2207 struct ReadyList *head;
2208 struct ForeignAddressList *pos;
2210 head = neighbour->plugins;
2211 while (head != NULL)
2213 if (0 == strcmp (tname, head->plugin->short_name))
2219 pos = head->addresses;
2220 while ( (pos != NULL) &&
2221 ( (pos->addrlen != addrlen) ||
2222 (memcmp(pos->addr, addr, addrlen) != 0) ) )
2224 if ( (session != NULL) &&
2225 (pos->session == session) )
2229 if ( (session != NULL) && (pos != NULL) )
2230 pos->session = session; /* learn it! */
2236 * Get the peer address struct for the given neighbour and
2237 * address. If it doesn't yet exist, create it.
2239 * @param neighbour which peer we care about
2240 * @param tname name of the transport plugin
2241 * @param session session of the plugin, or NULL for none
2242 * @param addr binary address
2243 * @param addrlen length of addr
2244 * @return NULL if we do not have a transport plugin for 'tname'
2246 static struct ForeignAddressList *
2247 add_peer_address (struct NeighbourList *neighbour,
2249 struct Session *session,
2253 struct ReadyList *head;
2254 struct ForeignAddressList *ret;
2256 ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2259 head = neighbour->plugins;
2261 while (head != NULL)
2263 if (0 == strcmp (tname, head->plugin->short_name))
2269 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2270 ret->session = session;
2273 ret->addr = (const char*) &ret[1];
2274 memcpy (&ret[1], addr, addrlen);
2280 ret->addrlen = addrlen;
2281 ret->expires = GNUNET_TIME_relative_to_absolute
2282 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2283 ret->latency = GNUNET_TIME_relative_get_forever();
2285 ret->timeout = GNUNET_TIME_relative_to_absolute
2286 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2287 ret->ready_list = head;
2288 ret->next = head->addresses;
2289 head->addresses = ret;
2295 * Closure for 'add_validated_address'.
2297 struct AddValidatedAddressContext
2300 * Entry that has been validated.
2302 const struct ValidationEntry *ve;
2305 * Flag set after we have added the address so
2306 * that we terminate the iteration next time.
2313 * Callback function used to fill a buffer of max bytes with a list of
2314 * addresses in the format used by HELLOs. Should use
2315 * "GNUNET_HELLO_add_address" as a helper function.
2317 * @param cls the 'struct AddValidatedAddressContext' with the validated address
2318 * @param max maximum number of bytes that can be written to buf
2319 * @param buf where to write the address information
2320 * @return number of bytes written, 0 to signal the
2321 * end of the iteration.
2324 add_validated_address (void *cls,
2325 size_t max, void *buf)
2327 struct AddValidatedAddressContext *avac = cls;
2328 const struct ValidationEntry *ve = avac->ve;
2330 if (GNUNET_YES == avac->done)
2332 avac->done = GNUNET_YES;
2333 return GNUNET_HELLO_add_address (ve->transport_name,
2334 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2344 * Closure for 'check_address_exists'.
2346 struct CheckAddressExistsClosure
2349 * Address to check for.
2354 * Name of the transport.
2361 struct Session *session;
2364 * Set to GNUNET_YES if the address exists.
2377 * Iterator over hash map entries. Checks if the given
2378 * validation entry is for the same address as what is given
2381 * @param cls the 'struct CheckAddressExistsClosure*'
2382 * @param key current key code (ignored)
2383 * @param value value in the hash map ('struct ValidationEntry')
2384 * @return GNUNET_YES if we should continue to
2385 * iterate (mismatch), GNUNET_NO if not (entry matched)
2388 check_address_exists (void *cls,
2389 const GNUNET_HashCode * key,
2392 struct CheckAddressExistsClosure *caec = cls;
2393 struct ValidationEntry *ve = value;
2395 if ( (0 == strcmp (caec->tname,
2396 ve->transport_name)) &&
2397 (caec->addrlen == ve->addrlen) &&
2398 (0 == memcmp (caec->addr,
2402 caec->exists = GNUNET_YES;
2405 if ( (ve->session != NULL) &&
2406 (caec->session == ve->session) )
2408 caec->exists = GNUNET_YES;
2417 * Iterator to free entries in the validation_map.
2419 * @param cls closure (unused)
2420 * @param key current key code
2421 * @param value value in the hash map (validation to abort)
2422 * @return GNUNET_YES (always)
2425 abort_validation (void *cls,
2426 const GNUNET_HashCode * key,
2429 struct ValidationEntry *va = value;
2431 if (GNUNET_SCHEDULER_NO_TASK != va->timeout_task)
2432 GNUNET_SCHEDULER_cancel (sched, va->timeout_task);
2433 GNUNET_free (va->transport_name);
2434 if (va->chvc != NULL)
2436 va->chvc->ve_count--;
2437 if (va->chvc->ve_count == 0)
2439 GNUNET_CONTAINER_DLL_remove (chvc_head,
2442 GNUNET_free (va->chvc);
2452 * HELLO validation cleanup task (validation failed).
2454 * @param cls the 'struct ValidationEntry' that failed
2455 * @param tc scheduler context (unused)
2458 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2460 struct ValidationEntry *va = cls;
2461 struct GNUNET_PeerIdentity pid;
2463 va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2464 GNUNET_STATISTICS_update (stats,
2465 gettext_noop ("# address validation timeouts"),
2468 GNUNET_CRYPTO_hash (&va->publicKey,
2470 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2472 GNUNET_break (GNUNET_OK ==
2473 GNUNET_CONTAINER_multihashmap_remove (validation_map,
2476 abort_validation (NULL, NULL, va);
2481 neighbour_timeout_task (void *cls,
2482 const struct GNUNET_SCHEDULER_TaskContext *tc)
2484 struct NeighbourList *n = cls;
2487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2488 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
2490 GNUNET_STATISTICS_update (stats,
2491 gettext_noop ("# disconnects due to timeout"),
2494 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2495 disconnect_neighbour (n, GNUNET_NO);
2500 * Schedule the job that will cause us to send a PING to the
2501 * foreign address to evaluate its validity and latency.
2503 * @param fal address to PING
2506 schedule_next_ping (struct ForeignAddressList *fal);
2510 * Add the given address to the list of foreign addresses
2511 * available for the given peer (check for duplicates).
2513 * @param cls the respective 'struct NeighbourList' to update
2514 * @param tname name of the transport
2515 * @param expiration expiration time
2516 * @param addr the address
2517 * @param addrlen length of the address
2518 * @return GNUNET_OK (always)
2521 add_to_foreign_address_list (void *cls,
2523 struct GNUNET_TIME_Absolute expiration,
2527 struct NeighbourList *n = cls;
2528 struct ForeignAddressList *fal;
2531 GNUNET_STATISTICS_update (stats,
2532 gettext_noop ("# valid peer addresses returned by PEERINFO"),
2536 fal = find_peer_address (n, tname, NULL, addr, addrlen);
2540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2541 "Adding address `%s' (%s) for peer `%4s' due to PEERINFO data for %llums.\n",
2542 a2s (tname, addr, addrlen),
2544 GNUNET_i2s (&n->id),
2547 fal = add_peer_address (n, tname, NULL, addr, addrlen);
2550 GNUNET_STATISTICS_update (stats,
2551 gettext_noop ("# previously validated addresses lacking transport"),
2557 fal->expires = GNUNET_TIME_absolute_max (expiration,
2559 schedule_next_ping (fal);
2565 fal->expires = GNUNET_TIME_absolute_max (expiration,
2570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2571 "Failed to add new address for `%4s'\n",
2572 GNUNET_i2s (&n->id));
2575 if (fal->validated == GNUNET_NO)
2577 fal->validated = GNUNET_YES;
2578 GNUNET_STATISTICS_update (stats,
2579 gettext_noop ("# peer addresses considered valid"),
2583 if (try == GNUNET_YES)
2585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2586 "Have new addresses, will try to trigger transmissions.\n");
2587 try_transmission_to_peer (n);
2594 * Add addresses in validated HELLO "h" to the set of addresses
2595 * we have for this peer.
2597 * @param cls closure ('struct NeighbourList*')
2598 * @param peer id of the peer, NULL for last call
2599 * @param h hello message for the peer (can be NULL)
2600 * @param trust amount of trust we have in the peer (not used)
2603 add_hello_for_peer (void *cls,
2604 const struct GNUNET_PeerIdentity *peer,
2605 const struct GNUNET_HELLO_Message *h,
2608 struct NeighbourList *n = cls;
2616 return; /* no HELLO available */
2618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2619 "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
2623 if (GNUNET_YES != n->public_key_valid)
2625 GNUNET_HELLO_get_key (h, &n->publicKey);
2626 n->public_key_valid = GNUNET_YES;
2628 GNUNET_HELLO_iterate_addresses (h,
2630 &add_to_foreign_address_list,
2636 * Create a fresh entry in our neighbour list for the given peer.
2637 * Will try to transmit our current HELLO to the new neighbour.
2638 * Do not call this function directly, use 'setup_peer_check_blacklist.
2640 * @param peer the peer for which we create the entry
2641 * @param do_hello should we schedule transmitting a HELLO
2642 * @return the new neighbour list entry
2644 static struct NeighbourList *
2645 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
2648 struct NeighbourList *n;
2649 struct TransportPlugin *tp;
2650 struct ReadyList *rl;
2653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2654 "Setting up state for neighbour `%4s'\n",
2657 GNUNET_assert (our_hello != NULL);
2658 GNUNET_STATISTICS_update (stats,
2659 gettext_noop ("# active neighbours"),
2662 n = GNUNET_malloc (sizeof (struct NeighbourList));
2663 n->next = neighbours;
2667 GNUNET_TIME_relative_to_absolute
2668 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2669 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2670 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2671 MAX_BANDWIDTH_CARRY_S);
2675 if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
2677 rl = GNUNET_malloc (sizeof (struct ReadyList));
2679 rl->next = n->plugins;
2682 rl->addresses = NULL;
2686 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2688 n->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
2689 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2690 &neighbour_timeout_task, n);
2693 n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
2694 0, GNUNET_TIME_UNIT_FOREVER_REL,
2695 &add_hello_for_peer, n);
2696 transmit_to_peer (NULL, NULL, 0,
2697 HELLO_ADDRESS_EXPIRATION,
2698 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
2706 * Function called after we have checked if communicating
2707 * with a given peer is acceptable.
2709 * @param cls closure
2710 * @param n NULL if communication is not acceptable
2712 typedef void (*SetupContinuation)(void *cls,
2713 struct NeighbourList *n);
2717 * Information kept for each client registered to perform
2723 * This is a linked list.
2725 struct Blacklisters *next;
2728 * This is a linked list.
2730 struct Blacklisters *prev;
2733 * Client responsible for this entry.
2735 struct GNUNET_SERVER_Client *client;
2738 * Blacklist check that we're currently performing.
2740 struct BlacklistCheck *bc;
2746 * Head of DLL of blacklisting clients.
2748 static struct Blacklisters *bl_head;
2751 * Tail of DLL of blacklisting clients.
2753 static struct Blacklisters *bl_tail;
2757 * Context we use when performing a blacklist check.
2759 struct BlacklistCheck
2763 * This is a linked list.
2765 struct BlacklistCheck *next;
2768 * This is a linked list.
2770 struct BlacklistCheck *prev;
2773 * Peer being checked.
2775 struct GNUNET_PeerIdentity peer;
2778 * Option for setup neighbour afterwards.
2783 * Continuation to call with the result.
2785 SetupContinuation cont;
2793 * Current transmission request handle for this client, or NULL if no
2794 * request is pending.
2796 struct GNUNET_CONNECTION_TransmitHandle *th;
2799 * Our current position in the blacklisters list.
2801 struct Blacklisters *bl_pos;
2804 * Current task performing the check.
2806 GNUNET_SCHEDULER_TaskIdentifier task;
2811 * Head of DLL of active blacklisting queries.
2813 static struct BlacklistCheck *bc_head;
2816 * Tail of DLL of active blacklisting queries.
2818 static struct BlacklistCheck *bc_tail;
2822 * Perform next action in the blacklist check.
2824 * @param cls the 'struct BlacklistCheck*'
2828 do_blacklist_check (void *cls,
2829 const struct GNUNET_SCHEDULER_TaskContext *tc);
2833 * Transmit blacklist query to the client.
2835 * @param cls the 'struct BlacklistCheck'
2836 * @param size number of bytes allowed
2837 * @param buf where to copy the message
2838 * @return number of bytes copied to buf
2841 transmit_blacklist_message (void *cls,
2845 struct BlacklistCheck *bc = cls;
2846 struct Blacklisters *bl;
2847 struct BlacklistMessage bm;
2852 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
2853 bc->task = GNUNET_SCHEDULER_add_now (sched,
2854 &do_blacklist_check,
2859 bm.header.size = htons (sizeof (struct BlacklistMessage));
2860 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
2861 bm.is_allowed = htonl (0);
2863 memcpy (buf, &bm, sizeof (bm));
2864 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
2870 * Perform next action in the blacklist check.
2872 * @param cls the 'struct BlacklistCheck*'
2876 do_blacklist_check (void *cls,
2877 const struct GNUNET_SCHEDULER_TaskContext *tc)
2879 struct BlacklistCheck *bc = cls;
2880 struct Blacklisters *bl;
2882 bc->task = GNUNET_SCHEDULER_NO_TASK;
2886 bc->cont (bc->cont_cls,
2887 setup_new_neighbour (&bc->peer, bc->do_hello));
2894 bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
2895 sizeof (struct BlacklistMessage),
2896 GNUNET_TIME_UNIT_FOREVER_REL,
2897 &transmit_blacklist_message,
2904 * Obtain a 'struct NeighbourList' for the given peer. If such an entry
2905 * does not yet exist, check the blacklist. If the blacklist says creating
2906 * one is acceptable, create one and call the continuation; otherwise
2907 * call the continuation with NULL.
2909 * @param peer peer to setup or look up a struct NeighbourList for
2910 * @param do_hello should we also schedule sending our HELLO to the peer
2911 * if this is a new record
2912 * @param cont function to call with the 'struct NeigbhbourList*'
2913 * @param cont_cls closure for cont
2916 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
2918 SetupContinuation cont,
2921 struct NeighbourList *n;
2922 struct BlacklistCheck *bc;
2924 n = find_neighbour(peer);
2930 if (bl_head == NULL)
2933 setup_new_neighbour (peer, do_hello));
2936 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
2937 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
2939 bc->do_hello = do_hello;
2941 bc->cont_cls = cont_cls;
2942 bc->bl_pos = bl_head;
2943 bc->task = GNUNET_SCHEDULER_add_now (sched,
2944 &do_blacklist_check,
2950 * Function called with the result of querying a new blacklister about
2951 * it being allowed (or not) to continue to talk to an existing neighbour.
2953 * @param cls the original 'struct NeighbourList'
2954 * @param n NULL if we need to disconnect
2957 confirm_or_drop_neighbour (void *cls,
2958 struct NeighbourList *n)
2960 struct NeighbourList * orig = cls;
2963 disconnect_neighbour (orig, GNUNET_NO);
2968 * Handle a request to start a blacklist.
2970 * @param cls closure (always NULL)
2971 * @param client identification of the client
2972 * @param message the actual message
2975 handle_blacklist_init (void *cls,
2976 struct GNUNET_SERVER_Client *client,
2977 const struct GNUNET_MessageHeader *message)
2979 struct Blacklisters *bl;
2980 struct BlacklistCheck *bc;
2981 struct NeighbourList *n;
2986 if (bl->client == client)
2989 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2994 bl = GNUNET_malloc (sizeof (struct Blacklisters));
2995 bl->client = client;
2996 GNUNET_SERVER_client_keep (client);
2997 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
2998 /* confirm that all existing connections are OK! */
3002 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3003 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3005 bc->do_hello = GNUNET_NO;
3006 bc->cont = &confirm_or_drop_neighbour;
3009 if (n == neighbours) /* all would wait for the same client, no need to
3010 create more than just the first task right now */
3011 bc->task = GNUNET_SCHEDULER_add_now (sched,
3012 &do_blacklist_check,
3020 * Handle a request to blacklist a peer.
3022 * @param cls closure (always NULL)
3023 * @param client identification of the client
3024 * @param message the actual message
3027 handle_blacklist_reply (void *cls,
3028 struct GNUNET_SERVER_Client *client,
3029 const struct GNUNET_MessageHeader *message)
3031 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
3032 struct Blacklisters *bl;
3033 struct BlacklistCheck *bc;
3036 while ( (bl != NULL) &&
3037 (bl->client != client) )
3041 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3046 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
3048 bc->cont (bc->cont_cls, NULL);
3049 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
3054 bc->bl_pos = bc->bl_pos->next;
3055 bc->task = GNUNET_SCHEDULER_add_now (sched,
3056 &do_blacklist_check,
3059 /* check if any other bc's are waiting for this blacklister */
3063 if ( (bc->bl_pos == bl) &&
3064 (GNUNET_SCHEDULER_NO_TASK == bc->task) )
3065 bc->task = GNUNET_SCHEDULER_add_now (sched,
3066 &do_blacklist_check,
3074 * Send periodic PING messages to a given foreign address.
3076 * @param cls our 'struct PeriodicValidationContext*'
3077 * @param tc task context
3080 send_periodic_ping (void *cls,
3081 const struct GNUNET_SCHEDULER_TaskContext *tc)
3083 struct ForeignAddressList *peer_address = cls;
3084 struct TransportPlugin *tp;
3085 struct ValidationEntry *va;
3086 struct NeighbourList *neighbour;
3087 struct TransportPingMessage ping;
3088 struct CheckAddressExistsClosure caec;
3090 uint16_t hello_size;
3093 peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3094 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3096 tp = peer_address->ready_list->plugin;
3097 neighbour = peer_address->ready_list->neighbour;
3098 if (GNUNET_YES != neighbour->public_key_valid)
3100 /* no public key yet, try again later */
3101 schedule_next_ping (peer_address);
3104 caec.addr = peer_address->addr;
3105 caec.addrlen = peer_address->addrlen;
3106 caec.tname = tp->short_name;
3107 caec.session = peer_address->session;
3108 caec.exists = GNUNET_NO;
3109 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3110 &check_address_exists,
3112 if (caec.exists == GNUNET_YES)
3114 /* During validation attempts we will likely trigger the other
3115 peer trying to validate our address which in turn will cause
3116 it to send us its HELLO, so we expect to hit this case rather
3117 frequently. Only print something if we are very verbose. */
3118 #if DEBUG_TRANSPORT > 1
3119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3120 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3121 (peer_address->addr != NULL)
3122 ? a2s (tp->short_name,
3124 peer_address->addrlen)
3127 GNUNET_i2s (&neighbour->id));
3129 schedule_next_ping (peer_address);
3132 va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3133 va->transport_name = GNUNET_strdup (tp->short_name);
3134 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3136 va->send_time = GNUNET_TIME_absolute_get();
3137 va->session = peer_address->session;
3138 if (peer_address->addr != NULL)
3140 va->addr = (const void*) &va[1];
3141 memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3142 va->addrlen = peer_address->addrlen;
3144 memcpy(&va->publicKey,
3145 &neighbour->publicKey,
3146 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3148 va->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
3149 HELLO_VERIFICATION_TIMEOUT,
3150 &timeout_hello_validation,
3152 GNUNET_CONTAINER_multihashmap_put (validation_map,
3153 &neighbour->id.hashPubKey,
3155 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3156 hello_size = GNUNET_HELLO_size(our_hello);
3157 tsize = sizeof(struct TransportPingMessage) + hello_size;
3158 message_buf = GNUNET_malloc(tsize);
3159 ping.challenge = htonl(va->challenge);
3160 ping.header.size = htons(sizeof(struct TransportPingMessage));
3161 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3162 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3163 memcpy(message_buf, our_hello, hello_size);
3164 memcpy(&message_buf[hello_size],
3166 sizeof(struct TransportPingMessage));
3167 #if DEBUG_TRANSPORT_REVALIDATION
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3169 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
3170 (peer_address->addr != NULL)
3171 ? a2s (peer_address->plugin->short_name,
3173 peer_address->addrlen)
3176 GNUNET_i2s (&neighbour->id),
3177 "HELLO", hello_size,
3178 "PING", sizeof (struct TransportPingMessage));
3180 GNUNET_STATISTICS_update (stats,
3181 gettext_noop ("# PING messages sent for re-validation"),
3184 transmit_to_peer (NULL, peer_address,
3185 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3186 HELLO_VERIFICATION_TIMEOUT,
3188 GNUNET_YES, neighbour);
3189 GNUNET_free(message_buf);
3190 schedule_next_ping (peer_address);
3195 * Schedule the job that will cause us to send a PING to the
3196 * foreign address to evaluate its validity and latency.
3198 * @param fal address to PING
3201 schedule_next_ping (struct ForeignAddressList *fal)
3203 struct GNUNET_TIME_Relative delay;
3205 if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3207 delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3208 delay.value /= 2; /* do before expiration */
3209 delay = GNUNET_TIME_relative_min (delay,
3210 LATENCY_EVALUATION_MAX_DELAY);
3211 if (GNUNET_YES != fal->estimated)
3213 delay = GNUNET_TIME_UNIT_ZERO;
3214 fal->estimated = GNUNET_YES;
3216 if (GNUNET_YES == fal->connected)
3218 delay = GNUNET_TIME_relative_min (delay,
3219 CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3221 /* FIXME: also adjust delay based on how close the last
3222 observed latency is to the latency of the best alternative */
3223 /* bound how fast we can go */
3224 delay = GNUNET_TIME_relative_max (delay,
3225 GNUNET_TIME_UNIT_SECONDS);
3226 /* randomize a bit (to avoid doing all at the same time) */
3227 delay.value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3228 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(sched,
3230 &send_periodic_ping,
3238 * Function that will be called if we receive some payload
3239 * from another peer.
3241 * @param message the payload
3242 * @param n peer who claimed to be the sender
3245 handle_payload_message (const struct GNUNET_MessageHeader *message,
3246 struct NeighbourList *n)
3248 struct InboundMessage *im;
3249 struct TransportClient *cpos;
3252 msize = ntohs (message->size);
3253 if (n->received_pong == GNUNET_NO)
3255 GNUNET_free_non_null (n->pre_connect_message_buffer);
3256 n->pre_connect_message_buffer = GNUNET_malloc (msize);
3257 memcpy (n->pre_connect_message_buffer, message, msize);
3261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3262 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
3263 ntohs (message->type),
3264 ntohs (message->size),
3265 GNUNET_i2s (&n->id));
3267 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3270 n->quota_violation_count++;
3272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3273 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
3274 n->in_tracker.available_bytes_per_s__,
3275 n->quota_violation_count);
3277 /* Discount 32k per violation */
3278 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3283 if (n->quota_violation_count > 0)
3285 /* try to add 32k back */
3286 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3288 n->quota_violation_count--;
3291 GNUNET_STATISTICS_update (stats,
3292 gettext_noop ("# payload received from other peers"),
3295 /* transmit message to all clients */
3296 im = GNUNET_malloc (sizeof (struct InboundMessage) + msize);
3297 im->header.size = htons (sizeof (struct InboundMessage) + msize);
3298 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3299 im->latency = GNUNET_TIME_relative_hton (n->latency);
3301 im->distance = ntohl(n->distance);
3302 memcpy (&im[1], message, msize);
3304 while (cpos != NULL)
3306 transmit_to_client (cpos, &im->header, GNUNET_YES);
3314 * Iterator over hash map entries. Checks if the given validation
3315 * entry is for the same challenge as what is given in the PONG.
3317 * @param cls the 'struct TransportPongMessage*'
3318 * @param key peer identity
3319 * @param value value in the hash map ('struct ValidationEntry')
3320 * @return GNUNET_YES if we should continue to
3321 * iterate (mismatch), GNUNET_NO if not (entry matched)
3324 check_pending_validation (void *cls,
3325 const GNUNET_HashCode * key,
3328 const struct TransportPongMessage *pong = cls;
3329 struct ValidationEntry *ve = value;
3330 struct AddValidatedAddressContext avac;
3331 unsigned int challenge = ntohl(pong->challenge);
3332 struct GNUNET_HELLO_Message *hello;
3333 struct GNUNET_PeerIdentity target;
3334 struct NeighbourList *n;
3335 struct ForeignAddressList *fal;
3336 struct GNUNET_MessageHeader *prem;
3338 if (ve->challenge != challenge)
3343 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PING,
3348 GNUNET_break_op (0);
3354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3355 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
3358 ? a2s (ve->transport_name,
3359 (const struct sockaddr *) ve->addr,
3362 ve->transport_name);
3364 GNUNET_STATISTICS_update (stats,
3365 gettext_noop ("# address validation successes"),
3368 /* create the updated HELLO */
3369 GNUNET_CRYPTO_hash (&ve->publicKey,
3370 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3371 &target.hashPubKey);
3372 if (ve->addr != NULL)
3374 avac.done = GNUNET_NO;
3376 hello = GNUNET_HELLO_create (&ve->publicKey,
3377 &add_validated_address,
3379 GNUNET_PEERINFO_add_peer (peerinfo,
3381 GNUNET_free (hello);
3383 n = find_neighbour (&target);
3386 n->publicKey = ve->publicKey;
3387 n->public_key_valid = GNUNET_YES;
3388 fal = add_peer_address (n,
3393 GNUNET_assert (fal != NULL);
3394 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
3395 fal->validated = GNUNET_YES;
3396 mark_address_connected (fal);
3397 GNUNET_STATISTICS_update (stats,
3398 gettext_noop ("# peer addresses considered valid"),
3401 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
3402 schedule_next_ping (fal);
3403 if (n->latency.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
3404 n->latency = fal->latency;
3406 n->latency.value = (fal->latency.value + n->latency.value) / 2;
3408 n->distance = fal->distance;
3409 if (GNUNET_NO == n->received_pong)
3411 n->received_pong = GNUNET_YES;
3412 notify_clients_connect (&target, n->latency, n->distance);
3413 if (NULL != (prem = n->pre_connect_message_buffer))
3415 n->pre_connect_message_buffer = NULL;
3416 handle_payload_message (prem, n);
3420 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
3422 GNUNET_SCHEDULER_cancel (sched,
3424 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
3425 try_transmission_to_peer (n);
3429 /* clean up validation entry */
3430 GNUNET_assert (GNUNET_YES ==
3431 GNUNET_CONTAINER_multihashmap_remove (validation_map,
3434 abort_validation (NULL, NULL, ve);
3440 * Function that will be called if we receive a validation
3441 * of an address challenge that we transmitted to another
3442 * peer. Note that the validation should only be considered
3443 * acceptable if the challenge matches AND if the sender
3444 * address is at least a plausible address for this peer
3445 * (otherwise we may be seeing a MiM attack).
3447 * @param cls closure
3448 * @param message the pong message
3449 * @param peer who responded to our challenge
3450 * @param sender_address string describing our sender address (as observed
3451 * by the other peer in binary format)
3452 * @param sender_address_len number of bytes in 'sender_address'
3455 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
3456 const struct GNUNET_PeerIdentity *peer,
3457 const char *sender_address,
3458 size_t sender_address_len)
3460 #if DEBUG_TRANSPORT > 1
3461 /* we get tons of these that just get discarded, only log
3462 if we are quite verbose */
3463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3464 "Receiving `%s' message from `%4s'.\n", "PONG",
3467 GNUNET_STATISTICS_update (stats,
3468 gettext_noop ("# PONG messages received"),
3471 if (GNUNET_SYSERR !=
3472 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
3474 &check_pending_validation,
3477 /* This is *expected* to happen a lot since we send
3478 PONGs to *all* known addresses of the sender of
3479 the PING, so most likely we get multiple PONGs
3480 per PING, and all but the first PONG will end up
3481 here. So really we should not print anything here
3482 unless we want to be very, very verbose... */
3483 #if DEBUG_TRANSPORT > 2
3484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3485 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
3497 * Try to validate a neighbour's address by sending him our HELLO and a PING.
3499 * @param cls the 'struct ValidationEntry*'
3500 * @param neighbour neighbour to validate, NULL if validation failed
3503 transmit_hello_and_ping (void *cls,
3504 struct NeighbourList *neighbour)
3506 struct ValidationEntry *va = cls;
3507 struct ForeignAddressList *peer_address;
3508 struct TransportPingMessage ping;
3509 uint16_t hello_size;
3512 struct GNUNET_PeerIdentity id;
3514 GNUNET_CRYPTO_hash (&va->publicKey,
3515 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3517 if (neighbour == NULL)
3519 /* FIXME: stats... */
3520 GNUNET_break (GNUNET_OK ==
3521 GNUNET_CONTAINER_multihashmap_remove (validation_map,
3524 abort_validation (NULL, NULL, va);
3527 neighbour->publicKey = va->publicKey;
3528 neighbour->public_key_valid = GNUNET_YES;
3529 peer_address = add_peer_address (neighbour,
3530 va->transport_name, NULL,
3531 (const void*) &va[1],
3533 if (peer_address == NULL)
3535 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3536 "Failed to add peer `%4s' for plugin `%s'\n",
3537 GNUNET_i2s (&neighbour->id),
3538 va->transport_name);
3539 GNUNET_break (GNUNET_OK ==
3540 GNUNET_CONTAINER_multihashmap_remove (validation_map,
3543 abort_validation (NULL, NULL, va);
3546 hello_size = GNUNET_HELLO_size(our_hello);
3547 tsize = sizeof(struct TransportPingMessage) + hello_size;
3548 message_buf = GNUNET_malloc(tsize);
3549 ping.challenge = htonl(va->challenge);
3550 ping.header.size = htons(sizeof(struct TransportPingMessage));
3551 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3552 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3553 memcpy(message_buf, our_hello, hello_size);
3554 memcpy(&message_buf[hello_size],
3556 sizeof(struct TransportPingMessage));
3558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3559 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
3560 a2s (va->transport_name,
3561 (const void*) &va[1], va->addrlen),
3563 GNUNET_i2s (&neighbour->id),
3564 "HELLO", hello_size,
3565 "PING", sizeof (struct TransportPingMessage));
3567 GNUNET_STATISTICS_update (stats,
3568 gettext_noop ("# PING messages sent for initial validation"),
3571 transmit_to_peer (NULL, peer_address,
3572 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3573 HELLO_VERIFICATION_TIMEOUT,
3575 GNUNET_YES, neighbour);
3576 GNUNET_free(message_buf);
3581 * Check if the given address is already being validated; if not,
3582 * append the given address to the list of entries that are being be
3583 * validated and initiate validation.
3585 * @param cls closure ('struct CheckHelloValidatedContext *')
3586 * @param tname name of the transport
3587 * @param expiration expiration time
3588 * @param addr the address
3589 * @param addrlen length of the address
3590 * @return GNUNET_OK (always)
3593 run_validation (void *cls,
3595 struct GNUNET_TIME_Absolute expiration,
3599 struct CheckHelloValidatedContext *chvc = cls;
3600 struct GNUNET_PeerIdentity id;
3601 struct TransportPlugin *tp;
3602 struct ValidationEntry *va;
3603 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
3604 struct CheckAddressExistsClosure caec;
3605 struct OwnAddressList *oal;
3607 GNUNET_assert (addr != NULL);
3608 GNUNET_STATISTICS_update (stats,
3609 gettext_noop ("# peer addresses scheduled for validation"),
3612 tp = find_transport (tname);
3615 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
3616 GNUNET_ERROR_TYPE_BULK,
3618 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
3620 GNUNET_STATISTICS_update (stats,
3621 gettext_noop ("# peer addresses not validated (plugin not available)"),
3626 /* check if this is one of our own addresses */
3627 oal = tp->addresses;
3630 if ( (oal->addrlen == addrlen) &&
3631 (0 == memcmp (oal->addr,
3635 /* not plausible, this address is equivalent to our own address! */
3636 GNUNET_STATISTICS_update (stats,
3637 gettext_noop ("# peer addresses not validated (loopback)"),
3644 GNUNET_HELLO_get_key (chvc->hello, &pk);
3645 GNUNET_CRYPTO_hash (&pk,
3647 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3650 if (is_blacklisted(&id, tp))
3653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3654 "Attempted to validate blacklisted peer `%s' using `%s'!\n",
3662 caec.addrlen = addrlen;
3663 caec.session = NULL;
3665 caec.exists = GNUNET_NO;
3666 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3667 &check_address_exists,
3669 if (caec.exists == GNUNET_YES)
3671 /* During validation attempts we will likely trigger the other
3672 peer trying to validate our address which in turn will cause
3673 it to send us its HELLO, so we expect to hit this case rather
3674 frequently. Only print something if we are very verbose. */
3675 #if DEBUG_TRANSPORT > 1
3676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3677 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3678 a2s (tname, addr, addrlen),
3682 GNUNET_STATISTICS_update (stats,
3683 gettext_noop ("# peer addresses not validated (in progress)"),
3688 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
3691 va->transport_name = GNUNET_strdup (tname);
3692 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3694 va->send_time = GNUNET_TIME_absolute_get();
3695 va->addr = (const void*) &va[1];
3696 memcpy (&va[1], addr, addrlen);
3697 va->addrlen = addrlen;
3698 GNUNET_HELLO_get_key (chvc->hello,
3700 va->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
3701 HELLO_VERIFICATION_TIMEOUT,
3702 &timeout_hello_validation,
3704 GNUNET_CONTAINER_multihashmap_put (validation_map,
3707 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3708 setup_peer_check_blacklist (&id, GNUNET_NO,
3709 &transmit_hello_and_ping,
3716 * Check if addresses in validated hello "h" overlap with
3717 * those in "chvc->hello" and validate the rest.
3719 * @param cls closure
3720 * @param peer id of the peer, NULL for last call
3721 * @param h hello message for the peer (can be NULL)
3722 * @param trust amount of trust we have in the peer (not used)
3725 check_hello_validated (void *cls,
3726 const struct GNUNET_PeerIdentity *peer,
3727 const struct GNUNET_HELLO_Message *h,
3730 struct CheckHelloValidatedContext *chvc = cls;
3731 struct GNUNET_HELLO_Message *plain_hello;
3732 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
3733 struct GNUNET_PeerIdentity target;
3734 struct NeighbourList *n;
3739 if (GNUNET_NO == chvc->hello_known)
3741 /* notify PEERINFO about the peer now, so that we at least
3742 have the public key if some other component needs it */
3743 GNUNET_HELLO_get_key (chvc->hello, &pk);
3744 GNUNET_CRYPTO_hash (&pk,
3745 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3746 &target.hashPubKey);
3747 plain_hello = GNUNET_HELLO_create (&pk,
3750 GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
3751 GNUNET_free (plain_hello);
3753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3754 "PEERINFO had no `%s' message for peer `%4s', full validation needed.\n",
3756 GNUNET_i2s (&target));
3758 GNUNET_STATISTICS_update (stats,
3759 gettext_noop ("# new HELLOs requiring full validation"),
3762 GNUNET_HELLO_iterate_addresses (chvc->hello,
3769 GNUNET_STATISTICS_update (stats,
3770 gettext_noop ("# duplicate HELLO (peer known)"),
3775 if (chvc->ve_count == 0)
3777 GNUNET_CONTAINER_DLL_remove (chvc_head,
3787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3788 "PEERINFO had `%s' message for peer `%4s', validating only new addresses.\n",
3792 chvc->hello_known = GNUNET_YES;
3793 n = find_neighbour (peer);
3796 GNUNET_HELLO_iterate_addresses (h,
3798 &add_to_foreign_address_list,
3800 try_transmission_to_peer (n);
3804 GNUNET_STATISTICS_update (stats,
3805 gettext_noop ("# no existing neighbour record (validating HELLO)"),
3809 GNUNET_STATISTICS_update (stats,
3810 gettext_noop ("# HELLO validations (update case)"),
3813 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
3815 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
3822 * Process HELLO-message.
3824 * @param plugin transport involved, may be NULL
3825 * @param message the actual message
3826 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
3829 process_hello (struct TransportPlugin *plugin,
3830 const struct GNUNET_MessageHeader *message)
3833 struct GNUNET_PeerIdentity target;
3834 const struct GNUNET_HELLO_Message *hello;
3835 struct CheckHelloValidatedContext *chvc;
3836 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
3838 hsize = ntohs (message->size);
3839 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
3840 (hsize < sizeof (struct GNUNET_MessageHeader)))
3843 return GNUNET_SYSERR;
3845 GNUNET_STATISTICS_update (stats,
3846 gettext_noop ("# HELLOs received for validation"),
3849 /* first, check if load is too high */
3850 if (GNUNET_SCHEDULER_get_load (sched,
3851 GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
3853 GNUNET_STATISTICS_update (stats,
3854 gettext_noop ("# HELLOs ignored due to high load"),
3859 hello = (const struct GNUNET_HELLO_Message *) message;
3860 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
3862 GNUNET_break_op (0);
3863 return GNUNET_SYSERR;
3865 GNUNET_CRYPTO_hash (&publicKey,
3866 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3867 &target.hashPubKey);
3868 if (0 == memcmp (&my_identity,
3870 sizeof (struct GNUNET_PeerIdentity)))
3872 GNUNET_STATISTICS_update (stats,
3873 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
3879 while (NULL != chvc)
3881 if (GNUNET_HELLO_equals (hello,
3883 GNUNET_TIME_absolute_get ()).value > 0)
3886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3887 "Received duplicate `%s' message for `%4s'; ignored\n",
3889 GNUNET_i2s (&target));
3891 return GNUNET_OK; /* validation already pending */
3893 if (GNUNET_HELLO_size(hello) == GNUNET_HELLO_size (chvc->hello))
3894 GNUNET_break (0 != memcmp (hello, chvc->hello,
3895 GNUNET_HELLO_size(hello)));
3899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3900 "Starting validation of `%s' message for `%4s' of size %u\n",
3902 GNUNET_i2s (&target),
3903 GNUNET_HELLO_size(hello));
3905 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
3907 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
3908 memcpy (&chvc[1], hello, hsize);
3909 GNUNET_CONTAINER_DLL_insert (chvc_head,
3912 /* finally, check if HELLO was previously validated
3913 (continuation will then schedule actual validation) */
3914 chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
3917 HELLO_VERIFICATION_TIMEOUT,
3918 &check_hello_validated, chvc);
3924 * The peer specified by the given neighbour has timed-out or a plugin
3925 * has disconnected. We may either need to do nothing (other plugins
3926 * still up), or trigger a full disconnect and clean up. This
3927 * function updates our state and does the necessary notifications.
3928 * Also notifies our clients that the neighbour is now officially
3931 * @param n the neighbour list entry for the peer
3932 * @param check should we just check if all plugins
3933 * disconnected or must we ask all plugins to
3937 disconnect_neighbour (struct NeighbourList *n, int check)
3939 struct ReadyList *rpos;
3940 struct NeighbourList *npos;
3941 struct NeighbourList *nprev;
3942 struct MessageQueue *mq;
3943 struct ForeignAddressList *peer_addresses;
3944 struct ForeignAddressList *peer_pos;
3946 if (GNUNET_YES == check)
3949 while (NULL != rpos)
3951 peer_addresses = rpos->addresses;
3952 while (peer_addresses != NULL)
3954 if (GNUNET_YES == peer_addresses->connected)
3955 return; /* still connected */
3956 peer_addresses = peer_addresses->next;
3962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
3963 "Disconnecting from `%4s'\n",
3964 GNUNET_i2s (&n->id));
3966 /* remove n from neighbours list */
3969 while ((npos != NULL) && (npos != n))
3974 GNUNET_assert (npos != NULL);
3976 neighbours = n->next;
3978 nprev->next = n->next;
3980 /* notify all clients about disconnect */
3981 if (GNUNET_YES == n->received_pong)
3982 notify_clients_disconnect (&n->id);
3984 /* clean up all plugins, cancel connections and pending transmissions */
3985 while (NULL != (rpos = n->plugins))
3987 n->plugins = rpos->next;
3988 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
3989 while (rpos->addresses != NULL)
3991 peer_pos = rpos->addresses;
3992 rpos->addresses = peer_pos->next;
3993 if (peer_pos->connected == GNUNET_YES)
3994 GNUNET_STATISTICS_update (stats,
3995 gettext_noop ("# connected addresses"),
3998 if (GNUNET_YES == peer_pos->validated)
3999 GNUNET_STATISTICS_update (stats,
4000 gettext_noop ("# peer addresses considered valid"),
4003 if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
4005 GNUNET_SCHEDULER_cancel (sched,
4006 peer_pos->revalidate_task);
4007 peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
4009 GNUNET_free(peer_pos);
4014 /* free all messages on the queue */
4015 while (NULL != (mq = n->messages_head))
4017 GNUNET_STATISTICS_update (stats,
4018 gettext_noop ("# bytes in message queue for other peers"),
4019 - (int64_t) mq->message_buf_size,
4021 GNUNET_STATISTICS_update (stats,
4022 gettext_noop ("# bytes discarded due to disconnect"),
4023 mq->message_buf_size,
4025 GNUNET_CONTAINER_DLL_remove (n->messages_head,
4028 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4030 sizeof(struct GNUNET_PeerIdentity)));
4033 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
4035 GNUNET_SCHEDULER_cancel (sched, n->timeout_task);
4036 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4038 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4040 GNUNET_SCHEDULER_cancel (sched, n->retry_task);
4041 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4043 if (n->piter != NULL)
4045 GNUNET_PEERINFO_iterate_cancel (n->piter);
4048 /* finally, free n itself */
4049 GNUNET_STATISTICS_update (stats,
4050 gettext_noop ("# active neighbours"),
4053 GNUNET_free_non_null (n->pre_connect_message_buffer);
4059 * We have received a PING message from someone. Need to send a PONG message
4060 * in response to the peer by any means necessary.
4063 handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
4064 const struct GNUNET_PeerIdentity *peer,
4065 const char *sender_address,
4066 uint16_t sender_address_len)
4068 struct TransportPlugin *plugin = cls;
4069 struct TransportPingMessage *ping;
4070 struct TransportPongMessage *pong;
4071 struct NeighbourList *n;
4072 struct ReadyList *rl;
4073 struct ForeignAddressList *fal;
4075 if (ntohs (message->size) != sizeof (struct TransportPingMessage))
4077 GNUNET_break_op (0);
4078 return GNUNET_SYSERR;
4081 ping = (struct TransportPingMessage *) message;
4082 if (0 != memcmp (&ping->target,
4083 plugin->env.my_identity,
4084 sizeof (struct GNUNET_PeerIdentity)))
4086 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4087 _("Received `%s' message not destined for me!\n"),
4089 return GNUNET_SYSERR;
4092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4093 "Processing `%s' from `%s'\n",
4095 (sender_address != NULL)
4096 ? a2s (plugin->short_name,
4097 (const struct sockaddr *)sender_address,
4101 GNUNET_STATISTICS_update (stats,
4102 gettext_noop ("# PING messages received"),
4105 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len);
4106 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len);
4107 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
4108 pong->purpose.size =
4109 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4111 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + sender_address_len);
4112 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PING);
4113 pong->challenge = ping->challenge;
4114 pong->addrlen = htons(sender_address_len);
4115 memcpy(&pong->signer,
4117 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
4118 if (sender_address != NULL)
4119 memcpy (&pong[1], sender_address, sender_address_len);
4121 GNUNET_assert (GNUNET_OK ==
4122 GNUNET_CRYPTO_rsa_sign (my_private_key,
4123 &pong->purpose, &pong->signature));
4125 n = find_neighbour(peer);
4126 GNUNET_assert (n != NULL);
4127 /* first try reliable response transmission */
4131 fal = rl->addresses;
4134 if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
4137 ntohs (pong->header.size),
4138 TRANSPORT_PONG_PRIORITY,
4139 HELLO_VERIFICATION_TIMEOUT,
4147 GNUNET_STATISTICS_update (stats,
4148 gettext_noop ("# PONGs unicast via reliable transport"),
4158 /* no reliable method found, do multicast */
4159 GNUNET_STATISTICS_update (stats,
4160 gettext_noop ("# PONGs multicast to all available addresses"),
4166 fal = rl->addresses;
4169 transmit_to_peer(NULL, fal,
4170 TRANSPORT_PONG_PRIORITY,
4171 HELLO_VERIFICATION_TIMEOUT,
4173 ntohs(pong->header.size),
4186 * Function called by the plugin for each received message.
4187 * Update data volumes, possibly notify plugins about
4188 * reducing the rate at which they read from the socket
4189 * and generally forward to our receive callback.
4191 * @param cls the "struct TransportPlugin *" we gave to the plugin
4192 * @param peer (claimed) identity of the other peer
4193 * @param message the message, NULL if we only care about
4194 * learning about the delay until we should receive again
4195 * @param distance in overlay hops; use 1 unless DV (or 0 if message == NULL)
4196 * @param session identifier used for this session (can be NULL)
4197 * @param sender_address binary address of the sender (if observed)
4198 * @param sender_address_len number of bytes in sender_address
4199 * @return how long the plugin should wait until receiving more data
4200 * (plugins that do not support this, can ignore the return value)
4202 static struct GNUNET_TIME_Relative
4203 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
4204 const struct GNUNET_MessageHeader *message,
4206 struct Session *session,
4207 const char *sender_address,
4208 uint16_t sender_address_len)
4210 struct TransportPlugin *plugin = cls;
4211 struct ReadyList *service_context;
4212 struct ForeignAddressList *peer_address;
4214 struct NeighbourList *n;
4215 struct GNUNET_TIME_Relative ret;
4217 if (is_blacklisted (peer, plugin))
4218 return GNUNET_TIME_UNIT_FOREVER_REL;
4220 n = find_neighbour (peer);
4222 n = setup_new_neighbour (peer, GNUNET_YES);
4223 service_context = n->plugins;
4224 while ((service_context != NULL) && (plugin != service_context->plugin))
4225 service_context = service_context->next;
4226 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
4227 peer_address = NULL;
4228 if (message != NULL)
4230 if ( (session != NULL) ||
4231 (sender_address != NULL) )
4232 peer_address = add_peer_address (n,
4236 sender_address_len);
4237 if (peer_address != NULL)
4239 peer_address->distance = distance;
4240 if (GNUNET_YES == peer_address->validated)
4241 mark_address_connected (peer_address);
4242 peer_address->timeout
4244 GNUNET_TIME_relative_to_absolute
4245 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
4246 schedule_next_ping (peer_address);
4248 /* update traffic received amount ... */
4249 msize = ntohs (message->size);
4250 GNUNET_STATISTICS_update (stats,
4251 gettext_noop ("# bytes received from other peers"),
4254 n->distance = distance;
4256 GNUNET_TIME_relative_to_absolute
4257 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
4258 GNUNET_SCHEDULER_cancel (sched,
4261 GNUNET_SCHEDULER_add_delayed (sched,
4262 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
4263 &neighbour_timeout_task, n);
4264 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
4266 /* dropping message due to frequent inbound volume violations! */
4267 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
4268 GNUNET_ERROR_TYPE_BULK,
4270 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
4271 n->in_tracker.available_bytes_per_s__,
4272 n->quota_violation_count);
4273 GNUNET_STATISTICS_update (stats,
4274 gettext_noop ("# bandwidth quota violations by other peers"),
4277 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
4280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4281 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
4282 ntohs (message->type),
4283 ntohs (message->size),
4286 switch (ntohs (message->type))
4288 case GNUNET_MESSAGE_TYPE_HELLO:
4289 GNUNET_STATISTICS_update (stats,
4290 gettext_noop ("# HELLO messages received from other peers"),
4293 process_hello (plugin, message);
4295 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
4296 handle_ping (plugin, message, peer, sender_address, sender_address_len);
4298 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
4299 handle_pong (plugin, message, peer, sender_address, sender_address_len);
4302 handle_payload_message (message, n);
4306 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
4309 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4310 "Throttling read (%llu bytes excess at %u b/s), waiting %llums before reading more.\n",
4311 (unsigned long long) n->in_tracker.consumption_since_last_update__,
4312 (unsigned int) n->in_tracker.available_bytes_per_s__,
4313 (unsigned long long) ret.value);
4314 GNUNET_STATISTICS_update (stats,
4315 gettext_noop ("# ms throttling suggested"),
4316 (int64_t) ret.value,
4323 * Handle START-message. This is the first message sent to us
4324 * by any client which causes us to add it to our list.
4326 * @param cls closure (always NULL)
4327 * @param client identification of the client
4328 * @param message the actual message
4331 handle_start (void *cls,
4332 struct GNUNET_SERVER_Client *client,
4333 const struct GNUNET_MessageHeader *message)
4335 struct TransportClient *c;
4336 struct ConnectInfoMessage cim;
4337 struct NeighbourList *n;
4340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4341 "Received `%s' request from client\n", "START");
4346 if (c->client == client)
4348 /* client already on our list! */
4350 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4355 c = GNUNET_malloc (sizeof (struct TransportClient));
4359 if (our_hello != NULL)
4362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4363 "Sending our own `%s' to new client\n", "HELLO");
4365 transmit_to_client (c,
4366 (const struct GNUNET_MessageHeader *) our_hello,
4368 /* tell new client about all existing connections */
4369 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
4370 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
4374 if (GNUNET_YES == n->received_pong)
4377 cim.latency = GNUNET_TIME_relative_hton (n->latency);
4378 cim.distance = htonl (n->distance);
4379 transmit_to_client (c, &cim.header, GNUNET_NO);
4384 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4389 * Handle HELLO-message.
4391 * @param cls closure (always NULL)
4392 * @param client identification of the client
4393 * @param message the actual message
4396 handle_hello (void *cls,
4397 struct GNUNET_SERVER_Client *client,
4398 const struct GNUNET_MessageHeader *message)
4402 GNUNET_STATISTICS_update (stats,
4403 gettext_noop ("# HELLOs received from clients"),
4406 ret = process_hello (NULL, message);
4407 GNUNET_SERVER_receive_done (client, ret);
4412 * Closure for 'transmit_client_message'; followed by
4413 * 'msize' bytes of the actual message.
4415 struct TransmitClientMessageContext
4418 * Client on whom's behalf we are sending.
4420 struct GNUNET_SERVER_Client *client;
4423 * Timeout for the transmission.
4425 struct GNUNET_TIME_Absolute timeout;
4433 * Size of the message in bytes.
4440 * Schedule transmission of a message we got from a client to a peer.
4442 * @param cls the 'struct TransmitClientMessageContext*'
4443 * @param n destination, or NULL on error (in that case, drop the message)
4446 transmit_client_message (void *cls,
4447 struct NeighbourList *n)
4449 struct TransmitClientMessageContext *tcmc = cls;
4450 struct TransportClient *tc;
4453 while ((tc != NULL) && (tc->client != tcmc->client))
4458 transmit_to_peer (tc, NULL, tcmc->priority,
4459 GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
4461 tcmc->msize, GNUNET_NO, n);
4463 GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
4464 GNUNET_SERVER_client_drop (tcmc->client);
4470 * Handle SEND-message.
4472 * @param cls closure (always NULL)
4473 * @param client identification of the client
4474 * @param message the actual message
4477 handle_send (void *cls,
4478 struct GNUNET_SERVER_Client *client,
4479 const struct GNUNET_MessageHeader *message)
4481 const struct OutboundMessage *obm;
4482 const struct GNUNET_MessageHeader *obmm;
4483 struct TransmitClientMessageContext *tcmc;
4487 size = ntohs (message->size);
4489 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
4492 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4495 GNUNET_STATISTICS_update (stats,
4496 gettext_noop ("# payload received for other peers"),
4499 obm = (const struct OutboundMessage *) message;
4500 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
4501 msize = size - sizeof (struct OutboundMessage);
4503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4504 "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
4505 "SEND", GNUNET_i2s (&obm->peer),
4509 tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
4510 tcmc->client = client;
4511 tcmc->priority = ntohl (obm->priority);
4512 tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
4513 tcmc->msize = msize;
4514 memcpy (&tcmc[1], obmm, msize);
4515 GNUNET_SERVER_client_keep (client);
4516 setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
4517 &transmit_client_message,
4523 * Handle SET_QUOTA-message.
4525 * @param cls closure (always NULL)
4526 * @param client identification of the client
4527 * @param message the actual message
4530 handle_set_quota (void *cls,
4531 struct GNUNET_SERVER_Client *client,
4532 const struct GNUNET_MessageHeader *message)
4534 const struct QuotaSetMessage *qsm =
4535 (const struct QuotaSetMessage *) message;
4536 struct NeighbourList *n;
4538 GNUNET_STATISTICS_update (stats,
4539 gettext_noop ("# SET QUOTA messages received"),
4542 n = find_neighbour (&qsm->peer);
4545 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4546 GNUNET_STATISTICS_update (stats,
4547 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
4553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4554 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
4556 (unsigned int) ntohl (qsm->quota.value__),
4557 (unsigned int) n->in_tracker.available_bytes_per_s__,
4558 GNUNET_i2s (&qsm->peer));
4560 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
4562 if (0 == ntohl (qsm->quota.value__))
4563 disconnect_neighbour (n, GNUNET_NO);
4564 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4569 * Take the given address and append it to the set of results send back to
4572 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
4573 * @param address the resolved name, NULL to indicate the last response
4576 transmit_address_to_client (void *cls, const char *address)
4578 struct GNUNET_SERVER_TransmitContext *tc = cls;
4581 if (NULL == address)
4584 slen = strlen (address) + 1;
4585 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
4586 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
4587 if (NULL == address)
4588 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
4593 * Handle AddressLookup-message.
4595 * @param cls closure (always NULL)
4596 * @param client identification of the client
4597 * @param message the actual message
4600 handle_address_lookup (void *cls,
4601 struct GNUNET_SERVER_Client *client,
4602 const struct GNUNET_MessageHeader *message)
4604 const struct AddressLookupMessage *alum;
4605 struct TransportPlugin *lsPlugin;
4606 const char *nameTransport;
4607 const char *address;
4609 struct GNUNET_SERVER_TransmitContext *tc;
4610 struct GNUNET_TIME_Absolute timeout;
4611 struct GNUNET_TIME_Relative rtimeout;
4614 size = ntohs (message->size);
4615 if (size < sizeof (struct AddressLookupMessage))
4617 GNUNET_break_op (0);
4618 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4621 alum = (const struct AddressLookupMessage *) message;
4622 uint32_t addressLen = ntohl (alum->addrlen);
4623 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
4625 GNUNET_break_op (0);
4626 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4629 address = (const char *) &alum[1];
4630 nameTransport = (const char *) &address[addressLen];
4632 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
4634 GNUNET_break_op (0);
4635 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4638 timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
4639 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
4640 numeric = ntohl (alum->numeric_only);
4641 lsPlugin = find_transport (nameTransport);
4642 if (NULL == lsPlugin)
4644 tc = GNUNET_SERVER_transmit_context_create (client);
4645 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
4646 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
4647 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
4650 tc = GNUNET_SERVER_transmit_context_create (client);
4651 lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
4653 address, addressLen,
4656 &transmit_address_to_client, tc);
4661 * Setup the environment for this plugin.
4664 create_environment (struct TransportPlugin *plug)
4666 plug->env.cfg = cfg;
4667 plug->env.sched = sched;
4668 plug->env.my_identity = &my_identity;
4669 plug->env.cls = plug;
4670 plug->env.receive = &plugin_env_receive;
4671 plug->env.notify_address = &plugin_env_notify_address;
4672 plug->env.session_end = &plugin_env_session_end;
4673 plug->env.max_connections = max_connect_per_transport;
4674 plug->env.stats = stats;
4679 * Start the specified transport (load the plugin).
4682 start_transport (struct GNUNET_SERVER_Handle *server,
4685 struct TransportPlugin *plug;
4688 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4689 _("Loading `%s' transport plugin\n"), name);
4690 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
4691 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
4692 create_environment (plug);
4693 plug->short_name = GNUNET_strdup (name);
4694 plug->lib_name = libname;
4695 plug->next = plugins;
4697 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
4698 if (plug->api == NULL)
4700 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4701 _("Failed to load transport plugin for `%s'\n"), name);
4702 GNUNET_free (plug->short_name);
4703 plugins = plug->next;
4704 GNUNET_free (libname);
4711 * Called whenever a client is disconnected. Frees our
4712 * resources associated with that client.
4714 * @param cls closure
4715 * @param client identification of the client
4718 client_disconnect_notification (void *cls,
4719 struct GNUNET_SERVER_Client *client)
4721 struct TransportClient *pos;
4722 struct TransportClient *prev;
4723 struct ClientMessageQueueEntry *mqe;
4724 struct Blacklisters *bl;
4725 struct BlacklistCheck *bc;
4730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4731 "Client disconnected, cleaning up.\n");
4733 /* clean up blacklister */
4737 if (bl->client == client)
4742 if (bc->bl_pos == bl)
4744 bc->bl_pos = bl->next;
4747 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
4750 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
4751 bc->task = GNUNET_SCHEDULER_add_now (sched,
4752 &do_blacklist_check,
4758 GNUNET_CONTAINER_DLL_remove (bl_head,
4761 GNUNET_SERVER_client_drop (bl->client);
4767 /* clean up 'normal' clients */
4770 while ((pos != NULL) && (pos->client != client))
4777 while (NULL != (mqe = pos->message_queue_head))
4779 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
4780 pos->message_queue_tail,
4782 pos->message_count--;
4786 clients = pos->next;
4788 prev->next = pos->next;
4789 if (GNUNET_YES == pos->tcs_pending)
4794 if (pos->th != NULL)
4796 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
4799 GNUNET_break (0 == pos->message_count);
4805 * Function called when the service shuts down. Unloads our plugins
4806 * and cancels pending validations.
4808 * @param cls closure, unused
4809 * @param tc task context (unused)
4812 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4814 struct TransportPlugin *plug;
4815 struct OwnAddressList *al;
4816 struct CheckHelloValidatedContext *chvc;
4818 while (neighbours != NULL)
4819 disconnect_neighbour (neighbours, GNUNET_NO);
4821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4822 "Transport service is unloading plugins...\n");
4824 while (NULL != (plug = plugins))
4826 plugins = plug->next;
4827 if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
4829 GNUNET_SCHEDULER_cancel (plug->env.sched,
4830 plug->address_update_task);
4831 plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
4833 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
4834 GNUNET_free (plug->lib_name);
4835 GNUNET_free (plug->short_name);
4836 while (NULL != (al = plug->addresses))
4838 plug->addresses = al->next;
4843 if (my_private_key != NULL)
4844 GNUNET_CRYPTO_rsa_key_free (my_private_key);
4845 GNUNET_free_non_null (our_hello);
4847 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
4850 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
4851 validation_map = NULL;
4853 /* free 'chvc' data structure */
4854 while (NULL != (chvc = chvc_head))
4856 chvc_head = chvc->next;
4857 if (chvc->piter != NULL)
4858 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
4861 GNUNET_assert (chvc->ve_count == 0);
4868 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
4871 if (peerinfo != NULL)
4873 GNUNET_PEERINFO_disconnect (peerinfo);
4876 /* Can we assume those are gone by now, or do we need to clean up
4878 GNUNET_break (bl_head == NULL);
4879 GNUNET_break (bc_head == NULL);
4884 * Initiate transport service.
4886 * @param cls closure
4887 * @param s scheduler to use
4888 * @param serv the initialized server
4889 * @param c configuration to use
4893 struct GNUNET_SCHEDULER_Handle *s,
4894 struct GNUNET_SERVER_Handle *serv,
4895 const struct GNUNET_CONFIGURATION_Handle *c)
4897 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
4898 {&handle_start, NULL,
4899 GNUNET_MESSAGE_TYPE_TRANSPORT_START, 0},
4900 {&handle_hello, NULL,
4901 GNUNET_MESSAGE_TYPE_HELLO, 0},
4902 {&handle_send, NULL,
4903 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
4904 {&handle_set_quota, NULL,
4905 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
4906 {&handle_address_lookup, NULL,
4907 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
4909 {&handle_blacklist_init, NULL,
4910 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
4911 {&handle_blacklist_reply, NULL,
4912 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
4918 unsigned long long tneigh;
4923 stats = GNUNET_STATISTICS_create (sched, "transport", cfg);
4924 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
4925 /* parse configuration */
4927 GNUNET_CONFIGURATION_get_value_number (c,
4932 GNUNET_CONFIGURATION_get_value_filename (c,
4934 "HOSTKEY", &keyfile)))
4936 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4938 ("Transport service is lacking key configuration settings. Exiting.\n"));
4939 GNUNET_SCHEDULER_shutdown (s);
4942 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
4945 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
4946 validation_map = NULL;
4949 max_connect_per_transport = (uint32_t) tneigh;
4950 peerinfo = GNUNET_PEERINFO_connect (sched, cfg);
4951 if (peerinfo == NULL)
4953 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4954 _("Could not access PEERINFO service. Exiting.\n"));
4955 GNUNET_SCHEDULER_shutdown (s);
4958 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
4961 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
4962 validation_map = NULL;
4963 GNUNET_free (keyfile);
4966 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4967 GNUNET_free (keyfile);
4968 if (my_private_key == NULL)
4970 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4972 ("Transport service could not access hostkey. Exiting.\n"));
4973 GNUNET_SCHEDULER_shutdown (s);
4976 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
4979 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
4980 validation_map = NULL;
4983 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4984 GNUNET_CRYPTO_hash (&my_public_key,
4985 sizeof (my_public_key), &my_identity.hashPubKey);
4986 /* setup notification */
4988 GNUNET_SERVER_disconnect_notify (server,
4989 &client_disconnect_notification, NULL);
4990 /* load plugins... */
4993 GNUNET_CONFIGURATION_get_value_string (c,
4994 "TRANSPORT", "PLUGINS", &plugs))
4996 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4997 _("Starting transport plugins `%s'\n"), plugs);
4998 pos = strtok (plugs, " ");
5001 start_transport (server, pos);
5003 pos = strtok (NULL, " ");
5005 GNUNET_free (plugs);
5007 GNUNET_SCHEDULER_add_delayed (sched,
5008 GNUNET_TIME_UNIT_FOREVER_REL,
5009 &shutdown_task, NULL);
5014 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport service ready.\n"));
5016 /* If we have a blacklist file, read from it */
5017 read_blacklist_file(cfg);
5018 /* process client requests */
5019 GNUNET_SERVER_add_handlers (server, handlers);
5024 * The main function for the transport service.
5026 * @param argc number of arguments from the command line
5027 * @param argv command line arguments
5028 * @return 0 ok, 1 on error
5031 main (int argc, char *const *argv)
5033 a2s (NULL, NULL, 0); /* make compiler happy */
5034 return (GNUNET_OK ==
5035 GNUNET_SERVICE_run (argc,
5038 GNUNET_SERVICE_OPTION_NONE,
5039 &run, NULL)) ? 0 : 1;
5042 /* end of gnunet-service-transport.c */