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
52 * Should we do some additional checks (to validate behavior
55 #define EXTRA_CHECKS GNUNET_YES
58 * How many messages can we have pending for a given client process
59 * before we start to drop incoming messages? We typically should
60 * have only one client and so this would be the primary buffer for
61 * messages, so the number should be chosen rather generously.
63 * The expectation here is that most of the time the queue is large
64 * enough so that a drop is virtually never required. Note that
65 * this value must be about as large as 'TOTAL_MSGS' in the
66 * 'test_transport_api_reliability.c', otherwise that testcase may
69 #define MAX_PENDING (128 * 1024)
72 * Size of the per-transport blacklist hash maps.
74 #define TRANSPORT_BLACKLIST_HT_SIZE 16
77 * How often should we try to reconnect to a peer using a particular
78 * transport plugin before giving up? Note that the plugin may be
79 * added back to the list after PLUGIN_RETRY_FREQUENCY expires.
81 #define MAX_CONNECT_RETRY 3
84 * Limit on the number of ready-to-run tasks when validating
85 * HELLOs. If more tasks are ready to run, we will drop
86 * HELLOs instead of validating them.
88 #define MAX_HELLO_LOAD 4
91 * How often must a peer violate bandwidth quotas before we start
92 * to simply drop its messages?
94 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
97 * How long until a HELLO verification attempt should time out?
98 * Must be rather small, otherwise a partially successful HELLO
99 * validation (some addresses working) might not be available
100 * before a client's request for a connection fails for good.
101 * Besides, if a single request to an address takes a long time,
102 * then the peer is unlikely worthwhile anyway.
104 #define HELLO_VERIFICATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
107 * How long is a PONG signature valid? We'll recycle a signature until
108 * 1/4 of this time is remaining. PONGs should expire so that if our
109 * external addresses change an adversary cannot replay them indefinitely.
110 * OTOH, we don't want to spend too much time generating PONG signatures,
111 * so they must have some lifetime to reduce our CPU usage.
113 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
116 * Priority to use for PONG messages.
118 #define TRANSPORT_PONG_PRIORITY 4
121 * How often do we re-add (cheaper) plugins to our list of plugins
122 * to try for a given connected peer?
124 #define PLUGIN_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
127 * After how long do we expire an address in a HELLO that we just
128 * validated? This value is also used for our own addresses when we
131 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
135 * How long before an existing address expires should we again try to
136 * validate it? Must be (significantly) smaller than
137 * HELLO_ADDRESS_EXPIRATION.
139 #define HELLO_REVALIDATION_START_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
142 * Maximum frequency for re-evaluating latencies for all transport addresses.
144 #define LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
147 * Maximum frequency for re-evaluating latencies for connected addresses.
149 #define CONNECTED_LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
151 #define VERY_BIG_DOUBLE_VALUE 100000000000LL
154 * List of addresses of other peers
156 struct ForeignAddressList
159 * This is a linked list.
161 struct ForeignAddressList *next;
164 * Which ready list does this entry belong to.
166 struct ReadyList *ready_list;
169 * How long until we auto-expire this address (unless it is
170 * re-confirmed by the transport)?
172 struct GNUNET_TIME_Absolute expires;
175 * Task used to re-validate addresses, updates latencies and
178 GNUNET_SCHEDULER_TaskIdentifier revalidate_task;
186 * Session (or NULL if no valid session currently exists or if the
187 * plugin does not use sessions).
189 struct Session *session;
191 struct ATS_ressource_entry * ressources;
193 struct ATS_quality_entry * quality;
196 * What was the last latency observed for this address, plugin and peer?
198 struct GNUNET_TIME_Relative latency;
201 * If we did not successfully transmit a message to the given peer
202 * via this connection during the specified time, we should consider
203 * the connection to be dead. This is used in the case that a TCP
204 * transport simply stalls writing to the stream but does not
205 * formerly get a signal that the other peer died.
207 struct GNUNET_TIME_Absolute timeout;
210 * How often have we tried to connect using this plugin? Used to
211 * discriminate against addresses that do not work well.
212 * FIXME: not yet used, but should be!
214 unsigned int connect_attempts;
217 * DV distance to this peer (1 if no DV is used).
218 * FIXME: need to set this from transport plugins!
228 * Have we ever estimated the latency of this address? Used to
229 * ensure that the first time we add an address, we immediately
235 * Are we currently connected via this address? The first time we
236 * successfully transmit or receive data to a peer via a particular
237 * address, we set this to GNUNET_YES. If we later get an error
238 * (disconnect notification, transmission failure, timeout), we set
239 * it back to GNUNET_NO.
244 * Is this plugin currently busy transmitting to the specific target?
245 * GNUNET_NO if not (initial, default state is GNUNET_NO). Internal
246 * messages do not count as 'in transmit'.
251 * Has this address been validated yet?
259 * Entry in linked list of network addresses for ourselves. Also
260 * includes a cached signature for 'struct TransportPongMessage's.
262 struct OwnAddressList
265 * This is a linked list.
267 struct OwnAddressList *next;
270 * How long until we actually auto-expire this address (unless it is
271 * re-confirmed by the transport)?
273 struct GNUNET_TIME_Absolute expires;
276 * How long until the current signature expires? (ZERO if the
277 * signature was never created).
279 struct GNUNET_TIME_Absolute pong_sig_expires;
282 * Signature for a 'struct TransportPongMessage' for this address.
284 struct GNUNET_CRYPTO_RsaSignature pong_signature;
295 * Entry in linked list of all of our plugins.
297 struct TransportPlugin
301 * This is a linked list.
303 struct TransportPlugin *next;
306 * API of the transport as returned by the plugin's
307 * initialization function.
309 struct GNUNET_TRANSPORT_PluginFunctions *api;
312 * Short name for the plugin (i.e. "tcp").
317 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
322 * List of our known addresses for this transport.
324 struct OwnAddressList *addresses;
327 * Environment this transport service is using
330 struct GNUNET_TRANSPORT_PluginEnvironment env;
333 * ID of task that is used to clean up expired addresses.
335 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
338 * Set to GNUNET_YES if we need to scrap the existing list of
339 * "addresses" and start fresh when we receive the next address
340 * update from a transport. Set to GNUNET_NO if we should just add
341 * the new address to the list and wait for the commit call.
345 struct ATS_plugin * rc;
348 * Hashmap of blacklisted peers for this particular transport.
350 struct GNUNET_CONTAINER_MultiHashMap *blacklist;
353 struct NeighbourList;
356 * For each neighbour we keep a list of messages
357 * that we still want to transmit to the neighbour.
363 * This is a doubly linked list.
365 struct MessageQueue *next;
368 * This is a doubly linked list.
370 struct MessageQueue *prev;
373 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
374 * stuck together in memory. Allocated at the end of this struct.
376 const char *message_buf;
379 * Size of the message buf
381 size_t message_buf_size;
384 * Client responsible for queueing the message;
385 * used to check that a client has no two messages
386 * pending for the same target. Can be NULL.
388 struct TransportClient *client;
391 * Using which specific address should we send this message?
393 struct ForeignAddressList *specific_address;
396 * Peer ID of the Neighbour this entry belongs to.
398 struct GNUNET_PeerIdentity neighbour_id;
401 * Plugin that we used for the transmission.
402 * NULL until we scheduled a transmission.
404 struct TransportPlugin *plugin;
407 * At what time should we fail?
409 struct GNUNET_TIME_Absolute timeout;
412 * Internal message of the transport system that should not be
413 * included in the usual SEND-SEND_OK transmission confirmation
414 * traffic management scheme. Typically, "internal_msg" will
415 * be set whenever "client" is NULL (but it is not strictly
421 * How important is the message?
423 unsigned int priority;
429 * For a given Neighbour, which plugins are available
430 * to talk to this peer and what are their costs?
435 * This is a linked list.
437 struct ReadyList *next;
440 * Which of our transport plugins does this entry
443 struct TransportPlugin *plugin;
446 * Transport addresses, latency, and readiness for
447 * this particular plugin.
449 struct ForeignAddressList *addresses;
452 * To which neighbour does this ready list belong to?
454 struct NeighbourList *neighbour;
459 * Entry in linked list of all of our current neighbours.
465 * This is a linked list.
467 struct NeighbourList *next;
470 * Which of our transports is connected to this peer
471 * and what is their status?
473 struct ReadyList *plugins;
476 * Head of list of messages we would like to send to this peer;
477 * must contain at most one message per client.
479 struct MessageQueue *messages_head;
482 * Tail of list of messages we would like to send to this peer; must
483 * contain at most one message per client.
485 struct MessageQueue *messages_tail;
488 * Buffer for at most one payload message used when we receive
489 * payload data before our PING-PONG has succeeded. We then
490 * store such messages in this intermediary buffer until the
491 * connection is fully up.
493 struct GNUNET_MessageHeader *pre_connect_message_buffer;
496 * Context for peerinfo iteration.
497 * NULL after we are done processing peerinfo's information.
499 struct GNUNET_PEERINFO_IteratorContext *piter;
502 * Public key for this peer. Valid only if the respective flag is set below.
504 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
507 * Identity of this neighbour.
509 struct GNUNET_PeerIdentity id;
512 * ID of task scheduled to run when this peer is about to
513 * time out (will free resources associated with the peer).
515 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
518 * ID of task scheduled to run when we should retry transmitting
519 * the head of the message queue. Actually triggered when the
520 * transmission is timing out (we trigger instantly when we have
521 * a chance of success).
523 GNUNET_SCHEDULER_TaskIdentifier retry_task;
526 * How long until we should consider this peer dead
527 * (if we don't receive another message in the
530 struct GNUNET_TIME_Absolute peer_timeout;
533 * Tracker for inbound bandwidth.
535 struct GNUNET_BANDWIDTH_Tracker in_tracker;
538 * The latency we have seen for this particular address for
539 * this particular peer. This latency may have been calculated
540 * over multiple transports. This value reflects how long it took
541 * us to receive a response when SENDING via this particular
542 * transport/neighbour/address combination!
544 * FIXME: we need to periodically send PINGs to update this
545 * latency (at least more often than the current "huge" (11h?)
548 struct GNUNET_TIME_Relative latency;
551 * How often has the other peer (recently) violated the
552 * inbound traffic limit? Incremented by 10 per violation,
553 * decremented by 1 per non-violation (for each
556 unsigned int quota_violation_count;
559 * DV distance to this peer (1 if no DV is used).
564 * Have we seen an PONG from this neighbour in the past (and
565 * not had a disconnect since)?
570 * Do we have a valid public key for this neighbour?
572 int public_key_valid;
575 * Performance data for the peer.
577 struct GNUNET_TRANSPORT_ATS_Information *ats;
580 * Identity of the neighbour.
582 struct GNUNET_PeerIdentity peer;
587 * Message used to ask a peer to validate receipt (to check an address
588 * from a HELLO). Followed by the address we are trying to validate,
589 * or an empty address if we are just sending a PING to confirm that a
590 * connection which the receiver (of the PING) initiated is still valid.
592 struct TransportPingMessage
596 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
598 struct GNUNET_MessageHeader header;
601 * Challenge code (to ensure fresh reply).
603 uint32_t challenge GNUNET_PACKED;
606 * Who is the intended recipient?
608 struct GNUNET_PeerIdentity target;
614 * Message used to validate a HELLO. The challenge is included in the
615 * confirmation to make matching of replies to requests possible. The
616 * signature signs our public key, an expiration time and our address.<p>
618 * This message is followed by our transport address that the PING tried
619 * to confirm (if we liked it). The address can be empty (zero bytes)
620 * if the PING had not address either (and we received the request via
621 * a connection that we initiated).
623 struct TransportPongMessage
627 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
629 struct GNUNET_MessageHeader header;
632 * Challenge code from PING (showing freshness). Not part of what
633 * is signed so that we can re-use signatures.
635 uint32_t challenge GNUNET_PACKED;
640 struct GNUNET_CRYPTO_RsaSignature signature;
643 * What are we signing and why? Two possible reason codes can be here:
644 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
645 * plausible address for this peer (pid is set to identity of signer); or
646 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING to confirm that this is
647 * an address we used to connect to the peer with the given pid.
649 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
652 * When does this signature expire?
654 struct GNUNET_TIME_AbsoluteNBO expiration;
657 * Either the identity of the peer Who signed this message, or the
658 * identity of the peer that we're connected to using the given
659 * address (depending on purpose.type).
661 struct GNUNET_PeerIdentity pid;
664 * Size of address appended to this message (part of what is
665 * being signed, hence not redundant).
673 * Linked list of messages to be transmitted to the client. Each
674 * entry is followed by the actual message.
676 struct ClientMessageQueueEntry
679 * This is a doubly-linked list.
681 struct ClientMessageQueueEntry *next;
684 * This is a doubly-linked list.
686 struct ClientMessageQueueEntry *prev;
691 * Client connected to the transport service.
693 struct TransportClient
697 * This is a linked list.
699 struct TransportClient *next;
702 * Handle to the client.
704 struct GNUNET_SERVER_Client *client;
707 * Linked list of messages yet to be transmitted to
710 struct ClientMessageQueueEntry *message_queue_head;
713 * Tail of linked list of messages yet to be transmitted to the
716 struct ClientMessageQueueEntry *message_queue_tail;
719 * Current transmit request handle.
721 struct GNUNET_CONNECTION_TransmitHandle *th;
724 * Is a call to "transmit_send_continuation" pending? If so, we
725 * must not free this struct (even if the corresponding client
726 * disconnects) and instead only remove it from the linked list and
727 * set the "client" field to NULL.
732 * Length of the list of messages pending for this client.
734 unsigned int message_count;
740 * Context of currently active requests to peerinfo
741 * for validation of HELLOs.
743 struct CheckHelloValidatedContext;
747 * Entry in map of all HELLOs awaiting validation.
749 struct ValidationEntry
753 * NULL if this entry is not part of a larger HELLO validation.
755 struct CheckHelloValidatedContext *chvc;
758 * The address, actually a pointer to the end
759 * of this struct. Do not free!
764 * Name of the transport.
766 char *transport_name;
769 * The public key of the peer.
771 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
774 * ID of task that will clean up this entry if we don't succeed
775 * with the validation first.
777 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
780 * At what time did we send this validation?
782 struct GNUNET_TIME_Absolute send_time;
785 * Session being validated (or NULL for none).
787 struct Session *session;
790 * Challenge number we used.
803 * Context of currently active requests to peerinfo
804 * for validation of HELLOs.
806 struct CheckHelloValidatedContext
810 * This is a doubly-linked list.
812 struct CheckHelloValidatedContext *next;
815 * This is a doubly-linked list.
817 struct CheckHelloValidatedContext *prev;
820 * Hello that we are validating.
822 const struct GNUNET_HELLO_Message *hello;
825 * Context for peerinfo iteration.
826 * NULL after we are done processing peerinfo's information.
828 struct GNUNET_PEERINFO_IteratorContext *piter;
831 * Was a HELLO known for this peer to peerinfo?
836 * Number of validation entries currently referring to this
839 unsigned int ve_count;
842 struct ATS_quality_metric
851 struct ATS_mechanism * prev;
852 struct ATS_mechanism * next;
853 struct ForeignAddressList * addr;
854 struct TransportPlugin * plugin;
855 struct ATS_peer * peer;
858 struct ATS_ressource_cost * rc;
864 struct GNUNET_PeerIdentity peer;
865 struct NeighbourList * n;
866 struct ATS_mechanism * m_head;
867 struct ATS_mechanism * m_tail;
869 /* preference value f */
881 struct ATS_ressource_entry
883 /* index in ressources array */
885 /* depending ATSi parameter to calculcate limits */
894 /* index in ressources array */
896 /* depending ATSi parameter to calculcate limits */
898 /* cfg option to load limits */
905 /* cofficients for the specific plugins */
915 static struct ATS_ressource ressources[] =
917 /* FIXME: the coefficients for the specific plugins */
918 {1, 7, "LAN_BW_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 1, 1, 2, 2, 1, 3},
919 {2, 7, "WAN_BW_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 1, 1, 2, 2, 2, 3},
920 {3, 4, "WLAN_ENERGY_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 0, 0, 0, 0, 2, 1}
922 {4, 4, "COST_ENERGY_CONSUMPTION", VERY_BIG_DOUBLE_VALUE},
923 {5, 5, "COST_CONNECT", VERY_BIG_DOUBLE_VALUE},
924 {6, 6, "COST_BANDWITH_AVAILABLE", VERY_BIG_DOUBLE_VALUE},
925 {7, 7, "COST_NETWORK_OVERHEAD", VERY_BIG_DOUBLE_VALUE},*/
928 static int available_ressources = 3;
936 * Time of last execution
938 struct GNUNET_TIME_Absolute last;
940 * Minimum intervall between two executions
942 struct GNUNET_TIME_Relative min_delta;
944 * Regular intervall when execution is triggered
946 struct GNUNET_TIME_Relative exec_intervall;
948 * Maximum execution time per calculation
950 struct GNUNET_TIME_Relative max_exec_duration;
952 * Maximum number of LP iterations per calculation
956 GNUNET_SCHEDULER_TaskIdentifier ats_task;
958 struct ATS_plugin * head;
959 struct ATS_plugin * tail;
966 static struct GNUNET_HELLO_Message *our_hello;
971 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
976 static struct GNUNET_PeerIdentity my_identity;
981 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
986 const struct GNUNET_CONFIGURATION_Handle *cfg;
989 * Linked list of all clients to this service.
991 static struct TransportClient *clients;
994 * All loaded plugins.
996 static struct TransportPlugin *plugins;
999 * Handle to peerinfo service.
1001 static struct GNUNET_PEERINFO_Handle *peerinfo;
1004 * All known neighbours and their HELLOs.
1006 static struct NeighbourList *neighbours;
1009 * Number of neighbours we'd like to have.
1011 static uint32_t max_connect_per_transport;
1014 * Head of linked list.
1016 static struct CheckHelloValidatedContext *chvc_head;
1019 * Tail of linked list.
1021 static struct CheckHelloValidatedContext *chvc_tail;
1024 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
1025 * of the given peer that we are currently validating).
1027 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
1030 * Handle for reporting statistics.
1032 static struct GNUNET_STATISTICS_Handle *stats;
1035 * Handle for ats information
1037 static struct ATS_info *ats;
1039 struct ATS_quality_entry
1047 static struct ATS_quality_metric qm[] =
1049 {1, 1028, "QUALITY_NET_DISTANCE"},
1050 {2, 1034, "QUALITY_NET_DELAY"},
1052 static int available_quality_metrics = 2;
1056 * The peer specified by the given neighbour has timed-out or a plugin
1057 * has disconnected. We may either need to do nothing (other plugins
1058 * still up), or trigger a full disconnect and clean up. This
1059 * function updates our state and do the necessary notifications.
1060 * Also notifies our clients that the neighbour is now officially
1063 * @param n the neighbour list entry for the peer
1064 * @param check should we just check if all plugins
1065 * disconnected or must we ask all plugins to
1068 static void disconnect_neighbour (struct NeighbourList *n, int check);
1071 * Check the ready list for the given neighbour and if a plugin is
1072 * ready for transmission (and if we have a message), do so!
1074 * @param nexi target peer for which to transmit
1076 static void try_transmission_to_peer (struct NeighbourList *n);
1081 void ats_shutdown ( );
1083 void ats_notify_peer_connect (
1084 const struct GNUNET_PeerIdentity *peer,
1085 const struct GNUNET_TRANSPORT_ATS_Information *ats_data);
1087 void ats_notify_peer_disconnect (
1088 const struct GNUNET_PeerIdentity *peer);
1090 void ats_notify_ats_data (
1091 const struct GNUNET_PeerIdentity *peer,
1092 const struct GNUNET_TRANSPORT_ATS_Information *ats_data);
1094 struct ForeignAddressList * ats_get_preferred_address (
1095 struct NeighbourList *n);
1098 * Find an entry in the neighbour list for a particular peer.
1100 * @return NULL if not found.
1102 static struct NeighbourList *
1103 find_neighbour (const struct GNUNET_PeerIdentity *key)
1105 struct NeighbourList *head = neighbours;
1107 while ((head != NULL) &&
1108 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
1113 static void update_addr_ats (struct ForeignAddressList *fal, const struct GNUNET_TRANSPORT_ATS_Information *ats_data, int ats_count)
1116 for (c1=0; c1<ats_count; c1++)
1118 for (c2=0; c2<available_quality_metrics; c2++)
1120 if (ntohl(ats_data[c1].type) == qm[c2].atis_index)
1122 fal->quality[c2].values[0] = fal->quality[c2].values[1];
1123 fal->quality[c2].values[1] = fal->quality[c2].values[2];
1124 fal->quality[c2].values[2] = ntohl(ats_data[c1].value);
1130 static void update_addr_value (struct ForeignAddressList *fal, uint32_t value , int ats_index)
1133 for (c=0; c<available_quality_metrics; c++)
1135 if (ats_index == qm[c].atis_index)
1137 fal->quality[c].values[0] = fal->quality[c].values[1];
1138 fal->quality[c].values[1] = fal->quality[c].values[2];
1139 fal->quality[c].values[2] = value;
1145 * Find an entry in the transport list for a particular transport.
1147 * @return NULL if not found.
1149 static struct TransportPlugin *
1150 find_transport (const char *short_name)
1152 struct TransportPlugin *head = plugins;
1153 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
1159 * Is a particular peer blacklisted for a particular transport?
1161 * @param peer the peer to check for
1162 * @param plugin the plugin used to connect to the peer
1164 * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
1167 is_blacklisted (const struct GNUNET_PeerIdentity *peer, struct TransportPlugin *plugin)
1170 if (plugin->blacklist != NULL)
1172 if (GNUNET_CONTAINER_multihashmap_contains (plugin->blacklist, &peer->hashPubKey) == GNUNET_YES)
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 "Peer `%s:%s' is blacklisted!\n",
1177 plugin->short_name, GNUNET_i2s (peer));
1180 GNUNET_STATISTICS_update (stats, "# blacklisted peers refused", 1, GNUNET_NO);
1190 add_peer_to_blacklist (struct GNUNET_PeerIdentity *peer, char *transport_name)
1192 struct TransportPlugin *plugin;
1194 plugin = find_transport(transport_name);
1195 if (plugin == NULL) /* Nothing to do */
1197 if (plugin->blacklist == NULL)
1198 plugin->blacklist = GNUNET_CONTAINER_multihashmap_create(TRANSPORT_BLACKLIST_HT_SIZE);
1199 GNUNET_assert(plugin->blacklist != NULL);
1200 GNUNET_CONTAINER_multihashmap_put(plugin->blacklist, &peer->hashPubKey,
1202 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1207 * Read the blacklist file, containing transport:peer entries.
1208 * Provided the transport is loaded, set up hashmap with these
1209 * entries to blacklist peers by transport.
1213 read_blacklist_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1220 struct GNUNET_PeerIdentity pid;
1222 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1223 unsigned int entries_found;
1224 char *transport_name;
1227 GNUNET_CONFIGURATION_get_value_filename (cfg,
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1234 "Option `%s' in section `%s' not specified!\n",
1240 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1241 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1242 | GNUNET_DISK_PERM_USER_WRITE);
1243 if (0 != STAT (fn, &frstat))
1245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1246 _("Could not read blacklist file `%s'\n"), fn);
1250 if (frstat.st_size == 0)
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 _("Blacklist file `%s' is empty.\n"),
1260 /* FIXME: use mmap */
1261 data = GNUNET_malloc_large (frstat.st_size);
1262 GNUNET_assert(data != NULL);
1263 if (frstat.st_size !=
1264 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1267 _("Failed to read blacklist from `%s'\n"), fn);
1274 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1276 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1277 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1280 while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') && !isspace ( (unsigned char) data[colon_pos]))
1283 if (colon_pos >= frstat.st_size)
1285 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1286 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1287 (unsigned long long) colon_pos);
1293 if (isspace( (unsigned char) data[colon_pos]))
1295 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1296 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1297 (unsigned long long) colon_pos);
1299 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1303 tsize = colon_pos - pos;
1304 if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size) || (tsize == 0))
1306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1307 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1308 (unsigned long long) colon_pos);
1317 transport_name = GNUNET_malloc(tsize + 1);
1318 memcpy(transport_name, &data[pos], tsize);
1319 pos = colon_pos + 1;
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 "Read transport name %s in blacklist file.\n",
1325 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1326 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1328 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1329 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1330 (unsigned long long) pos);
1332 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1334 GNUNET_free_non_null(transport_name);
1337 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1338 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1340 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1341 _("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
1342 (unsigned long long) pos,
1347 if (0 != memcmp (&pid,
1349 sizeof (struct GNUNET_PeerIdentity)))
1352 add_peer_to_blacklist (&pid,
1357 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1358 _("Found myself `%s' in blacklist (useless, ignored)\n"),
1362 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1363 GNUNET_free_non_null(transport_name);
1364 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1367 GNUNET_STATISTICS_update (stats, "# Transport entries blacklisted", entries_found, GNUNET_NO);
1374 * Function called to notify a client about the socket being ready to
1375 * queue more data. "buf" will be NULL and "size" zero if the socket
1376 * was closed for writing in the meantime.
1378 * @param cls closure
1379 * @param size number of bytes available in buf
1380 * @param buf where the callee should write the message
1381 * @return number of bytes written to buf
1384 transmit_to_client_callback (void *cls, size_t size, void *buf)
1386 struct TransportClient *client = cls;
1387 struct ClientMessageQueueEntry *q;
1390 const struct GNUNET_MessageHeader *msg;
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1398 "Transmission to client failed, closing connection.\n");
1400 /* fatal error with client, free message queue! */
1401 while (NULL != (q = client->message_queue_head))
1403 GNUNET_STATISTICS_update (stats,
1404 gettext_noop ("# bytes discarded (could not transmit to client)"),
1405 ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
1407 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1408 client->message_queue_tail,
1412 client->message_count = 0;
1417 while (NULL != (q = client->message_queue_head))
1419 msg = (const struct GNUNET_MessageHeader *) &q[1];
1420 msize = ntohs (msg->size);
1421 if (msize + tsize > size)
1424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1425 "Transmitting message of type %u to client.\n",
1428 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1429 client->message_queue_tail,
1431 memcpy (&cbuf[tsize], msg, msize);
1434 client->message_count--;
1438 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1439 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1441 GNUNET_TIME_UNIT_FOREVER_REL,
1442 &transmit_to_client_callback,
1444 GNUNET_assert (client->th != NULL);
1451 * Convert an address to a string.
1453 * @param plugin name of the plugin responsible for the address
1454 * @param addr binary address
1455 * @param addr_len number of bytes in addr
1456 * @return NULL on error, otherwise address string
1459 a2s (const char *plugin,
1463 struct TransportPlugin *p;
1467 p = find_transport (plugin);
1470 return p->api->address_to_string (p->api->cls,
1477 * Mark the given FAL entry as 'connected' (and hence preferred for
1478 * sending); also mark all others for the same peer as 'not connected'
1479 * (since only one can be preferred).
1481 * @param fal address to set to 'connected'
1484 mark_address_connected (struct ForeignAddressList *fal)
1486 struct ForeignAddressList *pos;
1489 GNUNET_assert (GNUNET_YES == fal->validated);
1490 if (fal->connected == GNUNET_YES)
1491 return; /* nothing to do */
1493 pos = fal->ready_list->addresses;
1496 if (GNUNET_YES == pos->connected)
1499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1500 "Marking address `%s' as no longer connected (due to connect on other address)\n",
1501 a2s (pos->ready_list->plugin->short_name,
1505 GNUNET_break (cnt == GNUNET_YES);
1507 pos->connected = GNUNET_NO;
1508 GNUNET_STATISTICS_update (stats,
1509 gettext_noop ("# connected addresses"),
1515 fal->connected = GNUNET_YES;
1516 if (GNUNET_YES == cnt)
1518 GNUNET_STATISTICS_update (stats,
1519 gettext_noop ("# connected addresses"),
1527 * Send the specified message to the specified client. Since multiple
1528 * messages may be pending for the same client at a time, this code
1529 * makes sure that no message is lost.
1531 * @param client client to transmit the message to
1532 * @param msg the message to send
1533 * @param may_drop can this message be dropped if the
1534 * message queue for this client is getting far too large?
1537 transmit_to_client (struct TransportClient *client,
1538 const struct GNUNET_MessageHeader *msg, int may_drop)
1540 struct ClientMessageQueueEntry *q;
1543 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1545 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1547 ("Dropping message of type %u and size %u, have %u messages pending (%u is the soft limit)\n"),
1550 client->message_count,
1552 GNUNET_STATISTICS_update (stats,
1553 gettext_noop ("# messages dropped due to slow client"),
1558 msize = ntohs (msg->size);
1559 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1560 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1561 memcpy (&q[1], msg, msize);
1562 GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1563 client->message_queue_tail,
1564 client->message_queue_tail,
1566 client->message_count++;
1567 if (client->th == NULL)
1569 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1571 GNUNET_TIME_UNIT_FOREVER_REL,
1572 &transmit_to_client_callback,
1574 GNUNET_assert (client->th != NULL);
1580 * Transmit a 'SEND_OK' notification to the given client for the
1583 * @param client who to notify
1584 * @param n neighbour to notify about, can be NULL (on failure)
1585 * @param target target of the transmission
1586 * @param result status code for the transmission request
1589 transmit_send_ok (struct TransportClient *client,
1590 struct NeighbourList *n,
1591 const struct GNUNET_PeerIdentity *target,
1594 struct SendOkMessage send_ok_msg;
1596 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1597 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1598 send_ok_msg.success = htonl (result);
1600 send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1602 send_ok_msg.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
1603 send_ok_msg.peer = *target;
1604 transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1609 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1610 * upon "completion" of a send request. This tells the API
1611 * that it is now legal to send another message to the given
1614 * @param cls closure, identifies the entry on the
1615 * message queue that was transmitted and the
1616 * client responsible for queuing the message
1617 * @param target the peer receiving the message
1618 * @param result GNUNET_OK on success, if the transmission
1619 * failed, we should not tell the client to transmit
1623 transmit_send_continuation (void *cls,
1624 const struct GNUNET_PeerIdentity *target,
1627 struct MessageQueue *mq = cls;
1628 struct NeighbourList *n;
1630 GNUNET_STATISTICS_update (stats,
1631 gettext_noop ("# bytes pending with plugins"),
1632 - (int64_t) mq->message_buf_size,
1634 if (result == GNUNET_OK)
1636 GNUNET_STATISTICS_update (stats,
1637 gettext_noop ("# bytes successfully transmitted by plugins"),
1638 mq->message_buf_size,
1643 GNUNET_STATISTICS_update (stats,
1644 gettext_noop ("# bytes with transmission failure by plugins"),
1645 mq->message_buf_size,
1648 if (mq->specific_address != NULL)
1650 if (result == GNUNET_OK)
1652 mq->specific_address->timeout =
1653 GNUNET_TIME_relative_to_absolute
1654 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1655 if (mq->specific_address->validated == GNUNET_YES)
1656 mark_address_connected (mq->specific_address);
1660 if (mq->specific_address->connected != GNUNET_NO)
1663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1664 "Marking address `%s' as no longer connected (due to transmission problem)\n",
1665 a2s (mq->specific_address->ready_list->plugin->short_name,
1666 mq->specific_address->addr,
1667 mq->specific_address->addrlen));
1669 GNUNET_STATISTICS_update (stats,
1670 gettext_noop ("# connected addresses"),
1673 mq->specific_address->connected = GNUNET_NO;
1676 if (! mq->internal_msg)
1677 mq->specific_address->in_transmit = GNUNET_NO;
1679 n = find_neighbour(&mq->neighbour_id);
1680 if (mq->client != NULL)
1681 transmit_send_ok (mq->client, n, target, result);
1684 try_transmission_to_peer (n);
1689 * Find an address in any of the available transports for
1690 * the given neighbour that would be good for message
1691 * transmission. This is essentially the transport selection
1694 * @param neighbour for whom to select an address
1695 * @return selected address, NULL if we have none
1697 struct ForeignAddressList *
1698 find_ready_address(struct NeighbourList *neighbour)
1700 struct ReadyList *head = neighbour->plugins;
1701 struct ForeignAddressList *addresses;
1702 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1703 struct ForeignAddressList *best_address;
1705 /* Hack to prefer unix domain sockets */
1706 struct ForeignAddressList *unix_address = NULL;
1708 best_address = NULL;
1709 while (head != NULL)
1711 addresses = head->addresses;
1712 while (addresses != NULL)
1714 if ( (addresses->timeout.abs_value < now.abs_value) &&
1715 (addresses->connected == GNUNET_YES) )
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1719 "Marking long-time inactive connection to `%4s' as down.\n",
1720 GNUNET_i2s (&neighbour->id));
1722 GNUNET_STATISTICS_update (stats,
1723 gettext_noop ("# connected addresses"),
1726 addresses->connected = GNUNET_NO;
1728 addresses = addresses->next;
1731 addresses = head->addresses;
1732 while (addresses != NULL)
1734 #if DEBUG_TRANSPORT > 1
1735 if (addresses->addr != NULL)
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1737 "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
1738 a2s (head->plugin->short_name,
1740 addresses->addrlen),
1741 GNUNET_i2s (&neighbour->id),
1742 addresses->connected,
1743 addresses->in_transmit,
1744 addresses->validated,
1745 addresses->connect_attempts,
1746 (unsigned long long) addresses->timeout.abs_value,
1747 (unsigned int) addresses->distance);
1749 if (0==strcmp(head->plugin->short_name,"unix"))
1751 if ((unix_address == NULL) || ((unix_address != NULL) &&
1752 (addresses->latency.rel_value < unix_address->latency.rel_value)))
1753 unix_address = addresses;
1755 if ( ( (best_address == NULL) ||
1756 (addresses->connected == GNUNET_YES) ||
1757 (best_address->connected == GNUNET_NO) ) &&
1758 (addresses->in_transmit == GNUNET_NO) &&
1759 ( (best_address == NULL) ||
1760 (addresses->latency.rel_value < best_address->latency.rel_value)) )
1761 best_address = addresses;
1762 /* FIXME: also give lower-latency addresses that are not
1763 connected a chance some times... */
1764 addresses = addresses->next;
1766 if (unix_address != NULL)
1770 if (unix_address != NULL)
1772 best_address = unix_address;
1774 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found unix address, forced this address\n");
1777 if (best_address != NULL)
1781 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1782 "Best address found (`%s') has latency of %llu ms.\n",
1783 (best_address->addrlen > 0)
1784 ? a2s (best_address->ready_list->plugin->short_name,
1786 best_address->addrlen)
1788 best_address->latency.rel_value);
1793 GNUNET_STATISTICS_update (stats,
1794 gettext_noop ("# transmission attempts failed (no address)"),
1799 return best_address;
1805 * We should re-try transmitting to the given peer,
1806 * hopefully we've learned something in the meantime.
1809 retry_transmission_task (void *cls,
1810 const struct GNUNET_SCHEDULER_TaskContext *tc)
1812 struct NeighbourList *n = cls;
1814 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1815 try_transmission_to_peer (n);
1820 * Check the ready list for the given neighbour and if a plugin is
1821 * ready for transmission (and if we have a message), do so!
1823 * @param neighbour target peer for which to transmit
1826 try_transmission_to_peer (struct NeighbourList *n)
1828 struct ReadyList *rl;
1829 struct MessageQueue *mq;
1830 struct GNUNET_TIME_Relative timeout;
1834 if (n->messages_head == NULL)
1837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1838 "Transmission queue for `%4s' is empty\n",
1839 GNUNET_i2s (&n->id));
1841 return; /* nothing to do */
1844 mq = n->messages_head;
1845 force_address = GNUNET_YES;
1846 if (mq->specific_address == NULL)
1849 mq->specific_address = ats_get_preferred_address(n);
1850 GNUNET_STATISTICS_update (stats,
1851 gettext_noop ("# transport selected peer address freely"),
1854 force_address = GNUNET_NO;
1856 if (mq->specific_address == NULL)
1858 GNUNET_STATISTICS_update (stats,
1859 gettext_noop ("# transport failed to selected peer address"),
1862 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1863 if (timeout.rel_value == 0)
1866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1867 "No destination address available to transmit message of size %u to peer `%4s'\n",
1868 mq->message_buf_size,
1869 GNUNET_i2s (&mq->neighbour_id));
1871 GNUNET_STATISTICS_update (stats,
1872 gettext_noop ("# bytes in message queue for other peers"),
1873 - (int64_t) mq->message_buf_size,
1875 GNUNET_STATISTICS_update (stats,
1876 gettext_noop ("# bytes discarded (no destination address available)"),
1877 mq->message_buf_size,
1879 if (mq->client != NULL)
1880 transmit_send_ok (mq->client, n, &n->id, GNUNET_NO);
1881 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1885 return; /* nobody ready */
1887 GNUNET_STATISTICS_update (stats,
1888 gettext_noop ("# message delivery deferred (no address)"),
1891 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
1892 GNUNET_SCHEDULER_cancel (n->retry_task);
1893 n->retry_task = GNUNET_SCHEDULER_add_delayed (timeout,
1894 &retry_transmission_task,
1897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1898 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
1899 mq->message_buf_size,
1900 GNUNET_i2s (&mq->neighbour_id),
1903 /* FIXME: might want to trigger peerinfo lookup here
1904 (unless that's already pending...) */
1907 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1910 if (mq->specific_address->connected == GNUNET_NO)
1911 mq->specific_address->connect_attempts++;
1912 rl = mq->specific_address->ready_list;
1913 mq->plugin = rl->plugin;
1914 if (!mq->internal_msg)
1915 mq->specific_address->in_transmit = GNUNET_YES;
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1918 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
1919 mq->message_buf_size,
1920 GNUNET_i2s (&n->id),
1921 (mq->specific_address->addr != NULL)
1922 ? a2s (mq->plugin->short_name,
1923 mq->specific_address->addr,
1924 mq->specific_address->addrlen)
1926 rl->plugin->short_name);
1928 GNUNET_STATISTICS_update (stats,
1929 gettext_noop ("# bytes in message queue for other peers"),
1930 - (int64_t) mq->message_buf_size,
1932 GNUNET_STATISTICS_update (stats,
1933 gettext_noop ("# bytes pending with plugins"),
1934 mq->message_buf_size,
1936 ret = rl->plugin->api->send (rl->plugin->api->cls,
1939 mq->message_buf_size,
1941 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1942 mq->specific_address->session,
1943 mq->specific_address->addr,
1944 mq->specific_address->addrlen,
1946 &transmit_send_continuation, mq);
1949 /* failure, but 'send' would not call continuation in this case,
1950 so we need to do it here! */
1951 transmit_send_continuation (mq,
1959 * Send the specified message to the specified peer.
1961 * @param client source of the transmission request (can be NULL)
1962 * @param peer_address ForeignAddressList where we should send this message
1963 * @param priority how important is the message
1964 * @param timeout how long do we have to transmit?
1965 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
1966 * @param message_buf_size total size of all messages in message_buf
1967 * @param is_internal is this an internal message; these are pre-pended and
1968 * also do not count for plugins being "ready" to transmit
1969 * @param neighbour handle to the neighbour for transmission
1972 transmit_to_peer (struct TransportClient *client,
1973 struct ForeignAddressList *peer_address,
1974 unsigned int priority,
1975 struct GNUNET_TIME_Relative timeout,
1976 const char *message_buf,
1977 size_t message_buf_size,
1978 int is_internal, struct NeighbourList *neighbour)
1980 struct MessageQueue *mq;
1985 /* check for duplicate submission */
1986 mq = neighbour->messages_head;
1989 if (mq->client == client)
1991 /* client transmitted to same peer twice
1992 before getting SEND_OK! */
2000 GNUNET_STATISTICS_update (stats,
2001 gettext_noop ("# bytes in message queue for other peers"),
2004 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
2005 mq->specific_address = peer_address;
2006 mq->client = client;
2007 /* FIXME: this memcpy can be up to 7% of our total runtime! */
2008 memcpy (&mq[1], message_buf, message_buf_size);
2009 mq->message_buf = (const char*) &mq[1];
2010 mq->message_buf_size = message_buf_size;
2011 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
2012 mq->internal_msg = is_internal;
2013 mq->priority = priority;
2014 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
2016 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
2017 neighbour->messages_tail,
2020 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
2021 neighbour->messages_tail,
2022 neighbour->messages_tail,
2024 try_transmission_to_peer (neighbour);
2031 struct GeneratorContext
2033 struct TransportPlugin *plug_pos;
2034 struct OwnAddressList *addr_pos;
2035 struct GNUNET_TIME_Absolute expiration;
2043 address_generator (void *cls, size_t max, void *buf)
2045 struct GeneratorContext *gc = cls;
2048 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
2050 gc->plug_pos = gc->plug_pos->next;
2051 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
2053 if (NULL == gc->plug_pos)
2058 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
2061 gc->addr_pos->addrlen, buf, max);
2062 gc->addr_pos = gc->addr_pos->next;
2068 * Construct our HELLO message from all of the addresses of
2069 * all of the transports.
2074 struct GNUNET_HELLO_Message *hello;
2075 struct TransportClient *cpos;
2076 struct NeighbourList *npos;
2077 struct GeneratorContext gc;
2079 gc.plug_pos = plugins;
2080 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
2081 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2082 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
2084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2085 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
2087 GNUNET_STATISTICS_update (stats,
2088 gettext_noop ("# refreshed my HELLO"),
2092 while (cpos != NULL)
2094 transmit_to_client (cpos,
2095 (const struct GNUNET_MessageHeader *) hello,
2100 GNUNET_free_non_null (our_hello);
2102 GNUNET_PEERINFO_add_peer (peerinfo, our_hello);
2104 while (npos != NULL)
2107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2108 "Transmitting updated `%s' to neighbour `%4s'\n",
2109 "HELLO", GNUNET_i2s (&npos->id));
2111 GNUNET_STATISTICS_update (stats,
2112 gettext_noop ("# transmitted my HELLO to other peers"),
2115 transmit_to_peer (NULL, NULL, 0,
2116 HELLO_ADDRESS_EXPIRATION,
2117 (const char *) our_hello,
2118 GNUNET_HELLO_size(our_hello),
2126 * Task used to clean up expired addresses for a plugin.
2128 * @param cls closure
2132 expire_address_task (void *cls,
2133 const struct GNUNET_SCHEDULER_TaskContext *tc);
2137 * Update the list of addresses for this plugin,
2138 * expiring those that are past their expiration date.
2140 * @param plugin addresses of which plugin should be recomputed?
2141 * @param fresh set to GNUNET_YES if a new address was added
2142 * and we need to regenerate the HELLO even if nobody
2146 update_addresses (struct TransportPlugin *plugin,
2149 static struct GNUNET_TIME_Absolute last_update;
2150 struct GNUNET_TIME_Relative min_remaining;
2151 struct GNUNET_TIME_Relative remaining;
2152 struct GNUNET_TIME_Absolute now;
2153 struct OwnAddressList *pos;
2154 struct OwnAddressList *prev;
2155 struct OwnAddressList *next;
2158 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_TASK)
2159 GNUNET_SCHEDULER_cancel (plugin->address_update_task);
2160 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
2161 now = GNUNET_TIME_absolute_get ();
2162 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
2163 expired = (GNUNET_TIME_absolute_get_duration (last_update).rel_value > (HELLO_ADDRESS_EXPIRATION.rel_value / 4));
2165 pos = plugin->addresses;
2169 if (pos->expires.abs_value < now.abs_value)
2171 expired = GNUNET_YES;
2173 plugin->addresses = pos->next;
2175 prev->next = pos->next;
2180 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
2181 if (remaining.rel_value < min_remaining.rel_value)
2182 min_remaining = remaining;
2188 if (expired || fresh)
2193 min_remaining = GNUNET_TIME_relative_min (min_remaining,
2194 GNUNET_TIME_relative_divide (HELLO_ADDRESS_EXPIRATION,
2196 plugin->address_update_task
2197 = GNUNET_SCHEDULER_add_delayed (min_remaining,
2198 &expire_address_task, plugin);
2203 * Task used to clean up expired addresses for a plugin.
2205 * @param cls closure
2209 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2211 struct TransportPlugin *plugin = cls;
2213 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
2214 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2215 update_addresses (plugin, GNUNET_NO);
2220 * Iterator over hash map entries that NULLs the session of validation
2221 * entries that match the given session.
2223 * @param cls closure (the 'struct Session*' to match against)
2224 * @param key current key code (peer ID, not used)
2225 * @param value value in the hash map ('struct ValidationEntry*')
2226 * @return GNUNET_YES (we should continue to iterate)
2229 remove_session_validations (void *cls,
2230 const GNUNET_HashCode * key,
2233 struct Session *session = cls;
2234 struct ValidationEntry *ve = value;
2236 if (session == ve->session)
2243 * We've been disconnected from the other peer (for some
2244 * connection-oriented transport). Either quickly
2245 * re-establish the connection or signal the disconnect
2248 * Only signal CORE level disconnect if ALL addresses
2249 * for the peer are exhausted.
2251 * @param p overall plugin context
2252 * @param nl neighbour that was disconnected
2255 try_fast_reconnect (struct TransportPlugin *p,
2256 struct NeighbourList *nl)
2258 /* FIXME-MW: fast reconnect / transport switching not implemented... */
2259 /* Note: the idea here is to hide problems with transports (or
2260 switching between plugins) from the core to eliminate the need to
2261 re-negotiate session keys and the like; OTOH, we should tell core
2262 quickly (much faster than timeout) `if a connection was lost and
2263 could not be re-established (i.e. other peer went down or is
2264 unable / refuses to communicate);
2266 So we should consider:
2267 1) ideally: our own willingness / need to connect
2268 2) prior failures to connect to this peer (by plugin)
2269 3) ideally: reasons why other peer terminated (as far as knowable)
2271 Most importantly, it must be POSSIBLE for another peer to terminate
2272 a connection for a while (without us instantly re-establishing it).
2273 Similarly, if another peer is gone we should quickly notify CORE.
2274 OTOH, if there was a minor glitch (i.e. crash of gnunet-service-transport
2275 on the other end), we should reconnect in such a way that BOTH CORE
2276 services never even notice.
2277 Furthermore, the same mechanism (or small variation) could be used
2278 to switch to a better-performing plugin (ATS).
2280 Finally, this needs to be tested throughly... */
2283 * GNUNET_NO in the call below makes transport disconnect the peer,
2284 * even if only a single address (out of say, six) went away. This
2285 * function must be careful to ONLY disconnect if the peer is gone,
2286 * not just a specifi address.
2288 * More specifically, half the places it was used had it WRONG.
2291 /* No reconnect, signal disconnect instead! */
2292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2293 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2294 "try_fast_reconnect");
2295 disconnect_neighbour (nl, GNUNET_YES);
2300 * Function that will be called whenever the plugin internally
2301 * cleans up a session pointer and hence the service needs to
2302 * discard all of those sessions as well. Plugins that do not
2303 * use sessions can simply omit calling this function and always
2304 * use NULL wherever a session pointer is needed.
2306 * @param cls closure
2307 * @param peer which peer was the session for
2308 * @param session which session is being destoyed
2311 plugin_env_session_end (void *cls,
2312 const struct GNUNET_PeerIdentity *peer,
2313 struct Session *session)
2315 struct TransportPlugin *p = cls;
2316 struct NeighbourList *nl;
2317 struct ReadyList *rl;
2318 struct ForeignAddressList *pos;
2319 struct ForeignAddressList *prev;
2321 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2322 &remove_session_validations,
2324 nl = find_neighbour (peer);
2326 return; /* was never marked as connected */
2330 if (rl->plugin == p)
2335 return; /* was never marked as connected */
2337 pos = rl->addresses;
2338 while ( (pos != NULL) &&
2339 (pos->session != session) )
2345 return; /* was never marked as connected */
2346 pos->session = NULL;
2347 if (pos->addrlen != 0)
2349 if (nl->received_pong != GNUNET_NO)
2350 try_fast_reconnect (p, nl);
2353 /* was inbound connection, free 'pos' */
2355 rl->addresses = pos->next;
2357 prev->next = pos->next;
2358 if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2360 GNUNET_SCHEDULER_cancel (pos->revalidate_task);
2361 pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2364 if (nl->received_pong == GNUNET_NO)
2365 return; /* nothing to do, never connected... */
2366 /* check if we have any validated addresses left */
2367 pos = rl->addresses;
2372 try_fast_reconnect (p, nl);
2377 /* no valid addresses left, signal disconnect! */
2379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2380 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2381 "plugin_env_session_end");
2382 /* FIXME: This doesn't mean there are no addresses left for this PEER,
2383 * it means there aren't any left for this PLUGIN/PEER combination! So
2384 * calling disconnect_neighbor here with GNUNET_NO forces disconnect
2385 * when it isn't necessary. Using GNUNET_YES at least checks to see
2386 * if there are any addresses that work first, so as not to overdo it.
2389 disconnect_neighbour (nl, GNUNET_YES);
2394 * Function that must be called by each plugin to notify the
2395 * transport service about the addresses under which the transport
2396 * provided by the plugin can be reached.
2398 * @param cls closure
2399 * @param name name of the transport that generated the address
2400 * @param addr one of the addresses of the host, NULL for the last address
2401 * the specific address format depends on the transport
2402 * @param addrlen length of the address
2403 * @param expires when should this address automatically expire?
2406 plugin_env_notify_address (void *cls,
2410 struct GNUNET_TIME_Relative expires)
2412 struct TransportPlugin *p = cls;
2413 struct OwnAddressList *al;
2414 struct GNUNET_TIME_Absolute abex;
2416 GNUNET_assert (addr != NULL);
2417 abex = GNUNET_TIME_relative_to_absolute (expires);
2418 GNUNET_assert (p == find_transport (name));
2422 if ( (addrlen == al->addrlen) &&
2423 (0 == memcmp (addr, &al[1], addrlen)) )
2426 update_addresses (p, GNUNET_NO);
2431 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2432 al->next = p->addresses;
2435 al->addrlen = addrlen;
2436 memcpy (&al[1], addr, addrlen);
2437 update_addresses (p, GNUNET_YES);
2442 * Notify all of our clients about a peer connecting.
2445 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2446 struct GNUNET_TIME_Relative latency,
2449 struct ConnectInfoMessage * cim;
2450 struct TransportClient *cpos;
2455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2456 "Notifying clients about connection from `%s'\n",
2459 GNUNET_STATISTICS_update (stats,
2460 gettext_noop ("# peers connected"),
2465 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
2466 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2470 cim = GNUNET_malloc (size);
2472 cim->header.size = htons (size);
2473 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2474 cim->ats_count = htonl(2);
2475 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2476 (&(cim->ats))[0].value = htonl (distance);
2477 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
2478 (&(cim->ats))[1].value = htonl ((uint32_t) latency.rel_value);
2479 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2480 (&(cim->ats))[2].value = htonl (0);
2481 memcpy (&cim->id, peer, sizeof (struct GNUNET_PeerIdentity));
2483 /* notify ats about connecting peer */
2484 ats_notify_peer_connect (peer, &(cim->ats));
2487 while (cpos != NULL)
2489 transmit_to_client (cpos, &(cim->header), GNUNET_NO);
2498 * Notify all of our clients about a peer disconnecting.
2501 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2503 struct DisconnectInfoMessage dim;
2504 struct TransportClient *cpos;
2507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2508 "Notifying clients about lost connection to `%s'\n",
2511 GNUNET_STATISTICS_update (stats,
2512 gettext_noop ("# peers connected"),
2515 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2516 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2517 dim.reserved = htonl (0);
2518 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2520 /* notify ats about connecting peer */
2521 ats_notify_peer_disconnect (peer);
2524 while (cpos != NULL)
2526 transmit_to_client (cpos, &dim.header, GNUNET_NO);
2533 * Find a ForeignAddressList entry for the given neighbour
2534 * that matches the given address and transport.
2536 * @param neighbour which peer we care about
2537 * @param tname name of the transport plugin
2538 * @param session session to look for, NULL for 'any'; otherwise
2539 * can be used for the service to "learn" this session ID
2541 * @param addr binary address
2542 * @param addrlen length of addr
2543 * @return NULL if no such entry exists
2545 static struct ForeignAddressList *
2546 find_peer_address(struct NeighbourList *neighbour,
2548 struct Session *session,
2552 struct ReadyList *head;
2553 struct ForeignAddressList *pos;
2555 head = neighbour->plugins;
2556 while (head != NULL)
2558 if (0 == strcmp (tname, head->plugin->short_name))
2564 pos = head->addresses;
2565 while ( (pos != NULL) &&
2566 ( (pos->addrlen != addrlen) ||
2567 (memcmp(pos->addr, addr, addrlen) != 0) ) )
2569 if ( (session != NULL) &&
2570 (pos->session == session) )
2574 if ( (session != NULL) && (pos != NULL) )
2575 pos->session = session; /* learn it! */
2581 * Get the peer address struct for the given neighbour and
2582 * address. If it doesn't yet exist, create it.
2584 * @param neighbour which peer we care about
2585 * @param tname name of the transport plugin
2586 * @param session session of the plugin, or NULL for none
2587 * @param addr binary address
2588 * @param addrlen length of addr
2589 * @return NULL if we do not have a transport plugin for 'tname'
2591 static struct ForeignAddressList *
2592 add_peer_address (struct NeighbourList *neighbour,
2594 struct Session *session,
2598 struct ReadyList *head;
2599 struct ForeignAddressList *ret;
2602 ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2605 head = neighbour->plugins;
2607 while (head != NULL)
2609 if (0 == strcmp (tname, head->plugin->short_name))
2615 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2616 ret->session = session;
2619 ret->addr = (const char*) &ret[1];
2620 memcpy (&ret[1], addr, addrlen);
2627 ret->ressources = GNUNET_malloc(available_ressources * sizeof (struct ATS_ressource_entry));
2629 for (c=0; c<available_ressources; c++)
2631 struct ATS_ressource_entry *r = ret->ressources;
2633 r[c].atis_index = ressources[c].atis_index;
2634 if (0 == strcmp(neighbour->plugins->plugin->short_name,"unix"))
2636 r[c].c = ressources[c].c_unix;
2639 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"udp"))
2641 r[c].c = ressources[c].c_udp;
2644 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"tcp"))
2646 r[c].c = ressources[c].c_tcp;
2649 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"http"))
2651 r[c].c = ressources[c].c_http;
2654 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"https"))
2656 r[c].c = ressources[c].c_https;
2659 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"wlan"))
2661 r[c].c = ressources[c].c_wlan;
2667 r[c].c = ressources[c].c_default;
2668 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"Assigning default cost to peer `%s' addr plugin `%s'! This should not happen!",
2669 GNUNET_i2s(&neighbour->peer), neighbour->plugins->plugin->short_name);
2673 ret->quality = GNUNET_malloc (available_quality_metrics * sizeof (struct ATS_quality_entry));
2674 ret->addrlen = addrlen;
2675 ret->expires = GNUNET_TIME_relative_to_absolute
2676 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2677 ret->latency = GNUNET_TIME_relative_get_forever();
2679 ret->timeout = GNUNET_TIME_relative_to_absolute
2680 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2681 ret->ready_list = head;
2682 ret->next = head->addresses;
2683 head->addresses = ret;
2689 * Closure for 'add_validated_address'.
2691 struct AddValidatedAddressContext
2694 * Entry that has been validated.
2696 const struct ValidationEntry *ve;
2699 * Flag set after we have added the address so
2700 * that we terminate the iteration next time.
2707 * Callback function used to fill a buffer of max bytes with a list of
2708 * addresses in the format used by HELLOs. Should use
2709 * "GNUNET_HELLO_add_address" as a helper function.
2711 * @param cls the 'struct AddValidatedAddressContext' with the validated address
2712 * @param max maximum number of bytes that can be written to buf
2713 * @param buf where to write the address information
2714 * @return number of bytes written, 0 to signal the
2715 * end of the iteration.
2718 add_validated_address (void *cls,
2719 size_t max, void *buf)
2721 struct AddValidatedAddressContext *avac = cls;
2722 const struct ValidationEntry *ve = avac->ve;
2724 if (GNUNET_YES == avac->done)
2726 avac->done = GNUNET_YES;
2727 return GNUNET_HELLO_add_address (ve->transport_name,
2728 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2738 * Closure for 'check_address_exists'.
2740 struct CheckAddressExistsClosure
2743 * Address to check for.
2748 * Name of the transport.
2755 struct Session *session;
2758 * Set to GNUNET_YES if the address exists.
2771 * Iterator over hash map entries. Checks if the given
2772 * validation entry is for the same address as what is given
2775 * @param cls the 'struct CheckAddressExistsClosure*'
2776 * @param key current key code (ignored)
2777 * @param value value in the hash map ('struct ValidationEntry')
2778 * @return GNUNET_YES if we should continue to
2779 * iterate (mismatch), GNUNET_NO if not (entry matched)
2782 check_address_exists (void *cls,
2783 const GNUNET_HashCode * key,
2786 struct CheckAddressExistsClosure *caec = cls;
2787 struct ValidationEntry *ve = value;
2789 if ( (0 == strcmp (caec->tname,
2790 ve->transport_name)) &&
2791 (caec->addrlen == ve->addrlen) &&
2792 (0 == memcmp (caec->addr,
2796 caec->exists = GNUNET_YES;
2799 if ( (ve->session != NULL) &&
2800 (caec->session == ve->session) )
2802 caec->exists = GNUNET_YES;
2811 * Iterator to free entries in the validation_map.
2813 * @param cls closure (unused)
2814 * @param key current key code
2815 * @param value value in the hash map (validation to abort)
2816 * @return GNUNET_YES (always)
2819 abort_validation (void *cls,
2820 const GNUNET_HashCode * key,
2823 struct ValidationEntry *va = value;
2825 if (GNUNET_SCHEDULER_NO_TASK != va->timeout_task)
2826 GNUNET_SCHEDULER_cancel (va->timeout_task);
2827 GNUNET_free (va->transport_name);
2828 if (va->chvc != NULL)
2830 va->chvc->ve_count--;
2831 if (va->chvc->ve_count == 0)
2833 GNUNET_CONTAINER_DLL_remove (chvc_head,
2836 GNUNET_free (va->chvc);
2846 * HELLO validation cleanup task (validation failed).
2848 * @param cls the 'struct ValidationEntry' that failed
2849 * @param tc scheduler context (unused)
2852 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2854 struct ValidationEntry *va = cls;
2855 struct GNUNET_PeerIdentity pid;
2857 va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2858 GNUNET_STATISTICS_update (stats,
2859 gettext_noop ("# address validation timeouts"),
2862 GNUNET_CRYPTO_hash (&va->publicKey,
2864 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2866 GNUNET_break (GNUNET_OK ==
2867 GNUNET_CONTAINER_multihashmap_remove (validation_map,
2870 abort_validation (NULL, NULL, va);
2875 neighbour_timeout_task (void *cls,
2876 const struct GNUNET_SCHEDULER_TaskContext *tc)
2878 struct NeighbourList *n = cls;
2881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2882 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
2884 GNUNET_STATISTICS_update (stats,
2885 gettext_noop ("# disconnects due to timeout"),
2888 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2889 disconnect_neighbour (n, GNUNET_NO);
2894 * Schedule the job that will cause us to send a PING to the
2895 * foreign address to evaluate its validity and latency.
2897 * @param fal address to PING
2900 schedule_next_ping (struct ForeignAddressList *fal);
2904 * Add the given address to the list of foreign addresses
2905 * available for the given peer (check for duplicates).
2907 * @param cls the respective 'struct NeighbourList' to update
2908 * @param tname name of the transport
2909 * @param expiration expiration time
2910 * @param addr the address
2911 * @param addrlen length of the address
2912 * @return GNUNET_OK (always)
2915 add_to_foreign_address_list (void *cls,
2917 struct GNUNET_TIME_Absolute expiration,
2921 struct NeighbourList *n = cls;
2922 struct ForeignAddressList *fal;
2925 GNUNET_STATISTICS_update (stats,
2926 gettext_noop ("# valid peer addresses returned by PEERINFO"),
2930 fal = find_peer_address (n, tname, NULL, addr, addrlen);
2933 #if DEBUG_TRANSPORT_HELLO
2934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2935 "Adding address `%s' (%s) for peer `%4s' due to PEERINFO data for %llums.\n",
2936 a2s (tname, addr, addrlen),
2938 GNUNET_i2s (&n->id),
2939 expiration.abs_value);
2941 fal = add_peer_address (n, tname, NULL, addr, addrlen);
2944 GNUNET_STATISTICS_update (stats,
2945 gettext_noop ("# previously validated addresses lacking transport"),
2951 fal->expires = GNUNET_TIME_absolute_max (expiration,
2953 schedule_next_ping (fal);
2959 fal->expires = GNUNET_TIME_absolute_max (expiration,
2964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2965 "Failed to add new address for `%4s'\n",
2966 GNUNET_i2s (&n->id));
2969 if (fal->validated == GNUNET_NO)
2971 fal->validated = GNUNET_YES;
2972 GNUNET_STATISTICS_update (stats,
2973 gettext_noop ("# peer addresses considered valid"),
2977 if (try == GNUNET_YES)
2979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2980 "Have new addresses, will try to trigger transmissions.\n");
2981 try_transmission_to_peer (n);
2988 * Add addresses in validated HELLO "h" to the set of addresses
2989 * we have for this peer.
2991 * @param cls closure ('struct NeighbourList*')
2992 * @param peer id of the peer, NULL for last call
2993 * @param h hello message for the peer (can be NULL)
2994 * @param err_msg NULL if successful, otherwise contains error message
2997 add_hello_for_peer (void *cls,
2998 const struct GNUNET_PeerIdentity *peer,
2999 const struct GNUNET_HELLO_Message *h,
3000 const char *err_msg)
3002 struct NeighbourList *n = cls;
3004 if (err_msg != NULL)
3006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3007 _("Error in communication with PEERINFO service\n"));
3012 GNUNET_STATISTICS_update (stats,
3013 gettext_noop ("# outstanding peerinfo iterate requests"),
3020 return; /* no HELLO available */
3022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3023 "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
3027 if (GNUNET_YES != n->public_key_valid)
3029 GNUNET_HELLO_get_key (h, &n->publicKey);
3030 n->public_key_valid = GNUNET_YES;
3032 GNUNET_HELLO_iterate_addresses (h,
3034 &add_to_foreign_address_list,
3040 * Create a fresh entry in our neighbour list for the given peer.
3041 * Will try to transmit our current HELLO to the new neighbour.
3042 * Do not call this function directly, use 'setup_peer_check_blacklist.
3044 * @param peer the peer for which we create the entry
3045 * @param do_hello should we schedule transmitting a HELLO
3046 * @return the new neighbour list entry
3048 static struct NeighbourList *
3049 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
3052 struct NeighbourList *n;
3053 struct TransportPlugin *tp;
3054 struct ReadyList *rl;
3057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3058 "Setting up state for neighbour `%4s'\n",
3061 GNUNET_assert (our_hello != NULL);
3062 GNUNET_STATISTICS_update (stats,
3063 gettext_noop ("# active neighbours"),
3066 n = GNUNET_malloc (sizeof (struct NeighbourList));
3067 n->next = neighbours;
3071 GNUNET_TIME_relative_to_absolute
3072 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3073 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
3074 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3075 MAX_BANDWIDTH_CARRY_S);
3079 if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
3081 rl = GNUNET_malloc (sizeof (struct ReadyList));
3083 rl->next = n->plugins;
3086 rl->addresses = NULL;
3090 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
3092 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
3093 &neighbour_timeout_task, n);
3096 GNUNET_STATISTICS_update (stats,
3097 gettext_noop ("# peerinfo new neighbor iterate requests"),
3100 GNUNET_STATISTICS_update (stats,
3101 gettext_noop ("# outstanding peerinfo iterate requests"),
3104 n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
3105 GNUNET_TIME_UNIT_FOREVER_REL,
3106 &add_hello_for_peer, n);
3108 GNUNET_STATISTICS_update (stats,
3109 gettext_noop ("# HELLO's sent to new neighbors"),
3112 transmit_to_peer (NULL, NULL, 0,
3113 HELLO_ADDRESS_EXPIRATION,
3114 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
3122 * Function called after we have checked if communicating
3123 * with a given peer is acceptable.
3125 * @param cls closure
3126 * @param n NULL if communication is not acceptable
3128 typedef void (*SetupContinuation)(void *cls,
3129 struct NeighbourList *n);
3133 * Information kept for each client registered to perform
3139 * This is a linked list.
3141 struct Blacklisters *next;
3144 * This is a linked list.
3146 struct Blacklisters *prev;
3149 * Client responsible for this entry.
3151 struct GNUNET_SERVER_Client *client;
3154 * Blacklist check that we're currently performing.
3156 struct BlacklistCheck *bc;
3162 * Head of DLL of blacklisting clients.
3164 static struct Blacklisters *bl_head;
3167 * Tail of DLL of blacklisting clients.
3169 static struct Blacklisters *bl_tail;
3173 * Context we use when performing a blacklist check.
3175 struct BlacklistCheck
3179 * This is a linked list.
3181 struct BlacklistCheck *next;
3184 * This is a linked list.
3186 struct BlacklistCheck *prev;
3189 * Peer being checked.
3191 struct GNUNET_PeerIdentity peer;
3194 * Option for setup neighbour afterwards.
3199 * Continuation to call with the result.
3201 SetupContinuation cont;
3209 * Current transmission request handle for this client, or NULL if no
3210 * request is pending.
3212 struct GNUNET_CONNECTION_TransmitHandle *th;
3215 * Our current position in the blacklisters list.
3217 struct Blacklisters *bl_pos;
3220 * Current task performing the check.
3222 GNUNET_SCHEDULER_TaskIdentifier task;
3227 * Head of DLL of active blacklisting queries.
3229 static struct BlacklistCheck *bc_head;
3232 * Tail of DLL of active blacklisting queries.
3234 static struct BlacklistCheck *bc_tail;
3238 * Perform next action in the blacklist check.
3240 * @param cls the 'struct BlacklistCheck*'
3244 do_blacklist_check (void *cls,
3245 const struct GNUNET_SCHEDULER_TaskContext *tc);
3248 * Transmit blacklist query to the client.
3250 * @param cls the 'struct BlacklistCheck'
3251 * @param size number of bytes allowed
3252 * @param buf where to copy the message
3253 * @return number of bytes copied to buf
3256 transmit_blacklist_message (void *cls,
3260 struct BlacklistCheck *bc = cls;
3261 struct Blacklisters *bl;
3262 struct BlacklistMessage bm;
3267 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
3268 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3273 bm.header.size = htons (sizeof (struct BlacklistMessage));
3274 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
3275 bm.is_allowed = htonl (0);
3277 memcpy (buf, &bm, sizeof (bm));
3278 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
3284 * Perform next action in the blacklist check.
3286 * @param cls the 'struct BlacklistCheck*'
3290 do_blacklist_check (void *cls,
3291 const struct GNUNET_SCHEDULER_TaskContext *tc)
3293 struct BlacklistCheck *bc = cls;
3294 struct Blacklisters *bl;
3296 bc->task = GNUNET_SCHEDULER_NO_TASK;
3300 bc->cont (bc->cont_cls,
3301 setup_new_neighbour (&bc->peer, bc->do_hello));
3308 bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
3309 sizeof (struct BlacklistMessage),
3310 GNUNET_TIME_UNIT_FOREVER_REL,
3311 &transmit_blacklist_message,
3318 * Obtain a 'struct NeighbourList' for the given peer. If such an entry
3319 * does not yet exist, check the blacklist. If the blacklist says creating
3320 * one is acceptable, create one and call the continuation; otherwise
3321 * call the continuation with NULL.
3323 * @param peer peer to setup or look up a struct NeighbourList for
3324 * @param do_hello should we also schedule sending our HELLO to the peer
3325 * if this is a new record
3326 * @param cont function to call with the 'struct NeigbhbourList*'
3327 * @param cont_cls closure for cont
3330 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
3332 SetupContinuation cont,
3335 struct NeighbourList *n;
3336 struct BlacklistCheck *bc;
3338 n = find_neighbour(peer);
3345 if (bl_head == NULL)
3348 cont (cont_cls, setup_new_neighbour (peer, do_hello));
3350 setup_new_neighbour(peer, do_hello);
3353 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3354 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3356 bc->do_hello = do_hello;
3358 bc->cont_cls = cont_cls;
3359 bc->bl_pos = bl_head;
3360 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3366 * Function called with the result of querying a new blacklister about
3367 * it being allowed (or not) to continue to talk to an existing neighbour.
3369 * @param cls the original 'struct NeighbourList'
3370 * @param n NULL if we need to disconnect
3373 confirm_or_drop_neighbour (void *cls,
3374 struct NeighbourList *n)
3376 struct NeighbourList * orig = cls;
3380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3381 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&orig->id),
3382 "confirm_or_drop_neighboUr");
3383 disconnect_neighbour (orig, GNUNET_NO);
3389 * Handle a request to start a blacklist.
3391 * @param cls closure (always NULL)
3392 * @param client identification of the client
3393 * @param message the actual message
3396 handle_blacklist_init (void *cls,
3397 struct GNUNET_SERVER_Client *client,
3398 const struct GNUNET_MessageHeader *message)
3400 struct Blacklisters *bl;
3401 struct BlacklistCheck *bc;
3402 struct NeighbourList *n;
3407 if (bl->client == client)
3410 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3415 bl = GNUNET_malloc (sizeof (struct Blacklisters));
3416 bl->client = client;
3417 GNUNET_SERVER_client_keep (client);
3418 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
3419 /* confirm that all existing connections are OK! */
3423 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3424 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3426 bc->do_hello = GNUNET_NO;
3427 bc->cont = &confirm_or_drop_neighbour;
3430 if (n == neighbours) /* all would wait for the same client, no need to
3431 create more than just the first task right now */
3432 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3440 * Handle a request to blacklist a peer.
3442 * @param cls closure (always NULL)
3443 * @param client identification of the client
3444 * @param message the actual message
3447 handle_blacklist_reply (void *cls,
3448 struct GNUNET_SERVER_Client *client,
3449 const struct GNUNET_MessageHeader *message)
3451 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
3452 struct Blacklisters *bl;
3453 struct BlacklistCheck *bc;
3456 while ( (bl != NULL) &&
3457 (bl->client != client) )
3461 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3466 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
3468 bc->cont (bc->cont_cls, NULL);
3469 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
3474 bc->bl_pos = bc->bl_pos->next;
3475 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3478 /* check if any other bc's are waiting for this blacklister */
3482 if ( (bc->bl_pos == bl) &&
3483 (GNUNET_SCHEDULER_NO_TASK == bc->task) )
3484 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3492 * Send periodic PING messages to a given foreign address.
3494 * @param cls our 'struct PeriodicValidationContext*'
3495 * @param tc task context
3498 send_periodic_ping (void *cls,
3499 const struct GNUNET_SCHEDULER_TaskContext *tc)
3501 struct ForeignAddressList *peer_address = cls;
3502 struct TransportPlugin *tp;
3503 struct ValidationEntry *va;
3504 struct NeighbourList *neighbour;
3505 struct TransportPingMessage ping;
3506 struct CheckAddressExistsClosure caec;
3508 uint16_t hello_size;
3512 peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3513 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3515 tp = peer_address->ready_list->plugin;
3516 neighbour = peer_address->ready_list->neighbour;
3517 if (GNUNET_YES != neighbour->public_key_valid)
3519 /* no public key yet, try again later */
3520 schedule_next_ping (peer_address);
3523 caec.addr = peer_address->addr;
3524 caec.addrlen = peer_address->addrlen;
3525 caec.tname = tp->short_name;
3526 caec.session = peer_address->session;
3527 caec.exists = GNUNET_NO;
3528 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3529 &check_address_exists,
3531 if (caec.exists == GNUNET_YES)
3533 /* During validation attempts we will likely trigger the other
3534 peer trying to validate our address which in turn will cause
3535 it to send us its HELLO, so we expect to hit this case rather
3536 frequently. Only print something if we are very verbose. */
3537 #if DEBUG_TRANSPORT > 1
3538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3539 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3540 (peer_address->addr != NULL)
3541 ? a2s (tp->short_name,
3543 peer_address->addrlen)
3546 GNUNET_i2s (&neighbour->id));
3548 schedule_next_ping (peer_address);
3551 va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3552 va->transport_name = GNUNET_strdup (tp->short_name);
3553 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
3555 va->send_time = GNUNET_TIME_absolute_get();
3556 va->session = peer_address->session;
3557 if (peer_address->addr != NULL)
3559 va->addr = (const void*) &va[1];
3560 memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3561 va->addrlen = peer_address->addrlen;
3563 memcpy(&va->publicKey,
3564 &neighbour->publicKey,
3565 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3567 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
3568 &timeout_hello_validation,
3570 GNUNET_CONTAINER_multihashmap_put (validation_map,
3571 &neighbour->id.hashPubKey,
3573 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3575 if (peer_address->validated != GNUNET_YES)
3576 hello_size = GNUNET_HELLO_size(our_hello);
3580 tsize = sizeof(struct TransportPingMessage) + hello_size;
3582 if (peer_address->addr != NULL)
3584 slen = strlen (tp->short_name) + 1;
3585 tsize += slen + peer_address->addrlen;
3589 slen = 0; /* make gcc happy */
3591 message_buf = GNUNET_malloc(tsize);
3592 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3593 ping.challenge = htonl(va->challenge);
3594 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3595 if (peer_address->validated != GNUNET_YES)
3597 memcpy(message_buf, our_hello, hello_size);
3600 if (peer_address->addr != NULL)
3602 ping.header.size = htons(sizeof(struct TransportPingMessage) +
3603 peer_address->addrlen +
3605 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3608 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3610 peer_address->addrlen);
3614 ping.header.size = htons(sizeof(struct TransportPingMessage));
3617 memcpy(&message_buf[hello_size],
3619 sizeof(struct TransportPingMessage));
3621 #if DEBUG_TRANSPORT_REVALIDATION
3622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3623 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s'\n",
3624 (peer_address->addr != NULL)
3625 ? a2s (peer_address->plugin->short_name,
3627 peer_address->addrlen)
3630 GNUNET_i2s (&neighbour->id),
3631 "HELLO", hello_size,
3634 if (peer_address->validated != GNUNET_YES)
3635 GNUNET_STATISTICS_update (stats,
3636 gettext_noop ("# PING with HELLO messages sent"),
3640 GNUNET_STATISTICS_update (stats,
3641 gettext_noop ("# PING without HELLO messages sent"),
3644 GNUNET_STATISTICS_update (stats,
3645 gettext_noop ("# PING messages sent for re-validation"),
3648 transmit_to_peer (NULL, peer_address,
3649 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3650 HELLO_VERIFICATION_TIMEOUT,
3652 GNUNET_YES, neighbour);
3653 GNUNET_free(message_buf);
3654 schedule_next_ping (peer_address);
3659 * Schedule the job that will cause us to send a PING to the
3660 * foreign address to evaluate its validity and latency.
3662 * @param fal address to PING
3665 schedule_next_ping (struct ForeignAddressList *fal)
3667 struct GNUNET_TIME_Relative delay;
3669 if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3671 delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3672 delay.rel_value /= 2; /* do before expiration */
3673 delay = GNUNET_TIME_relative_min (delay,
3674 LATENCY_EVALUATION_MAX_DELAY);
3675 if (GNUNET_YES != fal->estimated)
3677 delay = GNUNET_TIME_UNIT_ZERO;
3678 fal->estimated = GNUNET_YES;
3680 if (GNUNET_YES == fal->connected)
3682 delay = GNUNET_TIME_relative_min (delay,
3683 CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3685 /* FIXME: also adjust delay based on how close the last
3686 observed latency is to the latency of the best alternative */
3687 /* bound how fast we can go */
3688 delay = GNUNET_TIME_relative_max (delay,
3689 GNUNET_TIME_UNIT_SECONDS);
3690 /* randomize a bit (to avoid doing all at the same time) */
3691 delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3692 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(delay,
3693 &send_periodic_ping,
3701 * Function that will be called if we receive some payload
3702 * from another peer.
3704 * @param message the payload
3705 * @param n peer who claimed to be the sender
3708 handle_payload_message (const struct GNUNET_MessageHeader *message,
3709 struct NeighbourList *n)
3711 struct InboundMessage *im;
3712 struct TransportClient *cpos;
3715 msize = ntohs (message->size);
3716 if (n->received_pong == GNUNET_NO)
3718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3719 "Received message of type %u and size %u from `%4s', but no pong yet!!\n",
3720 ntohs (message->type),
3721 ntohs (message->size),
3722 GNUNET_i2s (&n->id));
3723 GNUNET_free_non_null (n->pre_connect_message_buffer);
3724 n->pre_connect_message_buffer = GNUNET_malloc (msize);
3725 memcpy (n->pre_connect_message_buffer, message, msize);
3730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3731 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
3732 ntohs (message->type),
3733 ntohs (message->size),
3734 GNUNET_i2s (&n->id));
3736 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3739 n->quota_violation_count++;
3741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3742 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
3743 n->in_tracker.available_bytes_per_s__,
3744 n->quota_violation_count);
3746 /* Discount 32k per violation */
3747 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3752 if (n->quota_violation_count > 0)
3754 /* try to add 32k back */
3755 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3757 n->quota_violation_count--;
3760 GNUNET_STATISTICS_update (stats,
3761 gettext_noop ("# payload received from other peers"),
3764 /* transmit message to all clients */
3765 uint32_t ats_count = 2;
3766 size_t size = sizeof (struct InboundMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) + msize;
3767 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
3770 im = GNUNET_malloc (size);
3771 im->header.size = htons (size);
3772 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3774 im->ats_count = htonl(ats_count);
3775 /* Setting ATS data */
3776 (&(im->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
3777 (&(im->ats))[0].value = htonl (n->distance);
3778 (&(im->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
3779 (&(im->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
3780 (&(im->ats))[ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
3781 (&(im->ats))[ats_count].value = htonl (0);
3783 memcpy (&((&(im->ats))[ats_count+1]), message, msize);
3785 while (cpos != NULL)
3787 transmit_to_client (cpos, &im->header, GNUNET_YES);
3795 * Iterator over hash map entries. Checks if the given validation
3796 * entry is for the same challenge as what is given in the PONG.
3798 * @param cls the 'struct TransportPongMessage*'
3799 * @param key peer identity
3800 * @param value value in the hash map ('struct ValidationEntry')
3801 * @return GNUNET_YES if we should continue to
3802 * iterate (mismatch), GNUNET_NO if not (entry matched)
3805 check_pending_validation (void *cls,
3806 const GNUNET_HashCode * key,
3809 const struct TransportPongMessage *pong = cls;
3810 struct ValidationEntry *ve = value;
3811 struct AddValidatedAddressContext avac;
3812 unsigned int challenge = ntohl(pong->challenge);
3813 struct GNUNET_HELLO_Message *hello;
3814 struct GNUNET_PeerIdentity target;
3815 struct NeighbourList *n;
3816 struct ForeignAddressList *fal;
3817 struct OwnAddressList *oal;
3818 struct TransportPlugin *tp;
3819 struct GNUNET_MessageHeader *prem;
3825 ps = ntohs (pong->header.size);
3826 if (ps < sizeof (struct TransportPongMessage))
3828 GNUNET_break_op (0);
3831 addr = (const char*) &pong[1];
3832 slen = strlen (ve->transport_name) + 1;
3833 if ( (ps - sizeof (struct TransportPongMessage) < slen) ||
3834 (ve->challenge != challenge) ||
3835 (addr[slen-1] != '\0') ||
3836 (0 != strcmp (addr, ve->transport_name)) ||
3837 (ntohl (pong->purpose.size)
3838 != sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3840 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
3841 sizeof (struct GNUNET_PeerIdentity) + ps - sizeof (struct TransportPongMessage)) )
3846 alen = ps - sizeof (struct TransportPongMessage) - slen;
3847 switch (ntohl (pong->purpose.purpose))
3849 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN:
3850 if ( (ve->addrlen + slen != ntohl (pong->addrlen)) ||
3851 (0 != memcmp (&addr[slen],
3855 return GNUNET_YES; /* different entry, keep trying! */
3857 if (0 != memcmp (&pong->pid,
3859 sizeof (struct GNUNET_PeerIdentity)))
3861 GNUNET_break_op (0);
3865 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
3870 GNUNET_break_op (0);
3875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3876 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
3878 a2s (ve->transport_name,
3879 (const struct sockaddr *) ve->addr,
3881 ve->transport_name);
3884 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING:
3885 if (0 != memcmp (&pong->pid,
3887 sizeof (struct GNUNET_PeerIdentity)))
3889 GNUNET_break_op (0);
3892 if (ve->addrlen != 0)
3894 /* must have been for a different validation entry */
3897 tp = find_transport (ve->transport_name);
3903 oal = tp->addresses;
3906 if ( (oal->addrlen == alen) &&
3907 (0 == memcmp (&oal[1],
3915 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3916 _("Not accepting PONG with address `%s' since I cannot confirm having this address.\n"),
3917 a2s (ve->transport_name,
3923 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING,
3928 GNUNET_break_op (0);
3933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3934 "Confirmed that peer `%4s' is talking to us using address `%s' (%s) for us.\n",
3936 a2s (ve->transport_name,
3939 ve->transport_name);
3943 GNUNET_break_op (0);
3946 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
3948 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3949 _("Received expired signature. Check system time.\n"));
3952 GNUNET_STATISTICS_update (stats,
3953 gettext_noop ("# address validation successes"),
3956 /* create the updated HELLO */
3957 GNUNET_CRYPTO_hash (&ve->publicKey,
3958 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3959 &target.hashPubKey);
3960 if (ve->addr != NULL)
3962 avac.done = GNUNET_NO;
3964 hello = GNUNET_HELLO_create (&ve->publicKey,
3965 &add_validated_address,
3967 GNUNET_PEERINFO_add_peer (peerinfo,
3969 GNUNET_free (hello);
3971 n = find_neighbour (&target);
3974 n->publicKey = ve->publicKey;
3975 n->public_key_valid = GNUNET_YES;
3976 fal = add_peer_address (n,
3981 GNUNET_assert (fal != NULL);
3982 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
3983 fal->validated = GNUNET_YES;
3984 mark_address_connected (fal);
3985 GNUNET_STATISTICS_update (stats,
3986 gettext_noop ("# peer addresses considered valid"),
3989 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
3990 update_addr_value (fal, GNUNET_TIME_absolute_get_duration (ve->send_time).rel_value, GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
3992 schedule_next_ping (fal);
3993 if (n->latency.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3994 n->latency = fal->latency;
3996 n->latency.rel_value = (fal->latency.rel_value + n->latency.rel_value) / 2;
3998 n->distance = fal->distance;
3999 if (GNUNET_NO == n->received_pong)
4001 n->received_pong = GNUNET_YES;
4003 notify_clients_connect (&target, n->latency, n->distance);
4004 if (NULL != (prem = n->pre_connect_message_buffer))
4006 n->pre_connect_message_buffer = NULL;
4007 handle_payload_message (prem, n);
4011 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4013 GNUNET_SCHEDULER_cancel (n->retry_task);
4014 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4015 try_transmission_to_peer (n);
4019 /* clean up validation entry */
4020 GNUNET_assert (GNUNET_YES ==
4021 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4024 abort_validation (NULL, NULL, ve);
4030 * Function that will be called if we receive a validation
4031 * of an address challenge that we transmitted to another
4032 * peer. Note that the validation should only be considered
4033 * acceptable if the challenge matches AND if the sender
4034 * address is at least a plausible address for this peer
4035 * (otherwise we may be seeing a MiM attack).
4037 * @param cls closure
4038 * @param message the pong message
4039 * @param peer who responded to our challenge
4040 * @param sender_address string describing our sender address (as observed
4041 * by the other peer in binary format)
4042 * @param sender_address_len number of bytes in 'sender_address'
4045 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
4046 const struct GNUNET_PeerIdentity *peer,
4047 const char *sender_address,
4048 size_t sender_address_len)
4050 #if DEBUG_TRANSPORT > 1
4051 /* we get tons of these that just get discarded, only log
4052 if we are quite verbose */
4053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4054 "Receiving `%s' message from `%4s'.\n", "PONG",
4057 GNUNET_STATISTICS_update (stats,
4058 gettext_noop ("# PONG messages received"),
4061 if (GNUNET_SYSERR !=
4062 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
4064 &check_pending_validation,
4067 /* This is *expected* to happen a lot since we send
4068 PONGs to *all* known addresses of the sender of
4069 the PING, so most likely we get multiple PONGs
4070 per PING, and all but the first PONG will end up
4071 here. So really we should not print anything here
4072 unless we want to be very, very verbose... */
4073 #if DEBUG_TRANSPORT > 2
4074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4075 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
4087 * Try to validate a neighbour's address by sending him our HELLO and a PING.
4089 * @param cls the 'struct ValidationEntry*'
4090 * @param neighbour neighbour to validate, NULL if validation failed
4093 transmit_hello_and_ping (void *cls,
4094 struct NeighbourList *neighbour)
4096 struct ValidationEntry *va = cls;
4097 struct ForeignAddressList *peer_address;
4098 struct TransportPingMessage ping;
4099 uint16_t hello_size;
4102 struct GNUNET_PeerIdentity id;
4105 GNUNET_CRYPTO_hash (&va->publicKey,
4106 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4108 if (neighbour == NULL)
4110 /* FIXME: stats... */
4111 GNUNET_break (GNUNET_OK ==
4112 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4115 abort_validation (NULL, NULL, va);
4118 neighbour->publicKey = va->publicKey;
4119 neighbour->public_key_valid = GNUNET_YES;
4120 peer_address = add_peer_address (neighbour,
4121 va->transport_name, NULL,
4122 (const void*) &va[1],
4124 if (peer_address == NULL)
4126 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4127 "Failed to add peer `%4s' for plugin `%s'\n",
4128 GNUNET_i2s (&neighbour->id),
4129 va->transport_name);
4130 GNUNET_break (GNUNET_OK ==
4131 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4134 abort_validation (NULL, NULL, va);
4137 hello_size = GNUNET_HELLO_size(our_hello);
4138 slen = strlen(va->transport_name) + 1;
4139 tsize = sizeof(struct TransportPingMessage) + hello_size + va->addrlen + slen;
4140 message_buf = GNUNET_malloc(tsize);
4141 ping.challenge = htonl(va->challenge);
4142 ping.header.size = htons(sizeof(struct TransportPingMessage) + slen + va->addrlen);
4143 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
4144 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
4145 memcpy(message_buf, our_hello, hello_size);
4146 memcpy(&message_buf[hello_size],
4148 sizeof(struct TransportPingMessage));
4149 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
4152 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
4156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4157 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
4160 : a2s (va->transport_name,
4161 (const void*) &va[1], va->addrlen),
4163 GNUNET_i2s (&neighbour->id),
4164 "HELLO", hello_size,
4165 "PING", sizeof (struct TransportPingMessage) + va->addrlen + slen);
4168 GNUNET_STATISTICS_update (stats,
4169 gettext_noop ("# PING messages sent for initial validation"),
4172 transmit_to_peer (NULL, peer_address,
4173 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
4174 HELLO_VERIFICATION_TIMEOUT,
4176 GNUNET_YES, neighbour);
4177 GNUNET_free(message_buf);
4182 * Check if the given address is already being validated; if not,
4183 * append the given address to the list of entries that are being be
4184 * validated and initiate validation.
4186 * @param cls closure ('struct CheckHelloValidatedContext *')
4187 * @param tname name of the transport
4188 * @param expiration expiration time
4189 * @param addr the address
4190 * @param addrlen length of the address
4191 * @return GNUNET_OK (always)
4194 run_validation (void *cls,
4196 struct GNUNET_TIME_Absolute expiration,
4200 struct CheckHelloValidatedContext *chvc = cls;
4201 struct GNUNET_PeerIdentity id;
4202 struct TransportPlugin *tp;
4203 struct ValidationEntry *va;
4204 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4205 struct CheckAddressExistsClosure caec;
4206 struct OwnAddressList *oal;
4208 GNUNET_assert (addr != NULL);
4210 GNUNET_STATISTICS_update (stats,
4211 gettext_noop ("# peer addresses scheduled for validation"),
4214 tp = find_transport (tname);
4217 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
4218 GNUNET_ERROR_TYPE_BULK,
4220 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
4222 GNUNET_STATISTICS_update (stats,
4223 gettext_noop ("# peer addresses not validated (plugin not available)"),
4228 /* check if this is one of our own addresses */
4229 oal = tp->addresses;
4232 if ( (oal->addrlen == addrlen) &&
4233 (0 == memcmp (&oal[1],
4237 /* not plausible, this address is equivalent to our own address! */
4238 GNUNET_STATISTICS_update (stats,
4239 gettext_noop ("# peer addresses not validated (loopback)"),
4246 GNUNET_HELLO_get_key (chvc->hello, &pk);
4247 GNUNET_CRYPTO_hash (&pk,
4249 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4252 if (is_blacklisted(&id, tp))
4255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4256 "Attempted to validate blacklisted peer `%s' using `%s'!\n",
4264 caec.addrlen = addrlen;
4265 caec.session = NULL;
4267 caec.exists = GNUNET_NO;
4268 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
4269 &check_address_exists,
4271 if (caec.exists == GNUNET_YES)
4273 /* During validation attempts we will likely trigger the other
4274 peer trying to validate our address which in turn will cause
4275 it to send us its HELLO, so we expect to hit this case rather
4276 frequently. Only print something if we are very verbose. */
4277 #if DEBUG_TRANSPORT > 1
4278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4279 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
4280 a2s (tname, addr, addrlen),
4284 GNUNET_STATISTICS_update (stats,
4285 gettext_noop ("# peer addresses not validated (in progress)"),
4290 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
4293 va->transport_name = GNUNET_strdup (tname);
4294 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
4296 va->send_time = GNUNET_TIME_absolute_get();
4297 va->addr = (const void*) &va[1];
4298 memcpy (&va[1], addr, addrlen);
4299 va->addrlen = addrlen;
4300 GNUNET_HELLO_get_key (chvc->hello,
4302 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
4303 &timeout_hello_validation,
4305 GNUNET_CONTAINER_multihashmap_put (validation_map,
4308 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4309 setup_peer_check_blacklist (&id, GNUNET_NO,
4310 &transmit_hello_and_ping,
4317 * Check if addresses in validated hello "h" overlap with
4318 * those in "chvc->hello" and validate the rest.
4320 * @param cls closure
4321 * @param peer id of the peer, NULL for last call
4322 * @param h hello message for the peer (can be NULL)
4323 * @param err_msg NULL if successful, otherwise contains error message
4326 check_hello_validated (void *cls,
4327 const struct GNUNET_PeerIdentity *peer,
4328 const struct GNUNET_HELLO_Message *h,
4329 const char *err_msg)
4331 struct CheckHelloValidatedContext *chvc = cls;
4332 struct GNUNET_HELLO_Message *plain_hello;
4333 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4334 struct GNUNET_PeerIdentity target;
4335 struct NeighbourList *n;
4337 if (err_msg != NULL)
4339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4340 _("Error in communication with PEERINFO service\n"));
4346 GNUNET_STATISTICS_update (stats,
4347 gettext_noop ("# outstanding peerinfo iterate requests"),
4351 if (GNUNET_NO == chvc->hello_known)
4353 /* notify PEERINFO about the peer now, so that we at least
4354 have the public key if some other component needs it */
4355 GNUNET_HELLO_get_key (chvc->hello, &pk);
4356 GNUNET_CRYPTO_hash (&pk,
4357 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4358 &target.hashPubKey);
4359 plain_hello = GNUNET_HELLO_create (&pk,
4362 GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
4363 GNUNET_free (plain_hello);
4364 #if DEBUG_TRANSPORT_HELLO
4365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4366 "PEERINFO had no `%s' message for peer `%4s', full validation needed.\n",
4368 GNUNET_i2s (&target));
4370 GNUNET_STATISTICS_update (stats,
4371 gettext_noop ("# new HELLOs requiring full validation"),
4374 GNUNET_HELLO_iterate_addresses (chvc->hello,
4381 GNUNET_STATISTICS_update (stats,
4382 gettext_noop ("# duplicate HELLO (peer known)"),
4387 if (chvc->ve_count == 0)
4389 GNUNET_CONTAINER_DLL_remove (chvc_head,
4398 #if DEBUG_TRANSPORT_HELLO
4399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4400 "PEERINFO had `%s' message for peer `%4s', validating only new addresses.\n",
4404 chvc->hello_known = GNUNET_YES;
4405 n = find_neighbour (peer);
4408 #if DEBUG_TRANSPORT_HELLO
4409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4410 "Calling hello_iterate_addresses for %s!\n",
4413 GNUNET_HELLO_iterate_addresses (h,
4415 &add_to_foreign_address_list,
4417 try_transmission_to_peer (n);
4421 #if DEBUG_TRANSPORT_HELLO
4422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4423 "No existing neighbor record for %s!\n",
4426 GNUNET_STATISTICS_update (stats,
4427 gettext_noop ("# no existing neighbour record (validating HELLO)"),
4431 GNUNET_STATISTICS_update (stats,
4432 gettext_noop ("# HELLO validations (update case)"),
4435 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
4437 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
4444 * Process HELLO-message.
4446 * @param plugin transport involved, may be NULL
4447 * @param message the actual message
4448 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
4451 process_hello (struct TransportPlugin *plugin,
4452 const struct GNUNET_MessageHeader *message)
4455 struct GNUNET_PeerIdentity target;
4456 const struct GNUNET_HELLO_Message *hello;
4457 struct CheckHelloValidatedContext *chvc;
4458 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
4459 #if DEBUG_TRANSPORT_HELLO > 2
4462 hsize = ntohs (message->size);
4463 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
4464 (hsize < sizeof (struct GNUNET_MessageHeader)))
4467 return GNUNET_SYSERR;
4469 GNUNET_STATISTICS_update (stats,
4470 gettext_noop ("# HELLOs received for validation"),
4474 /* first, check if load is too high */
4475 if (GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
4477 GNUNET_STATISTICS_update (stats,
4478 gettext_noop ("# HELLOs ignored due to high load"),
4481 #if DEBUG_TRANSPORT_HELLO
4482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4483 "Ignoring `%s' for `%4s', load too high.\n",
4485 GNUNET_i2s (&target));
4489 hello = (const struct GNUNET_HELLO_Message *) message;
4490 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
4492 #if DEBUG_TRANSPORT_HELLO
4493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4494 "Unable to get public key from `%s' for `%4s'!\n",
4496 GNUNET_i2s (&target));
4498 GNUNET_break_op (0);
4499 return GNUNET_SYSERR;
4502 GNUNET_CRYPTO_hash (&publicKey,
4503 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4504 &target.hashPubKey);
4506 #if DEBUG_TRANSPORT_HELLO
4507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4508 "Received `%s' message for `%4s'\n",
4510 GNUNET_i2s (&target));
4513 if (0 == memcmp (&my_identity,
4515 sizeof (struct GNUNET_PeerIdentity)))
4517 GNUNET_STATISTICS_update (stats,
4518 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
4524 while (NULL != chvc)
4526 if (GNUNET_HELLO_equals (hello,
4528 GNUNET_TIME_absolute_get ()).abs_value > 0)
4530 #if DEBUG_TRANSPORT_HELLO > 2
4531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4532 "Received duplicate `%s' message for `%4s'; ignored\n",
4534 GNUNET_i2s (&target));
4536 return GNUNET_OK; /* validation already pending */
4538 if (GNUNET_HELLO_size(hello) == GNUNET_HELLO_size (chvc->hello))
4539 GNUNET_break (0 != memcmp (hello, chvc->hello,
4540 GNUNET_HELLO_size(hello)));
4545 struct NeighbourList *temp_neighbor = find_neighbour(&target);
4546 if ((NULL != temp_neighbor))
4548 fprintf(stderr, "Already know peer, ignoring hello\n");
4553 #if DEBUG_TRANSPORT_HELLO > 2
4556 my_id = GNUNET_strdup(GNUNET_i2s(plugin->env.my_identity));
4557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4558 "%s: Starting validation of `%s' message for `%4s' via '%s' of size %u\n",
4561 GNUNET_i2s (&target),
4563 GNUNET_HELLO_size(hello));
4567 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
4569 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
4570 memcpy (&chvc[1], hello, hsize);
4571 GNUNET_CONTAINER_DLL_insert (chvc_head,
4574 /* finally, check if HELLO was previously validated
4575 (continuation will then schedule actual validation) */
4576 GNUNET_STATISTICS_update (stats,
4577 gettext_noop ("# peerinfo process hello iterate requests"),
4580 GNUNET_STATISTICS_update (stats,
4581 gettext_noop ("# outstanding peerinfo iterate requests"),
4584 chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
4586 HELLO_VERIFICATION_TIMEOUT,
4587 &check_hello_validated, chvc);
4593 * The peer specified by the given neighbour has timed-out or a plugin
4594 * has disconnected. We may either need to do nothing (other plugins
4595 * still up), or trigger a full disconnect and clean up. This
4596 * function updates our state and does the necessary notifications.
4597 * Also notifies our clients that the neighbour is now officially
4600 * @param n the neighbour list entry for the peer
4601 * @param check GNUNET_YES to check if ALL addresses for this peer
4602 * are gone, GNUNET_NO to force a disconnect of the peer
4603 * regardless of whether other addresses exist.
4606 disconnect_neighbour (struct NeighbourList *n, int check)
4608 struct ReadyList *rpos;
4609 struct NeighbourList *npos;
4610 struct NeighbourList *nprev;
4611 struct MessageQueue *mq;
4612 struct ForeignAddressList *peer_addresses;
4613 struct ForeignAddressList *peer_pos;
4615 if (GNUNET_YES == check)
4618 while (NULL != rpos)
4620 peer_addresses = rpos->addresses;
4621 while (peer_addresses != NULL)
4623 if (GNUNET_YES == peer_addresses->connected)
4625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4626 "NOT Disconnecting from `%4s', still have live addresses!\n",
4627 GNUNET_i2s (&n->id));
4628 return; /* still connected */
4630 peer_addresses = peer_addresses->next;
4636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4637 "Disconnecting from `%4s'\n",
4638 GNUNET_i2s (&n->id));
4640 /* remove n from neighbours list */
4643 while ((npos != NULL) && (npos != n))
4648 GNUNET_assert (npos != NULL);
4650 neighbours = n->next;
4652 nprev->next = n->next;
4654 /* notify all clients about disconnect */
4655 if (GNUNET_YES == n->received_pong)
4656 notify_clients_disconnect (&n->id);
4658 /* clean up all plugins, cancel connections and pending transmissions */
4659 while (NULL != (rpos = n->plugins))
4661 n->plugins = rpos->next;
4662 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
4663 while (rpos->addresses != NULL)
4665 peer_pos = rpos->addresses;
4666 rpos->addresses = peer_pos->next;
4667 if (peer_pos->connected == GNUNET_YES)
4668 GNUNET_STATISTICS_update (stats,
4669 gettext_noop ("# connected addresses"),
4672 if (GNUNET_YES == peer_pos->validated)
4673 GNUNET_STATISTICS_update (stats,
4674 gettext_noop ("# peer addresses considered valid"),
4677 if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
4679 GNUNET_SCHEDULER_cancel (peer_pos->revalidate_task);
4680 peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
4682 GNUNET_free(peer_pos->ressources);
4683 GNUNET_free(peer_pos->quality);
4684 GNUNET_free(peer_pos);
4689 /* free all messages on the queue */
4690 while (NULL != (mq = n->messages_head))
4692 GNUNET_STATISTICS_update (stats,
4693 gettext_noop ("# bytes in message queue for other peers"),
4694 - (int64_t) mq->message_buf_size,
4696 GNUNET_STATISTICS_update (stats,
4697 gettext_noop ("# bytes discarded due to disconnect"),
4698 mq->message_buf_size,
4700 GNUNET_CONTAINER_DLL_remove (n->messages_head,
4703 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4705 sizeof(struct GNUNET_PeerIdentity)));
4708 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
4710 GNUNET_SCHEDULER_cancel (n->timeout_task);
4711 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4713 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4715 GNUNET_SCHEDULER_cancel (n->retry_task);
4716 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4718 if (n->piter != NULL)
4720 GNUNET_PEERINFO_iterate_cancel (n->piter);
4721 GNUNET_STATISTICS_update (stats,
4722 gettext_noop ("# outstanding peerinfo iterate requests"),
4727 /* finally, free n itself */
4728 GNUNET_STATISTICS_update (stats,
4729 gettext_noop ("# active neighbours"),
4732 GNUNET_free_non_null (n->pre_connect_message_buffer);
4738 * We have received a PING message from someone. Need to send a PONG message
4739 * in response to the peer by any means necessary.
4742 handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
4743 const struct GNUNET_PeerIdentity *peer,
4744 struct Session *session,
4745 const char *sender_address,
4746 uint16_t sender_address_len)
4748 struct TransportPlugin *plugin = cls;
4749 struct SessionHeader *session_header = (struct SessionHeader*) session;
4750 struct TransportPingMessage *ping;
4751 struct TransportPongMessage *pong;
4752 struct NeighbourList *n;
4753 struct ReadyList *rl;
4754 struct ForeignAddressList *fal;
4755 struct OwnAddressList *oal;
4760 if (ntohs (message->size) < sizeof (struct TransportPingMessage))
4762 GNUNET_break_op (0);
4763 return GNUNET_SYSERR;
4766 ping = (struct TransportPingMessage *) message;
4767 if (0 != memcmp (&ping->target,
4768 plugin->env.my_identity,
4769 sizeof (struct GNUNET_PeerIdentity)))
4771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4772 _("Received `%s' message from `%s' destined for `%s' which is not me!\n"),
4774 (sender_address != NULL)
4775 ? a2s (plugin->short_name,
4776 (const struct sockaddr *)sender_address,
4779 GNUNET_i2s (&ping->target));
4780 return GNUNET_SYSERR;
4783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4784 "Processing `%s' from `%s'\n",
4786 (sender_address != NULL)
4787 ? a2s (plugin->short_name,
4788 (const struct sockaddr *)sender_address,
4792 GNUNET_STATISTICS_update (stats,
4793 gettext_noop ("# PING messages received"),
4796 addr = (const char*) &ping[1];
4797 alen = ntohs (message->size) - sizeof (struct TransportPingMessage);
4798 slen = strlen (plugin->short_name) + 1;
4801 /* peer wants to confirm that we have an outbound connection to him */
4802 if (session == NULL)
4804 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4805 _("Refusing to create PONG since I do not have a session with `%s'.\n"),
4807 return GNUNET_SYSERR;
4809 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len + slen);
4810 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len + slen);
4811 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
4812 pong->purpose.size =
4813 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4815 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
4816 sizeof (struct GNUNET_PeerIdentity) + sender_address_len + slen);
4817 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING);
4818 pong->challenge = ping->challenge;
4819 pong->addrlen = htonl(sender_address_len + slen);
4822 sizeof(struct GNUNET_PeerIdentity));
4826 if ((sender_address!=NULL) && (sender_address_len > 0))
4827 memcpy (&((char*)&pong[1])[slen],
4829 sender_address_len);
4830 if (GNUNET_TIME_absolute_get_remaining (session_header->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
4832 /* create / update cached sig */
4834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4835 "Creating PONG signature to indicate active connection.\n");
4837 session_header->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
4838 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
4839 GNUNET_assert (GNUNET_OK ==
4840 GNUNET_CRYPTO_rsa_sign (my_private_key,
4842 &session_header->pong_signature));
4846 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
4848 memcpy (&pong->signature,
4849 &session_header->pong_signature,
4850 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4856 /* peer wants to confirm that this is one of our addresses */
4860 plugin->api->check_address (plugin->api->cls,
4864 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4865 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
4866 a2s (plugin->short_name,
4871 oal = plugin->addresses;
4874 if ( (oal->addrlen == alen) &&
4881 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
4882 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
4883 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
4884 pong->purpose.size =
4885 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4887 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
4888 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
4889 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
4890 pong->challenge = ping->challenge;
4891 pong->addrlen = htonl(alen + slen);
4894 sizeof(struct GNUNET_PeerIdentity));
4895 memcpy (&pong[1], plugin->short_name, slen);
4896 memcpy (&((char*)&pong[1])[slen], addr, alen);
4897 if ( (oal != NULL) &&
4898 (GNUNET_TIME_absolute_get_remaining (oal->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4) )
4900 /* create / update cached sig */
4902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4903 "Creating PONG signature to indicate ownership.\n");
4905 oal->pong_sig_expires = GNUNET_TIME_absolute_min (oal->expires,
4906 GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
4907 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
4908 GNUNET_assert (GNUNET_OK ==
4909 GNUNET_CRYPTO_rsa_sign (my_private_key,
4911 &oal->pong_signature));
4912 memcpy (&pong->signature,
4913 &oal->pong_signature,
4914 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4916 else if (oal == NULL)
4918 /* not using cache (typically DV-only) */
4919 pong->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
4920 GNUNET_assert (GNUNET_OK ==
4921 GNUNET_CRYPTO_rsa_sign (my_private_key,
4927 /* can used cached version */
4928 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
4929 memcpy (&pong->signature,
4930 &oal->pong_signature,
4931 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4934 n = find_neighbour(peer);
4935 GNUNET_assert (n != NULL);
4936 /* first try reliable response transmission */
4940 fal = rl->addresses;
4943 if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
4946 ntohs (pong->header.size),
4947 TRANSPORT_PONG_PRIORITY,
4948 HELLO_VERIFICATION_TIMEOUT,
4956 GNUNET_STATISTICS_update (stats,
4957 gettext_noop ("# PONGs unicast via reliable transport"),
4967 /* no reliable method found, do multicast */
4968 GNUNET_STATISTICS_update (stats,
4969 gettext_noop ("# PONGs multicast to all available addresses"),
4975 fal = rl->addresses;
4978 transmit_to_peer(NULL, fal,
4979 TRANSPORT_PONG_PRIORITY,
4980 HELLO_VERIFICATION_TIMEOUT,
4982 ntohs(pong->header.size),
4998 * Function called by the plugin for each received message.
4999 * Update data volumes, possibly notify plugins about
5000 * reducing the rate at which they read from the socket
5001 * and generally forward to our receive callback.
5003 * @param cls the "struct TransportPlugin *" we gave to the plugin
5004 * @param peer (claimed) identity of the other peer
5005 * @param message the message, NULL if we only care about
5006 * learning about the delay until we should receive again
5007 * @param ats_data information for automatic transport selection
5008 * @param ats_count number of elements in ats not including 0-terminator
5009 * @param session identifier used for this session (can be NULL)
5010 * @param sender_address binary address of the sender (if observed)
5011 * @param sender_address_len number of bytes in sender_address
5012 * @return how long in ms the plugin should wait until receiving more data
5013 * (plugins that do not support this, can ignore the return value)
5015 static struct GNUNET_TIME_Relative
5016 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
5017 const struct GNUNET_MessageHeader *message,
5018 const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
5020 struct Session *session,
5021 const char *sender_address,
5022 uint16_t sender_address_len)
5024 struct TransportPlugin *plugin = cls;
5025 struct ReadyList *service_context;
5026 struct ForeignAddressList *peer_address;
5028 struct NeighbourList *n;
5029 struct GNUNET_TIME_Relative ret;
5030 if (is_blacklisted (peer, plugin))
5031 return GNUNET_TIME_UNIT_FOREVER_REL;
5035 n = find_neighbour (peer);
5037 n = setup_new_neighbour (peer, GNUNET_YES);
5038 service_context = n->plugins;
5039 while ((service_context != NULL) && (plugin != service_context->plugin))
5040 service_context = service_context->next;
5041 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
5042 peer_address = NULL;
5045 for (c=0; c<ats_count; c++)
5047 if (ntohl(ats_data[c].type) == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
5049 distance = ntohl(ats_data[c].value);
5052 /* notify ATS about incoming data */
5053 //ats_notify_ats_data(peer, ats_data);
5055 if (message != NULL)
5057 if ( (session != NULL) ||
5058 (sender_address != NULL) )
5059 peer_address = add_peer_address (n,
5063 sender_address_len);
5064 if (peer_address != NULL)
5067 update_addr_ats(peer_address, ats_data, ats_count);
5068 update_addr_value(peer_address, distance, GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5070 peer_address->distance = distance;
5071 if (GNUNET_YES == peer_address->validated)
5072 mark_address_connected (peer_address);
5073 peer_address->timeout
5075 GNUNET_TIME_relative_to_absolute
5076 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5077 schedule_next_ping (peer_address);
5079 /* update traffic received amount ... */
5080 msize = ntohs (message->size);
5081 GNUNET_STATISTICS_update (stats,
5082 gettext_noop ("# bytes received from other peers"),
5085 n->distance = distance;
5087 GNUNET_TIME_relative_to_absolute
5088 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5089 GNUNET_SCHEDULER_cancel (n->timeout_task);
5091 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
5092 &neighbour_timeout_task, n);
5093 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
5095 /* dropping message due to frequent inbound volume violations! */
5096 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
5097 GNUNET_ERROR_TYPE_BULK,
5099 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
5100 n->in_tracker.available_bytes_per_s__,
5101 n->quota_violation_count);
5102 GNUNET_STATISTICS_update (stats,
5103 gettext_noop ("# bandwidth quota violations by other peers"),
5106 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
5110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5111 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
5112 ntohs (message->type),
5113 ntohs (message->size),
5116 switch (ntohs (message->type))
5118 case GNUNET_MESSAGE_TYPE_HELLO:
5119 GNUNET_STATISTICS_update (stats,
5120 gettext_noop ("# HELLO messages received from other peers"),
5123 process_hello (plugin, message);
5125 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
5126 handle_ping (plugin, message, peer, session, sender_address, sender_address_len);
5128 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
5129 handle_pong (plugin, message, peer, sender_address, sender_address_len);
5132 handle_payload_message (message, n);
5136 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
5137 if (ret.rel_value > 0)
5139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5140 "Throttling read (%llu bytes excess at %u b/s), waiting %llums before reading more.\n",
5141 (unsigned long long) n->in_tracker.consumption_since_last_update__,
5142 (unsigned int) n->in_tracker.available_bytes_per_s__,
5143 (unsigned long long) ret.rel_value);
5144 GNUNET_STATISTICS_update (stats,
5145 gettext_noop ("# ms throttling suggested"),
5146 (int64_t) ret.rel_value,
5153 * Handle START-message. This is the first message sent to us
5154 * by any client which causes us to add it to our list.
5156 * @param cls closure (always NULL)
5157 * @param client identification of the client
5158 * @param message the actual message
5161 handle_start (void *cls,
5162 struct GNUNET_SERVER_Client *client,
5163 const struct GNUNET_MessageHeader *message)
5165 const struct StartMessage *start;
5166 struct TransportClient *c;
5167 struct ConnectInfoMessage * cim;
5168 struct NeighbourList *n;
5172 start = (const struct StartMessage*) message;
5174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5175 "Received `%s' request from client\n", "START");
5180 if (c->client == client)
5182 /* client already on our list! */
5184 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5189 if ( (GNUNET_NO != ntohl (start->do_check)) &&
5190 (0 != memcmp (&start->self,
5192 sizeof (struct GNUNET_PeerIdentity))) )
5194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5195 _("Rejecting control connection from peer `%s', which is not me!\n"),
5196 GNUNET_i2s (&start->self));
5197 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5200 c = GNUNET_malloc (sizeof (struct TransportClient));
5204 if (our_hello != NULL)
5207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5208 "Sending our own `%s' to new client\n", "HELLO");
5210 transmit_to_client (c,
5211 (const struct GNUNET_MessageHeader *) our_hello,
5213 /* tell new client about all existing connections */
5215 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
5216 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
5220 cim = GNUNET_malloc (size);
5221 cim->header.size = htons (size);
5222 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
5223 cim->ats_count = htonl(ats_count);
5224 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
5225 (&(cim->ats))[2].value = htonl (0);
5229 if (GNUNET_YES == n->received_pong)
5231 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5232 (&(cim->ats))[0].value = htonl (n->distance);
5233 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
5234 (&(cim->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
5236 transmit_to_client (c, &cim->header, GNUNET_NO);
5242 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5247 * Handle HELLO-message.
5249 * @param cls closure (always NULL)
5250 * @param client identification of the client
5251 * @param message the actual message
5254 handle_hello (void *cls,
5255 struct GNUNET_SERVER_Client *client,
5256 const struct GNUNET_MessageHeader *message)
5260 GNUNET_STATISTICS_update (stats,
5261 gettext_noop ("# HELLOs received from clients"),
5264 ret = process_hello (NULL, message);
5265 GNUNET_SERVER_receive_done (client, ret);
5270 * Closure for 'transmit_client_message'; followed by
5271 * 'msize' bytes of the actual message.
5273 struct TransmitClientMessageContext
5276 * Client on whom's behalf we are sending.
5278 struct GNUNET_SERVER_Client *client;
5281 * Timeout for the transmission.
5283 struct GNUNET_TIME_Absolute timeout;
5291 * Size of the message in bytes.
5298 * Schedule transmission of a message we got from a client to a peer.
5300 * @param cls the 'struct TransmitClientMessageContext*'
5301 * @param n destination, or NULL on error (in that case, drop the message)
5304 transmit_client_message (void *cls,
5305 struct NeighbourList *n)
5307 struct TransmitClientMessageContext *tcmc = cls;
5308 struct TransportClient *tc;
5311 while ((tc != NULL) && (tc->client != tcmc->client))
5316 transmit_to_peer (tc, NULL, tcmc->priority,
5317 GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
5319 tcmc->msize, GNUNET_NO, n);
5321 GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
5322 GNUNET_SERVER_client_drop (tcmc->client);
5328 * Handle SEND-message.
5330 * @param cls closure (always NULL)
5331 * @param client identification of the client
5332 * @param message the actual message
5335 handle_send (void *cls,
5336 struct GNUNET_SERVER_Client *client,
5337 const struct GNUNET_MessageHeader *message)
5339 const struct OutboundMessage *obm;
5340 const struct GNUNET_MessageHeader *obmm;
5341 struct TransmitClientMessageContext *tcmc;
5345 size = ntohs (message->size);
5347 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
5350 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5353 GNUNET_STATISTICS_update (stats,
5354 gettext_noop ("# payload received for other peers"),
5357 obm = (const struct OutboundMessage *) message;
5358 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
5359 msize = size - sizeof (struct OutboundMessage);
5361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5362 "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
5363 "SEND", GNUNET_i2s (&obm->peer),
5367 tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
5368 tcmc->client = client;
5369 tcmc->priority = ntohl (obm->priority);
5370 tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
5371 tcmc->msize = msize;
5372 /* FIXME: this memcpy can be up to 7% of our total runtime */
5373 memcpy (&tcmc[1], obmm, msize);
5374 GNUNET_SERVER_client_keep (client);
5375 setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
5376 &transmit_client_message,
5382 * Handle request connect message
5384 * @param cls closure (always NULL)
5385 * @param client identification of the client
5386 * @param message the actual message
5389 handle_request_connect (void *cls,
5390 struct GNUNET_SERVER_Client *client,
5391 const struct GNUNET_MessageHeader *message)
5393 const struct TransportRequestConnectMessage *trcm =
5394 (const struct TransportRequestConnectMessage *) message;
5396 GNUNET_STATISTICS_update (stats,
5397 gettext_noop ("# REQUEST CONNECT messages received"),
5400 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a request connect message for peer %s\n", GNUNET_i2s(&trcm->peer));
5401 setup_peer_check_blacklist (&trcm->peer, GNUNET_YES,
5403 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5407 * Handle SET_QUOTA-message.
5409 * @param cls closure (always NULL)
5410 * @param client identification of the client
5411 * @param message the actual message
5414 handle_set_quota (void *cls,
5415 struct GNUNET_SERVER_Client *client,
5416 const struct GNUNET_MessageHeader *message)
5418 const struct QuotaSetMessage *qsm =
5419 (const struct QuotaSetMessage *) message;
5420 struct NeighbourList *n;
5422 GNUNET_STATISTICS_update (stats,
5423 gettext_noop ("# SET QUOTA messages received"),
5426 n = find_neighbour (&qsm->peer);
5429 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5430 GNUNET_STATISTICS_update (stats,
5431 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
5437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5438 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
5440 (unsigned int) ntohl (qsm->quota.value__),
5441 (unsigned int) n->in_tracker.available_bytes_per_s__,
5442 GNUNET_i2s (&qsm->peer));
5444 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
5446 if (0 == ntohl (qsm->quota.value__))
5448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5449 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&n->id),
5451 disconnect_neighbour (n, GNUNET_NO);
5453 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5458 * Take the given address and append it to the set of results sent back to
5461 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
5462 * @param address the resolved name, NULL to indicate the last response
5465 transmit_address_to_client (void *cls, const char *address)
5467 struct GNUNET_SERVER_TransmitContext *tc = cls;
5470 if (NULL == address)
5473 slen = strlen (address) + 1;
5475 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
5476 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5477 if (NULL == address)
5478 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
5483 * Handle AddressLookup-message.
5485 * @param cls closure (always NULL)
5486 * @param client identification of the client
5487 * @param message the actual message
5490 handle_address_lookup (void *cls,
5491 struct GNUNET_SERVER_Client *client,
5492 const struct GNUNET_MessageHeader *message)
5494 const struct AddressLookupMessage *alum;
5495 struct TransportPlugin *lsPlugin;
5496 const char *nameTransport;
5497 const char *address;
5499 struct GNUNET_SERVER_TransmitContext *tc;
5500 struct GNUNET_TIME_Absolute timeout;
5501 struct GNUNET_TIME_Relative rtimeout;
5504 size = ntohs (message->size);
5505 if (size < sizeof (struct AddressLookupMessage))
5507 GNUNET_break_op (0);
5508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5511 alum = (const struct AddressLookupMessage *) message;
5512 uint32_t addressLen = ntohl (alum->addrlen);
5513 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
5515 GNUNET_break_op (0);
5516 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5519 address = (const char *) &alum[1];
5520 nameTransport = (const char *) &address[addressLen];
5522 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
5524 GNUNET_break_op (0);
5525 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5528 timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
5529 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5530 numeric = ntohl (alum->numeric_only);
5531 lsPlugin = find_transport (nameTransport);
5532 if (NULL == lsPlugin)
5534 tc = GNUNET_SERVER_transmit_context_create (client);
5535 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5536 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5537 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
5540 tc = GNUNET_SERVER_transmit_context_create (client);
5541 lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
5543 address, addressLen,
5546 &transmit_address_to_client, tc);
5551 * Setup the environment for this plugin.
5554 create_environment (struct TransportPlugin *plug)
5556 plug->env.cfg = cfg;
5557 plug->env.my_identity = &my_identity;
5558 plug->env.our_hello = &our_hello;
5559 plug->env.cls = plug;
5560 plug->env.receive = &plugin_env_receive;
5561 plug->env.notify_address = &plugin_env_notify_address;
5562 plug->env.session_end = &plugin_env_session_end;
5563 plug->env.max_connections = max_connect_per_transport;
5564 plug->env.stats = stats;
5569 * Start the specified transport (load the plugin).
5572 start_transport (struct GNUNET_SERVER_Handle *server,
5575 struct TransportPlugin *plug;
5578 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5579 _("Loading `%s' transport plugin\n"), name);
5580 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
5581 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
5582 create_environment (plug);
5583 plug->short_name = GNUNET_strdup (name);
5584 plug->lib_name = libname;
5585 plug->next = plugins;
5587 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
5588 if (plug->api == NULL)
5590 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5591 _("Failed to load transport plugin for `%s'\n"), name);
5592 GNUNET_free (plug->short_name);
5593 plugins = plug->next;
5594 GNUNET_free (libname);
5601 * Called whenever a client is disconnected. Frees our
5602 * resources associated with that client.
5604 * @param cls closure
5605 * @param client identification of the client
5608 client_disconnect_notification (void *cls,
5609 struct GNUNET_SERVER_Client *client)
5611 struct TransportClient *pos;
5612 struct TransportClient *prev;
5613 struct ClientMessageQueueEntry *mqe;
5614 struct Blacklisters *bl;
5615 struct BlacklistCheck *bc;
5620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
5621 "Client disconnected, cleaning up.\n");
5623 /* clean up blacklister */
5627 if (bl->client == client)
5632 if (bc->bl_pos == bl)
5634 bc->bl_pos = bl->next;
5637 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
5640 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
5641 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
5647 GNUNET_CONTAINER_DLL_remove (bl_head,
5650 GNUNET_SERVER_client_drop (bl->client);
5656 /* clean up 'normal' clients */
5659 while ((pos != NULL) && (pos->client != client))
5666 while (NULL != (mqe = pos->message_queue_head))
5668 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
5669 pos->message_queue_tail,
5671 pos->message_count--;
5675 clients = pos->next;
5677 prev->next = pos->next;
5678 if (GNUNET_YES == pos->tcs_pending)
5683 if (pos->th != NULL)
5685 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
5688 GNUNET_break (0 == pos->message_count);
5694 * Function called when the service shuts down. Unloads our plugins
5695 * and cancels pending validations.
5697 * @param cls closure, unused
5698 * @param tc task context (unused)
5701 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5703 struct TransportPlugin *plug;
5704 struct OwnAddressList *al;
5705 struct CheckHelloValidatedContext *chvc;
5707 while (neighbours != NULL)
5709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5710 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&neighbours->id),
5712 disconnect_neighbour (neighbours, GNUNET_NO);
5715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5716 "Transport service is unloading plugins...\n");
5718 while (NULL != (plug = plugins))
5720 plugins = plug->next;
5721 if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
5723 GNUNET_SCHEDULER_cancel (plug->address_update_task);
5724 plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
5726 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
5727 GNUNET_free (plug->lib_name);
5728 GNUNET_free (plug->short_name);
5729 while (NULL != (al = plug->addresses))
5731 plug->addresses = al->next;
5736 if (my_private_key != NULL)
5737 GNUNET_CRYPTO_rsa_key_free (my_private_key);
5738 GNUNET_free_non_null (our_hello);
5740 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
5743 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
5744 validation_map = NULL;
5748 /* free 'chvc' data structure */
5749 while (NULL != (chvc = chvc_head))
5751 chvc_head = chvc->next;
5752 if (chvc->piter != NULL)
5754 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
5755 GNUNET_STATISTICS_update (stats,
5756 gettext_noop ("# outstanding peerinfo iterate requests"),
5762 GNUNET_assert (chvc->ve_count == 0);
5769 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
5772 if (peerinfo != NULL)
5774 GNUNET_PEERINFO_disconnect (peerinfo);
5777 /* Can we assume those are gone by now, or do we need to clean up
5779 GNUNET_break (bl_head == NULL);
5780 GNUNET_break (bc_head == NULL);
5785 #define DEBUG_ATS GNUNET_NO
5786 #define VERBOSE_ATS GNUNET_NO
5789 /** solve the bandwidth distribution problem
5790 * @param max_it maximum iterations
5791 * @param max_dur maximum duration in ms
5792 * @param D weight for diversity
5793 * @param U weight for utility
5794 * @param R weight for relativity
5795 * @param v_b_min minimal bandwidth per peer
5796 * @param v_n_min minimum number of connections
5797 * @param res result struct
5798 * @return GNUNET_SYSERR if glpk is not available, number of mechanisms used
5800 static int ats_solve_problem (int max_it, int max_dur , double D, double U, double R, int v_b_min, int v_n_min, struct ATS_result *res)
5803 if (DEBUG_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "no glpk installed\n");
5804 return GNUNET_SYSERR;
5806 if (DEBUG_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "glpk installed\n");
5816 int c_c_ressources = available_ressources;
5817 int c_q_metrics = available_quality_metrics;
5819 //double M = 10000000000; // ~10 GB
5820 //double M = VERY_BIG_DOUBLE_VALUE;
5822 double Q[c_q_metrics+1];
5823 for (c=1; c<=c_q_metrics; c++)
5828 struct NeighbourList *next = neighbours;
5831 struct ReadyList *r_next = next->plugins;
5832 while (r_next != NULL)
5834 struct ForeignAddressList * a_next = r_next->addresses;
5835 while (a_next != NULL)
5838 a_next = a_next->next;
5840 r_next = r_next->next;
5848 if (DEBUG_ATS) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No addresses for bw distribution available\n", c_peers);
5852 struct ATS_mechanism * mechanisms = GNUNET_malloc((1+c_mechs) * sizeof (struct ATS_mechanism));
5853 struct ATS_peer * peers = GNUNET_malloc((1+c_peers) * sizeof (struct ATS_peer));
5855 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found mechanisms: %i\n", c_mechs);
5861 peers[c_peers].peer = next->id;
5862 peers[c_peers].m_head = NULL;
5863 peers[c_peers].m_tail = NULL;
5865 peers[c_peers].f = 1.0 / c_mechs;
5867 struct ReadyList *r_next = next->plugins;
5868 while (r_next != NULL)
5870 struct ForeignAddressList * a_next = r_next->addresses;
5871 while (a_next != NULL)
5873 if (DEBUG_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%i Peer: `%s' plugin `%s' %x:\n", c_mechs, GNUNET_i2s(&next->id), r_next->plugin->short_name, a_next);
5874 mechanisms[c_mechs].addr = a_next;
5875 mechanisms[c_mechs].col_index = c_mechs;
5876 mechanisms[c_mechs].peer = &peers[c_peers];
5877 mechanisms[c_mechs].next = NULL;
5878 mechanisms[c_mechs].plugin = r_next->plugin;
5880 GNUNET_CONTAINER_DLL_insert_tail(peers[c_peers].m_head, peers[c_peers].m_tail, &mechanisms[c_mechs]);
5882 a_next = a_next->next;
5884 r_next = r_next->next;
5892 if (v_n_min > c_peers)
5895 /* number of variables == coloumns */
5896 //int c_cols = 2 * c_mechs + 3 + c_q_metrics;
5897 /* number of constraints == rows */
5898 //int c_rows = 2 * c_peers + 2 * c_mechs + c_c_ressources + c_q_metrics + 3;
5900 if (VERBOSE_ATS) 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);
5902 int size = 1 + 3 + 10 *c_mechs + c_peers + (c_q_metrics*c_mechs)+ c_q_metrics + c_c_ressources * c_mechs ;
5903 //int size = 1 + 8 *c_mechs +2 + c_mechs + c_peers + (c_q_metrics*c_mechs)+c_q_metrics + c_c_ressources ;
5906 int * ia = GNUNET_malloc (size * sizeof (int));
5907 int * ja = GNUNET_malloc (size * sizeof (int));
5908 double * ar = GNUNET_malloc(size* sizeof (double));
5910 prob = glp_create_prob();
5911 glp_set_prob_name(prob, "gnunet ats bandwidth distribution");
5912 glp_set_obj_dir(prob, GLP_MAX);
5914 /* adding columns */
5916 glp_add_cols(prob, 2 * c_mechs);
5917 /* adding b_t cols */
5918 for (c=1; c <= c_mechs; c++)
5920 GNUNET_asprintf(&name, "b%i",c);
5921 glp_set_col_name(prob, c, name);
5923 glp_set_col_bnds(prob, c, GLP_LO, 0.0, 0.0);
5924 glp_set_obj_coef(prob, c, 1);
5927 /* adding n_t cols */
5928 for (c=c_mechs+1; c <= 2*c_mechs; c++)
5930 GNUNET_asprintf(&name, "n%i",(c-c_mechs));
5931 glp_set_col_name(prob, c, name);
5933 glp_set_col_bnds(prob, c, GLP_DB, 0.0, 1.0);
5934 glp_set_col_kind(prob, c, GLP_IV);
5935 glp_set_obj_coef(prob, c, 0);
5938 /* feasibility constraints */
5939 /* Constraint 1: one address per peer*/
5940 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constraint 1\n");
5942 glp_add_rows(prob, c_peers);
5943 for (c=1; c<=c_peers; c++)
5945 glp_set_row_bnds(prob, row_index, GLP_FX, 1.0, 1.0);
5947 struct ATS_mechanism *m = peers[c].m_head;
5950 ia[array_index] = row_index;
5951 ja[array_index] = (c_mechs + m->col_index);
5952 ar[array_index] = 1;
5953 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
5960 /* Constraint 2: only active mechanism gets bandwidth assigned */
5961 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constraint 2\n");
5962 glp_add_rows(prob, c_mechs);
5963 for (c=1; c<=c_mechs; c++)
5965 /* b_t - n_t * M <= 0 */
5966 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
5967 glp_set_row_bnds(prob, row_index, GLP_UP, 0.0, 0.0);
5969 ia[array_index] = row_index;
5970 ja[array_index] = mechanisms[c].col_index;
5971 ar[array_index] = 1;
5972 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
5974 ia[array_index] = row_index;
5975 ja[array_index] = c_mechs + mechanisms[c].col_index;
5976 ar[array_index] = -M;
5977 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
5982 /* Constraint 3: minimum bandwidth*/
5983 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constraint 3\n");
5984 glp_add_rows(prob, c_mechs);
5985 for (c=1; c<=c_mechs; c++)
5987 /* b_t - n_t * b_min <= 0 */
5988 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
5989 glp_set_row_bnds(prob, row_index, GLP_LO, 0.0, 0.0);
5991 ia[array_index] = row_index;
5992 ja[array_index] = mechanisms[c].col_index;
5993 ar[array_index] = 1;
5994 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
5996 ia[array_index] = row_index;
5997 ja[array_index] = c_mechs + mechanisms[c].col_index;
5998 ar[array_index] = -v_b_min;
5999 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6004 /* Constraint 4: max ressource capacity */
6005 /* V cr: bt * ct_r <= cr_max
6008 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constraint 4\n");
6009 glp_add_rows(prob, available_ressources);
6010 double ct_max = VERY_BIG_DOUBLE_VALUE;
6011 double ct_min = 0.0;
6013 for (c=0; c<available_ressources; c++)
6015 ct_max = ressources[c].c_max;
6016 ct_min = ressources[c].c_min;
6017 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] %f..%f\n",row_index, ct_min, ct_max);
6018 glp_set_row_bnds(prob, row_index, GLP_DB, ct_min, ct_max);
6020 for (c2=1; c2<=c_mechs; c2++)
6023 ia[array_index] = row_index;
6024 ja[array_index] = c2;
6025 value = mechanisms[c2].addr->ressources[c].c;
6026 ar[array_index] = value;
6027 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6033 /* Constraint 5: min number of connections*/
6034 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constraint 5\n");
6035 glp_add_rows(prob, 1);
6036 for (c=1; c<=c_mechs; c++)
6038 // b_t - n_t * b_min >= 0
6039 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6040 glp_set_row_bnds(prob, row_index, GLP_LO, v_n_min, 0.0);
6042 ia[array_index] = row_index;
6043 ja[array_index] = c_mechs + mechanisms[c].col_index;
6044 ar[array_index] = 1;
6045 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6050 /* optimisation constraints*/
6052 /* adding columns */
6053 glp_add_cols(prob, 3 + c_q_metrics);
6055 glp_set_col_name(prob, (2*c_mechs) + 1, "d");
6056 glp_set_obj_coef(prob, (2*c_mechs) + 1, D);
6057 glp_set_col_bnds(prob, (2*c_mechs) + 1, GLP_LO, 0.0, 0.0);
6058 glp_set_col_name(prob, (2*c_mechs) + 2, "u");
6059 glp_set_obj_coef(prob, (2*c_mechs) + 2, U);
6060 glp_set_col_bnds(prob, (2*c_mechs) + 2, GLP_LO, 0.0, 0.0);
6061 glp_set_col_name(prob, (2*c_mechs) + 3, "r");
6062 glp_set_obj_coef(prob, (2*c_mechs) + 3, R);
6063 glp_set_col_bnds(prob, (2*c_mechs) + 3, GLP_LO, 0.0, 0.0);
6065 for (c=1; c<= c_q_metrics; c++)
6067 GNUNET_asprintf(&name, "Q_%s",qm[c-1].name);
6068 glp_set_col_name(prob, (2*c_mechs) + 3 + c, name);
6069 glp_set_col_bnds(prob, (2*c_mechs) + 3 + c, GLP_LO, 0.0, 0.0);
6071 glp_set_obj_coef(prob, (2*c_mechs) + 3 + c, Q[c]);
6074 // Constraint 6: optimize for diversity
6075 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constraint 6\n");
6076 glp_add_rows(prob, 1);
6077 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6078 glp_set_row_bnds(prob, row_index, GLP_FX, 0.0, 0.0);
6079 //glp_set_row_bnds(prob, row_index, GLP_UP, 0.0, 0.0);
6080 for (c=1; c<=c_mechs; c++)
6082 // b_t - n_t * b_min >= 0
6083 ia[array_index] = row_index;
6084 ja[array_index] = c_mechs + mechanisms[c].col_index;
6085 ar[array_index] = 1;
6086 //if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6089 ia[array_index] = row_index;
6090 ja[array_index] = (2*c_mechs) + 1;
6091 ar[array_index] = -1;
6092 //if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6097 // Constraint 7: optimize for quality
6099 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constraint 7\n");
6100 glp_add_rows(prob, available_quality_metrics);
6101 for (c=1; c <= c_q_metrics; c++)
6103 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6104 glp_set_row_bnds(prob, row_index, GLP_FX, 0.0, 0.0);
6106 for (c2=1; c2<=c_mechs; c2++)
6109 ia[array_index] = row_index;
6110 ja[array_index] = c2;
6111 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY)
6114 v0 = mechanisms[c2].addr->quality[c-1].values[0];
6115 if (v1 < 1) v0 = 0.1;
6116 v1 = mechanisms[c2].addr->quality[c-1].values[1];
6117 if (v1 < 1) v0 = 0.1;
6118 v2 = mechanisms[c2].addr->quality[c-1].values[2];
6119 if (v1 < 1) v0 = 0.1;
6120 value = 100.0 / ((v0 + 2 * v1 + 3 * v2) / 6.0);
6122 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
6125 v0 = mechanisms[c2].addr->quality[c-1].values[0];
6127 v1 = mechanisms[c2].addr->quality[c-1].values[1];
6129 v2 = mechanisms[c2].addr->quality[c-1].values[2];
6131 value = (v0 + 2 * v1 + 3 * v2) / 6.0;
6133 value = (double) 10 / value;
6136 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DISTANCE VALUE %f\n",value);
6138 ar[array_index] = (mechanisms[c2].peer->f) * value ;
6139 //if (VERBOSE_ATS) 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]);
6143 ia[array_index] = row_index;
6144 ja[array_index] = (2*c_mechs) + 3 +c;
6145 ar[array_index] = -1;
6146 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6151 // Constraint 8: optimize bandwidth utility
6152 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constraint 8\n");
6153 glp_add_rows(prob, 1);
6154 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6155 glp_set_row_bnds(prob, row_index, GLP_FX, 0.0, 0.0);
6156 for (c=1; c<=c_mechs; c++)
6158 ia[array_index] = row_index;
6159 ja[array_index] = c;
6160 ar[array_index] = mechanisms[c].peer->f;
6161 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6164 ia[array_index] = row_index;
6165 ja[array_index] = (2*c_mechs) + 2;
6166 ar[array_index] = -1;
6168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6174 // Constraint 9: optimize relativity
6175 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constraint 9\n");
6176 glp_add_rows(prob, c_peers);
6177 for (c=1; c<=c_peers; c++)
6179 glp_set_row_bnds(prob, row_index, GLP_LO, 0.0, 0.0);
6181 struct ATS_mechanism *m = peers[c].m_head;
6184 ia[array_index] = row_index;
6185 ja[array_index] = m->col_index;
6186 ar[array_index] = 1;
6187 //if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6191 ia[array_index] = row_index;
6192 ja[array_index] = (2*c_mechs) + 3;
6193 ar[array_index] = -1;
6194 //if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6199 glp_load_matrix(prob, array_index-1, ia, ja, ar);
6202 glp_init_smcp(&opt_lp);
6204 opt_lp.msg_lev = GLP_MSG_ALL;
6206 opt_lp.msg_lev = GLP_MSG_OFF;
6207 result = glp_simplex(prob, &opt_lp);
6210 glp_init_iocp(&opt_mlp);
6211 /* maximum duration */
6212 opt_mlp.tm_lim = max_dur;
6215 opt_mlp.msg_lev = GLP_MSG_ALL;
6217 opt_mlp.msg_lev = GLP_MSG_OFF;
6219 result = glp_intopt (prob, &opt_mlp);
6220 solution = glp_mip_status (prob);
6227 GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i.mlp",c_peers, c_mechs);
6228 if (GNUNET_NO == GNUNET_DISK_file_test(filename))
6229 glp_write_lp (prob, NULL, filename);
6230 GNUNET_free (filename);
6238 case GLP_ESTOP : /* search terminated by application */
6239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Search terminated by application ");
6241 case GLP_EITLIM : /* iteration limit exceeded */
6242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Iteration limit exceeded ");
6245 case GLP_ETMLIM : /* time limit exceeded */
6246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Time limit exceeded ");
6248 case GLP_ENOPFS : /* no primal feasible solution */
6249 case GLP_ENODFS : /* no dual feasible solution */
6250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No feasible solution");
6253 case GLP_EBADB : /* invalid basis */
6254 case GLP_ESING : /* singular matrix */
6255 case GLP_ECOND : /* ill-conditioned matrix */
6256 case GLP_EBOUND : /* invalid bounds */
6257 case GLP_EFAIL : /* solver failed */
6258 case GLP_EOBJLL : /* objective lower limit reached */
6259 case GLP_EOBJUL : /* objective upper limit reached */
6260 case GLP_EROOT : /* root LP optimum not provided */
6261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid Input data: %i\n", result);
6266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Problem has been solved\n");
6272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MIP solution is undefined\n");
6275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MIP solution is integer optimal\n");
6278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MIP solution is integer feasible, however, its optimality (or non-optimality) has not been proven, \n");
6281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MI problem has no integer feasible solution\n");
6289 int error = GNUNET_NO;
6291 struct ATS_mechanism *t = NULL;
6292 for (c=1; c<= (c_peers); c++ )
6295 t = peers[c].m_head;
6298 bw = glp_get_col_prim(prob, t->col_index);
6301 if (VERBOSE_ATS) 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);
6302 if (check ==GNUNET_YES)
6304 glp_write_sol(prob, "invalid_solution.mlp");
6305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid solution, check invalid_solution.mlp");
6306 GNUNET_STATISTICS_update (stats, "ATS invalid solutions", 1, GNUNET_NO);
6309 if (check ==GNUNET_NO)
6316 for (c=1; c<= c_q_metrics; c++ )
6318 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(prob,2*c_mechs+3+c), glp_get_col_prim(prob,2*c_mechs+3+c));
6320 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(prob,2*c_mechs+1), glp_get_col_prim(prob,2*c_mechs+1));
6321 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(prob,2*c_mechs+2), glp_get_col_prim(prob,2*c_mechs+2));
6322 if (VERBOSE_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s %f\n", glp_get_col_name(prob,2*c_mechs+3), glp_get_col_prim(prob,2*c_mechs+3));
6324 res->c_mechs = c_mechs;
6325 res->c_peers = c_peers;
6326 res->solution = solution;
6331 glp_delete_prob(prob);
6337 for (c=0; c<c_mechs; c++)
6339 GNUNET_free_non_null (mechanisms[c].rc);
6342 GNUNET_free(mechanisms);
6349 void ats_calculate_bandwidth_distribution ()
6351 static int glpk = GNUNET_YES;
6352 struct GNUNET_TIME_Absolute start;
6353 struct GNUNET_TIME_Relative duration;
6354 struct ATS_result result;
6357 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_difference(ats->last,GNUNET_TIME_absolute_get());
6358 if (delta.rel_value < ats->min_delta.rel_value)
6361 //GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Minimum time between cycles not reached\n");
6367 if (INT_MAX < ats->max_exec_duration.rel_value)
6370 dur = (int) ats->max_exec_duration.rel_value;
6372 start = GNUNET_TIME_absolute_get();
6374 if (glpk==GNUNET_YES)
6376 start = GNUNET_TIME_absolute_get();
6377 c_mechs = ats_solve_problem(5000, 5000, 1.0, 1.0, 1.0, 1000, 5, &result);
6378 duration = GNUNET_TIME_absolute_get_difference(start,GNUNET_TIME_absolute_get());
6382 if (DEBUG_ATS) {GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP execution time in [ms] for %i mechanisms: %llu\n", c_mechs, duration.rel_value);}
6383 GNUNET_STATISTICS_set (stats, "ATS duration", duration.rel_value, GNUNET_NO);
6384 GNUNET_STATISTICS_set (stats, "ATS mechanisms", result.c_mechs, GNUNET_NO);
6385 GNUNET_STATISTICS_set (stats, "ATS peers", result.c_peers, GNUNET_NO);
6386 GNUNET_STATISTICS_set (stats, "ATS solution", result.solution, GNUNET_NO);
6387 GNUNET_STATISTICS_set (stats, "ATS timestamp", start.abs_value, GNUNET_NO);
6389 else if (c_mechs == 0)
6391 if (DEBUG_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP not executed: no addresses\n");
6393 else glpk = GNUNET_NO;
6395 ats->last = GNUNET_TIME_absolute_get();
6401 ats_schedule_calculation (void *cls,
6402 const struct GNUNET_SCHEDULER_TaskContext *tc)
6404 struct ATS_info *ats = (struct ATS_info *) cls;
6408 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6409 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
6413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running scheduled calculation\n");
6415 ats_calculate_bandwidth_distribution (ats);
6417 ats->ats_task = GNUNET_SCHEDULER_add_delayed (ats->exec_intervall,
6418 &ats_schedule_calculation, ats);
6423 ats = GNUNET_malloc(sizeof (struct ATS_info));
6425 ats->min_delta = ATS_MIN_INTERVAL;
6426 ats->exec_intervall = ATS_EXEC_INTERVAL;
6427 ats->max_exec_duration = ATS_MAX_EXEC_DURATION;
6428 ats->max_iterations = ATS_MAX_ITERATIONS;
6429 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6432 unsigned long long value;
6434 /* loading cost ressources */
6435 for (c=0; c<available_ressources; c++)
6437 GNUNET_asprintf(§ion,"%s_UP",ressources[c].cfg_param);
6438 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
6440 GNUNET_CONFIGURATION_get_value_number(cfg, "transport",section, &value);
6441 if (DEBUG_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found ressource cost: [%s] = %llu\n", section, value);
6442 ressources[c].c_max = value;
6444 GNUNET_free (section);
6445 GNUNET_asprintf(§ion,"%s_DOWN",ressources[c].cfg_param);
6446 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
6448 GNUNET_CONFIGURATION_get_value_number(cfg, "transport",section, &value);
6449 if (DEBUG_ATS) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found ressource cost: [%s] = %llu\n", section, value);
6450 ressources[c].c_min = value;
6452 GNUNET_free (section);
6455 ats->ats_task = GNUNET_SCHEDULER_add_now(&ats_schedule_calculation, ats);
6459 void ats_shutdown ()
6462 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_destroy\n");
6464 if (ats->ats_task != GNUNET_SCHEDULER_NO_TASK)
6465 GNUNET_SCHEDULER_cancel(ats->ats_task);
6466 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6468 struct ATS_plugin * p;
6469 struct ATS_ressource_cost * rc;
6473 GNUNET_CONTAINER_DLL_remove (ats->head,ats->tail, p);
6477 GNUNET_CONTAINER_DLL_remove (p->head,p->tail, rc);
6481 GNUNET_free(p->short_name);
6490 void ats_notify_peer_connect (
6491 const struct GNUNET_PeerIdentity *peer,
6492 const struct GNUNET_TRANSPORT_ATS_Information *ats_data)
6496 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_notify_peer_connect: %s\n",GNUNET_i2s(peer));
6499 while (ntohl(ats_data[c].type)!=0)
6502 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats type [%i]: %i\n",ntohl(ats_data[c].type), ntohl(ats_data[c].value));
6506 ats_calculate_bandwidth_distribution(ats);
6509 void ats_notify_peer_disconnect (
6510 const struct GNUNET_PeerIdentity *peer)
6513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_notify_peer_disconnect: %s\n",GNUNET_i2s(peer));
6515 ats_calculate_bandwidth_distribution (ats);
6519 void ats_notify_ats_data (
6520 const struct GNUNET_PeerIdentity *peer,
6521 const struct GNUNET_TRANSPORT_ATS_Information *ats_data)
6524 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "ATS_notify_ats_data: %s\n",GNUNET_i2s(peer));
6526 ats_calculate_bandwidth_distribution(ats);
6529 struct ForeignAddressList * ats_get_preferred_address (
6530 struct NeighbourList *n)
6533 //GNUNET_log (GNUNET_ERROR_TYPE_BULK, "ats_get_prefered_transport for peer: %s\n",GNUNET_i2s(&n->id));
6535 struct ReadyList *next = n->plugins;
6536 while (next != NULL)
6539 //GNUNET_log (GNUNET_ERROR_TYPE_BULK, "plugin: %s %i\n",next->plugin->short_name,strcmp(next->plugin->short_name,"unix"));
6543 return find_ready_address(n);
6547 * Initiate transport service.
6549 * @param cls closure
6550 * @param server the initialized server
6551 * @param c configuration to use
6555 struct GNUNET_SERVER_Handle *server,
6556 const struct GNUNET_CONFIGURATION_Handle *c)
6558 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
6559 {&handle_start, NULL,
6560 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
6561 {&handle_hello, NULL,
6562 GNUNET_MESSAGE_TYPE_HELLO, 0},
6563 {&handle_send, NULL,
6564 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
6565 {&handle_request_connect, NULL,
6566 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT, sizeof(struct TransportRequestConnectMessage)},
6567 {&handle_set_quota, NULL,
6568 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
6569 {&handle_address_lookup, NULL,
6570 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
6572 {&handle_blacklist_init, NULL,
6573 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
6574 {&handle_blacklist_reply, NULL,
6575 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
6581 unsigned long long tneigh;
6585 stats = GNUNET_STATISTICS_create ("transport", cfg);
6586 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
6587 /* parse configuration */
6589 GNUNET_CONFIGURATION_get_value_number (c,
6594 GNUNET_CONFIGURATION_get_value_filename (c,
6596 "HOSTKEY", &keyfile)))
6598 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6600 ("Transport service is lacking key configuration settings. Exiting.\n"));
6601 GNUNET_SCHEDULER_shutdown ();
6604 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6607 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6608 validation_map = NULL;
6612 max_connect_per_transport = (uint32_t) tneigh;
6613 peerinfo = GNUNET_PEERINFO_connect (cfg);
6614 if (peerinfo == NULL)
6616 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6617 _("Could not access PEERINFO service. Exiting.\n"));
6618 GNUNET_SCHEDULER_shutdown ();
6621 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6624 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6625 validation_map = NULL;
6626 GNUNET_free (keyfile);
6629 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6630 GNUNET_free (keyfile);
6631 if (my_private_key == NULL)
6633 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6635 ("Transport service could not access hostkey. Exiting.\n"));
6636 GNUNET_SCHEDULER_shutdown ();
6639 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6642 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6643 validation_map = NULL;
6646 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6647 GNUNET_CRYPTO_hash (&my_public_key,
6648 sizeof (my_public_key), &my_identity.hashPubKey);
6649 /* setup notification */
6650 GNUNET_SERVER_disconnect_notify (server,
6651 &client_disconnect_notification, NULL);
6652 /* load plugins... */
6655 GNUNET_CONFIGURATION_get_value_string (c,
6656 "TRANSPORT", "PLUGINS", &plugs))
6658 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6659 _("Starting transport plugins `%s'\n"), plugs);
6660 pos = strtok (plugs, " ");
6663 start_transport (server, pos);
6665 pos = strtok (NULL, " ");
6667 GNUNET_free (plugs);
6669 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
6670 &shutdown_task, NULL);
6677 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport service ready.\n"));
6679 /* If we have a blacklist file, read from it */
6680 read_blacklist_file(cfg);
6681 /* process client requests */
6682 GNUNET_SERVER_add_handlers (server, handlers);
6687 * The main function for the transport service.
6689 * @param argc number of arguments from the command line
6690 * @param argv command line arguments
6691 * @return 0 ok, 1 on error
6694 main (int argc, char *const *argv)
6696 a2s (NULL, NULL, 0); /* make compiler happy */
6697 return (GNUNET_OK ==
6698 GNUNET_SERVICE_run (argc,
6701 GNUNET_SERVICE_OPTION_NONE,
6702 &run, NULL)) ? 0 : 1;
6705 /* end of gnunet-service-transport.c */