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 #define ATS_Q_UPDATED 1
159 #define ATS_C_UPDATED 2
160 #define ATS_QC_UPDATED 3
161 #define ATS_UNMODIFIED 4
164 * List of addresses of other peers
166 struct ForeignAddressList
169 * This is a linked list.
171 struct ForeignAddressList *next;
174 * Which ready list does this entry belong to.
176 struct ReadyList *ready_list;
179 * How long until we auto-expire this address (unless it is
180 * re-confirmed by the transport)?
182 struct GNUNET_TIME_Absolute expires;
185 * Task used to re-validate addresses, updates latencies and
188 GNUNET_SCHEDULER_TaskIdentifier revalidate_task;
196 * Session (or NULL if no valid session currently exists or if the
197 * plugin does not use sessions).
199 struct Session *session;
201 struct ATS_ressource_entry * ressources;
203 struct ATS_quality_entry * quality;
206 * What was the last latency observed for this address, plugin and peer?
208 struct GNUNET_TIME_Relative latency;
211 * If we did not successfully transmit a message to the given peer
212 * via this connection during the specified time, we should consider
213 * the connection to be dead. This is used in the case that a TCP
214 * transport simply stalls writing to the stream but does not
215 * formerly get a signal that the other peer died.
217 struct GNUNET_TIME_Absolute timeout;
220 * How often have we tried to connect using this plugin? Used to
221 * discriminate against addresses that do not work well.
222 * FIXME: not yet used, but should be!
224 unsigned int connect_attempts;
227 * DV distance to this peer (1 if no DV is used).
228 * FIXME: need to set this from transport plugins!
238 * Have we ever estimated the latency of this address? Used to
239 * ensure that the first time we add an address, we immediately
245 * Are we currently connected via this address? The first time we
246 * successfully transmit or receive data to a peer via a particular
247 * address, we set this to GNUNET_YES. If we later get an error
248 * (disconnect notification, transmission failure, timeout), we set
249 * it back to GNUNET_NO.
254 * Is this plugin currently busy transmitting to the specific target?
255 * GNUNET_NO if not (initial, default state is GNUNET_NO). Internal
256 * messages do not count as 'in transmit'.
261 * Has this address been validated yet?
269 * Entry in linked list of network addresses for ourselves. Also
270 * includes a cached signature for 'struct TransportPongMessage's.
272 struct OwnAddressList
275 * This is a linked list.
277 struct OwnAddressList *next;
280 * How long until we actually auto-expire this address (unless it is
281 * re-confirmed by the transport)?
283 struct GNUNET_TIME_Absolute expires;
286 * How long until the current signature expires? (ZERO if the
287 * signature was never created).
289 struct GNUNET_TIME_Absolute pong_sig_expires;
292 * Signature for a 'struct TransportPongMessage' for this address.
294 struct GNUNET_CRYPTO_RsaSignature pong_signature;
305 * Entry in linked list of all of our plugins.
307 struct TransportPlugin
311 * This is a linked list.
313 struct TransportPlugin *next;
316 * API of the transport as returned by the plugin's
317 * initialization function.
319 struct GNUNET_TRANSPORT_PluginFunctions *api;
322 * Short name for the plugin (i.e. "tcp").
327 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
332 * List of our known addresses for this transport.
334 struct OwnAddressList *addresses;
337 * Environment this transport service is using
340 struct GNUNET_TRANSPORT_PluginEnvironment env;
343 * ID of task that is used to clean up expired addresses.
345 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
348 * Set to GNUNET_YES if we need to scrap the existing list of
349 * "addresses" and start fresh when we receive the next address
350 * update from a transport. Set to GNUNET_NO if we should just add
351 * the new address to the list and wait for the commit call.
355 struct ATS_plugin * rc;
358 * Hashmap of blacklisted peers for this particular transport.
360 struct GNUNET_CONTAINER_MultiHashMap *blacklist;
363 struct NeighbourList;
366 * For each neighbour we keep a list of messages
367 * that we still want to transmit to the neighbour.
373 * This is a doubly linked list.
375 struct MessageQueue *next;
378 * This is a doubly linked list.
380 struct MessageQueue *prev;
383 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
384 * stuck together in memory. Allocated at the end of this struct.
386 const char *message_buf;
389 * Size of the message buf
391 size_t message_buf_size;
394 * Client responsible for queueing the message;
395 * used to check that a client has no two messages
396 * pending for the same target. Can be NULL.
398 struct TransportClient *client;
401 * Using which specific address should we send this message?
403 struct ForeignAddressList *specific_address;
406 * Peer ID of the Neighbour this entry belongs to.
408 struct GNUNET_PeerIdentity neighbour_id;
411 * Plugin that we used for the transmission.
412 * NULL until we scheduled a transmission.
414 struct TransportPlugin *plugin;
417 * At what time should we fail?
419 struct GNUNET_TIME_Absolute timeout;
422 * Internal message of the transport system that should not be
423 * included in the usual SEND-SEND_OK transmission confirmation
424 * traffic management scheme. Typically, "internal_msg" will
425 * be set whenever "client" is NULL (but it is not strictly
431 * How important is the message?
433 unsigned int priority;
439 * For a given Neighbour, which plugins are available
440 * to talk to this peer and what are their costs?
445 * This is a linked list.
447 struct ReadyList *next;
450 * Which of our transport plugins does this entry
453 struct TransportPlugin *plugin;
456 * Transport addresses, latency, and readiness for
457 * this particular plugin.
459 struct ForeignAddressList *addresses;
462 * To which neighbour does this ready list belong to?
464 struct NeighbourList *neighbour;
469 * Entry in linked list of all of our current neighbours.
475 * This is a linked list.
477 struct NeighbourList *next;
480 * Which of our transports is connected to this peer
481 * and what is their status?
483 struct ReadyList *plugins;
486 * Head of list of messages we would like to send to this peer;
487 * must contain at most one message per client.
489 struct MessageQueue *messages_head;
492 * Tail of list of messages we would like to send to this peer; must
493 * contain at most one message per client.
495 struct MessageQueue *messages_tail;
498 * Buffer for at most one payload message used when we receive
499 * payload data before our PING-PONG has succeeded. We then
500 * store such messages in this intermediary buffer until the
501 * connection is fully up.
503 struct GNUNET_MessageHeader *pre_connect_message_buffer;
506 * Context for peerinfo iteration.
507 * NULL after we are done processing peerinfo's information.
509 struct GNUNET_PEERINFO_IteratorContext *piter;
512 * Public key for this peer. Valid only if the respective flag is set below.
514 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
517 * Identity of this neighbour.
519 struct GNUNET_PeerIdentity id;
522 * ID of task scheduled to run when this peer is about to
523 * time out (will free resources associated with the peer).
525 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
528 * ID of task scheduled to run when we should retry transmitting
529 * the head of the message queue. Actually triggered when the
530 * transmission is timing out (we trigger instantly when we have
531 * a chance of success).
533 GNUNET_SCHEDULER_TaskIdentifier retry_task;
536 * How long until we should consider this peer dead
537 * (if we don't receive another message in the
540 struct GNUNET_TIME_Absolute peer_timeout;
543 * Tracker for inbound bandwidth.
545 struct GNUNET_BANDWIDTH_Tracker in_tracker;
548 * The latency we have seen for this particular address for
549 * this particular peer. This latency may have been calculated
550 * over multiple transports. This value reflects how long it took
551 * us to receive a response when SENDING via this particular
552 * transport/neighbour/address combination!
554 * FIXME: we need to periodically send PINGs to update this
555 * latency (at least more often than the current "huge" (11h?)
558 struct GNUNET_TIME_Relative latency;
561 * How often has the other peer (recently) violated the
562 * inbound traffic limit? Incremented by 10 per violation,
563 * decremented by 1 per non-violation (for each
566 unsigned int quota_violation_count;
569 * DV distance to this peer (1 if no DV is used).
574 * Have we seen an PONG from this neighbour in the past (and
575 * not had a disconnect since)?
580 * Do we have a valid public key for this neighbour?
582 int public_key_valid;
585 * Performance data for the peer.
587 struct GNUNET_TRANSPORT_ATS_Information *ats;
590 * Identity of the neighbour.
592 struct GNUNET_PeerIdentity peer;
597 * Message used to ask a peer to validate receipt (to check an address
598 * from a HELLO). Followed by the address we are trying to validate,
599 * or an empty address if we are just sending a PING to confirm that a
600 * connection which the receiver (of the PING) initiated is still valid.
602 struct TransportPingMessage
606 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
608 struct GNUNET_MessageHeader header;
611 * Challenge code (to ensure fresh reply).
613 uint32_t challenge GNUNET_PACKED;
616 * Who is the intended recipient?
618 struct GNUNET_PeerIdentity target;
624 * Message used to validate a HELLO. The challenge is included in the
625 * confirmation to make matching of replies to requests possible. The
626 * signature signs our public key, an expiration time and our address.<p>
628 * This message is followed by our transport address that the PING tried
629 * to confirm (if we liked it). The address can be empty (zero bytes)
630 * if the PING had not address either (and we received the request via
631 * a connection that we initiated).
633 struct TransportPongMessage
637 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
639 struct GNUNET_MessageHeader header;
642 * Challenge code from PING (showing freshness). Not part of what
643 * is signed so that we can re-use signatures.
645 uint32_t challenge GNUNET_PACKED;
650 struct GNUNET_CRYPTO_RsaSignature signature;
653 * What are we signing and why? Two possible reason codes can be here:
654 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
655 * plausible address for this peer (pid is set to identity of signer); or
656 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING to confirm that this is
657 * an address we used to connect to the peer with the given pid.
659 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
662 * When does this signature expire?
664 struct GNUNET_TIME_AbsoluteNBO expiration;
667 * Either the identity of the peer Who signed this message, or the
668 * identity of the peer that we're connected to using the given
669 * address (depending on purpose.type).
671 struct GNUNET_PeerIdentity pid;
674 * Size of address appended to this message (part of what is
675 * being signed, hence not redundant).
683 * Linked list of messages to be transmitted to the client. Each
684 * entry is followed by the actual message.
686 struct ClientMessageQueueEntry
689 * This is a doubly-linked list.
691 struct ClientMessageQueueEntry *next;
694 * This is a doubly-linked list.
696 struct ClientMessageQueueEntry *prev;
701 * Client connected to the transport service.
703 struct TransportClient
707 * This is a linked list.
709 struct TransportClient *next;
712 * Handle to the client.
714 struct GNUNET_SERVER_Client *client;
717 * Linked list of messages yet to be transmitted to
720 struct ClientMessageQueueEntry *message_queue_head;
723 * Tail of linked list of messages yet to be transmitted to the
726 struct ClientMessageQueueEntry *message_queue_tail;
729 * Current transmit request handle.
731 struct GNUNET_CONNECTION_TransmitHandle *th;
734 * Is a call to "transmit_send_continuation" pending? If so, we
735 * must not free this struct (even if the corresponding client
736 * disconnects) and instead only remove it from the linked list and
737 * set the "client" field to NULL.
742 * Length of the list of messages pending for this client.
744 unsigned int message_count;
750 * Context of currently active requests to peerinfo
751 * for validation of HELLOs.
753 struct CheckHelloValidatedContext;
757 * Entry in map of all HELLOs awaiting validation.
759 struct ValidationEntry
763 * NULL if this entry is not part of a larger HELLO validation.
765 struct CheckHelloValidatedContext *chvc;
768 * The address, actually a pointer to the end
769 * of this struct. Do not free!
774 * Name of the transport.
776 char *transport_name;
779 * The public key of the peer.
781 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
784 * ID of task that will clean up this entry if we don't succeed
785 * with the validation first.
787 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
790 * At what time did we send this validation?
792 struct GNUNET_TIME_Absolute send_time;
795 * Session being validated (or NULL for none).
797 struct Session *session;
800 * Challenge number we used.
813 * Context of currently active requests to peerinfo
814 * for validation of HELLOs.
816 struct CheckHelloValidatedContext
820 * This is a doubly-linked list.
822 struct CheckHelloValidatedContext *next;
825 * This is a doubly-linked list.
827 struct CheckHelloValidatedContext *prev;
830 * Hello that we are validating.
832 const struct GNUNET_HELLO_Message *hello;
835 * Context for peerinfo iteration.
836 * NULL after we are done processing peerinfo's information.
838 struct GNUNET_PEERINFO_IteratorContext *piter;
841 * Was a HELLO known for this peer to peerinfo?
846 * Number of validation entries currently referring to this
849 unsigned int ve_count;
852 struct ATS_quality_metric
861 struct ATS_mechanism * prev;
862 struct ATS_mechanism * next;
863 struct ForeignAddressList * addr;
864 struct TransportPlugin * plugin;
865 struct ATS_peer * peer;
868 struct ATS_ressource_cost * rc;
874 struct GNUNET_PeerIdentity peer;
875 struct NeighbourList * n;
876 struct ATS_mechanism * m_head;
877 struct ATS_mechanism * m_tail;
879 /* preference value f */
887 * result of last GLPK run
893 * Ressource costs or quality metrics changed
894 * update problem before solving
896 int modified_resources;
899 * Ressource costs or quality metrics changed, update matrix
900 * update problem before solving
902 int modified_quality;
905 * Peers have connected or disconnected
906 * problem has to be recreated
908 int recreate_problem;
911 * Was the available basis invalid and we needed to rerun simplex?
913 int simplex_rerun_required;
916 * is problem currently valid and can it be solved
921 * Number of transport mechanisms in the problem
926 * Number of transport mechanisms in the problem
931 * row index where quality related rows start
936 * row index where quality related rows end
941 * row index where ressource cost related rows start
946 * row index where ressource cost related rows end
951 * column index for objective function value d
956 * column index for objective function value u
961 * column index for objective function value r
966 * column index for objective function value quality metrics
971 * column index for objective function value cost ressources
976 struct ATS_ressource_entry
978 /* index in ressources array */
980 /* depending ATSi parameter to calculcate limits */
989 /* index in ressources array */
991 /* depending ATSi parameter to calculcate limits */
993 /* cfg option to load limits */
1000 /* cofficients for the specific plugins */
1010 static struct ATS_ressource ressources[] =
1012 /* FIXME: the coefficients for the specific plugins */
1013 {1, 7, "LAN_BW_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 1, 1, 2, 2, 1, 3},
1014 {2, 7, "WAN_BW_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 1, 1, 2, 2, 2, 3},
1015 {3, 4, "WLAN_ENERGY_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 0, 0, 0, 0, 2, 1}
1017 {4, 4, "COST_ENERGY_CONSUMPTION", VERY_BIG_DOUBLE_VALUE},
1018 {5, 5, "COST_CONNECT", VERY_BIG_DOUBLE_VALUE},
1019 {6, 6, "COST_BANDWITH_AVAILABLE", VERY_BIG_DOUBLE_VALUE},
1020 {7, 7, "COST_NETWORK_OVERHEAD", VERY_BIG_DOUBLE_VALUE},*/
1023 static int available_ressources = 3;
1031 * Time of last execution
1033 struct GNUNET_TIME_Absolute last;
1035 * Minimum intervall between two executions
1037 struct GNUNET_TIME_Relative min_delta;
1039 * Regular intervall when execution is triggered
1041 struct GNUNET_TIME_Relative exec_interval;
1043 * Maximum execution time per calculation
1045 struct GNUNET_TIME_Relative max_exec_duration;
1049 * GLPK (MLP) problem object
1055 * task to recalculate the bandwidth assignment
1057 GNUNET_SCHEDULER_TaskIdentifier ats_task;
1060 * Current state of the GLPK problem
1062 struct ATS_stat stat;
1065 * mechanisms used in current problem
1066 * needed for problem modification
1068 struct ATS_mechanism * mechanisms;
1071 * peers used in current problem
1072 * needed for problem modification
1074 struct ATS_peer * peers;
1077 * number of successful executions
1079 int successful_executions;
1082 * number with an invalid result
1084 int invalid_executions;
1087 * Maximum number of LP iterations per calculation
1092 * Dump problem to a file?
1097 * Dump solution to a file
1102 * Dump solution when minimum peers:
1107 * Dump solution when minimum addresses:
1112 * Dump solution overwrite file:
1132 * Minimum bandwidth per peer
1137 * Minimum number of connections per peer
1144 * Our HELLO message.
1146 static struct GNUNET_HELLO_Message *our_hello;
1151 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
1156 static struct GNUNET_PeerIdentity my_identity;
1161 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
1164 * Our configuration.
1166 const struct GNUNET_CONFIGURATION_Handle *cfg;
1169 * Linked list of all clients to this service.
1171 static struct TransportClient *clients;
1174 * All loaded plugins.
1176 static struct TransportPlugin *plugins;
1179 * Handle to peerinfo service.
1181 static struct GNUNET_PEERINFO_Handle *peerinfo;
1184 * All known neighbours and their HELLOs.
1186 static struct NeighbourList *neighbours;
1189 * Number of neighbours we'd like to have.
1191 static uint32_t max_connect_per_transport;
1194 * Head of linked list.
1196 static struct CheckHelloValidatedContext *chvc_head;
1199 * Tail of linked list.
1201 static struct CheckHelloValidatedContext *chvc_tail;
1204 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
1205 * of the given peer that we are currently validating).
1207 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
1210 * Handle for reporting statistics.
1212 static struct GNUNET_STATISTICS_Handle *stats;
1215 * Handle for ats information
1217 static struct ATS_info *ats;
1219 struct ATS_quality_entry
1227 static struct ATS_quality_metric qm[] =
1229 {1, 1028, "QUALITY_NET_DISTANCE"},
1230 {2, 1034, "QUALITY_NET_DELAY"},
1232 static int available_quality_metrics = 2;
1236 * The peer specified by the given neighbour has timed-out or a plugin
1237 * has disconnected. We may either need to do nothing (other plugins
1238 * still up), or trigger a full disconnect and clean up. This
1239 * function updates our state and do the necessary notifications.
1240 * Also notifies our clients that the neighbour is now officially
1243 * @param n the neighbour list entry for the peer
1244 * @param check should we just check if all plugins
1245 * disconnected or must we ask all plugins to
1248 static void disconnect_neighbour (struct NeighbourList *n, int check);
1251 * Check the ready list for the given neighbour and if a plugin is
1252 * ready for transmission (and if we have a message), do so!
1254 * @param nexi target peer for which to transmit
1256 static void try_transmission_to_peer (struct NeighbourList *n);
1258 static void ats_shutdown ( );
1260 static void ats_notify_peer_connect (
1261 const struct GNUNET_PeerIdentity *peer,
1262 const struct GNUNET_TRANSPORT_ATS_Information *ats_data, int ats_count);
1264 static void ats_notify_peer_disconnect (
1265 const struct GNUNET_PeerIdentity *peer);
1268 static void ats_notify_ats_data (
1269 const struct GNUNET_PeerIdentity *peer,
1270 const struct GNUNET_TRANSPORT_ATS_Information *ats_data);
1273 struct ForeignAddressList * ats_get_preferred_address (
1274 struct NeighbourList *n);
1277 ats_calculate_bandwidth_distribution ();
1280 * Find an entry in the neighbour list for a particular peer.
1282 * @return NULL if not found.
1284 static struct NeighbourList *
1285 find_neighbour (const struct GNUNET_PeerIdentity *key)
1287 struct NeighbourList *head = neighbours;
1289 while ((head != NULL) &&
1290 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
1295 static int update_addr_value (struct ForeignAddressList *fal, uint32_t value , int ats_index)
1298 int set = GNUNET_NO;
1299 for (c=0; c<available_quality_metrics; c++)
1301 if (ats_index == qm[c].atis_index)
1303 fal->quality[c].values[0] = fal->quality[c].values[1];
1304 fal->quality[c].values[1] = fal->quality[c].values[2];
1305 fal->quality[c].values[2] = value;
1307 ats->stat.modified_quality = GNUNET_YES;
1310 if (set == GNUNET_NO)
1312 for (c=0; c<available_ressources; c++)
1314 if (ats_index == ressources[c].atis_index)
1316 fal->ressources[c].c = value;
1318 ats->stat.modified_resources = GNUNET_YES;
1327 update_addr_ats (struct ForeignAddressList *fal,
1328 const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
1333 for (c1=0; c1<ats_count; c1++)
1335 set = update_addr_value(fal, ntohl(ats_data[c1].value), ntohl(ats_data[c1].type));
1341 * Find an entry in the transport list for a particular transport.
1343 * @return NULL if not found.
1345 static struct TransportPlugin *
1346 find_transport (const char *short_name)
1348 struct TransportPlugin *head = plugins;
1349 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
1355 * Is a particular peer blacklisted for a particular transport?
1357 * @param peer the peer to check for
1358 * @param plugin the plugin used to connect to the peer
1360 * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
1363 is_blacklisted (const struct GNUNET_PeerIdentity *peer, struct TransportPlugin *plugin)
1366 if (plugin->blacklist != NULL)
1368 if (GNUNET_CONTAINER_multihashmap_contains (plugin->blacklist, &peer->hashPubKey) == GNUNET_YES)
1371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1372 "Peer `%s:%s' is blacklisted!\n",
1373 plugin->short_name, GNUNET_i2s (peer));
1376 GNUNET_STATISTICS_update (stats, "# blacklisted peers refused", 1, GNUNET_NO);
1386 add_peer_to_blacklist (struct GNUNET_PeerIdentity *peer,
1387 char *transport_name)
1389 struct TransportPlugin *plugin;
1391 plugin = find_transport(transport_name);
1392 if (plugin == NULL) /* Nothing to do */
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1396 "Adding peer `%s' with plugin `%s' to blacklist\n",
1400 if (plugin->blacklist == NULL)
1401 plugin->blacklist = GNUNET_CONTAINER_multihashmap_create(TRANSPORT_BLACKLIST_HT_SIZE);
1402 GNUNET_assert(plugin->blacklist != NULL);
1403 GNUNET_CONTAINER_multihashmap_put(plugin->blacklist, &peer->hashPubKey,
1405 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1410 * Read the blacklist file, containing transport:peer entries.
1411 * Provided the transport is loaded, set up hashmap with these
1412 * entries to blacklist peers by transport.
1416 read_blacklist_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1423 struct GNUNET_PeerIdentity pid;
1425 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1426 unsigned int entries_found;
1427 char *transport_name;
1430 GNUNET_CONFIGURATION_get_value_filename (cfg,
1436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1437 "Option `%s' in section `%s' not specified!\n",
1443 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1444 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1445 | GNUNET_DISK_PERM_USER_WRITE);
1446 if (0 != STAT (fn, &frstat))
1448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1449 _("Could not read blacklist file `%s'\n"), fn);
1453 if (frstat.st_size == 0)
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1457 _("Blacklist file `%s' is empty.\n"),
1463 /* FIXME: use mmap */
1464 data = GNUNET_malloc_large (frstat.st_size);
1465 GNUNET_assert(data != NULL);
1466 if (frstat.st_size !=
1467 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1470 _("Failed to read blacklist from `%s'\n"), fn);
1477 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1479 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1480 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1483 while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') && !isspace ( (unsigned char) data[colon_pos]))
1486 if (colon_pos >= frstat.st_size)
1488 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1489 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1490 (unsigned long long) colon_pos);
1496 if (isspace( (unsigned char) data[colon_pos]))
1498 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1499 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1500 (unsigned long long) colon_pos);
1502 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1506 tsize = colon_pos - pos;
1507 if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size) || (tsize == 0))
1509 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1510 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1511 (unsigned long long) colon_pos);
1520 transport_name = GNUNET_malloc(tsize + 1);
1521 memcpy(transport_name, &data[pos], tsize);
1522 pos = colon_pos + 1;
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1525 "Read transport name %s in blacklist file.\n",
1528 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1529 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1531 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1532 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1533 (unsigned long long) pos);
1535 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1537 GNUNET_free_non_null(transport_name);
1540 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1541 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1543 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1544 _("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
1545 (unsigned long long) pos,
1550 if (0 != memcmp (&pid,
1552 sizeof (struct GNUNET_PeerIdentity)))
1555 add_peer_to_blacklist (&pid,
1560 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1561 _("Found myself `%s' in blacklist (useless, ignored)\n"),
1565 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1566 GNUNET_free_non_null(transport_name);
1567 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1570 GNUNET_STATISTICS_update (stats, "# Transport entries blacklisted", entries_found, GNUNET_NO);
1577 * Function called to notify a client about the socket being ready to
1578 * queue more data. "buf" will be NULL and "size" zero if the socket
1579 * was closed for writing in the meantime.
1581 * @param cls closure
1582 * @param size number of bytes available in buf
1583 * @param buf where the callee should write the message
1584 * @return number of bytes written to buf
1587 transmit_to_client_callback (void *cls, size_t size, void *buf)
1589 struct TransportClient *client = cls;
1590 struct ClientMessageQueueEntry *q;
1593 const struct GNUNET_MessageHeader *msg;
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1601 "Transmission to client failed, closing connection.\n");
1603 /* fatal error with client, free message queue! */
1604 while (NULL != (q = client->message_queue_head))
1606 GNUNET_STATISTICS_update (stats,
1607 gettext_noop ("# bytes discarded (could not transmit to client)"),
1608 ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
1610 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1611 client->message_queue_tail,
1615 client->message_count = 0;
1620 while (NULL != (q = client->message_queue_head))
1622 msg = (const struct GNUNET_MessageHeader *) &q[1];
1623 msize = ntohs (msg->size);
1624 if (msize + tsize > size)
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1628 "Transmitting message of type %u to client.\n",
1631 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1632 client->message_queue_tail,
1634 memcpy (&cbuf[tsize], msg, msize);
1637 client->message_count--;
1641 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1642 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1644 GNUNET_TIME_UNIT_FOREVER_REL,
1645 &transmit_to_client_callback,
1647 GNUNET_assert (client->th != NULL);
1654 * Convert an address to a string.
1656 * @param plugin name of the plugin responsible for the address
1657 * @param addr binary address
1658 * @param addr_len number of bytes in addr
1659 * @return NULL on error, otherwise address string
1662 a2s (const char *plugin,
1666 struct TransportPlugin *p;
1670 p = find_transport (plugin);
1673 return p->api->address_to_string (p->api->cls,
1680 * Mark the given FAL entry as 'connected' (and hence preferred for
1681 * sending); also mark all others for the same peer as 'not connected'
1682 * (since only one can be preferred).
1684 * @param fal address to set to 'connected'
1687 mark_address_connected (struct ForeignAddressList *fal)
1689 struct ForeignAddressList *pos;
1692 GNUNET_assert (GNUNET_YES == fal->validated);
1693 if (fal->connected == GNUNET_YES)
1694 return; /* nothing to do */
1696 pos = fal->ready_list->addresses;
1699 if (GNUNET_YES == pos->connected)
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1703 "Marking address `%s' as no longer connected (due to connect on other address)\n",
1704 a2s (pos->ready_list->plugin->short_name,
1708 GNUNET_break (cnt == GNUNET_YES);
1710 pos->connected = GNUNET_NO;
1711 GNUNET_STATISTICS_update (stats,
1712 gettext_noop ("# connected addresses"),
1718 fal->connected = GNUNET_YES;
1719 if (GNUNET_YES == cnt)
1721 GNUNET_STATISTICS_update (stats,
1722 gettext_noop ("# connected addresses"),
1730 * Send the specified message to the specified client. Since multiple
1731 * messages may be pending for the same client at a time, this code
1732 * makes sure that no message is lost.
1734 * @param client client to transmit the message to
1735 * @param msg the message to send
1736 * @param may_drop can this message be dropped if the
1737 * message queue for this client is getting far too large?
1740 transmit_to_client (struct TransportClient *client,
1741 const struct GNUNET_MessageHeader *msg, int may_drop)
1743 struct ClientMessageQueueEntry *q;
1746 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1748 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1750 ("Dropping message of type %u and size %u, have %u messages pending (%u is the soft limit)\n"),
1753 client->message_count,
1755 GNUNET_STATISTICS_update (stats,
1756 gettext_noop ("# messages dropped due to slow client"),
1761 msize = ntohs (msg->size);
1762 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1763 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1764 memcpy (&q[1], msg, msize);
1765 GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1766 client->message_queue_tail,
1767 client->message_queue_tail,
1769 client->message_count++;
1770 if (client->th == NULL)
1772 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1774 GNUNET_TIME_UNIT_FOREVER_REL,
1775 &transmit_to_client_callback,
1777 GNUNET_assert (client->th != NULL);
1783 * Transmit a 'SEND_OK' notification to the given client for the
1786 * @param client who to notify
1787 * @param n neighbour to notify about, can be NULL (on failure)
1788 * @param target target of the transmission
1789 * @param result status code for the transmission request
1792 transmit_send_ok (struct TransportClient *client,
1793 struct NeighbourList *n,
1794 const struct GNUNET_PeerIdentity *target,
1797 struct SendOkMessage send_ok_msg;
1799 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1800 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1801 send_ok_msg.success = htonl (result);
1803 send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1805 send_ok_msg.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
1806 send_ok_msg.peer = *target;
1807 transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1812 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1813 * upon "completion" of a send request. This tells the API
1814 * that it is now legal to send another message to the given
1817 * @param cls closure, identifies the entry on the
1818 * message queue that was transmitted and the
1819 * client responsible for queuing the message
1820 * @param target the peer receiving the message
1821 * @param result GNUNET_OK on success, if the transmission
1822 * failed, we should not tell the client to transmit
1826 transmit_send_continuation (void *cls,
1827 const struct GNUNET_PeerIdentity *target,
1830 struct MessageQueue *mq = cls;
1831 struct NeighbourList *n;
1833 GNUNET_STATISTICS_update (stats,
1834 gettext_noop ("# bytes pending with plugins"),
1835 - (int64_t) mq->message_buf_size,
1837 if (result == GNUNET_OK)
1839 GNUNET_STATISTICS_update (stats,
1840 gettext_noop ("# bytes successfully transmitted by plugins"),
1841 mq->message_buf_size,
1846 GNUNET_STATISTICS_update (stats,
1847 gettext_noop ("# bytes with transmission failure by plugins"),
1848 mq->message_buf_size,
1851 if (mq->specific_address != NULL)
1853 if (result == GNUNET_OK)
1855 mq->specific_address->timeout =
1856 GNUNET_TIME_relative_to_absolute
1857 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1858 if (mq->specific_address->validated == GNUNET_YES)
1859 mark_address_connected (mq->specific_address);
1863 if (mq->specific_address->connected != GNUNET_NO)
1866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1867 "Marking address `%s' as no longer connected (due to transmission problem)\n",
1868 a2s (mq->specific_address->ready_list->plugin->short_name,
1869 mq->specific_address->addr,
1870 mq->specific_address->addrlen));
1872 GNUNET_STATISTICS_update (stats,
1873 gettext_noop ("# connected addresses"),
1876 mq->specific_address->connected = GNUNET_NO;
1879 if (! mq->internal_msg)
1880 mq->specific_address->in_transmit = GNUNET_NO;
1882 n = find_neighbour(&mq->neighbour_id);
1883 if (mq->client != NULL)
1884 transmit_send_ok (mq->client, n, target, result);
1887 try_transmission_to_peer (n);
1892 * Find an address in any of the available transports for
1893 * the given neighbour that would be good for message
1894 * transmission. This is essentially the transport selection
1897 * @param neighbour for whom to select an address
1898 * @return selected address, NULL if we have none
1900 struct ForeignAddressList *
1901 find_ready_address(struct NeighbourList *neighbour)
1903 struct ReadyList *head = neighbour->plugins;
1904 struct ForeignAddressList *addresses;
1905 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1906 struct ForeignAddressList *best_address;
1908 /* Hack to prefer unix domain sockets */
1909 struct ForeignAddressList *unix_address = NULL;
1911 best_address = NULL;
1912 while (head != NULL)
1914 addresses = head->addresses;
1915 while (addresses != NULL)
1917 if ( (addresses->timeout.abs_value < now.abs_value) &&
1918 (addresses->connected == GNUNET_YES) )
1921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1922 "Marking long-time inactive connection to `%4s' as down.\n",
1923 GNUNET_i2s (&neighbour->id));
1925 GNUNET_STATISTICS_update (stats,
1926 gettext_noop ("# connected addresses"),
1929 addresses->connected = GNUNET_NO;
1931 addresses = addresses->next;
1934 addresses = head->addresses;
1935 while (addresses != NULL)
1937 #if DEBUG_TRANSPORT > 1
1938 if (addresses->addr != NULL)
1939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1940 "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
1941 a2s (head->plugin->short_name,
1943 addresses->addrlen),
1944 GNUNET_i2s (&neighbour->id),
1945 addresses->connected,
1946 addresses->in_transmit,
1947 addresses->validated,
1948 addresses->connect_attempts,
1949 (unsigned long long) addresses->timeout.abs_value,
1950 (unsigned int) addresses->distance);
1952 if (0==strcmp(head->plugin->short_name,"unix"))
1954 if ((unix_address == NULL) || ((unix_address != NULL) &&
1955 (addresses->latency.rel_value < unix_address->latency.rel_value)))
1956 unix_address = addresses;
1958 if ( ( (best_address == NULL) ||
1959 (addresses->connected == GNUNET_YES) ||
1960 (best_address->connected == GNUNET_NO) ) &&
1961 (addresses->in_transmit == GNUNET_NO) &&
1962 ( (best_address == NULL) ||
1963 (addresses->latency.rel_value < best_address->latency.rel_value)) )
1964 best_address = addresses;
1965 /* FIXME: also give lower-latency addresses that are not
1966 connected a chance some times... */
1967 addresses = addresses->next;
1969 if (unix_address != NULL)
1973 if (unix_address != NULL)
1975 best_address = unix_address;
1977 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found unix address, forced this address\n");
1980 if (best_address != NULL)
1984 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1985 "Best address found (`%s') has latency of %llu ms.\n",
1986 (best_address->addrlen > 0)
1987 ? a2s (best_address->ready_list->plugin->short_name,
1989 best_address->addrlen)
1991 best_address->latency.rel_value);
1996 GNUNET_STATISTICS_update (stats,
1997 gettext_noop ("# transmission attempts failed (no address)"),
2002 return best_address;
2008 * We should re-try transmitting to the given peer,
2009 * hopefully we've learned something in the meantime.
2012 retry_transmission_task (void *cls,
2013 const struct GNUNET_SCHEDULER_TaskContext *tc)
2015 struct NeighbourList *n = cls;
2017 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
2018 try_transmission_to_peer (n);
2023 * Check the ready list for the given neighbour and if a plugin is
2024 * ready for transmission (and if we have a message), do so!
2026 * @param neighbour target peer for which to transmit
2029 try_transmission_to_peer (struct NeighbourList *n)
2031 struct ReadyList *rl;
2032 struct MessageQueue *mq;
2033 struct GNUNET_TIME_Relative timeout;
2037 if (n->messages_head == NULL)
2040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2041 "Transmission queue for `%4s' is empty\n",
2042 GNUNET_i2s (&n->id));
2044 return; /* nothing to do */
2047 mq = n->messages_head;
2048 force_address = GNUNET_YES;
2049 if (mq->specific_address == NULL)
2052 mq->specific_address = ats_get_preferred_address(n);
2053 GNUNET_STATISTICS_update (stats,
2054 gettext_noop ("# transport selected peer address freely"),
2057 force_address = GNUNET_NO;
2059 if (mq->specific_address == NULL)
2061 GNUNET_STATISTICS_update (stats,
2062 gettext_noop ("# transport failed to selected peer address"),
2065 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
2066 if (timeout.rel_value == 0)
2069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2070 "No destination address available to transmit message of size %u to peer `%4s'\n",
2071 mq->message_buf_size,
2072 GNUNET_i2s (&mq->neighbour_id));
2074 GNUNET_STATISTICS_update (stats,
2075 gettext_noop ("# bytes in message queue for other peers"),
2076 - (int64_t) mq->message_buf_size,
2078 GNUNET_STATISTICS_update (stats,
2079 gettext_noop ("# bytes discarded (no destination address available)"),
2080 mq->message_buf_size,
2082 if (mq->client != NULL)
2083 transmit_send_ok (mq->client, n, &n->id, GNUNET_NO);
2084 GNUNET_CONTAINER_DLL_remove (n->messages_head,
2088 return; /* nobody ready */
2090 GNUNET_STATISTICS_update (stats,
2091 gettext_noop ("# message delivery deferred (no address)"),
2094 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
2095 GNUNET_SCHEDULER_cancel (n->retry_task);
2096 n->retry_task = GNUNET_SCHEDULER_add_delayed (timeout,
2097 &retry_transmission_task,
2100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2101 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
2102 mq->message_buf_size,
2103 GNUNET_i2s (&mq->neighbour_id),
2106 /* FIXME: might want to trigger peerinfo lookup here
2107 (unless that's already pending...) */
2110 GNUNET_CONTAINER_DLL_remove (n->messages_head,
2113 if (mq->specific_address->connected == GNUNET_NO)
2114 mq->specific_address->connect_attempts++;
2115 rl = mq->specific_address->ready_list;
2116 mq->plugin = rl->plugin;
2117 if (!mq->internal_msg)
2118 mq->specific_address->in_transmit = GNUNET_YES;
2120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2121 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
2122 mq->message_buf_size,
2123 GNUNET_i2s (&n->id),
2124 (mq->specific_address->addr != NULL)
2125 ? a2s (mq->plugin->short_name,
2126 mq->specific_address->addr,
2127 mq->specific_address->addrlen)
2129 rl->plugin->short_name);
2131 GNUNET_STATISTICS_update (stats,
2132 gettext_noop ("# bytes in message queue for other peers"),
2133 - (int64_t) mq->message_buf_size,
2135 GNUNET_STATISTICS_update (stats,
2136 gettext_noop ("# bytes pending with plugins"),
2137 mq->message_buf_size,
2139 ret = rl->plugin->api->send (rl->plugin->api->cls,
2142 mq->message_buf_size,
2144 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2145 mq->specific_address->session,
2146 mq->specific_address->addr,
2147 mq->specific_address->addrlen,
2149 &transmit_send_continuation, mq);
2152 /* failure, but 'send' would not call continuation in this case,
2153 so we need to do it here! */
2154 transmit_send_continuation (mq,
2162 * Send the specified message to the specified peer.
2164 * @param client source of the transmission request (can be NULL)
2165 * @param peer_address ForeignAddressList where we should send this message
2166 * @param priority how important is the message
2167 * @param timeout how long do we have to transmit?
2168 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
2169 * @param message_buf_size total size of all messages in message_buf
2170 * @param is_internal is this an internal message; these are pre-pended and
2171 * also do not count for plugins being "ready" to transmit
2172 * @param neighbour handle to the neighbour for transmission
2175 transmit_to_peer (struct TransportClient *client,
2176 struct ForeignAddressList *peer_address,
2177 unsigned int priority,
2178 struct GNUNET_TIME_Relative timeout,
2179 const char *message_buf,
2180 size_t message_buf_size,
2181 int is_internal, struct NeighbourList *neighbour)
2183 struct MessageQueue *mq;
2188 /* check for duplicate submission */
2189 mq = neighbour->messages_head;
2192 if (mq->client == client)
2194 /* client transmitted to same peer twice
2195 before getting SEND_OK! */
2203 GNUNET_STATISTICS_update (stats,
2204 gettext_noop ("# bytes in message queue for other peers"),
2207 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
2208 mq->specific_address = peer_address;
2209 mq->client = client;
2210 /* FIXME: this memcpy can be up to 7% of our total runtime! */
2211 memcpy (&mq[1], message_buf, message_buf_size);
2212 mq->message_buf = (const char*) &mq[1];
2213 mq->message_buf_size = message_buf_size;
2214 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
2215 mq->internal_msg = is_internal;
2216 mq->priority = priority;
2217 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
2219 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
2220 neighbour->messages_tail,
2223 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
2224 neighbour->messages_tail,
2225 neighbour->messages_tail,
2227 try_transmission_to_peer (neighbour);
2234 struct GeneratorContext
2236 struct TransportPlugin *plug_pos;
2237 struct OwnAddressList *addr_pos;
2238 struct GNUNET_TIME_Absolute expiration;
2246 address_generator (void *cls, size_t max, void *buf)
2248 struct GeneratorContext *gc = cls;
2251 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
2253 gc->plug_pos = gc->plug_pos->next;
2254 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
2256 if (NULL == gc->plug_pos)
2261 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
2264 gc->addr_pos->addrlen, buf, max);
2265 gc->addr_pos = gc->addr_pos->next;
2271 * Construct our HELLO message from all of the addresses of
2272 * all of the transports.
2277 struct GNUNET_HELLO_Message *hello;
2278 struct TransportClient *cpos;
2279 struct NeighbourList *npos;
2280 struct GeneratorContext gc;
2282 gc.plug_pos = plugins;
2283 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
2284 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2285 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2288 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
2290 GNUNET_STATISTICS_update (stats,
2291 gettext_noop ("# refreshed my HELLO"),
2295 while (cpos != NULL)
2297 transmit_to_client (cpos,
2298 (const struct GNUNET_MessageHeader *) hello,
2303 GNUNET_free_non_null (our_hello);
2305 GNUNET_PEERINFO_add_peer (peerinfo, our_hello);
2307 while (npos != NULL)
2310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2311 "Transmitting updated `%s' to neighbour `%4s'\n",
2312 "HELLO", GNUNET_i2s (&npos->id));
2314 GNUNET_STATISTICS_update (stats,
2315 gettext_noop ("# transmitted my HELLO to other peers"),
2318 transmit_to_peer (NULL, NULL, 0,
2319 HELLO_ADDRESS_EXPIRATION,
2320 (const char *) our_hello,
2321 GNUNET_HELLO_size(our_hello),
2329 * Task used to clean up expired addresses for a plugin.
2331 * @param cls closure
2335 expire_address_task (void *cls,
2336 const struct GNUNET_SCHEDULER_TaskContext *tc);
2340 * Update the list of addresses for this plugin,
2341 * expiring those that are past their expiration date.
2343 * @param plugin addresses of which plugin should be recomputed?
2344 * @param fresh set to GNUNET_YES if a new address was added
2345 * and we need to regenerate the HELLO even if nobody
2349 update_addresses (struct TransportPlugin *plugin,
2352 static struct GNUNET_TIME_Absolute last_update;
2353 struct GNUNET_TIME_Relative min_remaining;
2354 struct GNUNET_TIME_Relative remaining;
2355 struct GNUNET_TIME_Absolute now;
2356 struct OwnAddressList *pos;
2357 struct OwnAddressList *prev;
2358 struct OwnAddressList *next;
2361 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_TASK)
2362 GNUNET_SCHEDULER_cancel (plugin->address_update_task);
2363 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
2364 now = GNUNET_TIME_absolute_get ();
2365 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
2366 expired = (GNUNET_TIME_absolute_get_duration (last_update).rel_value > (HELLO_ADDRESS_EXPIRATION.rel_value / 4));
2368 pos = plugin->addresses;
2372 if (pos->expires.abs_value < now.abs_value)
2374 expired = GNUNET_YES;
2376 plugin->addresses = pos->next;
2378 prev->next = pos->next;
2383 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
2384 if (remaining.rel_value < min_remaining.rel_value)
2385 min_remaining = remaining;
2391 if (expired || fresh)
2396 min_remaining = GNUNET_TIME_relative_min (min_remaining,
2397 GNUNET_TIME_relative_divide (HELLO_ADDRESS_EXPIRATION,
2399 plugin->address_update_task
2400 = GNUNET_SCHEDULER_add_delayed (min_remaining,
2401 &expire_address_task, plugin);
2406 * Task used to clean up expired addresses for a plugin.
2408 * @param cls closure
2412 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2414 struct TransportPlugin *plugin = cls;
2416 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
2417 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2418 update_addresses (plugin, GNUNET_NO);
2423 * Iterator over hash map entries that NULLs the session of validation
2424 * entries that match the given session.
2426 * @param cls closure (the 'struct Session*' to match against)
2427 * @param key current key code (peer ID, not used)
2428 * @param value value in the hash map ('struct ValidationEntry*')
2429 * @return GNUNET_YES (we should continue to iterate)
2432 remove_session_validations (void *cls,
2433 const GNUNET_HashCode * key,
2436 struct Session *session = cls;
2437 struct ValidationEntry *ve = value;
2439 if (session == ve->session)
2446 * We've been disconnected from the other peer (for some
2447 * connection-oriented transport). Either quickly
2448 * re-establish the connection or signal the disconnect
2451 * Only signal CORE level disconnect if ALL addresses
2452 * for the peer are exhausted.
2454 * @param p overall plugin context
2455 * @param nl neighbour that was disconnected
2458 try_fast_reconnect (struct TransportPlugin *p,
2459 struct NeighbourList *nl)
2461 /* FIXME-MW: fast reconnect / transport switching not implemented... */
2462 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2463 "try_fast_reconnect not implemented!\n");
2464 /* Note: the idea here is to hide problems with transports (or
2465 switching between plugins) from the core to eliminate the need to
2466 re-negotiate session keys and the like; OTOH, we should tell core
2467 quickly (much faster than timeout) `if a connection was lost and
2468 could not be re-established (i.e. other peer went down or is
2469 unable / refuses to communicate);
2471 So we should consider:
2472 1) ideally: our own willingness / need to connect
2473 2) prior failures to connect to this peer (by plugin)
2474 3) ideally: reasons why other peer terminated (as far as knowable)
2476 Most importantly, it must be POSSIBLE for another peer to terminate
2477 a connection for a while (without us instantly re-establishing it).
2478 Similarly, if another peer is gone we should quickly notify CORE.
2479 OTOH, if there was a minor glitch (i.e. crash of gnunet-service-transport
2480 on the other end), we should reconnect in such a way that BOTH CORE
2481 services never even notice.
2482 Furthermore, the same mechanism (or small variation) could be used
2483 to switch to a better-performing plugin (ATS).
2485 Finally, this needs to be tested throughly... */
2488 * GNUNET_NO in the call below makes transport disconnect the peer,
2489 * even if only a single address (out of say, six) went away. This
2490 * function must be careful to ONLY disconnect if the peer is gone,
2491 * not just a specifi address.
2493 * More specifically, half the places it was used had it WRONG.
2496 /* No reconnect, signal disconnect instead! */
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2499 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2500 "try_fast_reconnect");
2502 disconnect_neighbour (nl, GNUNET_YES);
2507 * Function that will be called whenever the plugin internally
2508 * cleans up a session pointer and hence the service needs to
2509 * discard all of those sessions as well. Plugins that do not
2510 * use sessions can simply omit calling this function and always
2511 * use NULL wherever a session pointer is needed.
2513 * @param cls closure
2514 * @param peer which peer was the session for
2515 * @param session which session is being destoyed
2518 plugin_env_session_end (void *cls,
2519 const struct GNUNET_PeerIdentity *peer,
2520 struct Session *session)
2522 struct TransportPlugin *p = cls;
2523 struct NeighbourList *nl;
2524 struct ReadyList *rl;
2525 struct ForeignAddressList *pos;
2526 struct ForeignAddressList *prev;
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2530 "Session ended with peer `%4s', %s\n",
2532 "plugin_env_session_end");
2534 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2535 &remove_session_validations,
2537 nl = find_neighbour (peer);
2541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2542 "No neighbour record found for peer `%4s'\n",
2545 return; /* was never marked as connected */
2550 if (rl->plugin == p)
2557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2558 "Plugin was associated with peer `%4s'\n",
2561 disconnect_neighbour (nl, GNUNET_YES);
2565 pos = rl->addresses;
2566 while ( (pos != NULL) &&
2567 (pos->session != session) )
2575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2576 "Session was never marked as ready for peer `%4s'\n",
2579 //FIXME: This conflicts with inbound tcp connections and tcp nat ... debugging in progress
2580 disconnect_neighbour (nl, GNUNET_YES);
2581 return; /* was never marked as connected */
2583 pos->session = NULL;
2584 if (pos->addrlen != 0)
2586 if (nl->received_pong != GNUNET_NO)
2587 try_fast_reconnect (p, nl);
2589 disconnect_neighbour (nl, GNUNET_YES);
2592 /* was inbound connection, free 'pos' */
2594 rl->addresses = pos->next;
2596 prev->next = pos->next;
2597 if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2599 GNUNET_SCHEDULER_cancel (pos->revalidate_task);
2600 pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2602 GNUNET_free_non_null(pos->ressources);
2603 GNUNET_free_non_null(pos->quality);
2605 ats->stat.recreate_problem = GNUNET_YES;
2606 if (nl->received_pong == GNUNET_NO)
2608 disconnect_neighbour (nl, GNUNET_YES);
2609 return; /* nothing to do, never connected... */
2611 /* check if we have any validated addresses left */
2612 pos = rl->addresses;
2617 try_fast_reconnect (p, nl);
2622 /* no valid addresses left, signal disconnect! */
2625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2626 "Disconnecting peer `%4s', %s\n",
2628 "plugin_env_session_end");
2630 /* FIXME: This doesn't mean there are no addresses left for this PEER,
2631 * it means there aren't any left for this PLUGIN/PEER combination! So
2632 * calling disconnect_neighbour here with GNUNET_NO forces disconnect
2633 * when it isn't necessary. Using GNUNET_YES at least checks to see
2634 * if there are any addresses that work first, so as not to overdo it.
2637 disconnect_neighbour (nl, GNUNET_YES);
2642 * Function that must be called by each plugin to notify the
2643 * transport service about the addresses under which the transport
2644 * provided by the plugin can be reached.
2646 * @param cls closure
2647 * @param name name of the transport that generated the address
2648 * @param addr one of the addresses of the host, NULL for the last address
2649 * the specific address format depends on the transport
2650 * @param addrlen length of the address
2651 * @param expires when should this address automatically expire?
2654 plugin_env_notify_address (void *cls,
2658 struct GNUNET_TIME_Relative expires)
2660 struct TransportPlugin *p = cls;
2661 struct OwnAddressList *al;
2662 struct GNUNET_TIME_Absolute abex;
2664 GNUNET_assert (addr != NULL);
2665 abex = GNUNET_TIME_relative_to_absolute (expires);
2666 GNUNET_assert (p == find_transport (name));
2670 if ( (addrlen == al->addrlen) &&
2671 (0 == memcmp (addr, &al[1], addrlen)) )
2674 update_addresses (p, GNUNET_NO);
2679 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2680 al->next = p->addresses;
2683 al->addrlen = addrlen;
2684 memcpy (&al[1], addr, addrlen);
2685 update_addresses (p, GNUNET_YES);
2690 * Notify all of our clients about a peer connecting.
2693 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2694 struct GNUNET_TIME_Relative latency,
2697 struct ConnectInfoMessage * cim;
2698 struct TransportClient *cpos;
2703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2704 "Notifying clients about connection from `%s'\n",
2707 GNUNET_STATISTICS_update (stats,
2708 gettext_noop ("# peers connected"),
2713 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
2714 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2718 cim = GNUNET_malloc (size);
2720 cim->header.size = htons (size);
2721 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2722 cim->ats_count = htonl(2);
2723 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2724 (&(cim->ats))[0].value = htonl (distance);
2725 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
2726 (&(cim->ats))[1].value = htonl ((uint32_t) latency.rel_value);
2727 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2728 (&(cim->ats))[2].value = htonl (0);
2729 memcpy (&cim->id, peer, sizeof (struct GNUNET_PeerIdentity));
2731 /* notify ats about connecting peer */
2732 ats_notify_peer_connect (peer, &(cim->ats), 2);
2735 while (cpos != NULL)
2737 transmit_to_client (cpos, &(cim->header), GNUNET_NO);
2746 * Notify all of our clients about a peer disconnecting.
2749 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2751 struct DisconnectInfoMessage dim;
2752 struct TransportClient *cpos;
2755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2756 "Notifying clients about lost connection to `%s'\n",
2759 GNUNET_STATISTICS_update (stats,
2760 gettext_noop ("# peers connected"),
2763 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2764 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2765 dim.reserved = htonl (0);
2766 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2768 /* notify ats about connecting peer */
2769 ats_notify_peer_disconnect (peer);
2772 while (cpos != NULL)
2774 transmit_to_client (cpos, &dim.header, GNUNET_NO);
2781 * Find a ForeignAddressList entry for the given neighbour
2782 * that matches the given address and transport.
2784 * @param neighbour which peer we care about
2785 * @param tname name of the transport plugin
2786 * @param session session to look for, NULL for 'any'; otherwise
2787 * can be used for the service to "learn" this session ID
2789 * @param addr binary address
2790 * @param addrlen length of addr
2791 * @return NULL if no such entry exists
2793 static struct ForeignAddressList *
2794 find_peer_address(struct NeighbourList *neighbour,
2796 struct Session *session,
2800 struct ReadyList *head;
2801 struct ForeignAddressList *pos;
2803 head = neighbour->plugins;
2804 while (head != NULL)
2806 if (0 == strcmp (tname, head->plugin->short_name))
2812 pos = head->addresses;
2813 while ( (pos != NULL) &&
2814 ( (pos->addrlen != addrlen) ||
2815 (memcmp(pos->addr, addr, addrlen) != 0) ) )
2817 if ( (session != NULL) &&
2818 (pos->session == session) )
2822 if ( (session != NULL) && (pos != NULL) )
2823 pos->session = session; /* learn it! */
2829 * Get the peer address struct for the given neighbour and
2830 * address. If it doesn't yet exist, create it.
2832 * @param neighbour which peer we care about
2833 * @param tname name of the transport plugin
2834 * @param session session of the plugin, or NULL for none
2835 * @param addr binary address
2836 * @param addrlen length of addr
2837 * @return NULL if we do not have a transport plugin for 'tname'
2839 static struct ForeignAddressList *
2840 add_peer_address (struct NeighbourList *neighbour,
2842 struct Session *session,
2846 struct ReadyList *head;
2847 struct ForeignAddressList *ret;
2850 ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2853 head = neighbour->plugins;
2855 while (head != NULL)
2857 if (0 == strcmp (tname, head->plugin->short_name))
2863 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2864 ret->session = session;
2865 if ((addrlen > 0) && (addr != NULL))
2867 ret->addr = (const char*) &ret[1];
2868 memcpy (&ret[1], addr, addrlen);
2875 ret->ressources = GNUNET_malloc(available_ressources * sizeof (struct ATS_ressource_entry));
2876 for (c=0; c<available_ressources; c++)
2878 struct ATS_ressource_entry *r = ret->ressources;
2880 r[c].atis_index = ressources[c].atis_index;
2881 if (0 == strcmp(neighbour->plugins->plugin->short_name,"unix"))
2883 r[c].c = ressources[c].c_unix;
2885 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"udp"))
2887 r[c].c = ressources[c].c_udp;
2889 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"tcp"))
2891 r[c].c = ressources[c].c_tcp;
2893 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"http"))
2895 r[c].c = ressources[c].c_http;
2897 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"https"))
2899 r[c].c = ressources[c].c_https;
2901 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"wlan"))
2903 r[c].c = ressources[c].c_wlan;
2907 r[c].c = ressources[c].c_default;
2908 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2909 "Assigning default cost to peer `%s' addr plugin `%s'! This should not happen!\n",
2910 GNUNET_i2s(&neighbour->peer),
2911 neighbour->plugins->plugin->short_name);
2915 ret->quality = GNUNET_malloc (available_quality_metrics * sizeof (struct ATS_quality_entry));
2916 ret->addrlen = addrlen;
2917 ret->expires = GNUNET_TIME_relative_to_absolute
2918 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2919 ret->latency = GNUNET_TIME_relative_get_forever();
2921 ret->timeout = GNUNET_TIME_relative_to_absolute
2922 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2923 ret->ready_list = head;
2924 ret->next = head->addresses;
2925 head->addresses = ret;
2931 * Closure for 'add_validated_address'.
2933 struct AddValidatedAddressContext
2936 * Entry that has been validated.
2938 const struct ValidationEntry *ve;
2941 * Flag set after we have added the address so
2942 * that we terminate the iteration next time.
2949 * Callback function used to fill a buffer of max bytes with a list of
2950 * addresses in the format used by HELLOs. Should use
2951 * "GNUNET_HELLO_add_address" as a helper function.
2953 * @param cls the 'struct AddValidatedAddressContext' with the validated address
2954 * @param max maximum number of bytes that can be written to buf
2955 * @param buf where to write the address information
2956 * @return number of bytes written, 0 to signal the
2957 * end of the iteration.
2960 add_validated_address (void *cls,
2961 size_t max, void *buf)
2963 struct AddValidatedAddressContext *avac = cls;
2964 const struct ValidationEntry *ve = avac->ve;
2966 if (GNUNET_YES == avac->done)
2968 avac->done = GNUNET_YES;
2969 return GNUNET_HELLO_add_address (ve->transport_name,
2970 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2980 * Closure for 'check_address_exists'.
2982 struct CheckAddressExistsClosure
2985 * Address to check for.
2990 * Name of the transport.
2997 struct Session *session;
3000 * Set to GNUNET_YES if the address exists.
3013 * Iterator over hash map entries. Checks if the given
3014 * validation entry is for the same address as what is given
3017 * @param cls the 'struct CheckAddressExistsClosure*'
3018 * @param key current key code (ignored)
3019 * @param value value in the hash map ('struct ValidationEntry')
3020 * @return GNUNET_YES if we should continue to
3021 * iterate (mismatch), GNUNET_NO if not (entry matched)
3024 check_address_exists (void *cls,
3025 const GNUNET_HashCode * key,
3028 struct CheckAddressExistsClosure *caec = cls;
3029 struct ValidationEntry *ve = value;
3031 if ( (0 == strcmp (caec->tname,
3032 ve->transport_name)) &&
3033 (caec->addrlen == ve->addrlen) &&
3034 (0 == memcmp (caec->addr,
3038 caec->exists = GNUNET_YES;
3041 if ( (ve->session != NULL) &&
3042 (caec->session == ve->session) )
3044 caec->exists = GNUNET_YES;
3053 * Iterator to free entries in the validation_map.
3055 * @param cls closure (unused)
3056 * @param key current key code
3057 * @param value value in the hash map (validation to abort)
3058 * @return GNUNET_YES (always)
3061 abort_validation (void *cls,
3062 const GNUNET_HashCode * key,
3065 struct ValidationEntry *va = value;
3067 if (GNUNET_SCHEDULER_NO_TASK != va->timeout_task)
3068 GNUNET_SCHEDULER_cancel (va->timeout_task);
3069 GNUNET_free (va->transport_name);
3070 if (va->chvc != NULL)
3072 va->chvc->ve_count--;
3073 if (va->chvc->ve_count == 0)
3075 GNUNET_CONTAINER_DLL_remove (chvc_head,
3078 GNUNET_free (va->chvc);
3088 * HELLO validation cleanup task (validation failed).
3090 * @param cls the 'struct ValidationEntry' that failed
3091 * @param tc scheduler context (unused)
3094 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3096 struct ValidationEntry *va = cls;
3097 struct GNUNET_PeerIdentity pid;
3099 va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3100 GNUNET_STATISTICS_update (stats,
3101 gettext_noop ("# address validation timeouts"),
3104 GNUNET_CRYPTO_hash (&va->publicKey,
3106 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3108 GNUNET_break (GNUNET_OK ==
3109 GNUNET_CONTAINER_multihashmap_remove (validation_map,
3112 abort_validation (NULL, NULL, va);
3117 neighbour_timeout_task (void *cls,
3118 const struct GNUNET_SCHEDULER_TaskContext *tc)
3120 struct NeighbourList *n = cls;
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
3124 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
3126 GNUNET_STATISTICS_update (stats,
3127 gettext_noop ("# disconnects due to timeout"),
3130 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3131 disconnect_neighbour (n, GNUNET_NO);
3136 * Schedule the job that will cause us to send a PING to the
3137 * foreign address to evaluate its validity and latency.
3139 * @param fal address to PING
3142 schedule_next_ping (struct ForeignAddressList *fal);
3146 * Add the given address to the list of foreign addresses
3147 * available for the given peer (check for duplicates).
3149 * @param cls the respective 'struct NeighbourList' to update
3150 * @param tname name of the transport
3151 * @param expiration expiration time
3152 * @param addr the address
3153 * @param addrlen length of the address
3154 * @return GNUNET_OK (always)
3157 add_to_foreign_address_list (void *cls,
3159 struct GNUNET_TIME_Absolute expiration,
3163 struct NeighbourList *n = cls;
3164 struct ForeignAddressList *fal;
3167 GNUNET_STATISTICS_update (stats,
3168 gettext_noop ("# valid peer addresses returned by PEERINFO"),
3172 fal = find_peer_address (n, tname, NULL, addr, addrlen);
3175 #if DEBUG_TRANSPORT_HELLO
3176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3177 "Adding address `%s' (%s) for peer `%4s' due to PEERINFO data for %llums.\n",
3178 a2s (tname, addr, addrlen),
3180 GNUNET_i2s (&n->id),
3181 expiration.abs_value);
3183 fal = add_peer_address (n, tname, NULL, addr, addrlen);
3186 GNUNET_STATISTICS_update (stats,
3187 gettext_noop ("# previously validated addresses lacking transport"),
3193 fal->expires = GNUNET_TIME_absolute_max (expiration,
3195 schedule_next_ping (fal);
3201 fal->expires = GNUNET_TIME_absolute_max (expiration,
3207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3208 "Failed to add new address for `%4s'\n",
3209 GNUNET_i2s (&n->id));
3213 if (fal->validated == GNUNET_NO)
3215 fal->validated = GNUNET_YES;
3216 GNUNET_STATISTICS_update (stats,
3217 gettext_noop ("# peer addresses considered valid"),
3221 if (try == GNUNET_YES)
3224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3225 "Have new addresses, will try to trigger transmissions.\n");
3227 try_transmission_to_peer (n);
3234 * Add addresses in validated HELLO "h" to the set of addresses
3235 * we have for this peer.
3237 * @param cls closure ('struct NeighbourList*')
3238 * @param peer id of the peer, NULL for last call
3239 * @param h hello message for the peer (can be NULL)
3240 * @param err_msg NULL if successful, otherwise contains error message
3243 add_hello_for_peer (void *cls,
3244 const struct GNUNET_PeerIdentity *peer,
3245 const struct GNUNET_HELLO_Message *h,
3246 const char *err_msg)
3248 struct NeighbourList *n = cls;
3250 if (err_msg != NULL)
3253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3254 _("Error in communication with PEERINFO service: %s\n"),
3261 GNUNET_STATISTICS_update (stats,
3262 gettext_noop ("# outstanding peerinfo iterate requests"),
3269 return; /* no HELLO available */
3271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3272 "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
3276 if (GNUNET_YES != n->public_key_valid)
3278 GNUNET_HELLO_get_key (h, &n->publicKey);
3279 n->public_key_valid = GNUNET_YES;
3281 GNUNET_HELLO_iterate_addresses (h,
3283 &add_to_foreign_address_list,
3289 * Create a fresh entry in our neighbour list for the given peer.
3290 * Will try to transmit our current HELLO to the new neighbour.
3291 * Do not call this function directly, use 'setup_peer_check_blacklist.
3293 * @param peer the peer for which we create the entry
3294 * @param do_hello should we schedule transmitting a HELLO
3295 * @return the new neighbour list entry
3297 static struct NeighbourList *
3298 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
3301 struct NeighbourList *n;
3302 struct TransportPlugin *tp;
3303 struct ReadyList *rl;
3306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3307 "Setting up state for neighbour `%4s'\n",
3310 GNUNET_assert (our_hello != NULL);
3311 GNUNET_STATISTICS_update (stats,
3312 gettext_noop ("# active neighbours"),
3315 n = GNUNET_malloc (sizeof (struct NeighbourList));
3316 n->next = neighbours;
3320 GNUNET_TIME_relative_to_absolute
3321 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3322 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
3323 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3324 MAX_BANDWIDTH_CARRY_S);
3328 if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
3330 rl = GNUNET_malloc (sizeof (struct ReadyList));
3332 rl->next = n->plugins;
3335 rl->addresses = NULL;
3339 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
3341 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
3342 &neighbour_timeout_task, n);
3345 GNUNET_STATISTICS_update (stats,
3346 gettext_noop ("# peerinfo new neighbor iterate requests"),
3349 GNUNET_STATISTICS_update (stats,
3350 gettext_noop ("# outstanding peerinfo iterate requests"),
3353 n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
3354 GNUNET_TIME_UNIT_FOREVER_REL,
3355 &add_hello_for_peer, n);
3357 GNUNET_STATISTICS_update (stats,
3358 gettext_noop ("# HELLO's sent to new neighbors"),
3361 transmit_to_peer (NULL, NULL, 0,
3362 HELLO_ADDRESS_EXPIRATION,
3363 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
3371 * Function called after we have checked if communicating
3372 * with a given peer is acceptable.
3374 * @param cls closure
3375 * @param n NULL if communication is not acceptable
3377 typedef void (*SetupContinuation)(void *cls,
3378 struct NeighbourList *n);
3382 * Information kept for each client registered to perform
3388 * This is a linked list.
3390 struct Blacklisters *next;
3393 * This is a linked list.
3395 struct Blacklisters *prev;
3398 * Client responsible for this entry.
3400 struct GNUNET_SERVER_Client *client;
3403 * Blacklist check that we're currently performing.
3405 struct BlacklistCheck *bc;
3411 * Head of DLL of blacklisting clients.
3413 static struct Blacklisters *bl_head;
3416 * Tail of DLL of blacklisting clients.
3418 static struct Blacklisters *bl_tail;
3422 * Context we use when performing a blacklist check.
3424 struct BlacklistCheck
3428 * This is a linked list.
3430 struct BlacklistCheck *next;
3433 * This is a linked list.
3435 struct BlacklistCheck *prev;
3438 * Peer being checked.
3440 struct GNUNET_PeerIdentity peer;
3443 * Option for setup neighbour afterwards.
3448 * Continuation to call with the result.
3450 SetupContinuation cont;
3458 * Current transmission request handle for this client, or NULL if no
3459 * request is pending.
3461 struct GNUNET_CONNECTION_TransmitHandle *th;
3464 * Our current position in the blacklisters list.
3466 struct Blacklisters *bl_pos;
3469 * Current task performing the check.
3471 GNUNET_SCHEDULER_TaskIdentifier task;
3476 * Head of DLL of active blacklisting queries.
3478 static struct BlacklistCheck *bc_head;
3481 * Tail of DLL of active blacklisting queries.
3483 static struct BlacklistCheck *bc_tail;
3487 * Perform next action in the blacklist check.
3489 * @param cls the 'struct BlacklistCheck*'
3493 do_blacklist_check (void *cls,
3494 const struct GNUNET_SCHEDULER_TaskContext *tc);
3497 * Transmit blacklist query to the client.
3499 * @param cls the 'struct BlacklistCheck'
3500 * @param size number of bytes allowed
3501 * @param buf where to copy the message
3502 * @return number of bytes copied to buf
3505 transmit_blacklist_message (void *cls,
3509 struct BlacklistCheck *bc = cls;
3510 struct Blacklisters *bl;
3511 struct BlacklistMessage bm;
3516 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
3517 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3519 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3520 "Failed to send blacklist test for peer `%s' to client\n",
3521 GNUNET_i2s (&bc->peer));
3525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3526 "Sending blacklist test for peer `%s' to client\n",
3527 GNUNET_i2s (&bc->peer));
3530 bm.header.size = htons (sizeof (struct BlacklistMessage));
3531 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
3532 bm.is_allowed = htonl (0);
3534 memcpy (buf, &bm, sizeof (bm));
3535 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
3541 * Perform next action in the blacklist check.
3543 * @param cls the 'struct BlacklistCheck*'
3547 do_blacklist_check (void *cls,
3548 const struct GNUNET_SCHEDULER_TaskContext *tc)
3550 struct BlacklistCheck *bc = cls;
3551 struct Blacklisters *bl;
3553 bc->task = GNUNET_SCHEDULER_NO_TASK;
3558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3559 "No blacklist clients active, will now setup neighbour record for peer `%s'\n",
3560 GNUNET_i2s (&bc->peer));
3562 bc->cont (bc->cont_cls,
3563 setup_new_neighbour (&bc->peer, bc->do_hello));
3570 bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
3571 sizeof (struct BlacklistMessage),
3572 GNUNET_TIME_UNIT_FOREVER_REL,
3573 &transmit_blacklist_message,
3580 * Obtain a 'struct NeighbourList' for the given peer. If such an entry
3581 * does not yet exist, check the blacklist. If the blacklist says creating
3582 * one is acceptable, create one and call the continuation; otherwise
3583 * call the continuation with NULL.
3585 * @param peer peer to setup or look up a struct NeighbourList for
3586 * @param do_hello should we also schedule sending our HELLO to the peer
3587 * if this is a new record
3588 * @param cont function to call with the 'struct NeigbhbourList*'
3589 * @param cont_cls closure for cont
3592 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
3594 SetupContinuation cont,
3597 struct NeighbourList *n;
3598 struct BlacklistCheck *bc;
3600 n = find_neighbour(peer);
3604 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3605 "Neighbour record exists for peer `%s'\n",
3612 if (bl_head == NULL)
3615 cont (cont_cls, setup_new_neighbour (peer, do_hello));
3617 setup_new_neighbour(peer, do_hello);
3620 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3621 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3623 bc->do_hello = do_hello;
3625 bc->cont_cls = cont_cls;
3626 bc->bl_pos = bl_head;
3627 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3633 * Function called with the result of querying a new blacklister about
3634 * it being allowed (or not) to continue to talk to an existing neighbour.
3636 * @param cls the original 'struct NeighbourList'
3637 * @param n NULL if we need to disconnect
3640 confirm_or_drop_neighbour (void *cls,
3641 struct NeighbourList *n)
3643 struct NeighbourList * orig = cls;
3648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3649 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&orig->id),
3650 "confirm_or_drop_neighboUr");
3652 disconnect_neighbour (orig, GNUNET_NO);
3658 * Handle a request to start a blacklist.
3660 * @param cls closure (always NULL)
3661 * @param client identification of the client
3662 * @param message the actual message
3665 handle_blacklist_init (void *cls,
3666 struct GNUNET_SERVER_Client *client,
3667 const struct GNUNET_MessageHeader *message)
3669 struct Blacklisters *bl;
3670 struct BlacklistCheck *bc;
3671 struct NeighbourList *n;
3676 if (bl->client == client)
3679 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3684 bl = GNUNET_malloc (sizeof (struct Blacklisters));
3685 bl->client = client;
3686 GNUNET_SERVER_client_keep (client);
3687 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
3688 /* confirm that all existing connections are OK! */
3692 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3693 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3695 bc->do_hello = GNUNET_NO;
3696 bc->cont = &confirm_or_drop_neighbour;
3699 if (n == neighbours) /* all would wait for the same client, no need to
3700 create more than just the first task right now */
3701 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3709 * Handle a request to blacklist a peer.
3711 * @param cls closure (always NULL)
3712 * @param client identification of the client
3713 * @param message the actual message
3716 handle_blacklist_reply (void *cls,
3717 struct GNUNET_SERVER_Client *client,
3718 const struct GNUNET_MessageHeader *message)
3720 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
3721 struct Blacklisters *bl;
3722 struct BlacklistCheck *bc;
3725 while ( (bl != NULL) &&
3726 (bl->client != client) )
3731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3732 "Blacklist client disconnected\n");
3734 /* FIXME: other error handling here!? */
3735 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3740 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
3743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3744 "Blacklist check failed, peer not allowed\n");
3746 bc->cont (bc->cont_cls, NULL);
3747 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
3753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3754 "Blacklist check succeeded, continuing with checks\n");
3756 bc->bl_pos = bc->bl_pos->next;
3757 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3760 /* check if any other bc's are waiting for this blacklister */
3764 if ( (bc->bl_pos == bl) &&
3765 (GNUNET_SCHEDULER_NO_TASK == bc->task) )
3766 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3774 * Send periodic PING messages to a given foreign address.
3776 * @param cls our 'struct PeriodicValidationContext*'
3777 * @param tc task context
3780 send_periodic_ping (void *cls,
3781 const struct GNUNET_SCHEDULER_TaskContext *tc)
3783 struct ForeignAddressList *peer_address = cls;
3784 struct TransportPlugin *tp;
3785 struct ValidationEntry *va;
3786 struct NeighbourList *neighbour;
3787 struct TransportPingMessage ping;
3788 struct CheckAddressExistsClosure caec;
3790 uint16_t hello_size;
3794 peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3795 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3797 tp = peer_address->ready_list->plugin;
3798 neighbour = peer_address->ready_list->neighbour;
3799 if (GNUNET_YES != neighbour->public_key_valid)
3801 /* no public key yet, try again later */
3802 schedule_next_ping (peer_address);
3805 caec.addr = peer_address->addr;
3806 caec.addrlen = peer_address->addrlen;
3807 caec.tname = tp->short_name;
3808 caec.session = peer_address->session;
3809 caec.exists = GNUNET_NO;
3810 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3811 &check_address_exists,
3813 if (caec.exists == GNUNET_YES)
3815 /* During validation attempts we will likely trigger the other
3816 peer trying to validate our address which in turn will cause
3817 it to send us its HELLO, so we expect to hit this case rather
3818 frequently. Only print something if we are very verbose. */
3819 #if DEBUG_TRANSPORT > 1
3820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3821 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3822 (peer_address->addr != NULL)
3823 ? a2s (tp->short_name,
3825 peer_address->addrlen)
3828 GNUNET_i2s (&neighbour->id));
3830 schedule_next_ping (peer_address);
3833 va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3834 va->transport_name = GNUNET_strdup (tp->short_name);
3835 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
3837 va->send_time = GNUNET_TIME_absolute_get();
3838 va->session = peer_address->session;
3839 if (peer_address->addr != NULL)
3841 va->addr = (const void*) &va[1];
3842 memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3843 va->addrlen = peer_address->addrlen;
3845 memcpy(&va->publicKey,
3846 &neighbour->publicKey,
3847 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3849 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
3850 &timeout_hello_validation,
3852 GNUNET_CONTAINER_multihashmap_put (validation_map,
3853 &neighbour->id.hashPubKey,
3855 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3857 if (peer_address->validated != GNUNET_YES)
3858 hello_size = GNUNET_HELLO_size(our_hello);
3862 tsize = sizeof(struct TransportPingMessage) + hello_size;
3864 if (peer_address->addr != NULL)
3866 slen = strlen (tp->short_name) + 1;
3867 tsize += slen + peer_address->addrlen;
3871 slen = 0; /* make gcc happy */
3873 message_buf = GNUNET_malloc(tsize);
3874 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3875 ping.challenge = htonl(va->challenge);
3876 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3877 if (peer_address->validated != GNUNET_YES)
3879 memcpy(message_buf, our_hello, hello_size);
3882 if (peer_address->addr != NULL)
3884 ping.header.size = htons(sizeof(struct TransportPingMessage) +
3885 peer_address->addrlen +
3887 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3890 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3892 peer_address->addrlen);
3896 ping.header.size = htons(sizeof(struct TransportPingMessage));
3899 memcpy(&message_buf[hello_size],
3901 sizeof(struct TransportPingMessage));
3903 #if DEBUG_TRANSPORT_REVALIDATION
3904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3905 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s'\n",
3906 (peer_address->addr != NULL)
3907 ? a2s (peer_address->plugin->short_name,
3909 peer_address->addrlen)
3912 GNUNET_i2s (&neighbour->id),
3913 "HELLO", hello_size,
3916 if (peer_address->validated != GNUNET_YES)
3917 GNUNET_STATISTICS_update (stats,
3918 gettext_noop ("# PING with HELLO messages sent"),
3922 GNUNET_STATISTICS_update (stats,
3923 gettext_noop ("# PING without HELLO messages sent"),
3926 GNUNET_STATISTICS_update (stats,
3927 gettext_noop ("# PING messages sent for re-validation"),
3930 transmit_to_peer (NULL, peer_address,
3931 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3932 HELLO_VERIFICATION_TIMEOUT,
3934 GNUNET_YES, neighbour);
3935 GNUNET_free(message_buf);
3936 schedule_next_ping (peer_address);
3941 * Schedule the job that will cause us to send a PING to the
3942 * foreign address to evaluate its validity and latency.
3944 * @param fal address to PING
3947 schedule_next_ping (struct ForeignAddressList *fal)
3949 struct GNUNET_TIME_Relative delay;
3951 if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3953 delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3954 delay.rel_value /= 2; /* do before expiration */
3955 delay = GNUNET_TIME_relative_min (delay,
3956 LATENCY_EVALUATION_MAX_DELAY);
3957 if (GNUNET_YES != fal->estimated)
3959 delay = GNUNET_TIME_UNIT_ZERO;
3960 fal->estimated = GNUNET_YES;
3962 if (GNUNET_YES == fal->connected)
3964 delay = GNUNET_TIME_relative_min (delay,
3965 CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3967 /* FIXME: also adjust delay based on how close the last
3968 observed latency is to the latency of the best alternative */
3969 /* bound how fast we can go */
3970 delay = GNUNET_TIME_relative_max (delay,
3971 GNUNET_TIME_UNIT_SECONDS);
3972 /* randomize a bit (to avoid doing all at the same time) */
3973 delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3974 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(delay,
3975 &send_periodic_ping,
3983 * Function that will be called if we receive some payload
3984 * from another peer.
3986 * @param message the payload
3987 * @param n peer who claimed to be the sender
3990 handle_payload_message (const struct GNUNET_MessageHeader *message,
3991 struct NeighbourList *n)
3993 struct InboundMessage *im;
3994 struct TransportClient *cpos;
3997 msize = ntohs (message->size);
3998 if (n->received_pong == GNUNET_NO)
4001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4002 "Received message of type %u and size %u from `%4s', but no pong yet!!\n",
4003 ntohs (message->type),
4004 ntohs (message->size),
4005 GNUNET_i2s (&n->id));
4007 GNUNET_free_non_null (n->pre_connect_message_buffer);
4008 n->pre_connect_message_buffer = GNUNET_malloc (msize);
4009 memcpy (n->pre_connect_message_buffer, message, msize);
4014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4015 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
4016 ntohs (message->type),
4017 ntohs (message->size),
4018 GNUNET_i2s (&n->id));
4020 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
4023 n->quota_violation_count++;
4025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4026 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
4027 n->in_tracker.available_bytes_per_s__,
4028 n->quota_violation_count);
4030 /* Discount 32k per violation */
4031 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
4036 if (n->quota_violation_count > 0)
4038 /* try to add 32k back */
4039 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
4041 n->quota_violation_count--;
4044 GNUNET_STATISTICS_update (stats,
4045 gettext_noop ("# payload received from other peers"),
4048 /* transmit message to all clients */
4049 uint32_t ats_count = 2;
4050 size_t size = sizeof (struct InboundMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) + msize;
4051 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
4054 im = GNUNET_malloc (size);
4055 im->header.size = htons (size);
4056 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
4058 im->ats_count = htonl(ats_count);
4059 /* Setting ATS data */
4060 (&(im->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
4061 (&(im->ats))[0].value = htonl (n->distance);
4062 (&(im->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
4063 (&(im->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
4064 (&(im->ats))[ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
4065 (&(im->ats))[ats_count].value = htonl (0);
4067 memcpy (&((&(im->ats))[ats_count+1]), message, msize);
4069 while (cpos != NULL)
4071 transmit_to_client (cpos, &im->header, GNUNET_YES);
4079 * Iterator over hash map entries. Checks if the given validation
4080 * entry is for the same challenge as what is given in the PONG.
4082 * @param cls the 'struct TransportPongMessage*'
4083 * @param key peer identity
4084 * @param value value in the hash map ('struct ValidationEntry')
4085 * @return GNUNET_YES if we should continue to
4086 * iterate (mismatch), GNUNET_NO if not (entry matched)
4089 check_pending_validation (void *cls,
4090 const GNUNET_HashCode * key,
4093 const struct TransportPongMessage *pong = cls;
4094 struct ValidationEntry *ve = value;
4095 struct AddValidatedAddressContext avac;
4096 unsigned int challenge = ntohl(pong->challenge);
4097 struct GNUNET_HELLO_Message *hello;
4098 struct GNUNET_PeerIdentity target;
4099 struct NeighbourList *n;
4100 struct ForeignAddressList *fal;
4101 struct OwnAddressList *oal;
4102 struct TransportPlugin *tp;
4103 struct GNUNET_MessageHeader *prem;
4109 ps = ntohs (pong->header.size);
4110 if (ps < sizeof (struct TransportPongMessage))
4112 GNUNET_break_op (0);
4115 addr = (const char*) &pong[1];
4116 slen = strlen (ve->transport_name) + 1;
4117 if ( (ps - sizeof (struct TransportPongMessage) < slen) ||
4118 (ve->challenge != challenge) ||
4119 (addr[slen-1] != '\0') ||
4120 (0 != strcmp (addr, ve->transport_name)) ||
4121 (ntohl (pong->purpose.size)
4122 != sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4124 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
4125 sizeof (struct GNUNET_PeerIdentity) + ps - sizeof (struct TransportPongMessage)) )
4130 alen = ps - sizeof (struct TransportPongMessage) - slen;
4131 switch (ntohl (pong->purpose.purpose))
4133 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN:
4134 if ( (ve->addrlen + slen != ntohl (pong->addrlen)) ||
4135 (0 != memcmp (&addr[slen],
4139 return GNUNET_YES; /* different entry, keep trying! */
4141 if (0 != memcmp (&pong->pid,
4143 sizeof (struct GNUNET_PeerIdentity)))
4145 GNUNET_break_op (0);
4149 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
4154 GNUNET_break_op (0);
4159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4160 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
4162 a2s (ve->transport_name,
4163 (const struct sockaddr *) ve->addr,
4165 ve->transport_name);
4168 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING:
4169 if (0 != memcmp (&pong->pid,
4171 sizeof (struct GNUNET_PeerIdentity)))
4174 GNUNET_asprintf(&peer, "%s",GNUNET_i2s (&pong->pid));
4176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4177 "Received PONG for different identity: I am `%s', PONG identity: `%s'\n",
4178 GNUNET_i2s (&my_identity),
4184 if (ve->addrlen != 0)
4186 /* must have been for a different validation entry */
4189 tp = find_transport (ve->transport_name);
4195 oal = tp->addresses;
4198 if ( (oal->addrlen == alen) &&
4199 (0 == memcmp (&oal[1],
4207 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4208 _("Not accepting PONG with address `%s' since I cannot confirm having this address.\n"),
4209 a2s (ve->transport_name,
4215 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING,
4220 GNUNET_break_op (0);
4225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4226 "Confirmed that peer `%4s' is talking to us using address `%s' (%s) for us.\n",
4228 a2s (ve->transport_name,
4231 ve->transport_name);
4235 GNUNET_break_op (0);
4238 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
4240 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4241 _("Received expired signature. Check system time.\n"));
4244 GNUNET_STATISTICS_update (stats,
4245 gettext_noop ("# address validation successes"),
4248 /* create the updated HELLO */
4249 GNUNET_CRYPTO_hash (&ve->publicKey,
4250 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4251 &target.hashPubKey);
4252 if (ve->addr != NULL)
4254 avac.done = GNUNET_NO;
4256 hello = GNUNET_HELLO_create (&ve->publicKey,
4257 &add_validated_address,
4259 GNUNET_PEERINFO_add_peer (peerinfo,
4261 GNUNET_free (hello);
4263 n = find_neighbour (&target);
4266 n->publicKey = ve->publicKey;
4267 n->public_key_valid = GNUNET_YES;
4268 fal = add_peer_address (n,
4273 GNUNET_assert (fal != NULL);
4274 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
4275 fal->validated = GNUNET_YES;
4276 mark_address_connected (fal);
4277 GNUNET_STATISTICS_update (stats,
4278 gettext_noop ("# peer addresses considered valid"),
4281 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
4282 update_addr_value (fal, GNUNET_TIME_absolute_get_duration (ve->send_time).rel_value, GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
4284 schedule_next_ping (fal);
4285 if (n->latency.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
4286 n->latency = fal->latency;
4288 n->latency.rel_value = (fal->latency.rel_value + n->latency.rel_value) / 2;
4290 n->distance = fal->distance;
4291 if (GNUNET_NO == n->received_pong)
4293 n->received_pong = GNUNET_YES;
4295 notify_clients_connect (&target, n->latency, n->distance);
4296 if (NULL != (prem = n->pre_connect_message_buffer))
4298 n->pre_connect_message_buffer = NULL;
4299 handle_payload_message (prem, n);
4303 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4305 GNUNET_SCHEDULER_cancel (n->retry_task);
4306 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4307 try_transmission_to_peer (n);
4311 /* clean up validation entry */
4312 GNUNET_assert (GNUNET_YES ==
4313 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4316 abort_validation (NULL, NULL, ve);
4322 * Function that will be called if we receive a validation
4323 * of an address challenge that we transmitted to another
4324 * peer. Note that the validation should only be considered
4325 * acceptable if the challenge matches AND if the sender
4326 * address is at least a plausible address for this peer
4327 * (otherwise we may be seeing a MiM attack).
4329 * @param cls closure
4330 * @param message the pong message
4331 * @param peer who responded to our challenge
4332 * @param sender_address string describing our sender address (as observed
4333 * by the other peer in binary format)
4334 * @param sender_address_len number of bytes in 'sender_address'
4337 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
4338 const struct GNUNET_PeerIdentity *peer,
4339 const char *sender_address,
4340 size_t sender_address_len)
4342 #if DEBUG_TRANSPORT > 1
4343 /* we get tons of these that just get discarded, only log
4344 if we are quite verbose */
4345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4346 "Receiving `%s' message from `%4s'.\n", "PONG",
4349 GNUNET_STATISTICS_update (stats,
4350 gettext_noop ("# PONG messages received"),
4353 if (GNUNET_SYSERR !=
4354 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
4356 &check_pending_validation,
4359 /* This is *expected* to happen a lot since we send
4360 PONGs to *all* known addresses of the sender of
4361 the PING, so most likely we get multiple PONGs
4362 per PING, and all but the first PONG will end up
4363 here. So really we should not print anything here
4364 unless we want to be very, very verbose... */
4365 #if DEBUG_TRANSPORT > 2
4366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4367 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
4379 * Try to validate a neighbour's address by sending him our HELLO and a PING.
4381 * @param cls the 'struct ValidationEntry*'
4382 * @param neighbour neighbour to validate, NULL if validation failed
4385 transmit_hello_and_ping (void *cls,
4386 struct NeighbourList *neighbour)
4388 struct ValidationEntry *va = cls;
4389 struct ForeignAddressList *peer_address;
4390 struct TransportPingMessage ping;
4391 uint16_t hello_size;
4394 struct GNUNET_PeerIdentity id;
4397 GNUNET_CRYPTO_hash (&va->publicKey,
4398 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4400 if (neighbour == NULL)
4402 /* FIXME: stats... */
4403 GNUNET_break (GNUNET_OK ==
4404 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4407 abort_validation (NULL, NULL, va);
4410 neighbour->publicKey = va->publicKey;
4411 neighbour->public_key_valid = GNUNET_YES;
4412 peer_address = add_peer_address (neighbour,
4413 va->transport_name, NULL,
4414 (const void*) &va[1],
4416 if (peer_address == NULL)
4418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4419 "Failed to add peer `%4s' for plugin `%s'\n",
4420 GNUNET_i2s (&neighbour->id),
4421 va->transport_name);
4422 GNUNET_break (GNUNET_OK ==
4423 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4426 abort_validation (NULL, NULL, va);
4429 hello_size = GNUNET_HELLO_size(our_hello);
4430 slen = strlen(va->transport_name) + 1;
4431 tsize = sizeof(struct TransportPingMessage) + hello_size + va->addrlen + slen;
4432 message_buf = GNUNET_malloc(tsize);
4433 ping.challenge = htonl(va->challenge);
4434 ping.header.size = htons(sizeof(struct TransportPingMessage) + slen + va->addrlen);
4435 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
4436 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
4437 memcpy(message_buf, our_hello, hello_size);
4438 memcpy(&message_buf[hello_size],
4440 sizeof(struct TransportPingMessage));
4441 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
4444 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
4448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4449 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
4452 : a2s (va->transport_name,
4453 (const void*) &va[1], va->addrlen),
4455 GNUNET_i2s (&neighbour->id),
4456 "HELLO", hello_size,
4457 "PING", sizeof (struct TransportPingMessage) + va->addrlen + slen);
4460 GNUNET_STATISTICS_update (stats,
4461 gettext_noop ("# PING messages sent for initial validation"),
4464 transmit_to_peer (NULL, peer_address,
4465 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
4466 HELLO_VERIFICATION_TIMEOUT,
4468 GNUNET_YES, neighbour);
4469 GNUNET_free(message_buf);
4474 * Check if the given address is already being validated; if not,
4475 * append the given address to the list of entries that are being be
4476 * validated and initiate validation.
4478 * @param cls closure ('struct CheckHelloValidatedContext *')
4479 * @param tname name of the transport
4480 * @param expiration expiration time
4481 * @param addr the address
4482 * @param addrlen length of the address
4483 * @return GNUNET_OK (always)
4486 run_validation (void *cls,
4488 struct GNUNET_TIME_Absolute expiration,
4492 struct CheckHelloValidatedContext *chvc = cls;
4493 struct GNUNET_PeerIdentity id;
4494 struct TransportPlugin *tp;
4495 struct ValidationEntry *va;
4496 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4497 struct CheckAddressExistsClosure caec;
4498 struct OwnAddressList *oal;
4500 GNUNET_assert (addr != NULL);
4502 GNUNET_STATISTICS_update (stats,
4503 gettext_noop ("# peer addresses scheduled for validation"),
4506 tp = find_transport (tname);
4509 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
4510 GNUNET_ERROR_TYPE_BULK,
4512 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
4514 GNUNET_STATISTICS_update (stats,
4515 gettext_noop ("# peer addresses not validated (plugin not available)"),
4520 /* check if this is one of our own addresses */
4521 oal = tp->addresses;
4524 if ( (oal->addrlen == addrlen) &&
4525 (0 == memcmp (&oal[1],
4529 /* not plausible, this address is equivalent to our own address! */
4530 GNUNET_STATISTICS_update (stats,
4531 gettext_noop ("# peer addresses not validated (loopback)"),
4538 GNUNET_HELLO_get_key (chvc->hello, &pk);
4539 GNUNET_CRYPTO_hash (&pk,
4541 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4544 if (is_blacklisted(&id, tp))
4547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4548 "Attempted to validate blacklisted peer `%s' using `%s'!\n",
4556 caec.addrlen = addrlen;
4557 caec.session = NULL;
4559 caec.exists = GNUNET_NO;
4560 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
4561 &check_address_exists,
4563 if (caec.exists == GNUNET_YES)
4565 /* During validation attempts we will likely trigger the other
4566 peer trying to validate our address which in turn will cause
4567 it to send us its HELLO, so we expect to hit this case rather
4568 frequently. Only print something if we are very verbose. */
4569 #if DEBUG_TRANSPORT > 1
4570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4571 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
4572 a2s (tname, addr, addrlen),
4576 GNUNET_STATISTICS_update (stats,
4577 gettext_noop ("# peer addresses not validated (in progress)"),
4582 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
4585 va->transport_name = GNUNET_strdup (tname);
4586 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
4588 va->send_time = GNUNET_TIME_absolute_get();
4589 va->addr = (const void*) &va[1];
4590 memcpy (&va[1], addr, addrlen);
4591 va->addrlen = addrlen;
4592 GNUNET_HELLO_get_key (chvc->hello,
4594 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
4595 &timeout_hello_validation,
4597 GNUNET_CONTAINER_multihashmap_put (validation_map,
4600 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4601 setup_peer_check_blacklist (&id, GNUNET_NO,
4602 &transmit_hello_and_ping,
4609 * Check if addresses in validated hello "h" overlap with
4610 * those in "chvc->hello" and validate the rest.
4612 * @param cls closure
4613 * @param peer id of the peer, NULL for last call
4614 * @param h hello message for the peer (can be NULL)
4615 * @param err_msg NULL if successful, otherwise contains error message
4618 check_hello_validated (void *cls,
4619 const struct GNUNET_PeerIdentity *peer,
4620 const struct GNUNET_HELLO_Message *h,
4621 const char *err_msg)
4623 struct CheckHelloValidatedContext *chvc = cls;
4624 struct GNUNET_HELLO_Message *plain_hello;
4625 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4626 struct GNUNET_PeerIdentity target;
4627 struct NeighbourList *n;
4629 if (err_msg != NULL)
4632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4633 _("Error in communication with PEERINFO service: %s\n"),
4641 GNUNET_STATISTICS_update (stats,
4642 gettext_noop ("# outstanding peerinfo iterate requests"),
4646 if (GNUNET_NO == chvc->hello_known)
4648 /* notify PEERINFO about the peer now, so that we at least
4649 have the public key if some other component needs it */
4650 GNUNET_HELLO_get_key (chvc->hello, &pk);
4651 GNUNET_CRYPTO_hash (&pk,
4652 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4653 &target.hashPubKey);
4654 plain_hello = GNUNET_HELLO_create (&pk,
4657 GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
4658 GNUNET_free (plain_hello);
4659 #if DEBUG_TRANSPORT_HELLO
4660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4661 "PEERINFO had no `%s' message for peer `%4s', full validation needed.\n",
4663 GNUNET_i2s (&target));
4665 GNUNET_STATISTICS_update (stats,
4666 gettext_noop ("# new HELLOs requiring full validation"),
4669 GNUNET_HELLO_iterate_addresses (chvc->hello,
4676 GNUNET_STATISTICS_update (stats,
4677 gettext_noop ("# duplicate HELLO (peer known)"),
4682 if (chvc->ve_count == 0)
4684 GNUNET_CONTAINER_DLL_remove (chvc_head,
4693 #if DEBUG_TRANSPORT_HELLO
4694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4695 "PEERINFO had `%s' message for peer `%4s', validating only new addresses.\n",
4699 chvc->hello_known = GNUNET_YES;
4700 n = find_neighbour (peer);
4703 #if DEBUG_TRANSPORT_HELLO
4704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4705 "Calling hello_iterate_addresses for %s!\n",
4708 GNUNET_HELLO_iterate_addresses (h,
4710 &add_to_foreign_address_list,
4712 try_transmission_to_peer (n);
4716 #if DEBUG_TRANSPORT_HELLO
4717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4718 "No existing neighbor record for %s!\n",
4721 GNUNET_STATISTICS_update (stats,
4722 gettext_noop ("# no existing neighbour record (validating HELLO)"),
4726 GNUNET_STATISTICS_update (stats,
4727 gettext_noop ("# HELLO validations (update case)"),
4730 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
4732 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
4739 * Process HELLO-message.
4741 * @param plugin transport involved, may be NULL
4742 * @param message the actual message
4743 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
4746 process_hello (struct TransportPlugin *plugin,
4747 const struct GNUNET_MessageHeader *message)
4750 struct GNUNET_PeerIdentity target;
4751 const struct GNUNET_HELLO_Message *hello;
4752 struct CheckHelloValidatedContext *chvc;
4753 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
4754 #if DEBUG_TRANSPORT_HELLO > 2
4757 hsize = ntohs (message->size);
4758 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
4759 (hsize < sizeof (struct GNUNET_MessageHeader)))
4762 return GNUNET_SYSERR;
4764 GNUNET_STATISTICS_update (stats,
4765 gettext_noop ("# HELLOs received for validation"),
4769 /* first, check if load is too high */
4770 if (GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
4772 GNUNET_STATISTICS_update (stats,
4773 gettext_noop ("# HELLOs ignored due to high load"),
4776 #if DEBUG_TRANSPORT_HELLO
4777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4778 "Ignoring `%s' for `%4s', load too high.\n",
4780 GNUNET_i2s (&target));
4784 hello = (const struct GNUNET_HELLO_Message *) message;
4785 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
4787 #if DEBUG_TRANSPORT_HELLO
4788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4789 "Unable to get public key from `%s' for `%4s'!\n",
4791 GNUNET_i2s (&target));
4793 GNUNET_break_op (0);
4794 return GNUNET_SYSERR;
4797 GNUNET_CRYPTO_hash (&publicKey,
4798 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4799 &target.hashPubKey);
4801 #if DEBUG_TRANSPORT_HELLO
4802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4803 "Received `%s' message for `%4s'\n",
4805 GNUNET_i2s (&target));
4808 if (0 == memcmp (&my_identity,
4810 sizeof (struct GNUNET_PeerIdentity)))
4812 GNUNET_STATISTICS_update (stats,
4813 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
4819 while (NULL != chvc)
4821 if (GNUNET_HELLO_equals (hello,
4823 GNUNET_TIME_absolute_get ()).abs_value > 0)
4825 #if DEBUG_TRANSPORT_HELLO > 2
4826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4827 "Received duplicate `%s' message for `%4s'; ignored\n",
4829 GNUNET_i2s (&target));
4831 return GNUNET_OK; /* validation already pending */
4833 if (GNUNET_HELLO_size(hello) == GNUNET_HELLO_size (chvc->hello))
4834 GNUNET_break (0 != memcmp (hello, chvc->hello,
4835 GNUNET_HELLO_size(hello)));
4840 struct NeighbourList *temp_neighbor = find_neighbour(&target);
4841 if ((NULL != temp_neighbor))
4843 fprintf(stderr, "Already know peer, ignoring hello\n");
4848 #if DEBUG_TRANSPORT_HELLO > 2
4851 my_id = GNUNET_strdup(GNUNET_i2s(plugin->env.my_identity));
4853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4854 "%s: Starting validation of `%s' message for `%4s' via '%s' of size %u\n",
4857 GNUNET_i2s (&target),
4859 GNUNET_HELLO_size(hello));
4864 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
4866 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
4867 memcpy (&chvc[1], hello, hsize);
4868 GNUNET_CONTAINER_DLL_insert (chvc_head,
4871 /* finally, check if HELLO was previously validated
4872 (continuation will then schedule actual validation) */
4873 GNUNET_STATISTICS_update (stats,
4874 gettext_noop ("# peerinfo process hello iterate requests"),
4877 GNUNET_STATISTICS_update (stats,
4878 gettext_noop ("# outstanding peerinfo iterate requests"),
4881 chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
4883 HELLO_VERIFICATION_TIMEOUT,
4884 &check_hello_validated, chvc);
4890 * The peer specified by the given neighbour has timed-out or a plugin
4891 * has disconnected. We may either need to do nothing (other plugins
4892 * still up), or trigger a full disconnect and clean up. This
4893 * function updates our state and does the necessary notifications.
4894 * Also notifies our clients that the neighbour is now officially
4897 * @param n the neighbour list entry for the peer
4898 * @param check GNUNET_YES to check if ALL addresses for this peer
4899 * are gone, GNUNET_NO to force a disconnect of the peer
4900 * regardless of whether other addresses exist.
4903 disconnect_neighbour (struct NeighbourList *n, int check)
4905 struct ReadyList *rpos;
4906 struct NeighbourList *npos;
4907 struct NeighbourList *nprev;
4908 struct MessageQueue *mq;
4909 struct ForeignAddressList *peer_addresses;
4910 struct ForeignAddressList *peer_pos;
4912 if (GNUNET_YES == check)
4915 while (NULL != rpos)
4917 peer_addresses = rpos->addresses;
4918 while (peer_addresses != NULL)
4920 // Do not disconnect if: an address is connected or an inbound address exists
4921 if ((GNUNET_YES == peer_addresses->connected) || (peer_addresses->addrlen == 0))
4924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4925 "NOT Disconnecting from `%4s', still have live addresses!\n",
4926 GNUNET_i2s (&n->id));
4928 return; /* still connected */
4930 peer_addresses = peer_addresses->next;
4936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4937 "Disconnecting from `%4s'\n",
4938 GNUNET_i2s (&n->id));
4940 /* remove n from neighbours list */
4943 while ((npos != NULL) && (npos != n))
4948 GNUNET_assert (npos != NULL);
4950 neighbours = n->next;
4952 nprev->next = n->next;
4954 /* notify all clients about disconnect */
4955 if (GNUNET_YES == n->received_pong)
4956 notify_clients_disconnect (&n->id);
4958 /* clean up all plugins, cancel connections and pending transmissions */
4959 while (NULL != (rpos = n->plugins))
4961 n->plugins = rpos->next;
4962 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
4963 while (rpos->addresses != NULL)
4965 peer_pos = rpos->addresses;
4966 rpos->addresses = peer_pos->next;
4967 if (peer_pos->connected == GNUNET_YES)
4968 GNUNET_STATISTICS_update (stats,
4969 gettext_noop ("# connected addresses"),
4972 if (GNUNET_YES == peer_pos->validated)
4973 GNUNET_STATISTICS_update (stats,
4974 gettext_noop ("# peer addresses considered valid"),
4977 if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
4979 GNUNET_SCHEDULER_cancel (peer_pos->revalidate_task);
4980 peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
4982 GNUNET_free(peer_pos->ressources);
4983 peer_pos->ressources = NULL;
4984 GNUNET_free(peer_pos->quality);
4985 peer_pos->ressources = NULL;
4986 GNUNET_free(peer_pos);
4987 ats->stat.recreate_problem = GNUNET_YES;
4992 /* free all messages on the queue */
4993 while (NULL != (mq = n->messages_head))
4995 GNUNET_STATISTICS_update (stats,
4996 gettext_noop ("# bytes in message queue for other peers"),
4997 - (int64_t) mq->message_buf_size,
4999 GNUNET_STATISTICS_update (stats,
5000 gettext_noop ("# bytes discarded due to disconnect"),
5001 mq->message_buf_size,
5003 GNUNET_CONTAINER_DLL_remove (n->messages_head,
5006 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
5008 sizeof(struct GNUNET_PeerIdentity)));
5011 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
5013 GNUNET_SCHEDULER_cancel (n->timeout_task);
5014 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
5016 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
5018 GNUNET_SCHEDULER_cancel (n->retry_task);
5019 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
5021 if (n->piter != NULL)
5023 GNUNET_PEERINFO_iterate_cancel (n->piter);
5024 GNUNET_STATISTICS_update (stats,
5025 gettext_noop ("# outstanding peerinfo iterate requests"),
5030 /* finally, free n itself */
5031 GNUNET_STATISTICS_update (stats,
5032 gettext_noop ("# active neighbours"),
5035 GNUNET_free_non_null (n->pre_connect_message_buffer);
5041 * We have received a PING message from someone. Need to send a PONG message
5042 * in response to the peer by any means necessary.
5045 handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
5046 const struct GNUNET_PeerIdentity *peer,
5047 struct Session *session,
5048 const char *sender_address,
5049 uint16_t sender_address_len)
5051 struct TransportPlugin *plugin = cls;
5052 struct SessionHeader *session_header = (struct SessionHeader*) session;
5053 struct TransportPingMessage *ping;
5054 struct TransportPongMessage *pong;
5055 struct NeighbourList *n;
5056 struct ReadyList *rl;
5057 struct ForeignAddressList *fal;
5058 struct OwnAddressList *oal;
5063 if (ntohs (message->size) < sizeof (struct TransportPingMessage))
5065 GNUNET_break_op (0);
5066 return GNUNET_SYSERR;
5069 ping = (struct TransportPingMessage *) message;
5070 if (0 != memcmp (&ping->target,
5071 plugin->env.my_identity,
5072 sizeof (struct GNUNET_PeerIdentity)))
5075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5076 _("Received `%s' message from `%s' destined for `%s' which is not me!\n"),
5078 (sender_address != NULL)
5079 ? a2s (plugin->short_name,
5080 (const struct sockaddr *)sender_address,
5083 GNUNET_i2s (&ping->target));
5085 return GNUNET_SYSERR;
5088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
5089 "Processing `%s' from `%s'\n",
5091 (sender_address != NULL)
5092 ? a2s (plugin->short_name,
5093 (const struct sockaddr *)sender_address,
5097 GNUNET_STATISTICS_update (stats,
5098 gettext_noop ("# PING messages received"),
5101 addr = (const char*) &ping[1];
5102 alen = ntohs (message->size) - sizeof (struct TransportPingMessage);
5103 slen = strlen (plugin->short_name) + 1;
5106 /* peer wants to confirm that we have an outbound connection to him */
5107 if (session == NULL)
5109 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5110 _("Refusing to create PONG since I do not have a session with `%s'.\n"),
5112 return GNUNET_SYSERR;
5114 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len + slen);
5115 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len + slen);
5116 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
5117 pong->purpose.size =
5118 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
5120 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
5121 sizeof (struct GNUNET_PeerIdentity) + sender_address_len + slen);
5122 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING);
5123 pong->challenge = ping->challenge;
5124 pong->addrlen = htonl(sender_address_len + slen);
5127 sizeof(struct GNUNET_PeerIdentity));
5131 if ((sender_address!=NULL) && (sender_address_len > 0))
5132 memcpy (&((char*)&pong[1])[slen],
5134 sender_address_len);
5135 if (GNUNET_TIME_absolute_get_remaining (session_header->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
5137 /* create / update cached sig */
5139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5140 "Creating PONG signature to indicate active connection.\n");
5142 session_header->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
5143 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
5144 GNUNET_assert (GNUNET_OK ==
5145 GNUNET_CRYPTO_rsa_sign (my_private_key,
5147 &session_header->pong_signature));
5151 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
5153 memcpy (&pong->signature,
5154 &session_header->pong_signature,
5155 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5161 /* peer wants to confirm that this is one of our addresses */
5165 plugin->api->check_address (plugin->api->cls,
5169 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5170 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
5171 a2s (plugin->short_name,
5176 oal = plugin->addresses;
5179 if ( (oal->addrlen == alen) &&
5186 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
5187 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
5188 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
5189 pong->purpose.size =
5190 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
5192 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
5193 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
5194 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
5195 pong->challenge = ping->challenge;
5196 pong->addrlen = htonl(alen + slen);
5199 sizeof(struct GNUNET_PeerIdentity));
5200 memcpy (&pong[1], plugin->short_name, slen);
5201 memcpy (&((char*)&pong[1])[slen], addr, alen);
5202 if ( (oal != NULL) &&
5203 (GNUNET_TIME_absolute_get_remaining (oal->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4) )
5205 /* create / update cached sig */
5207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5208 "Creating PONG signature to indicate ownership.\n");
5210 oal->pong_sig_expires = GNUNET_TIME_absolute_min (oal->expires,
5211 GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
5212 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5213 GNUNET_assert (GNUNET_OK ==
5214 GNUNET_CRYPTO_rsa_sign (my_private_key,
5216 &oal->pong_signature));
5217 memcpy (&pong->signature,
5218 &oal->pong_signature,
5219 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5221 else if (oal == NULL)
5223 /* not using cache (typically DV-only) */
5224 pong->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
5225 GNUNET_assert (GNUNET_OK ==
5226 GNUNET_CRYPTO_rsa_sign (my_private_key,
5232 /* can used cached version */
5233 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5234 memcpy (&pong->signature,
5235 &oal->pong_signature,
5236 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5239 n = find_neighbour(peer);
5240 GNUNET_assert (n != NULL);
5241 /* first try reliable response transmission */
5245 fal = rl->addresses;
5248 if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
5251 ntohs (pong->header.size),
5252 TRANSPORT_PONG_PRIORITY,
5253 HELLO_VERIFICATION_TIMEOUT,
5261 GNUNET_STATISTICS_update (stats,
5262 gettext_noop ("# PONGs unicast via reliable transport"),
5272 /* no reliable method found, do multicast */
5273 GNUNET_STATISTICS_update (stats,
5274 gettext_noop ("# PONGs multicast to all available addresses"),
5280 fal = rl->addresses;
5283 transmit_to_peer(NULL, fal,
5284 TRANSPORT_PONG_PRIORITY,
5285 HELLO_VERIFICATION_TIMEOUT,
5287 ntohs(pong->header.size),
5303 * Function called by the plugin for each received message.
5304 * Update data volumes, possibly notify plugins about
5305 * reducing the rate at which they read from the socket
5306 * and generally forward to our receive callback.
5308 * @param cls the "struct TransportPlugin *" we gave to the plugin
5309 * @param peer (claimed) identity of the other peer
5310 * @param message the message, NULL if we only care about
5311 * learning about the delay until we should receive again
5312 * @param ats_data information for automatic transport selection
5313 * @param ats_count number of elements in ats not including 0-terminator
5314 * @param session identifier used for this session (can be NULL)
5315 * @param sender_address binary address of the sender (if observed)
5316 * @param sender_address_len number of bytes in sender_address
5317 * @return how long in ms the plugin should wait until receiving more data
5318 * (plugins that do not support this, can ignore the return value)
5320 static struct GNUNET_TIME_Relative
5321 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
5322 const struct GNUNET_MessageHeader *message,
5323 const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
5325 struct Session *session,
5326 const char *sender_address,
5327 uint16_t sender_address_len)
5329 struct TransportPlugin *plugin = cls;
5330 struct ReadyList *service_context;
5331 struct ForeignAddressList *peer_address;
5333 struct NeighbourList *n;
5334 struct GNUNET_TIME_Relative ret;
5335 if (is_blacklisted (peer, plugin))
5336 return GNUNET_TIME_UNIT_FOREVER_REL;
5340 n = find_neighbour (peer);
5342 n = setup_new_neighbour (peer, GNUNET_YES);
5343 service_context = n->plugins;
5344 while ((service_context != NULL) && (plugin != service_context->plugin))
5345 service_context = service_context->next;
5346 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
5347 peer_address = NULL;
5350 for (c=0; c<ats_count; c++)
5352 if (ntohl(ats_data[c].type) == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
5354 distance = ntohl(ats_data[c].value);
5358 /* notify ATS about incoming data */
5359 //ats_notify_ats_data(peer, ats_data);
5362 if (message != NULL)
5364 if ( (session != NULL) ||
5365 (sender_address != NULL) )
5366 peer_address = add_peer_address (n,
5370 sender_address_len);
5371 if (peer_address != NULL)
5374 update_addr_ats(peer_address, ats_data, ats_count);
5375 update_addr_value(peer_address, distance, GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5377 peer_address->distance = distance;
5378 if (GNUNET_YES == peer_address->validated)
5379 mark_address_connected (peer_address);
5380 peer_address->timeout
5382 GNUNET_TIME_relative_to_absolute
5383 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5384 schedule_next_ping (peer_address);
5386 /* update traffic received amount ... */
5387 msize = ntohs (message->size);
5389 GNUNET_STATISTICS_update (stats,
5390 gettext_noop ("# bytes received from other peers"),
5393 n->distance = distance;
5395 GNUNET_TIME_relative_to_absolute
5396 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5397 GNUNET_SCHEDULER_cancel (n->timeout_task);
5399 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
5400 &neighbour_timeout_task, n);
5401 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
5403 /* dropping message due to frequent inbound volume violations! */
5404 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
5405 GNUNET_ERROR_TYPE_BULK,
5407 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
5408 n->in_tracker.available_bytes_per_s__,
5409 n->quota_violation_count);
5410 GNUNET_STATISTICS_update (stats,
5411 gettext_noop ("# bandwidth quota violations by other peers"),
5414 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
5416 if ((ntohs(message->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ATS) &&
5417 (ntohs(message->size) == (sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t))))
5419 uint32_t value = ntohl(*((uint32_t *) &message[1]));
5420 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GNUNET_MESSAGE_TYPE_TRANSPORT_ATS: %i \n", value);
5421 /* Force ressource and quality update */
5424 ats->stat.modified_resources = GNUNET_YES;
5425 ats->stat.modified_quality = GNUNET_YES;
5427 /* Force cost update */
5429 ats->stat.modified_resources = GNUNET_YES;
5430 /* Force quality update */
5432 ats->stat.modified_quality = GNUNET_YES;
5433 /* Force full rebuild */
5435 ats->stat.recreate_problem = GNUNET_YES;
5439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5440 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
5441 ntohs (message->type),
5442 ntohs (message->size),
5445 switch (ntohs (message->type))
5447 case GNUNET_MESSAGE_TYPE_HELLO:
5448 GNUNET_STATISTICS_update (stats,
5449 gettext_noop ("# HELLO messages received from other peers"),
5452 process_hello (plugin, message);
5454 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
5455 handle_ping (plugin, message, peer, session, sender_address, sender_address_len);
5457 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
5458 handle_pong (plugin, message, peer, sender_address, sender_address_len);
5460 case GNUNET_MESSAGE_TYPE_TRANSPORT_ATS:
5463 handle_payload_message (message, n);
5467 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
5468 if (ret.rel_value > 0)
5471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5472 "Throttling read (%llu bytes excess at %u b/s), waiting %llums before reading more.\n",
5473 (unsigned long long) n->in_tracker.consumption_since_last_update__,
5474 (unsigned int) n->in_tracker.available_bytes_per_s__,
5475 (unsigned long long) ret.rel_value);
5477 GNUNET_STATISTICS_update (stats,
5478 gettext_noop ("# ms throttling suggested"),
5479 (int64_t) ret.rel_value,
5486 * Handle START-message. This is the first message sent to us
5487 * by any client which causes us to add it to our list.
5489 * @param cls closure (always NULL)
5490 * @param client identification of the client
5491 * @param message the actual message
5494 handle_start (void *cls,
5495 struct GNUNET_SERVER_Client *client,
5496 const struct GNUNET_MessageHeader *message)
5498 const struct StartMessage *start;
5499 struct TransportClient *c;
5500 struct ConnectInfoMessage * cim;
5501 struct NeighbourList *n;
5505 start = (const struct StartMessage*) message;
5507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5508 "Received `%s' request from client\n", "START");
5513 if (c->client == client)
5515 /* client already on our list! */
5517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5522 if ( (GNUNET_NO != ntohl (start->do_check)) &&
5523 (0 != memcmp (&start->self,
5525 sizeof (struct GNUNET_PeerIdentity))) )
5527 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5528 _("Rejecting control connection from peer `%s', which is not me!\n"),
5529 GNUNET_i2s (&start->self));
5530 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5533 c = GNUNET_malloc (sizeof (struct TransportClient));
5537 if (our_hello != NULL)
5540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5541 "Sending our own `%s' to new client\n", "HELLO");
5543 transmit_to_client (c,
5544 (const struct GNUNET_MessageHeader *) our_hello,
5546 /* tell new client about all existing connections */
5548 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
5549 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
5553 cim = GNUNET_malloc (size);
5554 cim->header.size = htons (size);
5555 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
5556 cim->ats_count = htonl(ats_count);
5557 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
5558 (&(cim->ats))[2].value = htonl (0);
5562 if (GNUNET_YES == n->received_pong)
5564 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5565 (&(cim->ats))[0].value = htonl (n->distance);
5566 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
5567 (&(cim->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
5569 transmit_to_client (c, &cim->header, GNUNET_NO);
5575 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5580 * Handle HELLO-message.
5582 * @param cls closure (always NULL)
5583 * @param client identification of the client
5584 * @param message the actual message
5587 handle_hello (void *cls,
5588 struct GNUNET_SERVER_Client *client,
5589 const struct GNUNET_MessageHeader *message)
5593 GNUNET_STATISTICS_update (stats,
5594 gettext_noop ("# HELLOs received from clients"),
5597 ret = process_hello (NULL, message);
5598 GNUNET_SERVER_receive_done (client, ret);
5603 * Closure for 'transmit_client_message'; followed by
5604 * 'msize' bytes of the actual message.
5606 struct TransmitClientMessageContext
5609 * Client on whom's behalf we are sending.
5611 struct GNUNET_SERVER_Client *client;
5614 * Timeout for the transmission.
5616 struct GNUNET_TIME_Absolute timeout;
5624 * Size of the message in bytes.
5631 * Schedule transmission of a message we got from a client to a peer.
5633 * @param cls the 'struct TransmitClientMessageContext*'
5634 * @param n destination, or NULL on error (in that case, drop the message)
5637 transmit_client_message (void *cls,
5638 struct NeighbourList *n)
5640 struct TransmitClientMessageContext *tcmc = cls;
5641 struct TransportClient *tc;
5644 while ((tc != NULL) && (tc->client != tcmc->client))
5649 transmit_to_peer (tc, NULL, tcmc->priority,
5650 GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
5652 tcmc->msize, GNUNET_NO, n);
5654 GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
5655 GNUNET_SERVER_client_drop (tcmc->client);
5661 * Handle SEND-message.
5663 * @param cls closure (always NULL)
5664 * @param client identification of the client
5665 * @param message the actual message
5668 handle_send (void *cls,
5669 struct GNUNET_SERVER_Client *client,
5670 const struct GNUNET_MessageHeader *message)
5672 const struct OutboundMessage *obm;
5673 const struct GNUNET_MessageHeader *obmm;
5674 struct TransmitClientMessageContext *tcmc;
5678 size = ntohs (message->size);
5680 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
5683 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5686 GNUNET_STATISTICS_update (stats,
5687 gettext_noop ("# payload received for other peers"),
5690 obm = (const struct OutboundMessage *) message;
5691 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
5692 msize = size - sizeof (struct OutboundMessage);
5694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5695 "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
5696 "SEND", GNUNET_i2s (&obm->peer),
5700 tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
5701 tcmc->client = client;
5702 tcmc->priority = ntohl (obm->priority);
5703 tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
5704 tcmc->msize = msize;
5705 /* FIXME: this memcpy can be up to 7% of our total runtime */
5706 memcpy (&tcmc[1], obmm, msize);
5707 GNUNET_SERVER_client_keep (client);
5708 setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
5709 &transmit_client_message,
5715 * Handle request connect message
5717 * @param cls closure (always NULL)
5718 * @param client identification of the client
5719 * @param message the actual message
5722 handle_request_connect (void *cls,
5723 struct GNUNET_SERVER_Client *client,
5724 const struct GNUNET_MessageHeader *message)
5726 const struct TransportRequestConnectMessage *trcm =
5727 (const struct TransportRequestConnectMessage *) message;
5729 GNUNET_STATISTICS_update (stats,
5730 gettext_noop ("# REQUEST CONNECT messages received"),
5734 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
5735 "Received a request connect message for peer `%s'\n",
5736 GNUNET_i2s(&trcm->peer));
5738 setup_peer_check_blacklist (&trcm->peer, GNUNET_YES,
5740 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5745 * Handle SET_QUOTA-message.
5747 * @param cls closure (always NULL)
5748 * @param client identification of the client
5749 * @param message the actual message
5752 handle_set_quota (void *cls,
5753 struct GNUNET_SERVER_Client *client,
5754 const struct GNUNET_MessageHeader *message)
5756 const struct QuotaSetMessage *qsm =
5757 (const struct QuotaSetMessage *) message;
5758 struct NeighbourList *n;
5760 GNUNET_STATISTICS_update (stats,
5761 gettext_noop ("# SET QUOTA messages received"),
5764 n = find_neighbour (&qsm->peer);
5767 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5768 GNUNET_STATISTICS_update (stats,
5769 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
5775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5776 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
5778 (unsigned int) ntohl (qsm->quota.value__),
5779 (unsigned int) n->in_tracker.available_bytes_per_s__,
5780 GNUNET_i2s (&qsm->peer));
5782 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
5784 if (0 == ntohl (qsm->quota.value__))
5787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5788 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&n->id),
5791 disconnect_neighbour (n, GNUNET_NO);
5793 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5798 * Take the given address and append it to the set of results sent back to
5801 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
5802 * @param address the resolved name, NULL to indicate the last response
5805 transmit_address_to_client (void *cls, const char *address)
5807 struct GNUNET_SERVER_TransmitContext *tc = cls;
5810 if (NULL == address)
5813 slen = strlen (address) + 1;
5815 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
5816 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5817 if (NULL == address)
5818 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
5823 * Handle AddressLookup-message.
5825 * @param cls closure (always NULL)
5826 * @param client identification of the client
5827 * @param message the actual message
5830 handle_address_lookup (void *cls,
5831 struct GNUNET_SERVER_Client *client,
5832 const struct GNUNET_MessageHeader *message)
5834 const struct AddressLookupMessage *alum;
5835 struct TransportPlugin *lsPlugin;
5836 const char *nameTransport;
5837 const char *address;
5839 struct GNUNET_SERVER_TransmitContext *tc;
5840 struct GNUNET_TIME_Absolute timeout;
5841 struct GNUNET_TIME_Relative rtimeout;
5844 size = ntohs (message->size);
5845 if (size < sizeof (struct AddressLookupMessage))
5847 GNUNET_break_op (0);
5848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5851 alum = (const struct AddressLookupMessage *) message;
5852 uint32_t addressLen = ntohl (alum->addrlen);
5853 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
5855 GNUNET_break_op (0);
5856 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5859 address = (const char *) &alum[1];
5860 nameTransport = (const char *) &address[addressLen];
5862 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
5864 GNUNET_break_op (0);
5865 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5868 timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
5869 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5870 numeric = ntohl (alum->numeric_only);
5871 lsPlugin = find_transport (nameTransport);
5872 if (NULL == lsPlugin)
5874 tc = GNUNET_SERVER_transmit_context_create (client);
5875 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5876 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5877 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
5880 tc = GNUNET_SERVER_transmit_context_create (client);
5881 lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
5883 address, addressLen,
5886 &transmit_address_to_client, tc);
5891 * Setup the environment for this plugin.
5894 create_environment (struct TransportPlugin *plug)
5896 plug->env.cfg = cfg;
5897 plug->env.my_identity = &my_identity;
5898 plug->env.our_hello = &our_hello;
5899 plug->env.cls = plug;
5900 plug->env.receive = &plugin_env_receive;
5901 plug->env.notify_address = &plugin_env_notify_address;
5902 plug->env.session_end = &plugin_env_session_end;
5903 plug->env.max_connections = max_connect_per_transport;
5904 plug->env.stats = stats;
5909 * Start the specified transport (load the plugin).
5912 start_transport (struct GNUNET_SERVER_Handle *server,
5915 struct TransportPlugin *plug;
5918 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5919 _("Loading `%s' transport plugin\n"), name);
5920 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
5921 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
5922 create_environment (plug);
5923 plug->short_name = GNUNET_strdup (name);
5924 plug->lib_name = libname;
5925 plug->next = plugins;
5927 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
5928 if (plug->api == NULL)
5930 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5931 _("Failed to load transport plugin for `%s'\n"), name);
5932 GNUNET_free (plug->short_name);
5933 plugins = plug->next;
5934 GNUNET_free (libname);
5941 * Called whenever a client is disconnected. Frees our
5942 * resources associated with that client.
5944 * @param cls closure
5945 * @param client identification of the client
5948 client_disconnect_notification (void *cls,
5949 struct GNUNET_SERVER_Client *client)
5951 struct TransportClient *pos;
5952 struct TransportClient *prev;
5953 struct ClientMessageQueueEntry *mqe;
5954 struct Blacklisters *bl;
5955 struct BlacklistCheck *bc;
5960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
5961 "Client disconnected, cleaning up.\n");
5963 /* clean up blacklister */
5967 if (bl->client == client)
5972 if (bc->bl_pos == bl)
5974 bc->bl_pos = bl->next;
5977 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
5980 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
5981 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
5987 GNUNET_CONTAINER_DLL_remove (bl_head,
5990 GNUNET_SERVER_client_drop (bl->client);
5996 /* clean up 'normal' clients */
5999 while ((pos != NULL) && (pos->client != client))
6006 while (NULL != (mqe = pos->message_queue_head))
6008 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
6009 pos->message_queue_tail,
6011 pos->message_count--;
6015 clients = pos->next;
6017 prev->next = pos->next;
6018 if (GNUNET_YES == pos->tcs_pending)
6023 if (pos->th != NULL)
6025 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
6028 GNUNET_break (0 == pos->message_count);
6034 * Function called when the service shuts down. Unloads our plugins
6035 * and cancels pending validations.
6037 * @param cls closure, unused
6038 * @param tc task context (unused)
6041 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6043 struct TransportPlugin *plug;
6044 struct OwnAddressList *al;
6045 struct CheckHelloValidatedContext *chvc;
6047 while (neighbours != NULL)
6050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6051 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&neighbours->id),
6054 disconnect_neighbour (neighbours, GNUNET_NO);
6057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6058 "Transport service is unloading plugins...\n");
6060 while (NULL != (plug = plugins))
6062 plugins = plug->next;
6063 if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
6065 GNUNET_SCHEDULER_cancel (plug->address_update_task);
6066 plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
6068 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
6069 GNUNET_free (plug->lib_name);
6070 GNUNET_free (plug->short_name);
6071 while (NULL != (al = plug->addresses))
6073 plug->addresses = al->next;
6078 if (my_private_key != NULL)
6079 GNUNET_CRYPTO_rsa_key_free (my_private_key);
6080 GNUNET_free_non_null (our_hello);
6082 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
6085 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6086 validation_map = NULL;
6090 /* free 'chvc' data structure */
6091 while (NULL != (chvc = chvc_head))
6093 chvc_head = chvc->next;
6094 if (chvc->piter != NULL)
6096 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
6097 GNUNET_STATISTICS_update (stats,
6098 gettext_noop ("# outstanding peerinfo iterate requests"),
6105 GNUNET_assert (chvc->ve_count == 0);
6112 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6115 if (peerinfo != NULL)
6117 GNUNET_PEERINFO_disconnect (peerinfo);
6120 /* Can we assume those are gone by now, or do we need to clean up
6122 GNUNET_break (bl_head == NULL);
6123 GNUNET_break (bc_head == NULL);
6127 static int ats_evaluate_results (int result, int solution, char * problem)
6129 int cont = GNUNET_NO;
6130 #if DEBUG_ATS || VERBOSE_ATS
6131 int error_kind = GNUNET_ERROR_TYPE_DEBUG;
6134 error_kind = GNUNET_ERROR_TYPE_ERROR;
6138 case GNUNET_SYSERR : /* GNUNET problem, not GLPK related */
6139 #if DEBUG_ATS || VERBOSE_ATS
6140 GNUNET_log (error_kind, "%s , GLPK solving not executed\n", problem);
6143 case GLP_ESTOP : /* search terminated by application */
6144 #if DEBUG_ATS || VERBOSE_ATS
6145 GNUNET_log (error_kind, "%s , Search terminated by application\n", problem);
6148 case GLP_EITLIM : /* iteration limit exceeded */
6149 #if DEBUG_ATS || VERBOSE_ATS
6150 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s Iteration limit exceeded\n", problem);
6153 case GLP_ETMLIM : /* time limit exceeded */
6154 #if DEBUG_ATS || VERBOSE_ATS
6155 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s Time limit exceeded\n", problem);
6158 case GLP_ENOPFS : /* no primal feasible solution */
6159 case GLP_ENODFS : /* no dual feasible solution */
6160 #if DEBUG_ATS || VERBOSE_ATS
6161 GNUNET_log (error_kind, "%s No feasible solution\n", problem);
6165 case GLP_EBADB : /* invalid basis */
6166 case GLP_ESING : /* singular matrix */
6167 case GLP_ECOND : /* ill-conditioned matrix */
6168 case GLP_EBOUND : /* invalid bounds */
6169 case GLP_EFAIL : /* solver failed */
6170 case GLP_EOBJLL : /* objective lower limit reached */
6171 case GLP_EOBJUL : /* objective upper limit reached */
6172 case GLP_EROOT : /* root LP optimum not provided */
6173 #if DEBUG_ATS || VERBOSE_ATS
6174 GNUNET_log (error_kind, "%s Invalid Input data: %i\n", problem, result);
6179 #if DEBUG_ATS || VERBOSE_ATS
6180 GNUNET_log (error_kind, "%s Problem has been solved\n", problem);
6187 #if DEBUG_ATS || VERBOSE_ATS
6188 GNUNET_log (error_kind, "%s solution is undefined\n", problem);
6192 #if DEBUG_ATS || VERBOSE_ATS
6193 GNUNET_log (error_kind, "%s solution is optimal\n", problem);
6198 #if DEBUG_ATS || VERBOSE_ATS
6199 GNUNET_log (error_kind, "%s solution is %s feasible, however, its optimality (or non-optimality) has not been proven, \n", problem, (0==strcmp(problem,"LP")?"":"integer"));
6204 #if DEBUG_ATS || VERBOSE_ATS
6205 GNUNET_log (error_kind, "%s problem has no %sfeasible solution\n", problem, (0==strcmp(problem,"LP")?"":"integer "));
6209 #if DEBUG_ATS || VERBOSE_ATS
6210 GNUNET_log (error_kind, "%s problem is infeasible \n", problem);
6214 #if DEBUG_ATS || VERBOSE_ATS
6215 GNUNET_log (error_kind, "%s problem is unbounded \n", problem);
6223 static void ats_solve_problem (unsigned int max_it, unsigned int max_dur, unsigned int c_peers, unsigned int c_mechs, struct ATS_stat *stat)
6225 int result = GNUNET_SYSERR;
6226 int lp_solution = GNUNET_SYSERR;
6227 int mlp_solution = GNUNET_SYSERR;
6231 glp_init_smcp(&opt_lp);
6233 opt_lp.msg_lev = GLP_MSG_ALL;
6235 opt_lp.msg_lev = GLP_MSG_OFF;
6238 // setting iteration limit
6239 opt_lp.it_lim = max_it;
6241 opt_lp.tm_lim = max_dur;
6243 if (ats->stat.recreate_problem == GNUNET_YES)
6244 opt_lp.presolve = GLP_ON;
6245 result = glp_simplex(ats->prob, &opt_lp);
6246 lp_solution = glp_get_status (ats->prob);
6248 if ((result == GLP_ETMLIM) || (result == GLP_EITLIM))
6250 ats->stat.valid = GNUNET_NO;
6251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS exceeded time or iteration limit!\n");
6255 if (ats_evaluate_results(result, lp_solution, "LP") == GNUNET_YES)
6257 stat->valid = GNUNET_YES;
6261 ats->stat.simplex_rerun_required = GNUNET_YES;
6262 opt_lp.presolve = GLP_ON;
6263 result = glp_simplex(ats->prob, &opt_lp);
6264 lp_solution = glp_get_status (ats->prob);
6266 // TODO: Remove if this does not appear until release
6267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "EXECUTED SIMPLEX WITH PRESOLVER! %i \n", lp_solution);
6269 if (ats_evaluate_results(result, lp_solution, "LP") != GNUNET_YES)
6271 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "After execution simplex with presolver: STILL INVALID!\n");
6273 GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_%llu.mlp",ats->stat.c_peers, ats->stat.c_mechs, GNUNET_TIME_absolute_get().abs_value);
6274 glp_write_lp (ats->prob, NULL, filename);
6275 GNUNET_free (filename);
6276 stat->valid = GNUNET_NO;
6277 ats->stat.recreate_problem = GNUNET_YES;
6280 stat->valid = GNUNET_YES;
6285 glp_init_iocp(&opt_mlp);
6287 opt_mlp.tm_lim = max_dur;
6290 opt_mlp.msg_lev = GLP_MSG_ALL;
6292 opt_mlp.msg_lev = GLP_MSG_OFF;
6295 result = glp_intopt (ats->prob, &opt_mlp);
6296 mlp_solution = glp_mip_status (ats->prob);
6297 stat->solution = mlp_solution;
6299 if (ats_evaluate_results(result, mlp_solution, "MLP") == GNUNET_YES)
6301 stat->valid = GNUNET_YES;
6305 // TODO: Remove if this does not appear until release
6306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP SOLUTION INVALID: %i\n", lp_solution);
6307 stat->valid = GNUNET_NO;
6312 int error = GNUNET_NO;
6314 struct ATS_mechanism *t = NULL;
6315 for (c=1; c<= (c_peers); c++ )
6318 t = peers[c].m_head;
6321 bw = glp_get_col_prim(prob, t->col_index);
6325 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);
6327 if (check ==GNUNET_YES)
6329 glp_write_sol(prob, "invalid_solution.mlp");
6330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid solution, check invalid_solution.mlp");
6331 GNUNET_STATISTICS_update (stats, "ATS invalid solutions", 1, GNUNET_NO);
6334 if (check ==GNUNET_NO)
6342 if (glp_get_col_prim(ats->prob,2*c_mechs+1) != 1)
6345 for (c=1; c<= available_quality_metrics; c++ )
6347 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(ats->prob,2*c_mechs+3+c), glp_get_col_prim(ats->prob,2*c_mechs+3+c));
6349 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(ats->prob,2*c_mechs+1), glp_get_col_prim(ats->prob,2*c_mechs+1));
6350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(ats->prob,2*c_mechs+2), glp_get_col_prim(ats->prob,2*c_mechs+2));
6351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(ats->prob,2*c_mechs+3), glp_get_col_prim(ats->prob,2*c_mechs+3));
6352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "objective value: %f\n", glp_mip_obj_val(ats->prob));
6357 static void ats_delete_problem ()
6360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Deleting problem\n");
6364 for (c=0; c< (ats->stat).c_mechs; c++)
6365 GNUNET_free_non_null (ats->mechanisms[c].rc);
6368 if (ats->mechanisms!=NULL)
6370 GNUNET_free(ats->mechanisms);
6371 ats->mechanisms = NULL;
6374 if (ats->peers!=NULL)
6376 GNUNET_free(ats->peers);
6380 if (ats->prob != NULL)
6382 glp_delete_prob(ats->prob);
6386 ats->stat.begin_cr = GNUNET_SYSERR;
6387 ats->stat.begin_qm = GNUNET_SYSERR;
6388 ats->stat.c_mechs = 0;
6389 ats->stat.c_peers = 0;
6390 ats->stat.end_cr = GNUNET_SYSERR;
6391 ats->stat.end_qm = GNUNET_SYSERR;
6392 ats->stat.solution = GNUNET_SYSERR;
6393 ats->stat.valid = GNUNET_SYSERR;
6397 static void ats_update_problem_qm ()
6402 int c_q_metrics = available_quality_metrics;
6404 int *ja = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (int));
6405 double *ar = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (double));
6407 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating problem quality metrics\n");
6409 row_index = ats->stat.begin_qm;
6411 for (c=1; c <= c_q_metrics; c++)
6416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6419 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6420 for (c2=1; c2<=ats->stat.c_mechs; c2++)
6422 ja[array_index] = c2;
6424 GNUNET_assert (ats->mechanisms[c2].addr != NULL);
6425 GNUNET_assert (ats->mechanisms[c2].peer != NULL);
6427 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY)
6429 double v0 = 0, v1 = 0, v2 = 0;
6431 v0 = ats->mechanisms[c2].addr->quality[c-1].values[0];
6432 if (v1 < 1) v0 = 0.1;
6433 v1 = ats->mechanisms[c2].addr->quality[c-1].values[1];
6434 if (v1 < 1) v0 = 0.1;
6435 v2 = ats->mechanisms[c2].addr->quality[c-1].values[2];
6436 if (v1 < 1) v0 = 0.1;
6437 value = 100.0 / ((v0 + 2 * v1 + 3 * v2) / 6.0);
6440 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
6442 double v0 = 0, v1 = 0, v2 = 0;
6443 v0 = ats->mechanisms[c2].addr->quality[c-1].values[0];
6445 v1 = ats->mechanisms[c2].addr->quality[c-1].values[1];
6447 v2 = ats->mechanisms[c2].addr->quality[c-1].values[2];
6449 value = (v0 + 2 * v1 + 3 * v2) / 6.0;
6451 value = (double) 10 / value;
6455 ar[array_index] = (ats->mechanisms[c2].peer->f) * value;
6457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: %s [%i,%i]=%f \n",array_index, qm[c-1].name, row_index, ja[array_index], ar[array_index]);
6461 ja[array_index] = ats->stat.col_qm + c - 1;
6462 ar[array_index] = -1;
6465 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, row_index, ja[array_index], ar[array_index]);
6467 glp_set_mat_row (ats->prob, row_index, array_index, ja, ar);
6473 GNUNET_free_non_null (ja);
6474 GNUNET_free_non_null (ar);
6478 static void ats_update_problem_cr ()
6484 double ct_max, ct_min;
6486 int *ja = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (int));
6487 double *ar = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (double));
6489 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating problem quality metrics\n");
6490 row_index = ats->stat.begin_cr;
6493 for (c=0; c<available_ressources; c++)
6495 ct_max = ressources[c].c_max;
6496 ct_min = ressources[c].c_min;
6498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] %f..%f\n",row_index, ct_min, ct_max);
6500 glp_set_row_bnds(ats->prob, row_index, GLP_DB, ct_min, ct_max);
6502 for (c2=1; c2<=ats->stat.c_mechs; c2++)
6506 GNUNET_assert (ats->mechanisms[c2].addr != NULL);
6507 GNUNET_assert (ats->mechanisms[c2].peer != NULL);
6509 ja[array_index] = c2;
6510 value = ats->mechanisms[c2].addr->ressources[c].c;
6511 ar[array_index] = value;
6513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, row_index, ja[array_index], ar[array_index]);
6517 glp_set_mat_row (ats->prob, row_index, array_index, ja, ar);
6523 GNUNET_free_non_null (ja);
6524 GNUNET_free_non_null (ar);
6529 static void ats_update_problem_qm_TEST ()
6534 int old_ja[ats->stat.c_mechs + 2];
6535 double old_ar[ats->stat.c_mechs + 2];
6539 int *ja = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (int));
6540 double *ar = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (double));
6542 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating problem quality metrics TEST\n");
6544 if (ats->stat.begin_qm >0)
6545 row_index = ats->stat.begin_qm;
6550 for (c=0; c<available_quality_metrics; c++)
6553 c_old = glp_get_mat_row (ats->prob, row_index, old_ja, old_ar);
6555 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6557 for (c2=1; c2<=c_old; c2++)
6559 ja[c2] = old_ja[c2];
6560 if ((changed < 3) && (c2>2) && (old_ar[c2] != -1))
6562 ar[c2] = old_ar[c2] + 5 - changed;
6566 ar[c2] = old_ar[c2];
6568 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: old [%i,%i]=%f new [%i,%i]=%f\n",c2, row_index, old_ja[c2], old_ar[c2], row_index, ja[c2], ar[c2]);
6571 glp_set_mat_row (ats->prob, row_index, c_old, ja, ar);
6576 GNUNET_free_non_null (ja);
6577 GNUNET_free_non_null (ar);
6579 #endif //END: HAVE_LIBGLPK
6581 /** solve the bandwidth distribution problem
6582 * @param max_it maximum iterations
6583 * @param max_dur maximum duration in ms
6584 * @param D weight for diversity
6585 * @param U weight for utility
6586 * @param R weight for relativity
6587 * @param v_b_min minimal bandwidth per peer
6588 * @param v_n_min minimum number of connections
6589 * @param stat result struct
6590 * @return GNUNET_SYSERR if glpk is not available, number of mechanisms used
6592 static int ats_create_problem (double D, double U, double R, int v_b_min, int v_n_min, struct ATS_stat *stat)
6594 ats->prob = glp_create_prob();
6600 int c_c_ressources = available_ressources;
6601 int c_q_metrics = available_quality_metrics;
6603 double M = VERY_BIG_DOUBLE_VALUE;
6604 double Q[c_q_metrics+1];
6605 for (c=1; c<=c_q_metrics; c++)
6610 struct NeighbourList *next = neighbours;
6613 int found_addresses = GNUNET_NO;
6614 struct ReadyList *r_next = next->plugins;
6615 while (r_next != NULL)
6617 struct ForeignAddressList * a_next = r_next->addresses;
6618 while (a_next != NULL)
6621 found_addresses = GNUNET_YES;
6622 a_next = a_next->next;
6624 r_next = r_next->next;
6626 if (found_addresses) c_peers++;
6633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No addresses for bw distribution available\n", c_peers);
6635 stat->valid = GNUNET_NO;
6638 return GNUNET_SYSERR;
6641 ats->mechanisms = GNUNET_malloc((1+c_mechs) * sizeof (struct ATS_mechanism));
6642 ats->peers = GNUNET_malloc((1+c_peers) * sizeof (struct ATS_peer));
6644 struct ATS_mechanism * mechanisms = ats->mechanisms;
6645 struct ATS_peer * peers = ats->peers;
6653 int found_addresses = GNUNET_NO;
6654 struct ReadyList *r_next = next->plugins;
6655 while (r_next != NULL)
6657 struct ForeignAddressList * a_next = r_next->addresses;
6658 while (a_next != NULL)
6660 if (found_addresses == GNUNET_NO)
6662 peers[c_peers].peer = next->id;
6663 peers[c_peers].m_head = NULL;
6664 peers[c_peers].m_tail = NULL;
6665 peers[c_peers].f = 1.0 / c_mechs;
6668 mechanisms[c_mechs].addr = a_next;
6669 mechanisms[c_mechs].col_index = c_mechs;
6670 mechanisms[c_mechs].peer = &peers[c_peers];
6671 mechanisms[c_mechs].next = NULL;
6672 mechanisms[c_mechs].plugin = r_next->plugin;
6674 GNUNET_CONTAINER_DLL_insert_tail(peers[c_peers].m_head, peers[c_peers].m_tail, &mechanisms[c_mechs]);
6675 found_addresses = GNUNET_YES;
6678 a_next = a_next->next;
6680 r_next = r_next->next;
6682 if (found_addresses == GNUNET_YES)
6689 if (v_n_min > c_peers)
6693 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);
6696 int size = 1 + 3 + 10 *c_mechs + c_peers + (c_q_metrics*c_mechs)+ c_q_metrics + c_c_ressources * c_mechs ;
6699 int * ia = GNUNET_malloc (size * sizeof (int));
6700 int * ja = GNUNET_malloc (size * sizeof (int));
6701 double * ar = GNUNET_malloc(size* sizeof (double));
6703 glp_set_prob_name(ats->prob, "gnunet ats bandwidth distribution");
6704 glp_set_obj_dir(ats->prob, GLP_MAX);
6706 /* adding columns */
6708 glp_add_cols(ats->prob, 2 * c_mechs);
6709 /* adding b_t cols */
6710 for (c=1; c <= c_mechs; c++)
6713 GNUNET_asprintf(&name, "p_%s_b%i",GNUNET_i2s(&(mechanisms[c].peer->peer)), c);
6714 glp_set_col_name(ats->prob, c, name);
6716 glp_set_col_bnds(ats->prob, c, GLP_LO, 0.0, 0.0);
6717 glp_set_col_kind(ats->prob, c, GLP_CV);
6718 glp_set_obj_coef(ats->prob, c, 0);
6721 /* adding n_t cols */
6722 for (c=c_mechs+1; c <= 2*c_mechs; c++)
6724 GNUNET_asprintf(&name, "p_%s_n%i",GNUNET_i2s(&(mechanisms[c-c_mechs].peer->peer)),(c-c_mechs));
6725 glp_set_col_name(ats->prob, c, name);
6727 glp_set_col_bnds(ats->prob, c, GLP_DB, 0.0, 1.0);
6728 glp_set_col_kind(ats->prob, c, GLP_IV);
6729 glp_set_obj_coef(ats->prob, c, 0);
6732 /* feasibility constraints */
6733 /* Constraint 1: one address per peer*/
6735 glp_add_rows(ats->prob, c_peers);
6736 for (c=1; c<=c_peers; c++)
6739 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6741 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 1.0, 1.0);
6743 struct ATS_mechanism *m = peers[c].m_head;
6746 ia[array_index] = row_index;
6747 ja[array_index] = (c_mechs + m->col_index);
6748 ar[array_index] = 1;
6750 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6758 /* Constraint 2: only active mechanism gets bandwidth assigned */
6759 glp_add_rows(ats->prob, c_mechs);
6760 for (c=1; c<=c_mechs; c++)
6762 /* b_t - n_t * M <= 0 */
6764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6766 glp_set_row_bnds(ats->prob, row_index, GLP_UP, 0.0, 0.0);
6768 ia[array_index] = row_index;
6769 ja[array_index] = mechanisms[c].col_index;
6770 ar[array_index] = 1;
6772 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6775 ia[array_index] = row_index;
6776 ja[array_index] = c_mechs + mechanisms[c].col_index;
6777 ar[array_index] = -M;
6779 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6785 /* Constraint 3: minimum bandwidth*/
6786 glp_add_rows(ats->prob, c_mechs);
6787 for (c=1; c<=c_mechs; c++)
6789 /* b_t - n_t * b_min <= 0 */
6791 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6793 glp_set_row_bnds(ats->prob, row_index, GLP_LO, 0.0, 0.0);
6795 ia[array_index] = row_index;
6796 ja[array_index] = mechanisms[c].col_index;
6797 ar[array_index] = 1;
6799 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6802 ia[array_index] = row_index;
6803 ja[array_index] = c_mechs + mechanisms[c].col_index;
6804 ar[array_index] = -v_b_min;
6806 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6812 /* Constraint 4: max ressource capacity */
6813 /* V cr: bt * ct_r <= cr_max
6815 glp_add_rows(ats->prob, available_ressources);
6816 double ct_max = VERY_BIG_DOUBLE_VALUE;
6817 double ct_min = 0.0;
6819 stat->begin_cr = array_index;
6821 for (c=0; c<available_ressources; c++)
6823 ct_max = ressources[c].c_max;
6824 ct_min = ressources[c].c_min;
6826 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] %f..%f\n",row_index, ct_min, ct_max);
6828 glp_set_row_bnds(ats->prob, row_index, GLP_DB, ct_min, ct_max);
6830 for (c2=1; c2<=c_mechs; c2++)
6833 ia[array_index] = row_index;
6834 ja[array_index] = c2;
6835 value = mechanisms[c2].addr->ressources[c].c;
6836 ar[array_index] = value;
6838 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6844 stat->end_cr = array_index--;
6846 /* Constraint 5: min number of connections*/
6847 glp_add_rows(ats->prob, 1);
6848 for (c=1; c<=c_mechs; c++)
6850 // b_t - n_t * b_min >= 0
6852 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6854 glp_set_row_bnds(ats->prob, row_index, GLP_LO, v_n_min, 0.0);
6856 ia[array_index] = row_index;
6857 ja[array_index] = c_mechs + mechanisms[c].col_index;
6858 ar[array_index] = 1;
6860 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6866 // optimisation constraints
6870 // Constraint 6: optimize for diversity
6872 col_d = glp_add_cols(ats->prob, 1);
6873 stat->col_d = col_d;
6874 //GNUNET_assert (col_d == (2*c_mechs) + 1);
6875 glp_set_col_name(ats->prob, col_d, "d");
6876 glp_set_obj_coef(ats->prob, col_d, D);
6877 glp_set_col_bnds(ats->prob, col_d, GLP_LO, 0.0, 0.0);
6878 glp_add_rows(ats->prob, 1);
6880 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6882 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6883 for (c=1; c<=c_mechs; c++)
6885 ia[array_index] = row_index;
6886 ja[array_index] = c_mechs + mechanisms[c].col_index;
6887 ar[array_index] = 1;
6889 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6893 ia[array_index] = row_index;
6894 ja[array_index] = col_d;
6895 ar[array_index] = -1;
6897 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6903 // Constraint 7: optimize for quality
6905 col_qm = glp_add_cols(ats->prob, c_q_metrics);
6906 stat->col_qm = col_qm;
6907 //GNUNET_assert (col_qm == (2*c_mechs) + 3 + 1);
6908 for (c=0; c< c_q_metrics; c++)
6910 GNUNET_asprintf(&name, "Q_%s",qm[c].name);
6911 glp_set_col_name(ats->prob, col_qm + c, name);
6912 glp_set_col_bnds(ats->prob, col_qm + c, GLP_LO, 0.0, 0.0);
6914 glp_set_obj_coef(ats->prob, col_qm + c, Q[c]);
6916 glp_add_rows(ats->prob, available_quality_metrics);
6917 stat->begin_qm = row_index;
6918 for (c=1; c <= c_q_metrics; c++)
6921 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6924 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6925 for (c2=1; c2<=c_mechs; c2++)
6928 ia[array_index] = row_index;
6929 ja[array_index] = c2;
6930 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY)
6932 double v0 = 0, v1 = 0, v2 = 0;
6933 v0 = mechanisms[c2].addr->quality[c-1].values[0];
6934 if (v1 < 1) v0 = 0.1;
6935 v1 = mechanisms[c2].addr->quality[c-1].values[1];
6936 if (v1 < 1) v0 = 0.1;
6937 v2 = mechanisms[c2].addr->quality[c-1].values[2];
6938 if (v1 < 1) v0 = 0.1;
6939 value = 100.0 / ((v0 + 2 * v1 + 3 * v2) / 6.0);
6942 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
6944 double v0 = 0, v1 = 0, v2 = 0;
6945 v0 = mechanisms[c2].addr->quality[c-1].values[0];
6947 v1 = mechanisms[c2].addr->quality[c-1].values[1];
6949 v2 = mechanisms[c2].addr->quality[c-1].values[2];
6951 value = (v0 + 2 * v1 + 3 * v2) / 6.0;
6953 value = (double) 10 / value;
6957 ar[array_index] = (mechanisms[c2].peer->f) * value ;
6959 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]);
6964 ia[array_index] = row_index;
6965 ja[array_index] = col_qm + c - 1;
6966 ar[array_index] = -1;
6968 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6973 stat->end_qm = row_index-1;
6975 // Constraint 8: optimize bandwidth utility
6977 col_u = glp_add_cols(ats->prob, 1);
6978 stat->col_u = col_u;
6979 //GNUNET_assert (col_u == (2*c_mechs) + 2);
6980 glp_set_col_name(ats->prob, col_u, "u");
6981 glp_set_obj_coef(ats->prob, col_u, U);
6982 glp_set_col_bnds(ats->prob, col_u, GLP_LO, 0.0, 0.0);
6983 glp_add_rows(ats->prob, 1);
6985 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6987 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6988 for (c=1; c<=c_mechs; c++)
6990 ia[array_index] = row_index;
6991 ja[array_index] = c;
6992 ar[array_index] = mechanisms[c].peer->f;
6994 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6998 ia[array_index] = row_index;
6999 ja[array_index] = col_u;
7000 ar[array_index] = -1;
7002 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
7008 // Constraint 9: optimize relativity
7010 col_r = glp_add_cols(ats->prob, 1);
7011 stat->col_r = col_r;
7012 //GNUNET_assert (col_r == (2*c_mechs) + 3);
7013 glp_set_col_name(ats->prob, col_r, "r");
7014 glp_set_obj_coef(ats->prob, col_r, R);
7015 glp_set_col_bnds(ats->prob, col_r, GLP_LO, 0.0, 0.0);
7016 glp_add_rows(ats->prob, c_peers);
7017 for (c=1; c<=c_peers; c++)
7019 glp_set_row_bnds(ats->prob, row_index, GLP_LO, 0.0, 0.0);
7021 struct ATS_mechanism *m = peers[c].m_head;
7024 ia[array_index] = row_index;
7025 ja[array_index] = m->col_index;
7026 ar[array_index] = 1 / mechanisms[c].peer->f;
7028 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
7033 ia[array_index] = row_index;
7034 ja[array_index] = col_r;
7035 ar[array_index] = -1;
7037 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
7044 /* Loading the matrix */
7045 glp_load_matrix(ats->prob, array_index-1, ia, ja, ar);
7047 stat->c_mechs = c_mechs;
7048 stat->c_peers = c_peers;
7050 stat->valid = GNUNET_YES;
7062 void ats_notify_ats_data (
7063 const struct GNUNET_PeerIdentity *peer,
7064 const struct GNUNET_TRANSPORT_ATS_Information *ats_data)
7067 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "ATS_notify_ats_data: %s\n",GNUNET_i2s(peer));
7069 ats_calculate_bandwidth_distribution();
7071 #endif //END: HAVE_LIBGLPK
7074 ats_calculate_bandwidth_distribution ()
7078 struct GNUNET_TIME_Absolute start;
7079 struct GNUNET_TIME_Relative creation;
7080 struct GNUNET_TIME_Relative solving;
7081 char *text = "unmodified";
7083 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_difference (ats->last, GNUNET_TIME_absolute_get());
7084 if (delta.rel_value < ats->min_delta.rel_value)
7087 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Minimum time between cycles not reached\n");
7093 if (INT_MAX < ats->max_exec_duration.rel_value)
7096 dur = (int) ats->max_exec_duration.rel_value;
7098 ats->stat.simplex_rerun_required = GNUNET_NO;
7099 start = GNUNET_TIME_absolute_get();
7100 if ((ats->stat.recreate_problem == GNUNET_YES) || (ats->prob==NULL) || (ats->stat.valid == GNUNET_NO))
7103 ats->stat.recreate_problem = GNUNET_YES;
7104 ats_delete_problem ();
7105 ats_create_problem (ats->D, ats->U, ats->R, ats->v_b_min, ats->v_n_min, &ats->stat);
7107 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers/Addresses were modified... new problem: %i peer, %i mechs\n", ats->stat.c_peers, ats->stat.c_mechs);
7111 else if ((ats->stat.recreate_problem == GNUNET_NO) && (ats->stat.modified_resources == GNUNET_YES) && (ats->stat.valid == GNUNET_YES))
7113 text = "modified resources";
7114 ats_update_problem_cr();
7116 else if ((ats->stat.recreate_problem == GNUNET_NO) && (ats->stat.modified_quality == GNUNET_YES) && (ats->stat.valid == GNUNET_YES))
7118 text = "modified quality";
7119 ats_update_problem_qm();
7120 //ats_update_problem_qm_TEST ();
7124 else GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Problem is unmodified\n");
7127 creation = GNUNET_TIME_absolute_get_difference(start,GNUNET_TIME_absolute_get());
7128 start = GNUNET_TIME_absolute_get();
7130 ats->stat.solution = GLP_UNDEF;
7131 if (ats->stat.valid == GNUNET_YES)
7133 ats_solve_problem(ats->max_iterations, ats->max_exec_duration.rel_value, ats->stat.c_peers, ats->stat.c_mechs, &ats->stat);
7135 solving = GNUNET_TIME_absolute_get_difference(start,GNUNET_TIME_absolute_get());
7137 if (ats->stat.valid == GNUNET_YES)
7139 int msg_type = GNUNET_ERROR_TYPE_DEBUG;
7141 msg_type = GNUNET_ERROR_TYPE_ERROR;
7143 GNUNET_log (msg_type, "MLP %s: creation time: %llu, execution time: %llu, %i mechanisms, simplex rerun: %s, solution %s\n",
7144 text, creation.rel_value, solving.rel_value,
7146 (ats->stat.simplex_rerun_required == GNUNET_NO) ? "NO" : "YES", (ats->stat.solution == 5) ? "OPTIMAL" : "INVALID");
7147 ats->successful_executions ++;
7148 GNUNET_STATISTICS_set (stats, "# ATS successful executions", ats->successful_executions, GNUNET_NO);
7150 if ((ats->stat.recreate_problem == GNUNET_YES) || (ats->prob==NULL))
7151 GNUNET_STATISTICS_set (stats, "ATS state",ATS_NEW, GNUNET_NO);
7152 else if ((ats->stat.modified_resources == GNUNET_YES) &&
7153 (ats->stat.modified_quality == GNUNET_NO))
7154 GNUNET_STATISTICS_set (stats, "ATS state", ATS_C_UPDATED, GNUNET_NO);
7155 else if ((ats->stat.modified_resources == GNUNET_NO) &&
7156 (ats->stat.modified_quality == GNUNET_YES) &&
7157 (ats->stat.simplex_rerun_required == GNUNET_NO))
7158 GNUNET_STATISTICS_set (stats, "ATS state", ATS_Q_UPDATED, GNUNET_NO);
7159 else if ((ats->stat.modified_resources == GNUNET_YES) &&
7160 (ats->stat.modified_quality == GNUNET_YES) &&
7161 (ats->stat.simplex_rerun_required == GNUNET_NO))
7162 GNUNET_STATISTICS_set (stats, "ATS state", ATS_QC_UPDATED, GNUNET_NO);
7163 else if (ats->stat.simplex_rerun_required == GNUNET_NO)
7164 GNUNET_STATISTICS_set (stats, "ATS state", ATS_UNMODIFIED, GNUNET_NO);
7168 if (ats->stat.c_peers != 0)
7170 ats->invalid_executions ++;
7171 GNUNET_STATISTICS_set (stats, "# ATS invalid executions", ats->invalid_executions, GNUNET_NO);
7175 GNUNET_STATISTICS_set (stats, "# ATS successful executions", ats->successful_executions, GNUNET_NO);
7179 GNUNET_STATISTICS_set (stats, "ATS duration", solving.rel_value + creation.rel_value, GNUNET_NO);
7180 GNUNET_STATISTICS_set (stats, "ATS mechanisms", ats->stat.c_mechs, GNUNET_NO);
7181 GNUNET_STATISTICS_set (stats, "ATS peers", ats->stat.c_peers, GNUNET_NO);
7182 GNUNET_STATISTICS_set (stats, "ATS solution", ats->stat.solution, GNUNET_NO);
7183 GNUNET_STATISTICS_set (stats, "ATS timestamp", start.abs_value, GNUNET_NO);
7185 if ((ats->save_mlp == GNUNET_YES) && (ats->stat.c_mechs >= ats->dump_min_peers) && (ats->stat.c_mechs >= ats->dump_min_addr))
7188 if (ats->dump_overwrite == GNUNET_NO)
7190 GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_%s_%llu.mlp",
7191 ats->stat.c_peers, ats->stat.c_mechs, text, GNUNET_TIME_absolute_get().abs_value);
7192 glp_write_lp (ats->prob, NULL, filename);
7196 GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i.mlp",
7197 ats->stat.c_peers, ats->stat.c_mechs );
7198 glp_write_lp (ats->prob, NULL, filename);
7200 GNUNET_free (filename);
7202 if ((ats->save_solution == GNUNET_YES) && (ats->stat.c_mechs >= ats->dump_min_peers) && (ats->stat.c_mechs >= ats->dump_min_addr))
7205 if (ats->dump_overwrite == GNUNET_NO)
7207 GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_%s_%llu.sol",
7208 ats->stat.c_peers, ats->stat.c_mechs, text, GNUNET_TIME_absolute_get().abs_value);
7209 glp_print_sol (ats->prob, filename);
7213 GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i.sol",
7214 ats->stat.c_peers, ats->stat.c_mechs);
7215 glp_print_sol (ats->prob, filename);
7217 GNUNET_free (filename);
7220 ats->last = GNUNET_TIME_absolute_get();
7221 ats->stat.recreate_problem = GNUNET_NO;
7222 ats->stat.modified_resources = GNUNET_NO;
7223 ats->stat.modified_quality = GNUNET_NO;
7228 ats_schedule_calculation (void *cls,
7229 const struct GNUNET_SCHEDULER_TaskContext *tc)
7231 struct ATS_info *ats = (struct ATS_info *) cls;
7232 if (ats==NULL) return;
7234 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
7235 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
7239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running scheduled calculation\n");
7242 ats_calculate_bandwidth_distribution (ats);
7244 ats->ats_task = GNUNET_SCHEDULER_add_delayed (ats->exec_interval,
7245 &ats_schedule_calculation, ats);
7251 unsigned long long value;
7254 ats = GNUNET_malloc(sizeof (struct ATS_info));
7256 ats->min_delta = ATS_MIN_INTERVAL;
7257 ats->exec_interval = ATS_EXEC_INTERVAL;
7258 ats->max_exec_duration = ATS_MAX_EXEC_DURATION;
7259 ats->max_iterations = ATS_MAX_ITERATIONS;
7260 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
7263 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "GLPK not installed, ATS not active\n");
7270 ats->v_b_min = 64000;
7272 ats->dump_min_peers = 1;
7273 ats->dump_min_addr = 1;
7274 ats->dump_overwrite = GNUNET_NO;
7275 ats->mechanisms = NULL;
7277 ats->successful_executions = 0;
7278 ats->invalid_executions = 0;
7284 /* loading cost ressources */
7285 for (c=0; c<available_ressources; c++)
7287 GNUNET_asprintf(§ion,"%s_UP",ressources[c].cfg_param);
7288 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
7290 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "transport",section, &value))
7293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found ressource cost: [%s] = %llu\n", section, value);
7295 ressources[c].c_max = value;
7298 GNUNET_free (section);
7299 GNUNET_asprintf(§ion,"%s_DOWN",ressources[c].cfg_param);
7300 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
7302 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "transport",section, &value))
7305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found ressource cost: [%s] = %llu\n", section, value);
7307 ressources[c].c_min = value;
7310 GNUNET_free (section);
7313 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_MLP"))
7314 ats->save_mlp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "transport","DUMP_MLP");
7316 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_SOLUTION"))
7317 ats->save_solution = GNUNET_CONFIGURATION_get_value_yesno (cfg, "transport","DUMP_SOLUTION");
7318 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_OVERWRITE"))
7319 ats->dump_overwrite = GNUNET_CONFIGURATION_get_value_yesno (cfg, "transport","DUMP_OVERWRITE");
7320 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_MIN_PEERS"))
7322 GNUNET_CONFIGURATION_get_value_number(cfg, "transport","DUMP_MIN_PEERS", &value);
7323 ats->dump_min_peers= value;
7325 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_MIN_ADDRS"))
7327 GNUNET_CONFIGURATION_get_value_number(cfg, "transport","DUMP_MIN_ADDRS", &value);
7328 ats->dump_min_addr= value;
7330 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_OVERWRITE"))
7332 GNUNET_CONFIGURATION_get_value_number(cfg, "transport","DUMP_OVERWRITE", &value);
7333 ats->min_delta.rel_value = value;
7336 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "ATS_MIN_INTERVAL"))
7338 GNUNET_CONFIGURATION_get_value_number(cfg, "transport","ATS_MIN_INTERVAL", &value);
7339 ats->min_delta.rel_value = value;
7342 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "ATS_EXEC_INTERVAL"))
7344 GNUNET_CONFIGURATION_get_value_number(cfg, "transport","ATS_EXEC_INTERVAL", &value);
7345 ats->exec_interval.rel_value = value;
7347 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "ATS_MIN_INTERVAL"))
7349 GNUNET_CONFIGURATION_get_value_number(cfg, "transport","ATS_MIN_INTERVAL", &value);
7350 ats->min_delta.rel_value = value;
7353 ats->ats_task = GNUNET_SCHEDULER_add_now(&ats_schedule_calculation, ats);
7357 static void ats_shutdown ()
7360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_destroy\n");
7362 if (ats->ats_task != GNUNET_SCHEDULER_NO_TASK)
7363 GNUNET_SCHEDULER_cancel(ats->ats_task);
7364 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
7367 ats_delete_problem ();
7374 void ats_notify_peer_connect (
7375 const struct GNUNET_PeerIdentity *peer,
7376 const struct GNUNET_TRANSPORT_ATS_Information *ats_data, int ats_count)
7379 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_notify_peer_connect: %s\n",GNUNET_i2s(peer));
7381 //update_addr_ats();
7382 ats->stat.recreate_problem = GNUNET_YES;
7383 ats_calculate_bandwidth_distribution(ats);
7386 void ats_notify_peer_disconnect (
7387 const struct GNUNET_PeerIdentity *peer)
7390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_notify_peer_disconnect: %s\n",GNUNET_i2s(peer));
7392 ats->stat.recreate_problem = GNUNET_YES;
7393 ats_calculate_bandwidth_distribution (ats);
7396 struct ForeignAddressList * ats_get_preferred_address (
7397 struct NeighbourList *n)
7400 //GNUNET_log (GNUNET_ERROR_TYPE_BULK, "ats_get_prefered_transport for peer: %s\n",GNUNET_i2s(&n->id));
7402 struct ReadyList *next = n->plugins;
7403 while (next != NULL)
7406 //GNUNET_log (GNUNET_ERROR_TYPE_BULK, "plugin: %s %i\n",next->plugin->short_name,strcmp(next->plugin->short_name,"unix"));
7410 return find_ready_address(n);
7414 * Initiate transport service.
7416 * @param cls closure
7417 * @param server the initialized server
7418 * @param c configuration to use
7422 struct GNUNET_SERVER_Handle *server,
7423 const struct GNUNET_CONFIGURATION_Handle *c)
7425 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
7426 {&handle_start, NULL,
7427 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
7428 {&handle_hello, NULL,
7429 GNUNET_MESSAGE_TYPE_HELLO, 0},
7430 {&handle_send, NULL,
7431 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
7432 {&handle_request_connect, NULL,
7433 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT, sizeof(struct TransportRequestConnectMessage)},
7434 {&handle_set_quota, NULL,
7435 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
7436 {&handle_address_lookup, NULL,
7437 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
7439 {&handle_blacklist_init, NULL,
7440 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
7441 {&handle_blacklist_reply, NULL,
7442 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
7448 unsigned long long tneigh;
7452 stats = GNUNET_STATISTICS_create ("transport", cfg);
7453 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
7454 /* parse configuration */
7456 GNUNET_CONFIGURATION_get_value_number (c,
7461 GNUNET_CONFIGURATION_get_value_filename (c,
7463 "HOSTKEY", &keyfile)))
7465 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7467 ("Transport service is lacking key configuration settings. Exiting.\n"));
7468 GNUNET_SCHEDULER_shutdown ();
7471 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
7474 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
7475 validation_map = NULL;
7479 max_connect_per_transport = (uint32_t) tneigh;
7480 peerinfo = GNUNET_PEERINFO_connect (cfg);
7481 if (peerinfo == NULL)
7483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7484 _("Could not access PEERINFO service. Exiting.\n"));
7485 GNUNET_SCHEDULER_shutdown ();
7488 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
7491 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
7492 validation_map = NULL;
7493 GNUNET_free (keyfile);
7496 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7497 GNUNET_free (keyfile);
7498 if (my_private_key == NULL)
7500 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7502 ("Transport service could not access hostkey. Exiting.\n"));
7503 GNUNET_SCHEDULER_shutdown ();
7506 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
7509 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
7510 validation_map = NULL;
7513 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7514 GNUNET_CRYPTO_hash (&my_public_key,
7515 sizeof (my_public_key), &my_identity.hashPubKey);
7516 /* setup notification */
7517 GNUNET_SERVER_disconnect_notify (server,
7518 &client_disconnect_notification, NULL);
7519 /* load plugins... */
7522 GNUNET_CONFIGURATION_get_value_string (c,
7523 "TRANSPORT", "PLUGINS", &plugs))
7525 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7526 _("Starting transport plugins `%s'\n"), plugs);
7527 pos = strtok (plugs, " ");
7530 start_transport (server, pos);
7532 pos = strtok (NULL, " ");
7534 GNUNET_free (plugs);
7536 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
7537 &shutdown_task, NULL);
7544 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport service ready.\n"));
7546 /* If we have a blacklist file, read from it */
7547 read_blacklist_file(cfg);
7548 /* process client requests */
7549 GNUNET_SERVER_add_handlers (server, handlers);
7554 * The main function for the transport service.
7556 * @param argc number of arguments from the command line
7557 * @param argv command line arguments
7558 * @return 0 ok, 1 on error
7561 main (int argc, char *const *argv)
7563 a2s (NULL, NULL, 0); /* make compiler happy */
7564 return (GNUNET_OK ==
7565 GNUNET_SERVICE_run (argc,
7568 GNUNET_SERVICE_OPTION_NONE,
7569 &run, NULL)) ? 0 : 1;
7572 /* end of gnunet-service-transport.c */