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 * is problem currently valid and can it be solved
898 * Number of transport mechanisms in the problem
903 * Number of transport mechanisms in the problem
908 * row index where quality related rows start
913 * row index where quality related rows end
918 * row index where ressource cost related rows start
923 * row index where ressource cost related rows end
934 struct ATS_ressource_entry
936 /* index in ressources array */
938 /* depending ATSi parameter to calculcate limits */
947 /* index in ressources array */
949 /* depending ATSi parameter to calculcate limits */
951 /* cfg option to load limits */
958 /* cofficients for the specific plugins */
968 static struct ATS_ressource ressources[] =
970 /* FIXME: the coefficients for the specific plugins */
971 {1, 7, "LAN_BW_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 1, 1, 2, 2, 1, 3},
972 {2, 7, "WAN_BW_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 1, 1, 2, 2, 2, 3},
973 {3, 4, "WLAN_ENERGY_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 0, 0, 0, 0, 2, 1}
975 {4, 4, "COST_ENERGY_CONSUMPTION", VERY_BIG_DOUBLE_VALUE},
976 {5, 5, "COST_CONNECT", VERY_BIG_DOUBLE_VALUE},
977 {6, 6, "COST_BANDWITH_AVAILABLE", VERY_BIG_DOUBLE_VALUE},
978 {7, 7, "COST_NETWORK_OVERHEAD", VERY_BIG_DOUBLE_VALUE},*/
981 static int available_ressources = 3;
989 * Time of last execution
991 struct GNUNET_TIME_Absolute last;
993 * Minimum intervall between two executions
995 struct GNUNET_TIME_Relative min_delta;
997 * Regular intervall when execution is triggered
999 struct GNUNET_TIME_Relative exec_interval;
1001 * Maximum execution time per calculation
1003 struct GNUNET_TIME_Relative max_exec_duration;
1007 * GLPK (MLP) problem object
1013 * task to recalculate the bandwidth assignment
1015 GNUNET_SCHEDULER_TaskIdentifier ats_task;
1018 * Current state of the GLPK problem
1020 struct ATS_stat stat;
1023 * mechanisms used in current problem
1024 * needed for problem modification
1026 struct ATS_mechanism * mechanisms;
1029 * peers used in current problem
1030 * needed for problem modification
1032 struct ATS_peer * peers;
1035 * Maximum number of LP iterations per calculation
1040 * Dump problem to a file?
1045 * Dump solution to a file
1050 * Ressource costs or quality metrics changed
1051 * update problem before solving
1053 int modified_resources;
1056 * Ressource costs or quality metrics changed, update matrix
1057 * update problem before solving
1059 int modified_quality;
1062 * Peers have connected or disconnected
1063 * problem has to be recreated
1068 * Was the available basis invalid and we needed to rerun simplex?
1070 int simplex_rerun_required;
1088 * Minimum bandwidth per peer
1093 * Minimum number of connections per peer
1100 * Our HELLO message.
1102 static struct GNUNET_HELLO_Message *our_hello;
1107 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
1112 static struct GNUNET_PeerIdentity my_identity;
1117 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
1120 * Our configuration.
1122 const struct GNUNET_CONFIGURATION_Handle *cfg;
1125 * Linked list of all clients to this service.
1127 static struct TransportClient *clients;
1130 * All loaded plugins.
1132 static struct TransportPlugin *plugins;
1135 * Handle to peerinfo service.
1137 static struct GNUNET_PEERINFO_Handle *peerinfo;
1140 * All known neighbours and their HELLOs.
1142 static struct NeighbourList *neighbours;
1145 * Number of neighbours we'd like to have.
1147 static uint32_t max_connect_per_transport;
1150 * Head of linked list.
1152 static struct CheckHelloValidatedContext *chvc_head;
1155 * Tail of linked list.
1157 static struct CheckHelloValidatedContext *chvc_tail;
1160 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
1161 * of the given peer that we are currently validating).
1163 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
1166 * Handle for reporting statistics.
1168 static struct GNUNET_STATISTICS_Handle *stats;
1171 * Handle for ats information
1173 static struct ATS_info *ats;
1175 struct ATS_quality_entry
1183 static struct ATS_quality_metric qm[] =
1185 {1, 1028, "QUALITY_NET_DISTANCE"},
1186 {2, 1034, "QUALITY_NET_DELAY"},
1188 static int available_quality_metrics = 2;
1192 * The peer specified by the given neighbour has timed-out or a plugin
1193 * has disconnected. We may either need to do nothing (other plugins
1194 * still up), or trigger a full disconnect and clean up. This
1195 * function updates our state and do the necessary notifications.
1196 * Also notifies our clients that the neighbour is now officially
1199 * @param n the neighbour list entry for the peer
1200 * @param check should we just check if all plugins
1201 * disconnected or must we ask all plugins to
1204 static void disconnect_neighbour (struct NeighbourList *n, int check);
1207 * Check the ready list for the given neighbour and if a plugin is
1208 * ready for transmission (and if we have a message), do so!
1210 * @param nexi target peer for which to transmit
1212 static void try_transmission_to_peer (struct NeighbourList *n);
1214 static void ats_shutdown ( );
1216 static void ats_notify_peer_connect (
1217 const struct GNUNET_PeerIdentity *peer,
1218 const struct GNUNET_TRANSPORT_ATS_Information *ats_data, int ats_count);
1220 static void ats_notify_peer_disconnect (
1221 const struct GNUNET_PeerIdentity *peer);
1224 static void ats_notify_ats_data (
1225 const struct GNUNET_PeerIdentity *peer,
1226 const struct GNUNET_TRANSPORT_ATS_Information *ats_data);
1229 struct ForeignAddressList * ats_get_preferred_address (
1230 struct NeighbourList *n);
1233 ats_calculate_bandwidth_distribution ();
1236 * Find an entry in the neighbour list for a particular peer.
1238 * @return NULL if not found.
1240 static struct NeighbourList *
1241 find_neighbour (const struct GNUNET_PeerIdentity *key)
1243 struct NeighbourList *head = neighbours;
1245 while ((head != NULL) &&
1246 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
1251 static int update_addr_value (struct ForeignAddressList *fal, uint32_t value , int ats_index)
1254 int set = GNUNET_NO;
1255 for (c=0; c<available_quality_metrics; c++)
1257 if (ats_index == qm[c].atis_index)
1259 fal->quality[c].values[0] = fal->quality[c].values[1];
1260 fal->quality[c].values[1] = fal->quality[c].values[2];
1261 fal->quality[c].values[2] = value;
1263 ats->modified_quality = GNUNET_YES;
1266 if (set == GNUNET_NO)
1268 for (c=0; c<available_ressources; c++)
1270 if (ats_index == ressources[c].atis_index)
1272 fal->ressources[c].c = value;
1274 ats->modified_resources = GNUNET_YES;
1282 static int update_addr_ats (struct ForeignAddressList *fal, const struct GNUNET_TRANSPORT_ATS_Information *ats_data, int ats_count)
1286 for (c1=0; c1<ats_count; c1++)
1288 set = update_addr_value(fal, ntohl(ats_data[c1].value), ntohl(ats_data[c1].type));
1294 * Find an entry in the transport list for a particular transport.
1296 * @return NULL if not found.
1298 static struct TransportPlugin *
1299 find_transport (const char *short_name)
1301 struct TransportPlugin *head = plugins;
1302 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
1308 * Is a particular peer blacklisted for a particular transport?
1310 * @param peer the peer to check for
1311 * @param plugin the plugin used to connect to the peer
1313 * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
1316 is_blacklisted (const struct GNUNET_PeerIdentity *peer, struct TransportPlugin *plugin)
1319 if (plugin->blacklist != NULL)
1321 if (GNUNET_CONTAINER_multihashmap_contains (plugin->blacklist, &peer->hashPubKey) == GNUNET_YES)
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1325 "Peer `%s:%s' is blacklisted!\n",
1326 plugin->short_name, GNUNET_i2s (peer));
1329 GNUNET_STATISTICS_update (stats, "# blacklisted peers refused", 1, GNUNET_NO);
1339 add_peer_to_blacklist (struct GNUNET_PeerIdentity *peer, char *transport_name)
1341 struct TransportPlugin *plugin;
1343 plugin = find_transport(transport_name);
1344 if (plugin == NULL) /* Nothing to do */
1346 if (plugin->blacklist == NULL)
1347 plugin->blacklist = GNUNET_CONTAINER_multihashmap_create(TRANSPORT_BLACKLIST_HT_SIZE);
1348 GNUNET_assert(plugin->blacklist != NULL);
1349 GNUNET_CONTAINER_multihashmap_put(plugin->blacklist, &peer->hashPubKey,
1351 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1356 * Read the blacklist file, containing transport:peer entries.
1357 * Provided the transport is loaded, set up hashmap with these
1358 * entries to blacklist peers by transport.
1362 read_blacklist_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1369 struct GNUNET_PeerIdentity pid;
1371 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1372 unsigned int entries_found;
1373 char *transport_name;
1376 GNUNET_CONFIGURATION_get_value_filename (cfg,
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1383 "Option `%s' in section `%s' not specified!\n",
1389 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1390 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1391 | GNUNET_DISK_PERM_USER_WRITE);
1392 if (0 != STAT (fn, &frstat))
1394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1395 _("Could not read blacklist file `%s'\n"), fn);
1399 if (frstat.st_size == 0)
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 _("Blacklist file `%s' is empty.\n"),
1409 /* FIXME: use mmap */
1410 data = GNUNET_malloc_large (frstat.st_size);
1411 GNUNET_assert(data != NULL);
1412 if (frstat.st_size !=
1413 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1416 _("Failed to read blacklist from `%s'\n"), fn);
1423 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1425 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1426 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1429 while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') && !isspace ( (unsigned char) data[colon_pos]))
1432 if (colon_pos >= frstat.st_size)
1434 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1435 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1436 (unsigned long long) colon_pos);
1442 if (isspace( (unsigned char) data[colon_pos]))
1444 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1445 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1446 (unsigned long long) colon_pos);
1448 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1452 tsize = colon_pos - pos;
1453 if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size) || (tsize == 0))
1455 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1456 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1457 (unsigned long long) colon_pos);
1466 transport_name = GNUNET_malloc(tsize + 1);
1467 memcpy(transport_name, &data[pos], tsize);
1468 pos = colon_pos + 1;
1470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1471 "Read transport name %s in blacklist file.\n",
1474 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1475 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1477 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1478 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1479 (unsigned long long) pos);
1481 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1483 GNUNET_free_non_null(transport_name);
1486 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1487 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1489 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1490 _("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
1491 (unsigned long long) pos,
1496 if (0 != memcmp (&pid,
1498 sizeof (struct GNUNET_PeerIdentity)))
1501 add_peer_to_blacklist (&pid,
1506 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1507 _("Found myself `%s' in blacklist (useless, ignored)\n"),
1511 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1512 GNUNET_free_non_null(transport_name);
1513 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1516 GNUNET_STATISTICS_update (stats, "# Transport entries blacklisted", entries_found, GNUNET_NO);
1523 * Function called to notify a client about the socket being ready to
1524 * queue more data. "buf" will be NULL and "size" zero if the socket
1525 * was closed for writing in the meantime.
1527 * @param cls closure
1528 * @param size number of bytes available in buf
1529 * @param buf where the callee should write the message
1530 * @return number of bytes written to buf
1533 transmit_to_client_callback (void *cls, size_t size, void *buf)
1535 struct TransportClient *client = cls;
1536 struct ClientMessageQueueEntry *q;
1539 const struct GNUNET_MessageHeader *msg;
1546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1547 "Transmission to client failed, closing connection.\n");
1549 /* fatal error with client, free message queue! */
1550 while (NULL != (q = client->message_queue_head))
1552 GNUNET_STATISTICS_update (stats,
1553 gettext_noop ("# bytes discarded (could not transmit to client)"),
1554 ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
1556 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1557 client->message_queue_tail,
1561 client->message_count = 0;
1566 while (NULL != (q = client->message_queue_head))
1568 msg = (const struct GNUNET_MessageHeader *) &q[1];
1569 msize = ntohs (msg->size);
1570 if (msize + tsize > size)
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1574 "Transmitting message of type %u to client.\n",
1577 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1578 client->message_queue_tail,
1580 memcpy (&cbuf[tsize], msg, msize);
1583 client->message_count--;
1587 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1588 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1590 GNUNET_TIME_UNIT_FOREVER_REL,
1591 &transmit_to_client_callback,
1593 GNUNET_assert (client->th != NULL);
1600 * Convert an address to a string.
1602 * @param plugin name of the plugin responsible for the address
1603 * @param addr binary address
1604 * @param addr_len number of bytes in addr
1605 * @return NULL on error, otherwise address string
1608 a2s (const char *plugin,
1612 struct TransportPlugin *p;
1616 p = find_transport (plugin);
1619 return p->api->address_to_string (p->api->cls,
1626 * Mark the given FAL entry as 'connected' (and hence preferred for
1627 * sending); also mark all others for the same peer as 'not connected'
1628 * (since only one can be preferred).
1630 * @param fal address to set to 'connected'
1633 mark_address_connected (struct ForeignAddressList *fal)
1635 struct ForeignAddressList *pos;
1638 GNUNET_assert (GNUNET_YES == fal->validated);
1639 if (fal->connected == GNUNET_YES)
1640 return; /* nothing to do */
1642 pos = fal->ready_list->addresses;
1645 if (GNUNET_YES == pos->connected)
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1649 "Marking address `%s' as no longer connected (due to connect on other address)\n",
1650 a2s (pos->ready_list->plugin->short_name,
1654 GNUNET_break (cnt == GNUNET_YES);
1656 pos->connected = GNUNET_NO;
1657 GNUNET_STATISTICS_update (stats,
1658 gettext_noop ("# connected addresses"),
1664 fal->connected = GNUNET_YES;
1665 if (GNUNET_YES == cnt)
1667 GNUNET_STATISTICS_update (stats,
1668 gettext_noop ("# connected addresses"),
1676 * Send the specified message to the specified client. Since multiple
1677 * messages may be pending for the same client at a time, this code
1678 * makes sure that no message is lost.
1680 * @param client client to transmit the message to
1681 * @param msg the message to send
1682 * @param may_drop can this message be dropped if the
1683 * message queue for this client is getting far too large?
1686 transmit_to_client (struct TransportClient *client,
1687 const struct GNUNET_MessageHeader *msg, int may_drop)
1689 struct ClientMessageQueueEntry *q;
1692 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1694 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1696 ("Dropping message of type %u and size %u, have %u messages pending (%u is the soft limit)\n"),
1699 client->message_count,
1701 GNUNET_STATISTICS_update (stats,
1702 gettext_noop ("# messages dropped due to slow client"),
1707 msize = ntohs (msg->size);
1708 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1709 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1710 memcpy (&q[1], msg, msize);
1711 GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1712 client->message_queue_tail,
1713 client->message_queue_tail,
1715 client->message_count++;
1716 if (client->th == NULL)
1718 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1720 GNUNET_TIME_UNIT_FOREVER_REL,
1721 &transmit_to_client_callback,
1723 GNUNET_assert (client->th != NULL);
1729 * Transmit a 'SEND_OK' notification to the given client for the
1732 * @param client who to notify
1733 * @param n neighbour to notify about, can be NULL (on failure)
1734 * @param target target of the transmission
1735 * @param result status code for the transmission request
1738 transmit_send_ok (struct TransportClient *client,
1739 struct NeighbourList *n,
1740 const struct GNUNET_PeerIdentity *target,
1743 struct SendOkMessage send_ok_msg;
1745 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1746 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1747 send_ok_msg.success = htonl (result);
1749 send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1751 send_ok_msg.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
1752 send_ok_msg.peer = *target;
1753 transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1758 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1759 * upon "completion" of a send request. This tells the API
1760 * that it is now legal to send another message to the given
1763 * @param cls closure, identifies the entry on the
1764 * message queue that was transmitted and the
1765 * client responsible for queuing the message
1766 * @param target the peer receiving the message
1767 * @param result GNUNET_OK on success, if the transmission
1768 * failed, we should not tell the client to transmit
1772 transmit_send_continuation (void *cls,
1773 const struct GNUNET_PeerIdentity *target,
1776 struct MessageQueue *mq = cls;
1777 struct NeighbourList *n;
1779 GNUNET_STATISTICS_update (stats,
1780 gettext_noop ("# bytes pending with plugins"),
1781 - (int64_t) mq->message_buf_size,
1783 if (result == GNUNET_OK)
1785 GNUNET_STATISTICS_update (stats,
1786 gettext_noop ("# bytes successfully transmitted by plugins"),
1787 mq->message_buf_size,
1792 GNUNET_STATISTICS_update (stats,
1793 gettext_noop ("# bytes with transmission failure by plugins"),
1794 mq->message_buf_size,
1797 if (mq->specific_address != NULL)
1799 if (result == GNUNET_OK)
1801 mq->specific_address->timeout =
1802 GNUNET_TIME_relative_to_absolute
1803 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1804 if (mq->specific_address->validated == GNUNET_YES)
1805 mark_address_connected (mq->specific_address);
1809 if (mq->specific_address->connected != GNUNET_NO)
1812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1813 "Marking address `%s' as no longer connected (due to transmission problem)\n",
1814 a2s (mq->specific_address->ready_list->plugin->short_name,
1815 mq->specific_address->addr,
1816 mq->specific_address->addrlen));
1818 GNUNET_STATISTICS_update (stats,
1819 gettext_noop ("# connected addresses"),
1822 mq->specific_address->connected = GNUNET_NO;
1825 if (! mq->internal_msg)
1826 mq->specific_address->in_transmit = GNUNET_NO;
1828 n = find_neighbour(&mq->neighbour_id);
1829 if (mq->client != NULL)
1830 transmit_send_ok (mq->client, n, target, result);
1833 try_transmission_to_peer (n);
1838 * Find an address in any of the available transports for
1839 * the given neighbour that would be good for message
1840 * transmission. This is essentially the transport selection
1843 * @param neighbour for whom to select an address
1844 * @return selected address, NULL if we have none
1846 struct ForeignAddressList *
1847 find_ready_address(struct NeighbourList *neighbour)
1849 struct ReadyList *head = neighbour->plugins;
1850 struct ForeignAddressList *addresses;
1851 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1852 struct ForeignAddressList *best_address;
1854 /* Hack to prefer unix domain sockets */
1855 struct ForeignAddressList *unix_address = NULL;
1857 best_address = NULL;
1858 while (head != NULL)
1860 addresses = head->addresses;
1861 while (addresses != NULL)
1863 if ( (addresses->timeout.abs_value < now.abs_value) &&
1864 (addresses->connected == GNUNET_YES) )
1867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1868 "Marking long-time inactive connection to `%4s' as down.\n",
1869 GNUNET_i2s (&neighbour->id));
1871 GNUNET_STATISTICS_update (stats,
1872 gettext_noop ("# connected addresses"),
1875 addresses->connected = GNUNET_NO;
1877 addresses = addresses->next;
1880 addresses = head->addresses;
1881 while (addresses != NULL)
1883 #if DEBUG_TRANSPORT > 1
1884 if (addresses->addr != NULL)
1885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1886 "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
1887 a2s (head->plugin->short_name,
1889 addresses->addrlen),
1890 GNUNET_i2s (&neighbour->id),
1891 addresses->connected,
1892 addresses->in_transmit,
1893 addresses->validated,
1894 addresses->connect_attempts,
1895 (unsigned long long) addresses->timeout.abs_value,
1896 (unsigned int) addresses->distance);
1898 if (0==strcmp(head->plugin->short_name,"unix"))
1900 if ((unix_address == NULL) || ((unix_address != NULL) &&
1901 (addresses->latency.rel_value < unix_address->latency.rel_value)))
1902 unix_address = addresses;
1904 if ( ( (best_address == NULL) ||
1905 (addresses->connected == GNUNET_YES) ||
1906 (best_address->connected == GNUNET_NO) ) &&
1907 (addresses->in_transmit == GNUNET_NO) &&
1908 ( (best_address == NULL) ||
1909 (addresses->latency.rel_value < best_address->latency.rel_value)) )
1910 best_address = addresses;
1911 /* FIXME: also give lower-latency addresses that are not
1912 connected a chance some times... */
1913 addresses = addresses->next;
1915 if (unix_address != NULL)
1919 if (unix_address != NULL)
1921 best_address = unix_address;
1923 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found unix address, forced this address\n");
1926 if (best_address != NULL)
1930 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1931 "Best address found (`%s') has latency of %llu ms.\n",
1932 (best_address->addrlen > 0)
1933 ? a2s (best_address->ready_list->plugin->short_name,
1935 best_address->addrlen)
1937 best_address->latency.rel_value);
1942 GNUNET_STATISTICS_update (stats,
1943 gettext_noop ("# transmission attempts failed (no address)"),
1948 return best_address;
1954 * We should re-try transmitting to the given peer,
1955 * hopefully we've learned something in the meantime.
1958 retry_transmission_task (void *cls,
1959 const struct GNUNET_SCHEDULER_TaskContext *tc)
1961 struct NeighbourList *n = cls;
1963 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1964 try_transmission_to_peer (n);
1969 * Check the ready list for the given neighbour and if a plugin is
1970 * ready for transmission (and if we have a message), do so!
1972 * @param neighbour target peer for which to transmit
1975 try_transmission_to_peer (struct NeighbourList *n)
1977 struct ReadyList *rl;
1978 struct MessageQueue *mq;
1979 struct GNUNET_TIME_Relative timeout;
1983 if (n->messages_head == NULL)
1986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1987 "Transmission queue for `%4s' is empty\n",
1988 GNUNET_i2s (&n->id));
1990 return; /* nothing to do */
1993 mq = n->messages_head;
1994 force_address = GNUNET_YES;
1995 if (mq->specific_address == NULL)
1998 mq->specific_address = ats_get_preferred_address(n);
1999 GNUNET_STATISTICS_update (stats,
2000 gettext_noop ("# transport selected peer address freely"),
2003 force_address = GNUNET_NO;
2005 if (mq->specific_address == NULL)
2007 GNUNET_STATISTICS_update (stats,
2008 gettext_noop ("# transport failed to selected peer address"),
2011 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
2012 if (timeout.rel_value == 0)
2015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2016 "No destination address available to transmit message of size %u to peer `%4s'\n",
2017 mq->message_buf_size,
2018 GNUNET_i2s (&mq->neighbour_id));
2020 GNUNET_STATISTICS_update (stats,
2021 gettext_noop ("# bytes in message queue for other peers"),
2022 - (int64_t) mq->message_buf_size,
2024 GNUNET_STATISTICS_update (stats,
2025 gettext_noop ("# bytes discarded (no destination address available)"),
2026 mq->message_buf_size,
2028 if (mq->client != NULL)
2029 transmit_send_ok (mq->client, n, &n->id, GNUNET_NO);
2030 GNUNET_CONTAINER_DLL_remove (n->messages_head,
2034 return; /* nobody ready */
2036 GNUNET_STATISTICS_update (stats,
2037 gettext_noop ("# message delivery deferred (no address)"),
2040 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
2041 GNUNET_SCHEDULER_cancel (n->retry_task);
2042 n->retry_task = GNUNET_SCHEDULER_add_delayed (timeout,
2043 &retry_transmission_task,
2046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2047 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
2048 mq->message_buf_size,
2049 GNUNET_i2s (&mq->neighbour_id),
2052 /* FIXME: might want to trigger peerinfo lookup here
2053 (unless that's already pending...) */
2056 GNUNET_CONTAINER_DLL_remove (n->messages_head,
2059 if (mq->specific_address->connected == GNUNET_NO)
2060 mq->specific_address->connect_attempts++;
2061 rl = mq->specific_address->ready_list;
2062 mq->plugin = rl->plugin;
2063 if (!mq->internal_msg)
2064 mq->specific_address->in_transmit = GNUNET_YES;
2066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2067 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
2068 mq->message_buf_size,
2069 GNUNET_i2s (&n->id),
2070 (mq->specific_address->addr != NULL)
2071 ? a2s (mq->plugin->short_name,
2072 mq->specific_address->addr,
2073 mq->specific_address->addrlen)
2075 rl->plugin->short_name);
2077 GNUNET_STATISTICS_update (stats,
2078 gettext_noop ("# bytes in message queue for other peers"),
2079 - (int64_t) mq->message_buf_size,
2081 GNUNET_STATISTICS_update (stats,
2082 gettext_noop ("# bytes pending with plugins"),
2083 mq->message_buf_size,
2085 ret = rl->plugin->api->send (rl->plugin->api->cls,
2088 mq->message_buf_size,
2090 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2091 mq->specific_address->session,
2092 mq->specific_address->addr,
2093 mq->specific_address->addrlen,
2095 &transmit_send_continuation, mq);
2098 /* failure, but 'send' would not call continuation in this case,
2099 so we need to do it here! */
2100 transmit_send_continuation (mq,
2108 * Send the specified message to the specified peer.
2110 * @param client source of the transmission request (can be NULL)
2111 * @param peer_address ForeignAddressList where we should send this message
2112 * @param priority how important is the message
2113 * @param timeout how long do we have to transmit?
2114 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
2115 * @param message_buf_size total size of all messages in message_buf
2116 * @param is_internal is this an internal message; these are pre-pended and
2117 * also do not count for plugins being "ready" to transmit
2118 * @param neighbour handle to the neighbour for transmission
2121 transmit_to_peer (struct TransportClient *client,
2122 struct ForeignAddressList *peer_address,
2123 unsigned int priority,
2124 struct GNUNET_TIME_Relative timeout,
2125 const char *message_buf,
2126 size_t message_buf_size,
2127 int is_internal, struct NeighbourList *neighbour)
2129 struct MessageQueue *mq;
2134 /* check for duplicate submission */
2135 mq = neighbour->messages_head;
2138 if (mq->client == client)
2140 /* client transmitted to same peer twice
2141 before getting SEND_OK! */
2149 GNUNET_STATISTICS_update (stats,
2150 gettext_noop ("# bytes in message queue for other peers"),
2153 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
2154 mq->specific_address = peer_address;
2155 mq->client = client;
2156 /* FIXME: this memcpy can be up to 7% of our total runtime! */
2157 memcpy (&mq[1], message_buf, message_buf_size);
2158 mq->message_buf = (const char*) &mq[1];
2159 mq->message_buf_size = message_buf_size;
2160 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
2161 mq->internal_msg = is_internal;
2162 mq->priority = priority;
2163 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
2165 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
2166 neighbour->messages_tail,
2169 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
2170 neighbour->messages_tail,
2171 neighbour->messages_tail,
2173 try_transmission_to_peer (neighbour);
2180 struct GeneratorContext
2182 struct TransportPlugin *plug_pos;
2183 struct OwnAddressList *addr_pos;
2184 struct GNUNET_TIME_Absolute expiration;
2192 address_generator (void *cls, size_t max, void *buf)
2194 struct GeneratorContext *gc = cls;
2197 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
2199 gc->plug_pos = gc->plug_pos->next;
2200 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
2202 if (NULL == gc->plug_pos)
2207 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
2210 gc->addr_pos->addrlen, buf, max);
2211 gc->addr_pos = gc->addr_pos->next;
2217 * Construct our HELLO message from all of the addresses of
2218 * all of the transports.
2223 struct GNUNET_HELLO_Message *hello;
2224 struct TransportClient *cpos;
2225 struct NeighbourList *npos;
2226 struct GeneratorContext gc;
2228 gc.plug_pos = plugins;
2229 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
2230 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2231 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
2233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2234 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
2236 GNUNET_STATISTICS_update (stats,
2237 gettext_noop ("# refreshed my HELLO"),
2241 while (cpos != NULL)
2243 transmit_to_client (cpos,
2244 (const struct GNUNET_MessageHeader *) hello,
2249 GNUNET_free_non_null (our_hello);
2251 GNUNET_PEERINFO_add_peer (peerinfo, our_hello);
2253 while (npos != NULL)
2256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2257 "Transmitting updated `%s' to neighbour `%4s'\n",
2258 "HELLO", GNUNET_i2s (&npos->id));
2260 GNUNET_STATISTICS_update (stats,
2261 gettext_noop ("# transmitted my HELLO to other peers"),
2264 transmit_to_peer (NULL, NULL, 0,
2265 HELLO_ADDRESS_EXPIRATION,
2266 (const char *) our_hello,
2267 GNUNET_HELLO_size(our_hello),
2275 * Task used to clean up expired addresses for a plugin.
2277 * @param cls closure
2281 expire_address_task (void *cls,
2282 const struct GNUNET_SCHEDULER_TaskContext *tc);
2286 * Update the list of addresses for this plugin,
2287 * expiring those that are past their expiration date.
2289 * @param plugin addresses of which plugin should be recomputed?
2290 * @param fresh set to GNUNET_YES if a new address was added
2291 * and we need to regenerate the HELLO even if nobody
2295 update_addresses (struct TransportPlugin *plugin,
2298 static struct GNUNET_TIME_Absolute last_update;
2299 struct GNUNET_TIME_Relative min_remaining;
2300 struct GNUNET_TIME_Relative remaining;
2301 struct GNUNET_TIME_Absolute now;
2302 struct OwnAddressList *pos;
2303 struct OwnAddressList *prev;
2304 struct OwnAddressList *next;
2307 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_TASK)
2308 GNUNET_SCHEDULER_cancel (plugin->address_update_task);
2309 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
2310 now = GNUNET_TIME_absolute_get ();
2311 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
2312 expired = (GNUNET_TIME_absolute_get_duration (last_update).rel_value > (HELLO_ADDRESS_EXPIRATION.rel_value / 4));
2314 pos = plugin->addresses;
2318 if (pos->expires.abs_value < now.abs_value)
2320 expired = GNUNET_YES;
2322 plugin->addresses = pos->next;
2324 prev->next = pos->next;
2329 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
2330 if (remaining.rel_value < min_remaining.rel_value)
2331 min_remaining = remaining;
2337 if (expired || fresh)
2342 min_remaining = GNUNET_TIME_relative_min (min_remaining,
2343 GNUNET_TIME_relative_divide (HELLO_ADDRESS_EXPIRATION,
2345 plugin->address_update_task
2346 = GNUNET_SCHEDULER_add_delayed (min_remaining,
2347 &expire_address_task, plugin);
2352 * Task used to clean up expired addresses for a plugin.
2354 * @param cls closure
2358 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2360 struct TransportPlugin *plugin = cls;
2362 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
2363 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2364 update_addresses (plugin, GNUNET_NO);
2369 * Iterator over hash map entries that NULLs the session of validation
2370 * entries that match the given session.
2372 * @param cls closure (the 'struct Session*' to match against)
2373 * @param key current key code (peer ID, not used)
2374 * @param value value in the hash map ('struct ValidationEntry*')
2375 * @return GNUNET_YES (we should continue to iterate)
2378 remove_session_validations (void *cls,
2379 const GNUNET_HashCode * key,
2382 struct Session *session = cls;
2383 struct ValidationEntry *ve = value;
2385 if (session == ve->session)
2392 * We've been disconnected from the other peer (for some
2393 * connection-oriented transport). Either quickly
2394 * re-establish the connection or signal the disconnect
2397 * Only signal CORE level disconnect if ALL addresses
2398 * for the peer are exhausted.
2400 * @param p overall plugin context
2401 * @param nl neighbour that was disconnected
2404 try_fast_reconnect (struct TransportPlugin *p,
2405 struct NeighbourList *nl)
2407 /* FIXME-MW: fast reconnect / transport switching not implemented... */
2408 /* Note: the idea here is to hide problems with transports (or
2409 switching between plugins) from the core to eliminate the need to
2410 re-negotiate session keys and the like; OTOH, we should tell core
2411 quickly (much faster than timeout) `if a connection was lost and
2412 could not be re-established (i.e. other peer went down or is
2413 unable / refuses to communicate);
2415 So we should consider:
2416 1) ideally: our own willingness / need to connect
2417 2) prior failures to connect to this peer (by plugin)
2418 3) ideally: reasons why other peer terminated (as far as knowable)
2420 Most importantly, it must be POSSIBLE for another peer to terminate
2421 a connection for a while (without us instantly re-establishing it).
2422 Similarly, if another peer is gone we should quickly notify CORE.
2423 OTOH, if there was a minor glitch (i.e. crash of gnunet-service-transport
2424 on the other end), we should reconnect in such a way that BOTH CORE
2425 services never even notice.
2426 Furthermore, the same mechanism (or small variation) could be used
2427 to switch to a better-performing plugin (ATS).
2429 Finally, this needs to be tested throughly... */
2432 * GNUNET_NO in the call below makes transport disconnect the peer,
2433 * even if only a single address (out of say, six) went away. This
2434 * function must be careful to ONLY disconnect if the peer is gone,
2435 * not just a specifi address.
2437 * More specifically, half the places it was used had it WRONG.
2440 /* No reconnect, signal disconnect instead! */
2441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2442 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2443 "try_fast_reconnect");
2444 disconnect_neighbour (nl, GNUNET_YES);
2449 * Function that will be called whenever the plugin internally
2450 * cleans up a session pointer and hence the service needs to
2451 * discard all of those sessions as well. Plugins that do not
2452 * use sessions can simply omit calling this function and always
2453 * use NULL wherever a session pointer is needed.
2455 * @param cls closure
2456 * @param peer which peer was the session for
2457 * @param session which session is being destoyed
2460 plugin_env_session_end (void *cls,
2461 const struct GNUNET_PeerIdentity *peer,
2462 struct Session *session)
2464 struct TransportPlugin *p = cls;
2465 struct NeighbourList *nl;
2466 struct ReadyList *rl;
2467 struct ForeignAddressList *pos;
2468 struct ForeignAddressList *prev;
2470 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2471 &remove_session_validations,
2473 nl = find_neighbour (peer);
2475 return; /* was never marked as connected */
2479 if (rl->plugin == p)
2484 return; /* was never marked as connected */
2486 pos = rl->addresses;
2487 while ( (pos != NULL) &&
2488 (pos->session != session) )
2494 return; /* was never marked as connected */
2495 pos->session = NULL;
2496 if (pos->addrlen != 0)
2498 if (nl->received_pong != GNUNET_NO)
2499 try_fast_reconnect (p, nl);
2502 /* was inbound connection, free 'pos' */
2504 rl->addresses = pos->next;
2506 prev->next = pos->next;
2507 if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2509 GNUNET_SCHEDULER_cancel (pos->revalidate_task);
2510 pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2513 if (nl->received_pong == GNUNET_NO)
2514 return; /* nothing to do, never connected... */
2515 /* check if we have any validated addresses left */
2516 pos = rl->addresses;
2521 try_fast_reconnect (p, nl);
2526 /* no valid addresses left, signal disconnect! */
2528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2529 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2530 "plugin_env_session_end");
2531 /* FIXME: This doesn't mean there are no addresses left for this PEER,
2532 * it means there aren't any left for this PLUGIN/PEER combination! So
2533 * calling disconnect_neighbor here with GNUNET_NO forces disconnect
2534 * when it isn't necessary. Using GNUNET_YES at least checks to see
2535 * if there are any addresses that work first, so as not to overdo it.
2538 disconnect_neighbour (nl, GNUNET_YES);
2543 * Function that must be called by each plugin to notify the
2544 * transport service about the addresses under which the transport
2545 * provided by the plugin can be reached.
2547 * @param cls closure
2548 * @param name name of the transport that generated the address
2549 * @param addr one of the addresses of the host, NULL for the last address
2550 * the specific address format depends on the transport
2551 * @param addrlen length of the address
2552 * @param expires when should this address automatically expire?
2555 plugin_env_notify_address (void *cls,
2559 struct GNUNET_TIME_Relative expires)
2561 struct TransportPlugin *p = cls;
2562 struct OwnAddressList *al;
2563 struct GNUNET_TIME_Absolute abex;
2565 GNUNET_assert (addr != NULL);
2566 abex = GNUNET_TIME_relative_to_absolute (expires);
2567 GNUNET_assert (p == find_transport (name));
2571 if ( (addrlen == al->addrlen) &&
2572 (0 == memcmp (addr, &al[1], addrlen)) )
2575 update_addresses (p, GNUNET_NO);
2580 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2581 al->next = p->addresses;
2584 al->addrlen = addrlen;
2585 memcpy (&al[1], addr, addrlen);
2586 update_addresses (p, GNUNET_YES);
2591 * Notify all of our clients about a peer connecting.
2594 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2595 struct GNUNET_TIME_Relative latency,
2598 struct ConnectInfoMessage * cim;
2599 struct TransportClient *cpos;
2604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2605 "Notifying clients about connection from `%s'\n",
2608 GNUNET_STATISTICS_update (stats,
2609 gettext_noop ("# peers connected"),
2614 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
2615 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2619 cim = GNUNET_malloc (size);
2621 cim->header.size = htons (size);
2622 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2623 cim->ats_count = htonl(2);
2624 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2625 (&(cim->ats))[0].value = htonl (distance);
2626 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
2627 (&(cim->ats))[1].value = htonl ((uint32_t) latency.rel_value);
2628 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2629 (&(cim->ats))[2].value = htonl (0);
2630 memcpy (&cim->id, peer, sizeof (struct GNUNET_PeerIdentity));
2632 /* notify ats about connecting peer */
2633 ats_notify_peer_connect (peer, &(cim->ats), 2);
2636 while (cpos != NULL)
2638 transmit_to_client (cpos, &(cim->header), GNUNET_NO);
2647 * Notify all of our clients about a peer disconnecting.
2650 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2652 struct DisconnectInfoMessage dim;
2653 struct TransportClient *cpos;
2656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2657 "Notifying clients about lost connection to `%s'\n",
2660 GNUNET_STATISTICS_update (stats,
2661 gettext_noop ("# peers connected"),
2664 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2665 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2666 dim.reserved = htonl (0);
2667 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2669 /* notify ats about connecting peer */
2670 ats_notify_peer_disconnect (peer);
2673 while (cpos != NULL)
2675 transmit_to_client (cpos, &dim.header, GNUNET_NO);
2682 * Find a ForeignAddressList entry for the given neighbour
2683 * that matches the given address and transport.
2685 * @param neighbour which peer we care about
2686 * @param tname name of the transport plugin
2687 * @param session session to look for, NULL for 'any'; otherwise
2688 * can be used for the service to "learn" this session ID
2690 * @param addr binary address
2691 * @param addrlen length of addr
2692 * @return NULL if no such entry exists
2694 static struct ForeignAddressList *
2695 find_peer_address(struct NeighbourList *neighbour,
2697 struct Session *session,
2701 struct ReadyList *head;
2702 struct ForeignAddressList *pos;
2704 head = neighbour->plugins;
2705 while (head != NULL)
2707 if (0 == strcmp (tname, head->plugin->short_name))
2713 pos = head->addresses;
2714 while ( (pos != NULL) &&
2715 ( (pos->addrlen != addrlen) ||
2716 (memcmp(pos->addr, addr, addrlen) != 0) ) )
2718 if ( (session != NULL) &&
2719 (pos->session == session) )
2723 if ( (session != NULL) && (pos != NULL) )
2724 pos->session = session; /* learn it! */
2730 * Get the peer address struct for the given neighbour and
2731 * address. If it doesn't yet exist, create it.
2733 * @param neighbour which peer we care about
2734 * @param tname name of the transport plugin
2735 * @param session session of the plugin, or NULL for none
2736 * @param addr binary address
2737 * @param addrlen length of addr
2738 * @return NULL if we do not have a transport plugin for 'tname'
2740 static struct ForeignAddressList *
2741 add_peer_address (struct NeighbourList *neighbour,
2743 struct Session *session,
2747 struct ReadyList *head;
2748 struct ForeignAddressList *ret;
2751 ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2754 head = neighbour->plugins;
2756 while (head != NULL)
2758 if (0 == strcmp (tname, head->plugin->short_name))
2764 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2765 ret->session = session;
2766 if ((addrlen > 0) && (addr != NULL))
2768 ret->addr = (const char*) &ret[1];
2769 memcpy (&ret[1], addr, addrlen);
2776 ret->ressources = GNUNET_malloc(available_ressources * sizeof (struct ATS_ressource_entry));
2778 for (c=0; c<available_ressources; c++)
2780 struct ATS_ressource_entry *r = ret->ressources;
2782 r[c].atis_index = ressources[c].atis_index;
2783 if (0 == strcmp(neighbour->plugins->plugin->short_name,"unix"))
2785 r[c].c = ressources[c].c_unix;
2788 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"udp"))
2790 r[c].c = ressources[c].c_udp;
2793 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"tcp"))
2795 r[c].c = ressources[c].c_tcp;
2798 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"http"))
2800 r[c].c = ressources[c].c_http;
2803 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"https"))
2805 r[c].c = ressources[c].c_https;
2808 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"wlan"))
2810 r[c].c = ressources[c].c_wlan;
2816 r[c].c = ressources[c].c_default;
2817 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"Assigning default cost to peer `%s' addr plugin `%s'! This should not happen!",
2818 GNUNET_i2s(&neighbour->peer), neighbour->plugins->plugin->short_name);
2822 ret->quality = GNUNET_malloc (available_quality_metrics * sizeof (struct ATS_quality_entry));
2823 ret->addrlen = addrlen;
2824 ret->expires = GNUNET_TIME_relative_to_absolute
2825 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2826 ret->latency = GNUNET_TIME_relative_get_forever();
2828 ret->timeout = GNUNET_TIME_relative_to_absolute
2829 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2830 ret->ready_list = head;
2831 ret->next = head->addresses;
2832 head->addresses = ret;
2838 * Closure for 'add_validated_address'.
2840 struct AddValidatedAddressContext
2843 * Entry that has been validated.
2845 const struct ValidationEntry *ve;
2848 * Flag set after we have added the address so
2849 * that we terminate the iteration next time.
2856 * Callback function used to fill a buffer of max bytes with a list of
2857 * addresses in the format used by HELLOs. Should use
2858 * "GNUNET_HELLO_add_address" as a helper function.
2860 * @param cls the 'struct AddValidatedAddressContext' with the validated address
2861 * @param max maximum number of bytes that can be written to buf
2862 * @param buf where to write the address information
2863 * @return number of bytes written, 0 to signal the
2864 * end of the iteration.
2867 add_validated_address (void *cls,
2868 size_t max, void *buf)
2870 struct AddValidatedAddressContext *avac = cls;
2871 const struct ValidationEntry *ve = avac->ve;
2873 if (GNUNET_YES == avac->done)
2875 avac->done = GNUNET_YES;
2876 return GNUNET_HELLO_add_address (ve->transport_name,
2877 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2887 * Closure for 'check_address_exists'.
2889 struct CheckAddressExistsClosure
2892 * Address to check for.
2897 * Name of the transport.
2904 struct Session *session;
2907 * Set to GNUNET_YES if the address exists.
2920 * Iterator over hash map entries. Checks if the given
2921 * validation entry is for the same address as what is given
2924 * @param cls the 'struct CheckAddressExistsClosure*'
2925 * @param key current key code (ignored)
2926 * @param value value in the hash map ('struct ValidationEntry')
2927 * @return GNUNET_YES if we should continue to
2928 * iterate (mismatch), GNUNET_NO if not (entry matched)
2931 check_address_exists (void *cls,
2932 const GNUNET_HashCode * key,
2935 struct CheckAddressExistsClosure *caec = cls;
2936 struct ValidationEntry *ve = value;
2938 if ( (0 == strcmp (caec->tname,
2939 ve->transport_name)) &&
2940 (caec->addrlen == ve->addrlen) &&
2941 (0 == memcmp (caec->addr,
2945 caec->exists = GNUNET_YES;
2948 if ( (ve->session != NULL) &&
2949 (caec->session == ve->session) )
2951 caec->exists = GNUNET_YES;
2960 * Iterator to free entries in the validation_map.
2962 * @param cls closure (unused)
2963 * @param key current key code
2964 * @param value value in the hash map (validation to abort)
2965 * @return GNUNET_YES (always)
2968 abort_validation (void *cls,
2969 const GNUNET_HashCode * key,
2972 struct ValidationEntry *va = value;
2974 if (GNUNET_SCHEDULER_NO_TASK != va->timeout_task)
2975 GNUNET_SCHEDULER_cancel (va->timeout_task);
2976 GNUNET_free (va->transport_name);
2977 if (va->chvc != NULL)
2979 va->chvc->ve_count--;
2980 if (va->chvc->ve_count == 0)
2982 GNUNET_CONTAINER_DLL_remove (chvc_head,
2985 GNUNET_free (va->chvc);
2995 * HELLO validation cleanup task (validation failed).
2997 * @param cls the 'struct ValidationEntry' that failed
2998 * @param tc scheduler context (unused)
3001 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3003 struct ValidationEntry *va = cls;
3004 struct GNUNET_PeerIdentity pid;
3006 va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3007 GNUNET_STATISTICS_update (stats,
3008 gettext_noop ("# address validation timeouts"),
3011 GNUNET_CRYPTO_hash (&va->publicKey,
3013 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3015 GNUNET_break (GNUNET_OK ==
3016 GNUNET_CONTAINER_multihashmap_remove (validation_map,
3019 abort_validation (NULL, NULL, va);
3024 neighbour_timeout_task (void *cls,
3025 const struct GNUNET_SCHEDULER_TaskContext *tc)
3027 struct NeighbourList *n = cls;
3030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
3031 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
3033 GNUNET_STATISTICS_update (stats,
3034 gettext_noop ("# disconnects due to timeout"),
3037 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3038 disconnect_neighbour (n, GNUNET_NO);
3043 * Schedule the job that will cause us to send a PING to the
3044 * foreign address to evaluate its validity and latency.
3046 * @param fal address to PING
3049 schedule_next_ping (struct ForeignAddressList *fal);
3053 * Add the given address to the list of foreign addresses
3054 * available for the given peer (check for duplicates).
3056 * @param cls the respective 'struct NeighbourList' to update
3057 * @param tname name of the transport
3058 * @param expiration expiration time
3059 * @param addr the address
3060 * @param addrlen length of the address
3061 * @return GNUNET_OK (always)
3064 add_to_foreign_address_list (void *cls,
3066 struct GNUNET_TIME_Absolute expiration,
3070 struct NeighbourList *n = cls;
3071 struct ForeignAddressList *fal;
3074 GNUNET_STATISTICS_update (stats,
3075 gettext_noop ("# valid peer addresses returned by PEERINFO"),
3079 fal = find_peer_address (n, tname, NULL, addr, addrlen);
3082 #if DEBUG_TRANSPORT_HELLO
3083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3084 "Adding address `%s' (%s) for peer `%4s' due to PEERINFO data for %llums.\n",
3085 a2s (tname, addr, addrlen),
3087 GNUNET_i2s (&n->id),
3088 expiration.abs_value);
3090 fal = add_peer_address (n, tname, NULL, addr, addrlen);
3093 GNUNET_STATISTICS_update (stats,
3094 gettext_noop ("# previously validated addresses lacking transport"),
3100 fal->expires = GNUNET_TIME_absolute_max (expiration,
3102 schedule_next_ping (fal);
3108 fal->expires = GNUNET_TIME_absolute_max (expiration,
3113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3114 "Failed to add new address for `%4s'\n",
3115 GNUNET_i2s (&n->id));
3118 if (fal->validated == GNUNET_NO)
3120 fal->validated = GNUNET_YES;
3121 GNUNET_STATISTICS_update (stats,
3122 gettext_noop ("# peer addresses considered valid"),
3126 if (try == GNUNET_YES)
3128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3129 "Have new addresses, will try to trigger transmissions.\n");
3130 try_transmission_to_peer (n);
3137 * Add addresses in validated HELLO "h" to the set of addresses
3138 * we have for this peer.
3140 * @param cls closure ('struct NeighbourList*')
3141 * @param peer id of the peer, NULL for last call
3142 * @param h hello message for the peer (can be NULL)
3143 * @param err_msg NULL if successful, otherwise contains error message
3146 add_hello_for_peer (void *cls,
3147 const struct GNUNET_PeerIdentity *peer,
3148 const struct GNUNET_HELLO_Message *h,
3149 const char *err_msg)
3151 struct NeighbourList *n = cls;
3153 if (err_msg != NULL)
3155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3156 _("Error in communication with PEERINFO service\n"));
3161 GNUNET_STATISTICS_update (stats,
3162 gettext_noop ("# outstanding peerinfo iterate requests"),
3169 return; /* no HELLO available */
3171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3172 "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
3176 if (GNUNET_YES != n->public_key_valid)
3178 GNUNET_HELLO_get_key (h, &n->publicKey);
3179 n->public_key_valid = GNUNET_YES;
3181 GNUNET_HELLO_iterate_addresses (h,
3183 &add_to_foreign_address_list,
3189 * Create a fresh entry in our neighbour list for the given peer.
3190 * Will try to transmit our current HELLO to the new neighbour.
3191 * Do not call this function directly, use 'setup_peer_check_blacklist.
3193 * @param peer the peer for which we create the entry
3194 * @param do_hello should we schedule transmitting a HELLO
3195 * @return the new neighbour list entry
3197 static struct NeighbourList *
3198 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
3201 struct NeighbourList *n;
3202 struct TransportPlugin *tp;
3203 struct ReadyList *rl;
3206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3207 "Setting up state for neighbour `%4s'\n",
3210 GNUNET_assert (our_hello != NULL);
3211 GNUNET_STATISTICS_update (stats,
3212 gettext_noop ("# active neighbours"),
3215 n = GNUNET_malloc (sizeof (struct NeighbourList));
3216 n->next = neighbours;
3220 GNUNET_TIME_relative_to_absolute
3221 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3222 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
3223 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3224 MAX_BANDWIDTH_CARRY_S);
3228 if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
3230 rl = GNUNET_malloc (sizeof (struct ReadyList));
3232 rl->next = n->plugins;
3235 rl->addresses = NULL;
3239 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
3241 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
3242 &neighbour_timeout_task, n);
3245 GNUNET_STATISTICS_update (stats,
3246 gettext_noop ("# peerinfo new neighbor iterate requests"),
3249 GNUNET_STATISTICS_update (stats,
3250 gettext_noop ("# outstanding peerinfo iterate requests"),
3253 n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
3254 GNUNET_TIME_UNIT_FOREVER_REL,
3255 &add_hello_for_peer, n);
3257 GNUNET_STATISTICS_update (stats,
3258 gettext_noop ("# HELLO's sent to new neighbors"),
3261 transmit_to_peer (NULL, NULL, 0,
3262 HELLO_ADDRESS_EXPIRATION,
3263 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
3271 * Function called after we have checked if communicating
3272 * with a given peer is acceptable.
3274 * @param cls closure
3275 * @param n NULL if communication is not acceptable
3277 typedef void (*SetupContinuation)(void *cls,
3278 struct NeighbourList *n);
3282 * Information kept for each client registered to perform
3288 * This is a linked list.
3290 struct Blacklisters *next;
3293 * This is a linked list.
3295 struct Blacklisters *prev;
3298 * Client responsible for this entry.
3300 struct GNUNET_SERVER_Client *client;
3303 * Blacklist check that we're currently performing.
3305 struct BlacklistCheck *bc;
3311 * Head of DLL of blacklisting clients.
3313 static struct Blacklisters *bl_head;
3316 * Tail of DLL of blacklisting clients.
3318 static struct Blacklisters *bl_tail;
3322 * Context we use when performing a blacklist check.
3324 struct BlacklistCheck
3328 * This is a linked list.
3330 struct BlacklistCheck *next;
3333 * This is a linked list.
3335 struct BlacklistCheck *prev;
3338 * Peer being checked.
3340 struct GNUNET_PeerIdentity peer;
3343 * Option for setup neighbour afterwards.
3348 * Continuation to call with the result.
3350 SetupContinuation cont;
3358 * Current transmission request handle for this client, or NULL if no
3359 * request is pending.
3361 struct GNUNET_CONNECTION_TransmitHandle *th;
3364 * Our current position in the blacklisters list.
3366 struct Blacklisters *bl_pos;
3369 * Current task performing the check.
3371 GNUNET_SCHEDULER_TaskIdentifier task;
3376 * Head of DLL of active blacklisting queries.
3378 static struct BlacklistCheck *bc_head;
3381 * Tail of DLL of active blacklisting queries.
3383 static struct BlacklistCheck *bc_tail;
3387 * Perform next action in the blacklist check.
3389 * @param cls the 'struct BlacklistCheck*'
3393 do_blacklist_check (void *cls,
3394 const struct GNUNET_SCHEDULER_TaskContext *tc);
3397 * Transmit blacklist query to the client.
3399 * @param cls the 'struct BlacklistCheck'
3400 * @param size number of bytes allowed
3401 * @param buf where to copy the message
3402 * @return number of bytes copied to buf
3405 transmit_blacklist_message (void *cls,
3409 struct BlacklistCheck *bc = cls;
3410 struct Blacklisters *bl;
3411 struct BlacklistMessage bm;
3416 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
3417 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3422 bm.header.size = htons (sizeof (struct BlacklistMessage));
3423 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
3424 bm.is_allowed = htonl (0);
3426 memcpy (buf, &bm, sizeof (bm));
3427 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
3433 * Perform next action in the blacklist check.
3435 * @param cls the 'struct BlacklistCheck*'
3439 do_blacklist_check (void *cls,
3440 const struct GNUNET_SCHEDULER_TaskContext *tc)
3442 struct BlacklistCheck *bc = cls;
3443 struct Blacklisters *bl;
3445 bc->task = GNUNET_SCHEDULER_NO_TASK;
3449 bc->cont (bc->cont_cls,
3450 setup_new_neighbour (&bc->peer, bc->do_hello));
3457 bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
3458 sizeof (struct BlacklistMessage),
3459 GNUNET_TIME_UNIT_FOREVER_REL,
3460 &transmit_blacklist_message,
3467 * Obtain a 'struct NeighbourList' for the given peer. If such an entry
3468 * does not yet exist, check the blacklist. If the blacklist says creating
3469 * one is acceptable, create one and call the continuation; otherwise
3470 * call the continuation with NULL.
3472 * @param peer peer to setup or look up a struct NeighbourList for
3473 * @param do_hello should we also schedule sending our HELLO to the peer
3474 * if this is a new record
3475 * @param cont function to call with the 'struct NeigbhbourList*'
3476 * @param cont_cls closure for cont
3479 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
3481 SetupContinuation cont,
3484 struct NeighbourList *n;
3485 struct BlacklistCheck *bc;
3487 n = find_neighbour(peer);
3494 if (bl_head == NULL)
3497 cont (cont_cls, setup_new_neighbour (peer, do_hello));
3499 setup_new_neighbour(peer, do_hello);
3502 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3503 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3505 bc->do_hello = do_hello;
3507 bc->cont_cls = cont_cls;
3508 bc->bl_pos = bl_head;
3509 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3515 * Function called with the result of querying a new blacklister about
3516 * it being allowed (or not) to continue to talk to an existing neighbour.
3518 * @param cls the original 'struct NeighbourList'
3519 * @param n NULL if we need to disconnect
3522 confirm_or_drop_neighbour (void *cls,
3523 struct NeighbourList *n)
3525 struct NeighbourList * orig = cls;
3529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3530 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&orig->id),
3531 "confirm_or_drop_neighboUr");
3532 disconnect_neighbour (orig, GNUNET_NO);
3538 * Handle a request to start a blacklist.
3540 * @param cls closure (always NULL)
3541 * @param client identification of the client
3542 * @param message the actual message
3545 handle_blacklist_init (void *cls,
3546 struct GNUNET_SERVER_Client *client,
3547 const struct GNUNET_MessageHeader *message)
3549 struct Blacklisters *bl;
3550 struct BlacklistCheck *bc;
3551 struct NeighbourList *n;
3556 if (bl->client == client)
3559 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3564 bl = GNUNET_malloc (sizeof (struct Blacklisters));
3565 bl->client = client;
3566 GNUNET_SERVER_client_keep (client);
3567 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
3568 /* confirm that all existing connections are OK! */
3572 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3573 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3575 bc->do_hello = GNUNET_NO;
3576 bc->cont = &confirm_or_drop_neighbour;
3579 if (n == neighbours) /* all would wait for the same client, no need to
3580 create more than just the first task right now */
3581 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3589 * Handle a request to blacklist a peer.
3591 * @param cls closure (always NULL)
3592 * @param client identification of the client
3593 * @param message the actual message
3596 handle_blacklist_reply (void *cls,
3597 struct GNUNET_SERVER_Client *client,
3598 const struct GNUNET_MessageHeader *message)
3600 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
3601 struct Blacklisters *bl;
3602 struct BlacklistCheck *bc;
3605 while ( (bl != NULL) &&
3606 (bl->client != client) )
3610 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3615 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
3617 bc->cont (bc->cont_cls, NULL);
3618 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
3623 bc->bl_pos = bc->bl_pos->next;
3624 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3627 /* check if any other bc's are waiting for this blacklister */
3631 if ( (bc->bl_pos == bl) &&
3632 (GNUNET_SCHEDULER_NO_TASK == bc->task) )
3633 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3641 * Send periodic PING messages to a given foreign address.
3643 * @param cls our 'struct PeriodicValidationContext*'
3644 * @param tc task context
3647 send_periodic_ping (void *cls,
3648 const struct GNUNET_SCHEDULER_TaskContext *tc)
3650 struct ForeignAddressList *peer_address = cls;
3651 struct TransportPlugin *tp;
3652 struct ValidationEntry *va;
3653 struct NeighbourList *neighbour;
3654 struct TransportPingMessage ping;
3655 struct CheckAddressExistsClosure caec;
3657 uint16_t hello_size;
3661 peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3662 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3664 tp = peer_address->ready_list->plugin;
3665 neighbour = peer_address->ready_list->neighbour;
3666 if (GNUNET_YES != neighbour->public_key_valid)
3668 /* no public key yet, try again later */
3669 schedule_next_ping (peer_address);
3672 caec.addr = peer_address->addr;
3673 caec.addrlen = peer_address->addrlen;
3674 caec.tname = tp->short_name;
3675 caec.session = peer_address->session;
3676 caec.exists = GNUNET_NO;
3677 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3678 &check_address_exists,
3680 if (caec.exists == GNUNET_YES)
3682 /* During validation attempts we will likely trigger the other
3683 peer trying to validate our address which in turn will cause
3684 it to send us its HELLO, so we expect to hit this case rather
3685 frequently. Only print something if we are very verbose. */
3686 #if DEBUG_TRANSPORT > 1
3687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3688 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3689 (peer_address->addr != NULL)
3690 ? a2s (tp->short_name,
3692 peer_address->addrlen)
3695 GNUNET_i2s (&neighbour->id));
3697 schedule_next_ping (peer_address);
3700 va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3701 va->transport_name = GNUNET_strdup (tp->short_name);
3702 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
3704 va->send_time = GNUNET_TIME_absolute_get();
3705 va->session = peer_address->session;
3706 if (peer_address->addr != NULL)
3708 va->addr = (const void*) &va[1];
3709 memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3710 va->addrlen = peer_address->addrlen;
3712 memcpy(&va->publicKey,
3713 &neighbour->publicKey,
3714 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3716 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
3717 &timeout_hello_validation,
3719 GNUNET_CONTAINER_multihashmap_put (validation_map,
3720 &neighbour->id.hashPubKey,
3722 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3724 if (peer_address->validated != GNUNET_YES)
3725 hello_size = GNUNET_HELLO_size(our_hello);
3729 tsize = sizeof(struct TransportPingMessage) + hello_size;
3731 if (peer_address->addr != NULL)
3733 slen = strlen (tp->short_name) + 1;
3734 tsize += slen + peer_address->addrlen;
3738 slen = 0; /* make gcc happy */
3740 message_buf = GNUNET_malloc(tsize);
3741 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3742 ping.challenge = htonl(va->challenge);
3743 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3744 if (peer_address->validated != GNUNET_YES)
3746 memcpy(message_buf, our_hello, hello_size);
3749 if (peer_address->addr != NULL)
3751 ping.header.size = htons(sizeof(struct TransportPingMessage) +
3752 peer_address->addrlen +
3754 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3757 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3759 peer_address->addrlen);
3763 ping.header.size = htons(sizeof(struct TransportPingMessage));
3766 memcpy(&message_buf[hello_size],
3768 sizeof(struct TransportPingMessage));
3770 #if DEBUG_TRANSPORT_REVALIDATION
3771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3772 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s'\n",
3773 (peer_address->addr != NULL)
3774 ? a2s (peer_address->plugin->short_name,
3776 peer_address->addrlen)
3779 GNUNET_i2s (&neighbour->id),
3780 "HELLO", hello_size,
3783 if (peer_address->validated != GNUNET_YES)
3784 GNUNET_STATISTICS_update (stats,
3785 gettext_noop ("# PING with HELLO messages sent"),
3789 GNUNET_STATISTICS_update (stats,
3790 gettext_noop ("# PING without HELLO messages sent"),
3793 GNUNET_STATISTICS_update (stats,
3794 gettext_noop ("# PING messages sent for re-validation"),
3797 transmit_to_peer (NULL, peer_address,
3798 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3799 HELLO_VERIFICATION_TIMEOUT,
3801 GNUNET_YES, neighbour);
3802 GNUNET_free(message_buf);
3803 schedule_next_ping (peer_address);
3808 * Schedule the job that will cause us to send a PING to the
3809 * foreign address to evaluate its validity and latency.
3811 * @param fal address to PING
3814 schedule_next_ping (struct ForeignAddressList *fal)
3816 struct GNUNET_TIME_Relative delay;
3818 if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3820 delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3821 delay.rel_value /= 2; /* do before expiration */
3822 delay = GNUNET_TIME_relative_min (delay,
3823 LATENCY_EVALUATION_MAX_DELAY);
3824 if (GNUNET_YES != fal->estimated)
3826 delay = GNUNET_TIME_UNIT_ZERO;
3827 fal->estimated = GNUNET_YES;
3829 if (GNUNET_YES == fal->connected)
3831 delay = GNUNET_TIME_relative_min (delay,
3832 CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3834 /* FIXME: also adjust delay based on how close the last
3835 observed latency is to the latency of the best alternative */
3836 /* bound how fast we can go */
3837 delay = GNUNET_TIME_relative_max (delay,
3838 GNUNET_TIME_UNIT_SECONDS);
3839 /* randomize a bit (to avoid doing all at the same time) */
3840 delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3841 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(delay,
3842 &send_periodic_ping,
3850 * Function that will be called if we receive some payload
3851 * from another peer.
3853 * @param message the payload
3854 * @param n peer who claimed to be the sender
3857 handle_payload_message (const struct GNUNET_MessageHeader *message,
3858 struct NeighbourList *n)
3860 struct InboundMessage *im;
3861 struct TransportClient *cpos;
3864 msize = ntohs (message->size);
3865 if (n->received_pong == GNUNET_NO)
3867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3868 "Received message of type %u and size %u from `%4s', but no pong yet!!\n",
3869 ntohs (message->type),
3870 ntohs (message->size),
3871 GNUNET_i2s (&n->id));
3872 GNUNET_free_non_null (n->pre_connect_message_buffer);
3873 n->pre_connect_message_buffer = GNUNET_malloc (msize);
3874 memcpy (n->pre_connect_message_buffer, message, msize);
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3880 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
3881 ntohs (message->type),
3882 ntohs (message->size),
3883 GNUNET_i2s (&n->id));
3885 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3888 n->quota_violation_count++;
3890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3891 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
3892 n->in_tracker.available_bytes_per_s__,
3893 n->quota_violation_count);
3895 /* Discount 32k per violation */
3896 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3901 if (n->quota_violation_count > 0)
3903 /* try to add 32k back */
3904 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3906 n->quota_violation_count--;
3909 GNUNET_STATISTICS_update (stats,
3910 gettext_noop ("# payload received from other peers"),
3913 /* transmit message to all clients */
3914 uint32_t ats_count = 2;
3915 size_t size = sizeof (struct InboundMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) + msize;
3916 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
3919 im = GNUNET_malloc (size);
3920 im->header.size = htons (size);
3921 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3923 im->ats_count = htonl(ats_count);
3924 /* Setting ATS data */
3925 (&(im->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
3926 (&(im->ats))[0].value = htonl (n->distance);
3927 (&(im->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
3928 (&(im->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
3929 (&(im->ats))[ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
3930 (&(im->ats))[ats_count].value = htonl (0);
3932 memcpy (&((&(im->ats))[ats_count+1]), message, msize);
3934 while (cpos != NULL)
3936 transmit_to_client (cpos, &im->header, GNUNET_YES);
3944 * Iterator over hash map entries. Checks if the given validation
3945 * entry is for the same challenge as what is given in the PONG.
3947 * @param cls the 'struct TransportPongMessage*'
3948 * @param key peer identity
3949 * @param value value in the hash map ('struct ValidationEntry')
3950 * @return GNUNET_YES if we should continue to
3951 * iterate (mismatch), GNUNET_NO if not (entry matched)
3954 check_pending_validation (void *cls,
3955 const GNUNET_HashCode * key,
3958 const struct TransportPongMessage *pong = cls;
3959 struct ValidationEntry *ve = value;
3960 struct AddValidatedAddressContext avac;
3961 unsigned int challenge = ntohl(pong->challenge);
3962 struct GNUNET_HELLO_Message *hello;
3963 struct GNUNET_PeerIdentity target;
3964 struct NeighbourList *n;
3965 struct ForeignAddressList *fal;
3966 struct OwnAddressList *oal;
3967 struct TransportPlugin *tp;
3968 struct GNUNET_MessageHeader *prem;
3974 ps = ntohs (pong->header.size);
3975 if (ps < sizeof (struct TransportPongMessage))
3977 GNUNET_break_op (0);
3980 addr = (const char*) &pong[1];
3981 slen = strlen (ve->transport_name) + 1;
3982 if ( (ps - sizeof (struct TransportPongMessage) < slen) ||
3983 (ve->challenge != challenge) ||
3984 (addr[slen-1] != '\0') ||
3985 (0 != strcmp (addr, ve->transport_name)) ||
3986 (ntohl (pong->purpose.size)
3987 != sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3989 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
3990 sizeof (struct GNUNET_PeerIdentity) + ps - sizeof (struct TransportPongMessage)) )
3995 alen = ps - sizeof (struct TransportPongMessage) - slen;
3996 switch (ntohl (pong->purpose.purpose))
3998 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN:
3999 if ( (ve->addrlen + slen != ntohl (pong->addrlen)) ||
4000 (0 != memcmp (&addr[slen],
4004 return GNUNET_YES; /* different entry, keep trying! */
4006 if (0 != memcmp (&pong->pid,
4008 sizeof (struct GNUNET_PeerIdentity)))
4010 GNUNET_break_op (0);
4014 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
4019 GNUNET_break_op (0);
4024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4025 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
4027 a2s (ve->transport_name,
4028 (const struct sockaddr *) ve->addr,
4030 ve->transport_name);
4033 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING:
4034 if (0 != memcmp (&pong->pid,
4036 sizeof (struct GNUNET_PeerIdentity)))
4038 GNUNET_break_op (0);
4041 if (ve->addrlen != 0)
4043 /* must have been for a different validation entry */
4046 tp = find_transport (ve->transport_name);
4052 oal = tp->addresses;
4055 if ( (oal->addrlen == alen) &&
4056 (0 == memcmp (&oal[1],
4064 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4065 _("Not accepting PONG with address `%s' since I cannot confirm having this address.\n"),
4066 a2s (ve->transport_name,
4072 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING,
4077 GNUNET_break_op (0);
4082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4083 "Confirmed that peer `%4s' is talking to us using address `%s' (%s) for us.\n",
4085 a2s (ve->transport_name,
4088 ve->transport_name);
4092 GNUNET_break_op (0);
4095 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
4097 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4098 _("Received expired signature. Check system time.\n"));
4101 GNUNET_STATISTICS_update (stats,
4102 gettext_noop ("# address validation successes"),
4105 /* create the updated HELLO */
4106 GNUNET_CRYPTO_hash (&ve->publicKey,
4107 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4108 &target.hashPubKey);
4109 if (ve->addr != NULL)
4111 avac.done = GNUNET_NO;
4113 hello = GNUNET_HELLO_create (&ve->publicKey,
4114 &add_validated_address,
4116 GNUNET_PEERINFO_add_peer (peerinfo,
4118 GNUNET_free (hello);
4120 n = find_neighbour (&target);
4123 n->publicKey = ve->publicKey;
4124 n->public_key_valid = GNUNET_YES;
4125 fal = add_peer_address (n,
4130 GNUNET_assert (fal != NULL);
4131 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
4132 fal->validated = GNUNET_YES;
4133 mark_address_connected (fal);
4134 GNUNET_STATISTICS_update (stats,
4135 gettext_noop ("# peer addresses considered valid"),
4138 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
4139 update_addr_value (fal, GNUNET_TIME_absolute_get_duration (ve->send_time).rel_value, GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
4141 schedule_next_ping (fal);
4142 if (n->latency.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
4143 n->latency = fal->latency;
4145 n->latency.rel_value = (fal->latency.rel_value + n->latency.rel_value) / 2;
4147 n->distance = fal->distance;
4148 if (GNUNET_NO == n->received_pong)
4150 n->received_pong = GNUNET_YES;
4152 notify_clients_connect (&target, n->latency, n->distance);
4153 if (NULL != (prem = n->pre_connect_message_buffer))
4155 n->pre_connect_message_buffer = NULL;
4156 handle_payload_message (prem, n);
4160 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4162 GNUNET_SCHEDULER_cancel (n->retry_task);
4163 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4164 try_transmission_to_peer (n);
4168 /* clean up validation entry */
4169 GNUNET_assert (GNUNET_YES ==
4170 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4173 abort_validation (NULL, NULL, ve);
4179 * Function that will be called if we receive a validation
4180 * of an address challenge that we transmitted to another
4181 * peer. Note that the validation should only be considered
4182 * acceptable if the challenge matches AND if the sender
4183 * address is at least a plausible address for this peer
4184 * (otherwise we may be seeing a MiM attack).
4186 * @param cls closure
4187 * @param message the pong message
4188 * @param peer who responded to our challenge
4189 * @param sender_address string describing our sender address (as observed
4190 * by the other peer in binary format)
4191 * @param sender_address_len number of bytes in 'sender_address'
4194 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
4195 const struct GNUNET_PeerIdentity *peer,
4196 const char *sender_address,
4197 size_t sender_address_len)
4199 #if DEBUG_TRANSPORT > 1
4200 /* we get tons of these that just get discarded, only log
4201 if we are quite verbose */
4202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4203 "Receiving `%s' message from `%4s'.\n", "PONG",
4206 GNUNET_STATISTICS_update (stats,
4207 gettext_noop ("# PONG messages received"),
4210 if (GNUNET_SYSERR !=
4211 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
4213 &check_pending_validation,
4216 /* This is *expected* to happen a lot since we send
4217 PONGs to *all* known addresses of the sender of
4218 the PING, so most likely we get multiple PONGs
4219 per PING, and all but the first PONG will end up
4220 here. So really we should not print anything here
4221 unless we want to be very, very verbose... */
4222 #if DEBUG_TRANSPORT > 2
4223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4224 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
4236 * Try to validate a neighbour's address by sending him our HELLO and a PING.
4238 * @param cls the 'struct ValidationEntry*'
4239 * @param neighbour neighbour to validate, NULL if validation failed
4242 transmit_hello_and_ping (void *cls,
4243 struct NeighbourList *neighbour)
4245 struct ValidationEntry *va = cls;
4246 struct ForeignAddressList *peer_address;
4247 struct TransportPingMessage ping;
4248 uint16_t hello_size;
4251 struct GNUNET_PeerIdentity id;
4254 GNUNET_CRYPTO_hash (&va->publicKey,
4255 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4257 if (neighbour == NULL)
4259 /* FIXME: stats... */
4260 GNUNET_break (GNUNET_OK ==
4261 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4264 abort_validation (NULL, NULL, va);
4267 neighbour->publicKey = va->publicKey;
4268 neighbour->public_key_valid = GNUNET_YES;
4269 peer_address = add_peer_address (neighbour,
4270 va->transport_name, NULL,
4271 (const void*) &va[1],
4273 if (peer_address == NULL)
4275 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4276 "Failed to add peer `%4s' for plugin `%s'\n",
4277 GNUNET_i2s (&neighbour->id),
4278 va->transport_name);
4279 GNUNET_break (GNUNET_OK ==
4280 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4283 abort_validation (NULL, NULL, va);
4286 hello_size = GNUNET_HELLO_size(our_hello);
4287 slen = strlen(va->transport_name) + 1;
4288 tsize = sizeof(struct TransportPingMessage) + hello_size + va->addrlen + slen;
4289 message_buf = GNUNET_malloc(tsize);
4290 ping.challenge = htonl(va->challenge);
4291 ping.header.size = htons(sizeof(struct TransportPingMessage) + slen + va->addrlen);
4292 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
4293 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
4294 memcpy(message_buf, our_hello, hello_size);
4295 memcpy(&message_buf[hello_size],
4297 sizeof(struct TransportPingMessage));
4298 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
4301 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
4305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4306 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
4309 : a2s (va->transport_name,
4310 (const void*) &va[1], va->addrlen),
4312 GNUNET_i2s (&neighbour->id),
4313 "HELLO", hello_size,
4314 "PING", sizeof (struct TransportPingMessage) + va->addrlen + slen);
4317 GNUNET_STATISTICS_update (stats,
4318 gettext_noop ("# PING messages sent for initial validation"),
4321 transmit_to_peer (NULL, peer_address,
4322 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
4323 HELLO_VERIFICATION_TIMEOUT,
4325 GNUNET_YES, neighbour);
4326 GNUNET_free(message_buf);
4331 * Check if the given address is already being validated; if not,
4332 * append the given address to the list of entries that are being be
4333 * validated and initiate validation.
4335 * @param cls closure ('struct CheckHelloValidatedContext *')
4336 * @param tname name of the transport
4337 * @param expiration expiration time
4338 * @param addr the address
4339 * @param addrlen length of the address
4340 * @return GNUNET_OK (always)
4343 run_validation (void *cls,
4345 struct GNUNET_TIME_Absolute expiration,
4349 struct CheckHelloValidatedContext *chvc = cls;
4350 struct GNUNET_PeerIdentity id;
4351 struct TransportPlugin *tp;
4352 struct ValidationEntry *va;
4353 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4354 struct CheckAddressExistsClosure caec;
4355 struct OwnAddressList *oal;
4357 GNUNET_assert (addr != NULL);
4359 GNUNET_STATISTICS_update (stats,
4360 gettext_noop ("# peer addresses scheduled for validation"),
4363 tp = find_transport (tname);
4366 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
4367 GNUNET_ERROR_TYPE_BULK,
4369 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
4371 GNUNET_STATISTICS_update (stats,
4372 gettext_noop ("# peer addresses not validated (plugin not available)"),
4377 /* check if this is one of our own addresses */
4378 oal = tp->addresses;
4381 if ( (oal->addrlen == addrlen) &&
4382 (0 == memcmp (&oal[1],
4386 /* not plausible, this address is equivalent to our own address! */
4387 GNUNET_STATISTICS_update (stats,
4388 gettext_noop ("# peer addresses not validated (loopback)"),
4395 GNUNET_HELLO_get_key (chvc->hello, &pk);
4396 GNUNET_CRYPTO_hash (&pk,
4398 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4401 if (is_blacklisted(&id, tp))
4404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4405 "Attempted to validate blacklisted peer `%s' using `%s'!\n",
4413 caec.addrlen = addrlen;
4414 caec.session = NULL;
4416 caec.exists = GNUNET_NO;
4417 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
4418 &check_address_exists,
4420 if (caec.exists == GNUNET_YES)
4422 /* During validation attempts we will likely trigger the other
4423 peer trying to validate our address which in turn will cause
4424 it to send us its HELLO, so we expect to hit this case rather
4425 frequently. Only print something if we are very verbose. */
4426 #if DEBUG_TRANSPORT > 1
4427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4428 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
4429 a2s (tname, addr, addrlen),
4433 GNUNET_STATISTICS_update (stats,
4434 gettext_noop ("# peer addresses not validated (in progress)"),
4439 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
4442 va->transport_name = GNUNET_strdup (tname);
4443 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
4445 va->send_time = GNUNET_TIME_absolute_get();
4446 va->addr = (const void*) &va[1];
4447 memcpy (&va[1], addr, addrlen);
4448 va->addrlen = addrlen;
4449 GNUNET_HELLO_get_key (chvc->hello,
4451 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
4452 &timeout_hello_validation,
4454 GNUNET_CONTAINER_multihashmap_put (validation_map,
4457 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4458 setup_peer_check_blacklist (&id, GNUNET_NO,
4459 &transmit_hello_and_ping,
4466 * Check if addresses in validated hello "h" overlap with
4467 * those in "chvc->hello" and validate the rest.
4469 * @param cls closure
4470 * @param peer id of the peer, NULL for last call
4471 * @param h hello message for the peer (can be NULL)
4472 * @param err_msg NULL if successful, otherwise contains error message
4475 check_hello_validated (void *cls,
4476 const struct GNUNET_PeerIdentity *peer,
4477 const struct GNUNET_HELLO_Message *h,
4478 const char *err_msg)
4480 struct CheckHelloValidatedContext *chvc = cls;
4481 struct GNUNET_HELLO_Message *plain_hello;
4482 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4483 struct GNUNET_PeerIdentity target;
4484 struct NeighbourList *n;
4486 if (err_msg != NULL)
4488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4489 _("Error in communication with PEERINFO service\n"));
4495 GNUNET_STATISTICS_update (stats,
4496 gettext_noop ("# outstanding peerinfo iterate requests"),
4500 if (GNUNET_NO == chvc->hello_known)
4502 /* notify PEERINFO about the peer now, so that we at least
4503 have the public key if some other component needs it */
4504 GNUNET_HELLO_get_key (chvc->hello, &pk);
4505 GNUNET_CRYPTO_hash (&pk,
4506 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4507 &target.hashPubKey);
4508 plain_hello = GNUNET_HELLO_create (&pk,
4511 GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
4512 GNUNET_free (plain_hello);
4513 #if DEBUG_TRANSPORT_HELLO
4514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4515 "PEERINFO had no `%s' message for peer `%4s', full validation needed.\n",
4517 GNUNET_i2s (&target));
4519 GNUNET_STATISTICS_update (stats,
4520 gettext_noop ("# new HELLOs requiring full validation"),
4523 GNUNET_HELLO_iterate_addresses (chvc->hello,
4530 GNUNET_STATISTICS_update (stats,
4531 gettext_noop ("# duplicate HELLO (peer known)"),
4536 if (chvc->ve_count == 0)
4538 GNUNET_CONTAINER_DLL_remove (chvc_head,
4547 #if DEBUG_TRANSPORT_HELLO
4548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4549 "PEERINFO had `%s' message for peer `%4s', validating only new addresses.\n",
4553 chvc->hello_known = GNUNET_YES;
4554 n = find_neighbour (peer);
4557 #if DEBUG_TRANSPORT_HELLO
4558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4559 "Calling hello_iterate_addresses for %s!\n",
4562 GNUNET_HELLO_iterate_addresses (h,
4564 &add_to_foreign_address_list,
4566 try_transmission_to_peer (n);
4570 #if DEBUG_TRANSPORT_HELLO
4571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4572 "No existing neighbor record for %s!\n",
4575 GNUNET_STATISTICS_update (stats,
4576 gettext_noop ("# no existing neighbour record (validating HELLO)"),
4580 GNUNET_STATISTICS_update (stats,
4581 gettext_noop ("# HELLO validations (update case)"),
4584 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
4586 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
4593 * Process HELLO-message.
4595 * @param plugin transport involved, may be NULL
4596 * @param message the actual message
4597 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
4600 process_hello (struct TransportPlugin *plugin,
4601 const struct GNUNET_MessageHeader *message)
4604 struct GNUNET_PeerIdentity target;
4605 const struct GNUNET_HELLO_Message *hello;
4606 struct CheckHelloValidatedContext *chvc;
4607 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
4608 #if DEBUG_TRANSPORT_HELLO > 2
4611 hsize = ntohs (message->size);
4612 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
4613 (hsize < sizeof (struct GNUNET_MessageHeader)))
4616 return GNUNET_SYSERR;
4618 GNUNET_STATISTICS_update (stats,
4619 gettext_noop ("# HELLOs received for validation"),
4623 /* first, check if load is too high */
4624 if (GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
4626 GNUNET_STATISTICS_update (stats,
4627 gettext_noop ("# HELLOs ignored due to high load"),
4630 #if DEBUG_TRANSPORT_HELLO
4631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4632 "Ignoring `%s' for `%4s', load too high.\n",
4634 GNUNET_i2s (&target));
4638 hello = (const struct GNUNET_HELLO_Message *) message;
4639 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
4641 #if DEBUG_TRANSPORT_HELLO
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4643 "Unable to get public key from `%s' for `%4s'!\n",
4645 GNUNET_i2s (&target));
4647 GNUNET_break_op (0);
4648 return GNUNET_SYSERR;
4651 GNUNET_CRYPTO_hash (&publicKey,
4652 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4653 &target.hashPubKey);
4655 #if DEBUG_TRANSPORT_HELLO
4656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4657 "Received `%s' message for `%4s'\n",
4659 GNUNET_i2s (&target));
4662 if (0 == memcmp (&my_identity,
4664 sizeof (struct GNUNET_PeerIdentity)))
4666 GNUNET_STATISTICS_update (stats,
4667 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
4673 while (NULL != chvc)
4675 if (GNUNET_HELLO_equals (hello,
4677 GNUNET_TIME_absolute_get ()).abs_value > 0)
4679 #if DEBUG_TRANSPORT_HELLO > 2
4680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4681 "Received duplicate `%s' message for `%4s'; ignored\n",
4683 GNUNET_i2s (&target));
4685 return GNUNET_OK; /* validation already pending */
4687 if (GNUNET_HELLO_size(hello) == GNUNET_HELLO_size (chvc->hello))
4688 GNUNET_break (0 != memcmp (hello, chvc->hello,
4689 GNUNET_HELLO_size(hello)));
4694 struct NeighbourList *temp_neighbor = find_neighbour(&target);
4695 if ((NULL != temp_neighbor))
4697 fprintf(stderr, "Already know peer, ignoring hello\n");
4702 #if DEBUG_TRANSPORT_HELLO > 2
4705 my_id = GNUNET_strdup(GNUNET_i2s(plugin->env.my_identity));
4706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4707 "%s: Starting validation of `%s' message for `%4s' via '%s' of size %u\n",
4710 GNUNET_i2s (&target),
4712 GNUNET_HELLO_size(hello));
4716 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
4718 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
4719 memcpy (&chvc[1], hello, hsize);
4720 GNUNET_CONTAINER_DLL_insert (chvc_head,
4723 /* finally, check if HELLO was previously validated
4724 (continuation will then schedule actual validation) */
4725 GNUNET_STATISTICS_update (stats,
4726 gettext_noop ("# peerinfo process hello iterate requests"),
4729 GNUNET_STATISTICS_update (stats,
4730 gettext_noop ("# outstanding peerinfo iterate requests"),
4733 chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
4735 HELLO_VERIFICATION_TIMEOUT,
4736 &check_hello_validated, chvc);
4742 * The peer specified by the given neighbour has timed-out or a plugin
4743 * has disconnected. We may either need to do nothing (other plugins
4744 * still up), or trigger a full disconnect and clean up. This
4745 * function updates our state and does the necessary notifications.
4746 * Also notifies our clients that the neighbour is now officially
4749 * @param n the neighbour list entry for the peer
4750 * @param check GNUNET_YES to check if ALL addresses for this peer
4751 * are gone, GNUNET_NO to force a disconnect of the peer
4752 * regardless of whether other addresses exist.
4755 disconnect_neighbour (struct NeighbourList *n, int check)
4757 struct ReadyList *rpos;
4758 struct NeighbourList *npos;
4759 struct NeighbourList *nprev;
4760 struct MessageQueue *mq;
4761 struct ForeignAddressList *peer_addresses;
4762 struct ForeignAddressList *peer_pos;
4764 if (GNUNET_YES == check)
4767 while (NULL != rpos)
4769 peer_addresses = rpos->addresses;
4770 while (peer_addresses != NULL)
4772 if (GNUNET_YES == peer_addresses->connected)
4774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4775 "NOT Disconnecting from `%4s', still have live addresses!\n",
4776 GNUNET_i2s (&n->id));
4777 return; /* still connected */
4779 peer_addresses = peer_addresses->next;
4785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4786 "Disconnecting from `%4s'\n",
4787 GNUNET_i2s (&n->id));
4789 /* remove n from neighbours list */
4792 while ((npos != NULL) && (npos != n))
4797 GNUNET_assert (npos != NULL);
4799 neighbours = n->next;
4801 nprev->next = n->next;
4803 /* notify all clients about disconnect */
4804 if (GNUNET_YES == n->received_pong)
4805 notify_clients_disconnect (&n->id);
4807 /* clean up all plugins, cancel connections and pending transmissions */
4808 while (NULL != (rpos = n->plugins))
4810 n->plugins = rpos->next;
4811 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
4812 while (rpos->addresses != NULL)
4814 peer_pos = rpos->addresses;
4815 rpos->addresses = peer_pos->next;
4816 if (peer_pos->connected == GNUNET_YES)
4817 GNUNET_STATISTICS_update (stats,
4818 gettext_noop ("# connected addresses"),
4821 if (GNUNET_YES == peer_pos->validated)
4822 GNUNET_STATISTICS_update (stats,
4823 gettext_noop ("# peer addresses considered valid"),
4826 if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
4828 GNUNET_SCHEDULER_cancel (peer_pos->revalidate_task);
4829 peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
4831 GNUNET_free(peer_pos->ressources);
4832 peer_pos->ressources = NULL;
4833 GNUNET_free(peer_pos->quality);
4834 peer_pos->ressources = NULL;
4835 GNUNET_free(peer_pos);
4840 /* free all messages on the queue */
4841 while (NULL != (mq = n->messages_head))
4843 GNUNET_STATISTICS_update (stats,
4844 gettext_noop ("# bytes in message queue for other peers"),
4845 - (int64_t) mq->message_buf_size,
4847 GNUNET_STATISTICS_update (stats,
4848 gettext_noop ("# bytes discarded due to disconnect"),
4849 mq->message_buf_size,
4851 GNUNET_CONTAINER_DLL_remove (n->messages_head,
4854 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4856 sizeof(struct GNUNET_PeerIdentity)));
4859 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
4861 GNUNET_SCHEDULER_cancel (n->timeout_task);
4862 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4864 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4866 GNUNET_SCHEDULER_cancel (n->retry_task);
4867 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4869 if (n->piter != NULL)
4871 GNUNET_PEERINFO_iterate_cancel (n->piter);
4872 GNUNET_STATISTICS_update (stats,
4873 gettext_noop ("# outstanding peerinfo iterate requests"),
4878 /* finally, free n itself */
4879 GNUNET_STATISTICS_update (stats,
4880 gettext_noop ("# active neighbours"),
4883 GNUNET_free_non_null (n->pre_connect_message_buffer);
4889 * We have received a PING message from someone. Need to send a PONG message
4890 * in response to the peer by any means necessary.
4893 handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
4894 const struct GNUNET_PeerIdentity *peer,
4895 struct Session *session,
4896 const char *sender_address,
4897 uint16_t sender_address_len)
4899 struct TransportPlugin *plugin = cls;
4900 struct SessionHeader *session_header = (struct SessionHeader*) session;
4901 struct TransportPingMessage *ping;
4902 struct TransportPongMessage *pong;
4903 struct NeighbourList *n;
4904 struct ReadyList *rl;
4905 struct ForeignAddressList *fal;
4906 struct OwnAddressList *oal;
4911 if (ntohs (message->size) < sizeof (struct TransportPingMessage))
4913 GNUNET_break_op (0);
4914 return GNUNET_SYSERR;
4917 ping = (struct TransportPingMessage *) message;
4918 if (0 != memcmp (&ping->target,
4919 plugin->env.my_identity,
4920 sizeof (struct GNUNET_PeerIdentity)))
4922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4923 _("Received `%s' message from `%s' destined for `%s' which is not me!\n"),
4925 (sender_address != NULL)
4926 ? a2s (plugin->short_name,
4927 (const struct sockaddr *)sender_address,
4930 GNUNET_i2s (&ping->target));
4931 return GNUNET_SYSERR;
4934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4935 "Processing `%s' from `%s'\n",
4937 (sender_address != NULL)
4938 ? a2s (plugin->short_name,
4939 (const struct sockaddr *)sender_address,
4943 GNUNET_STATISTICS_update (stats,
4944 gettext_noop ("# PING messages received"),
4947 addr = (const char*) &ping[1];
4948 alen = ntohs (message->size) - sizeof (struct TransportPingMessage);
4949 slen = strlen (plugin->short_name) + 1;
4952 /* peer wants to confirm that we have an outbound connection to him */
4953 if (session == NULL)
4955 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4956 _("Refusing to create PONG since I do not have a session with `%s'.\n"),
4958 return GNUNET_SYSERR;
4960 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len + slen);
4961 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len + slen);
4962 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
4963 pong->purpose.size =
4964 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4966 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
4967 sizeof (struct GNUNET_PeerIdentity) + sender_address_len + slen);
4968 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING);
4969 pong->challenge = ping->challenge;
4970 pong->addrlen = htonl(sender_address_len + slen);
4973 sizeof(struct GNUNET_PeerIdentity));
4977 if ((sender_address!=NULL) && (sender_address_len > 0))
4978 memcpy (&((char*)&pong[1])[slen],
4980 sender_address_len);
4981 if (GNUNET_TIME_absolute_get_remaining (session_header->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
4983 /* create / update cached sig */
4985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4986 "Creating PONG signature to indicate active connection.\n");
4988 session_header->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
4989 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
4990 GNUNET_assert (GNUNET_OK ==
4991 GNUNET_CRYPTO_rsa_sign (my_private_key,
4993 &session_header->pong_signature));
4997 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
4999 memcpy (&pong->signature,
5000 &session_header->pong_signature,
5001 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5007 /* peer wants to confirm that this is one of our addresses */
5011 plugin->api->check_address (plugin->api->cls,
5015 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5016 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
5017 a2s (plugin->short_name,
5022 oal = plugin->addresses;
5025 if ( (oal->addrlen == alen) &&
5032 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
5033 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
5034 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
5035 pong->purpose.size =
5036 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
5038 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
5039 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
5040 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
5041 pong->challenge = ping->challenge;
5042 pong->addrlen = htonl(alen + slen);
5045 sizeof(struct GNUNET_PeerIdentity));
5046 memcpy (&pong[1], plugin->short_name, slen);
5047 memcpy (&((char*)&pong[1])[slen], addr, alen);
5048 if ( (oal != NULL) &&
5049 (GNUNET_TIME_absolute_get_remaining (oal->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4) )
5051 /* create / update cached sig */
5053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5054 "Creating PONG signature to indicate ownership.\n");
5056 oal->pong_sig_expires = GNUNET_TIME_absolute_min (oal->expires,
5057 GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
5058 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5059 GNUNET_assert (GNUNET_OK ==
5060 GNUNET_CRYPTO_rsa_sign (my_private_key,
5062 &oal->pong_signature));
5063 memcpy (&pong->signature,
5064 &oal->pong_signature,
5065 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5067 else if (oal == NULL)
5069 /* not using cache (typically DV-only) */
5070 pong->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
5071 GNUNET_assert (GNUNET_OK ==
5072 GNUNET_CRYPTO_rsa_sign (my_private_key,
5078 /* can used cached version */
5079 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5080 memcpy (&pong->signature,
5081 &oal->pong_signature,
5082 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5085 n = find_neighbour(peer);
5086 GNUNET_assert (n != NULL);
5087 /* first try reliable response transmission */
5091 fal = rl->addresses;
5094 if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
5097 ntohs (pong->header.size),
5098 TRANSPORT_PONG_PRIORITY,
5099 HELLO_VERIFICATION_TIMEOUT,
5107 GNUNET_STATISTICS_update (stats,
5108 gettext_noop ("# PONGs unicast via reliable transport"),
5118 /* no reliable method found, do multicast */
5119 GNUNET_STATISTICS_update (stats,
5120 gettext_noop ("# PONGs multicast to all available addresses"),
5126 fal = rl->addresses;
5129 transmit_to_peer(NULL, fal,
5130 TRANSPORT_PONG_PRIORITY,
5131 HELLO_VERIFICATION_TIMEOUT,
5133 ntohs(pong->header.size),
5149 * Function called by the plugin for each received message.
5150 * Update data volumes, possibly notify plugins about
5151 * reducing the rate at which they read from the socket
5152 * and generally forward to our receive callback.
5154 * @param cls the "struct TransportPlugin *" we gave to the plugin
5155 * @param peer (claimed) identity of the other peer
5156 * @param message the message, NULL if we only care about
5157 * learning about the delay until we should receive again
5158 * @param ats_data information for automatic transport selection
5159 * @param ats_count number of elements in ats not including 0-terminator
5160 * @param session identifier used for this session (can be NULL)
5161 * @param sender_address binary address of the sender (if observed)
5162 * @param sender_address_len number of bytes in sender_address
5163 * @return how long in ms the plugin should wait until receiving more data
5164 * (plugins that do not support this, can ignore the return value)
5166 static struct GNUNET_TIME_Relative
5167 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
5168 const struct GNUNET_MessageHeader *message,
5169 const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
5171 struct Session *session,
5172 const char *sender_address,
5173 uint16_t sender_address_len)
5175 struct TransportPlugin *plugin = cls;
5176 struct ReadyList *service_context;
5177 struct ForeignAddressList *peer_address;
5179 struct NeighbourList *n;
5180 struct GNUNET_TIME_Relative ret;
5181 if (is_blacklisted (peer, plugin))
5182 return GNUNET_TIME_UNIT_FOREVER_REL;
5186 n = find_neighbour (peer);
5188 n = setup_new_neighbour (peer, GNUNET_YES);
5189 service_context = n->plugins;
5190 while ((service_context != NULL) && (plugin != service_context->plugin))
5191 service_context = service_context->next;
5192 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
5193 peer_address = NULL;
5196 for (c=0; c<ats_count; c++)
5198 if (ntohl(ats_data[c].type) == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
5200 distance = ntohl(ats_data[c].value);
5204 /* notify ATS about incoming data */
5205 //ats_notify_ats_data(peer, ats_data);
5208 if (message != NULL)
5210 if ( (session != NULL) ||
5211 (sender_address != NULL) )
5212 peer_address = add_peer_address (n,
5216 sender_address_len);
5217 if (peer_address != NULL)
5220 update_addr_ats(peer_address, ats_data, ats_count);
5221 update_addr_value(peer_address, distance, GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5223 peer_address->distance = distance;
5224 if (GNUNET_YES == peer_address->validated)
5225 mark_address_connected (peer_address);
5226 peer_address->timeout
5228 GNUNET_TIME_relative_to_absolute
5229 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5230 schedule_next_ping (peer_address);
5232 /* update traffic received amount ... */
5233 msize = ntohs (message->size);
5235 GNUNET_STATISTICS_update (stats,
5236 gettext_noop ("# bytes received from other peers"),
5239 n->distance = distance;
5241 GNUNET_TIME_relative_to_absolute
5242 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5243 GNUNET_SCHEDULER_cancel (n->timeout_task);
5245 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
5246 &neighbour_timeout_task, n);
5247 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
5249 /* dropping message due to frequent inbound volume violations! */
5250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
5251 GNUNET_ERROR_TYPE_BULK,
5253 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
5254 n->in_tracker.available_bytes_per_s__,
5255 n->quota_violation_count);
5256 GNUNET_STATISTICS_update (stats,
5257 gettext_noop ("# bandwidth quota violations by other peers"),
5260 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
5262 if ((ntohs(message->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ATS) &&
5263 (ntohs(message->size) == (sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t))))
5265 uint32_t value = ntohl(*((uint32_t *) &message[1]));
5266 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GNUNET_MESSAGE_TYPE_TRANSPORT_ATS: %i \n", value);
5267 /* Force ressource and quality update */
5270 ats->modified_resources = GNUNET_YES;
5271 ats->modified_quality = GNUNET_YES;
5273 /* Force cost update */
5275 ats->modified_resources = GNUNET_YES;
5276 /* Force quality update */
5278 ats->modified_quality = GNUNET_YES;
5279 /* Force full rebuild */
5281 ats->modified_addr = GNUNET_YES;
5285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5286 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
5287 ntohs (message->type),
5288 ntohs (message->size),
5291 switch (ntohs (message->type))
5293 case GNUNET_MESSAGE_TYPE_HELLO:
5294 GNUNET_STATISTICS_update (stats,
5295 gettext_noop ("# HELLO messages received from other peers"),
5298 process_hello (plugin, message);
5300 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
5301 handle_ping (plugin, message, peer, session, sender_address, sender_address_len);
5303 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
5304 handle_pong (plugin, message, peer, sender_address, sender_address_len);
5306 case GNUNET_MESSAGE_TYPE_TRANSPORT_ATS:
5309 handle_payload_message (message, n);
5313 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
5314 if (ret.rel_value > 0)
5316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5317 "Throttling read (%llu bytes excess at %u b/s), waiting %llums before reading more.\n",
5318 (unsigned long long) n->in_tracker.consumption_since_last_update__,
5319 (unsigned int) n->in_tracker.available_bytes_per_s__,
5320 (unsigned long long) ret.rel_value);
5321 GNUNET_STATISTICS_update (stats,
5322 gettext_noop ("# ms throttling suggested"),
5323 (int64_t) ret.rel_value,
5330 * Handle START-message. This is the first message sent to us
5331 * by any client which causes us to add it to our list.
5333 * @param cls closure (always NULL)
5334 * @param client identification of the client
5335 * @param message the actual message
5338 handle_start (void *cls,
5339 struct GNUNET_SERVER_Client *client,
5340 const struct GNUNET_MessageHeader *message)
5342 const struct StartMessage *start;
5343 struct TransportClient *c;
5344 struct ConnectInfoMessage * cim;
5345 struct NeighbourList *n;
5349 start = (const struct StartMessage*) message;
5351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5352 "Received `%s' request from client\n", "START");
5357 if (c->client == client)
5359 /* client already on our list! */
5361 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5366 if ( (GNUNET_NO != ntohl (start->do_check)) &&
5367 (0 != memcmp (&start->self,
5369 sizeof (struct GNUNET_PeerIdentity))) )
5371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5372 _("Rejecting control connection from peer `%s', which is not me!\n"),
5373 GNUNET_i2s (&start->self));
5374 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5377 c = GNUNET_malloc (sizeof (struct TransportClient));
5381 if (our_hello != NULL)
5384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5385 "Sending our own `%s' to new client\n", "HELLO");
5387 transmit_to_client (c,
5388 (const struct GNUNET_MessageHeader *) our_hello,
5390 /* tell new client about all existing connections */
5392 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
5393 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
5397 cim = GNUNET_malloc (size);
5398 cim->header.size = htons (size);
5399 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
5400 cim->ats_count = htonl(ats_count);
5401 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
5402 (&(cim->ats))[2].value = htonl (0);
5406 if (GNUNET_YES == n->received_pong)
5408 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5409 (&(cim->ats))[0].value = htonl (n->distance);
5410 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
5411 (&(cim->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
5413 transmit_to_client (c, &cim->header, GNUNET_NO);
5419 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5424 * Handle HELLO-message.
5426 * @param cls closure (always NULL)
5427 * @param client identification of the client
5428 * @param message the actual message
5431 handle_hello (void *cls,
5432 struct GNUNET_SERVER_Client *client,
5433 const struct GNUNET_MessageHeader *message)
5437 GNUNET_STATISTICS_update (stats,
5438 gettext_noop ("# HELLOs received from clients"),
5441 ret = process_hello (NULL, message);
5442 GNUNET_SERVER_receive_done (client, ret);
5447 * Closure for 'transmit_client_message'; followed by
5448 * 'msize' bytes of the actual message.
5450 struct TransmitClientMessageContext
5453 * Client on whom's behalf we are sending.
5455 struct GNUNET_SERVER_Client *client;
5458 * Timeout for the transmission.
5460 struct GNUNET_TIME_Absolute timeout;
5468 * Size of the message in bytes.
5475 * Schedule transmission of a message we got from a client to a peer.
5477 * @param cls the 'struct TransmitClientMessageContext*'
5478 * @param n destination, or NULL on error (in that case, drop the message)
5481 transmit_client_message (void *cls,
5482 struct NeighbourList *n)
5484 struct TransmitClientMessageContext *tcmc = cls;
5485 struct TransportClient *tc;
5488 while ((tc != NULL) && (tc->client != tcmc->client))
5493 transmit_to_peer (tc, NULL, tcmc->priority,
5494 GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
5496 tcmc->msize, GNUNET_NO, n);
5498 GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
5499 GNUNET_SERVER_client_drop (tcmc->client);
5505 * Handle SEND-message.
5507 * @param cls closure (always NULL)
5508 * @param client identification of the client
5509 * @param message the actual message
5512 handle_send (void *cls,
5513 struct GNUNET_SERVER_Client *client,
5514 const struct GNUNET_MessageHeader *message)
5516 const struct OutboundMessage *obm;
5517 const struct GNUNET_MessageHeader *obmm;
5518 struct TransmitClientMessageContext *tcmc;
5522 size = ntohs (message->size);
5524 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
5527 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5530 GNUNET_STATISTICS_update (stats,
5531 gettext_noop ("# payload received for other peers"),
5534 obm = (const struct OutboundMessage *) message;
5535 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
5536 msize = size - sizeof (struct OutboundMessage);
5538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5539 "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
5540 "SEND", GNUNET_i2s (&obm->peer),
5544 tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
5545 tcmc->client = client;
5546 tcmc->priority = ntohl (obm->priority);
5547 tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
5548 tcmc->msize = msize;
5549 /* FIXME: this memcpy can be up to 7% of our total runtime */
5550 memcpy (&tcmc[1], obmm, msize);
5551 GNUNET_SERVER_client_keep (client);
5552 setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
5553 &transmit_client_message,
5559 * Handle request connect message
5561 * @param cls closure (always NULL)
5562 * @param client identification of the client
5563 * @param message the actual message
5566 handle_request_connect (void *cls,
5567 struct GNUNET_SERVER_Client *client,
5568 const struct GNUNET_MessageHeader *message)
5570 const struct TransportRequestConnectMessage *trcm =
5571 (const struct TransportRequestConnectMessage *) message;
5573 GNUNET_STATISTICS_update (stats,
5574 gettext_noop ("# REQUEST CONNECT messages received"),
5577 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a request connect message for peer %s\n", GNUNET_i2s(&trcm->peer));
5578 setup_peer_check_blacklist (&trcm->peer, GNUNET_YES,
5580 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5584 * Handle SET_QUOTA-message.
5586 * @param cls closure (always NULL)
5587 * @param client identification of the client
5588 * @param message the actual message
5591 handle_set_quota (void *cls,
5592 struct GNUNET_SERVER_Client *client,
5593 const struct GNUNET_MessageHeader *message)
5595 const struct QuotaSetMessage *qsm =
5596 (const struct QuotaSetMessage *) message;
5597 struct NeighbourList *n;
5599 GNUNET_STATISTICS_update (stats,
5600 gettext_noop ("# SET QUOTA messages received"),
5603 n = find_neighbour (&qsm->peer);
5606 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5607 GNUNET_STATISTICS_update (stats,
5608 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
5614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5615 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
5617 (unsigned int) ntohl (qsm->quota.value__),
5618 (unsigned int) n->in_tracker.available_bytes_per_s__,
5619 GNUNET_i2s (&qsm->peer));
5621 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
5623 if (0 == ntohl (qsm->quota.value__))
5625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5626 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&n->id),
5628 disconnect_neighbour (n, GNUNET_NO);
5630 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5635 * Take the given address and append it to the set of results sent back to
5638 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
5639 * @param address the resolved name, NULL to indicate the last response
5642 transmit_address_to_client (void *cls, const char *address)
5644 struct GNUNET_SERVER_TransmitContext *tc = cls;
5647 if (NULL == address)
5650 slen = strlen (address) + 1;
5652 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
5653 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5654 if (NULL == address)
5655 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
5660 * Handle AddressLookup-message.
5662 * @param cls closure (always NULL)
5663 * @param client identification of the client
5664 * @param message the actual message
5667 handle_address_lookup (void *cls,
5668 struct GNUNET_SERVER_Client *client,
5669 const struct GNUNET_MessageHeader *message)
5671 const struct AddressLookupMessage *alum;
5672 struct TransportPlugin *lsPlugin;
5673 const char *nameTransport;
5674 const char *address;
5676 struct GNUNET_SERVER_TransmitContext *tc;
5677 struct GNUNET_TIME_Absolute timeout;
5678 struct GNUNET_TIME_Relative rtimeout;
5681 size = ntohs (message->size);
5682 if (size < sizeof (struct AddressLookupMessage))
5684 GNUNET_break_op (0);
5685 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5688 alum = (const struct AddressLookupMessage *) message;
5689 uint32_t addressLen = ntohl (alum->addrlen);
5690 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
5692 GNUNET_break_op (0);
5693 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5696 address = (const char *) &alum[1];
5697 nameTransport = (const char *) &address[addressLen];
5699 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
5701 GNUNET_break_op (0);
5702 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5705 timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
5706 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5707 numeric = ntohl (alum->numeric_only);
5708 lsPlugin = find_transport (nameTransport);
5709 if (NULL == lsPlugin)
5711 tc = GNUNET_SERVER_transmit_context_create (client);
5712 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5713 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5714 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
5717 tc = GNUNET_SERVER_transmit_context_create (client);
5718 lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
5720 address, addressLen,
5723 &transmit_address_to_client, tc);
5728 * Setup the environment for this plugin.
5731 create_environment (struct TransportPlugin *plug)
5733 plug->env.cfg = cfg;
5734 plug->env.my_identity = &my_identity;
5735 plug->env.our_hello = &our_hello;
5736 plug->env.cls = plug;
5737 plug->env.receive = &plugin_env_receive;
5738 plug->env.notify_address = &plugin_env_notify_address;
5739 plug->env.session_end = &plugin_env_session_end;
5740 plug->env.max_connections = max_connect_per_transport;
5741 plug->env.stats = stats;
5746 * Start the specified transport (load the plugin).
5749 start_transport (struct GNUNET_SERVER_Handle *server,
5752 struct TransportPlugin *plug;
5755 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5756 _("Loading `%s' transport plugin\n"), name);
5757 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
5758 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
5759 create_environment (plug);
5760 plug->short_name = GNUNET_strdup (name);
5761 plug->lib_name = libname;
5762 plug->next = plugins;
5764 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
5765 if (plug->api == NULL)
5767 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5768 _("Failed to load transport plugin for `%s'\n"), name);
5769 GNUNET_free (plug->short_name);
5770 plugins = plug->next;
5771 GNUNET_free (libname);
5778 * Called whenever a client is disconnected. Frees our
5779 * resources associated with that client.
5781 * @param cls closure
5782 * @param client identification of the client
5785 client_disconnect_notification (void *cls,
5786 struct GNUNET_SERVER_Client *client)
5788 struct TransportClient *pos;
5789 struct TransportClient *prev;
5790 struct ClientMessageQueueEntry *mqe;
5791 struct Blacklisters *bl;
5792 struct BlacklistCheck *bc;
5797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
5798 "Client disconnected, cleaning up.\n");
5800 /* clean up blacklister */
5804 if (bl->client == client)
5809 if (bc->bl_pos == bl)
5811 bc->bl_pos = bl->next;
5814 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
5817 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
5818 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
5824 GNUNET_CONTAINER_DLL_remove (bl_head,
5827 GNUNET_SERVER_client_drop (bl->client);
5833 /* clean up 'normal' clients */
5836 while ((pos != NULL) && (pos->client != client))
5843 while (NULL != (mqe = pos->message_queue_head))
5845 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
5846 pos->message_queue_tail,
5848 pos->message_count--;
5852 clients = pos->next;
5854 prev->next = pos->next;
5855 if (GNUNET_YES == pos->tcs_pending)
5860 if (pos->th != NULL)
5862 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
5865 GNUNET_break (0 == pos->message_count);
5871 * Function called when the service shuts down. Unloads our plugins
5872 * and cancels pending validations.
5874 * @param cls closure, unused
5875 * @param tc task context (unused)
5878 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5880 struct TransportPlugin *plug;
5881 struct OwnAddressList *al;
5882 struct CheckHelloValidatedContext *chvc;
5884 while (neighbours != NULL)
5886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5887 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&neighbours->id),
5889 disconnect_neighbour (neighbours, GNUNET_NO);
5892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5893 "Transport service is unloading plugins...\n");
5895 while (NULL != (plug = plugins))
5897 plugins = plug->next;
5898 if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
5900 GNUNET_SCHEDULER_cancel (plug->address_update_task);
5901 plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
5903 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
5904 GNUNET_free (plug->lib_name);
5905 GNUNET_free (plug->short_name);
5906 while (NULL != (al = plug->addresses))
5908 plug->addresses = al->next;
5913 if (my_private_key != NULL)
5914 GNUNET_CRYPTO_rsa_key_free (my_private_key);
5915 GNUNET_free_non_null (our_hello);
5917 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
5920 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
5921 validation_map = NULL;
5925 /* free 'chvc' data structure */
5926 while (NULL != (chvc = chvc_head))
5928 chvc_head = chvc->next;
5929 if (chvc->piter != NULL)
5931 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
5932 GNUNET_STATISTICS_update (stats,
5933 gettext_noop ("# outstanding peerinfo iterate requests"),
5939 GNUNET_assert (chvc->ve_count == 0);
5946 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
5949 if (peerinfo != NULL)
5951 GNUNET_PEERINFO_disconnect (peerinfo);
5954 /* Can we assume those are gone by now, or do we need to clean up
5956 GNUNET_break (bl_head == NULL);
5957 GNUNET_break (bc_head == NULL);
5961 static int ats_evaluate_results (int result, int solution, char * problem)
5963 int cont = GNUNET_NO;
5964 int error_kind = GNUNET_ERROR_TYPE_DEBUG;
5966 error_kind = GNUNET_ERROR_TYPE_ERROR;
5970 case GLP_ESTOP : /* search terminated by application */
5971 GNUNET_log (error_kind, "%s , Search terminated by application ", problem);
5973 case GLP_EITLIM : /* iteration limit exceeded */
5974 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s Iteration limit exceeded ", problem);
5977 case GLP_ETMLIM : /* time limit exceeded */
5978 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s Time limit exceeded ", problem);
5980 case GLP_ENOPFS : /* no primal feasible solution */
5981 case GLP_ENODFS : /* no dual feasible solution */
5982 GNUNET_log (error_kind, "%s No feasible solution", problem);
5985 case GLP_EBADB : /* invalid basis */
5986 case GLP_ESING : /* singular matrix */
5987 case GLP_ECOND : /* ill-conditioned matrix */
5988 case GLP_EBOUND : /* invalid bounds */
5989 case GLP_EFAIL : /* solver failed */
5990 case GLP_EOBJLL : /* objective lower limit reached */
5991 case GLP_EOBJUL : /* objective upper limit reached */
5992 case GLP_EROOT : /* root LP optimum not provided */
5993 GNUNET_log (error_kind, "%s Invalid Input data: %i\n", problem, result);
5997 GNUNET_log (error_kind, "%s Problem has been solved\n", problem);
6003 GNUNET_log (error_kind, "%s solution is undefined\n", problem);
6006 GNUNET_log (error_kind, "%s solution is optimal\n", problem);
6010 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"));
6014 GNUNET_log (error_kind, "%s problem has no %sfeasible solution\n", problem, (0==strcmp(problem,"LP")?"":"integer "));
6017 GNUNET_log (error_kind, "%s problem is infeasible \n", problem);
6020 GNUNET_log (error_kind, "%s problem is unbounded \n", problem);
6027 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)
6036 glp_init_smcp(&opt_lp);
6038 opt_lp.msg_lev = GLP_MSG_ALL;
6040 opt_lp.msg_lev = GLP_MSG_OFF;
6043 // setting iteration limit
6044 opt_lp.it_lim = max_it;
6046 opt_lp.tm_lim = max_dur;
6048 if (ats->modified_addr == GNUNET_YES)
6049 opt_lp.presolve = GLP_ON;
6050 result = glp_simplex(ats->prob, &opt_lp);
6051 lp_solution = glp_get_status (ats->prob);
6053 if ((result == GLP_ETMLIM) || (result == GLP_ETMLIM))
6055 ats->stat.valid = GNUNET_NO;
6056 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS exceeded time or iteration limit!\n");
6060 if (ats_evaluate_results(result, lp_solution, "LP") == GNUNET_YES)
6062 stat->valid = GNUNET_YES;
6066 ats->simplex_rerun_required = GNUNET_YES;
6067 opt_lp.presolve = GLP_ON;
6068 result = glp_simplex(ats->prob, &opt_lp);
6069 lp_solution = glp_get_status (ats->prob);
6071 // TODO: Remove if this does not appear until release
6072 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "EXECUTED SIMPLEX WITH PRESOLVER! %i", lp_solution);
6074 if (ats_evaluate_results(result, lp_solution, "LP") != GNUNET_YES)
6076 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "After execution simplex with presolver: STILL INVALID!\n");
6078 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);
6079 glp_write_lp (ats->prob, NULL, filename);
6080 GNUNET_free (filename);
6081 stat->valid = GNUNET_NO;
6084 stat->valid = GNUNET_YES;
6089 glp_init_iocp(&opt_mlp);
6091 opt_mlp.tm_lim = max_dur;
6094 opt_mlp.msg_lev = GLP_MSG_ALL;
6096 opt_mlp.msg_lev = GLP_MSG_OFF;
6099 result = glp_intopt (ats->prob, &opt_mlp);
6100 mlp_solution = glp_mip_status (ats->prob);
6101 stat->solution = mlp_solution;
6103 if (ats_evaluate_results(result, mlp_solution, "MLP") == GNUNET_YES)
6105 stat->valid = GNUNET_YES;
6109 // TODO: Remove if this does not appear until release
6110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP SOLUTION INVALID: %i\n", lp_solution);
6111 stat->valid = GNUNET_NO;
6116 int error = GNUNET_NO;
6118 struct ATS_mechanism *t = NULL;
6119 for (c=1; c<= (c_peers); c++ )
6122 t = peers[c].m_head;
6125 bw = glp_get_col_prim(prob, t->col_index);
6129 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);
6131 if (check ==GNUNET_YES)
6133 glp_write_sol(prob, "invalid_solution.mlp");
6134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid solution, check invalid_solution.mlp");
6135 GNUNET_STATISTICS_update (stats, "ATS invalid solutions", 1, GNUNET_NO);
6138 if (check ==GNUNET_NO)
6146 if (glp_get_col_prim(ats->prob,2*c_mechs+1) != 1)
6149 for (c=1; c<= available_quality_metrics; c++ )
6151 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));
6153 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));
6154 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));
6155 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));
6156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "objective value: %f\n", glp_mip_obj_val(ats->prob));
6161 static void ats_delete_problem ()
6165 for (c=0; c< (ats->stat).c_mechs; c++)
6166 GNUNET_free_non_null (ats->mechanisms[c].rc);
6168 if (ats->mechanisms!=NULL)
6170 GNUNET_free(ats->mechanisms);
6171 ats->mechanisms = NULL;
6173 if (ats->peers!=NULL)
6175 GNUNET_free(ats->peers);
6179 if (ats->prob != NULL)
6181 glp_delete_prob(ats->prob);
6185 ats->stat.begin_cr = GNUNET_SYSERR;
6186 ats->stat.begin_qm = GNUNET_SYSERR;
6187 ats->stat.c_mechs = 0;
6188 ats->stat.c_peers = 0;
6189 ats->stat.end_cr = GNUNET_SYSERR;
6190 ats->stat.end_qm = GNUNET_SYSERR;
6191 ats->stat.solution = GNUNET_SYSERR;
6192 ats->stat.valid = GNUNET_SYSERR;
6196 static void ats_update_problem_qm ()
6201 int c_q_metrics = available_quality_metrics;
6203 int *ja = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (int));
6204 double *ar = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (double));
6206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating problem quality metrics\n");
6208 row_index = ats->stat.begin_qm;
6210 for (c=1; c <= c_q_metrics; c++)
6215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6218 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6219 for (c2=1; c2<=ats->stat.c_mechs; c2++)
6221 ja[array_index] = c2;
6223 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY)
6226 v0 = ats->mechanisms[c2].addr->quality[c-1].values[0];
6227 if (v1 < 1) v0 = 0.1;
6228 v1 = ats->mechanisms[c2].addr->quality[c-1].values[1];
6229 if (v1 < 1) v0 = 0.1;
6230 v2 = ats->mechanisms[c2].addr->quality[c-1].values[2];
6231 if (v1 < 1) v0 = 0.1;
6232 value = 100.0 / ((v0 + 2 * v1 + 3 * v2) / 6.0);
6235 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
6238 v0 = ats->mechanisms[c2].addr->quality[c-1].values[0];
6240 v1 = ats->mechanisms[c2].addr->quality[c-1].values[1];
6242 v2 = ats->mechanisms[c2].addr->quality[c-1].values[2];
6244 value = (v0 + 2 * v1 + 3 * v2) / 6.0;
6246 value = (double) 10 / value;
6250 ar[array_index] = (ats->mechanisms[c2].peer->f) * value;
6252 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]);
6256 ja[array_index] = ats->stat.col_qm + c - 1;
6257 ar[array_index] = -1;
6260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, row_index, ja[array_index], ar[array_index]);
6262 glp_set_mat_row (ats->prob, row_index, array_index, ja, ar);
6268 GNUNET_free_non_null (ja);
6269 GNUNET_free_non_null (ar);
6273 static void ats_update_problem_cr ()
6279 double ct_max, ct_min;
6281 int *ja = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (int));
6282 double *ar = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (double));
6284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating problem quality metrics\n");
6285 row_index = ats->stat.begin_cr;
6287 for (c=0; c<available_ressources; c++)
6289 ct_max = ressources[c].c_max;
6290 ct_min = ressources[c].c_min;
6292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] %f..%f\n",row_index, ct_min, ct_max);
6294 glp_set_row_bnds(ats->prob, row_index, GLP_DB, ct_min, ct_max);
6296 for (c2=1; c2<=ats->stat.c_mechs; c2++)
6299 ja[array_index] = c2;
6300 value = ats->mechanisms[c2].addr->ressources[c].c;
6301 ar[array_index] = value;
6303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, row_index, ja[array_index], ar[array_index]);
6307 glp_set_mat_row (ats->prob, row_index, array_index, ja, ar);
6313 GNUNET_free_non_null (ja);
6314 GNUNET_free_non_null (ar);
6319 static void ats_update_problem_qm_TEST ()
6324 int old_ja[ats->stat.c_mechs + 2];
6325 double old_ar[ats->stat.c_mechs + 2];
6329 int *ja = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (int));
6330 double *ar = GNUNET_malloc ((1 + ats->stat.c_mechs*2 + 3 + available_quality_metrics) * sizeof (double));
6332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating problem quality metrics TEST\n");
6334 if (ats->stat.begin_qm >0)
6335 row_index = ats->stat.begin_qm;
6340 for (c=0; c<available_quality_metrics; c++)
6343 c_old = glp_get_mat_row (ats->prob, row_index, old_ja, old_ar);
6345 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6347 for (c2=1; c2<=c_old; c2++)
6349 ja[c2] = old_ja[c2];
6350 if ((changed < 3) && (c2>2) && (old_ar[c2] != -1))
6352 ar[c2] = old_ar[c2] + 5 - changed;
6356 ar[c2] = old_ar[c2];
6358 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]);
6361 glp_set_mat_row (ats->prob, row_index, c_old, ja, ar);
6366 GNUNET_free_non_null (ja);
6367 GNUNET_free_non_null (ar);
6372 /** solve the bandwidth distribution problem
6373 * @param max_it maximum iterations
6374 * @param max_dur maximum duration in ms
6375 * @param D weight for diversity
6376 * @param U weight for utility
6377 * @param R weight for relativity
6378 * @param v_b_min minimal bandwidth per peer
6379 * @param v_n_min minimum number of connections
6380 * @param stat result struct
6381 * @return GNUNET_SYSERR if glpk is not available, number of mechanisms used
6383 static int ats_create_problem (double D, double U, double R, int v_b_min, int v_n_min, struct ATS_stat *stat)
6385 if (ats->prob != NULL)
6386 glp_delete_prob(ats->prob);
6388 ats->prob = glp_create_prob();
6394 int c_c_ressources = available_ressources;
6395 int c_q_metrics = available_quality_metrics;
6397 double M = VERY_BIG_DOUBLE_VALUE;
6398 double Q[c_q_metrics+1];
6399 for (c=1; c<=c_q_metrics; c++)
6404 struct NeighbourList *next = neighbours;
6407 struct ReadyList *r_next = next->plugins;
6408 while (r_next != NULL)
6410 struct ForeignAddressList * a_next = r_next->addresses;
6411 while (a_next != NULL)
6414 a_next = a_next->next;
6416 r_next = r_next->next;
6425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No addresses for bw distribution available\n", c_peers);
6427 stat->valid = GNUNET_NO;
6430 return GNUNET_SYSERR;
6433 ats->mechanisms = GNUNET_malloc((1+c_mechs) * sizeof (struct ATS_mechanism));
6434 ats->peers = GNUNET_malloc((1+c_peers) * sizeof (struct ATS_peer));
6436 struct ATS_mechanism * mechanisms = ats->mechanisms;
6437 struct ATS_peer * peers = ats->peers;
6444 peers[c_peers].peer = next->id;
6445 peers[c_peers].m_head = NULL;
6446 peers[c_peers].m_tail = NULL;
6448 peers[c_peers].f = 1.0 / c_mechs;
6450 struct ReadyList *r_next = next->plugins;
6451 while (r_next != NULL)
6453 struct ForeignAddressList * a_next = r_next->addresses;
6454 while (a_next != NULL)
6456 mechanisms[c_mechs].addr = a_next;
6457 mechanisms[c_mechs].col_index = c_mechs;
6458 mechanisms[c_mechs].peer = &peers[c_peers];
6459 mechanisms[c_mechs].next = NULL;
6460 mechanisms[c_mechs].plugin = r_next->plugin;
6462 GNUNET_CONTAINER_DLL_insert_tail(peers[c_peers].m_head, peers[c_peers].m_tail, &mechanisms[c_mechs]);
6464 a_next = a_next->next;
6466 r_next = r_next->next;
6474 if (v_n_min > c_peers)
6478 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);
6481 int size = 1 + 3 + 10 *c_mechs + c_peers + (c_q_metrics*c_mechs)+ c_q_metrics + c_c_ressources * c_mechs ;
6484 int * ia = GNUNET_malloc (size * sizeof (int));
6485 int * ja = GNUNET_malloc (size * sizeof (int));
6486 double * ar = GNUNET_malloc(size* sizeof (double));
6488 glp_set_prob_name(ats->prob, "gnunet ats bandwidth distribution");
6489 glp_set_obj_dir(ats->prob, GLP_MAX);
6491 /* adding columns */
6493 glp_add_cols(ats->prob, 2 * c_mechs);
6494 /* adding b_t cols */
6495 for (c=1; c <= c_mechs; c++)
6498 GNUNET_asprintf(&name, "p_%s_b%i",GNUNET_i2s(&(mechanisms[c].peer->peer)), c);
6499 glp_set_col_name(ats->prob, c, name);
6501 glp_set_col_bnds(ats->prob, c, GLP_LO, 0.0, 0.0);
6502 glp_set_obj_coef(ats->prob, c, 0);
6505 /* adding n_t cols */
6506 for (c=c_mechs+1; c <= 2*c_mechs; c++)
6508 GNUNET_asprintf(&name, "p_%s_n%i",GNUNET_i2s(&(mechanisms[c-c_mechs].peer->peer)),(c-c_mechs));
6509 glp_set_col_name(ats->prob, c, name);
6511 glp_set_col_bnds(ats->prob, c, GLP_DB, 0.0, 1.0);
6512 glp_set_col_kind(ats->prob, c, GLP_IV);
6513 glp_set_obj_coef(ats->prob, c, 0);
6516 /* feasibility constraints */
6517 /* Constraint 1: one address per peer*/
6519 glp_add_rows(ats->prob, c_peers);
6520 for (c=1; c<=c_peers; c++)
6522 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 1.0, 1.0);
6524 struct ATS_mechanism *m = peers[c].m_head;
6527 ia[array_index] = row_index;
6528 ja[array_index] = (c_mechs + m->col_index);
6529 ar[array_index] = 1;
6531 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6539 /* Constraint 2: only active mechanism gets bandwidth assigned */
6540 glp_add_rows(ats->prob, c_mechs);
6541 for (c=1; c<=c_mechs; c++)
6543 /* b_t - n_t * M <= 0 */
6545 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6547 glp_set_row_bnds(ats->prob, row_index, GLP_UP, 0.0, 0.0);
6549 ia[array_index] = row_index;
6550 ja[array_index] = mechanisms[c].col_index;
6551 ar[array_index] = 1;
6553 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6556 ia[array_index] = row_index;
6557 ja[array_index] = c_mechs + mechanisms[c].col_index;
6558 ar[array_index] = -M;
6560 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6566 /* Constraint 3: minimum bandwidth*/
6567 glp_add_rows(ats->prob, c_mechs);
6568 for (c=1; c<=c_mechs; c++)
6570 /* b_t - n_t * b_min <= 0 */
6572 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6574 glp_set_row_bnds(ats->prob, row_index, GLP_LO, 0.0, 0.0);
6576 ia[array_index] = row_index;
6577 ja[array_index] = mechanisms[c].col_index;
6578 ar[array_index] = 1;
6580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6583 ia[array_index] = row_index;
6584 ja[array_index] = c_mechs + mechanisms[c].col_index;
6585 ar[array_index] = -v_b_min;
6587 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6593 /* Constraint 4: max ressource capacity */
6594 /* V cr: bt * ct_r <= cr_max
6596 glp_add_rows(ats->prob, available_ressources);
6597 double ct_max = VERY_BIG_DOUBLE_VALUE;
6598 double ct_min = 0.0;
6600 stat->begin_cr = array_index;
6602 for (c=0; c<available_ressources; c++)
6604 ct_max = ressources[c].c_max;
6605 ct_min = ressources[c].c_min;
6607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] %f..%f\n",row_index, ct_min, ct_max);
6609 glp_set_row_bnds(ats->prob, row_index, GLP_DB, ct_min, ct_max);
6611 for (c2=1; c2<=c_mechs; c2++)
6614 ia[array_index] = row_index;
6615 ja[array_index] = c2;
6616 value = mechanisms[c2].addr->ressources[c].c;
6617 ar[array_index] = value;
6619 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6625 stat->end_cr = array_index--;
6627 /* Constraint 5: min number of connections*/
6628 glp_add_rows(ats->prob, 1);
6629 for (c=1; c<=c_mechs; c++)
6631 // b_t - n_t * b_min >= 0
6633 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6635 glp_set_row_bnds(ats->prob, row_index, GLP_LO, v_n_min, 0.0);
6637 ia[array_index] = row_index;
6638 ja[array_index] = c_mechs + mechanisms[c].col_index;
6639 ar[array_index] = 1;
6641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6647 // optimisation constraints
6651 // Constraint 6: optimize for diversity
6653 col_d = glp_add_cols(ats->prob, 1);
6654 stat->col_d = col_d;
6655 //GNUNET_assert (col_d == (2*c_mechs) + 1);
6656 glp_set_col_name(ats->prob, col_d, "d");
6657 glp_set_obj_coef(ats->prob, col_d, D);
6658 glp_set_col_bnds(ats->prob, col_d, GLP_LO, 0.0, 0.0);
6659 glp_add_rows(ats->prob, 1);
6661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6663 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6664 for (c=1; c<=c_mechs; c++)
6666 // b_t - n_t * b_min >= 0
6667 ia[array_index] = row_index;
6668 ja[array_index] = c_mechs + mechanisms[c].col_index;
6669 ar[array_index] = 1;
6671 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6675 ia[array_index] = row_index;
6676 ja[array_index] = col_d;
6677 ar[array_index] = -1;
6679 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6685 // Constraint 7: optimize for quality
6687 col_qm = glp_add_cols(ats->prob, c_q_metrics);
6688 stat->col_qm = col_qm;
6689 //GNUNET_assert (col_qm == (2*c_mechs) + 3 + 1);
6690 for (c=0; c< c_q_metrics; c++)
6692 GNUNET_asprintf(&name, "Q_%s",qm[c].name);
6693 glp_set_col_name(ats->prob, col_qm + c, name);
6694 glp_set_col_bnds(ats->prob, col_qm + c, GLP_LO, 0.0, 0.0);
6696 glp_set_obj_coef(ats->prob, col_qm + c, Q[c]);
6698 glp_add_rows(ats->prob, available_quality_metrics);
6699 stat->begin_qm = row_index;
6700 for (c=1; c <= c_q_metrics; c++)
6703 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6706 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6707 for (c2=1; c2<=c_mechs; c2++)
6710 ia[array_index] = row_index;
6711 ja[array_index] = c2;
6712 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY)
6715 v0 = mechanisms[c2].addr->quality[c-1].values[0];
6716 if (v1 < 1) v0 = 0.1;
6717 v1 = mechanisms[c2].addr->quality[c-1].values[1];
6718 if (v1 < 1) v0 = 0.1;
6719 v2 = mechanisms[c2].addr->quality[c-1].values[2];
6720 if (v1 < 1) v0 = 0.1;
6721 value = 100.0 / ((v0 + 2 * v1 + 3 * v2) / 6.0);
6724 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
6727 v0 = mechanisms[c2].addr->quality[c-1].values[0];
6729 v1 = mechanisms[c2].addr->quality[c-1].values[1];
6731 v2 = mechanisms[c2].addr->quality[c-1].values[2];
6733 value = (v0 + 2 * v1 + 3 * v2) / 6.0;
6735 value = (double) 10 / value;
6739 ar[array_index] = (mechanisms[c2].peer->f) * value ;
6741 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]);
6746 ia[array_index] = row_index;
6747 ja[array_index] = col_qm + c - 1;
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]);
6755 stat->end_qm = row_index-1;
6757 // Constraint 8: optimize bandwidth utility
6759 col_u = glp_add_cols(ats->prob, 1);
6760 stat->col_u = col_u;
6761 //GNUNET_assert (col_u == (2*c_mechs) + 2);
6762 glp_set_col_name(ats->prob, col_u, "u");
6763 glp_set_obj_coef(ats->prob, col_u, U);
6764 glp_set_col_bnds(ats->prob, col_u, GLP_LO, 0.0, 0.0);
6765 glp_add_rows(ats->prob, 1);
6767 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6769 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6770 for (c=1; c<=c_mechs; c++)
6772 ia[array_index] = row_index;
6773 ja[array_index] = c;
6774 ar[array_index] = mechanisms[c].peer->f;
6776 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6780 ia[array_index] = row_index;
6781 ja[array_index] = col_u;
6782 ar[array_index] = -1;
6784 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6790 // Constraint 9: optimize relativity
6792 col_r = glp_add_cols(ats->prob, 1);
6793 stat->col_r = col_r;
6794 //GNUNET_assert (col_r == (2*c_mechs) + 3);
6795 glp_set_col_name(ats->prob, col_r, "r");
6796 glp_set_obj_coef(ats->prob, col_r, R);
6797 glp_set_col_bnds(ats->prob, col_r, GLP_LO, 0.0, 0.0);
6798 glp_add_rows(ats->prob, c_peers);
6799 for (c=1; c<=c_peers; c++)
6801 glp_set_row_bnds(ats->prob, row_index, GLP_LO, 0.0, 0.0);
6803 struct ATS_mechanism *m = peers[c].m_head;
6806 ia[array_index] = row_index;
6807 ja[array_index] = m->col_index;
6808 ar[array_index] = 1 / mechanisms[c].peer->f;
6810 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6815 ia[array_index] = row_index;
6816 ja[array_index] = col_r;
6817 ar[array_index] = -1;
6819 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6826 /* Loading the matrix */
6827 glp_load_matrix(ats->prob, array_index-1, ia, ja, ar);
6829 stat->c_mechs = c_mechs;
6830 stat->c_peers = c_peers;
6832 stat->valid = GNUNET_YES;
6844 void ats_notify_ats_data (
6845 const struct GNUNET_PeerIdentity *peer,
6846 const struct GNUNET_TRANSPORT_ATS_Information *ats_data)
6849 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "ATS_notify_ats_data: %s\n",GNUNET_i2s(peer));
6851 ats_calculate_bandwidth_distribution();
6853 #endif //END: HAVE_LIBGLPK
6856 ats_calculate_bandwidth_distribution ()
6859 struct GNUNET_TIME_Absolute start;
6860 struct GNUNET_TIME_Relative creation;
6861 struct GNUNET_TIME_Relative solving;
6862 char *text = "unmodified";
6864 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_difference (ats->last, GNUNET_TIME_absolute_get());
6865 if (delta.rel_value < ats->min_delta.rel_value)
6868 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Minimum time between cycles not reached\n");
6874 if (INT_MAX < ats->max_exec_duration.rel_value)
6877 dur = (int) ats->max_exec_duration.rel_value;
6878 ats->simplex_rerun_required = GNUNET_NO;
6880 start = GNUNET_TIME_absolute_get();
6881 if ((ats->modified_addr == GNUNET_YES) || (ats->prob==NULL))
6884 ats->modified_addr = GNUNET_YES;
6885 ats_delete_problem ();
6886 ats_create_problem (ats->D, ats->U, ats->R, ats->v_b_min, ats->v_n_min, &ats->stat);
6888 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);
6891 else if ((ats->modified_addr == GNUNET_NO) && (ats->modified_resources == GNUNET_YES))
6893 ats_update_problem_cr();
6894 text = "modified resources";
6896 else if ((ats->modified_addr == GNUNET_NO) && (ats->modified_quality == GNUNET_YES))
6898 ats_update_problem_qm();
6899 //ats_update_problem_qm_TEST ();
6900 text = "modified quality";
6903 else GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Problem is unmodified\n");
6906 creation = GNUNET_TIME_absolute_get_difference(start,GNUNET_TIME_absolute_get());
6907 start = GNUNET_TIME_absolute_get();
6909 if (ats->stat.valid == GNUNET_YES)
6911 ats->stat.solution = GNUNET_SYSERR;
6912 ats_solve_problem(ats->max_iterations, ats->max_exec_duration.rel_value, ats->stat.c_peers, ats->stat.c_mechs, &ats->stat);
6914 solving = GNUNET_TIME_absolute_get_difference(start,GNUNET_TIME_absolute_get());
6916 if (ats->stat.valid == GNUNET_YES)
6919 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP %s: creation time in [ms] %llu execution time in [ms] %llu for %i mechanisms: simplex rerun: %s\n",
6920 text, creation.rel_value, solving.rel_value,
6922 (ats->simplex_rerun_required == GNUNET_NO) ? " NO" : "YES");
6924 GNUNET_STATISTICS_set (stats, "ATS duration", solving.rel_value + creation.rel_value, GNUNET_NO);
6925 GNUNET_STATISTICS_set (stats, "ATS mechanisms", ats->stat.c_mechs, GNUNET_NO);
6926 GNUNET_STATISTICS_set (stats, "ATS peers", ats->stat.c_peers, GNUNET_NO);
6927 GNUNET_STATISTICS_set (stats, "ATS solution", ats->stat.solution, GNUNET_NO);
6928 GNUNET_STATISTICS_set (stats, "ATS timestamp", start.abs_value, GNUNET_NO);
6930 if ((ats->modified_addr == GNUNET_YES) || (ats->prob==NULL))
6931 GNUNET_STATISTICS_set (stats, "ATS state",ATS_NEW, GNUNET_NO);
6932 else if ((ats->modified_resources == GNUNET_YES) &&
6933 (ats->modified_quality == GNUNET_NO))
6934 GNUNET_STATISTICS_set (stats, "ATS state", ATS_C_UPDATED, GNUNET_NO);
6935 else if ((ats->modified_resources == GNUNET_NO) &&
6936 (ats->modified_quality == GNUNET_YES) &&
6937 (ats->simplex_rerun_required == GNUNET_NO))
6938 GNUNET_STATISTICS_set (stats, "ATS state", ATS_Q_UPDATED, GNUNET_NO);
6939 else if ((ats->modified_resources == GNUNET_YES) &&
6940 (ats->modified_quality == GNUNET_YES) &&
6941 (ats->simplex_rerun_required == GNUNET_NO))
6942 GNUNET_STATISTICS_set (stats, "ATS state", ATS_QC_UPDATED, GNUNET_NO);
6943 else if (ats->simplex_rerun_required == GNUNET_NO)
6944 GNUNET_STATISTICS_set (stats, "ATS state", ATS_UNMODIFIED, GNUNET_NO);
6947 if (ats->save_mlp == GNUNET_YES)
6950 GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_%llu.mlp",
6951 ats->stat.c_peers, ats->stat.c_mechs, GNUNET_TIME_absolute_get().abs_value);
6952 //if (GNUNET_NO == GNUNET_DISK_file_test(filename))
6953 glp_write_lp (ats->prob, NULL, filename);
6954 GNUNET_free (filename);
6956 if (ats->save_solution == GNUNET_YES)
6959 GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_%llu.sol",
6960 ats->stat.c_peers, ats->stat.c_mechs, GNUNET_TIME_absolute_get().abs_value);
6961 //if (GNUNET_NO == GNUNET_DISK_file_test(filename))
6962 glp_print_sol (ats->prob, filename);
6963 GNUNET_free (filename);
6965 ats->last = GNUNET_TIME_absolute_get();
6966 ats->modified_addr = GNUNET_NO;
6967 ats->modified_resources = GNUNET_NO;
6968 ats->modified_quality = GNUNET_NO;
6973 ats_schedule_calculation (void *cls,
6974 const struct GNUNET_SCHEDULER_TaskContext *tc)
6976 struct ATS_info *ats = (struct ATS_info *) cls;
6977 if (ats==NULL) return;
6979 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6980 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
6984 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running scheduled calculation\n");
6987 ats_calculate_bandwidth_distribution (ats);
6989 ats->ats_task = GNUNET_SCHEDULER_add_delayed (ats->exec_interval,
6990 &ats_schedule_calculation, ats);
6996 unsigned long long value;
6999 ats = GNUNET_malloc(sizeof (struct ATS_info));
7001 ats->min_delta = ATS_MIN_INTERVAL;
7002 ats->exec_interval = ATS_EXEC_INTERVAL;
7003 ats->max_exec_duration = ATS_MAX_EXEC_DURATION;
7004 ats->max_iterations = ATS_MAX_ITERATIONS;
7005 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
7008 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "GLPK not installed, ATS not active\n");
7015 ats->v_b_min = 64000;
7021 /* loading cost ressources */
7022 for (c=0; c<available_ressources; c++)
7024 GNUNET_asprintf(§ion,"%s_UP",ressources[c].cfg_param);
7025 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
7027 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "transport",section, &value))
7030 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found ressource cost: [%s] = %llu\n", section, value);
7032 ressources[c].c_max = value;
7035 GNUNET_free (section);
7036 GNUNET_asprintf(§ion,"%s_DOWN",ressources[c].cfg_param);
7037 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
7039 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "transport",section, &value))
7042 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found ressource cost: [%s] = %llu\n", section, value);
7044 ressources[c].c_min = value;
7047 GNUNET_free (section);
7050 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_MLP"))
7051 ats->save_mlp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "transport","DUMP_MLP");
7053 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_SOLUTION"))
7054 ats->save_solution = GNUNET_CONFIGURATION_get_value_yesno (cfg, "transport","DUMP_SOLUTION");
7056 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "ATS_EXEC_INTERVAL"))
7058 GNUNET_CONFIGURATION_get_value_number(cfg, "transport","ATS_EXEC_INTERVAL", &value);
7059 ats->exec_interval.rel_value = value;
7061 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "ATS_MIN_INTERVAL"))
7063 GNUNET_CONFIGURATION_get_value_number(cfg, "transport","ATS_MIN_INTERVAL", &value);
7064 ats->min_delta.rel_value = value;
7067 ats->ats_task = GNUNET_SCHEDULER_add_now(&ats_schedule_calculation, ats);
7071 static void ats_shutdown ()
7074 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_destroy\n");
7076 if (ats->ats_task != GNUNET_SCHEDULER_NO_TASK)
7077 GNUNET_SCHEDULER_cancel(ats->ats_task);
7078 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
7080 ats_delete_problem ();
7086 void ats_notify_peer_connect (
7087 const struct GNUNET_PeerIdentity *peer,
7088 const struct GNUNET_TRANSPORT_ATS_Information *ats_data, int ats_count)
7091 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_notify_peer_connect: %s\n",GNUNET_i2s(peer));
7094 //update_addr_ats();
7095 ats->modified_addr = GNUNET_YES;
7097 ats_calculate_bandwidth_distribution(ats);
7100 void ats_notify_peer_disconnect (
7101 const struct GNUNET_PeerIdentity *peer)
7104 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_notify_peer_disconnect: %s\n",GNUNET_i2s(peer));
7107 ats->modified_addr = GNUNET_YES;
7109 ats_calculate_bandwidth_distribution (ats);
7112 struct ForeignAddressList * ats_get_preferred_address (
7113 struct NeighbourList *n)
7116 //GNUNET_log (GNUNET_ERROR_TYPE_BULK, "ats_get_prefered_transport for peer: %s\n",GNUNET_i2s(&n->id));
7118 struct ReadyList *next = n->plugins;
7119 while (next != NULL)
7122 //GNUNET_log (GNUNET_ERROR_TYPE_BULK, "plugin: %s %i\n",next->plugin->short_name,strcmp(next->plugin->short_name,"unix"));
7126 return find_ready_address(n);
7130 * Initiate transport service.
7132 * @param cls closure
7133 * @param server the initialized server
7134 * @param c configuration to use
7138 struct GNUNET_SERVER_Handle *server,
7139 const struct GNUNET_CONFIGURATION_Handle *c)
7141 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
7142 {&handle_start, NULL,
7143 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
7144 {&handle_hello, NULL,
7145 GNUNET_MESSAGE_TYPE_HELLO, 0},
7146 {&handle_send, NULL,
7147 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
7148 {&handle_request_connect, NULL,
7149 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT, sizeof(struct TransportRequestConnectMessage)},
7150 {&handle_set_quota, NULL,
7151 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
7152 {&handle_address_lookup, NULL,
7153 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
7155 {&handle_blacklist_init, NULL,
7156 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
7157 {&handle_blacklist_reply, NULL,
7158 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
7164 unsigned long long tneigh;
7168 stats = GNUNET_STATISTICS_create ("transport", cfg);
7169 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
7170 /* parse configuration */
7172 GNUNET_CONFIGURATION_get_value_number (c,
7177 GNUNET_CONFIGURATION_get_value_filename (c,
7179 "HOSTKEY", &keyfile)))
7181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7183 ("Transport service is lacking key configuration settings. Exiting.\n"));
7184 GNUNET_SCHEDULER_shutdown ();
7187 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
7190 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
7191 validation_map = NULL;
7195 max_connect_per_transport = (uint32_t) tneigh;
7196 peerinfo = GNUNET_PEERINFO_connect (cfg);
7197 if (peerinfo == NULL)
7199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7200 _("Could not access PEERINFO service. Exiting.\n"));
7201 GNUNET_SCHEDULER_shutdown ();
7204 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
7207 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
7208 validation_map = NULL;
7209 GNUNET_free (keyfile);
7212 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7213 GNUNET_free (keyfile);
7214 if (my_private_key == NULL)
7216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7218 ("Transport service could not access hostkey. Exiting.\n"));
7219 GNUNET_SCHEDULER_shutdown ();
7222 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
7225 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
7226 validation_map = NULL;
7229 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7230 GNUNET_CRYPTO_hash (&my_public_key,
7231 sizeof (my_public_key), &my_identity.hashPubKey);
7232 /* setup notification */
7233 GNUNET_SERVER_disconnect_notify (server,
7234 &client_disconnect_notification, NULL);
7235 /* load plugins... */
7238 GNUNET_CONFIGURATION_get_value_string (c,
7239 "TRANSPORT", "PLUGINS", &plugs))
7241 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7242 _("Starting transport plugins `%s'\n"), plugs);
7243 pos = strtok (plugs, " ");
7246 start_transport (server, pos);
7248 pos = strtok (NULL, " ");
7250 GNUNET_free (plugs);
7252 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
7253 &shutdown_task, NULL);
7260 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport service ready.\n"));
7262 /* If we have a blacklist file, read from it */
7263 read_blacklist_file(cfg);
7264 /* process client requests */
7265 GNUNET_SERVER_add_handlers (server, handlers);
7270 * The main function for the transport service.
7272 * @param argc number of arguments from the command line
7273 * @param argv command line arguments
7274 * @return 0 ok, 1 on error
7277 main (int argc, char *const *argv)
7279 a2s (NULL, NULL, 0); /* make compiler happy */
7280 return (GNUNET_OK ==
7281 GNUNET_SERVICE_run (argc,
7284 GNUNET_SERVICE_OPTION_NONE,
7285 &run, NULL)) ? 0 : 1;
7288 /* end of gnunet-service-transport.c */