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 */
885 struct ATS_ressource_entry
887 /* index in ressources array */
889 /* depending ATSi parameter to calculcate limits */
898 /* index in ressources array */
900 /* depending ATSi parameter to calculcate limits */
902 /* cfg option to load limits */
909 /* cofficients for the specific plugins */
919 static struct ATS_ressource ressources[] =
921 /* FIXME: the coefficients for the specific plugins */
922 {1, 7, "LAN_BW_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 1, 1, 2, 2, 1, 3},
923 {2, 7, "WAN_BW_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 1, 1, 2, 2, 2, 3},
924 {3, 4, "WLAN_ENERGY_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 0, 0, 0, 0, 2, 1}
926 {4, 4, "COST_ENERGY_CONSUMPTION", VERY_BIG_DOUBLE_VALUE},
927 {5, 5, "COST_CONNECT", VERY_BIG_DOUBLE_VALUE},
928 {6, 6, "COST_BANDWITH_AVAILABLE", VERY_BIG_DOUBLE_VALUE},
929 {7, 7, "COST_NETWORK_OVERHEAD", VERY_BIG_DOUBLE_VALUE},*/
932 static int available_ressources = 3;
940 * Time of last execution
942 struct GNUNET_TIME_Absolute last;
944 * Minimum intervall between two executions
946 struct GNUNET_TIME_Relative min_delta;
948 * Regular intervall when execution is triggered
950 struct GNUNET_TIME_Relative exec_intervall;
952 * Maximum execution time per calculation
954 struct GNUNET_TIME_Relative max_exec_duration;
956 * Maximum number of LP iterations per calculation
960 GNUNET_SCHEDULER_TaskIdentifier ats_task;
962 struct ATS_plugin * head;
963 struct ATS_plugin * tail;
970 static struct GNUNET_HELLO_Message *our_hello;
975 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
980 static struct GNUNET_PeerIdentity my_identity;
985 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
990 const struct GNUNET_CONFIGURATION_Handle *cfg;
993 * Linked list of all clients to this service.
995 static struct TransportClient *clients;
998 * All loaded plugins.
1000 static struct TransportPlugin *plugins;
1003 * Handle to peerinfo service.
1005 static struct GNUNET_PEERINFO_Handle *peerinfo;
1008 * All known neighbours and their HELLOs.
1010 static struct NeighbourList *neighbours;
1013 * Number of neighbours we'd like to have.
1015 static uint32_t max_connect_per_transport;
1018 * Head of linked list.
1020 static struct CheckHelloValidatedContext *chvc_head;
1023 * Tail of linked list.
1025 static struct CheckHelloValidatedContext *chvc_tail;
1028 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
1029 * of the given peer that we are currently validating).
1031 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
1034 * Handle for reporting statistics.
1036 static struct GNUNET_STATISTICS_Handle *stats;
1039 * Handle for ats information
1041 static struct ATS_info *ats;
1043 struct ATS_quality_entry
1051 static struct ATS_quality_metric qm[] =
1053 {1, 1028, "QUALITY_NET_DISTANCE"},
1054 {2, 1034, "QUALITY_NET_DELAY"},
1056 static int available_quality_metrics = 2;
1060 * The peer specified by the given neighbour has timed-out or a plugin
1061 * has disconnected. We may either need to do nothing (other plugins
1062 * still up), or trigger a full disconnect and clean up. This
1063 * function updates our state and do the necessary notifications.
1064 * Also notifies our clients that the neighbour is now officially
1067 * @param n the neighbour list entry for the peer
1068 * @param check should we just check if all plugins
1069 * disconnected or must we ask all plugins to
1072 static void disconnect_neighbour (struct NeighbourList *n, int check);
1075 * Check the ready list for the given neighbour and if a plugin is
1076 * ready for transmission (and if we have a message), do so!
1078 * @param nexi target peer for which to transmit
1080 static void try_transmission_to_peer (struct NeighbourList *n);
1085 void ats_shutdown ( );
1087 void ats_notify_peer_connect (
1088 const struct GNUNET_PeerIdentity *peer,
1089 const struct GNUNET_TRANSPORT_ATS_Information *ats_data);
1091 void ats_notify_peer_disconnect (
1092 const struct GNUNET_PeerIdentity *peer);
1094 void ats_notify_ats_data (
1095 const struct GNUNET_PeerIdentity *peer,
1096 const struct GNUNET_TRANSPORT_ATS_Information *ats_data);
1098 struct ForeignAddressList * ats_get_preferred_address (
1099 struct NeighbourList *n);
1102 * Find an entry in the neighbour list for a particular peer.
1104 * @return NULL if not found.
1106 static struct NeighbourList *
1107 find_neighbour (const struct GNUNET_PeerIdentity *key)
1109 struct NeighbourList *head = neighbours;
1111 while ((head != NULL) &&
1112 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
1117 static void update_addr_ats (struct ForeignAddressList *fal, const struct GNUNET_TRANSPORT_ATS_Information *ats_data, int ats_count)
1120 for (c1=0; c1<ats_count; c1++)
1122 for (c2=0; c2<available_quality_metrics; c2++)
1124 if (ntohl(ats_data[c1].type) == qm[c2].atis_index)
1126 fal->quality[c2].values[0] = fal->quality[c2].values[1];
1127 fal->quality[c2].values[1] = fal->quality[c2].values[2];
1128 fal->quality[c2].values[2] = ntohl(ats_data[c1].value);
1134 static void update_addr_value (struct ForeignAddressList *fal, uint32_t value , int ats_index)
1137 for (c=0; c<available_quality_metrics; c++)
1139 if (ats_index == qm[c].atis_index)
1141 fal->quality[c].values[0] = fal->quality[c].values[1];
1142 fal->quality[c].values[1] = fal->quality[c].values[2];
1143 fal->quality[c].values[2] = value;
1149 * Find an entry in the transport list for a particular transport.
1151 * @return NULL if not found.
1153 static struct TransportPlugin *
1154 find_transport (const char *short_name)
1156 struct TransportPlugin *head = plugins;
1157 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
1163 * Is a particular peer blacklisted for a particular transport?
1165 * @param peer the peer to check for
1166 * @param plugin the plugin used to connect to the peer
1168 * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
1171 is_blacklisted (const struct GNUNET_PeerIdentity *peer, struct TransportPlugin *plugin)
1174 if (plugin->blacklist != NULL)
1176 if (GNUNET_CONTAINER_multihashmap_contains (plugin->blacklist, &peer->hashPubKey) == GNUNET_YES)
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180 "Peer `%s:%s' is blacklisted!\n",
1181 plugin->short_name, GNUNET_i2s (peer));
1184 GNUNET_STATISTICS_update (stats, "# blacklisted peers refused", 1, GNUNET_NO);
1194 add_peer_to_blacklist (struct GNUNET_PeerIdentity *peer, char *transport_name)
1196 struct TransportPlugin *plugin;
1198 plugin = find_transport(transport_name);
1199 if (plugin == NULL) /* Nothing to do */
1201 if (plugin->blacklist == NULL)
1202 plugin->blacklist = GNUNET_CONTAINER_multihashmap_create(TRANSPORT_BLACKLIST_HT_SIZE);
1203 GNUNET_assert(plugin->blacklist != NULL);
1204 GNUNET_CONTAINER_multihashmap_put(plugin->blacklist, &peer->hashPubKey,
1206 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1211 * Read the blacklist file, containing transport:peer entries.
1212 * Provided the transport is loaded, set up hashmap with these
1213 * entries to blacklist peers by transport.
1217 read_blacklist_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1224 struct GNUNET_PeerIdentity pid;
1226 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1227 unsigned int entries_found;
1228 char *transport_name;
1231 GNUNET_CONFIGURATION_get_value_filename (cfg,
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238 "Option `%s' in section `%s' not specified!\n",
1244 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1245 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1246 | GNUNET_DISK_PERM_USER_WRITE);
1247 if (0 != STAT (fn, &frstat))
1249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1250 _("Could not read blacklist file `%s'\n"), fn);
1254 if (frstat.st_size == 0)
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 _("Blacklist file `%s' is empty.\n"),
1264 /* FIXME: use mmap */
1265 data = GNUNET_malloc_large (frstat.st_size);
1266 GNUNET_assert(data != NULL);
1267 if (frstat.st_size !=
1268 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1271 _("Failed to read blacklist from `%s'\n"), fn);
1278 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1280 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1281 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1284 while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') && !isspace ( (unsigned char) data[colon_pos]))
1287 if (colon_pos >= frstat.st_size)
1289 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1290 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1291 (unsigned long long) colon_pos);
1297 if (isspace( (unsigned char) data[colon_pos]))
1299 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1300 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1301 (unsigned long long) colon_pos);
1303 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1307 tsize = colon_pos - pos;
1308 if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size) || (tsize == 0))
1310 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1311 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1312 (unsigned long long) colon_pos);
1321 transport_name = GNUNET_malloc(tsize + 1);
1322 memcpy(transport_name, &data[pos], tsize);
1323 pos = colon_pos + 1;
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1326 "Read transport name %s in blacklist file.\n",
1329 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1330 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1332 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1333 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1334 (unsigned long long) pos);
1336 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1338 GNUNET_free_non_null(transport_name);
1341 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1342 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1344 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1345 _("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
1346 (unsigned long long) pos,
1351 if (0 != memcmp (&pid,
1353 sizeof (struct GNUNET_PeerIdentity)))
1356 add_peer_to_blacklist (&pid,
1361 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1362 _("Found myself `%s' in blacklist (useless, ignored)\n"),
1366 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1367 GNUNET_free_non_null(transport_name);
1368 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1371 GNUNET_STATISTICS_update (stats, "# Transport entries blacklisted", entries_found, GNUNET_NO);
1378 * Function called to notify a client about the socket being ready to
1379 * queue more data. "buf" will be NULL and "size" zero if the socket
1380 * was closed for writing in the meantime.
1382 * @param cls closure
1383 * @param size number of bytes available in buf
1384 * @param buf where the callee should write the message
1385 * @return number of bytes written to buf
1388 transmit_to_client_callback (void *cls, size_t size, void *buf)
1390 struct TransportClient *client = cls;
1391 struct ClientMessageQueueEntry *q;
1394 const struct GNUNET_MessageHeader *msg;
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "Transmission to client failed, closing connection.\n");
1404 /* fatal error with client, free message queue! */
1405 while (NULL != (q = client->message_queue_head))
1407 GNUNET_STATISTICS_update (stats,
1408 gettext_noop ("# bytes discarded (could not transmit to client)"),
1409 ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
1411 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1412 client->message_queue_tail,
1416 client->message_count = 0;
1421 while (NULL != (q = client->message_queue_head))
1423 msg = (const struct GNUNET_MessageHeader *) &q[1];
1424 msize = ntohs (msg->size);
1425 if (msize + tsize > size)
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "Transmitting message of type %u to client.\n",
1432 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1433 client->message_queue_tail,
1435 memcpy (&cbuf[tsize], msg, msize);
1438 client->message_count--;
1442 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1443 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1445 GNUNET_TIME_UNIT_FOREVER_REL,
1446 &transmit_to_client_callback,
1448 GNUNET_assert (client->th != NULL);
1455 * Convert an address to a string.
1457 * @param plugin name of the plugin responsible for the address
1458 * @param addr binary address
1459 * @param addr_len number of bytes in addr
1460 * @return NULL on error, otherwise address string
1463 a2s (const char *plugin,
1467 struct TransportPlugin *p;
1471 p = find_transport (plugin);
1474 return p->api->address_to_string (p->api->cls,
1481 * Mark the given FAL entry as 'connected' (and hence preferred for
1482 * sending); also mark all others for the same peer as 'not connected'
1483 * (since only one can be preferred).
1485 * @param fal address to set to 'connected'
1488 mark_address_connected (struct ForeignAddressList *fal)
1490 struct ForeignAddressList *pos;
1493 GNUNET_assert (GNUNET_YES == fal->validated);
1494 if (fal->connected == GNUNET_YES)
1495 return; /* nothing to do */
1497 pos = fal->ready_list->addresses;
1500 if (GNUNET_YES == pos->connected)
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 "Marking address `%s' as no longer connected (due to connect on other address)\n",
1505 a2s (pos->ready_list->plugin->short_name,
1509 GNUNET_break (cnt == GNUNET_YES);
1511 pos->connected = GNUNET_NO;
1512 GNUNET_STATISTICS_update (stats,
1513 gettext_noop ("# connected addresses"),
1519 fal->connected = GNUNET_YES;
1520 if (GNUNET_YES == cnt)
1522 GNUNET_STATISTICS_update (stats,
1523 gettext_noop ("# connected addresses"),
1531 * Send the specified message to the specified client. Since multiple
1532 * messages may be pending for the same client at a time, this code
1533 * makes sure that no message is lost.
1535 * @param client client to transmit the message to
1536 * @param msg the message to send
1537 * @param may_drop can this message be dropped if the
1538 * message queue for this client is getting far too large?
1541 transmit_to_client (struct TransportClient *client,
1542 const struct GNUNET_MessageHeader *msg, int may_drop)
1544 struct ClientMessageQueueEntry *q;
1547 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1549 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1551 ("Dropping message of type %u and size %u, have %u messages pending (%u is the soft limit)\n"),
1554 client->message_count,
1556 GNUNET_STATISTICS_update (stats,
1557 gettext_noop ("# messages dropped due to slow client"),
1562 msize = ntohs (msg->size);
1563 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1564 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1565 memcpy (&q[1], msg, msize);
1566 GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1567 client->message_queue_tail,
1568 client->message_queue_tail,
1570 client->message_count++;
1571 if (client->th == NULL)
1573 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1575 GNUNET_TIME_UNIT_FOREVER_REL,
1576 &transmit_to_client_callback,
1578 GNUNET_assert (client->th != NULL);
1584 * Transmit a 'SEND_OK' notification to the given client for the
1587 * @param client who to notify
1588 * @param n neighbour to notify about, can be NULL (on failure)
1589 * @param target target of the transmission
1590 * @param result status code for the transmission request
1593 transmit_send_ok (struct TransportClient *client,
1594 struct NeighbourList *n,
1595 const struct GNUNET_PeerIdentity *target,
1598 struct SendOkMessage send_ok_msg;
1600 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1601 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1602 send_ok_msg.success = htonl (result);
1604 send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1606 send_ok_msg.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
1607 send_ok_msg.peer = *target;
1608 transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1613 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1614 * upon "completion" of a send request. This tells the API
1615 * that it is now legal to send another message to the given
1618 * @param cls closure, identifies the entry on the
1619 * message queue that was transmitted and the
1620 * client responsible for queuing the message
1621 * @param target the peer receiving the message
1622 * @param result GNUNET_OK on success, if the transmission
1623 * failed, we should not tell the client to transmit
1627 transmit_send_continuation (void *cls,
1628 const struct GNUNET_PeerIdentity *target,
1631 struct MessageQueue *mq = cls;
1632 struct NeighbourList *n;
1634 GNUNET_STATISTICS_update (stats,
1635 gettext_noop ("# bytes pending with plugins"),
1636 - (int64_t) mq->message_buf_size,
1638 if (result == GNUNET_OK)
1640 GNUNET_STATISTICS_update (stats,
1641 gettext_noop ("# bytes successfully transmitted by plugins"),
1642 mq->message_buf_size,
1647 GNUNET_STATISTICS_update (stats,
1648 gettext_noop ("# bytes with transmission failure by plugins"),
1649 mq->message_buf_size,
1652 if (mq->specific_address != NULL)
1654 if (result == GNUNET_OK)
1656 mq->specific_address->timeout =
1657 GNUNET_TIME_relative_to_absolute
1658 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1659 if (mq->specific_address->validated == GNUNET_YES)
1660 mark_address_connected (mq->specific_address);
1664 if (mq->specific_address->connected != GNUNET_NO)
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1668 "Marking address `%s' as no longer connected (due to transmission problem)\n",
1669 a2s (mq->specific_address->ready_list->plugin->short_name,
1670 mq->specific_address->addr,
1671 mq->specific_address->addrlen));
1673 GNUNET_STATISTICS_update (stats,
1674 gettext_noop ("# connected addresses"),
1677 mq->specific_address->connected = GNUNET_NO;
1680 if (! mq->internal_msg)
1681 mq->specific_address->in_transmit = GNUNET_NO;
1683 n = find_neighbour(&mq->neighbour_id);
1684 if (mq->client != NULL)
1685 transmit_send_ok (mq->client, n, target, result);
1688 try_transmission_to_peer (n);
1693 * Find an address in any of the available transports for
1694 * the given neighbour that would be good for message
1695 * transmission. This is essentially the transport selection
1698 * @param neighbour for whom to select an address
1699 * @return selected address, NULL if we have none
1701 struct ForeignAddressList *
1702 find_ready_address(struct NeighbourList *neighbour)
1704 struct ReadyList *head = neighbour->plugins;
1705 struct ForeignAddressList *addresses;
1706 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1707 struct ForeignAddressList *best_address;
1709 /* Hack to prefer unix domain sockets */
1710 struct ForeignAddressList *unix_address = NULL;
1712 best_address = NULL;
1713 while (head != NULL)
1715 addresses = head->addresses;
1716 while (addresses != NULL)
1718 if ( (addresses->timeout.abs_value < now.abs_value) &&
1719 (addresses->connected == GNUNET_YES) )
1722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1723 "Marking long-time inactive connection to `%4s' as down.\n",
1724 GNUNET_i2s (&neighbour->id));
1726 GNUNET_STATISTICS_update (stats,
1727 gettext_noop ("# connected addresses"),
1730 addresses->connected = GNUNET_NO;
1732 addresses = addresses->next;
1735 addresses = head->addresses;
1736 while (addresses != NULL)
1738 #if DEBUG_TRANSPORT > 1
1739 if (addresses->addr != NULL)
1740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1741 "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
1742 a2s (head->plugin->short_name,
1744 addresses->addrlen),
1745 GNUNET_i2s (&neighbour->id),
1746 addresses->connected,
1747 addresses->in_transmit,
1748 addresses->validated,
1749 addresses->connect_attempts,
1750 (unsigned long long) addresses->timeout.abs_value,
1751 (unsigned int) addresses->distance);
1753 if (0==strcmp(head->plugin->short_name,"unix"))
1755 if ((unix_address == NULL) || ((unix_address != NULL) &&
1756 (addresses->latency.rel_value < unix_address->latency.rel_value)))
1757 unix_address = addresses;
1759 if ( ( (best_address == NULL) ||
1760 (addresses->connected == GNUNET_YES) ||
1761 (best_address->connected == GNUNET_NO) ) &&
1762 (addresses->in_transmit == GNUNET_NO) &&
1763 ( (best_address == NULL) ||
1764 (addresses->latency.rel_value < best_address->latency.rel_value)) )
1765 best_address = addresses;
1766 /* FIXME: also give lower-latency addresses that are not
1767 connected a chance some times... */
1768 addresses = addresses->next;
1770 if (unix_address != NULL)
1774 if (unix_address != NULL)
1776 best_address = unix_address;
1778 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found unix address, forced this address\n");
1781 if (best_address != NULL)
1785 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1786 "Best address found (`%s') has latency of %llu ms.\n",
1787 (best_address->addrlen > 0)
1788 ? a2s (best_address->ready_list->plugin->short_name,
1790 best_address->addrlen)
1792 best_address->latency.rel_value);
1797 GNUNET_STATISTICS_update (stats,
1798 gettext_noop ("# transmission attempts failed (no address)"),
1803 return best_address;
1809 * We should re-try transmitting to the given peer,
1810 * hopefully we've learned something in the meantime.
1813 retry_transmission_task (void *cls,
1814 const struct GNUNET_SCHEDULER_TaskContext *tc)
1816 struct NeighbourList *n = cls;
1818 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1819 try_transmission_to_peer (n);
1824 * Check the ready list for the given neighbour and if a plugin is
1825 * ready for transmission (and if we have a message), do so!
1827 * @param neighbour target peer for which to transmit
1830 try_transmission_to_peer (struct NeighbourList *n)
1832 struct ReadyList *rl;
1833 struct MessageQueue *mq;
1834 struct GNUNET_TIME_Relative timeout;
1838 if (n->messages_head == NULL)
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1842 "Transmission queue for `%4s' is empty\n",
1843 GNUNET_i2s (&n->id));
1845 return; /* nothing to do */
1848 mq = n->messages_head;
1849 force_address = GNUNET_YES;
1850 if (mq->specific_address == NULL)
1853 mq->specific_address = ats_get_preferred_address(n);
1854 GNUNET_STATISTICS_update (stats,
1855 gettext_noop ("# transport selected peer address freely"),
1858 force_address = GNUNET_NO;
1860 if (mq->specific_address == NULL)
1862 GNUNET_STATISTICS_update (stats,
1863 gettext_noop ("# transport failed to selected peer address"),
1866 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1867 if (timeout.rel_value == 0)
1870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1871 "No destination address available to transmit message of size %u to peer `%4s'\n",
1872 mq->message_buf_size,
1873 GNUNET_i2s (&mq->neighbour_id));
1875 GNUNET_STATISTICS_update (stats,
1876 gettext_noop ("# bytes in message queue for other peers"),
1877 - (int64_t) mq->message_buf_size,
1879 GNUNET_STATISTICS_update (stats,
1880 gettext_noop ("# bytes discarded (no destination address available)"),
1881 mq->message_buf_size,
1883 if (mq->client != NULL)
1884 transmit_send_ok (mq->client, n, &n->id, GNUNET_NO);
1885 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1889 return; /* nobody ready */
1891 GNUNET_STATISTICS_update (stats,
1892 gettext_noop ("# message delivery deferred (no address)"),
1895 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
1896 GNUNET_SCHEDULER_cancel (n->retry_task);
1897 n->retry_task = GNUNET_SCHEDULER_add_delayed (timeout,
1898 &retry_transmission_task,
1901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1902 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
1903 mq->message_buf_size,
1904 GNUNET_i2s (&mq->neighbour_id),
1907 /* FIXME: might want to trigger peerinfo lookup here
1908 (unless that's already pending...) */
1911 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1914 if (mq->specific_address->connected == GNUNET_NO)
1915 mq->specific_address->connect_attempts++;
1916 rl = mq->specific_address->ready_list;
1917 mq->plugin = rl->plugin;
1918 if (!mq->internal_msg)
1919 mq->specific_address->in_transmit = GNUNET_YES;
1921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1922 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
1923 mq->message_buf_size,
1924 GNUNET_i2s (&n->id),
1925 (mq->specific_address->addr != NULL)
1926 ? a2s (mq->plugin->short_name,
1927 mq->specific_address->addr,
1928 mq->specific_address->addrlen)
1930 rl->plugin->short_name);
1932 GNUNET_STATISTICS_update (stats,
1933 gettext_noop ("# bytes in message queue for other peers"),
1934 - (int64_t) mq->message_buf_size,
1936 GNUNET_STATISTICS_update (stats,
1937 gettext_noop ("# bytes pending with plugins"),
1938 mq->message_buf_size,
1940 ret = rl->plugin->api->send (rl->plugin->api->cls,
1943 mq->message_buf_size,
1945 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1946 mq->specific_address->session,
1947 mq->specific_address->addr,
1948 mq->specific_address->addrlen,
1950 &transmit_send_continuation, mq);
1953 /* failure, but 'send' would not call continuation in this case,
1954 so we need to do it here! */
1955 transmit_send_continuation (mq,
1963 * Send the specified message to the specified peer.
1965 * @param client source of the transmission request (can be NULL)
1966 * @param peer_address ForeignAddressList where we should send this message
1967 * @param priority how important is the message
1968 * @param timeout how long do we have to transmit?
1969 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
1970 * @param message_buf_size total size of all messages in message_buf
1971 * @param is_internal is this an internal message; these are pre-pended and
1972 * also do not count for plugins being "ready" to transmit
1973 * @param neighbour handle to the neighbour for transmission
1976 transmit_to_peer (struct TransportClient *client,
1977 struct ForeignAddressList *peer_address,
1978 unsigned int priority,
1979 struct GNUNET_TIME_Relative timeout,
1980 const char *message_buf,
1981 size_t message_buf_size,
1982 int is_internal, struct NeighbourList *neighbour)
1984 struct MessageQueue *mq;
1989 /* check for duplicate submission */
1990 mq = neighbour->messages_head;
1993 if (mq->client == client)
1995 /* client transmitted to same peer twice
1996 before getting SEND_OK! */
2004 GNUNET_STATISTICS_update (stats,
2005 gettext_noop ("# bytes in message queue for other peers"),
2008 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
2009 mq->specific_address = peer_address;
2010 mq->client = client;
2011 /* FIXME: this memcpy can be up to 7% of our total runtime! */
2012 memcpy (&mq[1], message_buf, message_buf_size);
2013 mq->message_buf = (const char*) &mq[1];
2014 mq->message_buf_size = message_buf_size;
2015 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
2016 mq->internal_msg = is_internal;
2017 mq->priority = priority;
2018 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
2020 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
2021 neighbour->messages_tail,
2024 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
2025 neighbour->messages_tail,
2026 neighbour->messages_tail,
2028 try_transmission_to_peer (neighbour);
2035 struct GeneratorContext
2037 struct TransportPlugin *plug_pos;
2038 struct OwnAddressList *addr_pos;
2039 struct GNUNET_TIME_Absolute expiration;
2047 address_generator (void *cls, size_t max, void *buf)
2049 struct GeneratorContext *gc = cls;
2052 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
2054 gc->plug_pos = gc->plug_pos->next;
2055 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
2057 if (NULL == gc->plug_pos)
2062 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
2065 gc->addr_pos->addrlen, buf, max);
2066 gc->addr_pos = gc->addr_pos->next;
2072 * Construct our HELLO message from all of the addresses of
2073 * all of the transports.
2078 struct GNUNET_HELLO_Message *hello;
2079 struct TransportClient *cpos;
2080 struct NeighbourList *npos;
2081 struct GeneratorContext gc;
2083 gc.plug_pos = plugins;
2084 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
2085 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2086 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2089 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
2091 GNUNET_STATISTICS_update (stats,
2092 gettext_noop ("# refreshed my HELLO"),
2096 while (cpos != NULL)
2098 transmit_to_client (cpos,
2099 (const struct GNUNET_MessageHeader *) hello,
2104 GNUNET_free_non_null (our_hello);
2106 GNUNET_PEERINFO_add_peer (peerinfo, our_hello);
2108 while (npos != NULL)
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2112 "Transmitting updated `%s' to neighbour `%4s'\n",
2113 "HELLO", GNUNET_i2s (&npos->id));
2115 GNUNET_STATISTICS_update (stats,
2116 gettext_noop ("# transmitted my HELLO to other peers"),
2119 transmit_to_peer (NULL, NULL, 0,
2120 HELLO_ADDRESS_EXPIRATION,
2121 (const char *) our_hello,
2122 GNUNET_HELLO_size(our_hello),
2130 * Task used to clean up expired addresses for a plugin.
2132 * @param cls closure
2136 expire_address_task (void *cls,
2137 const struct GNUNET_SCHEDULER_TaskContext *tc);
2141 * Update the list of addresses for this plugin,
2142 * expiring those that are past their expiration date.
2144 * @param plugin addresses of which plugin should be recomputed?
2145 * @param fresh set to GNUNET_YES if a new address was added
2146 * and we need to regenerate the HELLO even if nobody
2150 update_addresses (struct TransportPlugin *plugin,
2153 static struct GNUNET_TIME_Absolute last_update;
2154 struct GNUNET_TIME_Relative min_remaining;
2155 struct GNUNET_TIME_Relative remaining;
2156 struct GNUNET_TIME_Absolute now;
2157 struct OwnAddressList *pos;
2158 struct OwnAddressList *prev;
2159 struct OwnAddressList *next;
2162 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_TASK)
2163 GNUNET_SCHEDULER_cancel (plugin->address_update_task);
2164 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
2165 now = GNUNET_TIME_absolute_get ();
2166 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
2167 expired = (GNUNET_TIME_absolute_get_duration (last_update).rel_value > (HELLO_ADDRESS_EXPIRATION.rel_value / 4));
2169 pos = plugin->addresses;
2173 if (pos->expires.abs_value < now.abs_value)
2175 expired = GNUNET_YES;
2177 plugin->addresses = pos->next;
2179 prev->next = pos->next;
2184 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
2185 if (remaining.rel_value < min_remaining.rel_value)
2186 min_remaining = remaining;
2192 if (expired || fresh)
2197 min_remaining = GNUNET_TIME_relative_min (min_remaining,
2198 GNUNET_TIME_relative_divide (HELLO_ADDRESS_EXPIRATION,
2200 plugin->address_update_task
2201 = GNUNET_SCHEDULER_add_delayed (min_remaining,
2202 &expire_address_task, plugin);
2207 * Task used to clean up expired addresses for a plugin.
2209 * @param cls closure
2213 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2215 struct TransportPlugin *plugin = cls;
2217 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
2218 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2219 update_addresses (plugin, GNUNET_NO);
2224 * Iterator over hash map entries that NULLs the session of validation
2225 * entries that match the given session.
2227 * @param cls closure (the 'struct Session*' to match against)
2228 * @param key current key code (peer ID, not used)
2229 * @param value value in the hash map ('struct ValidationEntry*')
2230 * @return GNUNET_YES (we should continue to iterate)
2233 remove_session_validations (void *cls,
2234 const GNUNET_HashCode * key,
2237 struct Session *session = cls;
2238 struct ValidationEntry *ve = value;
2240 if (session == ve->session)
2247 * We've been disconnected from the other peer (for some
2248 * connection-oriented transport). Either quickly
2249 * re-establish the connection or signal the disconnect
2252 * Only signal CORE level disconnect if ALL addresses
2253 * for the peer are exhausted.
2255 * @param p overall plugin context
2256 * @param nl neighbour that was disconnected
2259 try_fast_reconnect (struct TransportPlugin *p,
2260 struct NeighbourList *nl)
2262 /* FIXME-MW: fast reconnect / transport switching not implemented... */
2263 /* Note: the idea here is to hide problems with transports (or
2264 switching between plugins) from the core to eliminate the need to
2265 re-negotiate session keys and the like; OTOH, we should tell core
2266 quickly (much faster than timeout) `if a connection was lost and
2267 could not be re-established (i.e. other peer went down or is
2268 unable / refuses to communicate);
2270 So we should consider:
2271 1) ideally: our own willingness / need to connect
2272 2) prior failures to connect to this peer (by plugin)
2273 3) ideally: reasons why other peer terminated (as far as knowable)
2275 Most importantly, it must be POSSIBLE for another peer to terminate
2276 a connection for a while (without us instantly re-establishing it).
2277 Similarly, if another peer is gone we should quickly notify CORE.
2278 OTOH, if there was a minor glitch (i.e. crash of gnunet-service-transport
2279 on the other end), we should reconnect in such a way that BOTH CORE
2280 services never even notice.
2281 Furthermore, the same mechanism (or small variation) could be used
2282 to switch to a better-performing plugin (ATS).
2284 Finally, this needs to be tested throughly... */
2287 * GNUNET_NO in the call below makes transport disconnect the peer,
2288 * even if only a single address (out of say, six) went away. This
2289 * function must be careful to ONLY disconnect if the peer is gone,
2290 * not just a specifi address.
2292 * More specifically, half the places it was used had it WRONG.
2295 /* No reconnect, signal disconnect instead! */
2296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2297 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2298 "try_fast_reconnect");
2299 disconnect_neighbour (nl, GNUNET_YES);
2304 * Function that will be called whenever the plugin internally
2305 * cleans up a session pointer and hence the service needs to
2306 * discard all of those sessions as well. Plugins that do not
2307 * use sessions can simply omit calling this function and always
2308 * use NULL wherever a session pointer is needed.
2310 * @param cls closure
2311 * @param peer which peer was the session for
2312 * @param session which session is being destoyed
2315 plugin_env_session_end (void *cls,
2316 const struct GNUNET_PeerIdentity *peer,
2317 struct Session *session)
2319 struct TransportPlugin *p = cls;
2320 struct NeighbourList *nl;
2321 struct ReadyList *rl;
2322 struct ForeignAddressList *pos;
2323 struct ForeignAddressList *prev;
2325 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2326 &remove_session_validations,
2328 nl = find_neighbour (peer);
2330 return; /* was never marked as connected */
2334 if (rl->plugin == p)
2339 return; /* was never marked as connected */
2341 pos = rl->addresses;
2342 while ( (pos != NULL) &&
2343 (pos->session != session) )
2349 return; /* was never marked as connected */
2350 pos->session = NULL;
2351 if (pos->addrlen != 0)
2353 if (nl->received_pong != GNUNET_NO)
2354 try_fast_reconnect (p, nl);
2357 /* was inbound connection, free 'pos' */
2359 rl->addresses = pos->next;
2361 prev->next = pos->next;
2362 if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2364 GNUNET_SCHEDULER_cancel (pos->revalidate_task);
2365 pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2368 if (nl->received_pong == GNUNET_NO)
2369 return; /* nothing to do, never connected... */
2370 /* check if we have any validated addresses left */
2371 pos = rl->addresses;
2376 try_fast_reconnect (p, nl);
2381 /* no valid addresses left, signal disconnect! */
2383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2384 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2385 "plugin_env_session_end");
2386 /* FIXME: This doesn't mean there are no addresses left for this PEER,
2387 * it means there aren't any left for this PLUGIN/PEER combination! So
2388 * calling disconnect_neighbor here with GNUNET_NO forces disconnect
2389 * when it isn't necessary. Using GNUNET_YES at least checks to see
2390 * if there are any addresses that work first, so as not to overdo it.
2393 disconnect_neighbour (nl, GNUNET_YES);
2398 * Function that must be called by each plugin to notify the
2399 * transport service about the addresses under which the transport
2400 * provided by the plugin can be reached.
2402 * @param cls closure
2403 * @param name name of the transport that generated the address
2404 * @param addr one of the addresses of the host, NULL for the last address
2405 * the specific address format depends on the transport
2406 * @param addrlen length of the address
2407 * @param expires when should this address automatically expire?
2410 plugin_env_notify_address (void *cls,
2414 struct GNUNET_TIME_Relative expires)
2416 struct TransportPlugin *p = cls;
2417 struct OwnAddressList *al;
2418 struct GNUNET_TIME_Absolute abex;
2420 GNUNET_assert (addr != NULL);
2421 abex = GNUNET_TIME_relative_to_absolute (expires);
2422 GNUNET_assert (p == find_transport (name));
2426 if ( (addrlen == al->addrlen) &&
2427 (0 == memcmp (addr, &al[1], addrlen)) )
2430 update_addresses (p, GNUNET_NO);
2435 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2436 al->next = p->addresses;
2439 al->addrlen = addrlen;
2440 memcpy (&al[1], addr, addrlen);
2441 update_addresses (p, GNUNET_YES);
2446 * Notify all of our clients about a peer connecting.
2449 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2450 struct GNUNET_TIME_Relative latency,
2453 struct ConnectInfoMessage * cim;
2454 struct TransportClient *cpos;
2459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2460 "Notifying clients about connection from `%s'\n",
2463 GNUNET_STATISTICS_update (stats,
2464 gettext_noop ("# peers connected"),
2469 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
2470 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2474 cim = GNUNET_malloc (size);
2476 cim->header.size = htons (size);
2477 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2478 cim->ats_count = htonl(2);
2479 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2480 (&(cim->ats))[0].value = htonl (distance);
2481 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
2482 (&(cim->ats))[1].value = htonl ((uint32_t) latency.rel_value);
2483 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2484 (&(cim->ats))[2].value = htonl (0);
2485 memcpy (&cim->id, peer, sizeof (struct GNUNET_PeerIdentity));
2487 /* notify ats about connecting peer */
2488 ats_notify_peer_connect (peer, &(cim->ats));
2491 while (cpos != NULL)
2493 transmit_to_client (cpos, &(cim->header), GNUNET_NO);
2502 * Notify all of our clients about a peer disconnecting.
2505 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2507 struct DisconnectInfoMessage dim;
2508 struct TransportClient *cpos;
2511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2512 "Notifying clients about lost connection to `%s'\n",
2515 GNUNET_STATISTICS_update (stats,
2516 gettext_noop ("# peers connected"),
2519 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2520 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2521 dim.reserved = htonl (0);
2522 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2524 /* notify ats about connecting peer */
2525 ats_notify_peer_disconnect (peer);
2528 while (cpos != NULL)
2530 transmit_to_client (cpos, &dim.header, GNUNET_NO);
2537 * Find a ForeignAddressList entry for the given neighbour
2538 * that matches the given address and transport.
2540 * @param neighbour which peer we care about
2541 * @param tname name of the transport plugin
2542 * @param session session to look for, NULL for 'any'; otherwise
2543 * can be used for the service to "learn" this session ID
2545 * @param addr binary address
2546 * @param addrlen length of addr
2547 * @return NULL if no such entry exists
2549 static struct ForeignAddressList *
2550 find_peer_address(struct NeighbourList *neighbour,
2552 struct Session *session,
2556 struct ReadyList *head;
2557 struct ForeignAddressList *pos;
2559 head = neighbour->plugins;
2560 while (head != NULL)
2562 if (0 == strcmp (tname, head->plugin->short_name))
2568 pos = head->addresses;
2569 while ( (pos != NULL) &&
2570 ( (pos->addrlen != addrlen) ||
2571 (memcmp(pos->addr, addr, addrlen) != 0) ) )
2573 if ( (session != NULL) &&
2574 (pos->session == session) )
2578 if ( (session != NULL) && (pos != NULL) )
2579 pos->session = session; /* learn it! */
2585 * Get the peer address struct for the given neighbour and
2586 * address. If it doesn't yet exist, create it.
2588 * @param neighbour which peer we care about
2589 * @param tname name of the transport plugin
2590 * @param session session of the plugin, or NULL for none
2591 * @param addr binary address
2592 * @param addrlen length of addr
2593 * @return NULL if we do not have a transport plugin for 'tname'
2595 static struct ForeignAddressList *
2596 add_peer_address (struct NeighbourList *neighbour,
2598 struct Session *session,
2602 struct ReadyList *head;
2603 struct ForeignAddressList *ret;
2606 ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2609 head = neighbour->plugins;
2611 while (head != NULL)
2613 if (0 == strcmp (tname, head->plugin->short_name))
2619 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2620 ret->session = session;
2621 if ((addrlen > 0) && (addr != NULL))
2623 ret->addr = (const char*) &ret[1];
2624 memcpy (&ret[1], addr, addrlen);
2631 ret->ressources = GNUNET_malloc(available_ressources * sizeof (struct ATS_ressource_entry));
2633 for (c=0; c<available_ressources; c++)
2635 struct ATS_ressource_entry *r = ret->ressources;
2637 r[c].atis_index = ressources[c].atis_index;
2638 if (0 == strcmp(neighbour->plugins->plugin->short_name,"unix"))
2640 r[c].c = ressources[c].c_unix;
2643 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"udp"))
2645 r[c].c = ressources[c].c_udp;
2648 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"tcp"))
2650 r[c].c = ressources[c].c_tcp;
2653 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"http"))
2655 r[c].c = ressources[c].c_http;
2658 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"https"))
2660 r[c].c = ressources[c].c_https;
2663 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"wlan"))
2665 r[c].c = ressources[c].c_wlan;
2671 r[c].c = ressources[c].c_default;
2672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"Assigning default cost to peer `%s' addr plugin `%s'! This should not happen!",
2673 GNUNET_i2s(&neighbour->peer), neighbour->plugins->plugin->short_name);
2677 ret->quality = GNUNET_malloc (available_quality_metrics * sizeof (struct ATS_quality_entry));
2678 ret->addrlen = addrlen;
2679 ret->expires = GNUNET_TIME_relative_to_absolute
2680 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2681 ret->latency = GNUNET_TIME_relative_get_forever();
2683 ret->timeout = GNUNET_TIME_relative_to_absolute
2684 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2685 ret->ready_list = head;
2686 ret->next = head->addresses;
2687 head->addresses = ret;
2693 * Closure for 'add_validated_address'.
2695 struct AddValidatedAddressContext
2698 * Entry that has been validated.
2700 const struct ValidationEntry *ve;
2703 * Flag set after we have added the address so
2704 * that we terminate the iteration next time.
2711 * Callback function used to fill a buffer of max bytes with a list of
2712 * addresses in the format used by HELLOs. Should use
2713 * "GNUNET_HELLO_add_address" as a helper function.
2715 * @param cls the 'struct AddValidatedAddressContext' with the validated address
2716 * @param max maximum number of bytes that can be written to buf
2717 * @param buf where to write the address information
2718 * @return number of bytes written, 0 to signal the
2719 * end of the iteration.
2722 add_validated_address (void *cls,
2723 size_t max, void *buf)
2725 struct AddValidatedAddressContext *avac = cls;
2726 const struct ValidationEntry *ve = avac->ve;
2728 if (GNUNET_YES == avac->done)
2730 avac->done = GNUNET_YES;
2731 return GNUNET_HELLO_add_address (ve->transport_name,
2732 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2742 * Closure for 'check_address_exists'.
2744 struct CheckAddressExistsClosure
2747 * Address to check for.
2752 * Name of the transport.
2759 struct Session *session;
2762 * Set to GNUNET_YES if the address exists.
2775 * Iterator over hash map entries. Checks if the given
2776 * validation entry is for the same address as what is given
2779 * @param cls the 'struct CheckAddressExistsClosure*'
2780 * @param key current key code (ignored)
2781 * @param value value in the hash map ('struct ValidationEntry')
2782 * @return GNUNET_YES if we should continue to
2783 * iterate (mismatch), GNUNET_NO if not (entry matched)
2786 check_address_exists (void *cls,
2787 const GNUNET_HashCode * key,
2790 struct CheckAddressExistsClosure *caec = cls;
2791 struct ValidationEntry *ve = value;
2793 if ( (0 == strcmp (caec->tname,
2794 ve->transport_name)) &&
2795 (caec->addrlen == ve->addrlen) &&
2796 (0 == memcmp (caec->addr,
2800 caec->exists = GNUNET_YES;
2803 if ( (ve->session != NULL) &&
2804 (caec->session == ve->session) )
2806 caec->exists = GNUNET_YES;
2815 * Iterator to free entries in the validation_map.
2817 * @param cls closure (unused)
2818 * @param key current key code
2819 * @param value value in the hash map (validation to abort)
2820 * @return GNUNET_YES (always)
2823 abort_validation (void *cls,
2824 const GNUNET_HashCode * key,
2827 struct ValidationEntry *va = value;
2829 if (GNUNET_SCHEDULER_NO_TASK != va->timeout_task)
2830 GNUNET_SCHEDULER_cancel (va->timeout_task);
2831 GNUNET_free (va->transport_name);
2832 if (va->chvc != NULL)
2834 va->chvc->ve_count--;
2835 if (va->chvc->ve_count == 0)
2837 GNUNET_CONTAINER_DLL_remove (chvc_head,
2840 GNUNET_free (va->chvc);
2850 * HELLO validation cleanup task (validation failed).
2852 * @param cls the 'struct ValidationEntry' that failed
2853 * @param tc scheduler context (unused)
2856 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2858 struct ValidationEntry *va = cls;
2859 struct GNUNET_PeerIdentity pid;
2861 va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2862 GNUNET_STATISTICS_update (stats,
2863 gettext_noop ("# address validation timeouts"),
2866 GNUNET_CRYPTO_hash (&va->publicKey,
2868 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2870 GNUNET_break (GNUNET_OK ==
2871 GNUNET_CONTAINER_multihashmap_remove (validation_map,
2874 abort_validation (NULL, NULL, va);
2879 neighbour_timeout_task (void *cls,
2880 const struct GNUNET_SCHEDULER_TaskContext *tc)
2882 struct NeighbourList *n = cls;
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2886 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
2888 GNUNET_STATISTICS_update (stats,
2889 gettext_noop ("# disconnects due to timeout"),
2892 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2893 disconnect_neighbour (n, GNUNET_NO);
2898 * Schedule the job that will cause us to send a PING to the
2899 * foreign address to evaluate its validity and latency.
2901 * @param fal address to PING
2904 schedule_next_ping (struct ForeignAddressList *fal);
2908 * Add the given address to the list of foreign addresses
2909 * available for the given peer (check for duplicates).
2911 * @param cls the respective 'struct NeighbourList' to update
2912 * @param tname name of the transport
2913 * @param expiration expiration time
2914 * @param addr the address
2915 * @param addrlen length of the address
2916 * @return GNUNET_OK (always)
2919 add_to_foreign_address_list (void *cls,
2921 struct GNUNET_TIME_Absolute expiration,
2925 struct NeighbourList *n = cls;
2926 struct ForeignAddressList *fal;
2929 GNUNET_STATISTICS_update (stats,
2930 gettext_noop ("# valid peer addresses returned by PEERINFO"),
2934 fal = find_peer_address (n, tname, NULL, addr, addrlen);
2937 #if DEBUG_TRANSPORT_HELLO
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2939 "Adding address `%s' (%s) for peer `%4s' due to PEERINFO data for %llums.\n",
2940 a2s (tname, addr, addrlen),
2942 GNUNET_i2s (&n->id),
2943 expiration.abs_value);
2945 fal = add_peer_address (n, tname, NULL, addr, addrlen);
2948 GNUNET_STATISTICS_update (stats,
2949 gettext_noop ("# previously validated addresses lacking transport"),
2955 fal->expires = GNUNET_TIME_absolute_max (expiration,
2957 schedule_next_ping (fal);
2963 fal->expires = GNUNET_TIME_absolute_max (expiration,
2968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2969 "Failed to add new address for `%4s'\n",
2970 GNUNET_i2s (&n->id));
2973 if (fal->validated == GNUNET_NO)
2975 fal->validated = GNUNET_YES;
2976 GNUNET_STATISTICS_update (stats,
2977 gettext_noop ("# peer addresses considered valid"),
2981 if (try == GNUNET_YES)
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2984 "Have new addresses, will try to trigger transmissions.\n");
2985 try_transmission_to_peer (n);
2992 * Add addresses in validated HELLO "h" to the set of addresses
2993 * we have for this peer.
2995 * @param cls closure ('struct NeighbourList*')
2996 * @param peer id of the peer, NULL for last call
2997 * @param h hello message for the peer (can be NULL)
2998 * @param err_msg NULL if successful, otherwise contains error message
3001 add_hello_for_peer (void *cls,
3002 const struct GNUNET_PeerIdentity *peer,
3003 const struct GNUNET_HELLO_Message *h,
3004 const char *err_msg)
3006 struct NeighbourList *n = cls;
3008 if (err_msg != NULL)
3010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3011 _("Error in communication with PEERINFO service\n"));
3016 GNUNET_STATISTICS_update (stats,
3017 gettext_noop ("# outstanding peerinfo iterate requests"),
3024 return; /* no HELLO available */
3026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3027 "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
3031 if (GNUNET_YES != n->public_key_valid)
3033 GNUNET_HELLO_get_key (h, &n->publicKey);
3034 n->public_key_valid = GNUNET_YES;
3036 GNUNET_HELLO_iterate_addresses (h,
3038 &add_to_foreign_address_list,
3044 * Create a fresh entry in our neighbour list for the given peer.
3045 * Will try to transmit our current HELLO to the new neighbour.
3046 * Do not call this function directly, use 'setup_peer_check_blacklist.
3048 * @param peer the peer for which we create the entry
3049 * @param do_hello should we schedule transmitting a HELLO
3050 * @return the new neighbour list entry
3052 static struct NeighbourList *
3053 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
3056 struct NeighbourList *n;
3057 struct TransportPlugin *tp;
3058 struct ReadyList *rl;
3061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3062 "Setting up state for neighbour `%4s'\n",
3065 GNUNET_assert (our_hello != NULL);
3066 GNUNET_STATISTICS_update (stats,
3067 gettext_noop ("# active neighbours"),
3070 n = GNUNET_malloc (sizeof (struct NeighbourList));
3071 n->next = neighbours;
3075 GNUNET_TIME_relative_to_absolute
3076 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3077 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
3078 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3079 MAX_BANDWIDTH_CARRY_S);
3083 if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
3085 rl = GNUNET_malloc (sizeof (struct ReadyList));
3087 rl->next = n->plugins;
3090 rl->addresses = NULL;
3094 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
3096 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
3097 &neighbour_timeout_task, n);
3100 GNUNET_STATISTICS_update (stats,
3101 gettext_noop ("# peerinfo new neighbor iterate requests"),
3104 GNUNET_STATISTICS_update (stats,
3105 gettext_noop ("# outstanding peerinfo iterate requests"),
3108 n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
3109 GNUNET_TIME_UNIT_FOREVER_REL,
3110 &add_hello_for_peer, n);
3112 GNUNET_STATISTICS_update (stats,
3113 gettext_noop ("# HELLO's sent to new neighbors"),
3116 transmit_to_peer (NULL, NULL, 0,
3117 HELLO_ADDRESS_EXPIRATION,
3118 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
3126 * Function called after we have checked if communicating
3127 * with a given peer is acceptable.
3129 * @param cls closure
3130 * @param n NULL if communication is not acceptable
3132 typedef void (*SetupContinuation)(void *cls,
3133 struct NeighbourList *n);
3137 * Information kept for each client registered to perform
3143 * This is a linked list.
3145 struct Blacklisters *next;
3148 * This is a linked list.
3150 struct Blacklisters *prev;
3153 * Client responsible for this entry.
3155 struct GNUNET_SERVER_Client *client;
3158 * Blacklist check that we're currently performing.
3160 struct BlacklistCheck *bc;
3166 * Head of DLL of blacklisting clients.
3168 static struct Blacklisters *bl_head;
3171 * Tail of DLL of blacklisting clients.
3173 static struct Blacklisters *bl_tail;
3177 * Context we use when performing a blacklist check.
3179 struct BlacklistCheck
3183 * This is a linked list.
3185 struct BlacklistCheck *next;
3188 * This is a linked list.
3190 struct BlacklistCheck *prev;
3193 * Peer being checked.
3195 struct GNUNET_PeerIdentity peer;
3198 * Option for setup neighbour afterwards.
3203 * Continuation to call with the result.
3205 SetupContinuation cont;
3213 * Current transmission request handle for this client, or NULL if no
3214 * request is pending.
3216 struct GNUNET_CONNECTION_TransmitHandle *th;
3219 * Our current position in the blacklisters list.
3221 struct Blacklisters *bl_pos;
3224 * Current task performing the check.
3226 GNUNET_SCHEDULER_TaskIdentifier task;
3231 * Head of DLL of active blacklisting queries.
3233 static struct BlacklistCheck *bc_head;
3236 * Tail of DLL of active blacklisting queries.
3238 static struct BlacklistCheck *bc_tail;
3242 * Perform next action in the blacklist check.
3244 * @param cls the 'struct BlacklistCheck*'
3248 do_blacklist_check (void *cls,
3249 const struct GNUNET_SCHEDULER_TaskContext *tc);
3252 * Transmit blacklist query to the client.
3254 * @param cls the 'struct BlacklistCheck'
3255 * @param size number of bytes allowed
3256 * @param buf where to copy the message
3257 * @return number of bytes copied to buf
3260 transmit_blacklist_message (void *cls,
3264 struct BlacklistCheck *bc = cls;
3265 struct Blacklisters *bl;
3266 struct BlacklistMessage bm;
3271 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
3272 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3277 bm.header.size = htons (sizeof (struct BlacklistMessage));
3278 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
3279 bm.is_allowed = htonl (0);
3281 memcpy (buf, &bm, sizeof (bm));
3282 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
3288 * Perform next action in the blacklist check.
3290 * @param cls the 'struct BlacklistCheck*'
3294 do_blacklist_check (void *cls,
3295 const struct GNUNET_SCHEDULER_TaskContext *tc)
3297 struct BlacklistCheck *bc = cls;
3298 struct Blacklisters *bl;
3300 bc->task = GNUNET_SCHEDULER_NO_TASK;
3304 bc->cont (bc->cont_cls,
3305 setup_new_neighbour (&bc->peer, bc->do_hello));
3312 bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
3313 sizeof (struct BlacklistMessage),
3314 GNUNET_TIME_UNIT_FOREVER_REL,
3315 &transmit_blacklist_message,
3322 * Obtain a 'struct NeighbourList' for the given peer. If such an entry
3323 * does not yet exist, check the blacklist. If the blacklist says creating
3324 * one is acceptable, create one and call the continuation; otherwise
3325 * call the continuation with NULL.
3327 * @param peer peer to setup or look up a struct NeighbourList for
3328 * @param do_hello should we also schedule sending our HELLO to the peer
3329 * if this is a new record
3330 * @param cont function to call with the 'struct NeigbhbourList*'
3331 * @param cont_cls closure for cont
3334 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
3336 SetupContinuation cont,
3339 struct NeighbourList *n;
3340 struct BlacklistCheck *bc;
3342 n = find_neighbour(peer);
3349 if (bl_head == NULL)
3352 cont (cont_cls, setup_new_neighbour (peer, do_hello));
3354 setup_new_neighbour(peer, do_hello);
3357 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3358 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3360 bc->do_hello = do_hello;
3362 bc->cont_cls = cont_cls;
3363 bc->bl_pos = bl_head;
3364 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3370 * Function called with the result of querying a new blacklister about
3371 * it being allowed (or not) to continue to talk to an existing neighbour.
3373 * @param cls the original 'struct NeighbourList'
3374 * @param n NULL if we need to disconnect
3377 confirm_or_drop_neighbour (void *cls,
3378 struct NeighbourList *n)
3380 struct NeighbourList * orig = cls;
3384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3385 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&orig->id),
3386 "confirm_or_drop_neighboUr");
3387 disconnect_neighbour (orig, GNUNET_NO);
3393 * Handle a request to start a blacklist.
3395 * @param cls closure (always NULL)
3396 * @param client identification of the client
3397 * @param message the actual message
3400 handle_blacklist_init (void *cls,
3401 struct GNUNET_SERVER_Client *client,
3402 const struct GNUNET_MessageHeader *message)
3404 struct Blacklisters *bl;
3405 struct BlacklistCheck *bc;
3406 struct NeighbourList *n;
3411 if (bl->client == client)
3414 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3419 bl = GNUNET_malloc (sizeof (struct Blacklisters));
3420 bl->client = client;
3421 GNUNET_SERVER_client_keep (client);
3422 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
3423 /* confirm that all existing connections are OK! */
3427 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3428 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3430 bc->do_hello = GNUNET_NO;
3431 bc->cont = &confirm_or_drop_neighbour;
3434 if (n == neighbours) /* all would wait for the same client, no need to
3435 create more than just the first task right now */
3436 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3444 * Handle a request to blacklist a peer.
3446 * @param cls closure (always NULL)
3447 * @param client identification of the client
3448 * @param message the actual message
3451 handle_blacklist_reply (void *cls,
3452 struct GNUNET_SERVER_Client *client,
3453 const struct GNUNET_MessageHeader *message)
3455 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
3456 struct Blacklisters *bl;
3457 struct BlacklistCheck *bc;
3460 while ( (bl != NULL) &&
3461 (bl->client != client) )
3465 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3470 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
3472 bc->cont (bc->cont_cls, NULL);
3473 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
3478 bc->bl_pos = bc->bl_pos->next;
3479 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3482 /* check if any other bc's are waiting for this blacklister */
3486 if ( (bc->bl_pos == bl) &&
3487 (GNUNET_SCHEDULER_NO_TASK == bc->task) )
3488 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3496 * Send periodic PING messages to a given foreign address.
3498 * @param cls our 'struct PeriodicValidationContext*'
3499 * @param tc task context
3502 send_periodic_ping (void *cls,
3503 const struct GNUNET_SCHEDULER_TaskContext *tc)
3505 struct ForeignAddressList *peer_address = cls;
3506 struct TransportPlugin *tp;
3507 struct ValidationEntry *va;
3508 struct NeighbourList *neighbour;
3509 struct TransportPingMessage ping;
3510 struct CheckAddressExistsClosure caec;
3512 uint16_t hello_size;
3516 peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3517 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3519 tp = peer_address->ready_list->plugin;
3520 neighbour = peer_address->ready_list->neighbour;
3521 if (GNUNET_YES != neighbour->public_key_valid)
3523 /* no public key yet, try again later */
3524 schedule_next_ping (peer_address);
3527 caec.addr = peer_address->addr;
3528 caec.addrlen = peer_address->addrlen;
3529 caec.tname = tp->short_name;
3530 caec.session = peer_address->session;
3531 caec.exists = GNUNET_NO;
3532 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3533 &check_address_exists,
3535 if (caec.exists == GNUNET_YES)
3537 /* During validation attempts we will likely trigger the other
3538 peer trying to validate our address which in turn will cause
3539 it to send us its HELLO, so we expect to hit this case rather
3540 frequently. Only print something if we are very verbose. */
3541 #if DEBUG_TRANSPORT > 1
3542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3543 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3544 (peer_address->addr != NULL)
3545 ? a2s (tp->short_name,
3547 peer_address->addrlen)
3550 GNUNET_i2s (&neighbour->id));
3552 schedule_next_ping (peer_address);
3555 va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3556 va->transport_name = GNUNET_strdup (tp->short_name);
3557 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
3559 va->send_time = GNUNET_TIME_absolute_get();
3560 va->session = peer_address->session;
3561 if (peer_address->addr != NULL)
3563 va->addr = (const void*) &va[1];
3564 memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3565 va->addrlen = peer_address->addrlen;
3567 memcpy(&va->publicKey,
3568 &neighbour->publicKey,
3569 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3571 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
3572 &timeout_hello_validation,
3574 GNUNET_CONTAINER_multihashmap_put (validation_map,
3575 &neighbour->id.hashPubKey,
3577 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3579 if (peer_address->validated != GNUNET_YES)
3580 hello_size = GNUNET_HELLO_size(our_hello);
3584 tsize = sizeof(struct TransportPingMessage) + hello_size;
3586 if (peer_address->addr != NULL)
3588 slen = strlen (tp->short_name) + 1;
3589 tsize += slen + peer_address->addrlen;
3593 slen = 0; /* make gcc happy */
3595 message_buf = GNUNET_malloc(tsize);
3596 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3597 ping.challenge = htonl(va->challenge);
3598 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3599 if (peer_address->validated != GNUNET_YES)
3601 memcpy(message_buf, our_hello, hello_size);
3604 if (peer_address->addr != NULL)
3606 ping.header.size = htons(sizeof(struct TransportPingMessage) +
3607 peer_address->addrlen +
3609 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3612 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3614 peer_address->addrlen);
3618 ping.header.size = htons(sizeof(struct TransportPingMessage));
3621 memcpy(&message_buf[hello_size],
3623 sizeof(struct TransportPingMessage));
3625 #if DEBUG_TRANSPORT_REVALIDATION
3626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3627 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s'\n",
3628 (peer_address->addr != NULL)
3629 ? a2s (peer_address->plugin->short_name,
3631 peer_address->addrlen)
3634 GNUNET_i2s (&neighbour->id),
3635 "HELLO", hello_size,
3638 if (peer_address->validated != GNUNET_YES)
3639 GNUNET_STATISTICS_update (stats,
3640 gettext_noop ("# PING with HELLO messages sent"),
3644 GNUNET_STATISTICS_update (stats,
3645 gettext_noop ("# PING without HELLO messages sent"),
3648 GNUNET_STATISTICS_update (stats,
3649 gettext_noop ("# PING messages sent for re-validation"),
3652 transmit_to_peer (NULL, peer_address,
3653 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3654 HELLO_VERIFICATION_TIMEOUT,
3656 GNUNET_YES, neighbour);
3657 GNUNET_free(message_buf);
3658 schedule_next_ping (peer_address);
3663 * Schedule the job that will cause us to send a PING to the
3664 * foreign address to evaluate its validity and latency.
3666 * @param fal address to PING
3669 schedule_next_ping (struct ForeignAddressList *fal)
3671 struct GNUNET_TIME_Relative delay;
3673 if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3675 delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3676 delay.rel_value /= 2; /* do before expiration */
3677 delay = GNUNET_TIME_relative_min (delay,
3678 LATENCY_EVALUATION_MAX_DELAY);
3679 if (GNUNET_YES != fal->estimated)
3681 delay = GNUNET_TIME_UNIT_ZERO;
3682 fal->estimated = GNUNET_YES;
3684 if (GNUNET_YES == fal->connected)
3686 delay = GNUNET_TIME_relative_min (delay,
3687 CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3689 /* FIXME: also adjust delay based on how close the last
3690 observed latency is to the latency of the best alternative */
3691 /* bound how fast we can go */
3692 delay = GNUNET_TIME_relative_max (delay,
3693 GNUNET_TIME_UNIT_SECONDS);
3694 /* randomize a bit (to avoid doing all at the same time) */
3695 delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3696 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(delay,
3697 &send_periodic_ping,
3705 * Function that will be called if we receive some payload
3706 * from another peer.
3708 * @param message the payload
3709 * @param n peer who claimed to be the sender
3712 handle_payload_message (const struct GNUNET_MessageHeader *message,
3713 struct NeighbourList *n)
3715 struct InboundMessage *im;
3716 struct TransportClient *cpos;
3719 msize = ntohs (message->size);
3720 if (n->received_pong == GNUNET_NO)
3722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3723 "Received message of type %u and size %u from `%4s', but no pong yet!!\n",
3724 ntohs (message->type),
3725 ntohs (message->size),
3726 GNUNET_i2s (&n->id));
3727 GNUNET_free_non_null (n->pre_connect_message_buffer);
3728 n->pre_connect_message_buffer = GNUNET_malloc (msize);
3729 memcpy (n->pre_connect_message_buffer, message, msize);
3734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3735 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
3736 ntohs (message->type),
3737 ntohs (message->size),
3738 GNUNET_i2s (&n->id));
3740 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3743 n->quota_violation_count++;
3745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3746 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
3747 n->in_tracker.available_bytes_per_s__,
3748 n->quota_violation_count);
3750 /* Discount 32k per violation */
3751 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3756 if (n->quota_violation_count > 0)
3758 /* try to add 32k back */
3759 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3761 n->quota_violation_count--;
3764 GNUNET_STATISTICS_update (stats,
3765 gettext_noop ("# payload received from other peers"),
3768 /* transmit message to all clients */
3769 uint32_t ats_count = 2;
3770 size_t size = sizeof (struct InboundMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) + msize;
3771 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
3774 im = GNUNET_malloc (size);
3775 im->header.size = htons (size);
3776 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3778 im->ats_count = htonl(ats_count);
3779 /* Setting ATS data */
3780 (&(im->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
3781 (&(im->ats))[0].value = htonl (n->distance);
3782 (&(im->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
3783 (&(im->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
3784 (&(im->ats))[ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
3785 (&(im->ats))[ats_count].value = htonl (0);
3787 memcpy (&((&(im->ats))[ats_count+1]), message, msize);
3789 while (cpos != NULL)
3791 transmit_to_client (cpos, &im->header, GNUNET_YES);
3799 * Iterator over hash map entries. Checks if the given validation
3800 * entry is for the same challenge as what is given in the PONG.
3802 * @param cls the 'struct TransportPongMessage*'
3803 * @param key peer identity
3804 * @param value value in the hash map ('struct ValidationEntry')
3805 * @return GNUNET_YES if we should continue to
3806 * iterate (mismatch), GNUNET_NO if not (entry matched)
3809 check_pending_validation (void *cls,
3810 const GNUNET_HashCode * key,
3813 const struct TransportPongMessage *pong = cls;
3814 struct ValidationEntry *ve = value;
3815 struct AddValidatedAddressContext avac;
3816 unsigned int challenge = ntohl(pong->challenge);
3817 struct GNUNET_HELLO_Message *hello;
3818 struct GNUNET_PeerIdentity target;
3819 struct NeighbourList *n;
3820 struct ForeignAddressList *fal;
3821 struct OwnAddressList *oal;
3822 struct TransportPlugin *tp;
3823 struct GNUNET_MessageHeader *prem;
3829 ps = ntohs (pong->header.size);
3830 if (ps < sizeof (struct TransportPongMessage))
3832 GNUNET_break_op (0);
3835 addr = (const char*) &pong[1];
3836 slen = strlen (ve->transport_name) + 1;
3837 if ( (ps - sizeof (struct TransportPongMessage) < slen) ||
3838 (ve->challenge != challenge) ||
3839 (addr[slen-1] != '\0') ||
3840 (0 != strcmp (addr, ve->transport_name)) ||
3841 (ntohl (pong->purpose.size)
3842 != sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3844 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
3845 sizeof (struct GNUNET_PeerIdentity) + ps - sizeof (struct TransportPongMessage)) )
3850 alen = ps - sizeof (struct TransportPongMessage) - slen;
3851 switch (ntohl (pong->purpose.purpose))
3853 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN:
3854 if ( (ve->addrlen + slen != ntohl (pong->addrlen)) ||
3855 (0 != memcmp (&addr[slen],
3859 return GNUNET_YES; /* different entry, keep trying! */
3861 if (0 != memcmp (&pong->pid,
3863 sizeof (struct GNUNET_PeerIdentity)))
3865 GNUNET_break_op (0);
3869 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
3874 GNUNET_break_op (0);
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3880 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
3882 a2s (ve->transport_name,
3883 (const struct sockaddr *) ve->addr,
3885 ve->transport_name);
3888 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING:
3889 if (0 != memcmp (&pong->pid,
3891 sizeof (struct GNUNET_PeerIdentity)))
3893 GNUNET_break_op (0);
3896 if (ve->addrlen != 0)
3898 /* must have been for a different validation entry */
3901 tp = find_transport (ve->transport_name);
3907 oal = tp->addresses;
3910 if ( (oal->addrlen == alen) &&
3911 (0 == memcmp (&oal[1],
3919 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3920 _("Not accepting PONG with address `%s' since I cannot confirm having this address.\n"),
3921 a2s (ve->transport_name,
3927 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING,
3932 GNUNET_break_op (0);
3937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3938 "Confirmed that peer `%4s' is talking to us using address `%s' (%s) for us.\n",
3940 a2s (ve->transport_name,
3943 ve->transport_name);
3947 GNUNET_break_op (0);
3950 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
3952 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3953 _("Received expired signature. Check system time.\n"));
3956 GNUNET_STATISTICS_update (stats,
3957 gettext_noop ("# address validation successes"),
3960 /* create the updated HELLO */
3961 GNUNET_CRYPTO_hash (&ve->publicKey,
3962 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3963 &target.hashPubKey);
3964 if (ve->addr != NULL)
3966 avac.done = GNUNET_NO;
3968 hello = GNUNET_HELLO_create (&ve->publicKey,
3969 &add_validated_address,
3971 GNUNET_PEERINFO_add_peer (peerinfo,
3973 GNUNET_free (hello);
3975 n = find_neighbour (&target);
3978 n->publicKey = ve->publicKey;
3979 n->public_key_valid = GNUNET_YES;
3980 fal = add_peer_address (n,
3985 GNUNET_assert (fal != NULL);
3986 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
3987 fal->validated = GNUNET_YES;
3988 mark_address_connected (fal);
3989 GNUNET_STATISTICS_update (stats,
3990 gettext_noop ("# peer addresses considered valid"),
3993 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
3994 update_addr_value (fal, GNUNET_TIME_absolute_get_duration (ve->send_time).rel_value, GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
3996 schedule_next_ping (fal);
3997 if (n->latency.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3998 n->latency = fal->latency;
4000 n->latency.rel_value = (fal->latency.rel_value + n->latency.rel_value) / 2;
4002 n->distance = fal->distance;
4003 if (GNUNET_NO == n->received_pong)
4005 n->received_pong = GNUNET_YES;
4007 notify_clients_connect (&target, n->latency, n->distance);
4008 if (NULL != (prem = n->pre_connect_message_buffer))
4010 n->pre_connect_message_buffer = NULL;
4011 handle_payload_message (prem, n);
4015 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4017 GNUNET_SCHEDULER_cancel (n->retry_task);
4018 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4019 try_transmission_to_peer (n);
4023 /* clean up validation entry */
4024 GNUNET_assert (GNUNET_YES ==
4025 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4028 abort_validation (NULL, NULL, ve);
4034 * Function that will be called if we receive a validation
4035 * of an address challenge that we transmitted to another
4036 * peer. Note that the validation should only be considered
4037 * acceptable if the challenge matches AND if the sender
4038 * address is at least a plausible address for this peer
4039 * (otherwise we may be seeing a MiM attack).
4041 * @param cls closure
4042 * @param message the pong message
4043 * @param peer who responded to our challenge
4044 * @param sender_address string describing our sender address (as observed
4045 * by the other peer in binary format)
4046 * @param sender_address_len number of bytes in 'sender_address'
4049 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
4050 const struct GNUNET_PeerIdentity *peer,
4051 const char *sender_address,
4052 size_t sender_address_len)
4054 #if DEBUG_TRANSPORT > 1
4055 /* we get tons of these that just get discarded, only log
4056 if we are quite verbose */
4057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4058 "Receiving `%s' message from `%4s'.\n", "PONG",
4061 GNUNET_STATISTICS_update (stats,
4062 gettext_noop ("# PONG messages received"),
4065 if (GNUNET_SYSERR !=
4066 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
4068 &check_pending_validation,
4071 /* This is *expected* to happen a lot since we send
4072 PONGs to *all* known addresses of the sender of
4073 the PING, so most likely we get multiple PONGs
4074 per PING, and all but the first PONG will end up
4075 here. So really we should not print anything here
4076 unless we want to be very, very verbose... */
4077 #if DEBUG_TRANSPORT > 2
4078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4079 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
4091 * Try to validate a neighbour's address by sending him our HELLO and a PING.
4093 * @param cls the 'struct ValidationEntry*'
4094 * @param neighbour neighbour to validate, NULL if validation failed
4097 transmit_hello_and_ping (void *cls,
4098 struct NeighbourList *neighbour)
4100 struct ValidationEntry *va = cls;
4101 struct ForeignAddressList *peer_address;
4102 struct TransportPingMessage ping;
4103 uint16_t hello_size;
4106 struct GNUNET_PeerIdentity id;
4109 GNUNET_CRYPTO_hash (&va->publicKey,
4110 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4112 if (neighbour == NULL)
4114 /* FIXME: stats... */
4115 GNUNET_break (GNUNET_OK ==
4116 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4119 abort_validation (NULL, NULL, va);
4122 neighbour->publicKey = va->publicKey;
4123 neighbour->public_key_valid = GNUNET_YES;
4124 peer_address = add_peer_address (neighbour,
4125 va->transport_name, NULL,
4126 (const void*) &va[1],
4128 if (peer_address == NULL)
4130 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4131 "Failed to add peer `%4s' for plugin `%s'\n",
4132 GNUNET_i2s (&neighbour->id),
4133 va->transport_name);
4134 GNUNET_break (GNUNET_OK ==
4135 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4138 abort_validation (NULL, NULL, va);
4141 hello_size = GNUNET_HELLO_size(our_hello);
4142 slen = strlen(va->transport_name) + 1;
4143 tsize = sizeof(struct TransportPingMessage) + hello_size + va->addrlen + slen;
4144 message_buf = GNUNET_malloc(tsize);
4145 ping.challenge = htonl(va->challenge);
4146 ping.header.size = htons(sizeof(struct TransportPingMessage) + slen + va->addrlen);
4147 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
4148 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
4149 memcpy(message_buf, our_hello, hello_size);
4150 memcpy(&message_buf[hello_size],
4152 sizeof(struct TransportPingMessage));
4153 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
4156 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
4160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4161 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
4164 : a2s (va->transport_name,
4165 (const void*) &va[1], va->addrlen),
4167 GNUNET_i2s (&neighbour->id),
4168 "HELLO", hello_size,
4169 "PING", sizeof (struct TransportPingMessage) + va->addrlen + slen);
4172 GNUNET_STATISTICS_update (stats,
4173 gettext_noop ("# PING messages sent for initial validation"),
4176 transmit_to_peer (NULL, peer_address,
4177 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
4178 HELLO_VERIFICATION_TIMEOUT,
4180 GNUNET_YES, neighbour);
4181 GNUNET_free(message_buf);
4186 * Check if the given address is already being validated; if not,
4187 * append the given address to the list of entries that are being be
4188 * validated and initiate validation.
4190 * @param cls closure ('struct CheckHelloValidatedContext *')
4191 * @param tname name of the transport
4192 * @param expiration expiration time
4193 * @param addr the address
4194 * @param addrlen length of the address
4195 * @return GNUNET_OK (always)
4198 run_validation (void *cls,
4200 struct GNUNET_TIME_Absolute expiration,
4204 struct CheckHelloValidatedContext *chvc = cls;
4205 struct GNUNET_PeerIdentity id;
4206 struct TransportPlugin *tp;
4207 struct ValidationEntry *va;
4208 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4209 struct CheckAddressExistsClosure caec;
4210 struct OwnAddressList *oal;
4212 GNUNET_assert (addr != NULL);
4214 GNUNET_STATISTICS_update (stats,
4215 gettext_noop ("# peer addresses scheduled for validation"),
4218 tp = find_transport (tname);
4221 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
4222 GNUNET_ERROR_TYPE_BULK,
4224 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
4226 GNUNET_STATISTICS_update (stats,
4227 gettext_noop ("# peer addresses not validated (plugin not available)"),
4232 /* check if this is one of our own addresses */
4233 oal = tp->addresses;
4236 if ( (oal->addrlen == addrlen) &&
4237 (0 == memcmp (&oal[1],
4241 /* not plausible, this address is equivalent to our own address! */
4242 GNUNET_STATISTICS_update (stats,
4243 gettext_noop ("# peer addresses not validated (loopback)"),
4250 GNUNET_HELLO_get_key (chvc->hello, &pk);
4251 GNUNET_CRYPTO_hash (&pk,
4253 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4256 if (is_blacklisted(&id, tp))
4259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4260 "Attempted to validate blacklisted peer `%s' using `%s'!\n",
4268 caec.addrlen = addrlen;
4269 caec.session = NULL;
4271 caec.exists = GNUNET_NO;
4272 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
4273 &check_address_exists,
4275 if (caec.exists == GNUNET_YES)
4277 /* During validation attempts we will likely trigger the other
4278 peer trying to validate our address which in turn will cause
4279 it to send us its HELLO, so we expect to hit this case rather
4280 frequently. Only print something if we are very verbose. */
4281 #if DEBUG_TRANSPORT > 1
4282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4283 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
4284 a2s (tname, addr, addrlen),
4288 GNUNET_STATISTICS_update (stats,
4289 gettext_noop ("# peer addresses not validated (in progress)"),
4294 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
4297 va->transport_name = GNUNET_strdup (tname);
4298 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
4300 va->send_time = GNUNET_TIME_absolute_get();
4301 va->addr = (const void*) &va[1];
4302 memcpy (&va[1], addr, addrlen);
4303 va->addrlen = addrlen;
4304 GNUNET_HELLO_get_key (chvc->hello,
4306 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
4307 &timeout_hello_validation,
4309 GNUNET_CONTAINER_multihashmap_put (validation_map,
4312 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4313 setup_peer_check_blacklist (&id, GNUNET_NO,
4314 &transmit_hello_and_ping,
4321 * Check if addresses in validated hello "h" overlap with
4322 * those in "chvc->hello" and validate the rest.
4324 * @param cls closure
4325 * @param peer id of the peer, NULL for last call
4326 * @param h hello message for the peer (can be NULL)
4327 * @param err_msg NULL if successful, otherwise contains error message
4330 check_hello_validated (void *cls,
4331 const struct GNUNET_PeerIdentity *peer,
4332 const struct GNUNET_HELLO_Message *h,
4333 const char *err_msg)
4335 struct CheckHelloValidatedContext *chvc = cls;
4336 struct GNUNET_HELLO_Message *plain_hello;
4337 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4338 struct GNUNET_PeerIdentity target;
4339 struct NeighbourList *n;
4341 if (err_msg != NULL)
4343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4344 _("Error in communication with PEERINFO service\n"));
4350 GNUNET_STATISTICS_update (stats,
4351 gettext_noop ("# outstanding peerinfo iterate requests"),
4355 if (GNUNET_NO == chvc->hello_known)
4357 /* notify PEERINFO about the peer now, so that we at least
4358 have the public key if some other component needs it */
4359 GNUNET_HELLO_get_key (chvc->hello, &pk);
4360 GNUNET_CRYPTO_hash (&pk,
4361 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4362 &target.hashPubKey);
4363 plain_hello = GNUNET_HELLO_create (&pk,
4366 GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
4367 GNUNET_free (plain_hello);
4368 #if DEBUG_TRANSPORT_HELLO
4369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4370 "PEERINFO had no `%s' message for peer `%4s', full validation needed.\n",
4372 GNUNET_i2s (&target));
4374 GNUNET_STATISTICS_update (stats,
4375 gettext_noop ("# new HELLOs requiring full validation"),
4378 GNUNET_HELLO_iterate_addresses (chvc->hello,
4385 GNUNET_STATISTICS_update (stats,
4386 gettext_noop ("# duplicate HELLO (peer known)"),
4391 if (chvc->ve_count == 0)
4393 GNUNET_CONTAINER_DLL_remove (chvc_head,
4402 #if DEBUG_TRANSPORT_HELLO
4403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4404 "PEERINFO had `%s' message for peer `%4s', validating only new addresses.\n",
4408 chvc->hello_known = GNUNET_YES;
4409 n = find_neighbour (peer);
4412 #if DEBUG_TRANSPORT_HELLO
4413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4414 "Calling hello_iterate_addresses for %s!\n",
4417 GNUNET_HELLO_iterate_addresses (h,
4419 &add_to_foreign_address_list,
4421 try_transmission_to_peer (n);
4425 #if DEBUG_TRANSPORT_HELLO
4426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4427 "No existing neighbor record for %s!\n",
4430 GNUNET_STATISTICS_update (stats,
4431 gettext_noop ("# no existing neighbour record (validating HELLO)"),
4435 GNUNET_STATISTICS_update (stats,
4436 gettext_noop ("# HELLO validations (update case)"),
4439 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
4441 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
4448 * Process HELLO-message.
4450 * @param plugin transport involved, may be NULL
4451 * @param message the actual message
4452 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
4455 process_hello (struct TransportPlugin *plugin,
4456 const struct GNUNET_MessageHeader *message)
4459 struct GNUNET_PeerIdentity target;
4460 const struct GNUNET_HELLO_Message *hello;
4461 struct CheckHelloValidatedContext *chvc;
4462 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
4463 #if DEBUG_TRANSPORT_HELLO > 2
4466 hsize = ntohs (message->size);
4467 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
4468 (hsize < sizeof (struct GNUNET_MessageHeader)))
4471 return GNUNET_SYSERR;
4473 GNUNET_STATISTICS_update (stats,
4474 gettext_noop ("# HELLOs received for validation"),
4478 /* first, check if load is too high */
4479 if (GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
4481 GNUNET_STATISTICS_update (stats,
4482 gettext_noop ("# HELLOs ignored due to high load"),
4485 #if DEBUG_TRANSPORT_HELLO
4486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4487 "Ignoring `%s' for `%4s', load too high.\n",
4489 GNUNET_i2s (&target));
4493 hello = (const struct GNUNET_HELLO_Message *) message;
4494 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
4496 #if DEBUG_TRANSPORT_HELLO
4497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4498 "Unable to get public key from `%s' for `%4s'!\n",
4500 GNUNET_i2s (&target));
4502 GNUNET_break_op (0);
4503 return GNUNET_SYSERR;
4506 GNUNET_CRYPTO_hash (&publicKey,
4507 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4508 &target.hashPubKey);
4510 #if DEBUG_TRANSPORT_HELLO
4511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4512 "Received `%s' message for `%4s'\n",
4514 GNUNET_i2s (&target));
4517 if (0 == memcmp (&my_identity,
4519 sizeof (struct GNUNET_PeerIdentity)))
4521 GNUNET_STATISTICS_update (stats,
4522 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
4528 while (NULL != chvc)
4530 if (GNUNET_HELLO_equals (hello,
4532 GNUNET_TIME_absolute_get ()).abs_value > 0)
4534 #if DEBUG_TRANSPORT_HELLO > 2
4535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4536 "Received duplicate `%s' message for `%4s'; ignored\n",
4538 GNUNET_i2s (&target));
4540 return GNUNET_OK; /* validation already pending */
4542 if (GNUNET_HELLO_size(hello) == GNUNET_HELLO_size (chvc->hello))
4543 GNUNET_break (0 != memcmp (hello, chvc->hello,
4544 GNUNET_HELLO_size(hello)));
4549 struct NeighbourList *temp_neighbor = find_neighbour(&target);
4550 if ((NULL != temp_neighbor))
4552 fprintf(stderr, "Already know peer, ignoring hello\n");
4557 #if DEBUG_TRANSPORT_HELLO > 2
4560 my_id = GNUNET_strdup(GNUNET_i2s(plugin->env.my_identity));
4561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4562 "%s: Starting validation of `%s' message for `%4s' via '%s' of size %u\n",
4565 GNUNET_i2s (&target),
4567 GNUNET_HELLO_size(hello));
4571 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
4573 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
4574 memcpy (&chvc[1], hello, hsize);
4575 GNUNET_CONTAINER_DLL_insert (chvc_head,
4578 /* finally, check if HELLO was previously validated
4579 (continuation will then schedule actual validation) */
4580 GNUNET_STATISTICS_update (stats,
4581 gettext_noop ("# peerinfo process hello iterate requests"),
4584 GNUNET_STATISTICS_update (stats,
4585 gettext_noop ("# outstanding peerinfo iterate requests"),
4588 chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
4590 HELLO_VERIFICATION_TIMEOUT,
4591 &check_hello_validated, chvc);
4597 * The peer specified by the given neighbour has timed-out or a plugin
4598 * has disconnected. We may either need to do nothing (other plugins
4599 * still up), or trigger a full disconnect and clean up. This
4600 * function updates our state and does the necessary notifications.
4601 * Also notifies our clients that the neighbour is now officially
4604 * @param n the neighbour list entry for the peer
4605 * @param check GNUNET_YES to check if ALL addresses for this peer
4606 * are gone, GNUNET_NO to force a disconnect of the peer
4607 * regardless of whether other addresses exist.
4610 disconnect_neighbour (struct NeighbourList *n, int check)
4612 struct ReadyList *rpos;
4613 struct NeighbourList *npos;
4614 struct NeighbourList *nprev;
4615 struct MessageQueue *mq;
4616 struct ForeignAddressList *peer_addresses;
4617 struct ForeignAddressList *peer_pos;
4619 if (GNUNET_YES == check)
4622 while (NULL != rpos)
4624 peer_addresses = rpos->addresses;
4625 while (peer_addresses != NULL)
4627 if (GNUNET_YES == peer_addresses->connected)
4629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4630 "NOT Disconnecting from `%4s', still have live addresses!\n",
4631 GNUNET_i2s (&n->id));
4632 return; /* still connected */
4634 peer_addresses = peer_addresses->next;
4640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4641 "Disconnecting from `%4s'\n",
4642 GNUNET_i2s (&n->id));
4644 /* remove n from neighbours list */
4647 while ((npos != NULL) && (npos != n))
4652 GNUNET_assert (npos != NULL);
4654 neighbours = n->next;
4656 nprev->next = n->next;
4658 /* notify all clients about disconnect */
4659 if (GNUNET_YES == n->received_pong)
4660 notify_clients_disconnect (&n->id);
4662 /* clean up all plugins, cancel connections and pending transmissions */
4663 while (NULL != (rpos = n->plugins))
4665 n->plugins = rpos->next;
4666 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
4667 while (rpos->addresses != NULL)
4669 peer_pos = rpos->addresses;
4670 rpos->addresses = peer_pos->next;
4671 if (peer_pos->connected == GNUNET_YES)
4672 GNUNET_STATISTICS_update (stats,
4673 gettext_noop ("# connected addresses"),
4676 if (GNUNET_YES == peer_pos->validated)
4677 GNUNET_STATISTICS_update (stats,
4678 gettext_noop ("# peer addresses considered valid"),
4681 if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
4683 GNUNET_SCHEDULER_cancel (peer_pos->revalidate_task);
4684 peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
4686 GNUNET_free(peer_pos->ressources);
4687 GNUNET_free(peer_pos->quality);
4688 GNUNET_free(peer_pos);
4693 /* free all messages on the queue */
4694 while (NULL != (mq = n->messages_head))
4696 GNUNET_STATISTICS_update (stats,
4697 gettext_noop ("# bytes in message queue for other peers"),
4698 - (int64_t) mq->message_buf_size,
4700 GNUNET_STATISTICS_update (stats,
4701 gettext_noop ("# bytes discarded due to disconnect"),
4702 mq->message_buf_size,
4704 GNUNET_CONTAINER_DLL_remove (n->messages_head,
4707 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4709 sizeof(struct GNUNET_PeerIdentity)));
4712 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
4714 GNUNET_SCHEDULER_cancel (n->timeout_task);
4715 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4717 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4719 GNUNET_SCHEDULER_cancel (n->retry_task);
4720 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4722 if (n->piter != NULL)
4724 GNUNET_PEERINFO_iterate_cancel (n->piter);
4725 GNUNET_STATISTICS_update (stats,
4726 gettext_noop ("# outstanding peerinfo iterate requests"),
4731 /* finally, free n itself */
4732 GNUNET_STATISTICS_update (stats,
4733 gettext_noop ("# active neighbours"),
4736 GNUNET_free_non_null (n->pre_connect_message_buffer);
4742 * We have received a PING message from someone. Need to send a PONG message
4743 * in response to the peer by any means necessary.
4746 handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
4747 const struct GNUNET_PeerIdentity *peer,
4748 struct Session *session,
4749 const char *sender_address,
4750 uint16_t sender_address_len)
4752 struct TransportPlugin *plugin = cls;
4753 struct SessionHeader *session_header = (struct SessionHeader*) session;
4754 struct TransportPingMessage *ping;
4755 struct TransportPongMessage *pong;
4756 struct NeighbourList *n;
4757 struct ReadyList *rl;
4758 struct ForeignAddressList *fal;
4759 struct OwnAddressList *oal;
4764 if (ntohs (message->size) < sizeof (struct TransportPingMessage))
4766 GNUNET_break_op (0);
4767 return GNUNET_SYSERR;
4770 ping = (struct TransportPingMessage *) message;
4771 if (0 != memcmp (&ping->target,
4772 plugin->env.my_identity,
4773 sizeof (struct GNUNET_PeerIdentity)))
4775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4776 _("Received `%s' message from `%s' destined for `%s' which is not me!\n"),
4778 (sender_address != NULL)
4779 ? a2s (plugin->short_name,
4780 (const struct sockaddr *)sender_address,
4783 GNUNET_i2s (&ping->target));
4784 return GNUNET_SYSERR;
4787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4788 "Processing `%s' from `%s'\n",
4790 (sender_address != NULL)
4791 ? a2s (plugin->short_name,
4792 (const struct sockaddr *)sender_address,
4796 GNUNET_STATISTICS_update (stats,
4797 gettext_noop ("# PING messages received"),
4800 addr = (const char*) &ping[1];
4801 alen = ntohs (message->size) - sizeof (struct TransportPingMessage);
4802 slen = strlen (plugin->short_name) + 1;
4805 /* peer wants to confirm that we have an outbound connection to him */
4806 if (session == NULL)
4808 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4809 _("Refusing to create PONG since I do not have a session with `%s'.\n"),
4811 return GNUNET_SYSERR;
4813 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len + slen);
4814 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len + slen);
4815 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
4816 pong->purpose.size =
4817 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4819 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
4820 sizeof (struct GNUNET_PeerIdentity) + sender_address_len + slen);
4821 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING);
4822 pong->challenge = ping->challenge;
4823 pong->addrlen = htonl(sender_address_len + slen);
4826 sizeof(struct GNUNET_PeerIdentity));
4830 if ((sender_address!=NULL) && (sender_address_len > 0))
4831 memcpy (&((char*)&pong[1])[slen],
4833 sender_address_len);
4834 if (GNUNET_TIME_absolute_get_remaining (session_header->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
4836 /* create / update cached sig */
4838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4839 "Creating PONG signature to indicate active connection.\n");
4841 session_header->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
4842 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
4843 GNUNET_assert (GNUNET_OK ==
4844 GNUNET_CRYPTO_rsa_sign (my_private_key,
4846 &session_header->pong_signature));
4850 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
4852 memcpy (&pong->signature,
4853 &session_header->pong_signature,
4854 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4860 /* peer wants to confirm that this is one of our addresses */
4864 plugin->api->check_address (plugin->api->cls,
4868 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4869 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
4870 a2s (plugin->short_name,
4875 oal = plugin->addresses;
4878 if ( (oal->addrlen == alen) &&
4885 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
4886 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
4887 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
4888 pong->purpose.size =
4889 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4891 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
4892 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
4893 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
4894 pong->challenge = ping->challenge;
4895 pong->addrlen = htonl(alen + slen);
4898 sizeof(struct GNUNET_PeerIdentity));
4899 memcpy (&pong[1], plugin->short_name, slen);
4900 memcpy (&((char*)&pong[1])[slen], addr, alen);
4901 if ( (oal != NULL) &&
4902 (GNUNET_TIME_absolute_get_remaining (oal->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4) )
4904 /* create / update cached sig */
4906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4907 "Creating PONG signature to indicate ownership.\n");
4909 oal->pong_sig_expires = GNUNET_TIME_absolute_min (oal->expires,
4910 GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
4911 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
4912 GNUNET_assert (GNUNET_OK ==
4913 GNUNET_CRYPTO_rsa_sign (my_private_key,
4915 &oal->pong_signature));
4916 memcpy (&pong->signature,
4917 &oal->pong_signature,
4918 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4920 else if (oal == NULL)
4922 /* not using cache (typically DV-only) */
4923 pong->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
4924 GNUNET_assert (GNUNET_OK ==
4925 GNUNET_CRYPTO_rsa_sign (my_private_key,
4931 /* can used cached version */
4932 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
4933 memcpy (&pong->signature,
4934 &oal->pong_signature,
4935 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4938 n = find_neighbour(peer);
4939 GNUNET_assert (n != NULL);
4940 /* first try reliable response transmission */
4944 fal = rl->addresses;
4947 if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
4950 ntohs (pong->header.size),
4951 TRANSPORT_PONG_PRIORITY,
4952 HELLO_VERIFICATION_TIMEOUT,
4960 GNUNET_STATISTICS_update (stats,
4961 gettext_noop ("# PONGs unicast via reliable transport"),
4971 /* no reliable method found, do multicast */
4972 GNUNET_STATISTICS_update (stats,
4973 gettext_noop ("# PONGs multicast to all available addresses"),
4979 fal = rl->addresses;
4982 transmit_to_peer(NULL, fal,
4983 TRANSPORT_PONG_PRIORITY,
4984 HELLO_VERIFICATION_TIMEOUT,
4986 ntohs(pong->header.size),
5002 * Function called by the plugin for each received message.
5003 * Update data volumes, possibly notify plugins about
5004 * reducing the rate at which they read from the socket
5005 * and generally forward to our receive callback.
5007 * @param cls the "struct TransportPlugin *" we gave to the plugin
5008 * @param peer (claimed) identity of the other peer
5009 * @param message the message, NULL if we only care about
5010 * learning about the delay until we should receive again
5011 * @param ats_data information for automatic transport selection
5012 * @param ats_count number of elements in ats not including 0-terminator
5013 * @param session identifier used for this session (can be NULL)
5014 * @param sender_address binary address of the sender (if observed)
5015 * @param sender_address_len number of bytes in sender_address
5016 * @return how long in ms the plugin should wait until receiving more data
5017 * (plugins that do not support this, can ignore the return value)
5019 static struct GNUNET_TIME_Relative
5020 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
5021 const struct GNUNET_MessageHeader *message,
5022 const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
5024 struct Session *session,
5025 const char *sender_address,
5026 uint16_t sender_address_len)
5028 struct TransportPlugin *plugin = cls;
5029 struct ReadyList *service_context;
5030 struct ForeignAddressList *peer_address;
5032 struct NeighbourList *n;
5033 struct GNUNET_TIME_Relative ret;
5034 if (is_blacklisted (peer, plugin))
5035 return GNUNET_TIME_UNIT_FOREVER_REL;
5039 n = find_neighbour (peer);
5041 n = setup_new_neighbour (peer, GNUNET_YES);
5042 service_context = n->plugins;
5043 while ((service_context != NULL) && (plugin != service_context->plugin))
5044 service_context = service_context->next;
5045 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
5046 peer_address = NULL;
5049 for (c=0; c<ats_count; c++)
5051 if (ntohl(ats_data[c].type) == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
5053 distance = ntohl(ats_data[c].value);
5056 /* notify ATS about incoming data */
5057 //ats_notify_ats_data(peer, ats_data);
5059 if (message != NULL)
5061 if ( (session != NULL) ||
5062 (sender_address != NULL) )
5063 peer_address = add_peer_address (n,
5067 sender_address_len);
5068 if (peer_address != NULL)
5071 update_addr_ats(peer_address, ats_data, ats_count);
5072 update_addr_value(peer_address, distance, GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5074 peer_address->distance = distance;
5075 if (GNUNET_YES == peer_address->validated)
5076 mark_address_connected (peer_address);
5077 peer_address->timeout
5079 GNUNET_TIME_relative_to_absolute
5080 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5081 schedule_next_ping (peer_address);
5083 /* update traffic received amount ... */
5084 msize = ntohs (message->size);
5085 GNUNET_STATISTICS_update (stats,
5086 gettext_noop ("# bytes received from other peers"),
5089 n->distance = distance;
5091 GNUNET_TIME_relative_to_absolute
5092 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5093 GNUNET_SCHEDULER_cancel (n->timeout_task);
5095 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
5096 &neighbour_timeout_task, n);
5097 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
5099 /* dropping message due to frequent inbound volume violations! */
5100 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
5101 GNUNET_ERROR_TYPE_BULK,
5103 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
5104 n->in_tracker.available_bytes_per_s__,
5105 n->quota_violation_count);
5106 GNUNET_STATISTICS_update (stats,
5107 gettext_noop ("# bandwidth quota violations by other peers"),
5110 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
5114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5115 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
5116 ntohs (message->type),
5117 ntohs (message->size),
5120 switch (ntohs (message->type))
5122 case GNUNET_MESSAGE_TYPE_HELLO:
5123 GNUNET_STATISTICS_update (stats,
5124 gettext_noop ("# HELLO messages received from other peers"),
5127 process_hello (plugin, message);
5129 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
5130 handle_ping (plugin, message, peer, session, sender_address, sender_address_len);
5132 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
5133 handle_pong (plugin, message, peer, sender_address, sender_address_len);
5136 handle_payload_message (message, n);
5140 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
5141 if (ret.rel_value > 0)
5143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5144 "Throttling read (%llu bytes excess at %u b/s), waiting %llums before reading more.\n",
5145 (unsigned long long) n->in_tracker.consumption_since_last_update__,
5146 (unsigned int) n->in_tracker.available_bytes_per_s__,
5147 (unsigned long long) ret.rel_value);
5148 GNUNET_STATISTICS_update (stats,
5149 gettext_noop ("# ms throttling suggested"),
5150 (int64_t) ret.rel_value,
5157 * Handle START-message. This is the first message sent to us
5158 * by any client which causes us to add it to our list.
5160 * @param cls closure (always NULL)
5161 * @param client identification of the client
5162 * @param message the actual message
5165 handle_start (void *cls,
5166 struct GNUNET_SERVER_Client *client,
5167 const struct GNUNET_MessageHeader *message)
5169 const struct StartMessage *start;
5170 struct TransportClient *c;
5171 struct ConnectInfoMessage * cim;
5172 struct NeighbourList *n;
5176 start = (const struct StartMessage*) message;
5178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5179 "Received `%s' request from client\n", "START");
5184 if (c->client == client)
5186 /* client already on our list! */
5188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5193 if ( (GNUNET_NO != ntohl (start->do_check)) &&
5194 (0 != memcmp (&start->self,
5196 sizeof (struct GNUNET_PeerIdentity))) )
5198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5199 _("Rejecting control connection from peer `%s', which is not me!\n"),
5200 GNUNET_i2s (&start->self));
5201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5204 c = GNUNET_malloc (sizeof (struct TransportClient));
5208 if (our_hello != NULL)
5211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5212 "Sending our own `%s' to new client\n", "HELLO");
5214 transmit_to_client (c,
5215 (const struct GNUNET_MessageHeader *) our_hello,
5217 /* tell new client about all existing connections */
5219 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
5220 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
5224 cim = GNUNET_malloc (size);
5225 cim->header.size = htons (size);
5226 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
5227 cim->ats_count = htonl(ats_count);
5228 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
5229 (&(cim->ats))[2].value = htonl (0);
5233 if (GNUNET_YES == n->received_pong)
5235 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5236 (&(cim->ats))[0].value = htonl (n->distance);
5237 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
5238 (&(cim->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
5240 transmit_to_client (c, &cim->header, GNUNET_NO);
5246 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5251 * Handle HELLO-message.
5253 * @param cls closure (always NULL)
5254 * @param client identification of the client
5255 * @param message the actual message
5258 handle_hello (void *cls,
5259 struct GNUNET_SERVER_Client *client,
5260 const struct GNUNET_MessageHeader *message)
5264 GNUNET_STATISTICS_update (stats,
5265 gettext_noop ("# HELLOs received from clients"),
5268 ret = process_hello (NULL, message);
5269 GNUNET_SERVER_receive_done (client, ret);
5274 * Closure for 'transmit_client_message'; followed by
5275 * 'msize' bytes of the actual message.
5277 struct TransmitClientMessageContext
5280 * Client on whom's behalf we are sending.
5282 struct GNUNET_SERVER_Client *client;
5285 * Timeout for the transmission.
5287 struct GNUNET_TIME_Absolute timeout;
5295 * Size of the message in bytes.
5302 * Schedule transmission of a message we got from a client to a peer.
5304 * @param cls the 'struct TransmitClientMessageContext*'
5305 * @param n destination, or NULL on error (in that case, drop the message)
5308 transmit_client_message (void *cls,
5309 struct NeighbourList *n)
5311 struct TransmitClientMessageContext *tcmc = cls;
5312 struct TransportClient *tc;
5315 while ((tc != NULL) && (tc->client != tcmc->client))
5320 transmit_to_peer (tc, NULL, tcmc->priority,
5321 GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
5323 tcmc->msize, GNUNET_NO, n);
5325 GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
5326 GNUNET_SERVER_client_drop (tcmc->client);
5332 * Handle SEND-message.
5334 * @param cls closure (always NULL)
5335 * @param client identification of the client
5336 * @param message the actual message
5339 handle_send (void *cls,
5340 struct GNUNET_SERVER_Client *client,
5341 const struct GNUNET_MessageHeader *message)
5343 const struct OutboundMessage *obm;
5344 const struct GNUNET_MessageHeader *obmm;
5345 struct TransmitClientMessageContext *tcmc;
5349 size = ntohs (message->size);
5351 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
5354 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5357 GNUNET_STATISTICS_update (stats,
5358 gettext_noop ("# payload received for other peers"),
5361 obm = (const struct OutboundMessage *) message;
5362 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
5363 msize = size - sizeof (struct OutboundMessage);
5365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5366 "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
5367 "SEND", GNUNET_i2s (&obm->peer),
5371 tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
5372 tcmc->client = client;
5373 tcmc->priority = ntohl (obm->priority);
5374 tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
5375 tcmc->msize = msize;
5376 /* FIXME: this memcpy can be up to 7% of our total runtime */
5377 memcpy (&tcmc[1], obmm, msize);
5378 GNUNET_SERVER_client_keep (client);
5379 setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
5380 &transmit_client_message,
5386 * Handle request connect message
5388 * @param cls closure (always NULL)
5389 * @param client identification of the client
5390 * @param message the actual message
5393 handle_request_connect (void *cls,
5394 struct GNUNET_SERVER_Client *client,
5395 const struct GNUNET_MessageHeader *message)
5397 const struct TransportRequestConnectMessage *trcm =
5398 (const struct TransportRequestConnectMessage *) message;
5400 GNUNET_STATISTICS_update (stats,
5401 gettext_noop ("# REQUEST CONNECT messages received"),
5404 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a request connect message for peer %s\n", GNUNET_i2s(&trcm->peer));
5405 setup_peer_check_blacklist (&trcm->peer, GNUNET_YES,
5407 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5411 * Handle SET_QUOTA-message.
5413 * @param cls closure (always NULL)
5414 * @param client identification of the client
5415 * @param message the actual message
5418 handle_set_quota (void *cls,
5419 struct GNUNET_SERVER_Client *client,
5420 const struct GNUNET_MessageHeader *message)
5422 const struct QuotaSetMessage *qsm =
5423 (const struct QuotaSetMessage *) message;
5424 struct NeighbourList *n;
5426 GNUNET_STATISTICS_update (stats,
5427 gettext_noop ("# SET QUOTA messages received"),
5430 n = find_neighbour (&qsm->peer);
5433 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5434 GNUNET_STATISTICS_update (stats,
5435 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
5441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5442 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
5444 (unsigned int) ntohl (qsm->quota.value__),
5445 (unsigned int) n->in_tracker.available_bytes_per_s__,
5446 GNUNET_i2s (&qsm->peer));
5448 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
5450 if (0 == ntohl (qsm->quota.value__))
5452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5453 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&n->id),
5455 disconnect_neighbour (n, GNUNET_NO);
5457 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5462 * Take the given address and append it to the set of results sent back to
5465 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
5466 * @param address the resolved name, NULL to indicate the last response
5469 transmit_address_to_client (void *cls, const char *address)
5471 struct GNUNET_SERVER_TransmitContext *tc = cls;
5474 if (NULL == address)
5477 slen = strlen (address) + 1;
5479 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
5480 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5481 if (NULL == address)
5482 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
5487 * Handle AddressLookup-message.
5489 * @param cls closure (always NULL)
5490 * @param client identification of the client
5491 * @param message the actual message
5494 handle_address_lookup (void *cls,
5495 struct GNUNET_SERVER_Client *client,
5496 const struct GNUNET_MessageHeader *message)
5498 const struct AddressLookupMessage *alum;
5499 struct TransportPlugin *lsPlugin;
5500 const char *nameTransport;
5501 const char *address;
5503 struct GNUNET_SERVER_TransmitContext *tc;
5504 struct GNUNET_TIME_Absolute timeout;
5505 struct GNUNET_TIME_Relative rtimeout;
5508 size = ntohs (message->size);
5509 if (size < sizeof (struct AddressLookupMessage))
5511 GNUNET_break_op (0);
5512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5515 alum = (const struct AddressLookupMessage *) message;
5516 uint32_t addressLen = ntohl (alum->addrlen);
5517 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
5519 GNUNET_break_op (0);
5520 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5523 address = (const char *) &alum[1];
5524 nameTransport = (const char *) &address[addressLen];
5526 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
5528 GNUNET_break_op (0);
5529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5532 timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
5533 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5534 numeric = ntohl (alum->numeric_only);
5535 lsPlugin = find_transport (nameTransport);
5536 if (NULL == lsPlugin)
5538 tc = GNUNET_SERVER_transmit_context_create (client);
5539 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5540 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5541 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
5544 tc = GNUNET_SERVER_transmit_context_create (client);
5545 lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
5547 address, addressLen,
5550 &transmit_address_to_client, tc);
5555 * Setup the environment for this plugin.
5558 create_environment (struct TransportPlugin *plug)
5560 plug->env.cfg = cfg;
5561 plug->env.my_identity = &my_identity;
5562 plug->env.our_hello = &our_hello;
5563 plug->env.cls = plug;
5564 plug->env.receive = &plugin_env_receive;
5565 plug->env.notify_address = &plugin_env_notify_address;
5566 plug->env.session_end = &plugin_env_session_end;
5567 plug->env.max_connections = max_connect_per_transport;
5568 plug->env.stats = stats;
5573 * Start the specified transport (load the plugin).
5576 start_transport (struct GNUNET_SERVER_Handle *server,
5579 struct TransportPlugin *plug;
5582 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5583 _("Loading `%s' transport plugin\n"), name);
5584 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
5585 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
5586 create_environment (plug);
5587 plug->short_name = GNUNET_strdup (name);
5588 plug->lib_name = libname;
5589 plug->next = plugins;
5591 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
5592 if (plug->api == NULL)
5594 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5595 _("Failed to load transport plugin for `%s'\n"), name);
5596 GNUNET_free (plug->short_name);
5597 plugins = plug->next;
5598 GNUNET_free (libname);
5605 * Called whenever a client is disconnected. Frees our
5606 * resources associated with that client.
5608 * @param cls closure
5609 * @param client identification of the client
5612 client_disconnect_notification (void *cls,
5613 struct GNUNET_SERVER_Client *client)
5615 struct TransportClient *pos;
5616 struct TransportClient *prev;
5617 struct ClientMessageQueueEntry *mqe;
5618 struct Blacklisters *bl;
5619 struct BlacklistCheck *bc;
5624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
5625 "Client disconnected, cleaning up.\n");
5627 /* clean up blacklister */
5631 if (bl->client == client)
5636 if (bc->bl_pos == bl)
5638 bc->bl_pos = bl->next;
5641 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
5644 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
5645 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
5651 GNUNET_CONTAINER_DLL_remove (bl_head,
5654 GNUNET_SERVER_client_drop (bl->client);
5660 /* clean up 'normal' clients */
5663 while ((pos != NULL) && (pos->client != client))
5670 while (NULL != (mqe = pos->message_queue_head))
5672 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
5673 pos->message_queue_tail,
5675 pos->message_count--;
5679 clients = pos->next;
5681 prev->next = pos->next;
5682 if (GNUNET_YES == pos->tcs_pending)
5687 if (pos->th != NULL)
5689 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
5692 GNUNET_break (0 == pos->message_count);
5698 * Function called when the service shuts down. Unloads our plugins
5699 * and cancels pending validations.
5701 * @param cls closure, unused
5702 * @param tc task context (unused)
5705 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5707 struct TransportPlugin *plug;
5708 struct OwnAddressList *al;
5709 struct CheckHelloValidatedContext *chvc;
5711 while (neighbours != NULL)
5713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5714 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&neighbours->id),
5716 disconnect_neighbour (neighbours, GNUNET_NO);
5719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5720 "Transport service is unloading plugins...\n");
5722 while (NULL != (plug = plugins))
5724 plugins = plug->next;
5725 if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
5727 GNUNET_SCHEDULER_cancel (plug->address_update_task);
5728 plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
5730 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
5731 GNUNET_free (plug->lib_name);
5732 GNUNET_free (plug->short_name);
5733 while (NULL != (al = plug->addresses))
5735 plug->addresses = al->next;
5740 if (my_private_key != NULL)
5741 GNUNET_CRYPTO_rsa_key_free (my_private_key);
5742 GNUNET_free_non_null (our_hello);
5744 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
5747 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
5748 validation_map = NULL;
5752 /* free 'chvc' data structure */
5753 while (NULL != (chvc = chvc_head))
5755 chvc_head = chvc->next;
5756 if (chvc->piter != NULL)
5758 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
5759 GNUNET_STATISTICS_update (stats,
5760 gettext_noop ("# outstanding peerinfo iterate requests"),
5766 GNUNET_assert (chvc->ve_count == 0);
5773 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
5776 if (peerinfo != NULL)
5778 GNUNET_PEERINFO_disconnect (peerinfo);
5781 /* Can we assume those are gone by now, or do we need to clean up
5783 GNUNET_break (bl_head == NULL);
5784 GNUNET_break (bc_head == NULL);
5788 static int ats_evaluate_results (int result, int solution, char * problem)
5790 int cont = GNUNET_NO;
5791 int error_kind = GNUNET_ERROR_TYPE_DEBUG;
5793 error_kind = GNUNET_ERROR_TYPE_ERROR;
5797 case GLP_ESTOP : /* search terminated by application */
5798 GNUNET_log (error_kind, "%s , Search terminated by application ", problem);
5800 case GLP_EITLIM : /* iteration limit exceeded */
5801 GNUNET_log (error_kind, "%s Iteration limit exceeded ", problem);
5804 case GLP_ETMLIM : /* time limit exceeded */
5805 GNUNET_log (error_kind, "%s Time limit exceeded ", problem);
5807 case GLP_ENOPFS : /* no primal feasible solution */
5808 case GLP_ENODFS : /* no dual feasible solution */
5809 GNUNET_log (error_kind, "%s No feasible solution", problem);
5812 case GLP_EBADB : /* invalid basis */
5813 case GLP_ESING : /* singular matrix */
5814 case GLP_ECOND : /* ill-conditioned matrix */
5815 case GLP_EBOUND : /* invalid bounds */
5816 case GLP_EFAIL : /* solver failed */
5817 case GLP_EOBJLL : /* objective lower limit reached */
5818 case GLP_EOBJUL : /* objective upper limit reached */
5819 case GLP_EROOT : /* root LP optimum not provided */
5820 GNUNET_log (error_kind, "%s Invalid Input data: %i\n", problem, result);
5824 GNUNET_log (error_kind, "%s Problem has been solved\n", problem);
5830 GNUNET_log (error_kind, "%s solution is undefined\n", problem);
5833 GNUNET_log (error_kind, "%s solution is optimal\n", problem);
5837 GNUNET_log (error_kind, "%s solution is integer feasible, however, its optimality (or non-optimality) has not been proven, \n", problem);
5841 GNUNET_log (error_kind, "%s problem has no integer feasible solution\n", problem);
5851 /** solve the bandwidth distribution problem
5852 * @param max_it maximum iterations
5853 * @param max_dur maximum duration in ms
5854 * @param D weight for diversity
5855 * @param U weight for utility
5856 * @param R weight for relativity
5857 * @param v_b_min minimal bandwidth per peer
5858 * @param v_n_min minimum number of connections
5859 * @param res result struct
5860 * @return GNUNET_SYSERR if glpk is not available, number of mechanisms used
5862 static int ats_solve_problem (int max_it, int max_dur , double D, double U, double R, int v_b_min, int v_n_min, struct ATS_result *res)
5865 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "no glpk installed\n");
5866 return GNUNET_SYSERR;
5869 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "glpk installed\n");
5881 int c_c_ressources = available_ressources;
5882 int c_q_metrics = available_quality_metrics;
5884 double M = VERY_BIG_DOUBLE_VALUE;
5885 double Q[c_q_metrics+1];
5886 for (c=1; c<=c_q_metrics; c++)
5891 struct NeighbourList *next = neighbours;
5894 struct ReadyList *r_next = next->plugins;
5895 while (r_next != NULL)
5897 struct ForeignAddressList * a_next = r_next->addresses;
5898 while (a_next != NULL)
5901 a_next = a_next->next;
5903 r_next = r_next->next;
5912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No addresses for bw distribution available\n", c_peers);
5914 return GNUNET_SYSERR;
5917 struct ATS_mechanism * mechanisms = GNUNET_malloc((1+c_mechs) * sizeof (struct ATS_mechanism));
5918 struct ATS_peer * peers = GNUNET_malloc((1+c_peers) * sizeof (struct ATS_peer));
5925 peers[c_peers].peer = next->id;
5926 peers[c_peers].m_head = NULL;
5927 peers[c_peers].m_tail = NULL;
5929 peers[c_peers].f = 1.0 / c_mechs;
5931 struct ReadyList *r_next = next->plugins;
5932 while (r_next != NULL)
5934 struct ForeignAddressList * a_next = r_next->addresses;
5935 while (a_next != NULL)
5937 mechanisms[c_mechs].addr = a_next;
5938 mechanisms[c_mechs].col_index = c_mechs;
5939 mechanisms[c_mechs].peer = &peers[c_peers];
5940 mechanisms[c_mechs].next = NULL;
5941 mechanisms[c_mechs].plugin = r_next->plugin;
5943 GNUNET_CONTAINER_DLL_insert_tail(peers[c_peers].m_head, peers[c_peers].m_tail, &mechanisms[c_mechs]);
5945 a_next = a_next->next;
5947 r_next = r_next->next;
5955 if (v_n_min > c_peers)
5959 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);
5962 int size = 1 + 3 + 10 *c_mechs + c_peers + (c_q_metrics*c_mechs)+ c_q_metrics + c_c_ressources * c_mechs ;
5965 int * ia = GNUNET_malloc (size * sizeof (int));
5966 int * ja = GNUNET_malloc (size * sizeof (int));
5967 double * ar = GNUNET_malloc(size* sizeof (double));
5969 prob = glp_create_prob();
5970 glp_set_prob_name(prob, "gnunet ats bandwidth distribution");
5971 glp_set_obj_dir(prob, GLP_MAX);
5973 /* adding columns */
5975 glp_add_cols(prob, 2 * c_mechs);
5976 /* adding b_t cols */
5977 for (c=1; c <= c_mechs; c++)
5979 GNUNET_asprintf(&name, "b%i",c);
5980 glp_set_col_name(prob, c, name);
5982 glp_set_col_bnds(prob, c, GLP_LO, 0.0, 0.0);
5983 glp_set_obj_coef(prob, c, 1);
5986 /* adding n_t cols */
5987 for (c=c_mechs+1; c <= 2*c_mechs; c++)
5989 GNUNET_asprintf(&name, "n%i",(c-c_mechs));
5990 glp_set_col_name(prob, c, name);
5992 glp_set_col_bnds(prob, c, GLP_DB, 0.0, 1.0);
5993 glp_set_col_kind(prob, c, GLP_IV);
5994 glp_set_obj_coef(prob, c, 0);
5997 /* feasibility constraints */
5998 /* Constraint 1: one address per peer*/
6000 glp_add_rows(prob, c_peers);
6001 for (c=1; c<=c_peers; c++)
6003 glp_set_row_bnds(prob, row_index, GLP_FX, 1.0, 1.0);
6005 struct ATS_mechanism *m = peers[c].m_head;
6008 ia[array_index] = row_index;
6009 ja[array_index] = (c_mechs + m->col_index);
6010 ar[array_index] = 1;
6012 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6020 /* Constraint 2: only active mechanism gets bandwidth assigned */
6021 glp_add_rows(prob, c_mechs);
6022 for (c=1; c<=c_mechs; c++)
6024 /* b_t - n_t * M <= 0 */
6026 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6028 glp_set_row_bnds(prob, row_index, GLP_UP, 0.0, 0.0);
6030 ia[array_index] = row_index;
6031 ja[array_index] = mechanisms[c].col_index;
6032 ar[array_index] = 1;
6034 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6037 ia[array_index] = row_index;
6038 ja[array_index] = c_mechs + mechanisms[c].col_index;
6039 ar[array_index] = -M;
6041 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6047 /* Constraint 3: minimum bandwidth*/
6048 glp_add_rows(prob, c_mechs);
6049 for (c=1; c<=c_mechs; c++)
6051 /* b_t - n_t * b_min <= 0 */
6053 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6055 glp_set_row_bnds(prob, row_index, GLP_LO, 0.0, 0.0);
6057 ia[array_index] = row_index;
6058 ja[array_index] = mechanisms[c].col_index;
6059 ar[array_index] = 1;
6061 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6064 ia[array_index] = row_index;
6065 ja[array_index] = c_mechs + mechanisms[c].col_index;
6066 ar[array_index] = -v_b_min;
6068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6074 /* Constraint 4: max ressource capacity */
6075 /* V cr: bt * ct_r <= cr_maxsolution
6077 glp_add_rows(prob, available_ressources);
6078 double ct_max = VERY_BIG_DOUBLE_VALUE;
6079 double ct_min = 0.0;
6081 for (c=0; c<available_ressources; c++)
6083 ct_max = ressources[c].c_max;
6084 ct_min = ressources[c].c_min;
6086 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] %f..%f\n",row_index, ct_min, ct_max);
6088 glp_set_row_bnds(prob, row_index, GLP_DB, ct_min, ct_max);
6090 for (c2=1; c2<=c_mechs; c2++)
6093 ia[array_index] = row_index;
6094 ja[array_index] = c2;
6095 value = mechanisms[c2].addr->ressources[c].c;
6096 ar[array_index] = value;
6098 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6105 /* Constraint 5: min number of connections*/
6106 glp_add_rows(prob, 1);
6107 for (c=1; c<=c_mechs; c++)
6109 // b_t - n_t * b_min >= 0
6111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6113 glp_set_row_bnds(prob, row_index, GLP_LO, v_n_min, 0.0);
6115 ia[array_index] = row_index;
6116 ja[array_index] = c_mechs + mechanisms[c].col_index;
6117 ar[array_index] = 1;
6119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6125 /* optimisation constraints*/
6127 /* adding columns */
6128 glp_add_cols(prob, 3 + c_q_metrics);
6130 glp_set_col_name(prob, (2*c_mechs) + 1, "d");
6131 glp_set_obj_coef(prob, (2*c_mechs) + 1, D);
6132 glp_set_col_bnds(prob, (2*c_mechs) + 1, GLP_LO, 0.0, 0.0);
6133 glp_set_col_name(prob, (2*c_mechs) + 2, "u");
6134 glp_set_obj_coef(prob, (2*c_mechs) + 2, U);
6135 glp_set_col_bnds(prob, (2*c_mechs) + 2, GLP_LO, 0.0, 0.0);
6136 glp_set_col_name(prob, (2*c_mechs) + 3, "r");
6137 glp_set_obj_coef(prob, (2*c_mechs) + 3, R);
6138 glp_set_col_bnds(prob, (2*c_mechs) + 3, GLP_LO, 0.0, 0.0);
6140 for (c=1; c<= c_q_metrics; c++)
6142 GNUNET_asprintf(&name, "Q_%s",qm[c-1].name);
6143 glp_set_col_name(prob, (2*c_mechs) + 3 + c, name);
6144 glp_set_col_bnds(prob, (2*c_mechs) + 3 + c, GLP_LO, 0.0, 0.0);
6146 glp_set_obj_coef(prob, (2*c_mechs) + 3 + c, Q[c]);
6149 // Constraint 6: optimize for diversity
6150 glp_add_rows(prob, 1);
6152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6154 glp_set_row_bnds(prob, row_index, GLP_FX, 0.0, 0.0);
6155 for (c=1; c<=c_mechs; c++)
6157 // b_t - n_t * b_min >= 0
6158 ia[array_index] = row_index;
6159 ja[array_index] = c_mechs + mechanisms[c].col_index;
6160 ar[array_index] = 1;
6162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6166 ia[array_index] = row_index;
6167 ja[array_index] = (2*c_mechs) + 1;
6168 ar[array_index] = -1;
6170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6176 // Constraint 7: optimize for quality
6177 glp_add_rows(prob, available_quality_metrics);
6178 for (c=1; c <= c_q_metrics; c++)
6181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6183 glp_set_row_bnds(prob, row_index, GLP_FX, 0.0, 0.0);
6185 for (c2=1; c2<=c_mechs; c2++)
6188 ia[array_index] = row_index;
6189 ja[array_index] = c2;
6190 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY)
6193 v0 = mechanisms[c2].addr->quality[c-1].values[0];
6194 if (v1 < 1) v0 = 0.1;
6195 v1 = mechanisms[c2].addr->quality[c-1].values[1];
6196 if (v1 < 1) v0 = 0.1;
6197 v2 = mechanisms[c2].addr->quality[c-1].values[2];
6198 if (v1 < 1) v0 = 0.1;
6199 value = 100.0 / ((v0 + 2 * v1 + 3 * v2) / 6.0);
6201 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
6204 v0 = mechanisms[c2].addr->quality[c-1].values[0];
6206 v1 = mechanisms[c2].addr->quality[c-1].values[1];
6208 v2 = mechanisms[c2].addr->quality[c-1].values[2];
6210 value = (v0 + 2 * v1 + 3 * v2) / 6.0;
6212 value = (double) 10 / value;
6216 ar[array_index] = (mechanisms[c2].peer->f) * value ;
6218 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]);
6223 ia[array_index] = row_index;
6224 ja[array_index] = (2*c_mechs) + 3 +c;
6225 ar[array_index] = -1;
6227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6233 // Constraint 8: optimize bandwidth utility
6234 glp_add_rows(prob, 1);
6236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6238 glp_set_row_bnds(prob, row_index, GLP_FX, 0.0, 0.0);
6239 for (c=1; c<=c_mechs; c++)
6241 ia[array_index] = row_index;
6242 ja[array_index] = c;
6243 ar[array_index] = mechanisms[c].peer->f;
6245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6249 ia[array_index] = row_index;
6250 ja[array_index] = (2*c_mechs) + 2;
6251 ar[array_index] = -1;
6253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6259 // Constraint 9: optimize relativity
6260 glp_add_rows(prob, c_peers);
6261 for (c=1; c<=c_peers; c++)
6263 glp_set_row_bnds(prob, row_index, GLP_LO, 0.0, 0.0);
6265 struct ATS_mechanism *m = peers[c].m_head;
6268 ia[array_index] = row_index;
6269 ja[array_index] = m->col_index;
6270 ar[array_index] = 1;
6272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6277 ia[array_index] = row_index;
6278 ja[array_index] = (2*c_mechs) + 3;
6279 ar[array_index] = -1;
6281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6287 /* Loading the matrix */
6288 glp_load_matrix(prob, array_index-1, ia, ja, ar);
6290 /* Solving simplex */
6292 glp_init_smcp(&opt_lp);
6296 opt_lp.msg_lev = GLP_MSG_ALL;
6298 opt_lp.msg_lev = GLP_MSG_OFF;
6300 result = glp_simplex(prob, &opt_lp);
6301 solution = glp_get_status (prob);
6303 if (GNUNET_YES == ats_evaluate_results(result, solution, "LP"))
6307 glp_init_iocp(&opt_mlp);
6308 /* maximum duration */
6309 opt_mlp.tm_lim = max_dur;
6312 opt_mlp.msg_lev = GLP_MSG_ALL;
6314 opt_mlp.msg_lev = GLP_MSG_OFF;
6316 result = glp_intopt (prob, &opt_mlp);
6317 solution = glp_mip_status (prob);
6318 ats_evaluate_results(result, solution, "MLP");
6326 GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i.mlp",c_peers, c_mechs);
6327 if (GNUNET_NO == GNUNET_DISK_file_test(filename))
6328 glp_write_lp (prob, NULL, filename);
6329 GNUNET_free (filename);
6334 int error = GNUNET_NO;
6336 struct ATS_mechanism *t = NULL;
6337 for (c=1; c<= (c_peers); c++ )
6340 t = peers[c].m_head;
6343 bw = glp_get_col_prim(prob, t->col_index);
6347 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);
6349 if (check ==GNUNET_YES)
6351 glp_write_sol(prob, "invalid_solution.mlp");
6352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid solution, check invalid_solution.mlp");
6353 GNUNET_STATISTICS_update (stats, "ATS invalid solutions", 1, GNUNET_NO);
6356 if (check ==GNUNET_NO)
6364 for (c=1; c<= c_q_metrics; c++ )
6367 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));
6370 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));
6371 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));
6372 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));
6374 res->c_mechs = c_mechs;
6375 res->c_peers = c_peers;
6376 res->solution = solution;
6380 glp_delete_prob(prob);
6386 for (c=0; c<c_mechs; c++)
6387 GNUNET_free_non_null (mechanisms[c].rc);
6389 GNUNET_free(mechanisms);
6396 void ats_calculate_bandwidth_distribution ()
6398 struct GNUNET_TIME_Absolute start;
6399 struct GNUNET_TIME_Relative duration;
6400 struct ATS_result result;
6403 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_difference(ats->last,GNUNET_TIME_absolute_get());
6404 if (delta.rel_value < ats->min_delta.rel_value)
6407 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Minimum time between cycles not reached\n");
6413 if (INT_MAX < ats->max_exec_duration.rel_value)
6416 dur = (int) ats->max_exec_duration.rel_value;
6418 start = GNUNET_TIME_absolute_get();
6419 c_mechs = ats_solve_problem(5000, 5000, 1.0, 1.0, 1.0, 1000, 5, &result);
6420 duration = GNUNET_TIME_absolute_get_difference(start,GNUNET_TIME_absolute_get());
6425 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP execution time in [ms] for %i mechanisms: %llu\n", c_mechs, duration.rel_value);
6427 GNUNET_STATISTICS_set (stats, "ATS duration", duration.rel_value, GNUNET_NO);
6428 GNUNET_STATISTICS_set (stats, "ATS mechanisms", result.c_mechs, GNUNET_NO);
6429 GNUNET_STATISTICS_set (stats, "ATS peers", result.c_peers, GNUNET_NO);
6430 GNUNET_STATISTICS_set (stats, "ATS solution", result.solution, GNUNET_NO);
6431 GNUNET_STATISTICS_set (stats, "ATS timestamp", start.abs_value, GNUNET_NO);
6434 else if (c_mechs == 0)
6436 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP not executed: no addresses\n");
6439 ats->last = GNUNET_TIME_absolute_get();
6445 ats_schedule_calculation (void *cls,
6446 const struct GNUNET_SCHEDULER_TaskContext *tc)
6448 struct ATS_info *ats = (struct ATS_info *) cls;
6452 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6453 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
6457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running scheduled calculation\n");
6460 ats_calculate_bandwidth_distribution (ats);
6462 ats->ats_task = GNUNET_SCHEDULER_add_delayed (ats->exec_intervall,
6463 &ats_schedule_calculation, ats);
6468 ats = GNUNET_malloc(sizeof (struct ATS_info));
6470 ats->min_delta = ATS_MIN_INTERVAL;
6471 ats->exec_intervall = ATS_EXEC_INTERVAL;
6472 ats->max_exec_duration = ATS_MAX_EXEC_DURATION;
6473 ats->max_iterations = ATS_MAX_ITERATIONS;
6474 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6477 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed, ATS not active");
6483 unsigned long long value;
6485 /* loading cost ressources */
6486 for (c=0; c<available_ressources; c++)
6488 GNUNET_asprintf(§ion,"%s_UP",ressources[c].cfg_param);
6489 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
6491 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "transport",section, &value))
6494 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found ressource cost: [%s] = %llu\n", section, value);
6496 ressources[c].c_max = value;
6499 GNUNET_free (section);
6500 GNUNET_asprintf(§ion,"%s_DOWN",ressources[c].cfg_param);
6501 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
6503 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "transport",section, &value))
6506 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found ressource cost: [%s] = %llu\n", section, value);
6508 ressources[c].c_min = value;
6511 GNUNET_free (section);
6514 ats->ats_task = GNUNET_SCHEDULER_add_now(&ats_schedule_calculation, ats);
6518 void ats_shutdown ()
6521 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_destroy\n");
6523 if (ats->ats_task != GNUNET_SCHEDULER_NO_TASK)
6524 GNUNET_SCHEDULER_cancel(ats->ats_task);
6525 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6527 struct ATS_plugin * p;
6528 struct ATS_ressource_cost * rc;
6532 GNUNET_CONTAINER_DLL_remove (ats->head,ats->tail, p);
6536 GNUNET_CONTAINER_DLL_remove (p->head,p->tail, rc);
6540 GNUNET_free(p->short_name);
6549 void ats_notify_peer_connect (
6550 const struct GNUNET_PeerIdentity *peer,
6551 const struct GNUNET_TRANSPORT_ATS_Information *ats_data)
6555 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_notify_peer_connect: %s\n",GNUNET_i2s(peer));
6558 while (ntohl(ats_data[c].type)!=0)
6561 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats type [%i]: %i\n",ntohl(ats_data[c].type), ntohl(ats_data[c].value));
6565 ats_calculate_bandwidth_distribution(ats);
6568 void ats_notify_peer_disconnect (
6569 const struct GNUNET_PeerIdentity *peer)
6572 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_notify_peer_disconnect: %s\n",GNUNET_i2s(peer));
6574 ats_calculate_bandwidth_distribution (ats);
6578 void ats_notify_ats_data (
6579 const struct GNUNET_PeerIdentity *peer,
6580 const struct GNUNET_TRANSPORT_ATS_Information *ats_data)
6583 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "ATS_notify_ats_data: %s\n",GNUNET_i2s(peer));
6585 ats_calculate_bandwidth_distribution(ats);
6588 struct ForeignAddressList * ats_get_preferred_address (
6589 struct NeighbourList *n)
6592 //GNUNET_log (GNUNET_ERROR_TYPE_BULK, "ats_get_prefered_transport for peer: %s\n",GNUNET_i2s(&n->id));
6594 struct ReadyList *next = n->plugins;
6595 while (next != NULL)
6598 //GNUNET_log (GNUNET_ERROR_TYPE_BULK, "plugin: %s %i\n",next->plugin->short_name,strcmp(next->plugin->short_name,"unix"));
6602 return find_ready_address(n);
6606 * Initiate transport service.
6608 * @param cls closure
6609 * @param server the initialized server
6610 * @param c configuration to use
6614 struct GNUNET_SERVER_Handle *server,
6615 const struct GNUNET_CONFIGURATION_Handle *c)
6617 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
6618 {&handle_start, NULL,
6619 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
6620 {&handle_hello, NULL,
6621 GNUNET_MESSAGE_TYPE_HELLO, 0},
6622 {&handle_send, NULL,
6623 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
6624 {&handle_request_connect, NULL,
6625 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT, sizeof(struct TransportRequestConnectMessage)},
6626 {&handle_set_quota, NULL,
6627 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
6628 {&handle_address_lookup, NULL,
6629 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
6631 {&handle_blacklist_init, NULL,
6632 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
6633 {&handle_blacklist_reply, NULL,
6634 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
6640 unsigned long long tneigh;
6644 stats = GNUNET_STATISTICS_create ("transport", cfg);
6645 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
6646 /* parse configuration */
6648 GNUNET_CONFIGURATION_get_value_number (c,
6653 GNUNET_CONFIGURATION_get_value_filename (c,
6655 "HOSTKEY", &keyfile)))
6657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6659 ("Transport service is lacking key configuration settings. Exiting.\n"));
6660 GNUNET_SCHEDULER_shutdown ();
6663 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6666 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6667 validation_map = NULL;
6671 max_connect_per_transport = (uint32_t) tneigh;
6672 peerinfo = GNUNET_PEERINFO_connect (cfg);
6673 if (peerinfo == NULL)
6675 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6676 _("Could not access PEERINFO service. Exiting.\n"));
6677 GNUNET_SCHEDULER_shutdown ();
6680 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6683 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6684 validation_map = NULL;
6685 GNUNET_free (keyfile);
6688 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6689 GNUNET_free (keyfile);
6690 if (my_private_key == NULL)
6692 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6694 ("Transport service could not access hostkey. Exiting.\n"));
6695 GNUNET_SCHEDULER_shutdown ();
6698 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6701 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6702 validation_map = NULL;
6705 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6706 GNUNET_CRYPTO_hash (&my_public_key,
6707 sizeof (my_public_key), &my_identity.hashPubKey);
6708 /* setup notification */
6709 GNUNET_SERVER_disconnect_notify (server,
6710 &client_disconnect_notification, NULL);
6711 /* load plugins... */
6714 GNUNET_CONFIGURATION_get_value_string (c,
6715 "TRANSPORT", "PLUGINS", &plugs))
6717 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6718 _("Starting transport plugins `%s'\n"), plugs);
6719 pos = strtok (plugs, " ");
6722 start_transport (server, pos);
6724 pos = strtok (NULL, " ");
6726 GNUNET_free (plugs);
6728 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
6729 &shutdown_task, NULL);
6736 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport service ready.\n"));
6738 /* If we have a blacklist file, read from it */
6739 read_blacklist_file(cfg);
6740 /* process client requests */
6741 GNUNET_SERVER_add_handlers (server, handlers);
6746 * The main function for the transport service.
6748 * @param argc number of arguments from the command line
6749 * @param argv command line arguments
6750 * @return 0 ok, 1 on error
6753 main (int argc, char *const *argv)
6755 a2s (NULL, NULL, 0); /* make compiler happy */
6756 return (GNUNET_OK ==
6757 GNUNET_SERVICE_run (argc,
6760 GNUNET_SERVICE_OPTION_NONE,
6761 &run, NULL)) ? 0 : 1;
6764 /* end of gnunet-service-transport.c */