2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/gnunet-service-transport.c
23 * @brief low-level P2P messaging
24 * @author Christian Grothoff
28 #include "gnunet_client_lib.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "gnunet_plugin_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_service_lib.h"
38 #include "gnunet_signatures.h"
39 #include "gnunet_transport_plugin.h"
40 #include "transport.h"
45 #define DEBUG_BLACKLIST GNUNET_NO
47 #define DEBUG_PING_PONG GNUNET_NO
49 #define DEBUG_TRANSPORT_HELLO GNUNET_NO
51 #define DEBUG_ATS GNUNET_NO
53 #define VERBOSE_ATS GNUNET_NO
56 * Should we do some additional checks (to validate behavior
59 #define EXTRA_CHECKS GNUNET_YES
62 * How many messages can we have pending for a given client process
63 * before we start to drop incoming messages? We typically should
64 * have only one client and so this would be the primary buffer for
65 * messages, so the number should be chosen rather generously.
67 * The expectation here is that most of the time the queue is large
68 * enough so that a drop is virtually never required. Note that
69 * this value must be about as large as 'TOTAL_MSGS' in the
70 * 'test_transport_api_reliability.c', otherwise that testcase may
73 #define MAX_PENDING (128 * 1024)
76 * Size of the per-transport blacklist hash maps.
78 #define TRANSPORT_BLACKLIST_HT_SIZE 16
81 * How often should we try to reconnect to a peer using a particular
82 * transport plugin before giving up? Note that the plugin may be
83 * added back to the list after PLUGIN_RETRY_FREQUENCY expires.
85 #define MAX_CONNECT_RETRY 3
88 * Limit on the number of ready-to-run tasks when validating
89 * HELLOs. If more tasks are ready to run, we will drop
90 * HELLOs instead of validating them.
92 #define MAX_HELLO_LOAD 4
95 * How often must a peer violate bandwidth quotas before we start
96 * to simply drop its messages?
98 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
101 * How long until a HELLO verification attempt should time out?
102 * Must be rather small, otherwise a partially successful HELLO
103 * validation (some addresses working) might not be available
104 * before a client's request for a connection fails for good.
105 * Besides, if a single request to an address takes a long time,
106 * then the peer is unlikely worthwhile anyway.
108 #define HELLO_VERIFICATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
111 * How long is a PONG signature valid? We'll recycle a signature until
112 * 1/4 of this time is remaining. PONGs should expire so that if our
113 * external addresses change an adversary cannot replay them indefinitely.
114 * OTOH, we don't want to spend too much time generating PONG signatures,
115 * so they must have some lifetime to reduce our CPU usage.
117 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
120 * Priority to use for PONG messages.
122 #define TRANSPORT_PONG_PRIORITY 4
125 * How often do we re-add (cheaper) plugins to our list of plugins
126 * to try for a given connected peer?
128 #define PLUGIN_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
131 * After how long do we expire an address in a HELLO that we just
132 * validated? This value is also used for our own addresses when we
135 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
139 * How long before an existing address expires should we again try to
140 * validate it? Must be (significantly) smaller than
141 * HELLO_ADDRESS_EXPIRATION.
143 #define HELLO_REVALIDATION_START_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
146 * Maximum frequency for re-evaluating latencies for all transport addresses.
148 #define LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
151 * Maximum frequency for re-evaluating latencies for connected addresses.
153 #define CONNECTED_LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
155 #define VERY_BIG_DOUBLE_VALUE 100000000000LL
158 * List of addresses of other peers
160 struct ForeignAddressList
163 * This is a linked list.
165 struct ForeignAddressList *next;
168 * Which ready list does this entry belong to.
170 struct ReadyList *ready_list;
173 * How long until we auto-expire this address (unless it is
174 * re-confirmed by the transport)?
176 struct GNUNET_TIME_Absolute expires;
179 * Task used to re-validate addresses, updates latencies and
182 GNUNET_SCHEDULER_TaskIdentifier revalidate_task;
190 * Session (or NULL if no valid session currently exists or if the
191 * plugin does not use sessions).
193 struct Session *session;
195 struct ATS_ressource_entry * ressources;
197 struct ATS_quality_entry * quality;
200 * What was the last latency observed for this address, plugin and peer?
202 struct GNUNET_TIME_Relative latency;
205 * If we did not successfully transmit a message to the given peer
206 * via this connection during the specified time, we should consider
207 * the connection to be dead. This is used in the case that a TCP
208 * transport simply stalls writing to the stream but does not
209 * formerly get a signal that the other peer died.
211 struct GNUNET_TIME_Absolute timeout;
214 * How often have we tried to connect using this plugin? Used to
215 * discriminate against addresses that do not work well.
216 * FIXME: not yet used, but should be!
218 unsigned int connect_attempts;
221 * DV distance to this peer (1 if no DV is used).
222 * FIXME: need to set this from transport plugins!
232 * Have we ever estimated the latency of this address? Used to
233 * ensure that the first time we add an address, we immediately
239 * Are we currently connected via this address? The first time we
240 * successfully transmit or receive data to a peer via a particular
241 * address, we set this to GNUNET_YES. If we later get an error
242 * (disconnect notification, transmission failure, timeout), we set
243 * it back to GNUNET_NO.
248 * Is this plugin currently busy transmitting to the specific target?
249 * GNUNET_NO if not (initial, default state is GNUNET_NO). Internal
250 * messages do not count as 'in transmit'.
255 * Has this address been validated yet?
263 * Entry in linked list of network addresses for ourselves. Also
264 * includes a cached signature for 'struct TransportPongMessage's.
266 struct OwnAddressList
269 * This is a linked list.
271 struct OwnAddressList *next;
274 * How long until we actually auto-expire this address (unless it is
275 * re-confirmed by the transport)?
277 struct GNUNET_TIME_Absolute expires;
280 * How long until the current signature expires? (ZERO if the
281 * signature was never created).
283 struct GNUNET_TIME_Absolute pong_sig_expires;
286 * Signature for a 'struct TransportPongMessage' for this address.
288 struct GNUNET_CRYPTO_RsaSignature pong_signature;
299 * Entry in linked list of all of our plugins.
301 struct TransportPlugin
305 * This is a linked list.
307 struct TransportPlugin *next;
310 * API of the transport as returned by the plugin's
311 * initialization function.
313 struct GNUNET_TRANSPORT_PluginFunctions *api;
316 * Short name for the plugin (i.e. "tcp").
321 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
326 * List of our known addresses for this transport.
328 struct OwnAddressList *addresses;
331 * Environment this transport service is using
334 struct GNUNET_TRANSPORT_PluginEnvironment env;
337 * ID of task that is used to clean up expired addresses.
339 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
342 * Set to GNUNET_YES if we need to scrap the existing list of
343 * "addresses" and start fresh when we receive the next address
344 * update from a transport. Set to GNUNET_NO if we should just add
345 * the new address to the list and wait for the commit call.
349 struct ATS_plugin * rc;
352 * Hashmap of blacklisted peers for this particular transport.
354 struct GNUNET_CONTAINER_MultiHashMap *blacklist;
357 struct NeighbourList;
360 * For each neighbour we keep a list of messages
361 * that we still want to transmit to the neighbour.
367 * This is a doubly linked list.
369 struct MessageQueue *next;
372 * This is a doubly linked list.
374 struct MessageQueue *prev;
377 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
378 * stuck together in memory. Allocated at the end of this struct.
380 const char *message_buf;
383 * Size of the message buf
385 size_t message_buf_size;
388 * Client responsible for queueing the message;
389 * used to check that a client has no two messages
390 * pending for the same target. Can be NULL.
392 struct TransportClient *client;
395 * Using which specific address should we send this message?
397 struct ForeignAddressList *specific_address;
400 * Peer ID of the Neighbour this entry belongs to.
402 struct GNUNET_PeerIdentity neighbour_id;
405 * Plugin that we used for the transmission.
406 * NULL until we scheduled a transmission.
408 struct TransportPlugin *plugin;
411 * At what time should we fail?
413 struct GNUNET_TIME_Absolute timeout;
416 * Internal message of the transport system that should not be
417 * included in the usual SEND-SEND_OK transmission confirmation
418 * traffic management scheme. Typically, "internal_msg" will
419 * be set whenever "client" is NULL (but it is not strictly
425 * How important is the message?
427 unsigned int priority;
433 * For a given Neighbour, which plugins are available
434 * to talk to this peer and what are their costs?
439 * This is a linked list.
441 struct ReadyList *next;
444 * Which of our transport plugins does this entry
447 struct TransportPlugin *plugin;
450 * Transport addresses, latency, and readiness for
451 * this particular plugin.
453 struct ForeignAddressList *addresses;
456 * To which neighbour does this ready list belong to?
458 struct NeighbourList *neighbour;
463 * Entry in linked list of all of our current neighbours.
469 * This is a linked list.
471 struct NeighbourList *next;
474 * Which of our transports is connected to this peer
475 * and what is their status?
477 struct ReadyList *plugins;
480 * Head of list of messages we would like to send to this peer;
481 * must contain at most one message per client.
483 struct MessageQueue *messages_head;
486 * Tail of list of messages we would like to send to this peer; must
487 * contain at most one message per client.
489 struct MessageQueue *messages_tail;
492 * Buffer for at most one payload message used when we receive
493 * payload data before our PING-PONG has succeeded. We then
494 * store such messages in this intermediary buffer until the
495 * connection is fully up.
497 struct GNUNET_MessageHeader *pre_connect_message_buffer;
500 * Context for peerinfo iteration.
501 * NULL after we are done processing peerinfo's information.
503 struct GNUNET_PEERINFO_IteratorContext *piter;
506 * Public key for this peer. Valid only if the respective flag is set below.
508 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
511 * Identity of this neighbour.
513 struct GNUNET_PeerIdentity id;
516 * ID of task scheduled to run when this peer is about to
517 * time out (will free resources associated with the peer).
519 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
522 * ID of task scheduled to run when we should retry transmitting
523 * the head of the message queue. Actually triggered when the
524 * transmission is timing out (we trigger instantly when we have
525 * a chance of success).
527 GNUNET_SCHEDULER_TaskIdentifier retry_task;
530 * How long until we should consider this peer dead
531 * (if we don't receive another message in the
534 struct GNUNET_TIME_Absolute peer_timeout;
537 * Tracker for inbound bandwidth.
539 struct GNUNET_BANDWIDTH_Tracker in_tracker;
542 * The latency we have seen for this particular address for
543 * this particular peer. This latency may have been calculated
544 * over multiple transports. This value reflects how long it took
545 * us to receive a response when SENDING via this particular
546 * transport/neighbour/address combination!
548 * FIXME: we need to periodically send PINGs to update this
549 * latency (at least more often than the current "huge" (11h?)
552 struct GNUNET_TIME_Relative latency;
555 * How often has the other peer (recently) violated the
556 * inbound traffic limit? Incremented by 10 per violation,
557 * decremented by 1 per non-violation (for each
560 unsigned int quota_violation_count;
563 * DV distance to this peer (1 if no DV is used).
568 * Have we seen an PONG from this neighbour in the past (and
569 * not had a disconnect since)?
574 * Do we have a valid public key for this neighbour?
576 int public_key_valid;
579 * Performance data for the peer.
581 struct GNUNET_TRANSPORT_ATS_Information *ats;
584 * Identity of the neighbour.
586 struct GNUNET_PeerIdentity peer;
591 * Message used to ask a peer to validate receipt (to check an address
592 * from a HELLO). Followed by the address we are trying to validate,
593 * or an empty address if we are just sending a PING to confirm that a
594 * connection which the receiver (of the PING) initiated is still valid.
596 struct TransportPingMessage
600 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
602 struct GNUNET_MessageHeader header;
605 * Challenge code (to ensure fresh reply).
607 uint32_t challenge GNUNET_PACKED;
610 * Who is the intended recipient?
612 struct GNUNET_PeerIdentity target;
618 * Message used to validate a HELLO. The challenge is included in the
619 * confirmation to make matching of replies to requests possible. The
620 * signature signs our public key, an expiration time and our address.<p>
622 * This message is followed by our transport address that the PING tried
623 * to confirm (if we liked it). The address can be empty (zero bytes)
624 * if the PING had not address either (and we received the request via
625 * a connection that we initiated).
627 struct TransportPongMessage
631 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
633 struct GNUNET_MessageHeader header;
636 * Challenge code from PING (showing freshness). Not part of what
637 * is signed so that we can re-use signatures.
639 uint32_t challenge GNUNET_PACKED;
644 struct GNUNET_CRYPTO_RsaSignature signature;
647 * What are we signing and why? Two possible reason codes can be here:
648 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
649 * plausible address for this peer (pid is set to identity of signer); or
650 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING to confirm that this is
651 * an address we used to connect to the peer with the given pid.
653 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
656 * When does this signature expire?
658 struct GNUNET_TIME_AbsoluteNBO expiration;
661 * Either the identity of the peer Who signed this message, or the
662 * identity of the peer that we're connected to using the given
663 * address (depending on purpose.type).
665 struct GNUNET_PeerIdentity pid;
668 * Size of address appended to this message (part of what is
669 * being signed, hence not redundant).
677 * Linked list of messages to be transmitted to the client. Each
678 * entry is followed by the actual message.
680 struct ClientMessageQueueEntry
683 * This is a doubly-linked list.
685 struct ClientMessageQueueEntry *next;
688 * This is a doubly-linked list.
690 struct ClientMessageQueueEntry *prev;
695 * Client connected to the transport service.
697 struct TransportClient
701 * This is a linked list.
703 struct TransportClient *next;
706 * Handle to the client.
708 struct GNUNET_SERVER_Client *client;
711 * Linked list of messages yet to be transmitted to
714 struct ClientMessageQueueEntry *message_queue_head;
717 * Tail of linked list of messages yet to be transmitted to the
720 struct ClientMessageQueueEntry *message_queue_tail;
723 * Current transmit request handle.
725 struct GNUNET_CONNECTION_TransmitHandle *th;
728 * Is a call to "transmit_send_continuation" pending? If so, we
729 * must not free this struct (even if the corresponding client
730 * disconnects) and instead only remove it from the linked list and
731 * set the "client" field to NULL.
736 * Length of the list of messages pending for this client.
738 unsigned int message_count;
744 * Context of currently active requests to peerinfo
745 * for validation of HELLOs.
747 struct CheckHelloValidatedContext;
751 * Entry in map of all HELLOs awaiting validation.
753 struct ValidationEntry
757 * NULL if this entry is not part of a larger HELLO validation.
759 struct CheckHelloValidatedContext *chvc;
762 * The address, actually a pointer to the end
763 * of this struct. Do not free!
768 * Name of the transport.
770 char *transport_name;
773 * The public key of the peer.
775 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
778 * ID of task that will clean up this entry if we don't succeed
779 * with the validation first.
781 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
784 * At what time did we send this validation?
786 struct GNUNET_TIME_Absolute send_time;
789 * Session being validated (or NULL for none).
791 struct Session *session;
794 * Challenge number we used.
807 * Context of currently active requests to peerinfo
808 * for validation of HELLOs.
810 struct CheckHelloValidatedContext
814 * This is a doubly-linked list.
816 struct CheckHelloValidatedContext *next;
819 * This is a doubly-linked list.
821 struct CheckHelloValidatedContext *prev;
824 * Hello that we are validating.
826 const struct GNUNET_HELLO_Message *hello;
829 * Context for peerinfo iteration.
830 * NULL after we are done processing peerinfo's information.
832 struct GNUNET_PEERINFO_IteratorContext *piter;
835 * Was a HELLO known for this peer to peerinfo?
840 * Number of validation entries currently referring to this
843 unsigned int ve_count;
846 struct ATS_quality_metric
855 struct ATS_mechanism * prev;
856 struct ATS_mechanism * next;
857 struct ForeignAddressList * addr;
858 struct TransportPlugin * plugin;
859 struct ATS_peer * peer;
862 struct ATS_ressource_cost * rc;
868 struct GNUNET_PeerIdentity peer;
869 struct NeighbourList * n;
870 struct ATS_mechanism * m_head;
871 struct ATS_mechanism * m_tail;
873 /* preference value f */
886 struct ATS_ressource_entry
888 /* index in ressources array */
890 /* depending ATSi parameter to calculcate limits */
899 /* index in ressources array */
901 /* depending ATSi parameter to calculcate limits */
903 /* cfg option to load limits */
910 /* cofficients for the specific plugins */
920 static struct ATS_ressource ressources[] =
922 /* FIXME: the coefficients for the specific plugins */
923 {1, 7, "LAN_BW_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 1, 1, 2, 2, 1, 3},
924 {2, 7, "WAN_BW_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 1, 1, 2, 2, 2, 3},
925 {3, 4, "WLAN_ENERGY_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 0, 0, 0, 0, 2, 1}
927 {4, 4, "COST_ENERGY_CONSUMPTION", VERY_BIG_DOUBLE_VALUE},
928 {5, 5, "COST_CONNECT", VERY_BIG_DOUBLE_VALUE},
929 {6, 6, "COST_BANDWITH_AVAILABLE", VERY_BIG_DOUBLE_VALUE},
930 {7, 7, "COST_NETWORK_OVERHEAD", VERY_BIG_DOUBLE_VALUE},*/
933 static int available_ressources = 3;
941 * Time of last execution
943 struct GNUNET_TIME_Absolute last;
945 * Minimum intervall between two executions
947 struct GNUNET_TIME_Relative min_delta;
949 * Regular intervall when execution is triggered
951 struct GNUNET_TIME_Relative exec_intervall;
953 * Maximum execution time per calculation
955 struct GNUNET_TIME_Relative max_exec_duration;
957 * Maximum number of LP iterations per calculation
965 * Ressource costs or quality metrics changed, update matrix
967 int modified_resources;
970 * Ressource costs or quality metrics changed, update matrix
972 int modified_quality;
975 * Peers have connected or disconnected, problem has to be recreated
985 GNUNET_SCHEDULER_TaskIdentifier ats_task;
987 struct ATS_result res;
997 static struct GNUNET_HELLO_Message *our_hello;
1002 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
1007 static struct GNUNET_PeerIdentity my_identity;
1012 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
1015 * Our configuration.
1017 const struct GNUNET_CONFIGURATION_Handle *cfg;
1020 * Linked list of all clients to this service.
1022 static struct TransportClient *clients;
1025 * All loaded plugins.
1027 static struct TransportPlugin *plugins;
1030 * Handle to peerinfo service.
1032 static struct GNUNET_PEERINFO_Handle *peerinfo;
1035 * All known neighbours and their HELLOs.
1037 static struct NeighbourList *neighbours;
1040 * Number of neighbours we'd like to have.
1042 static uint32_t max_connect_per_transport;
1045 * Head of linked list.
1047 static struct CheckHelloValidatedContext *chvc_head;
1050 * Tail of linked list.
1052 static struct CheckHelloValidatedContext *chvc_tail;
1055 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
1056 * of the given peer that we are currently validating).
1058 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
1061 * Handle for reporting statistics.
1063 static struct GNUNET_STATISTICS_Handle *stats;
1066 * Handle for ats information
1068 static struct ATS_info *ats;
1070 struct ATS_quality_entry
1078 static struct ATS_quality_metric qm[] =
1080 {1, 1028, "QUALITY_NET_DISTANCE"},
1081 {2, 1034, "QUALITY_NET_DELAY"},
1083 static int available_quality_metrics = 2;
1087 * The peer specified by the given neighbour has timed-out or a plugin
1088 * has disconnected. We may either need to do nothing (other plugins
1089 * still up), or trigger a full disconnect and clean up. This
1090 * function updates our state and do the necessary notifications.
1091 * Also notifies our clients that the neighbour is now officially
1094 * @param n the neighbour list entry for the peer
1095 * @param check should we just check if all plugins
1096 * disconnected or must we ask all plugins to
1099 static void disconnect_neighbour (struct NeighbourList *n, int check);
1102 * Check the ready list for the given neighbour and if a plugin is
1103 * ready for transmission (and if we have a message), do so!
1105 * @param nexi target peer for which to transmit
1107 static void try_transmission_to_peer (struct NeighbourList *n);
1112 void ats_shutdown ( );
1114 void ats_notify_peer_connect (
1115 const struct GNUNET_PeerIdentity *peer,
1116 const struct GNUNET_TRANSPORT_ATS_Information *ats_data);
1118 void ats_notify_peer_disconnect (
1119 const struct GNUNET_PeerIdentity *peer);
1121 void ats_notify_ats_data (
1122 const struct GNUNET_PeerIdentity *peer,
1123 const struct GNUNET_TRANSPORT_ATS_Information *ats_data);
1125 struct ForeignAddressList * ats_get_preferred_address (
1126 struct NeighbourList *n);
1129 * Find an entry in the neighbour list for a particular peer.
1131 * @return NULL if not found.
1133 static struct NeighbourList *
1134 find_neighbour (const struct GNUNET_PeerIdentity *key)
1136 struct NeighbourList *head = neighbours;
1138 while ((head != NULL) &&
1139 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
1144 static void update_addr_ats (struct ForeignAddressList *fal, const struct GNUNET_TRANSPORT_ATS_Information *ats_data, int ats_count)
1147 for (c1=0; c1<ats_count; c1++)
1149 for (c2=0; c2<available_quality_metrics; c2++)
1151 if (ntohl(ats_data[c1].type) == qm[c2].atis_index)
1153 fal->quality[c2].values[0] = fal->quality[c2].values[1];
1154 fal->quality[c2].values[1] = fal->quality[c2].values[2];
1155 fal->quality[c2].values[2] = ntohl(ats_data[c1].value);
1161 static void update_addr_value (struct ForeignAddressList *fal, uint32_t value , int ats_index)
1164 for (c=0; c<available_quality_metrics; c++)
1166 if (ats_index == qm[c].atis_index)
1168 fal->quality[c].values[0] = fal->quality[c].values[1];
1169 fal->quality[c].values[1] = fal->quality[c].values[2];
1170 fal->quality[c].values[2] = value;
1176 * Find an entry in the transport list for a particular transport.
1178 * @return NULL if not found.
1180 static struct TransportPlugin *
1181 find_transport (const char *short_name)
1183 struct TransportPlugin *head = plugins;
1184 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
1190 * Is a particular peer blacklisted for a particular transport?
1192 * @param peer the peer to check for
1193 * @param plugin the plugin used to connect to the peer
1195 * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
1198 is_blacklisted (const struct GNUNET_PeerIdentity *peer, struct TransportPlugin *plugin)
1201 if (plugin->blacklist != NULL)
1203 if (GNUNET_CONTAINER_multihashmap_contains (plugin->blacklist, &peer->hashPubKey) == GNUNET_YES)
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "Peer `%s:%s' is blacklisted!\n",
1208 plugin->short_name, GNUNET_i2s (peer));
1211 GNUNET_STATISTICS_update (stats, "# blacklisted peers refused", 1, GNUNET_NO);
1221 add_peer_to_blacklist (struct GNUNET_PeerIdentity *peer, char *transport_name)
1223 struct TransportPlugin *plugin;
1225 plugin = find_transport(transport_name);
1226 if (plugin == NULL) /* Nothing to do */
1228 if (plugin->blacklist == NULL)
1229 plugin->blacklist = GNUNET_CONTAINER_multihashmap_create(TRANSPORT_BLACKLIST_HT_SIZE);
1230 GNUNET_assert(plugin->blacklist != NULL);
1231 GNUNET_CONTAINER_multihashmap_put(plugin->blacklist, &peer->hashPubKey,
1233 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1238 * Read the blacklist file, containing transport:peer entries.
1239 * Provided the transport is loaded, set up hashmap with these
1240 * entries to blacklist peers by transport.
1244 read_blacklist_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1251 struct GNUNET_PeerIdentity pid;
1253 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1254 unsigned int entries_found;
1255 char *transport_name;
1258 GNUNET_CONFIGURATION_get_value_filename (cfg,
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 "Option `%s' in section `%s' not specified!\n",
1271 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1272 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1273 | GNUNET_DISK_PERM_USER_WRITE);
1274 if (0 != STAT (fn, &frstat))
1276 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1277 _("Could not read blacklist file `%s'\n"), fn);
1281 if (frstat.st_size == 0)
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1285 _("Blacklist file `%s' is empty.\n"),
1291 /* FIXME: use mmap */
1292 data = GNUNET_malloc_large (frstat.st_size);
1293 GNUNET_assert(data != NULL);
1294 if (frstat.st_size !=
1295 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1298 _("Failed to read blacklist from `%s'\n"), fn);
1305 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1307 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1308 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1311 while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') && !isspace ( (unsigned char) data[colon_pos]))
1314 if (colon_pos >= frstat.st_size)
1316 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1317 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1318 (unsigned long long) colon_pos);
1324 if (isspace( (unsigned char) data[colon_pos]))
1326 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1327 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1328 (unsigned long long) colon_pos);
1330 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1334 tsize = colon_pos - pos;
1335 if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size) || (tsize == 0))
1337 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1338 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1339 (unsigned long long) colon_pos);
1348 transport_name = GNUNET_malloc(tsize + 1);
1349 memcpy(transport_name, &data[pos], tsize);
1350 pos = colon_pos + 1;
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "Read transport name %s in blacklist file.\n",
1356 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1357 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1359 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1360 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1361 (unsigned long long) pos);
1363 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1365 GNUNET_free_non_null(transport_name);
1368 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1369 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1371 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1372 _("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
1373 (unsigned long long) pos,
1378 if (0 != memcmp (&pid,
1380 sizeof (struct GNUNET_PeerIdentity)))
1383 add_peer_to_blacklist (&pid,
1388 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1389 _("Found myself `%s' in blacklist (useless, ignored)\n"),
1393 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1394 GNUNET_free_non_null(transport_name);
1395 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1398 GNUNET_STATISTICS_update (stats, "# Transport entries blacklisted", entries_found, GNUNET_NO);
1405 * Function called to notify a client about the socket being ready to
1406 * queue more data. "buf" will be NULL and "size" zero if the socket
1407 * was closed for writing in the meantime.
1409 * @param cls closure
1410 * @param size number of bytes available in buf
1411 * @param buf where the callee should write the message
1412 * @return number of bytes written to buf
1415 transmit_to_client_callback (void *cls, size_t size, void *buf)
1417 struct TransportClient *client = cls;
1418 struct ClientMessageQueueEntry *q;
1421 const struct GNUNET_MessageHeader *msg;
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "Transmission to client failed, closing connection.\n");
1431 /* fatal error with client, free message queue! */
1432 while (NULL != (q = client->message_queue_head))
1434 GNUNET_STATISTICS_update (stats,
1435 gettext_noop ("# bytes discarded (could not transmit to client)"),
1436 ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
1438 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1439 client->message_queue_tail,
1443 client->message_count = 0;
1448 while (NULL != (q = client->message_queue_head))
1450 msg = (const struct GNUNET_MessageHeader *) &q[1];
1451 msize = ntohs (msg->size);
1452 if (msize + tsize > size)
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 "Transmitting message of type %u to client.\n",
1459 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1460 client->message_queue_tail,
1462 memcpy (&cbuf[tsize], msg, msize);
1465 client->message_count--;
1469 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1470 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1472 GNUNET_TIME_UNIT_FOREVER_REL,
1473 &transmit_to_client_callback,
1475 GNUNET_assert (client->th != NULL);
1482 * Convert an address to a string.
1484 * @param plugin name of the plugin responsible for the address
1485 * @param addr binary address
1486 * @param addr_len number of bytes in addr
1487 * @return NULL on error, otherwise address string
1490 a2s (const char *plugin,
1494 struct TransportPlugin *p;
1498 p = find_transport (plugin);
1501 return p->api->address_to_string (p->api->cls,
1508 * Mark the given FAL entry as 'connected' (and hence preferred for
1509 * sending); also mark all others for the same peer as 'not connected'
1510 * (since only one can be preferred).
1512 * @param fal address to set to 'connected'
1515 mark_address_connected (struct ForeignAddressList *fal)
1517 struct ForeignAddressList *pos;
1520 GNUNET_assert (GNUNET_YES == fal->validated);
1521 if (fal->connected == GNUNET_YES)
1522 return; /* nothing to do */
1524 pos = fal->ready_list->addresses;
1527 if (GNUNET_YES == pos->connected)
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1531 "Marking address `%s' as no longer connected (due to connect on other address)\n",
1532 a2s (pos->ready_list->plugin->short_name,
1536 GNUNET_break (cnt == GNUNET_YES);
1538 pos->connected = GNUNET_NO;
1539 GNUNET_STATISTICS_update (stats,
1540 gettext_noop ("# connected addresses"),
1546 fal->connected = GNUNET_YES;
1547 if (GNUNET_YES == cnt)
1549 GNUNET_STATISTICS_update (stats,
1550 gettext_noop ("# connected addresses"),
1558 * Send the specified message to the specified client. Since multiple
1559 * messages may be pending for the same client at a time, this code
1560 * makes sure that no message is lost.
1562 * @param client client to transmit the message to
1563 * @param msg the message to send
1564 * @param may_drop can this message be dropped if the
1565 * message queue for this client is getting far too large?
1568 transmit_to_client (struct TransportClient *client,
1569 const struct GNUNET_MessageHeader *msg, int may_drop)
1571 struct ClientMessageQueueEntry *q;
1574 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1576 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1578 ("Dropping message of type %u and size %u, have %u messages pending (%u is the soft limit)\n"),
1581 client->message_count,
1583 GNUNET_STATISTICS_update (stats,
1584 gettext_noop ("# messages dropped due to slow client"),
1589 msize = ntohs (msg->size);
1590 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1591 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1592 memcpy (&q[1], msg, msize);
1593 GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1594 client->message_queue_tail,
1595 client->message_queue_tail,
1597 client->message_count++;
1598 if (client->th == NULL)
1600 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1602 GNUNET_TIME_UNIT_FOREVER_REL,
1603 &transmit_to_client_callback,
1605 GNUNET_assert (client->th != NULL);
1611 * Transmit a 'SEND_OK' notification to the given client for the
1614 * @param client who to notify
1615 * @param n neighbour to notify about, can be NULL (on failure)
1616 * @param target target of the transmission
1617 * @param result status code for the transmission request
1620 transmit_send_ok (struct TransportClient *client,
1621 struct NeighbourList *n,
1622 const struct GNUNET_PeerIdentity *target,
1625 struct SendOkMessage send_ok_msg;
1627 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1628 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1629 send_ok_msg.success = htonl (result);
1631 send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1633 send_ok_msg.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
1634 send_ok_msg.peer = *target;
1635 transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1640 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1641 * upon "completion" of a send request. This tells the API
1642 * that it is now legal to send another message to the given
1645 * @param cls closure, identifies the entry on the
1646 * message queue that was transmitted and the
1647 * client responsible for queuing the message
1648 * @param target the peer receiving the message
1649 * @param result GNUNET_OK on success, if the transmission
1650 * failed, we should not tell the client to transmit
1654 transmit_send_continuation (void *cls,
1655 const struct GNUNET_PeerIdentity *target,
1658 struct MessageQueue *mq = cls;
1659 struct NeighbourList *n;
1661 GNUNET_STATISTICS_update (stats,
1662 gettext_noop ("# bytes pending with plugins"),
1663 - (int64_t) mq->message_buf_size,
1665 if (result == GNUNET_OK)
1667 GNUNET_STATISTICS_update (stats,
1668 gettext_noop ("# bytes successfully transmitted by plugins"),
1669 mq->message_buf_size,
1674 GNUNET_STATISTICS_update (stats,
1675 gettext_noop ("# bytes with transmission failure by plugins"),
1676 mq->message_buf_size,
1679 if (mq->specific_address != NULL)
1681 if (result == GNUNET_OK)
1683 mq->specific_address->timeout =
1684 GNUNET_TIME_relative_to_absolute
1685 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1686 if (mq->specific_address->validated == GNUNET_YES)
1687 mark_address_connected (mq->specific_address);
1691 if (mq->specific_address->connected != GNUNET_NO)
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "Marking address `%s' as no longer connected (due to transmission problem)\n",
1696 a2s (mq->specific_address->ready_list->plugin->short_name,
1697 mq->specific_address->addr,
1698 mq->specific_address->addrlen));
1700 GNUNET_STATISTICS_update (stats,
1701 gettext_noop ("# connected addresses"),
1704 mq->specific_address->connected = GNUNET_NO;
1707 if (! mq->internal_msg)
1708 mq->specific_address->in_transmit = GNUNET_NO;
1710 n = find_neighbour(&mq->neighbour_id);
1711 if (mq->client != NULL)
1712 transmit_send_ok (mq->client, n, target, result);
1715 try_transmission_to_peer (n);
1720 * Find an address in any of the available transports for
1721 * the given neighbour that would be good for message
1722 * transmission. This is essentially the transport selection
1725 * @param neighbour for whom to select an address
1726 * @return selected address, NULL if we have none
1728 struct ForeignAddressList *
1729 find_ready_address(struct NeighbourList *neighbour)
1731 struct ReadyList *head = neighbour->plugins;
1732 struct ForeignAddressList *addresses;
1733 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1734 struct ForeignAddressList *best_address;
1736 /* Hack to prefer unix domain sockets */
1737 struct ForeignAddressList *unix_address = NULL;
1739 best_address = NULL;
1740 while (head != NULL)
1742 addresses = head->addresses;
1743 while (addresses != NULL)
1745 if ( (addresses->timeout.abs_value < now.abs_value) &&
1746 (addresses->connected == GNUNET_YES) )
1749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1750 "Marking long-time inactive connection to `%4s' as down.\n",
1751 GNUNET_i2s (&neighbour->id));
1753 GNUNET_STATISTICS_update (stats,
1754 gettext_noop ("# connected addresses"),
1757 addresses->connected = GNUNET_NO;
1759 addresses = addresses->next;
1762 addresses = head->addresses;
1763 while (addresses != NULL)
1765 #if DEBUG_TRANSPORT > 1
1766 if (addresses->addr != NULL)
1767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1768 "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
1769 a2s (head->plugin->short_name,
1771 addresses->addrlen),
1772 GNUNET_i2s (&neighbour->id),
1773 addresses->connected,
1774 addresses->in_transmit,
1775 addresses->validated,
1776 addresses->connect_attempts,
1777 (unsigned long long) addresses->timeout.abs_value,
1778 (unsigned int) addresses->distance);
1780 if (0==strcmp(head->plugin->short_name,"unix"))
1782 if ((unix_address == NULL) || ((unix_address != NULL) &&
1783 (addresses->latency.rel_value < unix_address->latency.rel_value)))
1784 unix_address = addresses;
1786 if ( ( (best_address == NULL) ||
1787 (addresses->connected == GNUNET_YES) ||
1788 (best_address->connected == GNUNET_NO) ) &&
1789 (addresses->in_transmit == GNUNET_NO) &&
1790 ( (best_address == NULL) ||
1791 (addresses->latency.rel_value < best_address->latency.rel_value)) )
1792 best_address = addresses;
1793 /* FIXME: also give lower-latency addresses that are not
1794 connected a chance some times... */
1795 addresses = addresses->next;
1797 if (unix_address != NULL)
1801 if (unix_address != NULL)
1803 best_address = unix_address;
1805 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found unix address, forced this address\n");
1808 if (best_address != NULL)
1812 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1813 "Best address found (`%s') has latency of %llu ms.\n",
1814 (best_address->addrlen > 0)
1815 ? a2s (best_address->ready_list->plugin->short_name,
1817 best_address->addrlen)
1819 best_address->latency.rel_value);
1824 GNUNET_STATISTICS_update (stats,
1825 gettext_noop ("# transmission attempts failed (no address)"),
1830 return best_address;
1836 * We should re-try transmitting to the given peer,
1837 * hopefully we've learned something in the meantime.
1840 retry_transmission_task (void *cls,
1841 const struct GNUNET_SCHEDULER_TaskContext *tc)
1843 struct NeighbourList *n = cls;
1845 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1846 try_transmission_to_peer (n);
1851 * Check the ready list for the given neighbour and if a plugin is
1852 * ready for transmission (and if we have a message), do so!
1854 * @param neighbour target peer for which to transmit
1857 try_transmission_to_peer (struct NeighbourList *n)
1859 struct ReadyList *rl;
1860 struct MessageQueue *mq;
1861 struct GNUNET_TIME_Relative timeout;
1865 if (n->messages_head == NULL)
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1869 "Transmission queue for `%4s' is empty\n",
1870 GNUNET_i2s (&n->id));
1872 return; /* nothing to do */
1875 mq = n->messages_head;
1876 force_address = GNUNET_YES;
1877 if (mq->specific_address == NULL)
1880 mq->specific_address = ats_get_preferred_address(n);
1881 GNUNET_STATISTICS_update (stats,
1882 gettext_noop ("# transport selected peer address freely"),
1885 force_address = GNUNET_NO;
1887 if (mq->specific_address == NULL)
1889 GNUNET_STATISTICS_update (stats,
1890 gettext_noop ("# transport failed to selected peer address"),
1893 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1894 if (timeout.rel_value == 0)
1897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1898 "No destination address available to transmit message of size %u to peer `%4s'\n",
1899 mq->message_buf_size,
1900 GNUNET_i2s (&mq->neighbour_id));
1902 GNUNET_STATISTICS_update (stats,
1903 gettext_noop ("# bytes in message queue for other peers"),
1904 - (int64_t) mq->message_buf_size,
1906 GNUNET_STATISTICS_update (stats,
1907 gettext_noop ("# bytes discarded (no destination address available)"),
1908 mq->message_buf_size,
1910 if (mq->client != NULL)
1911 transmit_send_ok (mq->client, n, &n->id, GNUNET_NO);
1912 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1916 return; /* nobody ready */
1918 GNUNET_STATISTICS_update (stats,
1919 gettext_noop ("# message delivery deferred (no address)"),
1922 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
1923 GNUNET_SCHEDULER_cancel (n->retry_task);
1924 n->retry_task = GNUNET_SCHEDULER_add_delayed (timeout,
1925 &retry_transmission_task,
1928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1929 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
1930 mq->message_buf_size,
1931 GNUNET_i2s (&mq->neighbour_id),
1934 /* FIXME: might want to trigger peerinfo lookup here
1935 (unless that's already pending...) */
1938 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1941 if (mq->specific_address->connected == GNUNET_NO)
1942 mq->specific_address->connect_attempts++;
1943 rl = mq->specific_address->ready_list;
1944 mq->plugin = rl->plugin;
1945 if (!mq->internal_msg)
1946 mq->specific_address->in_transmit = GNUNET_YES;
1948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1949 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
1950 mq->message_buf_size,
1951 GNUNET_i2s (&n->id),
1952 (mq->specific_address->addr != NULL)
1953 ? a2s (mq->plugin->short_name,
1954 mq->specific_address->addr,
1955 mq->specific_address->addrlen)
1957 rl->plugin->short_name);
1959 GNUNET_STATISTICS_update (stats,
1960 gettext_noop ("# bytes in message queue for other peers"),
1961 - (int64_t) mq->message_buf_size,
1963 GNUNET_STATISTICS_update (stats,
1964 gettext_noop ("# bytes pending with plugins"),
1965 mq->message_buf_size,
1967 ret = rl->plugin->api->send (rl->plugin->api->cls,
1970 mq->message_buf_size,
1972 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1973 mq->specific_address->session,
1974 mq->specific_address->addr,
1975 mq->specific_address->addrlen,
1977 &transmit_send_continuation, mq);
1980 /* failure, but 'send' would not call continuation in this case,
1981 so we need to do it here! */
1982 transmit_send_continuation (mq,
1990 * Send the specified message to the specified peer.
1992 * @param client source of the transmission request (can be NULL)
1993 * @param peer_address ForeignAddressList where we should send this message
1994 * @param priority how important is the message
1995 * @param timeout how long do we have to transmit?
1996 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
1997 * @param message_buf_size total size of all messages in message_buf
1998 * @param is_internal is this an internal message; these are pre-pended and
1999 * also do not count for plugins being "ready" to transmit
2000 * @param neighbour handle to the neighbour for transmission
2003 transmit_to_peer (struct TransportClient *client,
2004 struct ForeignAddressList *peer_address,
2005 unsigned int priority,
2006 struct GNUNET_TIME_Relative timeout,
2007 const char *message_buf,
2008 size_t message_buf_size,
2009 int is_internal, struct NeighbourList *neighbour)
2011 struct MessageQueue *mq;
2016 /* check for duplicate submission */
2017 mq = neighbour->messages_head;
2020 if (mq->client == client)
2022 /* client transmitted to same peer twice
2023 before getting SEND_OK! */
2031 GNUNET_STATISTICS_update (stats,
2032 gettext_noop ("# bytes in message queue for other peers"),
2035 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
2036 mq->specific_address = peer_address;
2037 mq->client = client;
2038 /* FIXME: this memcpy can be up to 7% of our total runtime! */
2039 memcpy (&mq[1], message_buf, message_buf_size);
2040 mq->message_buf = (const char*) &mq[1];
2041 mq->message_buf_size = message_buf_size;
2042 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
2043 mq->internal_msg = is_internal;
2044 mq->priority = priority;
2045 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
2047 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
2048 neighbour->messages_tail,
2051 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
2052 neighbour->messages_tail,
2053 neighbour->messages_tail,
2055 try_transmission_to_peer (neighbour);
2062 struct GeneratorContext
2064 struct TransportPlugin *plug_pos;
2065 struct OwnAddressList *addr_pos;
2066 struct GNUNET_TIME_Absolute expiration;
2074 address_generator (void *cls, size_t max, void *buf)
2076 struct GeneratorContext *gc = cls;
2079 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
2081 gc->plug_pos = gc->plug_pos->next;
2082 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
2084 if (NULL == gc->plug_pos)
2089 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
2092 gc->addr_pos->addrlen, buf, max);
2093 gc->addr_pos = gc->addr_pos->next;
2099 * Construct our HELLO message from all of the addresses of
2100 * all of the transports.
2105 struct GNUNET_HELLO_Message *hello;
2106 struct TransportClient *cpos;
2107 struct NeighbourList *npos;
2108 struct GeneratorContext gc;
2110 gc.plug_pos = plugins;
2111 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
2112 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2113 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
2115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2116 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
2118 GNUNET_STATISTICS_update (stats,
2119 gettext_noop ("# refreshed my HELLO"),
2123 while (cpos != NULL)
2125 transmit_to_client (cpos,
2126 (const struct GNUNET_MessageHeader *) hello,
2131 GNUNET_free_non_null (our_hello);
2133 GNUNET_PEERINFO_add_peer (peerinfo, our_hello);
2135 while (npos != NULL)
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2139 "Transmitting updated `%s' to neighbour `%4s'\n",
2140 "HELLO", GNUNET_i2s (&npos->id));
2142 GNUNET_STATISTICS_update (stats,
2143 gettext_noop ("# transmitted my HELLO to other peers"),
2146 transmit_to_peer (NULL, NULL, 0,
2147 HELLO_ADDRESS_EXPIRATION,
2148 (const char *) our_hello,
2149 GNUNET_HELLO_size(our_hello),
2157 * Task used to clean up expired addresses for a plugin.
2159 * @param cls closure
2163 expire_address_task (void *cls,
2164 const struct GNUNET_SCHEDULER_TaskContext *tc);
2168 * Update the list of addresses for this plugin,
2169 * expiring those that are past their expiration date.
2171 * @param plugin addresses of which plugin should be recomputed?
2172 * @param fresh set to GNUNET_YES if a new address was added
2173 * and we need to regenerate the HELLO even if nobody
2177 update_addresses (struct TransportPlugin *plugin,
2180 static struct GNUNET_TIME_Absolute last_update;
2181 struct GNUNET_TIME_Relative min_remaining;
2182 struct GNUNET_TIME_Relative remaining;
2183 struct GNUNET_TIME_Absolute now;
2184 struct OwnAddressList *pos;
2185 struct OwnAddressList *prev;
2186 struct OwnAddressList *next;
2189 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_TASK)
2190 GNUNET_SCHEDULER_cancel (plugin->address_update_task);
2191 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
2192 now = GNUNET_TIME_absolute_get ();
2193 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
2194 expired = (GNUNET_TIME_absolute_get_duration (last_update).rel_value > (HELLO_ADDRESS_EXPIRATION.rel_value / 4));
2196 pos = plugin->addresses;
2200 if (pos->expires.abs_value < now.abs_value)
2202 expired = GNUNET_YES;
2204 plugin->addresses = pos->next;
2206 prev->next = pos->next;
2211 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
2212 if (remaining.rel_value < min_remaining.rel_value)
2213 min_remaining = remaining;
2219 if (expired || fresh)
2224 min_remaining = GNUNET_TIME_relative_min (min_remaining,
2225 GNUNET_TIME_relative_divide (HELLO_ADDRESS_EXPIRATION,
2227 plugin->address_update_task
2228 = GNUNET_SCHEDULER_add_delayed (min_remaining,
2229 &expire_address_task, plugin);
2234 * Task used to clean up expired addresses for a plugin.
2236 * @param cls closure
2240 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2242 struct TransportPlugin *plugin = cls;
2244 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
2245 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2246 update_addresses (plugin, GNUNET_NO);
2251 * Iterator over hash map entries that NULLs the session of validation
2252 * entries that match the given session.
2254 * @param cls closure (the 'struct Session*' to match against)
2255 * @param key current key code (peer ID, not used)
2256 * @param value value in the hash map ('struct ValidationEntry*')
2257 * @return GNUNET_YES (we should continue to iterate)
2260 remove_session_validations (void *cls,
2261 const GNUNET_HashCode * key,
2264 struct Session *session = cls;
2265 struct ValidationEntry *ve = value;
2267 if (session == ve->session)
2274 * We've been disconnected from the other peer (for some
2275 * connection-oriented transport). Either quickly
2276 * re-establish the connection or signal the disconnect
2279 * Only signal CORE level disconnect if ALL addresses
2280 * for the peer are exhausted.
2282 * @param p overall plugin context
2283 * @param nl neighbour that was disconnected
2286 try_fast_reconnect (struct TransportPlugin *p,
2287 struct NeighbourList *nl)
2289 /* FIXME-MW: fast reconnect / transport switching not implemented... */
2290 /* Note: the idea here is to hide problems with transports (or
2291 switching between plugins) from the core to eliminate the need to
2292 re-negotiate session keys and the like; OTOH, we should tell core
2293 quickly (much faster than timeout) `if a connection was lost and
2294 could not be re-established (i.e. other peer went down or is
2295 unable / refuses to communicate);
2297 So we should consider:
2298 1) ideally: our own willingness / need to connect
2299 2) prior failures to connect to this peer (by plugin)
2300 3) ideally: reasons why other peer terminated (as far as knowable)
2302 Most importantly, it must be POSSIBLE for another peer to terminate
2303 a connection for a while (without us instantly re-establishing it).
2304 Similarly, if another peer is gone we should quickly notify CORE.
2305 OTOH, if there was a minor glitch (i.e. crash of gnunet-service-transport
2306 on the other end), we should reconnect in such a way that BOTH CORE
2307 services never even notice.
2308 Furthermore, the same mechanism (or small variation) could be used
2309 to switch to a better-performing plugin (ATS).
2311 Finally, this needs to be tested throughly... */
2314 * GNUNET_NO in the call below makes transport disconnect the peer,
2315 * even if only a single address (out of say, six) went away. This
2316 * function must be careful to ONLY disconnect if the peer is gone,
2317 * not just a specifi address.
2319 * More specifically, half the places it was used had it WRONG.
2322 /* No reconnect, signal disconnect instead! */
2323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2324 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2325 "try_fast_reconnect");
2326 disconnect_neighbour (nl, GNUNET_YES);
2331 * Function that will be called whenever the plugin internally
2332 * cleans up a session pointer and hence the service needs to
2333 * discard all of those sessions as well. Plugins that do not
2334 * use sessions can simply omit calling this function and always
2335 * use NULL wherever a session pointer is needed.
2337 * @param cls closure
2338 * @param peer which peer was the session for
2339 * @param session which session is being destoyed
2342 plugin_env_session_end (void *cls,
2343 const struct GNUNET_PeerIdentity *peer,
2344 struct Session *session)
2346 struct TransportPlugin *p = cls;
2347 struct NeighbourList *nl;
2348 struct ReadyList *rl;
2349 struct ForeignAddressList *pos;
2350 struct ForeignAddressList *prev;
2352 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2353 &remove_session_validations,
2355 nl = find_neighbour (peer);
2357 return; /* was never marked as connected */
2361 if (rl->plugin == p)
2366 return; /* was never marked as connected */
2368 pos = rl->addresses;
2369 while ( (pos != NULL) &&
2370 (pos->session != session) )
2376 return; /* was never marked as connected */
2377 pos->session = NULL;
2378 if (pos->addrlen != 0)
2380 if (nl->received_pong != GNUNET_NO)
2381 try_fast_reconnect (p, nl);
2384 /* was inbound connection, free 'pos' */
2386 rl->addresses = pos->next;
2388 prev->next = pos->next;
2389 if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2391 GNUNET_SCHEDULER_cancel (pos->revalidate_task);
2392 pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2395 if (nl->received_pong == GNUNET_NO)
2396 return; /* nothing to do, never connected... */
2397 /* check if we have any validated addresses left */
2398 pos = rl->addresses;
2403 try_fast_reconnect (p, nl);
2408 /* no valid addresses left, signal disconnect! */
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2411 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2412 "plugin_env_session_end");
2413 /* FIXME: This doesn't mean there are no addresses left for this PEER,
2414 * it means there aren't any left for this PLUGIN/PEER combination! So
2415 * calling disconnect_neighbor here with GNUNET_NO forces disconnect
2416 * when it isn't necessary. Using GNUNET_YES at least checks to see
2417 * if there are any addresses that work first, so as not to overdo it.
2420 disconnect_neighbour (nl, GNUNET_YES);
2425 * Function that must be called by each plugin to notify the
2426 * transport service about the addresses under which the transport
2427 * provided by the plugin can be reached.
2429 * @param cls closure
2430 * @param name name of the transport that generated the address
2431 * @param addr one of the addresses of the host, NULL for the last address
2432 * the specific address format depends on the transport
2433 * @param addrlen length of the address
2434 * @param expires when should this address automatically expire?
2437 plugin_env_notify_address (void *cls,
2441 struct GNUNET_TIME_Relative expires)
2443 struct TransportPlugin *p = cls;
2444 struct OwnAddressList *al;
2445 struct GNUNET_TIME_Absolute abex;
2447 GNUNET_assert (addr != NULL);
2448 abex = GNUNET_TIME_relative_to_absolute (expires);
2449 GNUNET_assert (p == find_transport (name));
2453 if ( (addrlen == al->addrlen) &&
2454 (0 == memcmp (addr, &al[1], addrlen)) )
2457 update_addresses (p, GNUNET_NO);
2462 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2463 al->next = p->addresses;
2466 al->addrlen = addrlen;
2467 memcpy (&al[1], addr, addrlen);
2468 update_addresses (p, GNUNET_YES);
2473 * Notify all of our clients about a peer connecting.
2476 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2477 struct GNUNET_TIME_Relative latency,
2480 struct ConnectInfoMessage * cim;
2481 struct TransportClient *cpos;
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2487 "Notifying clients about connection from `%s'\n",
2490 GNUNET_STATISTICS_update (stats,
2491 gettext_noop ("# peers connected"),
2496 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
2497 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2501 cim = GNUNET_malloc (size);
2503 cim->header.size = htons (size);
2504 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2505 cim->ats_count = htonl(2);
2506 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2507 (&(cim->ats))[0].value = htonl (distance);
2508 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
2509 (&(cim->ats))[1].value = htonl ((uint32_t) latency.rel_value);
2510 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2511 (&(cim->ats))[2].value = htonl (0);
2512 memcpy (&cim->id, peer, sizeof (struct GNUNET_PeerIdentity));
2514 /* notify ats about connecting peer */
2515 ats_notify_peer_connect (peer, &(cim->ats));
2518 while (cpos != NULL)
2520 transmit_to_client (cpos, &(cim->header), GNUNET_NO);
2529 * Notify all of our clients about a peer disconnecting.
2532 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2534 struct DisconnectInfoMessage dim;
2535 struct TransportClient *cpos;
2538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2539 "Notifying clients about lost connection to `%s'\n",
2542 GNUNET_STATISTICS_update (stats,
2543 gettext_noop ("# peers connected"),
2546 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2547 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2548 dim.reserved = htonl (0);
2549 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2551 /* notify ats about connecting peer */
2552 ats_notify_peer_disconnect (peer);
2555 while (cpos != NULL)
2557 transmit_to_client (cpos, &dim.header, GNUNET_NO);
2564 * Find a ForeignAddressList entry for the given neighbour
2565 * that matches the given address and transport.
2567 * @param neighbour which peer we care about
2568 * @param tname name of the transport plugin
2569 * @param session session to look for, NULL for 'any'; otherwise
2570 * can be used for the service to "learn" this session ID
2572 * @param addr binary address
2573 * @param addrlen length of addr
2574 * @return NULL if no such entry exists
2576 static struct ForeignAddressList *
2577 find_peer_address(struct NeighbourList *neighbour,
2579 struct Session *session,
2583 struct ReadyList *head;
2584 struct ForeignAddressList *pos;
2586 head = neighbour->plugins;
2587 while (head != NULL)
2589 if (0 == strcmp (tname, head->plugin->short_name))
2595 pos = head->addresses;
2596 while ( (pos != NULL) &&
2597 ( (pos->addrlen != addrlen) ||
2598 (memcmp(pos->addr, addr, addrlen) != 0) ) )
2600 if ( (session != NULL) &&
2601 (pos->session == session) )
2605 if ( (session != NULL) && (pos != NULL) )
2606 pos->session = session; /* learn it! */
2612 * Get the peer address struct for the given neighbour and
2613 * address. If it doesn't yet exist, create it.
2615 * @param neighbour which peer we care about
2616 * @param tname name of the transport plugin
2617 * @param session session of the plugin, or NULL for none
2618 * @param addr binary address
2619 * @param addrlen length of addr
2620 * @return NULL if we do not have a transport plugin for 'tname'
2622 static struct ForeignAddressList *
2623 add_peer_address (struct NeighbourList *neighbour,
2625 struct Session *session,
2629 struct ReadyList *head;
2630 struct ForeignAddressList *ret;
2633 ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2636 head = neighbour->plugins;
2638 while (head != NULL)
2640 if (0 == strcmp (tname, head->plugin->short_name))
2646 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2647 ret->session = session;
2648 if ((addrlen > 0) && (addr != NULL))
2650 ret->addr = (const char*) &ret[1];
2651 memcpy (&ret[1], addr, addrlen);
2658 ret->ressources = GNUNET_malloc(available_ressources * sizeof (struct ATS_ressource_entry));
2660 for (c=0; c<available_ressources; c++)
2662 struct ATS_ressource_entry *r = ret->ressources;
2664 r[c].atis_index = ressources[c].atis_index;
2665 if (0 == strcmp(neighbour->plugins->plugin->short_name,"unix"))
2667 r[c].c = ressources[c].c_unix;
2670 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"udp"))
2672 r[c].c = ressources[c].c_udp;
2675 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"tcp"))
2677 r[c].c = ressources[c].c_tcp;
2680 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"http"))
2682 r[c].c = ressources[c].c_http;
2685 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"https"))
2687 r[c].c = ressources[c].c_https;
2690 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"wlan"))
2692 r[c].c = ressources[c].c_wlan;
2698 r[c].c = ressources[c].c_default;
2699 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"Assigning default cost to peer `%s' addr plugin `%s'! This should not happen!",
2700 GNUNET_i2s(&neighbour->peer), neighbour->plugins->plugin->short_name);
2704 ret->quality = GNUNET_malloc (available_quality_metrics * sizeof (struct ATS_quality_entry));
2705 ret->addrlen = addrlen;
2706 ret->expires = GNUNET_TIME_relative_to_absolute
2707 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2708 ret->latency = GNUNET_TIME_relative_get_forever();
2710 ret->timeout = GNUNET_TIME_relative_to_absolute
2711 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2712 ret->ready_list = head;
2713 ret->next = head->addresses;
2714 head->addresses = ret;
2720 * Closure for 'add_validated_address'.
2722 struct AddValidatedAddressContext
2725 * Entry that has been validated.
2727 const struct ValidationEntry *ve;
2730 * Flag set after we have added the address so
2731 * that we terminate the iteration next time.
2738 * Callback function used to fill a buffer of max bytes with a list of
2739 * addresses in the format used by HELLOs. Should use
2740 * "GNUNET_HELLO_add_address" as a helper function.
2742 * @param cls the 'struct AddValidatedAddressContext' with the validated address
2743 * @param max maximum number of bytes that can be written to buf
2744 * @param buf where to write the address information
2745 * @return number of bytes written, 0 to signal the
2746 * end of the iteration.
2749 add_validated_address (void *cls,
2750 size_t max, void *buf)
2752 struct AddValidatedAddressContext *avac = cls;
2753 const struct ValidationEntry *ve = avac->ve;
2755 if (GNUNET_YES == avac->done)
2757 avac->done = GNUNET_YES;
2758 return GNUNET_HELLO_add_address (ve->transport_name,
2759 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2769 * Closure for 'check_address_exists'.
2771 struct CheckAddressExistsClosure
2774 * Address to check for.
2779 * Name of the transport.
2786 struct Session *session;
2789 * Set to GNUNET_YES if the address exists.
2802 * Iterator over hash map entries. Checks if the given
2803 * validation entry is for the same address as what is given
2806 * @param cls the 'struct CheckAddressExistsClosure*'
2807 * @param key current key code (ignored)
2808 * @param value value in the hash map ('struct ValidationEntry')
2809 * @return GNUNET_YES if we should continue to
2810 * iterate (mismatch), GNUNET_NO if not (entry matched)
2813 check_address_exists (void *cls,
2814 const GNUNET_HashCode * key,
2817 struct CheckAddressExistsClosure *caec = cls;
2818 struct ValidationEntry *ve = value;
2820 if ( (0 == strcmp (caec->tname,
2821 ve->transport_name)) &&
2822 (caec->addrlen == ve->addrlen) &&
2823 (0 == memcmp (caec->addr,
2827 caec->exists = GNUNET_YES;
2830 if ( (ve->session != NULL) &&
2831 (caec->session == ve->session) )
2833 caec->exists = GNUNET_YES;
2842 * Iterator to free entries in the validation_map.
2844 * @param cls closure (unused)
2845 * @param key current key code
2846 * @param value value in the hash map (validation to abort)
2847 * @return GNUNET_YES (always)
2850 abort_validation (void *cls,
2851 const GNUNET_HashCode * key,
2854 struct ValidationEntry *va = value;
2856 if (GNUNET_SCHEDULER_NO_TASK != va->timeout_task)
2857 GNUNET_SCHEDULER_cancel (va->timeout_task);
2858 GNUNET_free (va->transport_name);
2859 if (va->chvc != NULL)
2861 va->chvc->ve_count--;
2862 if (va->chvc->ve_count == 0)
2864 GNUNET_CONTAINER_DLL_remove (chvc_head,
2867 GNUNET_free (va->chvc);
2877 * HELLO validation cleanup task (validation failed).
2879 * @param cls the 'struct ValidationEntry' that failed
2880 * @param tc scheduler context (unused)
2883 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2885 struct ValidationEntry *va = cls;
2886 struct GNUNET_PeerIdentity pid;
2888 va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2889 GNUNET_STATISTICS_update (stats,
2890 gettext_noop ("# address validation timeouts"),
2893 GNUNET_CRYPTO_hash (&va->publicKey,
2895 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2897 GNUNET_break (GNUNET_OK ==
2898 GNUNET_CONTAINER_multihashmap_remove (validation_map,
2901 abort_validation (NULL, NULL, va);
2906 neighbour_timeout_task (void *cls,
2907 const struct GNUNET_SCHEDULER_TaskContext *tc)
2909 struct NeighbourList *n = cls;
2912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2913 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
2915 GNUNET_STATISTICS_update (stats,
2916 gettext_noop ("# disconnects due to timeout"),
2919 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2920 disconnect_neighbour (n, GNUNET_NO);
2925 * Schedule the job that will cause us to send a PING to the
2926 * foreign address to evaluate its validity and latency.
2928 * @param fal address to PING
2931 schedule_next_ping (struct ForeignAddressList *fal);
2935 * Add the given address to the list of foreign addresses
2936 * available for the given peer (check for duplicates).
2938 * @param cls the respective 'struct NeighbourList' to update
2939 * @param tname name of the transport
2940 * @param expiration expiration time
2941 * @param addr the address
2942 * @param addrlen length of the address
2943 * @return GNUNET_OK (always)
2946 add_to_foreign_address_list (void *cls,
2948 struct GNUNET_TIME_Absolute expiration,
2952 struct NeighbourList *n = cls;
2953 struct ForeignAddressList *fal;
2956 GNUNET_STATISTICS_update (stats,
2957 gettext_noop ("# valid peer addresses returned by PEERINFO"),
2961 fal = find_peer_address (n, tname, NULL, addr, addrlen);
2964 #if DEBUG_TRANSPORT_HELLO
2965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2966 "Adding address `%s' (%s) for peer `%4s' due to PEERINFO data for %llums.\n",
2967 a2s (tname, addr, addrlen),
2969 GNUNET_i2s (&n->id),
2970 expiration.abs_value);
2972 fal = add_peer_address (n, tname, NULL, addr, addrlen);
2975 GNUNET_STATISTICS_update (stats,
2976 gettext_noop ("# previously validated addresses lacking transport"),
2982 fal->expires = GNUNET_TIME_absolute_max (expiration,
2984 schedule_next_ping (fal);
2990 fal->expires = GNUNET_TIME_absolute_max (expiration,
2995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2996 "Failed to add new address for `%4s'\n",
2997 GNUNET_i2s (&n->id));
3000 if (fal->validated == GNUNET_NO)
3002 fal->validated = GNUNET_YES;
3003 GNUNET_STATISTICS_update (stats,
3004 gettext_noop ("# peer addresses considered valid"),
3008 if (try == GNUNET_YES)
3010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3011 "Have new addresses, will try to trigger transmissions.\n");
3012 try_transmission_to_peer (n);
3019 * Add addresses in validated HELLO "h" to the set of addresses
3020 * we have for this peer.
3022 * @param cls closure ('struct NeighbourList*')
3023 * @param peer id of the peer, NULL for last call
3024 * @param h hello message for the peer (can be NULL)
3025 * @param err_msg NULL if successful, otherwise contains error message
3028 add_hello_for_peer (void *cls,
3029 const struct GNUNET_PeerIdentity *peer,
3030 const struct GNUNET_HELLO_Message *h,
3031 const char *err_msg)
3033 struct NeighbourList *n = cls;
3035 if (err_msg != NULL)
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3038 _("Error in communication with PEERINFO service\n"));
3043 GNUNET_STATISTICS_update (stats,
3044 gettext_noop ("# outstanding peerinfo iterate requests"),
3051 return; /* no HELLO available */
3053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3054 "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
3058 if (GNUNET_YES != n->public_key_valid)
3060 GNUNET_HELLO_get_key (h, &n->publicKey);
3061 n->public_key_valid = GNUNET_YES;
3063 GNUNET_HELLO_iterate_addresses (h,
3065 &add_to_foreign_address_list,
3071 * Create a fresh entry in our neighbour list for the given peer.
3072 * Will try to transmit our current HELLO to the new neighbour.
3073 * Do not call this function directly, use 'setup_peer_check_blacklist.
3075 * @param peer the peer for which we create the entry
3076 * @param do_hello should we schedule transmitting a HELLO
3077 * @return the new neighbour list entry
3079 static struct NeighbourList *
3080 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
3083 struct NeighbourList *n;
3084 struct TransportPlugin *tp;
3085 struct ReadyList *rl;
3088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3089 "Setting up state for neighbour `%4s'\n",
3092 GNUNET_assert (our_hello != NULL);
3093 GNUNET_STATISTICS_update (stats,
3094 gettext_noop ("# active neighbours"),
3097 n = GNUNET_malloc (sizeof (struct NeighbourList));
3098 n->next = neighbours;
3102 GNUNET_TIME_relative_to_absolute
3103 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3104 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
3105 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3106 MAX_BANDWIDTH_CARRY_S);
3110 if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
3112 rl = GNUNET_malloc (sizeof (struct ReadyList));
3114 rl->next = n->plugins;
3117 rl->addresses = NULL;
3121 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
3123 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
3124 &neighbour_timeout_task, n);
3127 GNUNET_STATISTICS_update (stats,
3128 gettext_noop ("# peerinfo new neighbor iterate requests"),
3131 GNUNET_STATISTICS_update (stats,
3132 gettext_noop ("# outstanding peerinfo iterate requests"),
3135 n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
3136 GNUNET_TIME_UNIT_FOREVER_REL,
3137 &add_hello_for_peer, n);
3139 GNUNET_STATISTICS_update (stats,
3140 gettext_noop ("# HELLO's sent to new neighbors"),
3143 transmit_to_peer (NULL, NULL, 0,
3144 HELLO_ADDRESS_EXPIRATION,
3145 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
3153 * Function called after we have checked if communicating
3154 * with a given peer is acceptable.
3156 * @param cls closure
3157 * @param n NULL if communication is not acceptable
3159 typedef void (*SetupContinuation)(void *cls,
3160 struct NeighbourList *n);
3164 * Information kept for each client registered to perform
3170 * This is a linked list.
3172 struct Blacklisters *next;
3175 * This is a linked list.
3177 struct Blacklisters *prev;
3180 * Client responsible for this entry.
3182 struct GNUNET_SERVER_Client *client;
3185 * Blacklist check that we're currently performing.
3187 struct BlacklistCheck *bc;
3193 * Head of DLL of blacklisting clients.
3195 static struct Blacklisters *bl_head;
3198 * Tail of DLL of blacklisting clients.
3200 static struct Blacklisters *bl_tail;
3204 * Context we use when performing a blacklist check.
3206 struct BlacklistCheck
3210 * This is a linked list.
3212 struct BlacklistCheck *next;
3215 * This is a linked list.
3217 struct BlacklistCheck *prev;
3220 * Peer being checked.
3222 struct GNUNET_PeerIdentity peer;
3225 * Option for setup neighbour afterwards.
3230 * Continuation to call with the result.
3232 SetupContinuation cont;
3240 * Current transmission request handle for this client, or NULL if no
3241 * request is pending.
3243 struct GNUNET_CONNECTION_TransmitHandle *th;
3246 * Our current position in the blacklisters list.
3248 struct Blacklisters *bl_pos;
3251 * Current task performing the check.
3253 GNUNET_SCHEDULER_TaskIdentifier task;
3258 * Head of DLL of active blacklisting queries.
3260 static struct BlacklistCheck *bc_head;
3263 * Tail of DLL of active blacklisting queries.
3265 static struct BlacklistCheck *bc_tail;
3269 * Perform next action in the blacklist check.
3271 * @param cls the 'struct BlacklistCheck*'
3275 do_blacklist_check (void *cls,
3276 const struct GNUNET_SCHEDULER_TaskContext *tc);
3279 * Transmit blacklist query to the client.
3281 * @param cls the 'struct BlacklistCheck'
3282 * @param size number of bytes allowed
3283 * @param buf where to copy the message
3284 * @return number of bytes copied to buf
3287 transmit_blacklist_message (void *cls,
3291 struct BlacklistCheck *bc = cls;
3292 struct Blacklisters *bl;
3293 struct BlacklistMessage bm;
3298 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
3299 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3304 bm.header.size = htons (sizeof (struct BlacklistMessage));
3305 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
3306 bm.is_allowed = htonl (0);
3308 memcpy (buf, &bm, sizeof (bm));
3309 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
3315 * Perform next action in the blacklist check.
3317 * @param cls the 'struct BlacklistCheck*'
3321 do_blacklist_check (void *cls,
3322 const struct GNUNET_SCHEDULER_TaskContext *tc)
3324 struct BlacklistCheck *bc = cls;
3325 struct Blacklisters *bl;
3327 bc->task = GNUNET_SCHEDULER_NO_TASK;
3331 bc->cont (bc->cont_cls,
3332 setup_new_neighbour (&bc->peer, bc->do_hello));
3339 bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
3340 sizeof (struct BlacklistMessage),
3341 GNUNET_TIME_UNIT_FOREVER_REL,
3342 &transmit_blacklist_message,
3349 * Obtain a 'struct NeighbourList' for the given peer. If such an entry
3350 * does not yet exist, check the blacklist. If the blacklist says creating
3351 * one is acceptable, create one and call the continuation; otherwise
3352 * call the continuation with NULL.
3354 * @param peer peer to setup or look up a struct NeighbourList for
3355 * @param do_hello should we also schedule sending our HELLO to the peer
3356 * if this is a new record
3357 * @param cont function to call with the 'struct NeigbhbourList*'
3358 * @param cont_cls closure for cont
3361 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
3363 SetupContinuation cont,
3366 struct NeighbourList *n;
3367 struct BlacklistCheck *bc;
3369 n = find_neighbour(peer);
3376 if (bl_head == NULL)
3379 cont (cont_cls, setup_new_neighbour (peer, do_hello));
3381 setup_new_neighbour(peer, do_hello);
3384 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3385 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3387 bc->do_hello = do_hello;
3389 bc->cont_cls = cont_cls;
3390 bc->bl_pos = bl_head;
3391 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3397 * Function called with the result of querying a new blacklister about
3398 * it being allowed (or not) to continue to talk to an existing neighbour.
3400 * @param cls the original 'struct NeighbourList'
3401 * @param n NULL if we need to disconnect
3404 confirm_or_drop_neighbour (void *cls,
3405 struct NeighbourList *n)
3407 struct NeighbourList * orig = cls;
3411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3412 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&orig->id),
3413 "confirm_or_drop_neighboUr");
3414 disconnect_neighbour (orig, GNUNET_NO);
3420 * Handle a request to start a blacklist.
3422 * @param cls closure (always NULL)
3423 * @param client identification of the client
3424 * @param message the actual message
3427 handle_blacklist_init (void *cls,
3428 struct GNUNET_SERVER_Client *client,
3429 const struct GNUNET_MessageHeader *message)
3431 struct Blacklisters *bl;
3432 struct BlacklistCheck *bc;
3433 struct NeighbourList *n;
3438 if (bl->client == client)
3441 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3446 bl = GNUNET_malloc (sizeof (struct Blacklisters));
3447 bl->client = client;
3448 GNUNET_SERVER_client_keep (client);
3449 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
3450 /* confirm that all existing connections are OK! */
3454 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3455 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3457 bc->do_hello = GNUNET_NO;
3458 bc->cont = &confirm_or_drop_neighbour;
3461 if (n == neighbours) /* all would wait for the same client, no need to
3462 create more than just the first task right now */
3463 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3471 * Handle a request to blacklist a peer.
3473 * @param cls closure (always NULL)
3474 * @param client identification of the client
3475 * @param message the actual message
3478 handle_blacklist_reply (void *cls,
3479 struct GNUNET_SERVER_Client *client,
3480 const struct GNUNET_MessageHeader *message)
3482 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
3483 struct Blacklisters *bl;
3484 struct BlacklistCheck *bc;
3487 while ( (bl != NULL) &&
3488 (bl->client != client) )
3492 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3497 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
3499 bc->cont (bc->cont_cls, NULL);
3500 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
3505 bc->bl_pos = bc->bl_pos->next;
3506 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3509 /* check if any other bc's are waiting for this blacklister */
3513 if ( (bc->bl_pos == bl) &&
3514 (GNUNET_SCHEDULER_NO_TASK == bc->task) )
3515 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3523 * Send periodic PING messages to a given foreign address.
3525 * @param cls our 'struct PeriodicValidationContext*'
3526 * @param tc task context
3529 send_periodic_ping (void *cls,
3530 const struct GNUNET_SCHEDULER_TaskContext *tc)
3532 struct ForeignAddressList *peer_address = cls;
3533 struct TransportPlugin *tp;
3534 struct ValidationEntry *va;
3535 struct NeighbourList *neighbour;
3536 struct TransportPingMessage ping;
3537 struct CheckAddressExistsClosure caec;
3539 uint16_t hello_size;
3543 peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3544 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3546 tp = peer_address->ready_list->plugin;
3547 neighbour = peer_address->ready_list->neighbour;
3548 if (GNUNET_YES != neighbour->public_key_valid)
3550 /* no public key yet, try again later */
3551 schedule_next_ping (peer_address);
3554 caec.addr = peer_address->addr;
3555 caec.addrlen = peer_address->addrlen;
3556 caec.tname = tp->short_name;
3557 caec.session = peer_address->session;
3558 caec.exists = GNUNET_NO;
3559 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3560 &check_address_exists,
3562 if (caec.exists == GNUNET_YES)
3564 /* During validation attempts we will likely trigger the other
3565 peer trying to validate our address which in turn will cause
3566 it to send us its HELLO, so we expect to hit this case rather
3567 frequently. Only print something if we are very verbose. */
3568 #if DEBUG_TRANSPORT > 1
3569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3570 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3571 (peer_address->addr != NULL)
3572 ? a2s (tp->short_name,
3574 peer_address->addrlen)
3577 GNUNET_i2s (&neighbour->id));
3579 schedule_next_ping (peer_address);
3582 va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3583 va->transport_name = GNUNET_strdup (tp->short_name);
3584 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
3586 va->send_time = GNUNET_TIME_absolute_get();
3587 va->session = peer_address->session;
3588 if (peer_address->addr != NULL)
3590 va->addr = (const void*) &va[1];
3591 memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3592 va->addrlen = peer_address->addrlen;
3594 memcpy(&va->publicKey,
3595 &neighbour->publicKey,
3596 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3598 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
3599 &timeout_hello_validation,
3601 GNUNET_CONTAINER_multihashmap_put (validation_map,
3602 &neighbour->id.hashPubKey,
3604 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3606 if (peer_address->validated != GNUNET_YES)
3607 hello_size = GNUNET_HELLO_size(our_hello);
3611 tsize = sizeof(struct TransportPingMessage) + hello_size;
3613 if (peer_address->addr != NULL)
3615 slen = strlen (tp->short_name) + 1;
3616 tsize += slen + peer_address->addrlen;
3620 slen = 0; /* make gcc happy */
3622 message_buf = GNUNET_malloc(tsize);
3623 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3624 ping.challenge = htonl(va->challenge);
3625 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3626 if (peer_address->validated != GNUNET_YES)
3628 memcpy(message_buf, our_hello, hello_size);
3631 if (peer_address->addr != NULL)
3633 ping.header.size = htons(sizeof(struct TransportPingMessage) +
3634 peer_address->addrlen +
3636 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3639 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3641 peer_address->addrlen);
3645 ping.header.size = htons(sizeof(struct TransportPingMessage));
3648 memcpy(&message_buf[hello_size],
3650 sizeof(struct TransportPingMessage));
3652 #if DEBUG_TRANSPORT_REVALIDATION
3653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3654 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s'\n",
3655 (peer_address->addr != NULL)
3656 ? a2s (peer_address->plugin->short_name,
3658 peer_address->addrlen)
3661 GNUNET_i2s (&neighbour->id),
3662 "HELLO", hello_size,
3665 if (peer_address->validated != GNUNET_YES)
3666 GNUNET_STATISTICS_update (stats,
3667 gettext_noop ("# PING with HELLO messages sent"),
3671 GNUNET_STATISTICS_update (stats,
3672 gettext_noop ("# PING without HELLO messages sent"),
3675 GNUNET_STATISTICS_update (stats,
3676 gettext_noop ("# PING messages sent for re-validation"),
3679 transmit_to_peer (NULL, peer_address,
3680 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3681 HELLO_VERIFICATION_TIMEOUT,
3683 GNUNET_YES, neighbour);
3684 GNUNET_free(message_buf);
3685 schedule_next_ping (peer_address);
3690 * Schedule the job that will cause us to send a PING to the
3691 * foreign address to evaluate its validity and latency.
3693 * @param fal address to PING
3696 schedule_next_ping (struct ForeignAddressList *fal)
3698 struct GNUNET_TIME_Relative delay;
3700 if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3702 delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3703 delay.rel_value /= 2; /* do before expiration */
3704 delay = GNUNET_TIME_relative_min (delay,
3705 LATENCY_EVALUATION_MAX_DELAY);
3706 if (GNUNET_YES != fal->estimated)
3708 delay = GNUNET_TIME_UNIT_ZERO;
3709 fal->estimated = GNUNET_YES;
3711 if (GNUNET_YES == fal->connected)
3713 delay = GNUNET_TIME_relative_min (delay,
3714 CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3716 /* FIXME: also adjust delay based on how close the last
3717 observed latency is to the latency of the best alternative */
3718 /* bound how fast we can go */
3719 delay = GNUNET_TIME_relative_max (delay,
3720 GNUNET_TIME_UNIT_SECONDS);
3721 /* randomize a bit (to avoid doing all at the same time) */
3722 delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3723 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(delay,
3724 &send_periodic_ping,
3732 * Function that will be called if we receive some payload
3733 * from another peer.
3735 * @param message the payload
3736 * @param n peer who claimed to be the sender
3739 handle_payload_message (const struct GNUNET_MessageHeader *message,
3740 struct NeighbourList *n)
3742 struct InboundMessage *im;
3743 struct TransportClient *cpos;
3746 msize = ntohs (message->size);
3747 if (n->received_pong == GNUNET_NO)
3749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3750 "Received message of type %u and size %u from `%4s', but no pong yet!!\n",
3751 ntohs (message->type),
3752 ntohs (message->size),
3753 GNUNET_i2s (&n->id));
3754 GNUNET_free_non_null (n->pre_connect_message_buffer);
3755 n->pre_connect_message_buffer = GNUNET_malloc (msize);
3756 memcpy (n->pre_connect_message_buffer, message, msize);
3761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3762 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
3763 ntohs (message->type),
3764 ntohs (message->size),
3765 GNUNET_i2s (&n->id));
3767 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3770 n->quota_violation_count++;
3772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3773 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
3774 n->in_tracker.available_bytes_per_s__,
3775 n->quota_violation_count);
3777 /* Discount 32k per violation */
3778 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3783 if (n->quota_violation_count > 0)
3785 /* try to add 32k back */
3786 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3788 n->quota_violation_count--;
3791 GNUNET_STATISTICS_update (stats,
3792 gettext_noop ("# payload received from other peers"),
3795 /* transmit message to all clients */
3796 uint32_t ats_count = 2;
3797 size_t size = sizeof (struct InboundMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) + msize;
3798 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
3801 im = GNUNET_malloc (size);
3802 im->header.size = htons (size);
3803 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3805 im->ats_count = htonl(ats_count);
3806 /* Setting ATS data */
3807 (&(im->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
3808 (&(im->ats))[0].value = htonl (n->distance);
3809 (&(im->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
3810 (&(im->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
3811 (&(im->ats))[ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
3812 (&(im->ats))[ats_count].value = htonl (0);
3814 memcpy (&((&(im->ats))[ats_count+1]), message, msize);
3816 while (cpos != NULL)
3818 transmit_to_client (cpos, &im->header, GNUNET_YES);
3826 * Iterator over hash map entries. Checks if the given validation
3827 * entry is for the same challenge as what is given in the PONG.
3829 * @param cls the 'struct TransportPongMessage*'
3830 * @param key peer identity
3831 * @param value value in the hash map ('struct ValidationEntry')
3832 * @return GNUNET_YES if we should continue to
3833 * iterate (mismatch), GNUNET_NO if not (entry matched)
3836 check_pending_validation (void *cls,
3837 const GNUNET_HashCode * key,
3840 const struct TransportPongMessage *pong = cls;
3841 struct ValidationEntry *ve = value;
3842 struct AddValidatedAddressContext avac;
3843 unsigned int challenge = ntohl(pong->challenge);
3844 struct GNUNET_HELLO_Message *hello;
3845 struct GNUNET_PeerIdentity target;
3846 struct NeighbourList *n;
3847 struct ForeignAddressList *fal;
3848 struct OwnAddressList *oal;
3849 struct TransportPlugin *tp;
3850 struct GNUNET_MessageHeader *prem;
3856 ps = ntohs (pong->header.size);
3857 if (ps < sizeof (struct TransportPongMessage))
3859 GNUNET_break_op (0);
3862 addr = (const char*) &pong[1];
3863 slen = strlen (ve->transport_name) + 1;
3864 if ( (ps - sizeof (struct TransportPongMessage) < slen) ||
3865 (ve->challenge != challenge) ||
3866 (addr[slen-1] != '\0') ||
3867 (0 != strcmp (addr, ve->transport_name)) ||
3868 (ntohl (pong->purpose.size)
3869 != sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3871 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
3872 sizeof (struct GNUNET_PeerIdentity) + ps - sizeof (struct TransportPongMessage)) )
3877 alen = ps - sizeof (struct TransportPongMessage) - slen;
3878 switch (ntohl (pong->purpose.purpose))
3880 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN:
3881 if ( (ve->addrlen + slen != ntohl (pong->addrlen)) ||
3882 (0 != memcmp (&addr[slen],
3886 return GNUNET_YES; /* different entry, keep trying! */
3888 if (0 != memcmp (&pong->pid,
3890 sizeof (struct GNUNET_PeerIdentity)))
3892 GNUNET_break_op (0);
3896 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
3901 GNUNET_break_op (0);
3906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3907 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
3909 a2s (ve->transport_name,
3910 (const struct sockaddr *) ve->addr,
3912 ve->transport_name);
3915 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING:
3916 if (0 != memcmp (&pong->pid,
3918 sizeof (struct GNUNET_PeerIdentity)))
3920 GNUNET_break_op (0);
3923 if (ve->addrlen != 0)
3925 /* must have been for a different validation entry */
3928 tp = find_transport (ve->transport_name);
3934 oal = tp->addresses;
3937 if ( (oal->addrlen == alen) &&
3938 (0 == memcmp (&oal[1],
3946 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3947 _("Not accepting PONG with address `%s' since I cannot confirm having this address.\n"),
3948 a2s (ve->transport_name,
3954 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING,
3959 GNUNET_break_op (0);
3964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3965 "Confirmed that peer `%4s' is talking to us using address `%s' (%s) for us.\n",
3967 a2s (ve->transport_name,
3970 ve->transport_name);
3974 GNUNET_break_op (0);
3977 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
3979 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3980 _("Received expired signature. Check system time.\n"));
3983 GNUNET_STATISTICS_update (stats,
3984 gettext_noop ("# address validation successes"),
3987 /* create the updated HELLO */
3988 GNUNET_CRYPTO_hash (&ve->publicKey,
3989 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3990 &target.hashPubKey);
3991 if (ve->addr != NULL)
3993 avac.done = GNUNET_NO;
3995 hello = GNUNET_HELLO_create (&ve->publicKey,
3996 &add_validated_address,
3998 GNUNET_PEERINFO_add_peer (peerinfo,
4000 GNUNET_free (hello);
4002 n = find_neighbour (&target);
4005 n->publicKey = ve->publicKey;
4006 n->public_key_valid = GNUNET_YES;
4007 fal = add_peer_address (n,
4012 GNUNET_assert (fal != NULL);
4013 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
4014 fal->validated = GNUNET_YES;
4015 mark_address_connected (fal);
4016 GNUNET_STATISTICS_update (stats,
4017 gettext_noop ("# peer addresses considered valid"),
4020 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
4021 update_addr_value (fal, GNUNET_TIME_absolute_get_duration (ve->send_time).rel_value, GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
4023 schedule_next_ping (fal);
4024 if (n->latency.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
4025 n->latency = fal->latency;
4027 n->latency.rel_value = (fal->latency.rel_value + n->latency.rel_value) / 2;
4029 n->distance = fal->distance;
4030 if (GNUNET_NO == n->received_pong)
4032 n->received_pong = GNUNET_YES;
4034 notify_clients_connect (&target, n->latency, n->distance);
4035 if (NULL != (prem = n->pre_connect_message_buffer))
4037 n->pre_connect_message_buffer = NULL;
4038 handle_payload_message (prem, n);
4042 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4044 GNUNET_SCHEDULER_cancel (n->retry_task);
4045 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4046 try_transmission_to_peer (n);
4050 /* clean up validation entry */
4051 GNUNET_assert (GNUNET_YES ==
4052 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4055 abort_validation (NULL, NULL, ve);
4061 * Function that will be called if we receive a validation
4062 * of an address challenge that we transmitted to another
4063 * peer. Note that the validation should only be considered
4064 * acceptable if the challenge matches AND if the sender
4065 * address is at least a plausible address for this peer
4066 * (otherwise we may be seeing a MiM attack).
4068 * @param cls closure
4069 * @param message the pong message
4070 * @param peer who responded to our challenge
4071 * @param sender_address string describing our sender address (as observed
4072 * by the other peer in binary format)
4073 * @param sender_address_len number of bytes in 'sender_address'
4076 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
4077 const struct GNUNET_PeerIdentity *peer,
4078 const char *sender_address,
4079 size_t sender_address_len)
4081 #if DEBUG_TRANSPORT > 1
4082 /* we get tons of these that just get discarded, only log
4083 if we are quite verbose */
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4085 "Receiving `%s' message from `%4s'.\n", "PONG",
4088 GNUNET_STATISTICS_update (stats,
4089 gettext_noop ("# PONG messages received"),
4092 if (GNUNET_SYSERR !=
4093 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
4095 &check_pending_validation,
4098 /* This is *expected* to happen a lot since we send
4099 PONGs to *all* known addresses of the sender of
4100 the PING, so most likely we get multiple PONGs
4101 per PING, and all but the first PONG will end up
4102 here. So really we should not print anything here
4103 unless we want to be very, very verbose... */
4104 #if DEBUG_TRANSPORT > 2
4105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4106 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
4118 * Try to validate a neighbour's address by sending him our HELLO and a PING.
4120 * @param cls the 'struct ValidationEntry*'
4121 * @param neighbour neighbour to validate, NULL if validation failed
4124 transmit_hello_and_ping (void *cls,
4125 struct NeighbourList *neighbour)
4127 struct ValidationEntry *va = cls;
4128 struct ForeignAddressList *peer_address;
4129 struct TransportPingMessage ping;
4130 uint16_t hello_size;
4133 struct GNUNET_PeerIdentity id;
4136 GNUNET_CRYPTO_hash (&va->publicKey,
4137 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4139 if (neighbour == NULL)
4141 /* FIXME: stats... */
4142 GNUNET_break (GNUNET_OK ==
4143 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4146 abort_validation (NULL, NULL, va);
4149 neighbour->publicKey = va->publicKey;
4150 neighbour->public_key_valid = GNUNET_YES;
4151 peer_address = add_peer_address (neighbour,
4152 va->transport_name, NULL,
4153 (const void*) &va[1],
4155 if (peer_address == NULL)
4157 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4158 "Failed to add peer `%4s' for plugin `%s'\n",
4159 GNUNET_i2s (&neighbour->id),
4160 va->transport_name);
4161 GNUNET_break (GNUNET_OK ==
4162 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4165 abort_validation (NULL, NULL, va);
4168 hello_size = GNUNET_HELLO_size(our_hello);
4169 slen = strlen(va->transport_name) + 1;
4170 tsize = sizeof(struct TransportPingMessage) + hello_size + va->addrlen + slen;
4171 message_buf = GNUNET_malloc(tsize);
4172 ping.challenge = htonl(va->challenge);
4173 ping.header.size = htons(sizeof(struct TransportPingMessage) + slen + va->addrlen);
4174 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
4175 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
4176 memcpy(message_buf, our_hello, hello_size);
4177 memcpy(&message_buf[hello_size],
4179 sizeof(struct TransportPingMessage));
4180 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
4183 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
4187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4188 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
4191 : a2s (va->transport_name,
4192 (const void*) &va[1], va->addrlen),
4194 GNUNET_i2s (&neighbour->id),
4195 "HELLO", hello_size,
4196 "PING", sizeof (struct TransportPingMessage) + va->addrlen + slen);
4199 GNUNET_STATISTICS_update (stats,
4200 gettext_noop ("# PING messages sent for initial validation"),
4203 transmit_to_peer (NULL, peer_address,
4204 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
4205 HELLO_VERIFICATION_TIMEOUT,
4207 GNUNET_YES, neighbour);
4208 GNUNET_free(message_buf);
4213 * Check if the given address is already being validated; if not,
4214 * append the given address to the list of entries that are being be
4215 * validated and initiate validation.
4217 * @param cls closure ('struct CheckHelloValidatedContext *')
4218 * @param tname name of the transport
4219 * @param expiration expiration time
4220 * @param addr the address
4221 * @param addrlen length of the address
4222 * @return GNUNET_OK (always)
4225 run_validation (void *cls,
4227 struct GNUNET_TIME_Absolute expiration,
4231 struct CheckHelloValidatedContext *chvc = cls;
4232 struct GNUNET_PeerIdentity id;
4233 struct TransportPlugin *tp;
4234 struct ValidationEntry *va;
4235 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4236 struct CheckAddressExistsClosure caec;
4237 struct OwnAddressList *oal;
4239 GNUNET_assert (addr != NULL);
4241 GNUNET_STATISTICS_update (stats,
4242 gettext_noop ("# peer addresses scheduled for validation"),
4245 tp = find_transport (tname);
4248 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
4249 GNUNET_ERROR_TYPE_BULK,
4251 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
4253 GNUNET_STATISTICS_update (stats,
4254 gettext_noop ("# peer addresses not validated (plugin not available)"),
4259 /* check if this is one of our own addresses */
4260 oal = tp->addresses;
4263 if ( (oal->addrlen == addrlen) &&
4264 (0 == memcmp (&oal[1],
4268 /* not plausible, this address is equivalent to our own address! */
4269 GNUNET_STATISTICS_update (stats,
4270 gettext_noop ("# peer addresses not validated (loopback)"),
4277 GNUNET_HELLO_get_key (chvc->hello, &pk);
4278 GNUNET_CRYPTO_hash (&pk,
4280 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4283 if (is_blacklisted(&id, tp))
4286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4287 "Attempted to validate blacklisted peer `%s' using `%s'!\n",
4295 caec.addrlen = addrlen;
4296 caec.session = NULL;
4298 caec.exists = GNUNET_NO;
4299 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
4300 &check_address_exists,
4302 if (caec.exists == GNUNET_YES)
4304 /* During validation attempts we will likely trigger the other
4305 peer trying to validate our address which in turn will cause
4306 it to send us its HELLO, so we expect to hit this case rather
4307 frequently. Only print something if we are very verbose. */
4308 #if DEBUG_TRANSPORT > 1
4309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4310 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
4311 a2s (tname, addr, addrlen),
4315 GNUNET_STATISTICS_update (stats,
4316 gettext_noop ("# peer addresses not validated (in progress)"),
4321 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
4324 va->transport_name = GNUNET_strdup (tname);
4325 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
4327 va->send_time = GNUNET_TIME_absolute_get();
4328 va->addr = (const void*) &va[1];
4329 memcpy (&va[1], addr, addrlen);
4330 va->addrlen = addrlen;
4331 GNUNET_HELLO_get_key (chvc->hello,
4333 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
4334 &timeout_hello_validation,
4336 GNUNET_CONTAINER_multihashmap_put (validation_map,
4339 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4340 setup_peer_check_blacklist (&id, GNUNET_NO,
4341 &transmit_hello_and_ping,
4348 * Check if addresses in validated hello "h" overlap with
4349 * those in "chvc->hello" and validate the rest.
4351 * @param cls closure
4352 * @param peer id of the peer, NULL for last call
4353 * @param h hello message for the peer (can be NULL)
4354 * @param err_msg NULL if successful, otherwise contains error message
4357 check_hello_validated (void *cls,
4358 const struct GNUNET_PeerIdentity *peer,
4359 const struct GNUNET_HELLO_Message *h,
4360 const char *err_msg)
4362 struct CheckHelloValidatedContext *chvc = cls;
4363 struct GNUNET_HELLO_Message *plain_hello;
4364 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4365 struct GNUNET_PeerIdentity target;
4366 struct NeighbourList *n;
4368 if (err_msg != NULL)
4370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4371 _("Error in communication with PEERINFO service\n"));
4377 GNUNET_STATISTICS_update (stats,
4378 gettext_noop ("# outstanding peerinfo iterate requests"),
4382 if (GNUNET_NO == chvc->hello_known)
4384 /* notify PEERINFO about the peer now, so that we at least
4385 have the public key if some other component needs it */
4386 GNUNET_HELLO_get_key (chvc->hello, &pk);
4387 GNUNET_CRYPTO_hash (&pk,
4388 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4389 &target.hashPubKey);
4390 plain_hello = GNUNET_HELLO_create (&pk,
4393 GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
4394 GNUNET_free (plain_hello);
4395 #if DEBUG_TRANSPORT_HELLO
4396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4397 "PEERINFO had no `%s' message for peer `%4s', full validation needed.\n",
4399 GNUNET_i2s (&target));
4401 GNUNET_STATISTICS_update (stats,
4402 gettext_noop ("# new HELLOs requiring full validation"),
4405 GNUNET_HELLO_iterate_addresses (chvc->hello,
4412 GNUNET_STATISTICS_update (stats,
4413 gettext_noop ("# duplicate HELLO (peer known)"),
4418 if (chvc->ve_count == 0)
4420 GNUNET_CONTAINER_DLL_remove (chvc_head,
4429 #if DEBUG_TRANSPORT_HELLO
4430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4431 "PEERINFO had `%s' message for peer `%4s', validating only new addresses.\n",
4435 chvc->hello_known = GNUNET_YES;
4436 n = find_neighbour (peer);
4439 #if DEBUG_TRANSPORT_HELLO
4440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4441 "Calling hello_iterate_addresses for %s!\n",
4444 GNUNET_HELLO_iterate_addresses (h,
4446 &add_to_foreign_address_list,
4448 try_transmission_to_peer (n);
4452 #if DEBUG_TRANSPORT_HELLO
4453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4454 "No existing neighbor record for %s!\n",
4457 GNUNET_STATISTICS_update (stats,
4458 gettext_noop ("# no existing neighbour record (validating HELLO)"),
4462 GNUNET_STATISTICS_update (stats,
4463 gettext_noop ("# HELLO validations (update case)"),
4466 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
4468 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
4475 * Process HELLO-message.
4477 * @param plugin transport involved, may be NULL
4478 * @param message the actual message
4479 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
4482 process_hello (struct TransportPlugin *plugin,
4483 const struct GNUNET_MessageHeader *message)
4486 struct GNUNET_PeerIdentity target;
4487 const struct GNUNET_HELLO_Message *hello;
4488 struct CheckHelloValidatedContext *chvc;
4489 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
4490 #if DEBUG_TRANSPORT_HELLO > 2
4493 hsize = ntohs (message->size);
4494 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
4495 (hsize < sizeof (struct GNUNET_MessageHeader)))
4498 return GNUNET_SYSERR;
4500 GNUNET_STATISTICS_update (stats,
4501 gettext_noop ("# HELLOs received for validation"),
4505 /* first, check if load is too high */
4506 if (GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
4508 GNUNET_STATISTICS_update (stats,
4509 gettext_noop ("# HELLOs ignored due to high load"),
4512 #if DEBUG_TRANSPORT_HELLO
4513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4514 "Ignoring `%s' for `%4s', load too high.\n",
4516 GNUNET_i2s (&target));
4520 hello = (const struct GNUNET_HELLO_Message *) message;
4521 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
4523 #if DEBUG_TRANSPORT_HELLO
4524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4525 "Unable to get public key from `%s' for `%4s'!\n",
4527 GNUNET_i2s (&target));
4529 GNUNET_break_op (0);
4530 return GNUNET_SYSERR;
4533 GNUNET_CRYPTO_hash (&publicKey,
4534 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4535 &target.hashPubKey);
4537 #if DEBUG_TRANSPORT_HELLO
4538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4539 "Received `%s' message for `%4s'\n",
4541 GNUNET_i2s (&target));
4544 if (0 == memcmp (&my_identity,
4546 sizeof (struct GNUNET_PeerIdentity)))
4548 GNUNET_STATISTICS_update (stats,
4549 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
4555 while (NULL != chvc)
4557 if (GNUNET_HELLO_equals (hello,
4559 GNUNET_TIME_absolute_get ()).abs_value > 0)
4561 #if DEBUG_TRANSPORT_HELLO > 2
4562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4563 "Received duplicate `%s' message for `%4s'; ignored\n",
4565 GNUNET_i2s (&target));
4567 return GNUNET_OK; /* validation already pending */
4569 if (GNUNET_HELLO_size(hello) == GNUNET_HELLO_size (chvc->hello))
4570 GNUNET_break (0 != memcmp (hello, chvc->hello,
4571 GNUNET_HELLO_size(hello)));
4576 struct NeighbourList *temp_neighbor = find_neighbour(&target);
4577 if ((NULL != temp_neighbor))
4579 fprintf(stderr, "Already know peer, ignoring hello\n");
4584 #if DEBUG_TRANSPORT_HELLO > 2
4587 my_id = GNUNET_strdup(GNUNET_i2s(plugin->env.my_identity));
4588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4589 "%s: Starting validation of `%s' message for `%4s' via '%s' of size %u\n",
4592 GNUNET_i2s (&target),
4594 GNUNET_HELLO_size(hello));
4598 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
4600 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
4601 memcpy (&chvc[1], hello, hsize);
4602 GNUNET_CONTAINER_DLL_insert (chvc_head,
4605 /* finally, check if HELLO was previously validated
4606 (continuation will then schedule actual validation) */
4607 GNUNET_STATISTICS_update (stats,
4608 gettext_noop ("# peerinfo process hello iterate requests"),
4611 GNUNET_STATISTICS_update (stats,
4612 gettext_noop ("# outstanding peerinfo iterate requests"),
4615 chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
4617 HELLO_VERIFICATION_TIMEOUT,
4618 &check_hello_validated, chvc);
4624 * The peer specified by the given neighbour has timed-out or a plugin
4625 * has disconnected. We may either need to do nothing (other plugins
4626 * still up), or trigger a full disconnect and clean up. This
4627 * function updates our state and does the necessary notifications.
4628 * Also notifies our clients that the neighbour is now officially
4631 * @param n the neighbour list entry for the peer
4632 * @param check GNUNET_YES to check if ALL addresses for this peer
4633 * are gone, GNUNET_NO to force a disconnect of the peer
4634 * regardless of whether other addresses exist.
4637 disconnect_neighbour (struct NeighbourList *n, int check)
4639 struct ReadyList *rpos;
4640 struct NeighbourList *npos;
4641 struct NeighbourList *nprev;
4642 struct MessageQueue *mq;
4643 struct ForeignAddressList *peer_addresses;
4644 struct ForeignAddressList *peer_pos;
4646 if (GNUNET_YES == check)
4649 while (NULL != rpos)
4651 peer_addresses = rpos->addresses;
4652 while (peer_addresses != NULL)
4654 if (GNUNET_YES == peer_addresses->connected)
4656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4657 "NOT Disconnecting from `%4s', still have live addresses!\n",
4658 GNUNET_i2s (&n->id));
4659 return; /* still connected */
4661 peer_addresses = peer_addresses->next;
4667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4668 "Disconnecting from `%4s'\n",
4669 GNUNET_i2s (&n->id));
4671 /* remove n from neighbours list */
4674 while ((npos != NULL) && (npos != n))
4679 GNUNET_assert (npos != NULL);
4681 neighbours = n->next;
4683 nprev->next = n->next;
4685 /* notify all clients about disconnect */
4686 if (GNUNET_YES == n->received_pong)
4687 notify_clients_disconnect (&n->id);
4689 /* clean up all plugins, cancel connections and pending transmissions */
4690 while (NULL != (rpos = n->plugins))
4692 n->plugins = rpos->next;
4693 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
4694 while (rpos->addresses != NULL)
4696 peer_pos = rpos->addresses;
4697 rpos->addresses = peer_pos->next;
4698 if (peer_pos->connected == GNUNET_YES)
4699 GNUNET_STATISTICS_update (stats,
4700 gettext_noop ("# connected addresses"),
4703 if (GNUNET_YES == peer_pos->validated)
4704 GNUNET_STATISTICS_update (stats,
4705 gettext_noop ("# peer addresses considered valid"),
4708 if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
4710 GNUNET_SCHEDULER_cancel (peer_pos->revalidate_task);
4711 peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
4713 GNUNET_free(peer_pos->ressources);
4714 GNUNET_free(peer_pos->quality);
4715 GNUNET_free(peer_pos);
4720 /* free all messages on the queue */
4721 while (NULL != (mq = n->messages_head))
4723 GNUNET_STATISTICS_update (stats,
4724 gettext_noop ("# bytes in message queue for other peers"),
4725 - (int64_t) mq->message_buf_size,
4727 GNUNET_STATISTICS_update (stats,
4728 gettext_noop ("# bytes discarded due to disconnect"),
4729 mq->message_buf_size,
4731 GNUNET_CONTAINER_DLL_remove (n->messages_head,
4734 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4736 sizeof(struct GNUNET_PeerIdentity)));
4739 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
4741 GNUNET_SCHEDULER_cancel (n->timeout_task);
4742 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4744 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4746 GNUNET_SCHEDULER_cancel (n->retry_task);
4747 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4749 if (n->piter != NULL)
4751 GNUNET_PEERINFO_iterate_cancel (n->piter);
4752 GNUNET_STATISTICS_update (stats,
4753 gettext_noop ("# outstanding peerinfo iterate requests"),
4758 /* finally, free n itself */
4759 GNUNET_STATISTICS_update (stats,
4760 gettext_noop ("# active neighbours"),
4763 GNUNET_free_non_null (n->pre_connect_message_buffer);
4769 * We have received a PING message from someone. Need to send a PONG message
4770 * in response to the peer by any means necessary.
4773 handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
4774 const struct GNUNET_PeerIdentity *peer,
4775 struct Session *session,
4776 const char *sender_address,
4777 uint16_t sender_address_len)
4779 struct TransportPlugin *plugin = cls;
4780 struct SessionHeader *session_header = (struct SessionHeader*) session;
4781 struct TransportPingMessage *ping;
4782 struct TransportPongMessage *pong;
4783 struct NeighbourList *n;
4784 struct ReadyList *rl;
4785 struct ForeignAddressList *fal;
4786 struct OwnAddressList *oal;
4791 if (ntohs (message->size) < sizeof (struct TransportPingMessage))
4793 GNUNET_break_op (0);
4794 return GNUNET_SYSERR;
4797 ping = (struct TransportPingMessage *) message;
4798 if (0 != memcmp (&ping->target,
4799 plugin->env.my_identity,
4800 sizeof (struct GNUNET_PeerIdentity)))
4802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4803 _("Received `%s' message from `%s' destined for `%s' which is not me!\n"),
4805 (sender_address != NULL)
4806 ? a2s (plugin->short_name,
4807 (const struct sockaddr *)sender_address,
4810 GNUNET_i2s (&ping->target));
4811 return GNUNET_SYSERR;
4814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4815 "Processing `%s' from `%s'\n",
4817 (sender_address != NULL)
4818 ? a2s (plugin->short_name,
4819 (const struct sockaddr *)sender_address,
4823 GNUNET_STATISTICS_update (stats,
4824 gettext_noop ("# PING messages received"),
4827 addr = (const char*) &ping[1];
4828 alen = ntohs (message->size) - sizeof (struct TransportPingMessage);
4829 slen = strlen (plugin->short_name) + 1;
4832 /* peer wants to confirm that we have an outbound connection to him */
4833 if (session == NULL)
4835 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4836 _("Refusing to create PONG since I do not have a session with `%s'.\n"),
4838 return GNUNET_SYSERR;
4840 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len + slen);
4841 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len + slen);
4842 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
4843 pong->purpose.size =
4844 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4846 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
4847 sizeof (struct GNUNET_PeerIdentity) + sender_address_len + slen);
4848 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING);
4849 pong->challenge = ping->challenge;
4850 pong->addrlen = htonl(sender_address_len + slen);
4853 sizeof(struct GNUNET_PeerIdentity));
4857 if ((sender_address!=NULL) && (sender_address_len > 0))
4858 memcpy (&((char*)&pong[1])[slen],
4860 sender_address_len);
4861 if (GNUNET_TIME_absolute_get_remaining (session_header->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
4863 /* create / update cached sig */
4865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4866 "Creating PONG signature to indicate active connection.\n");
4868 session_header->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
4869 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
4870 GNUNET_assert (GNUNET_OK ==
4871 GNUNET_CRYPTO_rsa_sign (my_private_key,
4873 &session_header->pong_signature));
4877 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
4879 memcpy (&pong->signature,
4880 &session_header->pong_signature,
4881 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4887 /* peer wants to confirm that this is one of our addresses */
4891 plugin->api->check_address (plugin->api->cls,
4895 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4896 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
4897 a2s (plugin->short_name,
4902 oal = plugin->addresses;
4905 if ( (oal->addrlen == alen) &&
4912 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
4913 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
4914 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
4915 pong->purpose.size =
4916 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4918 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
4919 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
4920 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
4921 pong->challenge = ping->challenge;
4922 pong->addrlen = htonl(alen + slen);
4925 sizeof(struct GNUNET_PeerIdentity));
4926 memcpy (&pong[1], plugin->short_name, slen);
4927 memcpy (&((char*)&pong[1])[slen], addr, alen);
4928 if ( (oal != NULL) &&
4929 (GNUNET_TIME_absolute_get_remaining (oal->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4) )
4931 /* create / update cached sig */
4933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4934 "Creating PONG signature to indicate ownership.\n");
4936 oal->pong_sig_expires = GNUNET_TIME_absolute_min (oal->expires,
4937 GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
4938 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
4939 GNUNET_assert (GNUNET_OK ==
4940 GNUNET_CRYPTO_rsa_sign (my_private_key,
4942 &oal->pong_signature));
4943 memcpy (&pong->signature,
4944 &oal->pong_signature,
4945 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4947 else if (oal == NULL)
4949 /* not using cache (typically DV-only) */
4950 pong->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
4951 GNUNET_assert (GNUNET_OK ==
4952 GNUNET_CRYPTO_rsa_sign (my_private_key,
4958 /* can used cached version */
4959 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
4960 memcpy (&pong->signature,
4961 &oal->pong_signature,
4962 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4965 n = find_neighbour(peer);
4966 GNUNET_assert (n != NULL);
4967 /* first try reliable response transmission */
4971 fal = rl->addresses;
4974 if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
4977 ntohs (pong->header.size),
4978 TRANSPORT_PONG_PRIORITY,
4979 HELLO_VERIFICATION_TIMEOUT,
4987 GNUNET_STATISTICS_update (stats,
4988 gettext_noop ("# PONGs unicast via reliable transport"),
4998 /* no reliable method found, do multicast */
4999 GNUNET_STATISTICS_update (stats,
5000 gettext_noop ("# PONGs multicast to all available addresses"),
5006 fal = rl->addresses;
5009 transmit_to_peer(NULL, fal,
5010 TRANSPORT_PONG_PRIORITY,
5011 HELLO_VERIFICATION_TIMEOUT,
5013 ntohs(pong->header.size),
5029 * Function called by the plugin for each received message.
5030 * Update data volumes, possibly notify plugins about
5031 * reducing the rate at which they read from the socket
5032 * and generally forward to our receive callback.
5034 * @param cls the "struct TransportPlugin *" we gave to the plugin
5035 * @param peer (claimed) identity of the other peer
5036 * @param message the message, NULL if we only care about
5037 * learning about the delay until we should receive again
5038 * @param ats_data information for automatic transport selection
5039 * @param ats_count number of elements in ats not including 0-terminator
5040 * @param session identifier used for this session (can be NULL)
5041 * @param sender_address binary address of the sender (if observed)
5042 * @param sender_address_len number of bytes in sender_address
5043 * @return how long in ms the plugin should wait until receiving more data
5044 * (plugins that do not support this, can ignore the return value)
5046 static struct GNUNET_TIME_Relative
5047 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
5048 const struct GNUNET_MessageHeader *message,
5049 const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
5051 struct Session *session,
5052 const char *sender_address,
5053 uint16_t sender_address_len)
5055 struct TransportPlugin *plugin = cls;
5056 struct ReadyList *service_context;
5057 struct ForeignAddressList *peer_address;
5059 struct NeighbourList *n;
5060 struct GNUNET_TIME_Relative ret;
5061 if (is_blacklisted (peer, plugin))
5062 return GNUNET_TIME_UNIT_FOREVER_REL;
5066 n = find_neighbour (peer);
5068 n = setup_new_neighbour (peer, GNUNET_YES);
5069 service_context = n->plugins;
5070 while ((service_context != NULL) && (plugin != service_context->plugin))
5071 service_context = service_context->next;
5072 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
5073 peer_address = NULL;
5076 for (c=0; c<ats_count; c++)
5078 if (ntohl(ats_data[c].type) == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
5080 distance = ntohl(ats_data[c].value);
5083 /* notify ATS about incoming data */
5084 //ats_notify_ats_data(peer, ats_data);
5086 if (message != NULL)
5088 if ( (session != NULL) ||
5089 (sender_address != NULL) )
5090 peer_address = add_peer_address (n,
5094 sender_address_len);
5095 if (peer_address != NULL)
5098 update_addr_ats(peer_address, ats_data, ats_count);
5099 update_addr_value(peer_address, distance, GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5101 peer_address->distance = distance;
5102 if (GNUNET_YES == peer_address->validated)
5103 mark_address_connected (peer_address);
5104 peer_address->timeout
5106 GNUNET_TIME_relative_to_absolute
5107 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5108 schedule_next_ping (peer_address);
5110 /* update traffic received amount ... */
5111 msize = ntohs (message->size);
5112 GNUNET_STATISTICS_update (stats,
5113 gettext_noop ("# bytes received from other peers"),
5116 n->distance = distance;
5118 GNUNET_TIME_relative_to_absolute
5119 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5120 GNUNET_SCHEDULER_cancel (n->timeout_task);
5122 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
5123 &neighbour_timeout_task, n);
5124 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
5126 /* dropping message due to frequent inbound volume violations! */
5127 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
5128 GNUNET_ERROR_TYPE_BULK,
5130 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
5131 n->in_tracker.available_bytes_per_s__,
5132 n->quota_violation_count);
5133 GNUNET_STATISTICS_update (stats,
5134 gettext_noop ("# bandwidth quota violations by other peers"),
5137 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
5141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5142 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
5143 ntohs (message->type),
5144 ntohs (message->size),
5147 switch (ntohs (message->type))
5149 case GNUNET_MESSAGE_TYPE_HELLO:
5150 GNUNET_STATISTICS_update (stats,
5151 gettext_noop ("# HELLO messages received from other peers"),
5154 process_hello (plugin, message);
5156 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
5157 handle_ping (plugin, message, peer, session, sender_address, sender_address_len);
5159 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
5160 handle_pong (plugin, message, peer, sender_address, sender_address_len);
5163 handle_payload_message (message, n);
5167 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
5168 if (ret.rel_value > 0)
5170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5171 "Throttling read (%llu bytes excess at %u b/s), waiting %llums before reading more.\n",
5172 (unsigned long long) n->in_tracker.consumption_since_last_update__,
5173 (unsigned int) n->in_tracker.available_bytes_per_s__,
5174 (unsigned long long) ret.rel_value);
5175 GNUNET_STATISTICS_update (stats,
5176 gettext_noop ("# ms throttling suggested"),
5177 (int64_t) ret.rel_value,
5184 * Handle START-message. This is the first message sent to us
5185 * by any client which causes us to add it to our list.
5187 * @param cls closure (always NULL)
5188 * @param client identification of the client
5189 * @param message the actual message
5192 handle_start (void *cls,
5193 struct GNUNET_SERVER_Client *client,
5194 const struct GNUNET_MessageHeader *message)
5196 const struct StartMessage *start;
5197 struct TransportClient *c;
5198 struct ConnectInfoMessage * cim;
5199 struct NeighbourList *n;
5203 start = (const struct StartMessage*) message;
5205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5206 "Received `%s' request from client\n", "START");
5211 if (c->client == client)
5213 /* client already on our list! */
5215 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5220 if ( (GNUNET_NO != ntohl (start->do_check)) &&
5221 (0 != memcmp (&start->self,
5223 sizeof (struct GNUNET_PeerIdentity))) )
5225 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5226 _("Rejecting control connection from peer `%s', which is not me!\n"),
5227 GNUNET_i2s (&start->self));
5228 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5231 c = GNUNET_malloc (sizeof (struct TransportClient));
5235 if (our_hello != NULL)
5238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5239 "Sending our own `%s' to new client\n", "HELLO");
5241 transmit_to_client (c,
5242 (const struct GNUNET_MessageHeader *) our_hello,
5244 /* tell new client about all existing connections */
5246 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
5247 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
5251 cim = GNUNET_malloc (size);
5252 cim->header.size = htons (size);
5253 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
5254 cim->ats_count = htonl(ats_count);
5255 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
5256 (&(cim->ats))[2].value = htonl (0);
5260 if (GNUNET_YES == n->received_pong)
5262 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5263 (&(cim->ats))[0].value = htonl (n->distance);
5264 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
5265 (&(cim->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
5267 transmit_to_client (c, &cim->header, GNUNET_NO);
5273 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5278 * Handle HELLO-message.
5280 * @param cls closure (always NULL)
5281 * @param client identification of the client
5282 * @param message the actual message
5285 handle_hello (void *cls,
5286 struct GNUNET_SERVER_Client *client,
5287 const struct GNUNET_MessageHeader *message)
5291 GNUNET_STATISTICS_update (stats,
5292 gettext_noop ("# HELLOs received from clients"),
5295 ret = process_hello (NULL, message);
5296 GNUNET_SERVER_receive_done (client, ret);
5301 * Closure for 'transmit_client_message'; followed by
5302 * 'msize' bytes of the actual message.
5304 struct TransmitClientMessageContext
5307 * Client on whom's behalf we are sending.
5309 struct GNUNET_SERVER_Client *client;
5312 * Timeout for the transmission.
5314 struct GNUNET_TIME_Absolute timeout;
5322 * Size of the message in bytes.
5329 * Schedule transmission of a message we got from a client to a peer.
5331 * @param cls the 'struct TransmitClientMessageContext*'
5332 * @param n destination, or NULL on error (in that case, drop the message)
5335 transmit_client_message (void *cls,
5336 struct NeighbourList *n)
5338 struct TransmitClientMessageContext *tcmc = cls;
5339 struct TransportClient *tc;
5342 while ((tc != NULL) && (tc->client != tcmc->client))
5347 transmit_to_peer (tc, NULL, tcmc->priority,
5348 GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
5350 tcmc->msize, GNUNET_NO, n);
5352 GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
5353 GNUNET_SERVER_client_drop (tcmc->client);
5359 * Handle SEND-message.
5361 * @param cls closure (always NULL)
5362 * @param client identification of the client
5363 * @param message the actual message
5366 handle_send (void *cls,
5367 struct GNUNET_SERVER_Client *client,
5368 const struct GNUNET_MessageHeader *message)
5370 const struct OutboundMessage *obm;
5371 const struct GNUNET_MessageHeader *obmm;
5372 struct TransmitClientMessageContext *tcmc;
5376 size = ntohs (message->size);
5378 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
5381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5384 GNUNET_STATISTICS_update (stats,
5385 gettext_noop ("# payload received for other peers"),
5388 obm = (const struct OutboundMessage *) message;
5389 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
5390 msize = size - sizeof (struct OutboundMessage);
5392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5393 "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
5394 "SEND", GNUNET_i2s (&obm->peer),
5398 tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
5399 tcmc->client = client;
5400 tcmc->priority = ntohl (obm->priority);
5401 tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
5402 tcmc->msize = msize;
5403 /* FIXME: this memcpy can be up to 7% of our total runtime */
5404 memcpy (&tcmc[1], obmm, msize);
5405 GNUNET_SERVER_client_keep (client);
5406 setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
5407 &transmit_client_message,
5413 * Handle request connect message
5415 * @param cls closure (always NULL)
5416 * @param client identification of the client
5417 * @param message the actual message
5420 handle_request_connect (void *cls,
5421 struct GNUNET_SERVER_Client *client,
5422 const struct GNUNET_MessageHeader *message)
5424 const struct TransportRequestConnectMessage *trcm =
5425 (const struct TransportRequestConnectMessage *) message;
5427 GNUNET_STATISTICS_update (stats,
5428 gettext_noop ("# REQUEST CONNECT messages received"),
5431 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a request connect message for peer %s\n", GNUNET_i2s(&trcm->peer));
5432 setup_peer_check_blacklist (&trcm->peer, GNUNET_YES,
5434 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5438 * Handle SET_QUOTA-message.
5440 * @param cls closure (always NULL)
5441 * @param client identification of the client
5442 * @param message the actual message
5445 handle_set_quota (void *cls,
5446 struct GNUNET_SERVER_Client *client,
5447 const struct GNUNET_MessageHeader *message)
5449 const struct QuotaSetMessage *qsm =
5450 (const struct QuotaSetMessage *) message;
5451 struct NeighbourList *n;
5453 GNUNET_STATISTICS_update (stats,
5454 gettext_noop ("# SET QUOTA messages received"),
5457 n = find_neighbour (&qsm->peer);
5460 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5461 GNUNET_STATISTICS_update (stats,
5462 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
5468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5469 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
5471 (unsigned int) ntohl (qsm->quota.value__),
5472 (unsigned int) n->in_tracker.available_bytes_per_s__,
5473 GNUNET_i2s (&qsm->peer));
5475 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
5477 if (0 == ntohl (qsm->quota.value__))
5479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5480 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&n->id),
5482 disconnect_neighbour (n, GNUNET_NO);
5484 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5489 * Take the given address and append it to the set of results sent back to
5492 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
5493 * @param address the resolved name, NULL to indicate the last response
5496 transmit_address_to_client (void *cls, const char *address)
5498 struct GNUNET_SERVER_TransmitContext *tc = cls;
5501 if (NULL == address)
5504 slen = strlen (address) + 1;
5506 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
5507 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5508 if (NULL == address)
5509 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
5514 * Handle AddressLookup-message.
5516 * @param cls closure (always NULL)
5517 * @param client identification of the client
5518 * @param message the actual message
5521 handle_address_lookup (void *cls,
5522 struct GNUNET_SERVER_Client *client,
5523 const struct GNUNET_MessageHeader *message)
5525 const struct AddressLookupMessage *alum;
5526 struct TransportPlugin *lsPlugin;
5527 const char *nameTransport;
5528 const char *address;
5530 struct GNUNET_SERVER_TransmitContext *tc;
5531 struct GNUNET_TIME_Absolute timeout;
5532 struct GNUNET_TIME_Relative rtimeout;
5535 size = ntohs (message->size);
5536 if (size < sizeof (struct AddressLookupMessage))
5538 GNUNET_break_op (0);
5539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5542 alum = (const struct AddressLookupMessage *) message;
5543 uint32_t addressLen = ntohl (alum->addrlen);
5544 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
5546 GNUNET_break_op (0);
5547 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5550 address = (const char *) &alum[1];
5551 nameTransport = (const char *) &address[addressLen];
5553 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
5555 GNUNET_break_op (0);
5556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5559 timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
5560 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5561 numeric = ntohl (alum->numeric_only);
5562 lsPlugin = find_transport (nameTransport);
5563 if (NULL == lsPlugin)
5565 tc = GNUNET_SERVER_transmit_context_create (client);
5566 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5567 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5568 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
5571 tc = GNUNET_SERVER_transmit_context_create (client);
5572 lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
5574 address, addressLen,
5577 &transmit_address_to_client, tc);
5582 * Setup the environment for this plugin.
5585 create_environment (struct TransportPlugin *plug)
5587 plug->env.cfg = cfg;
5588 plug->env.my_identity = &my_identity;
5589 plug->env.our_hello = &our_hello;
5590 plug->env.cls = plug;
5591 plug->env.receive = &plugin_env_receive;
5592 plug->env.notify_address = &plugin_env_notify_address;
5593 plug->env.session_end = &plugin_env_session_end;
5594 plug->env.max_connections = max_connect_per_transport;
5595 plug->env.stats = stats;
5600 * Start the specified transport (load the plugin).
5603 start_transport (struct GNUNET_SERVER_Handle *server,
5606 struct TransportPlugin *plug;
5609 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5610 _("Loading `%s' transport plugin\n"), name);
5611 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
5612 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
5613 create_environment (plug);
5614 plug->short_name = GNUNET_strdup (name);
5615 plug->lib_name = libname;
5616 plug->next = plugins;
5618 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
5619 if (plug->api == NULL)
5621 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5622 _("Failed to load transport plugin for `%s'\n"), name);
5623 GNUNET_free (plug->short_name);
5624 plugins = plug->next;
5625 GNUNET_free (libname);
5632 * Called whenever a client is disconnected. Frees our
5633 * resources associated with that client.
5635 * @param cls closure
5636 * @param client identification of the client
5639 client_disconnect_notification (void *cls,
5640 struct GNUNET_SERVER_Client *client)
5642 struct TransportClient *pos;
5643 struct TransportClient *prev;
5644 struct ClientMessageQueueEntry *mqe;
5645 struct Blacklisters *bl;
5646 struct BlacklistCheck *bc;
5651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
5652 "Client disconnected, cleaning up.\n");
5654 /* clean up blacklister */
5658 if (bl->client == client)
5663 if (bc->bl_pos == bl)
5665 bc->bl_pos = bl->next;
5668 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
5671 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
5672 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
5678 GNUNET_CONTAINER_DLL_remove (bl_head,
5681 GNUNET_SERVER_client_drop (bl->client);
5687 /* clean up 'normal' clients */
5690 while ((pos != NULL) && (pos->client != client))
5697 while (NULL != (mqe = pos->message_queue_head))
5699 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
5700 pos->message_queue_tail,
5702 pos->message_count--;
5706 clients = pos->next;
5708 prev->next = pos->next;
5709 if (GNUNET_YES == pos->tcs_pending)
5714 if (pos->th != NULL)
5716 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
5719 GNUNET_break (0 == pos->message_count);
5725 * Function called when the service shuts down. Unloads our plugins
5726 * and cancels pending validations.
5728 * @param cls closure, unused
5729 * @param tc task context (unused)
5732 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5734 struct TransportPlugin *plug;
5735 struct OwnAddressList *al;
5736 struct CheckHelloValidatedContext *chvc;
5738 while (neighbours != NULL)
5740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5741 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&neighbours->id),
5743 disconnect_neighbour (neighbours, GNUNET_NO);
5746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5747 "Transport service is unloading plugins...\n");
5749 while (NULL != (plug = plugins))
5751 plugins = plug->next;
5752 if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
5754 GNUNET_SCHEDULER_cancel (plug->address_update_task);
5755 plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
5757 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
5758 GNUNET_free (plug->lib_name);
5759 GNUNET_free (plug->short_name);
5760 while (NULL != (al = plug->addresses))
5762 plug->addresses = al->next;
5767 if (my_private_key != NULL)
5768 GNUNET_CRYPTO_rsa_key_free (my_private_key);
5769 GNUNET_free_non_null (our_hello);
5771 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
5774 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
5775 validation_map = NULL;
5779 /* free 'chvc' data structure */
5780 while (NULL != (chvc = chvc_head))
5782 chvc_head = chvc->next;
5783 if (chvc->piter != NULL)
5785 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
5786 GNUNET_STATISTICS_update (stats,
5787 gettext_noop ("# outstanding peerinfo iterate requests"),
5793 GNUNET_assert (chvc->ve_count == 0);
5800 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
5803 if (peerinfo != NULL)
5805 GNUNET_PEERINFO_disconnect (peerinfo);
5808 /* Can we assume those are gone by now, or do we need to clean up
5810 GNUNET_break (bl_head == NULL);
5811 GNUNET_break (bc_head == NULL);
5815 static int ats_evaluate_results (int result, int solution, char * problem)
5817 int cont = GNUNET_NO;
5818 int error_kind = GNUNET_ERROR_TYPE_DEBUG;
5820 error_kind = GNUNET_ERROR_TYPE_ERROR;
5824 case GLP_ESTOP : /* search terminated by application */
5825 GNUNET_log (error_kind, "%s , Search terminated by application ", problem);
5827 case GLP_EITLIM : /* iteration limit exceeded */
5828 GNUNET_log (error_kind, "%s Iteration limit exceeded ", problem);
5831 case GLP_ETMLIM : /* time limit exceeded */
5832 GNUNET_log (error_kind, "%s Time limit exceeded ", problem);
5834 case GLP_ENOPFS : /* no primal feasible solution */
5835 case GLP_ENODFS : /* no dual feasible solution */
5836 GNUNET_log (error_kind, "%s No feasible solution", problem);
5839 case GLP_EBADB : /* invalid basis */
5840 case GLP_ESING : /* singular matrix */
5841 case GLP_ECOND : /* ill-conditioned matrix */
5842 case GLP_EBOUND : /* invalid bounds */
5843 case GLP_EFAIL : /* solver failed */
5844 case GLP_EOBJLL : /* objective lower limit reached */
5845 case GLP_EOBJUL : /* objective upper limit reached */
5846 case GLP_EROOT : /* root LP optimum not provided */
5847 GNUNET_log (error_kind, "%s Invalid Input data: %i\n", problem, result);
5851 GNUNET_log (error_kind, "%s Problem has been solved\n", problem);
5857 GNUNET_log (error_kind, "%s solution is undefined\n", problem);
5860 GNUNET_log (error_kind, "%s solution is optimal\n", problem);
5864 GNUNET_log (error_kind, "%s solution is integer feasible, however, its optimality (or non-optimality) has not been proven, \n", problem);
5868 GNUNET_log (error_kind, "%s problem has no integer feasible solution\n", problem);
5878 static void ats_solve_problem (unsigned int max_it, unsigned int max_dur, unsigned int c_peers, unsigned int c_mechs, struct ATS_result *res)
5885 glp_prob *prob = ats->prob;
5888 glp_init_smcp(&opt_lp);
5890 opt_lp.msg_lev = GLP_MSG_ALL;
5892 opt_lp.msg_lev = GLP_MSG_OFF;
5894 opt_lp.presolve = GLP_ON;
5895 result = glp_simplex(prob, &opt_lp);
5896 solution = glp_get_status (prob);
5898 if (GNUNET_YES == ats_evaluate_results(result, solution, "LP"))
5902 glp_init_iocp(&opt_mlp);
5903 /* maximum duration */
5904 opt_mlp.presolve = GLP_ON;
5905 opt_mlp.tm_lim = max_dur;
5908 opt_mlp.msg_lev = GLP_MSG_ALL;
5910 opt_mlp.msg_lev = GLP_MSG_OFF;
5912 result = glp_intopt (prob, &opt_mlp);
5913 solution = glp_mip_status (prob);
5914 res->solution = solution;
5915 res->valid = GNUNET_NO;
5916 if (ats_evaluate_results(result, solution, "MLP") == GNUNET_YES)
5917 res->valid = GNUNET_YES;
5921 if ((ats->save_mlp == GNUNET_YES) && (c_peers > 1))
5924 GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_llu.mlp",c_peers, c_mechs, GNUNET_TIME_absolute_get().abs_value);
5925 if (GNUNET_NO == GNUNET_DISK_file_test(filename))
5926 glp_write_mip (prob, filename);
5927 GNUNET_free (filename);
5929 if ((ats->save_solution == GNUNET_YES) && (c_peers > 1))
5932 GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_%llu.sol",c_peers, c_mechs, GNUNET_TIME_absolute_get().abs_value);
5933 if (GNUNET_NO == GNUNET_DISK_file_test(filename))
5934 glp_print_sol (prob, filename);
5935 GNUNET_free (filename);
5940 int error = GNUNET_NO;
5942 struct ATS_mechanism *t = NULL;
5943 for (c=1; c<= (c_peers); c++ )
5946 t = peers[c].m_head;
5949 bw = glp_get_col_prim(prob, t->col_index);
5953 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[%i][%i] `%s' %s %s %f\n", c, t->col_index, GNUNET_h2s(&peers[c].peer.hashPubKey), t->plugin->short_name, glp_get_col_name(prob,t->col_index), bw);
5955 if (check ==GNUNET_YES)
5957 glp_write_sol(prob, "invalid_solution.mlp");
5958 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid solution, check invalid_solution.mlp");
5959 GNUNET_STATISTICS_update (stats, "ATS invalid solutions", 1, GNUNET_NO);
5962 if (check ==GNUNET_NO)
5970 for (c=1; c<= available_quality_metrics; c++ )
5972 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(prob,2*c_mechs+3+c), glp_get_col_prim(prob,2*c_mechs+3+c));
5974 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(prob,2*c_mechs+1), glp_get_col_prim(prob,2*c_mechs+1));
5975 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(prob,2*c_mechs+2), glp_get_col_prim(prob,2*c_mechs+2));
5976 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(prob,2*c_mechs+3), glp_get_col_prim(prob,2*c_mechs+3));
5982 /** solve the bandwidth distribution problem
5983 * @param max_it maximum iterations
5984 * @param max_dur maximum duration in ms
5985 * @param D weight for diversity
5986 * @param U weight for utility
5987 * @param R weight for relativity
5988 * @param v_b_min minimal bandwidth per peer
5989 * @param v_n_min minimum number of connections
5990 * @param res result struct
5991 * @return GNUNET_SYSERR if glpk is not available, number of mechanisms used
5993 static int ats_create_problem (double D, double U, double R, int v_b_min, int v_n_min, struct ATS_result *res)
5996 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "no glpk installed\n");
5997 return GNUNET_SYSERR;
6000 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "glpk installed\n");
6003 if (ats->prob != NULL)
6004 glp_delete_prob(ats->prob);
6006 ats->prob = glp_create_prob();
6012 int c_c_ressources = available_ressources;
6013 int c_q_metrics = available_quality_metrics;
6015 double M = VERY_BIG_DOUBLE_VALUE;
6016 double Q[c_q_metrics+1];
6017 for (c=1; c<=c_q_metrics; c++)
6022 struct NeighbourList *next = neighbours;
6025 struct ReadyList *r_next = next->plugins;
6026 while (r_next != NULL)
6028 struct ForeignAddressList * a_next = r_next->addresses;
6029 while (a_next != NULL)
6032 a_next = a_next->next;
6034 r_next = r_next->next;
6043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No addresses for bw distribution available\n", c_peers);
6045 res->valid = GNUNET_NO;
6046 return GNUNET_SYSERR;
6049 struct ATS_mechanism * mechanisms = GNUNET_malloc((1+c_mechs) * sizeof (struct ATS_mechanism));
6050 struct ATS_peer * peers = GNUNET_malloc((1+c_peers) * sizeof (struct ATS_peer));
6057 peers[c_peers].peer = next->id;
6058 peers[c_peers].m_head = NULL;
6059 peers[c_peers].m_tail = NULL;
6061 peers[c_peers].f = 1.0 / c_mechs;
6063 struct ReadyList *r_next = next->plugins;
6064 while (r_next != NULL)
6066 struct ForeignAddressList * a_next = r_next->addresses;
6067 while (a_next != NULL)
6069 mechanisms[c_mechs].addr = a_next;
6070 mechanisms[c_mechs].col_index = c_mechs;
6071 mechanisms[c_mechs].peer = &peers[c_peers];
6072 mechanisms[c_mechs].next = NULL;
6073 mechanisms[c_mechs].plugin = r_next->plugin;
6075 GNUNET_CONTAINER_DLL_insert_tail(peers[c_peers].m_head, peers[c_peers].m_tail, &mechanisms[c_mechs]);
6077 a_next = a_next->next;
6079 r_next = r_next->next;
6087 if (v_n_min > c_peers)
6091 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Creating problem with: %i peers, %i mechanisms, %i resource entries, %i quality metrics \n", c_peers, c_mechs, c_c_ressources, c_q_metrics);
6094 int size = 1 + 3 + 10 *c_mechs + c_peers + (c_q_metrics*c_mechs)+ c_q_metrics + c_c_ressources * c_mechs ;
6097 int * ia = GNUNET_malloc (size * sizeof (int));
6098 int * ja = GNUNET_malloc (size * sizeof (int));
6099 double * ar = GNUNET_malloc(size* sizeof (double));
6101 glp_set_prob_name(ats->prob, "gnunet ats bandwidth distribution");
6102 glp_set_obj_dir(ats->prob, GLP_MAX);
6104 /* adding columns */
6106 glp_add_cols(ats->prob, 2 * c_mechs);
6107 /* adding b_t cols */
6108 for (c=1; c <= c_mechs; c++)
6111 GNUNET_asprintf(&name, "p_%s_b%i",GNUNET_i2s(&(mechanisms[c].peer->peer)), c);
6112 glp_set_col_name(ats->prob, c, name);
6114 glp_set_col_bnds(ats->prob, c, GLP_LO, 0.0, 0.0);
6115 glp_set_obj_coef(ats->prob, c, 1);
6118 /* adding n_t cols */
6119 for (c=c_mechs+1; c <= 2*c_mechs; c++)
6121 GNUNET_asprintf(&name, "p_%s_n%i",GNUNET_i2s(&(mechanisms[c-c_mechs].peer->peer)),(c-c_mechs));
6122 glp_set_col_name(ats->prob, c, name);
6124 glp_set_col_bnds(ats->prob, c, GLP_DB, 0.0, 1.0);
6125 glp_set_col_kind(ats->prob, c, GLP_IV);
6126 glp_set_obj_coef(ats->prob, c, 0);
6129 /* feasibility constraints */
6130 /* Constraint 1: one address per peer*/
6132 glp_add_rows(ats->prob, c_peers);
6133 for (c=1; c<=c_peers; c++)
6135 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 1.0, 1.0);
6137 struct ATS_mechanism *m = peers[c].m_head;
6140 ia[array_index] = row_index;
6141 ja[array_index] = (c_mechs + m->col_index);
6142 ar[array_index] = 1;
6144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6152 /* Constraint 2: only active mechanism gets bandwidth assigned */
6153 glp_add_rows(ats->prob, c_mechs);
6154 for (c=1; c<=c_mechs; c++)
6156 /* b_t - n_t * M <= 0 */
6158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6160 glp_set_row_bnds(ats->prob, row_index, GLP_UP, 0.0, 0.0);
6162 ia[array_index] = row_index;
6163 ja[array_index] = mechanisms[c].col_index;
6164 ar[array_index] = 1;
6166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6169 ia[array_index] = row_index;
6170 ja[array_index] = c_mechs + mechanisms[c].col_index;
6171 ar[array_index] = -M;
6173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6179 /* Constraint 3: minimum bandwidth*/
6180 glp_add_rows(ats->prob, c_mechs);
6181 for (c=1; c<=c_mechs; c++)
6183 /* b_t - n_t * b_min <= 0 */
6185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6187 glp_set_row_bnds(ats->prob, row_index, GLP_LO, 0.0, 0.0);
6189 ia[array_index] = row_index;
6190 ja[array_index] = mechanisms[c].col_index;
6191 ar[array_index] = 1;
6193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6196 ia[array_index] = row_index;
6197 ja[array_index] = c_mechs + mechanisms[c].col_index;
6198 ar[array_index] = -v_b_min;
6200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6206 /* Constraint 4: max ressource capacity */
6207 /* V cr: bt * ct_r <= cr_maxsolution
6209 glp_add_rows(ats->prob, available_ressources);
6210 double ct_max = VERY_BIG_DOUBLE_VALUE;
6211 double ct_min = 0.0;
6213 for (c=0; c<available_ressources; c++)
6215 ct_max = ressources[c].c_max;
6216 ct_min = ressources[c].c_min;
6218 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] %f..%f\n",row_index, ct_min, ct_max);
6220 glp_set_row_bnds(ats->prob, row_index, GLP_DB, ct_min, ct_max);
6222 for (c2=1; c2<=c_mechs; c2++)
6225 ia[array_index] = row_index;
6226 ja[array_index] = c2;
6227 value = mechanisms[c2].addr->ressources[c].c;
6228 ar[array_index] = value;
6230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6237 /* Constraint 5: min number of connections*/
6238 glp_add_rows(ats->prob, 1);
6239 for (c=1; c<=c_mechs; c++)
6241 // b_t - n_t * b_min >= 0
6243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6245 glp_set_row_bnds(ats->prob, row_index, GLP_LO, v_n_min, 0.0);
6247 ia[array_index] = row_index;
6248 ja[array_index] = c_mechs + mechanisms[c].col_index;
6249 ar[array_index] = 1;
6251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6257 /* optimisation constraints*/
6259 /* adding columns */
6260 glp_add_cols(ats->prob, 3 + c_q_metrics);
6262 glp_set_col_name(ats->prob, (2*c_mechs) + 1, "d");
6263 glp_set_obj_coef(ats->prob, (2*c_mechs) + 1, D);
6264 glp_set_col_bnds(ats->prob, (2*c_mechs) + 1, GLP_LO, 0.0, 0.0);
6265 glp_set_col_name(ats->prob, (2*c_mechs) + 2, "u");
6266 glp_set_obj_coef(ats->prob, (2*c_mechs) + 2, U);
6267 glp_set_col_bnds(ats->prob, (2*c_mechs) + 2, GLP_LO, 0.0, 0.0);
6268 glp_set_col_name(ats->prob, (2*c_mechs) + 3, "r");
6269 glp_set_obj_coef(ats->prob, (2*c_mechs) + 3, R);
6270 glp_set_col_bnds(ats->prob, (2*c_mechs) + 3, GLP_LO, 0.0, 0.0);
6272 for (c=1; c<= c_q_metrics; c++)
6274 GNUNET_asprintf(&name, "Q_%s",qm[c-1].name);
6275 glp_set_col_name(ats->prob, (2*c_mechs) + 3 + c, name);
6276 glp_set_col_bnds(ats->prob, (2*c_mechs) + 3 + c, GLP_LO, 0.0, 0.0);
6278 glp_set_obj_coef(ats->prob, (2*c_mechs) + 3 + c, Q[c]);
6281 // Constraint 6: optimize for diversity
6282 glp_add_rows(ats->prob, 1);
6284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6286 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6287 for (c=1; c<=c_mechs; c++)
6289 // b_t - n_t * b_min >= 0
6290 ia[array_index] = row_index;
6291 ja[array_index] = c_mechs + mechanisms[c].col_index;
6292 ar[array_index] = 1;
6294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6298 ia[array_index] = row_index;
6299 ja[array_index] = (2*c_mechs) + 1;
6300 ar[array_index] = -1;
6302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6308 // Constraint 7: optimize for quality
6309 glp_add_rows(ats->prob, available_quality_metrics);
6310 for (c=1; c <= c_q_metrics; c++)
6313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6315 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6317 for (c2=1; c2<=c_mechs; c2++)
6320 ia[array_index] = row_index;
6321 ja[array_index] = c2;
6322 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY)
6325 v0 = mechanisms[c2].addr->quality[c-1].values[0];
6326 if (v1 < 1) v0 = 0.1;
6327 v1 = mechanisms[c2].addr->quality[c-1].values[1];
6328 if (v1 < 1) v0 = 0.1;
6329 v2 = mechanisms[c2].addr->quality[c-1].values[2];
6330 if (v1 < 1) v0 = 0.1;
6331 value = 100.0 / ((v0 + 2 * v1 + 3 * v2) / 6.0);
6333 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
6336 v0 = mechanisms[c2].addr->quality[c-1].values[0];
6338 v1 = mechanisms[c2].addr->quality[c-1].values[1];
6340 v2 = mechanisms[c2].addr->quality[c-1].values[2];
6342 value = (v0 + 2 * v1 + 3 * v2) / 6.0;
6344 value = (double) 10 / value;
6348 ar[array_index] = (mechanisms[c2].peer->f) * value ;
6350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: %s [%i,%i]=%f \n",array_index, qm[c-1].name, ia[array_index], ja[array_index], ar[array_index]);
6355 ia[array_index] = row_index;
6356 ja[array_index] = (2*c_mechs) + 3 +c;
6357 ar[array_index] = -1;
6359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6365 // Constraint 8: optimize bandwidth utility
6366 glp_add_rows(ats->prob, 1);
6368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6370 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6371 for (c=1; c<=c_mechs; c++)
6373 ia[array_index] = row_index;
6374 ja[array_index] = c;
6375 ar[array_index] = mechanisms[c].peer->f;
6377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6381 ia[array_index] = row_index;
6382 ja[array_index] = (2*c_mechs) + 2;
6383 ar[array_index] = -1;
6385 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6391 // Constraint 9: optimize relativity
6392 glp_add_rows(ats->prob, c_peers);
6393 for (c=1; c<=c_peers; c++)
6395 glp_set_row_bnds(ats->prob, row_index, GLP_LO, 0.0, 0.0);
6397 struct ATS_mechanism *m = peers[c].m_head;
6400 ia[array_index] = row_index;
6401 ja[array_index] = m->col_index;
6402 ar[array_index] = 1;
6404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6409 ia[array_index] = row_index;
6410 ja[array_index] = (2*c_mechs) + 3;
6411 ar[array_index] = -1;
6413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6419 /* Loading the matrix */
6420 glp_load_matrix(ats->prob, array_index-1, ia, ja, ar);
6422 res->c_mechs = c_mechs;
6423 res->c_peers = c_peers;
6425 res->valid = GNUNET_YES;
6433 for (c=0; c<c_mechs; c++)
6434 GNUNET_free_non_null (mechanisms[c].rc);
6436 GNUNET_free(mechanisms);
6442 void ats_calculate_bandwidth_distribution ()
6444 struct GNUNET_TIME_Absolute start;
6445 struct GNUNET_TIME_Relative duration;
6447 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_difference(ats->last,GNUNET_TIME_absolute_get());
6448 if (delta.rel_value < ats->min_delta.rel_value)
6451 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Minimum time between cycles not reached\n");
6457 if (INT_MAX < ats->max_exec_duration.rel_value)
6460 dur = (int) ats->max_exec_duration.rel_value;
6464 start = GNUNET_TIME_absolute_get();
6465 if ((ats->modified_addr == GNUNET_YES) || (ats->prob==NULL))
6468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers/Addresses were modified... have to recreate problem\n");
6470 ats->modified_addr = GNUNET_NO;
6471 ats->res.c_mechs = 0;
6472 ats->res.c_peers = 0;
6473 ats->res.solution = 0;
6474 ats->res.valid = GNUNET_NO;
6475 ats_create_problem (ats->D, ats->U, ats->R, ats->v_b_min, ats->v_n_min, &ats->res);
6477 else if (ats->modified_resources)
6479 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "RESSOURCE...modified problem\n");
6482 else if (ats->modified_quality)
6484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "RESSOURCE...quality problem\n");
6488 else GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Problem is unmodified\n");
6491 if (ats->res.valid == GNUNET_YES)
6493 ats_solve_problem(ats->max_iterations, ats->max_exec_duration.rel_value, ats->res.c_peers, ats->res.c_mechs, &ats->res);
6496 duration = GNUNET_TIME_absolute_get_difference(start,GNUNET_TIME_absolute_get());
6498 if ((ats->res.valid == GNUNET_YES))
6501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP execution time in [ms] for %i mechanisms: %llu %llu \n", res, duration.rel_value, ats->res.solution);
6503 GNUNET_STATISTICS_set (stats, "ATS duration", duration.rel_value, GNUNET_NO);
6504 GNUNET_STATISTICS_set (stats, "ATS mechanisms", ats->res.c_mechs, GNUNET_NO);
6505 GNUNET_STATISTICS_set (stats, "ATS peers", ats->res.c_peers, GNUNET_NO);
6506 GNUNET_STATISTICS_set (stats, "ATS solution", ats->res.solution, GNUNET_NO);
6507 GNUNET_STATISTICS_set (stats, "ATS timestamp", start.abs_value, GNUNET_NO);
6512 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP not executed: no addresses\n");
6515 ats->last = GNUNET_TIME_absolute_get();
6517 ats->modified_addr = GNUNET_NO;
6518 ats->modified_resources = GNUNET_NO;
6519 ats->modified_quality = GNUNET_NO;
6527 ats_schedule_calculation (void *cls,
6528 const struct GNUNET_SCHEDULER_TaskContext *tc)
6530 struct ATS_info *ats = (struct ATS_info *) cls;
6534 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6535 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
6539 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running scheduled calculation\n");
6542 ats_calculate_bandwidth_distribution (ats);
6544 ats->ats_task = GNUNET_SCHEDULER_add_delayed (ats->exec_intervall,
6545 &ats_schedule_calculation, ats);
6550 ats = GNUNET_malloc(sizeof (struct ATS_info));
6552 ats->min_delta = ATS_MIN_INTERVAL;
6553 ats->exec_intervall = ATS_EXEC_INTERVAL;
6554 ats->max_exec_duration = ATS_MAX_EXEC_DURATION;
6555 ats->max_iterations = ATS_MAX_ITERATIONS;
6556 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed, ATS not active");
6566 ats->v_b_min = 64000;
6571 unsigned long long value;
6573 /* loading cost ressources */
6574 for (c=0; c<available_ressources; c++)
6576 GNUNET_asprintf(§ion,"%s_UP",ressources[c].cfg_param);
6577 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
6579 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "transport",section, &value))
6582 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found ressource cost: [%s] = %llu\n", section, value);
6584 ressources[c].c_max = value;
6587 GNUNET_free (section);
6588 GNUNET_asprintf(§ion,"%s_DOWN",ressources[c].cfg_param);
6589 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
6591 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "transport",section, &value))
6594 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found ressource cost: [%s] = %llu\n", section, value);
6596 ressources[c].c_min = value;
6599 GNUNET_free (section);
6602 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_MLP"))
6603 ats->save_mlp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "transport","DUMP_MLP");
6605 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_SOLUTION"))
6606 ats->save_solution = GNUNET_CONFIGURATION_get_value_yesno (cfg, "transport","DUMP_SOLUTION");
6608 ats->ats_task = GNUNET_SCHEDULER_add_now(&ats_schedule_calculation, ats);
6612 void ats_shutdown ()
6615 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_destroy\n");
6617 if (ats->ats_task != GNUNET_SCHEDULER_NO_TASK)
6618 GNUNET_SCHEDULER_cancel(ats->ats_task);
6619 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6621 if (ats->prob != NULL)
6623 glp_delete_prob(ats->prob);
6631 void ats_notify_peer_connect (
6632 const struct GNUNET_PeerIdentity *peer,
6633 const struct GNUNET_TRANSPORT_ATS_Information *ats_data)
6637 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_notify_peer_connect: %s\n",GNUNET_i2s(peer));
6639 while (ntohl(ats_data[c].type)!=0)
6642 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats type [%i]: %i\n",ntohl(ats_data[c].type), ntohl(ats_data[c].value));
6647 ats->modified_addr = GNUNET_YES;
6649 ats_calculate_bandwidth_distribution(ats);
6652 void ats_notify_peer_disconnect (
6653 const struct GNUNET_PeerIdentity *peer)
6656 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_notify_peer_disconnect: %s\n",GNUNET_i2s(peer));
6659 ats->modified_addr = GNUNET_YES;
6661 ats_calculate_bandwidth_distribution (ats);
6665 void ats_notify_ats_data (
6666 const struct GNUNET_PeerIdentity *peer,
6667 const struct GNUNET_TRANSPORT_ATS_Information *ats_data)
6670 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "ATS_notify_ats_data: %s\n",GNUNET_i2s(peer));
6672 ats_calculate_bandwidth_distribution(ats);
6675 struct ForeignAddressList * ats_get_preferred_address (
6676 struct NeighbourList *n)
6679 //GNUNET_log (GNUNET_ERROR_TYPE_BULK, "ats_get_prefered_transport for peer: %s\n",GNUNET_i2s(&n->id));
6681 struct ReadyList *next = n->plugins;
6682 while (next != NULL)
6685 //GNUNET_log (GNUNET_ERROR_TYPE_BULK, "plugin: %s %i\n",next->plugin->short_name,strcmp(next->plugin->short_name,"unix"));
6689 return find_ready_address(n);
6693 * Initiate transport service.
6695 * @param cls closure
6696 * @param server the initialized server
6697 * @param c configuration to use
6701 struct GNUNET_SERVER_Handle *server,
6702 const struct GNUNET_CONFIGURATION_Handle *c)
6704 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
6705 {&handle_start, NULL,
6706 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
6707 {&handle_hello, NULL,
6708 GNUNET_MESSAGE_TYPE_HELLO, 0},
6709 {&handle_send, NULL,
6710 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
6711 {&handle_request_connect, NULL,
6712 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT, sizeof(struct TransportRequestConnectMessage)},
6713 {&handle_set_quota, NULL,
6714 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
6715 {&handle_address_lookup, NULL,
6716 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
6718 {&handle_blacklist_init, NULL,
6719 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
6720 {&handle_blacklist_reply, NULL,
6721 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
6727 unsigned long long tneigh;
6731 stats = GNUNET_STATISTICS_create ("transport", cfg);
6732 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
6733 /* parse configuration */
6735 GNUNET_CONFIGURATION_get_value_number (c,
6740 GNUNET_CONFIGURATION_get_value_filename (c,
6742 "HOSTKEY", &keyfile)))
6744 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6746 ("Transport service is lacking key configuration settings. Exiting.\n"));
6747 GNUNET_SCHEDULER_shutdown ();
6750 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6753 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6754 validation_map = NULL;
6758 max_connect_per_transport = (uint32_t) tneigh;
6759 peerinfo = GNUNET_PEERINFO_connect (cfg);
6760 if (peerinfo == NULL)
6762 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6763 _("Could not access PEERINFO service. Exiting.\n"));
6764 GNUNET_SCHEDULER_shutdown ();
6767 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6770 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6771 validation_map = NULL;
6772 GNUNET_free (keyfile);
6775 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6776 GNUNET_free (keyfile);
6777 if (my_private_key == NULL)
6779 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6781 ("Transport service could not access hostkey. Exiting.\n"));
6782 GNUNET_SCHEDULER_shutdown ();
6785 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6788 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6789 validation_map = NULL;
6792 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6793 GNUNET_CRYPTO_hash (&my_public_key,
6794 sizeof (my_public_key), &my_identity.hashPubKey);
6795 /* setup notification */
6796 GNUNET_SERVER_disconnect_notify (server,
6797 &client_disconnect_notification, NULL);
6798 /* load plugins... */
6801 GNUNET_CONFIGURATION_get_value_string (c,
6802 "TRANSPORT", "PLUGINS", &plugs))
6804 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6805 _("Starting transport plugins `%s'\n"), plugs);
6806 pos = strtok (plugs, " ");
6809 start_transport (server, pos);
6811 pos = strtok (NULL, " ");
6813 GNUNET_free (plugs);
6815 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
6816 &shutdown_task, NULL);
6823 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport service ready.\n"));
6825 /* If we have a blacklist file, read from it */
6826 read_blacklist_file(cfg);
6827 /* process client requests */
6828 GNUNET_SERVER_add_handlers (server, handlers);
6833 * The main function for the transport service.
6835 * @param argc number of arguments from the command line
6836 * @param argv command line arguments
6837 * @return 0 ok, 1 on error
6840 main (int argc, char *const *argv)
6842 a2s (NULL, NULL, 0); /* make compiler happy */
6843 return (GNUNET_OK ==
6844 GNUNET_SERVICE_run (argc,
6847 GNUNET_SERVICE_OPTION_NONE,
6848 &run, NULL)) ? 0 : 1;
6851 /* end of gnunet-service-transport.c */