2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/gnunet-service-transport.c
23 * @brief low-level P2P messaging
24 * @author Christian Grothoff
28 #include "gnunet_client_lib.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "gnunet_plugin_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_service_lib.h"
38 #include "gnunet_signatures.h"
39 #include "gnunet_transport_plugin.h"
40 #include "transport.h"
45 #define DEBUG_BLACKLIST GNUNET_NO
47 #define DEBUG_PING_PONG GNUNET_NO
49 #define DEBUG_TRANSPORT_HELLO GNUNET_NO
51 #define DEBUG_ATS GNUNET_NO
53 #define VERBOSE_ATS GNUNET_NO
56 * Should we do some additional checks (to validate behavior
59 #define EXTRA_CHECKS GNUNET_YES
62 * How many messages can we have pending for a given client process
63 * before we start to drop incoming messages? We typically should
64 * have only one client and so this would be the primary buffer for
65 * messages, so the number should be chosen rather generously.
67 * The expectation here is that most of the time the queue is large
68 * enough so that a drop is virtually never required. Note that
69 * this value must be about as large as 'TOTAL_MSGS' in the
70 * 'test_transport_api_reliability.c', otherwise that testcase may
73 #define MAX_PENDING (128 * 1024)
76 * Size of the per-transport blacklist hash maps.
78 #define TRANSPORT_BLACKLIST_HT_SIZE 16
81 * How often should we try to reconnect to a peer using a particular
82 * transport plugin before giving up? Note that the plugin may be
83 * added back to the list after PLUGIN_RETRY_FREQUENCY expires.
85 #define MAX_CONNECT_RETRY 3
88 * Limit on the number of ready-to-run tasks when validating
89 * HELLOs. If more tasks are ready to run, we will drop
90 * HELLOs instead of validating them.
92 #define MAX_HELLO_LOAD 4
95 * How often must a peer violate bandwidth quotas before we start
96 * to simply drop its messages?
98 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
101 * How long until a HELLO verification attempt should time out?
102 * Must be rather small, otherwise a partially successful HELLO
103 * validation (some addresses working) might not be available
104 * before a client's request for a connection fails for good.
105 * Besides, if a single request to an address takes a long time,
106 * then the peer is unlikely worthwhile anyway.
108 #define HELLO_VERIFICATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
111 * How long is a PONG signature valid? We'll recycle a signature until
112 * 1/4 of this time is remaining. PONGs should expire so that if our
113 * external addresses change an adversary cannot replay them indefinitely.
114 * OTOH, we don't want to spend too much time generating PONG signatures,
115 * so they must have some lifetime to reduce our CPU usage.
117 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
120 * Priority to use for PONG messages.
122 #define TRANSPORT_PONG_PRIORITY 4
125 * How often do we re-add (cheaper) plugins to our list of plugins
126 * to try for a given connected peer?
128 #define PLUGIN_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
131 * After how long do we expire an address in a HELLO that we just
132 * validated? This value is also used for our own addresses when we
135 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
139 * How long before an existing address expires should we again try to
140 * validate it? Must be (significantly) smaller than
141 * HELLO_ADDRESS_EXPIRATION.
143 #define HELLO_REVALIDATION_START_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
146 * Maximum frequency for re-evaluating latencies for all transport addresses.
148 #define LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
151 * Maximum frequency for re-evaluating latencies for connected addresses.
153 #define CONNECTED_LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
155 #define VERY_BIG_DOUBLE_VALUE 100000000000LL
158 * List of addresses of other peers
160 struct ForeignAddressList
163 * This is a linked list.
165 struct ForeignAddressList *next;
168 * Which ready list does this entry belong to.
170 struct ReadyList *ready_list;
173 * How long until we auto-expire this address (unless it is
174 * re-confirmed by the transport)?
176 struct GNUNET_TIME_Absolute expires;
179 * Task used to re-validate addresses, updates latencies and
182 GNUNET_SCHEDULER_TaskIdentifier revalidate_task;
190 * Session (or NULL if no valid session currently exists or if the
191 * plugin does not use sessions).
193 struct Session *session;
195 struct ATS_ressource_entry * ressources;
197 struct ATS_quality_entry * quality;
200 * What was the last latency observed for this address, plugin and peer?
202 struct GNUNET_TIME_Relative latency;
205 * If we did not successfully transmit a message to the given peer
206 * via this connection during the specified time, we should consider
207 * the connection to be dead. This is used in the case that a TCP
208 * transport simply stalls writing to the stream but does not
209 * formerly get a signal that the other peer died.
211 struct GNUNET_TIME_Absolute timeout;
214 * How often have we tried to connect using this plugin? Used to
215 * discriminate against addresses that do not work well.
216 * FIXME: not yet used, but should be!
218 unsigned int connect_attempts;
221 * DV distance to this peer (1 if no DV is used).
222 * FIXME: need to set this from transport plugins!
232 * Have we ever estimated the latency of this address? Used to
233 * ensure that the first time we add an address, we immediately
239 * Are we currently connected via this address? The first time we
240 * successfully transmit or receive data to a peer via a particular
241 * address, we set this to GNUNET_YES. If we later get an error
242 * (disconnect notification, transmission failure, timeout), we set
243 * it back to GNUNET_NO.
248 * Is this plugin currently busy transmitting to the specific target?
249 * GNUNET_NO if not (initial, default state is GNUNET_NO). Internal
250 * messages do not count as 'in transmit'.
255 * Has this address been validated yet?
263 * Entry in linked list of network addresses for ourselves. Also
264 * includes a cached signature for 'struct TransportPongMessage's.
266 struct OwnAddressList
269 * This is a linked list.
271 struct OwnAddressList *next;
274 * How long until we actually auto-expire this address (unless it is
275 * re-confirmed by the transport)?
277 struct GNUNET_TIME_Absolute expires;
280 * How long until the current signature expires? (ZERO if the
281 * signature was never created).
283 struct GNUNET_TIME_Absolute pong_sig_expires;
286 * Signature for a 'struct TransportPongMessage' for this address.
288 struct GNUNET_CRYPTO_RsaSignature pong_signature;
299 * Entry in linked list of all of our plugins.
301 struct TransportPlugin
305 * This is a linked list.
307 struct TransportPlugin *next;
310 * API of the transport as returned by the plugin's
311 * initialization function.
313 struct GNUNET_TRANSPORT_PluginFunctions *api;
316 * Short name for the plugin (i.e. "tcp").
321 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
326 * List of our known addresses for this transport.
328 struct OwnAddressList *addresses;
331 * Environment this transport service is using
334 struct GNUNET_TRANSPORT_PluginEnvironment env;
337 * ID of task that is used to clean up expired addresses.
339 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
342 * Set to GNUNET_YES if we need to scrap the existing list of
343 * "addresses" and start fresh when we receive the next address
344 * update from a transport. Set to GNUNET_NO if we should just add
345 * the new address to the list and wait for the commit call.
349 struct ATS_plugin * rc;
352 * Hashmap of blacklisted peers for this particular transport.
354 struct GNUNET_CONTAINER_MultiHashMap *blacklist;
357 struct NeighbourList;
360 * For each neighbour we keep a list of messages
361 * that we still want to transmit to the neighbour.
367 * This is a doubly linked list.
369 struct MessageQueue *next;
372 * This is a doubly linked list.
374 struct MessageQueue *prev;
377 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
378 * stuck together in memory. Allocated at the end of this struct.
380 const char *message_buf;
383 * Size of the message buf
385 size_t message_buf_size;
388 * Client responsible for queueing the message;
389 * used to check that a client has no two messages
390 * pending for the same target. Can be NULL.
392 struct TransportClient *client;
395 * Using which specific address should we send this message?
397 struct ForeignAddressList *specific_address;
400 * Peer ID of the Neighbour this entry belongs to.
402 struct GNUNET_PeerIdentity neighbour_id;
405 * Plugin that we used for the transmission.
406 * NULL until we scheduled a transmission.
408 struct TransportPlugin *plugin;
411 * At what time should we fail?
413 struct GNUNET_TIME_Absolute timeout;
416 * Internal message of the transport system that should not be
417 * included in the usual SEND-SEND_OK transmission confirmation
418 * traffic management scheme. Typically, "internal_msg" will
419 * be set whenever "client" is NULL (but it is not strictly
425 * How important is the message?
427 unsigned int priority;
433 * For a given Neighbour, which plugins are available
434 * to talk to this peer and what are their costs?
439 * This is a linked list.
441 struct ReadyList *next;
444 * Which of our transport plugins does this entry
447 struct TransportPlugin *plugin;
450 * Transport addresses, latency, and readiness for
451 * this particular plugin.
453 struct ForeignAddressList *addresses;
456 * To which neighbour does this ready list belong to?
458 struct NeighbourList *neighbour;
463 * Entry in linked list of all of our current neighbours.
469 * This is a linked list.
471 struct NeighbourList *next;
474 * Which of our transports is connected to this peer
475 * and what is their status?
477 struct ReadyList *plugins;
480 * Head of list of messages we would like to send to this peer;
481 * must contain at most one message per client.
483 struct MessageQueue *messages_head;
486 * Tail of list of messages we would like to send to this peer; must
487 * contain at most one message per client.
489 struct MessageQueue *messages_tail;
492 * Buffer for at most one payload message used when we receive
493 * payload data before our PING-PONG has succeeded. We then
494 * store such messages in this intermediary buffer until the
495 * connection is fully up.
497 struct GNUNET_MessageHeader *pre_connect_message_buffer;
500 * Context for peerinfo iteration.
501 * NULL after we are done processing peerinfo's information.
503 struct GNUNET_PEERINFO_IteratorContext *piter;
506 * Public key for this peer. Valid only if the respective flag is set below.
508 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
511 * Identity of this neighbour.
513 struct GNUNET_PeerIdentity id;
516 * ID of task scheduled to run when this peer is about to
517 * time out (will free resources associated with the peer).
519 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
522 * ID of task scheduled to run when we should retry transmitting
523 * the head of the message queue. Actually triggered when the
524 * transmission is timing out (we trigger instantly when we have
525 * a chance of success).
527 GNUNET_SCHEDULER_TaskIdentifier retry_task;
530 * How long until we should consider this peer dead
531 * (if we don't receive another message in the
534 struct GNUNET_TIME_Absolute peer_timeout;
537 * Tracker for inbound bandwidth.
539 struct GNUNET_BANDWIDTH_Tracker in_tracker;
542 * The latency we have seen for this particular address for
543 * this particular peer. This latency may have been calculated
544 * over multiple transports. This value reflects how long it took
545 * us to receive a response when SENDING via this particular
546 * transport/neighbour/address combination!
548 * FIXME: we need to periodically send PINGs to update this
549 * latency (at least more often than the current "huge" (11h?)
552 struct GNUNET_TIME_Relative latency;
555 * How often has the other peer (recently) violated the
556 * inbound traffic limit? Incremented by 10 per violation,
557 * decremented by 1 per non-violation (for each
560 unsigned int quota_violation_count;
563 * DV distance to this peer (1 if no DV is used).
568 * Have we seen an PONG from this neighbour in the past (and
569 * not had a disconnect since)?
574 * Do we have a valid public key for this neighbour?
576 int public_key_valid;
579 * Performance data for the peer.
581 struct GNUNET_TRANSPORT_ATS_Information *ats;
584 * Identity of the neighbour.
586 struct GNUNET_PeerIdentity peer;
591 * Message used to ask a peer to validate receipt (to check an address
592 * from a HELLO). Followed by the address we are trying to validate,
593 * or an empty address if we are just sending a PING to confirm that a
594 * connection which the receiver (of the PING) initiated is still valid.
596 struct TransportPingMessage
600 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
602 struct GNUNET_MessageHeader header;
605 * Challenge code (to ensure fresh reply).
607 uint32_t challenge GNUNET_PACKED;
610 * Who is the intended recipient?
612 struct GNUNET_PeerIdentity target;
618 * Message used to validate a HELLO. The challenge is included in the
619 * confirmation to make matching of replies to requests possible. The
620 * signature signs our public key, an expiration time and our address.<p>
622 * This message is followed by our transport address that the PING tried
623 * to confirm (if we liked it). The address can be empty (zero bytes)
624 * if the PING had not address either (and we received the request via
625 * a connection that we initiated).
627 struct TransportPongMessage
631 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
633 struct GNUNET_MessageHeader header;
636 * Challenge code from PING (showing freshness). Not part of what
637 * is signed so that we can re-use signatures.
639 uint32_t challenge GNUNET_PACKED;
644 struct GNUNET_CRYPTO_RsaSignature signature;
647 * What are we signing and why? Two possible reason codes can be here:
648 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
649 * plausible address for this peer (pid is set to identity of signer); or
650 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING to confirm that this is
651 * an address we used to connect to the peer with the given pid.
653 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
656 * When does this signature expire?
658 struct GNUNET_TIME_AbsoluteNBO expiration;
661 * Either the identity of the peer Who signed this message, or the
662 * identity of the peer that we're connected to using the given
663 * address (depending on purpose.type).
665 struct GNUNET_PeerIdentity pid;
668 * Size of address appended to this message (part of what is
669 * being signed, hence not redundant).
677 * Linked list of messages to be transmitted to the client. Each
678 * entry is followed by the actual message.
680 struct ClientMessageQueueEntry
683 * This is a doubly-linked list.
685 struct ClientMessageQueueEntry *next;
688 * This is a doubly-linked list.
690 struct ClientMessageQueueEntry *prev;
695 * Client connected to the transport service.
697 struct TransportClient
701 * This is a linked list.
703 struct TransportClient *next;
706 * Handle to the client.
708 struct GNUNET_SERVER_Client *client;
711 * Linked list of messages yet to be transmitted to
714 struct ClientMessageQueueEntry *message_queue_head;
717 * Tail of linked list of messages yet to be transmitted to the
720 struct ClientMessageQueueEntry *message_queue_tail;
723 * Current transmit request handle.
725 struct GNUNET_CONNECTION_TransmitHandle *th;
728 * Is a call to "transmit_send_continuation" pending? If so, we
729 * must not free this struct (even if the corresponding client
730 * disconnects) and instead only remove it from the linked list and
731 * set the "client" field to NULL.
736 * Length of the list of messages pending for this client.
738 unsigned int message_count;
744 * Context of currently active requests to peerinfo
745 * for validation of HELLOs.
747 struct CheckHelloValidatedContext;
751 * Entry in map of all HELLOs awaiting validation.
753 struct ValidationEntry
757 * NULL if this entry is not part of a larger HELLO validation.
759 struct CheckHelloValidatedContext *chvc;
762 * The address, actually a pointer to the end
763 * of this struct. Do not free!
768 * Name of the transport.
770 char *transport_name;
773 * The public key of the peer.
775 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
778 * ID of task that will clean up this entry if we don't succeed
779 * with the validation first.
781 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
784 * At what time did we send this validation?
786 struct GNUNET_TIME_Absolute send_time;
789 * Session being validated (or NULL for none).
791 struct Session *session;
794 * Challenge number we used.
807 * Context of currently active requests to peerinfo
808 * for validation of HELLOs.
810 struct CheckHelloValidatedContext
814 * This is a doubly-linked list.
816 struct CheckHelloValidatedContext *next;
819 * This is a doubly-linked list.
821 struct CheckHelloValidatedContext *prev;
824 * Hello that we are validating.
826 const struct GNUNET_HELLO_Message *hello;
829 * Context for peerinfo iteration.
830 * NULL after we are done processing peerinfo's information.
832 struct GNUNET_PEERINFO_IteratorContext *piter;
835 * Was a HELLO known for this peer to peerinfo?
840 * Number of validation entries currently referring to this
843 unsigned int ve_count;
846 struct ATS_quality_metric
855 struct ATS_mechanism * prev;
856 struct ATS_mechanism * next;
857 struct ForeignAddressList * addr;
858 struct TransportPlugin * plugin;
859 struct ATS_peer * peer;
862 struct ATS_ressource_cost * rc;
868 struct GNUNET_PeerIdentity peer;
869 struct NeighbourList * n;
870 struct ATS_mechanism * m_head;
871 struct ATS_mechanism * m_tail;
873 /* preference value f */
893 struct ATS_ressource_entry
895 /* index in ressources array */
897 /* depending ATSi parameter to calculcate limits */
906 /* index in ressources array */
908 /* depending ATSi parameter to calculcate limits */
910 /* cfg option to load limits */
917 /* cofficients for the specific plugins */
927 static struct ATS_ressource ressources[] =
929 /* FIXME: the coefficients for the specific plugins */
930 {1, 7, "LAN_BW_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 1, 1, 2, 2, 1, 3},
931 {2, 7, "WAN_BW_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 1, 1, 2, 2, 2, 3},
932 {3, 4, "WLAN_ENERGY_LIMIT", 0, VERY_BIG_DOUBLE_VALUE, 0, 0, 0, 0, 0, 2, 1}
934 {4, 4, "COST_ENERGY_CONSUMPTION", VERY_BIG_DOUBLE_VALUE},
935 {5, 5, "COST_CONNECT", VERY_BIG_DOUBLE_VALUE},
936 {6, 6, "COST_BANDWITH_AVAILABLE", VERY_BIG_DOUBLE_VALUE},
937 {7, 7, "COST_NETWORK_OVERHEAD", VERY_BIG_DOUBLE_VALUE},*/
940 static int available_ressources = 3;
948 * Time of last execution
950 struct GNUNET_TIME_Absolute last;
952 * Minimum intervall between two executions
954 struct GNUNET_TIME_Relative min_delta;
956 * Regular intervall when execution is triggered
958 struct GNUNET_TIME_Relative exec_intervall;
960 * Maximum execution time per calculation
962 struct GNUNET_TIME_Relative max_exec_duration;
964 * Maximum number of LP iterations per calculation
972 * Ressource costs or quality metrics changed, update matrix
974 int modified_resources;
977 * Ressource costs or quality metrics changed, update matrix
979 int modified_quality;
982 * Peers have connected or disconnected, problem has to be recreated
992 GNUNET_SCHEDULER_TaskIdentifier ats_task;
994 struct ATS_result res;
999 struct ATS_mechanism * mechanisms;
1000 struct ATS_peer * peers;
1005 * Our HELLO message.
1007 static struct GNUNET_HELLO_Message *our_hello;
1012 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
1017 static struct GNUNET_PeerIdentity my_identity;
1022 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
1025 * Our configuration.
1027 const struct GNUNET_CONFIGURATION_Handle *cfg;
1030 * Linked list of all clients to this service.
1032 static struct TransportClient *clients;
1035 * All loaded plugins.
1037 static struct TransportPlugin *plugins;
1040 * Handle to peerinfo service.
1042 static struct GNUNET_PEERINFO_Handle *peerinfo;
1045 * All known neighbours and their HELLOs.
1047 static struct NeighbourList *neighbours;
1050 * Number of neighbours we'd like to have.
1052 static uint32_t max_connect_per_transport;
1055 * Head of linked list.
1057 static struct CheckHelloValidatedContext *chvc_head;
1060 * Tail of linked list.
1062 static struct CheckHelloValidatedContext *chvc_tail;
1065 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
1066 * of the given peer that we are currently validating).
1068 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
1071 * Handle for reporting statistics.
1073 static struct GNUNET_STATISTICS_Handle *stats;
1076 * Handle for ats information
1078 static struct ATS_info *ats;
1080 struct ATS_quality_entry
1088 static struct ATS_quality_metric qm[] =
1090 {1, 1028, "QUALITY_NET_DISTANCE"},
1091 {2, 1034, "QUALITY_NET_DELAY"},
1093 static int available_quality_metrics = 2;
1097 * The peer specified by the given neighbour has timed-out or a plugin
1098 * has disconnected. We may either need to do nothing (other plugins
1099 * still up), or trigger a full disconnect and clean up. This
1100 * function updates our state and do the necessary notifications.
1101 * Also notifies our clients that the neighbour is now officially
1104 * @param n the neighbour list entry for the peer
1105 * @param check should we just check if all plugins
1106 * disconnected or must we ask all plugins to
1109 static void disconnect_neighbour (struct NeighbourList *n, int check);
1112 * Check the ready list for the given neighbour and if a plugin is
1113 * ready for transmission (and if we have a message), do so!
1115 * @param nexi target peer for which to transmit
1117 static void try_transmission_to_peer (struct NeighbourList *n);
1119 static void ats_shutdown ( );
1121 static void ats_notify_peer_connect (
1122 const struct GNUNET_PeerIdentity *peer,
1123 const struct GNUNET_TRANSPORT_ATS_Information *ats_data);
1125 static void ats_notify_peer_disconnect (
1126 const struct GNUNET_PeerIdentity *peer);
1128 static void ats_notify_ats_data (
1129 const struct GNUNET_PeerIdentity *peer,
1130 const struct GNUNET_TRANSPORT_ATS_Information *ats_data);
1132 struct ForeignAddressList * ats_get_preferred_address (
1133 struct NeighbourList *n);
1136 ats_calculate_bandwidth_distribution ();
1139 * Find an entry in the neighbour list for a particular peer.
1141 * @return NULL if not found.
1143 static struct NeighbourList *
1144 find_neighbour (const struct GNUNET_PeerIdentity *key)
1146 struct NeighbourList *head = neighbours;
1148 while ((head != NULL) &&
1149 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
1154 static int update_addr_ats (struct ForeignAddressList *fal, const struct GNUNET_TRANSPORT_ATS_Information *ats_data, int ats_count)
1158 for (c1=0; c1<ats_count; c1++)
1160 for (c2=0; c2<available_quality_metrics; c2++)
1162 if (ntohl(ats_data[c1].type) == qm[c2].atis_index)
1164 /* shifting history */
1165 // fal->quality[c2].values[0] = fal->quality[c2].values[1];
1166 // fal->quality[c2].values[1] = fal->quality[c2].values[2];
1167 /* setting latest value */
1168 // fal->quality[c2].values[2] = ntohl(ats_data[c1].value);
1169 // set = GNUNET_YES;
1172 if (set == GNUNET_NO)
1174 for (c2=0; c2<available_ressources; c2++)
1176 if (ntohl(ats_data[c1].type) == ressources[c2].atis_index)
1178 //fal->ressources[c2].c = ntohl(ats_data[c1].value);
1187 static int update_addr_value (struct ForeignAddressList *fal, uint32_t value , int ats_index)
1190 int set = GNUNET_NO;
1191 for (c=0; c<available_quality_metrics; c++)
1193 if (ats_index == qm[c].atis_index)
1195 // fal->quality[c].values[0] = fal->quality[c].values[1];
1196 // fal->quality[c].values[1] = fal->quality[c].values[2];
1197 // fal->quality[c].values[2] = value;
1201 if (set == GNUNET_NO)
1203 for (c=0; c<available_ressources; c++)
1205 if (ats_index == ressources[c].atis_index)
1207 // fal->ressources[c].c = value;
1208 // set = GNUNET_YES;
1217 * Find an entry in the transport list for a particular transport.
1219 * @return NULL if not found.
1221 static struct TransportPlugin *
1222 find_transport (const char *short_name)
1224 struct TransportPlugin *head = plugins;
1225 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
1231 * Is a particular peer blacklisted for a particular transport?
1233 * @param peer the peer to check for
1234 * @param plugin the plugin used to connect to the peer
1236 * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
1239 is_blacklisted (const struct GNUNET_PeerIdentity *peer, struct TransportPlugin *plugin)
1242 if (plugin->blacklist != NULL)
1244 if (GNUNET_CONTAINER_multihashmap_contains (plugin->blacklist, &peer->hashPubKey) == GNUNET_YES)
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 "Peer `%s:%s' is blacklisted!\n",
1249 plugin->short_name, GNUNET_i2s (peer));
1252 GNUNET_STATISTICS_update (stats, "# blacklisted peers refused", 1, GNUNET_NO);
1262 add_peer_to_blacklist (struct GNUNET_PeerIdentity *peer, char *transport_name)
1264 struct TransportPlugin *plugin;
1266 plugin = find_transport(transport_name);
1267 if (plugin == NULL) /* Nothing to do */
1269 if (plugin->blacklist == NULL)
1270 plugin->blacklist = GNUNET_CONTAINER_multihashmap_create(TRANSPORT_BLACKLIST_HT_SIZE);
1271 GNUNET_assert(plugin->blacklist != NULL);
1272 GNUNET_CONTAINER_multihashmap_put(plugin->blacklist, &peer->hashPubKey,
1274 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1279 * Read the blacklist file, containing transport:peer entries.
1280 * Provided the transport is loaded, set up hashmap with these
1281 * entries to blacklist peers by transport.
1285 read_blacklist_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1292 struct GNUNET_PeerIdentity pid;
1294 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1295 unsigned int entries_found;
1296 char *transport_name;
1299 GNUNET_CONFIGURATION_get_value_filename (cfg,
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1306 "Option `%s' in section `%s' not specified!\n",
1312 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1313 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1314 | GNUNET_DISK_PERM_USER_WRITE);
1315 if (0 != STAT (fn, &frstat))
1317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1318 _("Could not read blacklist file `%s'\n"), fn);
1322 if (frstat.st_size == 0)
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1326 _("Blacklist file `%s' is empty.\n"),
1332 /* FIXME: use mmap */
1333 data = GNUNET_malloc_large (frstat.st_size);
1334 GNUNET_assert(data != NULL);
1335 if (frstat.st_size !=
1336 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1338 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1339 _("Failed to read blacklist from `%s'\n"), fn);
1346 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1348 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1349 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1352 while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') && !isspace ( (unsigned char) data[colon_pos]))
1355 if (colon_pos >= frstat.st_size)
1357 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1358 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1359 (unsigned long long) colon_pos);
1365 if (isspace( (unsigned char) data[colon_pos]))
1367 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1368 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1369 (unsigned long long) colon_pos);
1371 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1375 tsize = colon_pos - pos;
1376 if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size) || (tsize == 0))
1378 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1379 _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1380 (unsigned long long) colon_pos);
1389 transport_name = GNUNET_malloc(tsize + 1);
1390 memcpy(transport_name, &data[pos], tsize);
1391 pos = colon_pos + 1;
1393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1394 "Read transport name %s in blacklist file.\n",
1397 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1398 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1400 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1401 _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1402 (unsigned long long) pos);
1404 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1406 GNUNET_free_non_null(transport_name);
1409 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1410 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1412 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1413 _("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
1414 (unsigned long long) pos,
1419 if (0 != memcmp (&pid,
1421 sizeof (struct GNUNET_PeerIdentity)))
1424 add_peer_to_blacklist (&pid,
1429 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1430 _("Found myself `%s' in blacklist (useless, ignored)\n"),
1434 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1435 GNUNET_free_non_null(transport_name);
1436 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1439 GNUNET_STATISTICS_update (stats, "# Transport entries blacklisted", entries_found, GNUNET_NO);
1446 * Function called to notify a client about the socket being ready to
1447 * queue more data. "buf" will be NULL and "size" zero if the socket
1448 * was closed for writing in the meantime.
1450 * @param cls closure
1451 * @param size number of bytes available in buf
1452 * @param buf where the callee should write the message
1453 * @return number of bytes written to buf
1456 transmit_to_client_callback (void *cls, size_t size, void *buf)
1458 struct TransportClient *client = cls;
1459 struct ClientMessageQueueEntry *q;
1462 const struct GNUNET_MessageHeader *msg;
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1470 "Transmission to client failed, closing connection.\n");
1472 /* fatal error with client, free message queue! */
1473 while (NULL != (q = client->message_queue_head))
1475 GNUNET_STATISTICS_update (stats,
1476 gettext_noop ("# bytes discarded (could not transmit to client)"),
1477 ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
1479 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1480 client->message_queue_tail,
1484 client->message_count = 0;
1489 while (NULL != (q = client->message_queue_head))
1491 msg = (const struct GNUNET_MessageHeader *) &q[1];
1492 msize = ntohs (msg->size);
1493 if (msize + tsize > size)
1496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1497 "Transmitting message of type %u to client.\n",
1500 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1501 client->message_queue_tail,
1503 memcpy (&cbuf[tsize], msg, msize);
1506 client->message_count--;
1510 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1511 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1513 GNUNET_TIME_UNIT_FOREVER_REL,
1514 &transmit_to_client_callback,
1516 GNUNET_assert (client->th != NULL);
1523 * Convert an address to a string.
1525 * @param plugin name of the plugin responsible for the address
1526 * @param addr binary address
1527 * @param addr_len number of bytes in addr
1528 * @return NULL on error, otherwise address string
1531 a2s (const char *plugin,
1535 struct TransportPlugin *p;
1539 p = find_transport (plugin);
1542 return p->api->address_to_string (p->api->cls,
1549 * Mark the given FAL entry as 'connected' (and hence preferred for
1550 * sending); also mark all others for the same peer as 'not connected'
1551 * (since only one can be preferred).
1553 * @param fal address to set to 'connected'
1556 mark_address_connected (struct ForeignAddressList *fal)
1558 struct ForeignAddressList *pos;
1561 GNUNET_assert (GNUNET_YES == fal->validated);
1562 if (fal->connected == GNUNET_YES)
1563 return; /* nothing to do */
1565 pos = fal->ready_list->addresses;
1568 if (GNUNET_YES == pos->connected)
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1572 "Marking address `%s' as no longer connected (due to connect on other address)\n",
1573 a2s (pos->ready_list->plugin->short_name,
1577 GNUNET_break (cnt == GNUNET_YES);
1579 pos->connected = GNUNET_NO;
1580 GNUNET_STATISTICS_update (stats,
1581 gettext_noop ("# connected addresses"),
1587 fal->connected = GNUNET_YES;
1588 if (GNUNET_YES == cnt)
1590 GNUNET_STATISTICS_update (stats,
1591 gettext_noop ("# connected addresses"),
1599 * Send the specified message to the specified client. Since multiple
1600 * messages may be pending for the same client at a time, this code
1601 * makes sure that no message is lost.
1603 * @param client client to transmit the message to
1604 * @param msg the message to send
1605 * @param may_drop can this message be dropped if the
1606 * message queue for this client is getting far too large?
1609 transmit_to_client (struct TransportClient *client,
1610 const struct GNUNET_MessageHeader *msg, int may_drop)
1612 struct ClientMessageQueueEntry *q;
1615 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1617 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1619 ("Dropping message of type %u and size %u, have %u messages pending (%u is the soft limit)\n"),
1622 client->message_count,
1624 GNUNET_STATISTICS_update (stats,
1625 gettext_noop ("# messages dropped due to slow client"),
1630 msize = ntohs (msg->size);
1631 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1632 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1633 memcpy (&q[1], msg, msize);
1634 GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1635 client->message_queue_tail,
1636 client->message_queue_tail,
1638 client->message_count++;
1639 if (client->th == NULL)
1641 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1643 GNUNET_TIME_UNIT_FOREVER_REL,
1644 &transmit_to_client_callback,
1646 GNUNET_assert (client->th != NULL);
1652 * Transmit a 'SEND_OK' notification to the given client for the
1655 * @param client who to notify
1656 * @param n neighbour to notify about, can be NULL (on failure)
1657 * @param target target of the transmission
1658 * @param result status code for the transmission request
1661 transmit_send_ok (struct TransportClient *client,
1662 struct NeighbourList *n,
1663 const struct GNUNET_PeerIdentity *target,
1666 struct SendOkMessage send_ok_msg;
1668 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1669 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1670 send_ok_msg.success = htonl (result);
1672 send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1674 send_ok_msg.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
1675 send_ok_msg.peer = *target;
1676 transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1681 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1682 * upon "completion" of a send request. This tells the API
1683 * that it is now legal to send another message to the given
1686 * @param cls closure, identifies the entry on the
1687 * message queue that was transmitted and the
1688 * client responsible for queuing the message
1689 * @param target the peer receiving the message
1690 * @param result GNUNET_OK on success, if the transmission
1691 * failed, we should not tell the client to transmit
1695 transmit_send_continuation (void *cls,
1696 const struct GNUNET_PeerIdentity *target,
1699 struct MessageQueue *mq = cls;
1700 struct NeighbourList *n;
1702 GNUNET_STATISTICS_update (stats,
1703 gettext_noop ("# bytes pending with plugins"),
1704 - (int64_t) mq->message_buf_size,
1706 if (result == GNUNET_OK)
1708 GNUNET_STATISTICS_update (stats,
1709 gettext_noop ("# bytes successfully transmitted by plugins"),
1710 mq->message_buf_size,
1715 GNUNET_STATISTICS_update (stats,
1716 gettext_noop ("# bytes with transmission failure by plugins"),
1717 mq->message_buf_size,
1720 if (mq->specific_address != NULL)
1722 if (result == GNUNET_OK)
1724 mq->specific_address->timeout =
1725 GNUNET_TIME_relative_to_absolute
1726 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1727 if (mq->specific_address->validated == GNUNET_YES)
1728 mark_address_connected (mq->specific_address);
1732 if (mq->specific_address->connected != GNUNET_NO)
1735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1736 "Marking address `%s' as no longer connected (due to transmission problem)\n",
1737 a2s (mq->specific_address->ready_list->plugin->short_name,
1738 mq->specific_address->addr,
1739 mq->specific_address->addrlen));
1741 GNUNET_STATISTICS_update (stats,
1742 gettext_noop ("# connected addresses"),
1745 mq->specific_address->connected = GNUNET_NO;
1748 if (! mq->internal_msg)
1749 mq->specific_address->in_transmit = GNUNET_NO;
1751 n = find_neighbour(&mq->neighbour_id);
1752 if (mq->client != NULL)
1753 transmit_send_ok (mq->client, n, target, result);
1756 try_transmission_to_peer (n);
1761 * Find an address in any of the available transports for
1762 * the given neighbour that would be good for message
1763 * transmission. This is essentially the transport selection
1766 * @param neighbour for whom to select an address
1767 * @return selected address, NULL if we have none
1769 struct ForeignAddressList *
1770 find_ready_address(struct NeighbourList *neighbour)
1772 struct ReadyList *head = neighbour->plugins;
1773 struct ForeignAddressList *addresses;
1774 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1775 struct ForeignAddressList *best_address;
1777 /* Hack to prefer unix domain sockets */
1778 struct ForeignAddressList *unix_address = NULL;
1780 best_address = NULL;
1781 while (head != NULL)
1783 addresses = head->addresses;
1784 while (addresses != NULL)
1786 if ( (addresses->timeout.abs_value < now.abs_value) &&
1787 (addresses->connected == GNUNET_YES) )
1790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1791 "Marking long-time inactive connection to `%4s' as down.\n",
1792 GNUNET_i2s (&neighbour->id));
1794 GNUNET_STATISTICS_update (stats,
1795 gettext_noop ("# connected addresses"),
1798 addresses->connected = GNUNET_NO;
1800 addresses = addresses->next;
1803 addresses = head->addresses;
1804 while (addresses != NULL)
1806 #if DEBUG_TRANSPORT > 1
1807 if (addresses->addr != NULL)
1808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1809 "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
1810 a2s (head->plugin->short_name,
1812 addresses->addrlen),
1813 GNUNET_i2s (&neighbour->id),
1814 addresses->connected,
1815 addresses->in_transmit,
1816 addresses->validated,
1817 addresses->connect_attempts,
1818 (unsigned long long) addresses->timeout.abs_value,
1819 (unsigned int) addresses->distance);
1821 if (0==strcmp(head->plugin->short_name,"unix"))
1823 if ((unix_address == NULL) || ((unix_address != NULL) &&
1824 (addresses->latency.rel_value < unix_address->latency.rel_value)))
1825 unix_address = addresses;
1827 if ( ( (best_address == NULL) ||
1828 (addresses->connected == GNUNET_YES) ||
1829 (best_address->connected == GNUNET_NO) ) &&
1830 (addresses->in_transmit == GNUNET_NO) &&
1831 ( (best_address == NULL) ||
1832 (addresses->latency.rel_value < best_address->latency.rel_value)) )
1833 best_address = addresses;
1834 /* FIXME: also give lower-latency addresses that are not
1835 connected a chance some times... */
1836 addresses = addresses->next;
1838 if (unix_address != NULL)
1842 if (unix_address != NULL)
1844 best_address = unix_address;
1846 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found unix address, forced this address\n");
1849 if (best_address != NULL)
1853 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1854 "Best address found (`%s') has latency of %llu ms.\n",
1855 (best_address->addrlen > 0)
1856 ? a2s (best_address->ready_list->plugin->short_name,
1858 best_address->addrlen)
1860 best_address->latency.rel_value);
1865 GNUNET_STATISTICS_update (stats,
1866 gettext_noop ("# transmission attempts failed (no address)"),
1871 return best_address;
1877 * We should re-try transmitting to the given peer,
1878 * hopefully we've learned something in the meantime.
1881 retry_transmission_task (void *cls,
1882 const struct GNUNET_SCHEDULER_TaskContext *tc)
1884 struct NeighbourList *n = cls;
1886 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1887 try_transmission_to_peer (n);
1892 * Check the ready list for the given neighbour and if a plugin is
1893 * ready for transmission (and if we have a message), do so!
1895 * @param neighbour target peer for which to transmit
1898 try_transmission_to_peer (struct NeighbourList *n)
1900 struct ReadyList *rl;
1901 struct MessageQueue *mq;
1902 struct GNUNET_TIME_Relative timeout;
1906 if (n->messages_head == NULL)
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1910 "Transmission queue for `%4s' is empty\n",
1911 GNUNET_i2s (&n->id));
1913 return; /* nothing to do */
1916 mq = n->messages_head;
1917 force_address = GNUNET_YES;
1918 if (mq->specific_address == NULL)
1921 mq->specific_address = ats_get_preferred_address(n);
1922 GNUNET_STATISTICS_update (stats,
1923 gettext_noop ("# transport selected peer address freely"),
1926 force_address = GNUNET_NO;
1928 if (mq->specific_address == NULL)
1930 GNUNET_STATISTICS_update (stats,
1931 gettext_noop ("# transport failed to selected peer address"),
1934 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1935 if (timeout.rel_value == 0)
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1939 "No destination address available to transmit message of size %u to peer `%4s'\n",
1940 mq->message_buf_size,
1941 GNUNET_i2s (&mq->neighbour_id));
1943 GNUNET_STATISTICS_update (stats,
1944 gettext_noop ("# bytes in message queue for other peers"),
1945 - (int64_t) mq->message_buf_size,
1947 GNUNET_STATISTICS_update (stats,
1948 gettext_noop ("# bytes discarded (no destination address available)"),
1949 mq->message_buf_size,
1951 if (mq->client != NULL)
1952 transmit_send_ok (mq->client, n, &n->id, GNUNET_NO);
1953 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1957 return; /* nobody ready */
1959 GNUNET_STATISTICS_update (stats,
1960 gettext_noop ("# message delivery deferred (no address)"),
1963 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
1964 GNUNET_SCHEDULER_cancel (n->retry_task);
1965 n->retry_task = GNUNET_SCHEDULER_add_delayed (timeout,
1966 &retry_transmission_task,
1969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1970 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
1971 mq->message_buf_size,
1972 GNUNET_i2s (&mq->neighbour_id),
1975 /* FIXME: might want to trigger peerinfo lookup here
1976 (unless that's already pending...) */
1979 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1982 if (mq->specific_address->connected == GNUNET_NO)
1983 mq->specific_address->connect_attempts++;
1984 rl = mq->specific_address->ready_list;
1985 mq->plugin = rl->plugin;
1986 if (!mq->internal_msg)
1987 mq->specific_address->in_transmit = GNUNET_YES;
1989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1990 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
1991 mq->message_buf_size,
1992 GNUNET_i2s (&n->id),
1993 (mq->specific_address->addr != NULL)
1994 ? a2s (mq->plugin->short_name,
1995 mq->specific_address->addr,
1996 mq->specific_address->addrlen)
1998 rl->plugin->short_name);
2000 GNUNET_STATISTICS_update (stats,
2001 gettext_noop ("# bytes in message queue for other peers"),
2002 - (int64_t) mq->message_buf_size,
2004 GNUNET_STATISTICS_update (stats,
2005 gettext_noop ("# bytes pending with plugins"),
2006 mq->message_buf_size,
2008 ret = rl->plugin->api->send (rl->plugin->api->cls,
2011 mq->message_buf_size,
2013 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2014 mq->specific_address->session,
2015 mq->specific_address->addr,
2016 mq->specific_address->addrlen,
2018 &transmit_send_continuation, mq);
2021 /* failure, but 'send' would not call continuation in this case,
2022 so we need to do it here! */
2023 transmit_send_continuation (mq,
2031 * Send the specified message to the specified peer.
2033 * @param client source of the transmission request (can be NULL)
2034 * @param peer_address ForeignAddressList where we should send this message
2035 * @param priority how important is the message
2036 * @param timeout how long do we have to transmit?
2037 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
2038 * @param message_buf_size total size of all messages in message_buf
2039 * @param is_internal is this an internal message; these are pre-pended and
2040 * also do not count for plugins being "ready" to transmit
2041 * @param neighbour handle to the neighbour for transmission
2044 transmit_to_peer (struct TransportClient *client,
2045 struct ForeignAddressList *peer_address,
2046 unsigned int priority,
2047 struct GNUNET_TIME_Relative timeout,
2048 const char *message_buf,
2049 size_t message_buf_size,
2050 int is_internal, struct NeighbourList *neighbour)
2052 struct MessageQueue *mq;
2057 /* check for duplicate submission */
2058 mq = neighbour->messages_head;
2061 if (mq->client == client)
2063 /* client transmitted to same peer twice
2064 before getting SEND_OK! */
2072 GNUNET_STATISTICS_update (stats,
2073 gettext_noop ("# bytes in message queue for other peers"),
2076 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
2077 mq->specific_address = peer_address;
2078 mq->client = client;
2079 /* FIXME: this memcpy can be up to 7% of our total runtime! */
2080 memcpy (&mq[1], message_buf, message_buf_size);
2081 mq->message_buf = (const char*) &mq[1];
2082 mq->message_buf_size = message_buf_size;
2083 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
2084 mq->internal_msg = is_internal;
2085 mq->priority = priority;
2086 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
2088 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
2089 neighbour->messages_tail,
2092 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
2093 neighbour->messages_tail,
2094 neighbour->messages_tail,
2096 try_transmission_to_peer (neighbour);
2103 struct GeneratorContext
2105 struct TransportPlugin *plug_pos;
2106 struct OwnAddressList *addr_pos;
2107 struct GNUNET_TIME_Absolute expiration;
2115 address_generator (void *cls, size_t max, void *buf)
2117 struct GeneratorContext *gc = cls;
2120 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
2122 gc->plug_pos = gc->plug_pos->next;
2123 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
2125 if (NULL == gc->plug_pos)
2130 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
2133 gc->addr_pos->addrlen, buf, max);
2134 gc->addr_pos = gc->addr_pos->next;
2140 * Construct our HELLO message from all of the addresses of
2141 * all of the transports.
2146 struct GNUNET_HELLO_Message *hello;
2147 struct TransportClient *cpos;
2148 struct NeighbourList *npos;
2149 struct GeneratorContext gc;
2151 gc.plug_pos = plugins;
2152 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
2153 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2154 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
2156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2157 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
2159 GNUNET_STATISTICS_update (stats,
2160 gettext_noop ("# refreshed my HELLO"),
2164 while (cpos != NULL)
2166 transmit_to_client (cpos,
2167 (const struct GNUNET_MessageHeader *) hello,
2172 GNUNET_free_non_null (our_hello);
2174 GNUNET_PEERINFO_add_peer (peerinfo, our_hello);
2176 while (npos != NULL)
2179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2180 "Transmitting updated `%s' to neighbour `%4s'\n",
2181 "HELLO", GNUNET_i2s (&npos->id));
2183 GNUNET_STATISTICS_update (stats,
2184 gettext_noop ("# transmitted my HELLO to other peers"),
2187 transmit_to_peer (NULL, NULL, 0,
2188 HELLO_ADDRESS_EXPIRATION,
2189 (const char *) our_hello,
2190 GNUNET_HELLO_size(our_hello),
2198 * Task used to clean up expired addresses for a plugin.
2200 * @param cls closure
2204 expire_address_task (void *cls,
2205 const struct GNUNET_SCHEDULER_TaskContext *tc);
2209 * Update the list of addresses for this plugin,
2210 * expiring those that are past their expiration date.
2212 * @param plugin addresses of which plugin should be recomputed?
2213 * @param fresh set to GNUNET_YES if a new address was added
2214 * and we need to regenerate the HELLO even if nobody
2218 update_addresses (struct TransportPlugin *plugin,
2221 static struct GNUNET_TIME_Absolute last_update;
2222 struct GNUNET_TIME_Relative min_remaining;
2223 struct GNUNET_TIME_Relative remaining;
2224 struct GNUNET_TIME_Absolute now;
2225 struct OwnAddressList *pos;
2226 struct OwnAddressList *prev;
2227 struct OwnAddressList *next;
2230 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_TASK)
2231 GNUNET_SCHEDULER_cancel (plugin->address_update_task);
2232 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
2233 now = GNUNET_TIME_absolute_get ();
2234 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
2235 expired = (GNUNET_TIME_absolute_get_duration (last_update).rel_value > (HELLO_ADDRESS_EXPIRATION.rel_value / 4));
2237 pos = plugin->addresses;
2241 if (pos->expires.abs_value < now.abs_value)
2243 expired = GNUNET_YES;
2245 plugin->addresses = pos->next;
2247 prev->next = pos->next;
2252 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
2253 if (remaining.rel_value < min_remaining.rel_value)
2254 min_remaining = remaining;
2260 if (expired || fresh)
2265 min_remaining = GNUNET_TIME_relative_min (min_remaining,
2266 GNUNET_TIME_relative_divide (HELLO_ADDRESS_EXPIRATION,
2268 plugin->address_update_task
2269 = GNUNET_SCHEDULER_add_delayed (min_remaining,
2270 &expire_address_task, plugin);
2275 * Task used to clean up expired addresses for a plugin.
2277 * @param cls closure
2281 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2283 struct TransportPlugin *plugin = cls;
2285 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
2286 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2287 update_addresses (plugin, GNUNET_NO);
2292 * Iterator over hash map entries that NULLs the session of validation
2293 * entries that match the given session.
2295 * @param cls closure (the 'struct Session*' to match against)
2296 * @param key current key code (peer ID, not used)
2297 * @param value value in the hash map ('struct ValidationEntry*')
2298 * @return GNUNET_YES (we should continue to iterate)
2301 remove_session_validations (void *cls,
2302 const GNUNET_HashCode * key,
2305 struct Session *session = cls;
2306 struct ValidationEntry *ve = value;
2308 if (session == ve->session)
2315 * We've been disconnected from the other peer (for some
2316 * connection-oriented transport). Either quickly
2317 * re-establish the connection or signal the disconnect
2320 * Only signal CORE level disconnect if ALL addresses
2321 * for the peer are exhausted.
2323 * @param p overall plugin context
2324 * @param nl neighbour that was disconnected
2327 try_fast_reconnect (struct TransportPlugin *p,
2328 struct NeighbourList *nl)
2330 /* FIXME-MW: fast reconnect / transport switching not implemented... */
2331 /* Note: the idea here is to hide problems with transports (or
2332 switching between plugins) from the core to eliminate the need to
2333 re-negotiate session keys and the like; OTOH, we should tell core
2334 quickly (much faster than timeout) `if a connection was lost and
2335 could not be re-established (i.e. other peer went down or is
2336 unable / refuses to communicate);
2338 So we should consider:
2339 1) ideally: our own willingness / need to connect
2340 2) prior failures to connect to this peer (by plugin)
2341 3) ideally: reasons why other peer terminated (as far as knowable)
2343 Most importantly, it must be POSSIBLE for another peer to terminate
2344 a connection for a while (without us instantly re-establishing it).
2345 Similarly, if another peer is gone we should quickly notify CORE.
2346 OTOH, if there was a minor glitch (i.e. crash of gnunet-service-transport
2347 on the other end), we should reconnect in such a way that BOTH CORE
2348 services never even notice.
2349 Furthermore, the same mechanism (or small variation) could be used
2350 to switch to a better-performing plugin (ATS).
2352 Finally, this needs to be tested throughly... */
2355 * GNUNET_NO in the call below makes transport disconnect the peer,
2356 * even if only a single address (out of say, six) went away. This
2357 * function must be careful to ONLY disconnect if the peer is gone,
2358 * not just a specifi address.
2360 * More specifically, half the places it was used had it WRONG.
2363 /* No reconnect, signal disconnect instead! */
2364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2365 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2366 "try_fast_reconnect");
2367 disconnect_neighbour (nl, GNUNET_YES);
2372 * Function that will be called whenever the plugin internally
2373 * cleans up a session pointer and hence the service needs to
2374 * discard all of those sessions as well. Plugins that do not
2375 * use sessions can simply omit calling this function and always
2376 * use NULL wherever a session pointer is needed.
2378 * @param cls closure
2379 * @param peer which peer was the session for
2380 * @param session which session is being destoyed
2383 plugin_env_session_end (void *cls,
2384 const struct GNUNET_PeerIdentity *peer,
2385 struct Session *session)
2387 struct TransportPlugin *p = cls;
2388 struct NeighbourList *nl;
2389 struct ReadyList *rl;
2390 struct ForeignAddressList *pos;
2391 struct ForeignAddressList *prev;
2393 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2394 &remove_session_validations,
2396 nl = find_neighbour (peer);
2398 return; /* was never marked as connected */
2402 if (rl->plugin == p)
2407 return; /* was never marked as connected */
2409 pos = rl->addresses;
2410 while ( (pos != NULL) &&
2411 (pos->session != session) )
2417 return; /* was never marked as connected */
2418 pos->session = NULL;
2419 if (pos->addrlen != 0)
2421 if (nl->received_pong != GNUNET_NO)
2422 try_fast_reconnect (p, nl);
2425 /* was inbound connection, free 'pos' */
2427 rl->addresses = pos->next;
2429 prev->next = pos->next;
2430 if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2432 GNUNET_SCHEDULER_cancel (pos->revalidate_task);
2433 pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2436 if (nl->received_pong == GNUNET_NO)
2437 return; /* nothing to do, never connected... */
2438 /* check if we have any validated addresses left */
2439 pos = rl->addresses;
2444 try_fast_reconnect (p, nl);
2449 /* no valid addresses left, signal disconnect! */
2451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2452 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2453 "plugin_env_session_end");
2454 /* FIXME: This doesn't mean there are no addresses left for this PEER,
2455 * it means there aren't any left for this PLUGIN/PEER combination! So
2456 * calling disconnect_neighbor here with GNUNET_NO forces disconnect
2457 * when it isn't necessary. Using GNUNET_YES at least checks to see
2458 * if there are any addresses that work first, so as not to overdo it.
2461 disconnect_neighbour (nl, GNUNET_YES);
2466 * Function that must be called by each plugin to notify the
2467 * transport service about the addresses under which the transport
2468 * provided by the plugin can be reached.
2470 * @param cls closure
2471 * @param name name of the transport that generated the address
2472 * @param addr one of the addresses of the host, NULL for the last address
2473 * the specific address format depends on the transport
2474 * @param addrlen length of the address
2475 * @param expires when should this address automatically expire?
2478 plugin_env_notify_address (void *cls,
2482 struct GNUNET_TIME_Relative expires)
2484 struct TransportPlugin *p = cls;
2485 struct OwnAddressList *al;
2486 struct GNUNET_TIME_Absolute abex;
2488 GNUNET_assert (addr != NULL);
2489 abex = GNUNET_TIME_relative_to_absolute (expires);
2490 GNUNET_assert (p == find_transport (name));
2494 if ( (addrlen == al->addrlen) &&
2495 (0 == memcmp (addr, &al[1], addrlen)) )
2498 update_addresses (p, GNUNET_NO);
2503 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2504 al->next = p->addresses;
2507 al->addrlen = addrlen;
2508 memcpy (&al[1], addr, addrlen);
2509 update_addresses (p, GNUNET_YES);
2514 * Notify all of our clients about a peer connecting.
2517 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2518 struct GNUNET_TIME_Relative latency,
2521 struct ConnectInfoMessage * cim;
2522 struct TransportClient *cpos;
2527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2528 "Notifying clients about connection from `%s'\n",
2531 GNUNET_STATISTICS_update (stats,
2532 gettext_noop ("# peers connected"),
2537 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
2538 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2542 cim = GNUNET_malloc (size);
2544 cim->header.size = htons (size);
2545 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2546 cim->ats_count = htonl(2);
2547 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2548 (&(cim->ats))[0].value = htonl (distance);
2549 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
2550 (&(cim->ats))[1].value = htonl ((uint32_t) latency.rel_value);
2551 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2552 (&(cim->ats))[2].value = htonl (0);
2553 memcpy (&cim->id, peer, sizeof (struct GNUNET_PeerIdentity));
2555 /* notify ats about connecting peer */
2556 ats_notify_peer_connect (peer, &(cim->ats));
2559 while (cpos != NULL)
2561 transmit_to_client (cpos, &(cim->header), GNUNET_NO);
2570 * Notify all of our clients about a peer disconnecting.
2573 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2575 struct DisconnectInfoMessage dim;
2576 struct TransportClient *cpos;
2579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2580 "Notifying clients about lost connection to `%s'\n",
2583 GNUNET_STATISTICS_update (stats,
2584 gettext_noop ("# peers connected"),
2587 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2588 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2589 dim.reserved = htonl (0);
2590 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2592 /* notify ats about connecting peer */
2593 ats_notify_peer_disconnect (peer);
2596 while (cpos != NULL)
2598 transmit_to_client (cpos, &dim.header, GNUNET_NO);
2605 * Find a ForeignAddressList entry for the given neighbour
2606 * that matches the given address and transport.
2608 * @param neighbour which peer we care about
2609 * @param tname name of the transport plugin
2610 * @param session session to look for, NULL for 'any'; otherwise
2611 * can be used for the service to "learn" this session ID
2613 * @param addr binary address
2614 * @param addrlen length of addr
2615 * @return NULL if no such entry exists
2617 static struct ForeignAddressList *
2618 find_peer_address(struct NeighbourList *neighbour,
2620 struct Session *session,
2624 struct ReadyList *head;
2625 struct ForeignAddressList *pos;
2627 head = neighbour->plugins;
2628 while (head != NULL)
2630 if (0 == strcmp (tname, head->plugin->short_name))
2636 pos = head->addresses;
2637 while ( (pos != NULL) &&
2638 ( (pos->addrlen != addrlen) ||
2639 (memcmp(pos->addr, addr, addrlen) != 0) ) )
2641 if ( (session != NULL) &&
2642 (pos->session == session) )
2646 if ( (session != NULL) && (pos != NULL) )
2647 pos->session = session; /* learn it! */
2653 * Get the peer address struct for the given neighbour and
2654 * address. If it doesn't yet exist, create it.
2656 * @param neighbour which peer we care about
2657 * @param tname name of the transport plugin
2658 * @param session session of the plugin, or NULL for none
2659 * @param addr binary address
2660 * @param addrlen length of addr
2661 * @return NULL if we do not have a transport plugin for 'tname'
2663 static struct ForeignAddressList *
2664 add_peer_address (struct NeighbourList *neighbour,
2666 struct Session *session,
2670 struct ReadyList *head;
2671 struct ForeignAddressList *ret;
2674 ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2677 head = neighbour->plugins;
2679 while (head != NULL)
2681 if (0 == strcmp (tname, head->plugin->short_name))
2687 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2688 ret->session = session;
2689 if ((addrlen > 0) && (addr != NULL))
2691 ret->addr = (const char*) &ret[1];
2692 memcpy (&ret[1], addr, addrlen);
2699 ret->ressources = GNUNET_malloc(available_ressources * sizeof (struct ATS_ressource_entry));
2701 for (c=0; c<available_ressources; c++)
2703 struct ATS_ressource_entry *r = ret->ressources;
2705 r[c].atis_index = ressources[c].atis_index;
2706 if (0 == strcmp(neighbour->plugins->plugin->short_name,"unix"))
2708 r[c].c = ressources[c].c_unix;
2711 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"udp"))
2713 r[c].c = ressources[c].c_udp;
2716 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"tcp"))
2718 r[c].c = ressources[c].c_tcp;
2721 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"http"))
2723 r[c].c = ressources[c].c_http;
2726 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"https"))
2728 r[c].c = ressources[c].c_https;
2731 else if (0 == strcmp(neighbour->plugins->plugin->short_name,"wlan"))
2733 r[c].c = ressources[c].c_wlan;
2739 r[c].c = ressources[c].c_default;
2740 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"Assigning default cost to peer `%s' addr plugin `%s'! This should not happen!",
2741 GNUNET_i2s(&neighbour->peer), neighbour->plugins->plugin->short_name);
2745 ret->quality = GNUNET_malloc (available_quality_metrics * sizeof (struct ATS_quality_entry));
2746 ret->addrlen = addrlen;
2747 ret->expires = GNUNET_TIME_relative_to_absolute
2748 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2749 ret->latency = GNUNET_TIME_relative_get_forever();
2751 ret->timeout = GNUNET_TIME_relative_to_absolute
2752 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2753 ret->ready_list = head;
2754 ret->next = head->addresses;
2755 head->addresses = ret;
2761 * Closure for 'add_validated_address'.
2763 struct AddValidatedAddressContext
2766 * Entry that has been validated.
2768 const struct ValidationEntry *ve;
2771 * Flag set after we have added the address so
2772 * that we terminate the iteration next time.
2779 * Callback function used to fill a buffer of max bytes with a list of
2780 * addresses in the format used by HELLOs. Should use
2781 * "GNUNET_HELLO_add_address" as a helper function.
2783 * @param cls the 'struct AddValidatedAddressContext' with the validated address
2784 * @param max maximum number of bytes that can be written to buf
2785 * @param buf where to write the address information
2786 * @return number of bytes written, 0 to signal the
2787 * end of the iteration.
2790 add_validated_address (void *cls,
2791 size_t max, void *buf)
2793 struct AddValidatedAddressContext *avac = cls;
2794 const struct ValidationEntry *ve = avac->ve;
2796 if (GNUNET_YES == avac->done)
2798 avac->done = GNUNET_YES;
2799 return GNUNET_HELLO_add_address (ve->transport_name,
2800 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2810 * Closure for 'check_address_exists'.
2812 struct CheckAddressExistsClosure
2815 * Address to check for.
2820 * Name of the transport.
2827 struct Session *session;
2830 * Set to GNUNET_YES if the address exists.
2843 * Iterator over hash map entries. Checks if the given
2844 * validation entry is for the same address as what is given
2847 * @param cls the 'struct CheckAddressExistsClosure*'
2848 * @param key current key code (ignored)
2849 * @param value value in the hash map ('struct ValidationEntry')
2850 * @return GNUNET_YES if we should continue to
2851 * iterate (mismatch), GNUNET_NO if not (entry matched)
2854 check_address_exists (void *cls,
2855 const GNUNET_HashCode * key,
2858 struct CheckAddressExistsClosure *caec = cls;
2859 struct ValidationEntry *ve = value;
2861 if ( (0 == strcmp (caec->tname,
2862 ve->transport_name)) &&
2863 (caec->addrlen == ve->addrlen) &&
2864 (0 == memcmp (caec->addr,
2868 caec->exists = GNUNET_YES;
2871 if ( (ve->session != NULL) &&
2872 (caec->session == ve->session) )
2874 caec->exists = GNUNET_YES;
2883 * Iterator to free entries in the validation_map.
2885 * @param cls closure (unused)
2886 * @param key current key code
2887 * @param value value in the hash map (validation to abort)
2888 * @return GNUNET_YES (always)
2891 abort_validation (void *cls,
2892 const GNUNET_HashCode * key,
2895 struct ValidationEntry *va = value;
2897 if (GNUNET_SCHEDULER_NO_TASK != va->timeout_task)
2898 GNUNET_SCHEDULER_cancel (va->timeout_task);
2899 GNUNET_free (va->transport_name);
2900 if (va->chvc != NULL)
2902 va->chvc->ve_count--;
2903 if (va->chvc->ve_count == 0)
2905 GNUNET_CONTAINER_DLL_remove (chvc_head,
2908 GNUNET_free (va->chvc);
2918 * HELLO validation cleanup task (validation failed).
2920 * @param cls the 'struct ValidationEntry' that failed
2921 * @param tc scheduler context (unused)
2924 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2926 struct ValidationEntry *va = cls;
2927 struct GNUNET_PeerIdentity pid;
2929 va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2930 GNUNET_STATISTICS_update (stats,
2931 gettext_noop ("# address validation timeouts"),
2934 GNUNET_CRYPTO_hash (&va->publicKey,
2936 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2938 GNUNET_break (GNUNET_OK ==
2939 GNUNET_CONTAINER_multihashmap_remove (validation_map,
2942 abort_validation (NULL, NULL, va);
2947 neighbour_timeout_task (void *cls,
2948 const struct GNUNET_SCHEDULER_TaskContext *tc)
2950 struct NeighbourList *n = cls;
2953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2954 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
2956 GNUNET_STATISTICS_update (stats,
2957 gettext_noop ("# disconnects due to timeout"),
2960 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2961 disconnect_neighbour (n, GNUNET_NO);
2966 * Schedule the job that will cause us to send a PING to the
2967 * foreign address to evaluate its validity and latency.
2969 * @param fal address to PING
2972 schedule_next_ping (struct ForeignAddressList *fal);
2976 * Add the given address to the list of foreign addresses
2977 * available for the given peer (check for duplicates).
2979 * @param cls the respective 'struct NeighbourList' to update
2980 * @param tname name of the transport
2981 * @param expiration expiration time
2982 * @param addr the address
2983 * @param addrlen length of the address
2984 * @return GNUNET_OK (always)
2987 add_to_foreign_address_list (void *cls,
2989 struct GNUNET_TIME_Absolute expiration,
2993 struct NeighbourList *n = cls;
2994 struct ForeignAddressList *fal;
2997 GNUNET_STATISTICS_update (stats,
2998 gettext_noop ("# valid peer addresses returned by PEERINFO"),
3002 fal = find_peer_address (n, tname, NULL, addr, addrlen);
3005 #if DEBUG_TRANSPORT_HELLO
3006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3007 "Adding address `%s' (%s) for peer `%4s' due to PEERINFO data for %llums.\n",
3008 a2s (tname, addr, addrlen),
3010 GNUNET_i2s (&n->id),
3011 expiration.abs_value);
3013 fal = add_peer_address (n, tname, NULL, addr, addrlen);
3016 GNUNET_STATISTICS_update (stats,
3017 gettext_noop ("# previously validated addresses lacking transport"),
3023 fal->expires = GNUNET_TIME_absolute_max (expiration,
3025 schedule_next_ping (fal);
3031 fal->expires = GNUNET_TIME_absolute_max (expiration,
3036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3037 "Failed to add new address for `%4s'\n",
3038 GNUNET_i2s (&n->id));
3041 if (fal->validated == GNUNET_NO)
3043 fal->validated = GNUNET_YES;
3044 GNUNET_STATISTICS_update (stats,
3045 gettext_noop ("# peer addresses considered valid"),
3049 if (try == GNUNET_YES)
3051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3052 "Have new addresses, will try to trigger transmissions.\n");
3053 try_transmission_to_peer (n);
3060 * Add addresses in validated HELLO "h" to the set of addresses
3061 * we have for this peer.
3063 * @param cls closure ('struct NeighbourList*')
3064 * @param peer id of the peer, NULL for last call
3065 * @param h hello message for the peer (can be NULL)
3066 * @param err_msg NULL if successful, otherwise contains error message
3069 add_hello_for_peer (void *cls,
3070 const struct GNUNET_PeerIdentity *peer,
3071 const struct GNUNET_HELLO_Message *h,
3072 const char *err_msg)
3074 struct NeighbourList *n = cls;
3076 if (err_msg != NULL)
3078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3079 _("Error in communication with PEERINFO service\n"));
3084 GNUNET_STATISTICS_update (stats,
3085 gettext_noop ("# outstanding peerinfo iterate requests"),
3092 return; /* no HELLO available */
3094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3095 "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
3099 if (GNUNET_YES != n->public_key_valid)
3101 GNUNET_HELLO_get_key (h, &n->publicKey);
3102 n->public_key_valid = GNUNET_YES;
3104 GNUNET_HELLO_iterate_addresses (h,
3106 &add_to_foreign_address_list,
3112 * Create a fresh entry in our neighbour list for the given peer.
3113 * Will try to transmit our current HELLO to the new neighbour.
3114 * Do not call this function directly, use 'setup_peer_check_blacklist.
3116 * @param peer the peer for which we create the entry
3117 * @param do_hello should we schedule transmitting a HELLO
3118 * @return the new neighbour list entry
3120 static struct NeighbourList *
3121 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
3124 struct NeighbourList *n;
3125 struct TransportPlugin *tp;
3126 struct ReadyList *rl;
3129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3130 "Setting up state for neighbour `%4s'\n",
3133 GNUNET_assert (our_hello != NULL);
3134 GNUNET_STATISTICS_update (stats,
3135 gettext_noop ("# active neighbours"),
3138 n = GNUNET_malloc (sizeof (struct NeighbourList));
3139 n->next = neighbours;
3143 GNUNET_TIME_relative_to_absolute
3144 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3145 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
3146 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3147 MAX_BANDWIDTH_CARRY_S);
3151 if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
3153 rl = GNUNET_malloc (sizeof (struct ReadyList));
3155 rl->next = n->plugins;
3158 rl->addresses = NULL;
3162 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
3164 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
3165 &neighbour_timeout_task, n);
3168 GNUNET_STATISTICS_update (stats,
3169 gettext_noop ("# peerinfo new neighbor iterate requests"),
3172 GNUNET_STATISTICS_update (stats,
3173 gettext_noop ("# outstanding peerinfo iterate requests"),
3176 n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
3177 GNUNET_TIME_UNIT_FOREVER_REL,
3178 &add_hello_for_peer, n);
3180 GNUNET_STATISTICS_update (stats,
3181 gettext_noop ("# HELLO's sent to new neighbors"),
3184 transmit_to_peer (NULL, NULL, 0,
3185 HELLO_ADDRESS_EXPIRATION,
3186 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
3194 * Function called after we have checked if communicating
3195 * with a given peer is acceptable.
3197 * @param cls closure
3198 * @param n NULL if communication is not acceptable
3200 typedef void (*SetupContinuation)(void *cls,
3201 struct NeighbourList *n);
3205 * Information kept for each client registered to perform
3211 * This is a linked list.
3213 struct Blacklisters *next;
3216 * This is a linked list.
3218 struct Blacklisters *prev;
3221 * Client responsible for this entry.
3223 struct GNUNET_SERVER_Client *client;
3226 * Blacklist check that we're currently performing.
3228 struct BlacklistCheck *bc;
3234 * Head of DLL of blacklisting clients.
3236 static struct Blacklisters *bl_head;
3239 * Tail of DLL of blacklisting clients.
3241 static struct Blacklisters *bl_tail;
3245 * Context we use when performing a blacklist check.
3247 struct BlacklistCheck
3251 * This is a linked list.
3253 struct BlacklistCheck *next;
3256 * This is a linked list.
3258 struct BlacklistCheck *prev;
3261 * Peer being checked.
3263 struct GNUNET_PeerIdentity peer;
3266 * Option for setup neighbour afterwards.
3271 * Continuation to call with the result.
3273 SetupContinuation cont;
3281 * Current transmission request handle for this client, or NULL if no
3282 * request is pending.
3284 struct GNUNET_CONNECTION_TransmitHandle *th;
3287 * Our current position in the blacklisters list.
3289 struct Blacklisters *bl_pos;
3292 * Current task performing the check.
3294 GNUNET_SCHEDULER_TaskIdentifier task;
3299 * Head of DLL of active blacklisting queries.
3301 static struct BlacklistCheck *bc_head;
3304 * Tail of DLL of active blacklisting queries.
3306 static struct BlacklistCheck *bc_tail;
3310 * Perform next action in the blacklist check.
3312 * @param cls the 'struct BlacklistCheck*'
3316 do_blacklist_check (void *cls,
3317 const struct GNUNET_SCHEDULER_TaskContext *tc);
3320 * Transmit blacklist query to the client.
3322 * @param cls the 'struct BlacklistCheck'
3323 * @param size number of bytes allowed
3324 * @param buf where to copy the message
3325 * @return number of bytes copied to buf
3328 transmit_blacklist_message (void *cls,
3332 struct BlacklistCheck *bc = cls;
3333 struct Blacklisters *bl;
3334 struct BlacklistMessage bm;
3339 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
3340 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3345 bm.header.size = htons (sizeof (struct BlacklistMessage));
3346 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
3347 bm.is_allowed = htonl (0);
3349 memcpy (buf, &bm, sizeof (bm));
3350 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
3356 * Perform next action in the blacklist check.
3358 * @param cls the 'struct BlacklistCheck*'
3362 do_blacklist_check (void *cls,
3363 const struct GNUNET_SCHEDULER_TaskContext *tc)
3365 struct BlacklistCheck *bc = cls;
3366 struct Blacklisters *bl;
3368 bc->task = GNUNET_SCHEDULER_NO_TASK;
3372 bc->cont (bc->cont_cls,
3373 setup_new_neighbour (&bc->peer, bc->do_hello));
3380 bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
3381 sizeof (struct BlacklistMessage),
3382 GNUNET_TIME_UNIT_FOREVER_REL,
3383 &transmit_blacklist_message,
3390 * Obtain a 'struct NeighbourList' for the given peer. If such an entry
3391 * does not yet exist, check the blacklist. If the blacklist says creating
3392 * one is acceptable, create one and call the continuation; otherwise
3393 * call the continuation with NULL.
3395 * @param peer peer to setup or look up a struct NeighbourList for
3396 * @param do_hello should we also schedule sending our HELLO to the peer
3397 * if this is a new record
3398 * @param cont function to call with the 'struct NeigbhbourList*'
3399 * @param cont_cls closure for cont
3402 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
3404 SetupContinuation cont,
3407 struct NeighbourList *n;
3408 struct BlacklistCheck *bc;
3410 n = find_neighbour(peer);
3417 if (bl_head == NULL)
3420 cont (cont_cls, setup_new_neighbour (peer, do_hello));
3422 setup_new_neighbour(peer, do_hello);
3425 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3426 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3428 bc->do_hello = do_hello;
3430 bc->cont_cls = cont_cls;
3431 bc->bl_pos = bl_head;
3432 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3438 * Function called with the result of querying a new blacklister about
3439 * it being allowed (or not) to continue to talk to an existing neighbour.
3441 * @param cls the original 'struct NeighbourList'
3442 * @param n NULL if we need to disconnect
3445 confirm_or_drop_neighbour (void *cls,
3446 struct NeighbourList *n)
3448 struct NeighbourList * orig = cls;
3452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3453 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&orig->id),
3454 "confirm_or_drop_neighboUr");
3455 disconnect_neighbour (orig, GNUNET_NO);
3461 * Handle a request to start a blacklist.
3463 * @param cls closure (always NULL)
3464 * @param client identification of the client
3465 * @param message the actual message
3468 handle_blacklist_init (void *cls,
3469 struct GNUNET_SERVER_Client *client,
3470 const struct GNUNET_MessageHeader *message)
3472 struct Blacklisters *bl;
3473 struct BlacklistCheck *bc;
3474 struct NeighbourList *n;
3479 if (bl->client == client)
3482 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3487 bl = GNUNET_malloc (sizeof (struct Blacklisters));
3488 bl->client = client;
3489 GNUNET_SERVER_client_keep (client);
3490 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
3491 /* confirm that all existing connections are OK! */
3495 bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3496 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3498 bc->do_hello = GNUNET_NO;
3499 bc->cont = &confirm_or_drop_neighbour;
3502 if (n == neighbours) /* all would wait for the same client, no need to
3503 create more than just the first task right now */
3504 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3512 * Handle a request to blacklist a peer.
3514 * @param cls closure (always NULL)
3515 * @param client identification of the client
3516 * @param message the actual message
3519 handle_blacklist_reply (void *cls,
3520 struct GNUNET_SERVER_Client *client,
3521 const struct GNUNET_MessageHeader *message)
3523 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
3524 struct Blacklisters *bl;
3525 struct BlacklistCheck *bc;
3528 while ( (bl != NULL) &&
3529 (bl->client != client) )
3533 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3538 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
3540 bc->cont (bc->cont_cls, NULL);
3541 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
3546 bc->bl_pos = bc->bl_pos->next;
3547 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3550 /* check if any other bc's are waiting for this blacklister */
3554 if ( (bc->bl_pos == bl) &&
3555 (GNUNET_SCHEDULER_NO_TASK == bc->task) )
3556 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3564 * Send periodic PING messages to a given foreign address.
3566 * @param cls our 'struct PeriodicValidationContext*'
3567 * @param tc task context
3570 send_periodic_ping (void *cls,
3571 const struct GNUNET_SCHEDULER_TaskContext *tc)
3573 struct ForeignAddressList *peer_address = cls;
3574 struct TransportPlugin *tp;
3575 struct ValidationEntry *va;
3576 struct NeighbourList *neighbour;
3577 struct TransportPingMessage ping;
3578 struct CheckAddressExistsClosure caec;
3580 uint16_t hello_size;
3584 peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3585 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3587 tp = peer_address->ready_list->plugin;
3588 neighbour = peer_address->ready_list->neighbour;
3589 if (GNUNET_YES != neighbour->public_key_valid)
3591 /* no public key yet, try again later */
3592 schedule_next_ping (peer_address);
3595 caec.addr = peer_address->addr;
3596 caec.addrlen = peer_address->addrlen;
3597 caec.tname = tp->short_name;
3598 caec.session = peer_address->session;
3599 caec.exists = GNUNET_NO;
3600 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3601 &check_address_exists,
3603 if (caec.exists == GNUNET_YES)
3605 /* During validation attempts we will likely trigger the other
3606 peer trying to validate our address which in turn will cause
3607 it to send us its HELLO, so we expect to hit this case rather
3608 frequently. Only print something if we are very verbose. */
3609 #if DEBUG_TRANSPORT > 1
3610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3611 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3612 (peer_address->addr != NULL)
3613 ? a2s (tp->short_name,
3615 peer_address->addrlen)
3618 GNUNET_i2s (&neighbour->id));
3620 schedule_next_ping (peer_address);
3623 va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3624 va->transport_name = GNUNET_strdup (tp->short_name);
3625 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
3627 va->send_time = GNUNET_TIME_absolute_get();
3628 va->session = peer_address->session;
3629 if (peer_address->addr != NULL)
3631 va->addr = (const void*) &va[1];
3632 memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3633 va->addrlen = peer_address->addrlen;
3635 memcpy(&va->publicKey,
3636 &neighbour->publicKey,
3637 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3639 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
3640 &timeout_hello_validation,
3642 GNUNET_CONTAINER_multihashmap_put (validation_map,
3643 &neighbour->id.hashPubKey,
3645 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3647 if (peer_address->validated != GNUNET_YES)
3648 hello_size = GNUNET_HELLO_size(our_hello);
3652 tsize = sizeof(struct TransportPingMessage) + hello_size;
3654 if (peer_address->addr != NULL)
3656 slen = strlen (tp->short_name) + 1;
3657 tsize += slen + peer_address->addrlen;
3661 slen = 0; /* make gcc happy */
3663 message_buf = GNUNET_malloc(tsize);
3664 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3665 ping.challenge = htonl(va->challenge);
3666 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3667 if (peer_address->validated != GNUNET_YES)
3669 memcpy(message_buf, our_hello, hello_size);
3672 if (peer_address->addr != NULL)
3674 ping.header.size = htons(sizeof(struct TransportPingMessage) +
3675 peer_address->addrlen +
3677 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3680 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3682 peer_address->addrlen);
3686 ping.header.size = htons(sizeof(struct TransportPingMessage));
3689 memcpy(&message_buf[hello_size],
3691 sizeof(struct TransportPingMessage));
3693 #if DEBUG_TRANSPORT_REVALIDATION
3694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3695 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s'\n",
3696 (peer_address->addr != NULL)
3697 ? a2s (peer_address->plugin->short_name,
3699 peer_address->addrlen)
3702 GNUNET_i2s (&neighbour->id),
3703 "HELLO", hello_size,
3706 if (peer_address->validated != GNUNET_YES)
3707 GNUNET_STATISTICS_update (stats,
3708 gettext_noop ("# PING with HELLO messages sent"),
3712 GNUNET_STATISTICS_update (stats,
3713 gettext_noop ("# PING without HELLO messages sent"),
3716 GNUNET_STATISTICS_update (stats,
3717 gettext_noop ("# PING messages sent for re-validation"),
3720 transmit_to_peer (NULL, peer_address,
3721 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3722 HELLO_VERIFICATION_TIMEOUT,
3724 GNUNET_YES, neighbour);
3725 GNUNET_free(message_buf);
3726 schedule_next_ping (peer_address);
3731 * Schedule the job that will cause us to send a PING to the
3732 * foreign address to evaluate its validity and latency.
3734 * @param fal address to PING
3737 schedule_next_ping (struct ForeignAddressList *fal)
3739 struct GNUNET_TIME_Relative delay;
3741 if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3743 delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3744 delay.rel_value /= 2; /* do before expiration */
3745 delay = GNUNET_TIME_relative_min (delay,
3746 LATENCY_EVALUATION_MAX_DELAY);
3747 if (GNUNET_YES != fal->estimated)
3749 delay = GNUNET_TIME_UNIT_ZERO;
3750 fal->estimated = GNUNET_YES;
3752 if (GNUNET_YES == fal->connected)
3754 delay = GNUNET_TIME_relative_min (delay,
3755 CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3757 /* FIXME: also adjust delay based on how close the last
3758 observed latency is to the latency of the best alternative */
3759 /* bound how fast we can go */
3760 delay = GNUNET_TIME_relative_max (delay,
3761 GNUNET_TIME_UNIT_SECONDS);
3762 /* randomize a bit (to avoid doing all at the same time) */
3763 delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3764 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(delay,
3765 &send_periodic_ping,
3773 * Function that will be called if we receive some payload
3774 * from another peer.
3776 * @param message the payload
3777 * @param n peer who claimed to be the sender
3780 handle_payload_message (const struct GNUNET_MessageHeader *message,
3781 struct NeighbourList *n)
3783 struct InboundMessage *im;
3784 struct TransportClient *cpos;
3787 msize = ntohs (message->size);
3788 if (n->received_pong == GNUNET_NO)
3790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3791 "Received message of type %u and size %u from `%4s', but no pong yet!!\n",
3792 ntohs (message->type),
3793 ntohs (message->size),
3794 GNUNET_i2s (&n->id));
3795 GNUNET_free_non_null (n->pre_connect_message_buffer);
3796 n->pre_connect_message_buffer = GNUNET_malloc (msize);
3797 memcpy (n->pre_connect_message_buffer, message, msize);
3802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3803 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
3804 ntohs (message->type),
3805 ntohs (message->size),
3806 GNUNET_i2s (&n->id));
3808 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3811 n->quota_violation_count++;
3813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3814 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
3815 n->in_tracker.available_bytes_per_s__,
3816 n->quota_violation_count);
3818 /* Discount 32k per violation */
3819 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3824 if (n->quota_violation_count > 0)
3826 /* try to add 32k back */
3827 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3829 n->quota_violation_count--;
3832 GNUNET_STATISTICS_update (stats,
3833 gettext_noop ("# payload received from other peers"),
3836 /* transmit message to all clients */
3837 uint32_t ats_count = 2;
3838 size_t size = sizeof (struct InboundMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) + msize;
3839 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
3842 im = GNUNET_malloc (size);
3843 im->header.size = htons (size);
3844 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3846 im->ats_count = htonl(ats_count);
3847 /* Setting ATS data */
3848 (&(im->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
3849 (&(im->ats))[0].value = htonl (n->distance);
3850 (&(im->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
3851 (&(im->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
3852 (&(im->ats))[ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
3853 (&(im->ats))[ats_count].value = htonl (0);
3855 memcpy (&((&(im->ats))[ats_count+1]), message, msize);
3857 while (cpos != NULL)
3859 transmit_to_client (cpos, &im->header, GNUNET_YES);
3867 * Iterator over hash map entries. Checks if the given validation
3868 * entry is for the same challenge as what is given in the PONG.
3870 * @param cls the 'struct TransportPongMessage*'
3871 * @param key peer identity
3872 * @param value value in the hash map ('struct ValidationEntry')
3873 * @return GNUNET_YES if we should continue to
3874 * iterate (mismatch), GNUNET_NO if not (entry matched)
3877 check_pending_validation (void *cls,
3878 const GNUNET_HashCode * key,
3881 const struct TransportPongMessage *pong = cls;
3882 struct ValidationEntry *ve = value;
3883 struct AddValidatedAddressContext avac;
3884 unsigned int challenge = ntohl(pong->challenge);
3885 struct GNUNET_HELLO_Message *hello;
3886 struct GNUNET_PeerIdentity target;
3887 struct NeighbourList *n;
3888 struct ForeignAddressList *fal;
3889 struct OwnAddressList *oal;
3890 struct TransportPlugin *tp;
3891 struct GNUNET_MessageHeader *prem;
3897 ps = ntohs (pong->header.size);
3898 if (ps < sizeof (struct TransportPongMessage))
3900 GNUNET_break_op (0);
3903 addr = (const char*) &pong[1];
3904 slen = strlen (ve->transport_name) + 1;
3905 if ( (ps - sizeof (struct TransportPongMessage) < slen) ||
3906 (ve->challenge != challenge) ||
3907 (addr[slen-1] != '\0') ||
3908 (0 != strcmp (addr, ve->transport_name)) ||
3909 (ntohl (pong->purpose.size)
3910 != sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3912 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
3913 sizeof (struct GNUNET_PeerIdentity) + ps - sizeof (struct TransportPongMessage)) )
3918 alen = ps - sizeof (struct TransportPongMessage) - slen;
3919 switch (ntohl (pong->purpose.purpose))
3921 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN:
3922 if ( (ve->addrlen + slen != ntohl (pong->addrlen)) ||
3923 (0 != memcmp (&addr[slen],
3927 return GNUNET_YES; /* different entry, keep trying! */
3929 if (0 != memcmp (&pong->pid,
3931 sizeof (struct GNUNET_PeerIdentity)))
3933 GNUNET_break_op (0);
3937 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
3942 GNUNET_break_op (0);
3947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3948 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
3950 a2s (ve->transport_name,
3951 (const struct sockaddr *) ve->addr,
3953 ve->transport_name);
3956 case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING:
3957 if (0 != memcmp (&pong->pid,
3959 sizeof (struct GNUNET_PeerIdentity)))
3961 GNUNET_break_op (0);
3964 if (ve->addrlen != 0)
3966 /* must have been for a different validation entry */
3969 tp = find_transport (ve->transport_name);
3975 oal = tp->addresses;
3978 if ( (oal->addrlen == alen) &&
3979 (0 == memcmp (&oal[1],
3987 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3988 _("Not accepting PONG with address `%s' since I cannot confirm having this address.\n"),
3989 a2s (ve->transport_name,
3995 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING,
4000 GNUNET_break_op (0);
4005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4006 "Confirmed that peer `%4s' is talking to us using address `%s' (%s) for us.\n",
4008 a2s (ve->transport_name,
4011 ve->transport_name);
4015 GNUNET_break_op (0);
4018 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
4020 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4021 _("Received expired signature. Check system time.\n"));
4024 GNUNET_STATISTICS_update (stats,
4025 gettext_noop ("# address validation successes"),
4028 /* create the updated HELLO */
4029 GNUNET_CRYPTO_hash (&ve->publicKey,
4030 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4031 &target.hashPubKey);
4032 if (ve->addr != NULL)
4034 avac.done = GNUNET_NO;
4036 hello = GNUNET_HELLO_create (&ve->publicKey,
4037 &add_validated_address,
4039 GNUNET_PEERINFO_add_peer (peerinfo,
4041 GNUNET_free (hello);
4043 n = find_neighbour (&target);
4046 n->publicKey = ve->publicKey;
4047 n->public_key_valid = GNUNET_YES;
4048 fal = add_peer_address (n,
4053 GNUNET_assert (fal != NULL);
4054 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
4055 fal->validated = GNUNET_YES;
4056 mark_address_connected (fal);
4057 GNUNET_STATISTICS_update (stats,
4058 gettext_noop ("# peer addresses considered valid"),
4061 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
4062 update_addr_value (fal, GNUNET_TIME_absolute_get_duration (ve->send_time).rel_value, GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
4064 schedule_next_ping (fal);
4065 if (n->latency.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
4066 n->latency = fal->latency;
4068 n->latency.rel_value = (fal->latency.rel_value + n->latency.rel_value) / 2;
4070 n->distance = fal->distance;
4071 if (GNUNET_NO == n->received_pong)
4073 n->received_pong = GNUNET_YES;
4075 notify_clients_connect (&target, n->latency, n->distance);
4076 if (NULL != (prem = n->pre_connect_message_buffer))
4078 n->pre_connect_message_buffer = NULL;
4079 handle_payload_message (prem, n);
4083 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4085 GNUNET_SCHEDULER_cancel (n->retry_task);
4086 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4087 try_transmission_to_peer (n);
4091 /* clean up validation entry */
4092 GNUNET_assert (GNUNET_YES ==
4093 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4096 abort_validation (NULL, NULL, ve);
4102 * Function that will be called if we receive a validation
4103 * of an address challenge that we transmitted to another
4104 * peer. Note that the validation should only be considered
4105 * acceptable if the challenge matches AND if the sender
4106 * address is at least a plausible address for this peer
4107 * (otherwise we may be seeing a MiM attack).
4109 * @param cls closure
4110 * @param message the pong message
4111 * @param peer who responded to our challenge
4112 * @param sender_address string describing our sender address (as observed
4113 * by the other peer in binary format)
4114 * @param sender_address_len number of bytes in 'sender_address'
4117 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
4118 const struct GNUNET_PeerIdentity *peer,
4119 const char *sender_address,
4120 size_t sender_address_len)
4122 #if DEBUG_TRANSPORT > 1
4123 /* we get tons of these that just get discarded, only log
4124 if we are quite verbose */
4125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4126 "Receiving `%s' message from `%4s'.\n", "PONG",
4129 GNUNET_STATISTICS_update (stats,
4130 gettext_noop ("# PONG messages received"),
4133 if (GNUNET_SYSERR !=
4134 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
4136 &check_pending_validation,
4139 /* This is *expected* to happen a lot since we send
4140 PONGs to *all* known addresses of the sender of
4141 the PING, so most likely we get multiple PONGs
4142 per PING, and all but the first PONG will end up
4143 here. So really we should not print anything here
4144 unless we want to be very, very verbose... */
4145 #if DEBUG_TRANSPORT > 2
4146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4147 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
4159 * Try to validate a neighbour's address by sending him our HELLO and a PING.
4161 * @param cls the 'struct ValidationEntry*'
4162 * @param neighbour neighbour to validate, NULL if validation failed
4165 transmit_hello_and_ping (void *cls,
4166 struct NeighbourList *neighbour)
4168 struct ValidationEntry *va = cls;
4169 struct ForeignAddressList *peer_address;
4170 struct TransportPingMessage ping;
4171 uint16_t hello_size;
4174 struct GNUNET_PeerIdentity id;
4177 GNUNET_CRYPTO_hash (&va->publicKey,
4178 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4180 if (neighbour == NULL)
4182 /* FIXME: stats... */
4183 GNUNET_break (GNUNET_OK ==
4184 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4187 abort_validation (NULL, NULL, va);
4190 neighbour->publicKey = va->publicKey;
4191 neighbour->public_key_valid = GNUNET_YES;
4192 peer_address = add_peer_address (neighbour,
4193 va->transport_name, NULL,
4194 (const void*) &va[1],
4196 if (peer_address == NULL)
4198 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4199 "Failed to add peer `%4s' for plugin `%s'\n",
4200 GNUNET_i2s (&neighbour->id),
4201 va->transport_name);
4202 GNUNET_break (GNUNET_OK ==
4203 GNUNET_CONTAINER_multihashmap_remove (validation_map,
4206 abort_validation (NULL, NULL, va);
4209 hello_size = GNUNET_HELLO_size(our_hello);
4210 slen = strlen(va->transport_name) + 1;
4211 tsize = sizeof(struct TransportPingMessage) + hello_size + va->addrlen + slen;
4212 message_buf = GNUNET_malloc(tsize);
4213 ping.challenge = htonl(va->challenge);
4214 ping.header.size = htons(sizeof(struct TransportPingMessage) + slen + va->addrlen);
4215 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
4216 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
4217 memcpy(message_buf, our_hello, hello_size);
4218 memcpy(&message_buf[hello_size],
4220 sizeof(struct TransportPingMessage));
4221 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
4224 memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
4228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4229 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
4232 : a2s (va->transport_name,
4233 (const void*) &va[1], va->addrlen),
4235 GNUNET_i2s (&neighbour->id),
4236 "HELLO", hello_size,
4237 "PING", sizeof (struct TransportPingMessage) + va->addrlen + slen);
4240 GNUNET_STATISTICS_update (stats,
4241 gettext_noop ("# PING messages sent for initial validation"),
4244 transmit_to_peer (NULL, peer_address,
4245 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
4246 HELLO_VERIFICATION_TIMEOUT,
4248 GNUNET_YES, neighbour);
4249 GNUNET_free(message_buf);
4254 * Check if the given address is already being validated; if not,
4255 * append the given address to the list of entries that are being be
4256 * validated and initiate validation.
4258 * @param cls closure ('struct CheckHelloValidatedContext *')
4259 * @param tname name of the transport
4260 * @param expiration expiration time
4261 * @param addr the address
4262 * @param addrlen length of the address
4263 * @return GNUNET_OK (always)
4266 run_validation (void *cls,
4268 struct GNUNET_TIME_Absolute expiration,
4272 struct CheckHelloValidatedContext *chvc = cls;
4273 struct GNUNET_PeerIdentity id;
4274 struct TransportPlugin *tp;
4275 struct ValidationEntry *va;
4276 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4277 struct CheckAddressExistsClosure caec;
4278 struct OwnAddressList *oal;
4280 GNUNET_assert (addr != NULL);
4282 GNUNET_STATISTICS_update (stats,
4283 gettext_noop ("# peer addresses scheduled for validation"),
4286 tp = find_transport (tname);
4289 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
4290 GNUNET_ERROR_TYPE_BULK,
4292 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
4294 GNUNET_STATISTICS_update (stats,
4295 gettext_noop ("# peer addresses not validated (plugin not available)"),
4300 /* check if this is one of our own addresses */
4301 oal = tp->addresses;
4304 if ( (oal->addrlen == addrlen) &&
4305 (0 == memcmp (&oal[1],
4309 /* not plausible, this address is equivalent to our own address! */
4310 GNUNET_STATISTICS_update (stats,
4311 gettext_noop ("# peer addresses not validated (loopback)"),
4318 GNUNET_HELLO_get_key (chvc->hello, &pk);
4319 GNUNET_CRYPTO_hash (&pk,
4321 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4324 if (is_blacklisted(&id, tp))
4327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4328 "Attempted to validate blacklisted peer `%s' using `%s'!\n",
4336 caec.addrlen = addrlen;
4337 caec.session = NULL;
4339 caec.exists = GNUNET_NO;
4340 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
4341 &check_address_exists,
4343 if (caec.exists == GNUNET_YES)
4345 /* During validation attempts we will likely trigger the other
4346 peer trying to validate our address which in turn will cause
4347 it to send us its HELLO, so we expect to hit this case rather
4348 frequently. Only print something if we are very verbose. */
4349 #if DEBUG_TRANSPORT > 1
4350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4351 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
4352 a2s (tname, addr, addrlen),
4356 GNUNET_STATISTICS_update (stats,
4357 gettext_noop ("# peer addresses not validated (in progress)"),
4362 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
4365 va->transport_name = GNUNET_strdup (tname);
4366 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
4368 va->send_time = GNUNET_TIME_absolute_get();
4369 va->addr = (const void*) &va[1];
4370 memcpy (&va[1], addr, addrlen);
4371 va->addrlen = addrlen;
4372 GNUNET_HELLO_get_key (chvc->hello,
4374 va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
4375 &timeout_hello_validation,
4377 GNUNET_CONTAINER_multihashmap_put (validation_map,
4380 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4381 setup_peer_check_blacklist (&id, GNUNET_NO,
4382 &transmit_hello_and_ping,
4389 * Check if addresses in validated hello "h" overlap with
4390 * those in "chvc->hello" and validate the rest.
4392 * @param cls closure
4393 * @param peer id of the peer, NULL for last call
4394 * @param h hello message for the peer (can be NULL)
4395 * @param err_msg NULL if successful, otherwise contains error message
4398 check_hello_validated (void *cls,
4399 const struct GNUNET_PeerIdentity *peer,
4400 const struct GNUNET_HELLO_Message *h,
4401 const char *err_msg)
4403 struct CheckHelloValidatedContext *chvc = cls;
4404 struct GNUNET_HELLO_Message *plain_hello;
4405 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4406 struct GNUNET_PeerIdentity target;
4407 struct NeighbourList *n;
4409 if (err_msg != NULL)
4411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4412 _("Error in communication with PEERINFO service\n"));
4418 GNUNET_STATISTICS_update (stats,
4419 gettext_noop ("# outstanding peerinfo iterate requests"),
4423 if (GNUNET_NO == chvc->hello_known)
4425 /* notify PEERINFO about the peer now, so that we at least
4426 have the public key if some other component needs it */
4427 GNUNET_HELLO_get_key (chvc->hello, &pk);
4428 GNUNET_CRYPTO_hash (&pk,
4429 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4430 &target.hashPubKey);
4431 plain_hello = GNUNET_HELLO_create (&pk,
4434 GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
4435 GNUNET_free (plain_hello);
4436 #if DEBUG_TRANSPORT_HELLO
4437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4438 "PEERINFO had no `%s' message for peer `%4s', full validation needed.\n",
4440 GNUNET_i2s (&target));
4442 GNUNET_STATISTICS_update (stats,
4443 gettext_noop ("# new HELLOs requiring full validation"),
4446 GNUNET_HELLO_iterate_addresses (chvc->hello,
4453 GNUNET_STATISTICS_update (stats,
4454 gettext_noop ("# duplicate HELLO (peer known)"),
4459 if (chvc->ve_count == 0)
4461 GNUNET_CONTAINER_DLL_remove (chvc_head,
4470 #if DEBUG_TRANSPORT_HELLO
4471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4472 "PEERINFO had `%s' message for peer `%4s', validating only new addresses.\n",
4476 chvc->hello_known = GNUNET_YES;
4477 n = find_neighbour (peer);
4480 #if DEBUG_TRANSPORT_HELLO
4481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4482 "Calling hello_iterate_addresses for %s!\n",
4485 GNUNET_HELLO_iterate_addresses (h,
4487 &add_to_foreign_address_list,
4489 try_transmission_to_peer (n);
4493 #if DEBUG_TRANSPORT_HELLO
4494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4495 "No existing neighbor record for %s!\n",
4498 GNUNET_STATISTICS_update (stats,
4499 gettext_noop ("# no existing neighbour record (validating HELLO)"),
4503 GNUNET_STATISTICS_update (stats,
4504 gettext_noop ("# HELLO validations (update case)"),
4507 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
4509 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
4516 * Process HELLO-message.
4518 * @param plugin transport involved, may be NULL
4519 * @param message the actual message
4520 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
4523 process_hello (struct TransportPlugin *plugin,
4524 const struct GNUNET_MessageHeader *message)
4527 struct GNUNET_PeerIdentity target;
4528 const struct GNUNET_HELLO_Message *hello;
4529 struct CheckHelloValidatedContext *chvc;
4530 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
4531 #if DEBUG_TRANSPORT_HELLO > 2
4534 hsize = ntohs (message->size);
4535 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
4536 (hsize < sizeof (struct GNUNET_MessageHeader)))
4539 return GNUNET_SYSERR;
4541 GNUNET_STATISTICS_update (stats,
4542 gettext_noop ("# HELLOs received for validation"),
4546 /* first, check if load is too high */
4547 if (GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
4549 GNUNET_STATISTICS_update (stats,
4550 gettext_noop ("# HELLOs ignored due to high load"),
4553 #if DEBUG_TRANSPORT_HELLO
4554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4555 "Ignoring `%s' for `%4s', load too high.\n",
4557 GNUNET_i2s (&target));
4561 hello = (const struct GNUNET_HELLO_Message *) message;
4562 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
4564 #if DEBUG_TRANSPORT_HELLO
4565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4566 "Unable to get public key from `%s' for `%4s'!\n",
4568 GNUNET_i2s (&target));
4570 GNUNET_break_op (0);
4571 return GNUNET_SYSERR;
4574 GNUNET_CRYPTO_hash (&publicKey,
4575 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4576 &target.hashPubKey);
4578 #if DEBUG_TRANSPORT_HELLO
4579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4580 "Received `%s' message for `%4s'\n",
4582 GNUNET_i2s (&target));
4585 if (0 == memcmp (&my_identity,
4587 sizeof (struct GNUNET_PeerIdentity)))
4589 GNUNET_STATISTICS_update (stats,
4590 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
4596 while (NULL != chvc)
4598 if (GNUNET_HELLO_equals (hello,
4600 GNUNET_TIME_absolute_get ()).abs_value > 0)
4602 #if DEBUG_TRANSPORT_HELLO > 2
4603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4604 "Received duplicate `%s' message for `%4s'; ignored\n",
4606 GNUNET_i2s (&target));
4608 return GNUNET_OK; /* validation already pending */
4610 if (GNUNET_HELLO_size(hello) == GNUNET_HELLO_size (chvc->hello))
4611 GNUNET_break (0 != memcmp (hello, chvc->hello,
4612 GNUNET_HELLO_size(hello)));
4617 struct NeighbourList *temp_neighbor = find_neighbour(&target);
4618 if ((NULL != temp_neighbor))
4620 fprintf(stderr, "Already know peer, ignoring hello\n");
4625 #if DEBUG_TRANSPORT_HELLO > 2
4628 my_id = GNUNET_strdup(GNUNET_i2s(plugin->env.my_identity));
4629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4630 "%s: Starting validation of `%s' message for `%4s' via '%s' of size %u\n",
4633 GNUNET_i2s (&target),
4635 GNUNET_HELLO_size(hello));
4639 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
4641 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
4642 memcpy (&chvc[1], hello, hsize);
4643 GNUNET_CONTAINER_DLL_insert (chvc_head,
4646 /* finally, check if HELLO was previously validated
4647 (continuation will then schedule actual validation) */
4648 GNUNET_STATISTICS_update (stats,
4649 gettext_noop ("# peerinfo process hello iterate requests"),
4652 GNUNET_STATISTICS_update (stats,
4653 gettext_noop ("# outstanding peerinfo iterate requests"),
4656 chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
4658 HELLO_VERIFICATION_TIMEOUT,
4659 &check_hello_validated, chvc);
4665 * The peer specified by the given neighbour has timed-out or a plugin
4666 * has disconnected. We may either need to do nothing (other plugins
4667 * still up), or trigger a full disconnect and clean up. This
4668 * function updates our state and does the necessary notifications.
4669 * Also notifies our clients that the neighbour is now officially
4672 * @param n the neighbour list entry for the peer
4673 * @param check GNUNET_YES to check if ALL addresses for this peer
4674 * are gone, GNUNET_NO to force a disconnect of the peer
4675 * regardless of whether other addresses exist.
4678 disconnect_neighbour (struct NeighbourList *n, int check)
4680 struct ReadyList *rpos;
4681 struct NeighbourList *npos;
4682 struct NeighbourList *nprev;
4683 struct MessageQueue *mq;
4684 struct ForeignAddressList *peer_addresses;
4685 struct ForeignAddressList *peer_pos;
4687 if (GNUNET_YES == check)
4690 while (NULL != rpos)
4692 peer_addresses = rpos->addresses;
4693 while (peer_addresses != NULL)
4695 if (GNUNET_YES == peer_addresses->connected)
4697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4698 "NOT Disconnecting from `%4s', still have live addresses!\n",
4699 GNUNET_i2s (&n->id));
4700 return; /* still connected */
4702 peer_addresses = peer_addresses->next;
4708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4709 "Disconnecting from `%4s'\n",
4710 GNUNET_i2s (&n->id));
4712 /* remove n from neighbours list */
4715 while ((npos != NULL) && (npos != n))
4720 GNUNET_assert (npos != NULL);
4722 neighbours = n->next;
4724 nprev->next = n->next;
4726 /* notify all clients about disconnect */
4727 if (GNUNET_YES == n->received_pong)
4728 notify_clients_disconnect (&n->id);
4730 /* clean up all plugins, cancel connections and pending transmissions */
4731 while (NULL != (rpos = n->plugins))
4733 n->plugins = rpos->next;
4734 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
4735 while (rpos->addresses != NULL)
4737 peer_pos = rpos->addresses;
4738 rpos->addresses = peer_pos->next;
4739 if (peer_pos->connected == GNUNET_YES)
4740 GNUNET_STATISTICS_update (stats,
4741 gettext_noop ("# connected addresses"),
4744 if (GNUNET_YES == peer_pos->validated)
4745 GNUNET_STATISTICS_update (stats,
4746 gettext_noop ("# peer addresses considered valid"),
4749 if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
4751 GNUNET_SCHEDULER_cancel (peer_pos->revalidate_task);
4752 peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
4754 GNUNET_free(peer_pos->ressources);
4755 GNUNET_free(peer_pos->quality);
4756 GNUNET_free(peer_pos);
4761 /* free all messages on the queue */
4762 while (NULL != (mq = n->messages_head))
4764 GNUNET_STATISTICS_update (stats,
4765 gettext_noop ("# bytes in message queue for other peers"),
4766 - (int64_t) mq->message_buf_size,
4768 GNUNET_STATISTICS_update (stats,
4769 gettext_noop ("# bytes discarded due to disconnect"),
4770 mq->message_buf_size,
4772 GNUNET_CONTAINER_DLL_remove (n->messages_head,
4775 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4777 sizeof(struct GNUNET_PeerIdentity)));
4780 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
4782 GNUNET_SCHEDULER_cancel (n->timeout_task);
4783 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4785 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4787 GNUNET_SCHEDULER_cancel (n->retry_task);
4788 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4790 if (n->piter != NULL)
4792 GNUNET_PEERINFO_iterate_cancel (n->piter);
4793 GNUNET_STATISTICS_update (stats,
4794 gettext_noop ("# outstanding peerinfo iterate requests"),
4799 /* finally, free n itself */
4800 GNUNET_STATISTICS_update (stats,
4801 gettext_noop ("# active neighbours"),
4804 GNUNET_free_non_null (n->pre_connect_message_buffer);
4810 * We have received a PING message from someone. Need to send a PONG message
4811 * in response to the peer by any means necessary.
4814 handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
4815 const struct GNUNET_PeerIdentity *peer,
4816 struct Session *session,
4817 const char *sender_address,
4818 uint16_t sender_address_len)
4820 struct TransportPlugin *plugin = cls;
4821 struct SessionHeader *session_header = (struct SessionHeader*) session;
4822 struct TransportPingMessage *ping;
4823 struct TransportPongMessage *pong;
4824 struct NeighbourList *n;
4825 struct ReadyList *rl;
4826 struct ForeignAddressList *fal;
4827 struct OwnAddressList *oal;
4832 if (ntohs (message->size) < sizeof (struct TransportPingMessage))
4834 GNUNET_break_op (0);
4835 return GNUNET_SYSERR;
4838 ping = (struct TransportPingMessage *) message;
4839 if (0 != memcmp (&ping->target,
4840 plugin->env.my_identity,
4841 sizeof (struct GNUNET_PeerIdentity)))
4843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4844 _("Received `%s' message from `%s' destined for `%s' which is not me!\n"),
4846 (sender_address != NULL)
4847 ? a2s (plugin->short_name,
4848 (const struct sockaddr *)sender_address,
4851 GNUNET_i2s (&ping->target));
4852 return GNUNET_SYSERR;
4855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4856 "Processing `%s' from `%s'\n",
4858 (sender_address != NULL)
4859 ? a2s (plugin->short_name,
4860 (const struct sockaddr *)sender_address,
4864 GNUNET_STATISTICS_update (stats,
4865 gettext_noop ("# PING messages received"),
4868 addr = (const char*) &ping[1];
4869 alen = ntohs (message->size) - sizeof (struct TransportPingMessage);
4870 slen = strlen (plugin->short_name) + 1;
4873 /* peer wants to confirm that we have an outbound connection to him */
4874 if (session == NULL)
4876 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4877 _("Refusing to create PONG since I do not have a session with `%s'.\n"),
4879 return GNUNET_SYSERR;
4881 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len + slen);
4882 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len + 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) + sender_address_len + slen);
4889 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING);
4890 pong->challenge = ping->challenge;
4891 pong->addrlen = htonl(sender_address_len + slen);
4894 sizeof(struct GNUNET_PeerIdentity));
4898 if ((sender_address!=NULL) && (sender_address_len > 0))
4899 memcpy (&((char*)&pong[1])[slen],
4901 sender_address_len);
4902 if (GNUNET_TIME_absolute_get_remaining (session_header->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
4904 /* create / update cached sig */
4906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4907 "Creating PONG signature to indicate active connection.\n");
4909 session_header->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
4910 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
4911 GNUNET_assert (GNUNET_OK ==
4912 GNUNET_CRYPTO_rsa_sign (my_private_key,
4914 &session_header->pong_signature));
4918 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
4920 memcpy (&pong->signature,
4921 &session_header->pong_signature,
4922 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4928 /* peer wants to confirm that this is one of our addresses */
4932 plugin->api->check_address (plugin->api->cls,
4936 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4937 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
4938 a2s (plugin->short_name,
4943 oal = plugin->addresses;
4946 if ( (oal->addrlen == alen) &&
4953 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
4954 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
4955 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
4956 pong->purpose.size =
4957 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4959 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
4960 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
4961 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
4962 pong->challenge = ping->challenge;
4963 pong->addrlen = htonl(alen + slen);
4966 sizeof(struct GNUNET_PeerIdentity));
4967 memcpy (&pong[1], plugin->short_name, slen);
4968 memcpy (&((char*)&pong[1])[slen], addr, alen);
4969 if ( (oal != NULL) &&
4970 (GNUNET_TIME_absolute_get_remaining (oal->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4) )
4972 /* create / update cached sig */
4974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4975 "Creating PONG signature to indicate ownership.\n");
4977 oal->pong_sig_expires = GNUNET_TIME_absolute_min (oal->expires,
4978 GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
4979 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
4980 GNUNET_assert (GNUNET_OK ==
4981 GNUNET_CRYPTO_rsa_sign (my_private_key,
4983 &oal->pong_signature));
4984 memcpy (&pong->signature,
4985 &oal->pong_signature,
4986 sizeof (struct GNUNET_CRYPTO_RsaSignature));
4988 else if (oal == NULL)
4990 /* not using cache (typically DV-only) */
4991 pong->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
4992 GNUNET_assert (GNUNET_OK ==
4993 GNUNET_CRYPTO_rsa_sign (my_private_key,
4999 /* can used cached version */
5000 pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5001 memcpy (&pong->signature,
5002 &oal->pong_signature,
5003 sizeof (struct GNUNET_CRYPTO_RsaSignature));
5006 n = find_neighbour(peer);
5007 GNUNET_assert (n != NULL);
5008 /* first try reliable response transmission */
5012 fal = rl->addresses;
5015 if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
5018 ntohs (pong->header.size),
5019 TRANSPORT_PONG_PRIORITY,
5020 HELLO_VERIFICATION_TIMEOUT,
5028 GNUNET_STATISTICS_update (stats,
5029 gettext_noop ("# PONGs unicast via reliable transport"),
5039 /* no reliable method found, do multicast */
5040 GNUNET_STATISTICS_update (stats,
5041 gettext_noop ("# PONGs multicast to all available addresses"),
5047 fal = rl->addresses;
5050 transmit_to_peer(NULL, fal,
5051 TRANSPORT_PONG_PRIORITY,
5052 HELLO_VERIFICATION_TIMEOUT,
5054 ntohs(pong->header.size),
5070 * Function called by the plugin for each received message.
5071 * Update data volumes, possibly notify plugins about
5072 * reducing the rate at which they read from the socket
5073 * and generally forward to our receive callback.
5075 * @param cls the "struct TransportPlugin *" we gave to the plugin
5076 * @param peer (claimed) identity of the other peer
5077 * @param message the message, NULL if we only care about
5078 * learning about the delay until we should receive again
5079 * @param ats_data information for automatic transport selection
5080 * @param ats_count number of elements in ats not including 0-terminator
5081 * @param session identifier used for this session (can be NULL)
5082 * @param sender_address binary address of the sender (if observed)
5083 * @param sender_address_len number of bytes in sender_address
5084 * @return how long in ms the plugin should wait until receiving more data
5085 * (plugins that do not support this, can ignore the return value)
5087 static struct GNUNET_TIME_Relative
5088 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
5089 const struct GNUNET_MessageHeader *message,
5090 const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
5092 struct Session *session,
5093 const char *sender_address,
5094 uint16_t sender_address_len)
5096 struct TransportPlugin *plugin = cls;
5097 struct ReadyList *service_context;
5098 struct ForeignAddressList *peer_address;
5100 struct NeighbourList *n;
5101 struct GNUNET_TIME_Relative ret;
5102 if (is_blacklisted (peer, plugin))
5103 return GNUNET_TIME_UNIT_FOREVER_REL;
5107 n = find_neighbour (peer);
5109 n = setup_new_neighbour (peer, GNUNET_YES);
5110 service_context = n->plugins;
5111 while ((service_context != NULL) && (plugin != service_context->plugin))
5112 service_context = service_context->next;
5113 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
5114 peer_address = NULL;
5117 for (c=0; c<ats_count; c++)
5119 if (ntohl(ats_data[c].type) == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
5121 distance = ntohl(ats_data[c].value);
5124 /* notify ATS about incoming data */
5125 //ats_notify_ats_data(peer, ats_data);
5127 if (message != NULL)
5129 if ( (session != NULL) ||
5130 (sender_address != NULL) )
5131 peer_address = add_peer_address (n,
5135 sender_address_len);
5136 if (peer_address != NULL)
5139 update_addr_ats(peer_address, ats_data, ats_count);
5140 update_addr_value(peer_address, distance, GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5142 peer_address->distance = distance;
5143 if (GNUNET_YES == peer_address->validated)
5144 mark_address_connected (peer_address);
5145 peer_address->timeout
5147 GNUNET_TIME_relative_to_absolute
5148 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5149 schedule_next_ping (peer_address);
5151 /* update traffic received amount ... */
5152 msize = ntohs (message->size);
5153 GNUNET_STATISTICS_update (stats,
5154 gettext_noop ("# bytes received from other peers"),
5157 n->distance = distance;
5159 GNUNET_TIME_relative_to_absolute
5160 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5161 GNUNET_SCHEDULER_cancel (n->timeout_task);
5163 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
5164 &neighbour_timeout_task, n);
5165 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
5167 /* dropping message due to frequent inbound volume violations! */
5168 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
5169 GNUNET_ERROR_TYPE_BULK,
5171 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
5172 n->in_tracker.available_bytes_per_s__,
5173 n->quota_violation_count);
5174 GNUNET_STATISTICS_update (stats,
5175 gettext_noop ("# bandwidth quota violations by other peers"),
5178 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
5182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5183 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
5184 ntohs (message->type),
5185 ntohs (message->size),
5188 switch (ntohs (message->type))
5190 case GNUNET_MESSAGE_TYPE_HELLO:
5191 GNUNET_STATISTICS_update (stats,
5192 gettext_noop ("# HELLO messages received from other peers"),
5195 process_hello (plugin, message);
5197 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
5198 handle_ping (plugin, message, peer, session, sender_address, sender_address_len);
5200 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
5201 handle_pong (plugin, message, peer, sender_address, sender_address_len);
5204 handle_payload_message (message, n);
5208 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
5209 if (ret.rel_value > 0)
5211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5212 "Throttling read (%llu bytes excess at %u b/s), waiting %llums before reading more.\n",
5213 (unsigned long long) n->in_tracker.consumption_since_last_update__,
5214 (unsigned int) n->in_tracker.available_bytes_per_s__,
5215 (unsigned long long) ret.rel_value);
5216 GNUNET_STATISTICS_update (stats,
5217 gettext_noop ("# ms throttling suggested"),
5218 (int64_t) ret.rel_value,
5225 * Handle START-message. This is the first message sent to us
5226 * by any client which causes us to add it to our list.
5228 * @param cls closure (always NULL)
5229 * @param client identification of the client
5230 * @param message the actual message
5233 handle_start (void *cls,
5234 struct GNUNET_SERVER_Client *client,
5235 const struct GNUNET_MessageHeader *message)
5237 const struct StartMessage *start;
5238 struct TransportClient *c;
5239 struct ConnectInfoMessage * cim;
5240 struct NeighbourList *n;
5244 start = (const struct StartMessage*) message;
5246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5247 "Received `%s' request from client\n", "START");
5252 if (c->client == client)
5254 /* client already on our list! */
5256 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5261 if ( (GNUNET_NO != ntohl (start->do_check)) &&
5262 (0 != memcmp (&start->self,
5264 sizeof (struct GNUNET_PeerIdentity))) )
5266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5267 _("Rejecting control connection from peer `%s', which is not me!\n"),
5268 GNUNET_i2s (&start->self));
5269 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5272 c = GNUNET_malloc (sizeof (struct TransportClient));
5276 if (our_hello != NULL)
5279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5280 "Sending our own `%s' to new client\n", "HELLO");
5282 transmit_to_client (c,
5283 (const struct GNUNET_MessageHeader *) our_hello,
5285 /* tell new client about all existing connections */
5287 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
5288 if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
5292 cim = GNUNET_malloc (size);
5293 cim->header.size = htons (size);
5294 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
5295 cim->ats_count = htonl(ats_count);
5296 (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
5297 (&(cim->ats))[2].value = htonl (0);
5301 if (GNUNET_YES == n->received_pong)
5303 (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5304 (&(cim->ats))[0].value = htonl (n->distance);
5305 (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
5306 (&(cim->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
5308 transmit_to_client (c, &cim->header, GNUNET_NO);
5314 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5319 * Handle HELLO-message.
5321 * @param cls closure (always NULL)
5322 * @param client identification of the client
5323 * @param message the actual message
5326 handle_hello (void *cls,
5327 struct GNUNET_SERVER_Client *client,
5328 const struct GNUNET_MessageHeader *message)
5332 GNUNET_STATISTICS_update (stats,
5333 gettext_noop ("# HELLOs received from clients"),
5336 ret = process_hello (NULL, message);
5337 GNUNET_SERVER_receive_done (client, ret);
5342 * Closure for 'transmit_client_message'; followed by
5343 * 'msize' bytes of the actual message.
5345 struct TransmitClientMessageContext
5348 * Client on whom's behalf we are sending.
5350 struct GNUNET_SERVER_Client *client;
5353 * Timeout for the transmission.
5355 struct GNUNET_TIME_Absolute timeout;
5363 * Size of the message in bytes.
5370 * Schedule transmission of a message we got from a client to a peer.
5372 * @param cls the 'struct TransmitClientMessageContext*'
5373 * @param n destination, or NULL on error (in that case, drop the message)
5376 transmit_client_message (void *cls,
5377 struct NeighbourList *n)
5379 struct TransmitClientMessageContext *tcmc = cls;
5380 struct TransportClient *tc;
5383 while ((tc != NULL) && (tc->client != tcmc->client))
5388 transmit_to_peer (tc, NULL, tcmc->priority,
5389 GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
5391 tcmc->msize, GNUNET_NO, n);
5393 GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
5394 GNUNET_SERVER_client_drop (tcmc->client);
5400 * Handle SEND-message.
5402 * @param cls closure (always NULL)
5403 * @param client identification of the client
5404 * @param message the actual message
5407 handle_send (void *cls,
5408 struct GNUNET_SERVER_Client *client,
5409 const struct GNUNET_MessageHeader *message)
5411 const struct OutboundMessage *obm;
5412 const struct GNUNET_MessageHeader *obmm;
5413 struct TransmitClientMessageContext *tcmc;
5417 size = ntohs (message->size);
5419 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
5422 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5425 GNUNET_STATISTICS_update (stats,
5426 gettext_noop ("# payload received for other peers"),
5429 obm = (const struct OutboundMessage *) message;
5430 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
5431 msize = size - sizeof (struct OutboundMessage);
5433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5434 "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
5435 "SEND", GNUNET_i2s (&obm->peer),
5439 tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
5440 tcmc->client = client;
5441 tcmc->priority = ntohl (obm->priority);
5442 tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
5443 tcmc->msize = msize;
5444 /* FIXME: this memcpy can be up to 7% of our total runtime */
5445 memcpy (&tcmc[1], obmm, msize);
5446 GNUNET_SERVER_client_keep (client);
5447 setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
5448 &transmit_client_message,
5454 * Handle request connect message
5456 * @param cls closure (always NULL)
5457 * @param client identification of the client
5458 * @param message the actual message
5461 handle_request_connect (void *cls,
5462 struct GNUNET_SERVER_Client *client,
5463 const struct GNUNET_MessageHeader *message)
5465 const struct TransportRequestConnectMessage *trcm =
5466 (const struct TransportRequestConnectMessage *) message;
5468 GNUNET_STATISTICS_update (stats,
5469 gettext_noop ("# REQUEST CONNECT messages received"),
5472 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a request connect message for peer %s\n", GNUNET_i2s(&trcm->peer));
5473 setup_peer_check_blacklist (&trcm->peer, GNUNET_YES,
5475 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5479 * Handle SET_QUOTA-message.
5481 * @param cls closure (always NULL)
5482 * @param client identification of the client
5483 * @param message the actual message
5486 handle_set_quota (void *cls,
5487 struct GNUNET_SERVER_Client *client,
5488 const struct GNUNET_MessageHeader *message)
5490 const struct QuotaSetMessage *qsm =
5491 (const struct QuotaSetMessage *) message;
5492 struct NeighbourList *n;
5494 GNUNET_STATISTICS_update (stats,
5495 gettext_noop ("# SET QUOTA messages received"),
5498 n = find_neighbour (&qsm->peer);
5501 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5502 GNUNET_STATISTICS_update (stats,
5503 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
5509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5510 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
5512 (unsigned int) ntohl (qsm->quota.value__),
5513 (unsigned int) n->in_tracker.available_bytes_per_s__,
5514 GNUNET_i2s (&qsm->peer));
5516 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
5518 if (0 == ntohl (qsm->quota.value__))
5520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5521 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&n->id),
5523 disconnect_neighbour (n, GNUNET_NO);
5525 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5530 * Take the given address and append it to the set of results sent back to
5533 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
5534 * @param address the resolved name, NULL to indicate the last response
5537 transmit_address_to_client (void *cls, const char *address)
5539 struct GNUNET_SERVER_TransmitContext *tc = cls;
5542 if (NULL == address)
5545 slen = strlen (address) + 1;
5547 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
5548 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5549 if (NULL == address)
5550 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
5555 * Handle AddressLookup-message.
5557 * @param cls closure (always NULL)
5558 * @param client identification of the client
5559 * @param message the actual message
5562 handle_address_lookup (void *cls,
5563 struct GNUNET_SERVER_Client *client,
5564 const struct GNUNET_MessageHeader *message)
5566 const struct AddressLookupMessage *alum;
5567 struct TransportPlugin *lsPlugin;
5568 const char *nameTransport;
5569 const char *address;
5571 struct GNUNET_SERVER_TransmitContext *tc;
5572 struct GNUNET_TIME_Absolute timeout;
5573 struct GNUNET_TIME_Relative rtimeout;
5576 size = ntohs (message->size);
5577 if (size < sizeof (struct AddressLookupMessage))
5579 GNUNET_break_op (0);
5580 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5583 alum = (const struct AddressLookupMessage *) message;
5584 uint32_t addressLen = ntohl (alum->addrlen);
5585 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
5587 GNUNET_break_op (0);
5588 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5591 address = (const char *) &alum[1];
5592 nameTransport = (const char *) &address[addressLen];
5594 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
5596 GNUNET_break_op (0);
5597 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5600 timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
5601 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5602 numeric = ntohl (alum->numeric_only);
5603 lsPlugin = find_transport (nameTransport);
5604 if (NULL == lsPlugin)
5606 tc = GNUNET_SERVER_transmit_context_create (client);
5607 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5608 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5609 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
5612 tc = GNUNET_SERVER_transmit_context_create (client);
5613 lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
5615 address, addressLen,
5618 &transmit_address_to_client, tc);
5623 * Setup the environment for this plugin.
5626 create_environment (struct TransportPlugin *plug)
5628 plug->env.cfg = cfg;
5629 plug->env.my_identity = &my_identity;
5630 plug->env.our_hello = &our_hello;
5631 plug->env.cls = plug;
5632 plug->env.receive = &plugin_env_receive;
5633 plug->env.notify_address = &plugin_env_notify_address;
5634 plug->env.session_end = &plugin_env_session_end;
5635 plug->env.max_connections = max_connect_per_transport;
5636 plug->env.stats = stats;
5641 * Start the specified transport (load the plugin).
5644 start_transport (struct GNUNET_SERVER_Handle *server,
5647 struct TransportPlugin *plug;
5650 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5651 _("Loading `%s' transport plugin\n"), name);
5652 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
5653 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
5654 create_environment (plug);
5655 plug->short_name = GNUNET_strdup (name);
5656 plug->lib_name = libname;
5657 plug->next = plugins;
5659 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
5660 if (plug->api == NULL)
5662 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5663 _("Failed to load transport plugin for `%s'\n"), name);
5664 GNUNET_free (plug->short_name);
5665 plugins = plug->next;
5666 GNUNET_free (libname);
5673 * Called whenever a client is disconnected. Frees our
5674 * resources associated with that client.
5676 * @param cls closure
5677 * @param client identification of the client
5680 client_disconnect_notification (void *cls,
5681 struct GNUNET_SERVER_Client *client)
5683 struct TransportClient *pos;
5684 struct TransportClient *prev;
5685 struct ClientMessageQueueEntry *mqe;
5686 struct Blacklisters *bl;
5687 struct BlacklistCheck *bc;
5692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
5693 "Client disconnected, cleaning up.\n");
5695 /* clean up blacklister */
5699 if (bl->client == client)
5704 if (bc->bl_pos == bl)
5706 bc->bl_pos = bl->next;
5709 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
5712 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
5713 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
5719 GNUNET_CONTAINER_DLL_remove (bl_head,
5722 GNUNET_SERVER_client_drop (bl->client);
5728 /* clean up 'normal' clients */
5731 while ((pos != NULL) && (pos->client != client))
5738 while (NULL != (mqe = pos->message_queue_head))
5740 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
5741 pos->message_queue_tail,
5743 pos->message_count--;
5747 clients = pos->next;
5749 prev->next = pos->next;
5750 if (GNUNET_YES == pos->tcs_pending)
5755 if (pos->th != NULL)
5757 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
5760 GNUNET_break (0 == pos->message_count);
5766 * Function called when the service shuts down. Unloads our plugins
5767 * and cancels pending validations.
5769 * @param cls closure, unused
5770 * @param tc task context (unused)
5773 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5775 struct TransportPlugin *plug;
5776 struct OwnAddressList *al;
5777 struct CheckHelloValidatedContext *chvc;
5779 while (neighbours != NULL)
5781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5782 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&neighbours->id),
5784 disconnect_neighbour (neighbours, GNUNET_NO);
5787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5788 "Transport service is unloading plugins...\n");
5790 while (NULL != (plug = plugins))
5792 plugins = plug->next;
5793 if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
5795 GNUNET_SCHEDULER_cancel (plug->address_update_task);
5796 plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
5798 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
5799 GNUNET_free (plug->lib_name);
5800 GNUNET_free (plug->short_name);
5801 while (NULL != (al = plug->addresses))
5803 plug->addresses = al->next;
5808 if (my_private_key != NULL)
5809 GNUNET_CRYPTO_rsa_key_free (my_private_key);
5810 GNUNET_free_non_null (our_hello);
5812 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
5815 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
5816 validation_map = NULL;
5820 /* free 'chvc' data structure */
5821 while (NULL != (chvc = chvc_head))
5823 chvc_head = chvc->next;
5824 if (chvc->piter != NULL)
5826 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
5827 GNUNET_STATISTICS_update (stats,
5828 gettext_noop ("# outstanding peerinfo iterate requests"),
5834 GNUNET_assert (chvc->ve_count == 0);
5841 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
5844 if (peerinfo != NULL)
5846 GNUNET_PEERINFO_disconnect (peerinfo);
5849 /* Can we assume those are gone by now, or do we need to clean up
5851 GNUNET_break (bl_head == NULL);
5852 GNUNET_break (bc_head == NULL);
5856 static int ats_evaluate_results (int result, int solution, char * problem)
5858 int cont = GNUNET_NO;
5859 int error_kind = GNUNET_ERROR_TYPE_DEBUG;
5861 error_kind = GNUNET_ERROR_TYPE_ERROR;
5865 case GLP_ESTOP : /* search terminated by application */
5866 GNUNET_log (error_kind, "%s , Search terminated by application ", problem);
5868 case GLP_EITLIM : /* iteration limit exceeded */
5869 GNUNET_log (error_kind, "%s Iteration limit exceeded ", problem);
5872 case GLP_ETMLIM : /* time limit exceeded */
5873 GNUNET_log (error_kind, "%s Time limit exceeded ", problem);
5875 case GLP_ENOPFS : /* no primal feasible solution */
5876 case GLP_ENODFS : /* no dual feasible solution */
5877 GNUNET_log (error_kind, "%s No feasible solution", problem);
5880 case GLP_EBADB : /* invalid basis */
5881 case GLP_ESING : /* singular matrix */
5882 case GLP_ECOND : /* ill-conditioned matrix */
5883 case GLP_EBOUND : /* invalid bounds */
5884 case GLP_EFAIL : /* solver failed */
5885 case GLP_EOBJLL : /* objective lower limit reached */
5886 case GLP_EOBJUL : /* objective upper limit reached */
5887 case GLP_EROOT : /* root LP optimum not provided */
5888 GNUNET_log (error_kind, "%s Invalid Input data: %i\n", problem, result);
5892 GNUNET_log (error_kind, "%s Problem has been solved\n", problem);
5898 GNUNET_log (error_kind, "%s solution is undefined\n", problem);
5901 GNUNET_log (error_kind, "%s solution is optimal\n", problem);
5905 GNUNET_log (error_kind, "%s solution is %s feasible, however, its optimality (or non-optimality) has not been proven, \n", problem, (0==strcmp(problem,"LP")?"":"integer"));
5909 GNUNET_log (error_kind, "%s problem has no %sfeasible solution\n", problem, (0==strcmp(problem,"LP")?"":"integer "));
5912 GNUNET_log (error_kind, "%s problem is infeasible \n", problem);
5915 GNUNET_log (error_kind, "%s problem is unbounded \n", problem);
5923 static void ats_solve_problem (unsigned int max_it, unsigned int max_dur, unsigned int c_peers, unsigned int c_mechs, struct ATS_result *res)
5930 glp_prob *prob = ats->prob;
5933 glp_init_smcp(&opt_lp);
5935 opt_lp.msg_lev = GLP_MSG_ALL;
5938 opt_lp.msg_lev = GLP_MSG_OFF;
5940 //opt_lp.presolve = GLP_ON;
5941 result = glp_simplex(prob, &opt_lp);
5942 solution = glp_get_status (prob);
5944 if (GNUNET_YES == ats_evaluate_results(result, solution, "LP"))
5948 glp_init_iocp(&opt_mlp);
5949 /* maximum duration */
5950 //opt_mlp.presolve = GLP_ON;
5951 opt_mlp.tm_lim = max_dur;
5954 opt_mlp.msg_lev = GLP_MSG_ALL;
5957 opt_mlp.msg_lev = GLP_MSG_OFF;
5959 result = glp_intopt (prob, &opt_mlp);
5960 solution = glp_mip_status (prob);
5961 res->solution = solution;
5962 res->valid = GNUNET_NO;
5963 if (ats_evaluate_results(result, solution, "MLP") == GNUNET_YES)
5964 res->valid = GNUNET_YES;
5968 if ((ats->save_mlp == GNUNET_YES) && (c_peers > 1))
5971 GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_llu.mlp",c_peers, c_mechs, GNUNET_TIME_absolute_get().abs_value);
5972 //if (GNUNET_NO == GNUNET_DISK_file_test(filename))
5973 glp_write_lp (prob, NULL, filename);
5974 GNUNET_free (filename);
5976 if ((ats->save_solution == GNUNET_YES) && (c_peers > 1))
5979 GNUNET_asprintf (&filename, "ats_mlp_p%i_m%i_llu.sol",c_peers, c_mechs, GNUNET_TIME_absolute_get().abs_value);
5980 //if (GNUNET_NO == GNUNET_DISK_file_test(filename))
5981 glp_print_sol (prob, filename);
5982 GNUNET_free (filename);
5987 int error = GNUNET_NO;
5989 struct ATS_mechanism *t = NULL;
5990 for (c=1; c<= (c_peers); c++ )
5993 t = peers[c].m_head;
5996 bw = glp_get_col_prim(prob, t->col_index);
6000 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);
6002 if (check ==GNUNET_YES)
6004 glp_write_sol(prob, "invalid_solution.mlp");
6005 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid solution, check invalid_solution.mlp");
6006 GNUNET_STATISTICS_update (stats, "ATS invalid solutions", 1, GNUNET_NO);
6009 if (check ==GNUNET_NO)
6017 for (c=1; c<= available_quality_metrics; c++ )
6019 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));
6021 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));
6022 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));
6023 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));
6027 static void ats_delete_problem ()
6031 for (c=0; c< (ats->res).c_mechs; c++)
6032 GNUNET_free_non_null (ats->mechanisms[c].rc);
6034 if (ats->mechanisms!=NULL)
6036 GNUNET_free(ats->mechanisms);
6037 ats->mechanisms = NULL;
6039 if (ats->peers!=NULL)
6041 GNUNET_free(ats->peers);
6045 if (ats->prob != NULL)
6047 glp_delete_prob(ats->prob);
6051 ats->res.begin_cr = GNUNET_SYSERR;
6052 ats->res.begin_qm = GNUNET_SYSERR;
6053 ats->res.c_mechs = 0;
6054 ats->res.c_peers = 0;
6055 ats->res.end_cr = GNUNET_SYSERR;
6056 ats->res.end_qm = GNUNET_SYSERR;
6057 ats->res.solution = GNUNET_SYSERR;
6058 ats->res.valid = GNUNET_SYSERR;
6061 static void ats_update_problem ()
6067 /** solve the bandwidth distribution problem
6068 * @param max_it maximum iterations
6069 * @param max_dur maximum duration in ms
6070 * @param D weight for diversity
6071 * @param U weight for utility
6072 * @param R weight for relativity
6073 * @param v_b_min minimal bandwidth per peer
6074 * @param v_n_min minimum number of connections
6075 * @param res result struct
6076 * @return GNUNET_SYSERR if glpk is not available, number of mechanisms used
6078 static int ats_create_problem (double D, double U, double R, int v_b_min, int v_n_min, struct ATS_result *res)
6080 if (ats->prob != NULL)
6081 glp_delete_prob(ats->prob);
6083 ats->prob = glp_create_prob();
6089 int c_c_ressources = available_ressources;
6090 int c_q_metrics = available_quality_metrics;
6092 double M = VERY_BIG_DOUBLE_VALUE;
6093 double Q[c_q_metrics+1];
6094 for (c=1; c<=c_q_metrics; c++)
6099 struct NeighbourList *next = neighbours;
6102 struct ReadyList *r_next = next->plugins;
6103 while (r_next != NULL)
6105 struct ForeignAddressList * a_next = r_next->addresses;
6106 while (a_next != NULL)
6109 a_next = a_next->next;
6111 r_next = r_next->next;
6120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No addresses for bw distribution available\n", c_peers);
6122 res->valid = GNUNET_NO;
6125 return GNUNET_SYSERR;
6128 ats->mechanisms = GNUNET_malloc((1+c_mechs) * sizeof (struct ATS_mechanism));
6129 ats->peers = GNUNET_malloc((1+c_peers) * sizeof (struct ATS_peer));
6131 struct ATS_mechanism * mechanisms = ats->mechanisms;
6132 struct ATS_peer * peers = ats->peers;
6139 peers[c_peers].peer = next->id;
6140 peers[c_peers].m_head = NULL;
6141 peers[c_peers].m_tail = NULL;
6143 peers[c_peers].f = 1.0 / c_mechs;
6145 struct ReadyList *r_next = next->plugins;
6146 while (r_next != NULL)
6148 struct ForeignAddressList * a_next = r_next->addresses;
6149 while (a_next != NULL)
6151 mechanisms[c_mechs].addr = a_next;
6152 mechanisms[c_mechs].col_index = c_mechs;
6153 mechanisms[c_mechs].peer = &peers[c_peers];
6154 mechanisms[c_mechs].next = NULL;
6155 mechanisms[c_mechs].plugin = r_next->plugin;
6157 GNUNET_CONTAINER_DLL_insert_tail(peers[c_peers].m_head, peers[c_peers].m_tail, &mechanisms[c_mechs]);
6159 a_next = a_next->next;
6161 r_next = r_next->next;
6169 if (v_n_min > c_peers)
6173 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);
6176 int size = 1 + 3 + 10 *c_mechs + c_peers + (c_q_metrics*c_mechs)+ c_q_metrics + c_c_ressources * c_mechs ;
6179 int * ia = GNUNET_malloc (size * sizeof (int));
6180 int * ja = GNUNET_malloc (size * sizeof (int));
6181 double * ar = GNUNET_malloc(size* sizeof (double));
6183 glp_set_prob_name(ats->prob, "gnunet ats bandwidth distribution");
6184 glp_set_obj_dir(ats->prob, GLP_MAX);
6186 /* adding columns */
6188 glp_add_cols(ats->prob, 2 * c_mechs);
6189 /* adding b_t cols */
6190 for (c=1; c <= c_mechs; c++)
6193 GNUNET_asprintf(&name, "p_%s_b%i",GNUNET_i2s(&(mechanisms[c].peer->peer)), c);
6194 glp_set_col_name(ats->prob, c, name);
6196 glp_set_col_bnds(ats->prob, c, GLP_LO, 0.0, 0.0);
6197 glp_set_obj_coef(ats->prob, c, 0);
6200 /* adding n_t cols */
6201 for (c=c_mechs+1; c <= 2*c_mechs; c++)
6203 GNUNET_asprintf(&name, "p_%s_n%i",GNUNET_i2s(&(mechanisms[c-c_mechs].peer->peer)),(c-c_mechs));
6204 glp_set_col_name(ats->prob, c, name);
6206 glp_set_col_bnds(ats->prob, c, GLP_DB, 0.0, 1.0);
6207 glp_set_col_kind(ats->prob, c, GLP_IV);
6208 glp_set_obj_coef(ats->prob, c, 0);
6211 /* feasibility constraints */
6212 /* Constraint 1: one address per peer*/
6214 glp_add_rows(ats->prob, c_peers);
6215 for (c=1; c<=c_peers; c++)
6217 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 1.0, 1.0);
6219 struct ATS_mechanism *m = peers[c].m_head;
6222 ia[array_index] = row_index;
6223 ja[array_index] = (c_mechs + m->col_index);
6224 ar[array_index] = 1;
6226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6234 /* Constraint 2: only active mechanism gets bandwidth assigned */
6235 glp_add_rows(ats->prob, c_mechs);
6236 for (c=1; c<=c_mechs; c++)
6238 /* b_t - n_t * M <= 0 */
6240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6242 glp_set_row_bnds(ats->prob, row_index, GLP_UP, 0.0, 0.0);
6244 ia[array_index] = row_index;
6245 ja[array_index] = mechanisms[c].col_index;
6246 ar[array_index] = 1;
6248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6251 ia[array_index] = row_index;
6252 ja[array_index] = c_mechs + mechanisms[c].col_index;
6253 ar[array_index] = -M;
6255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6261 /* Constraint 3: minimum bandwidth*/
6262 glp_add_rows(ats->prob, c_mechs);
6263 for (c=1; c<=c_mechs; c++)
6265 /* b_t - n_t * b_min <= 0 */
6267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6269 glp_set_row_bnds(ats->prob, row_index, GLP_LO, 0.0, 0.0);
6271 ia[array_index] = row_index;
6272 ja[array_index] = mechanisms[c].col_index;
6273 ar[array_index] = 1;
6275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6278 ia[array_index] = row_index;
6279 ja[array_index] = c_mechs + mechanisms[c].col_index;
6280 ar[array_index] = -v_b_min;
6282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6288 /* Constraint 4: max ressource capacity */
6289 /* V cr: bt * ct_r <= cr_max
6291 glp_add_rows(ats->prob, available_ressources);
6292 double ct_max = VERY_BIG_DOUBLE_VALUE;
6293 double ct_min = 0.0;
6295 res->begin_cr = array_index;
6297 for (c=0; c<available_ressources; c++)
6299 ct_max = ressources[c].c_max;
6300 ct_min = ressources[c].c_min;
6302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] %f..%f\n",row_index, ct_min, ct_max);
6304 glp_set_row_bnds(ats->prob, row_index, GLP_DB, ct_min, ct_max);
6306 for (c2=1; c2<=c_mechs; c2++)
6309 ia[array_index] = row_index;
6310 ja[array_index] = c2;
6311 value = mechanisms[c2].addr->ressources[c].c;
6312 ar[array_index] = value;
6314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6320 res->end_cr = array_index--;
6322 /* Constraint 5: min number of connections*/
6323 glp_add_rows(ats->prob, 1);
6324 for (c=1; c<=c_mechs; c++)
6326 // b_t - n_t * b_min >= 0
6328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6330 glp_set_row_bnds(ats->prob, row_index, GLP_LO, v_n_min, 0.0);
6332 ia[array_index] = row_index;
6333 ja[array_index] = c_mechs + mechanisms[c].col_index;
6334 ar[array_index] = 1;
6336 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6342 // optimisation constraints
6345 //glp_add_cols(ats->prob, 3 + c_q_metrics);
6347 // Constraint 6: optimize for diversity
6348 glp_add_cols(ats->prob, 1);
6349 glp_set_col_name(ats->prob, (2*c_mechs) + 1, "d");
6350 glp_set_obj_coef(ats->prob, (2*c_mechs) + 1, D);
6351 glp_set_col_bnds(ats->prob, (2*c_mechs) + 1, GLP_LO, 0.0, 0.0);
6352 glp_add_rows(ats->prob, 1);
6354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6356 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6357 for (c=1; c<=c_mechs; c++)
6359 // b_t - n_t * b_min >= 0
6360 ia[array_index] = row_index;
6361 ja[array_index] = c_mechs + mechanisms[c].col_index;
6362 ar[array_index] = 1;
6364 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6368 ia[array_index] = row_index;
6369 ja[array_index] = (2*c_mechs) + 1;
6370 ar[array_index] = -1;
6372 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6377 // Constraint 8: optimize bandwidth utility
6378 glp_add_cols(ats->prob, 1);
6379 glp_set_col_name(ats->prob, (2*c_mechs) + 2, "u");
6380 glp_set_obj_coef(ats->prob, (2*c_mechs) + 2, U);
6381 glp_set_col_bnds(ats->prob, (2*c_mechs) + 2, GLP_LO, 0.0, 0.0);
6382 glp_add_rows(ats->prob, 1);
6384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6386 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6387 for (c=1; c<=c_mechs; c++)
6389 ia[array_index] = row_index;
6390 ja[array_index] = c;
6391 ar[array_index] = mechanisms[c].peer->f;
6393 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6397 ia[array_index] = row_index;
6398 ja[array_index] = (2*c_mechs) + 2;
6399 ar[array_index] = -1;
6401 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6407 // Constraint 9: optimize relativity
6408 glp_add_cols(ats->prob, 1);
6409 glp_set_col_name(ats->prob, (2*c_mechs) + 3, "r");
6410 glp_set_obj_coef(ats->prob, (2*c_mechs) + 3, R);
6411 glp_set_col_bnds(ats->prob, (2*c_mechs) + 3, GLP_LO, 0.0, 0.0);
6412 glp_add_rows(ats->prob, c_peers);
6413 for (c=1; c<=c_peers; c++)
6415 glp_set_row_bnds(ats->prob, row_index, GLP_LO, 0.0, 0.0);
6417 struct ATS_mechanism *m = peers[c].m_head;
6420 ia[array_index] = row_index;
6421 ja[array_index] = m->col_index;
6422 ar[array_index] = 1 / mechanisms[c].peer->f;
6424 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6429 ia[array_index] = row_index;
6430 ja[array_index] = (2*c_mechs) + 3;
6431 ar[array_index] = -1;
6433 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6440 // Constraint 7: optimize for quality
6441 glp_add_cols(ats->prob, c_q_metrics);
6442 for (c=1; c<= c_q_metrics; c++)
6444 GNUNET_asprintf(&name, "Q_%s",qm[c-1].name);
6445 glp_set_col_name(ats->prob, (2*c_mechs) + 3 + c, name);
6446 glp_set_col_bnds(ats->prob, (2*c_mechs) + 3 + c, GLP_LO, 0.0, 0.0);
6448 glp_set_obj_coef(ats->prob, (2*c_mechs) + 3 + c, Q[c]);
6450 glp_add_rows(ats->prob, available_quality_metrics);
6451 res->begin_qm = array_index;
6452 for (c=1; c <= c_q_metrics; c++)
6455 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bounds [row]=[%i] \n",row_index);
6458 glp_set_row_bnds(ats->prob, row_index, GLP_FX, 0.0, 0.0);
6459 for (c2=1; c2<=c_mechs; c2++)
6462 ia[array_index] = row_index;
6463 ja[array_index] = c2;
6464 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY)
6467 v0 = mechanisms[c2].addr->quality[c-1].values[0];
6468 if (v1 < 1) v0 = 0.1;
6469 v1 = mechanisms[c2].addr->quality[c-1].values[1];
6470 if (v1 < 1) v0 = 0.1;
6471 v2 = mechanisms[c2].addr->quality[c-1].values[2];
6472 if (v1 < 1) v0 = 0.1;
6473 value = 100.0 / ((v0 + 2 * v1 + 3 * v2) / 6.0);
6476 if (qm[c-1].atis_index == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
6479 v0 = mechanisms[c2].addr->quality[c-1].values[0];
6481 v1 = mechanisms[c2].addr->quality[c-1].values[1];
6483 v2 = mechanisms[c2].addr->quality[c-1].values[2];
6485 value = (v0 + 2 * v1 + 3 * v2) / 6.0;
6487 value = (double) 10 / value;
6491 ar[array_index] = (mechanisms[c2].peer->f) * value ;
6493 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]);
6498 ia[array_index] = row_index;
6499 ja[array_index] = (2*c_mechs) + 3 +c;
6500 ar[array_index] = -1;
6502 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[index]=[%i]: [%i,%i]=%f \n",array_index, ia[array_index], ja[array_index], ar[array_index]);
6507 res->end_qm = array_index-1;
6509 /* Loading the matrix */
6510 glp_load_matrix(ats->prob, array_index-1, ia, ja, ar);
6512 res->c_mechs = c_mechs;
6513 res->c_peers = c_peers;
6515 res->valid = GNUNET_YES;
6527 void ats_notify_ats_data (
6528 const struct GNUNET_PeerIdentity *peer,
6529 const struct GNUNET_TRANSPORT_ATS_Information *ats_data)
6532 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "ATS_notify_ats_data: %s\n",GNUNET_i2s(peer));
6534 ats_calculate_bandwidth_distribution(ats);
6539 ats_calculate_bandwidth_distribution ()
6542 struct GNUNET_TIME_Absolute start;
6543 struct GNUNET_TIME_Relative duration;
6545 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_difference(ats->last,GNUNET_TIME_absolute_get());
6546 if (delta.rel_value < ats->min_delta.rel_value)
6549 GNUNET_log (GNUNET_ERROR_TYPE_BULK, "Minimum time between cycles not reached\n");
6555 if (INT_MAX < ats->max_exec_duration.rel_value)
6558 dur = (int) ats->max_exec_duration.rel_value;
6560 start = GNUNET_TIME_absolute_get();
6561 if ((ats->modified_addr == GNUNET_YES) || (ats->prob==NULL))
6563 ats->modified_addr = GNUNET_NO;
6564 ats_delete_problem ();
6565 ats_create_problem (ats->D, ats->U, ats->R, ats->v_b_min, ats->v_n_min, &ats->res);
6567 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers/Addresses were modified... new problem: %i peer, %i mechs\n", ats->res.c_peers, ats->res.c_mechs);
6570 else if (ats->modified_resources == GNUNET_YES)
6572 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "RESSOURCE...modified problem\n");
6573 ats_update_problem();
6575 else if (ats->modified_quality == GNUNET_YES)
6577 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "RESSOURCE...quality problem\n");
6578 ats_update_problem();
6581 else GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Problem is unmodified\n");
6584 if (ats->res.valid == GNUNET_YES)
6586 ats->res.solution = GNUNET_SYSERR;
6587 ats_solve_problem(ats->max_iterations, ats->max_exec_duration.rel_value, ats->res.c_peers, ats->res.c_mechs, &ats->res);
6588 if (ats->res.solution != 5)
6589 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Problem solution is not optimal: %i\n", ats->res.solution);
6592 duration = GNUNET_TIME_absolute_get_difference(start,GNUNET_TIME_absolute_get());
6594 if (ats->res.valid == GNUNET_YES)
6597 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP execution time in [ms] for %i mechanisms: %llu\n", ats->res.c_mechs, duration.rel_value);
6599 GNUNET_STATISTICS_set (stats, "ATS duration", duration.rel_value, GNUNET_NO);
6600 GNUNET_STATISTICS_set (stats, "ATS mechanisms", ats->res.c_mechs, GNUNET_NO);
6601 GNUNET_STATISTICS_set (stats, "ATS peers", ats->res.c_peers, GNUNET_NO);
6602 GNUNET_STATISTICS_set (stats, "ATS solution", ats->res.solution, GNUNET_NO);
6603 GNUNET_STATISTICS_set (stats, "ATS timestamp", start.abs_value, GNUNET_NO);
6606 else if (ats->res.valid == GNUNET_NO)
6608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP not executed: no addresses\n");
6611 ats->last = GNUNET_TIME_absolute_get();
6613 ats->modified_addr = GNUNET_NO;
6614 ats->modified_resources = GNUNET_NO;
6615 ats->modified_quality = GNUNET_NO;
6623 ats_schedule_calculation (void *cls,
6624 const struct GNUNET_SCHEDULER_TaskContext *tc)
6626 struct ATS_info *ats = (struct ATS_info *) cls;
6630 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6631 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
6635 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running scheduled calculation\n");
6638 ats_calculate_bandwidth_distribution (ats);
6640 ats->ats_task = GNUNET_SCHEDULER_add_delayed (ats->exec_intervall,
6641 &ats_schedule_calculation, ats);
6647 unsigned long long value;
6650 ats = GNUNET_malloc(sizeof (struct ATS_info));
6652 ats->min_delta = ATS_MIN_INTERVAL;
6653 ats->exec_intervall = ATS_EXEC_INTERVAL;
6654 ats->max_exec_duration = ATS_MAX_EXEC_DURATION;
6655 ats->max_iterations = ATS_MAX_ITERATIONS;
6656 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6659 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed, ATS not active");
6666 ats->v_b_min = 64000;
6672 /* loading cost ressources */
6673 for (c=0; c<available_ressources; c++)
6675 GNUNET_asprintf(§ion,"%s_UP",ressources[c].cfg_param);
6676 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
6678 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "transport",section, &value))
6681 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found ressource cost: [%s] = %llu\n", section, value);
6683 ressources[c].c_max = value;
6686 GNUNET_free (section);
6687 GNUNET_asprintf(§ion,"%s_DOWN",ressources[c].cfg_param);
6688 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
6690 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "transport",section, &value))
6693 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found ressource cost: [%s] = %llu\n", section, value);
6695 ressources[c].c_min = value;
6698 GNUNET_free (section);
6701 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_MLP"))
6702 ats->save_mlp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "transport","DUMP_MLP");
6704 if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_SOLUTION"))
6705 ats->save_solution = GNUNET_CONFIGURATION_get_value_yesno (cfg, "transport","DUMP_SOLUTION");
6707 ats->ats_task = GNUNET_SCHEDULER_add_now(&ats_schedule_calculation, ats);
6711 static void ats_shutdown ()
6714 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_destroy\n");
6716 if (ats->ats_task != GNUNET_SCHEDULER_NO_TASK)
6717 GNUNET_SCHEDULER_cancel(ats->ats_task);
6718 ats->ats_task = GNUNET_SCHEDULER_NO_TASK;
6720 ats_delete_problem ();
6726 void ats_notify_peer_connect (
6727 const struct GNUNET_PeerIdentity *peer,
6728 const struct GNUNET_TRANSPORT_ATS_Information *ats_data)
6732 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_notify_peer_connect: %s\n",GNUNET_i2s(peer));
6734 while (ntohl(ats_data[c].type)!=0)
6737 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats type [%i]: %i\n",ntohl(ats_data[c].type), ntohl(ats_data[c].value));
6742 ats->modified_addr = GNUNET_YES;
6744 ats_calculate_bandwidth_distribution(ats);
6747 void ats_notify_peer_disconnect (
6748 const struct GNUNET_PeerIdentity *peer)
6751 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ats_notify_peer_disconnect: %s\n",GNUNET_i2s(peer));
6754 ats->modified_addr = GNUNET_YES;
6756 ats_calculate_bandwidth_distribution (ats);
6759 struct ForeignAddressList * ats_get_preferred_address (
6760 struct NeighbourList *n)
6763 //GNUNET_log (GNUNET_ERROR_TYPE_BULK, "ats_get_prefered_transport for peer: %s\n",GNUNET_i2s(&n->id));
6765 struct ReadyList *next = n->plugins;
6766 while (next != NULL)
6769 //GNUNET_log (GNUNET_ERROR_TYPE_BULK, "plugin: %s %i\n",next->plugin->short_name,strcmp(next->plugin->short_name,"unix"));
6773 return find_ready_address(n);
6777 * Initiate transport service.
6779 * @param cls closure
6780 * @param server the initialized server
6781 * @param c configuration to use
6785 struct GNUNET_SERVER_Handle *server,
6786 const struct GNUNET_CONFIGURATION_Handle *c)
6788 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
6789 {&handle_start, NULL,
6790 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
6791 {&handle_hello, NULL,
6792 GNUNET_MESSAGE_TYPE_HELLO, 0},
6793 {&handle_send, NULL,
6794 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
6795 {&handle_request_connect, NULL,
6796 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT, sizeof(struct TransportRequestConnectMessage)},
6797 {&handle_set_quota, NULL,
6798 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
6799 {&handle_address_lookup, NULL,
6800 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
6802 {&handle_blacklist_init, NULL,
6803 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
6804 {&handle_blacklist_reply, NULL,
6805 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
6811 unsigned long long tneigh;
6815 stats = GNUNET_STATISTICS_create ("transport", cfg);
6816 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
6817 /* parse configuration */
6819 GNUNET_CONFIGURATION_get_value_number (c,
6824 GNUNET_CONFIGURATION_get_value_filename (c,
6826 "HOSTKEY", &keyfile)))
6828 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6830 ("Transport service is lacking key configuration settings. Exiting.\n"));
6831 GNUNET_SCHEDULER_shutdown ();
6834 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6837 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6838 validation_map = NULL;
6842 max_connect_per_transport = (uint32_t) tneigh;
6843 peerinfo = GNUNET_PEERINFO_connect (cfg);
6844 if (peerinfo == NULL)
6846 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6847 _("Could not access PEERINFO service. Exiting.\n"));
6848 GNUNET_SCHEDULER_shutdown ();
6851 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6854 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6855 validation_map = NULL;
6856 GNUNET_free (keyfile);
6859 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6860 GNUNET_free (keyfile);
6861 if (my_private_key == NULL)
6863 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6865 ("Transport service could not access hostkey. Exiting.\n"));
6866 GNUNET_SCHEDULER_shutdown ();
6869 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6872 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6873 validation_map = NULL;
6876 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6877 GNUNET_CRYPTO_hash (&my_public_key,
6878 sizeof (my_public_key), &my_identity.hashPubKey);
6879 /* setup notification */
6880 GNUNET_SERVER_disconnect_notify (server,
6881 &client_disconnect_notification, NULL);
6882 /* load plugins... */
6885 GNUNET_CONFIGURATION_get_value_string (c,
6886 "TRANSPORT", "PLUGINS", &plugs))
6888 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6889 _("Starting transport plugins `%s'\n"), plugs);
6890 pos = strtok (plugs, " ");
6893 start_transport (server, pos);
6895 pos = strtok (NULL, " ");
6897 GNUNET_free (plugs);
6899 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
6900 &shutdown_task, NULL);
6907 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport service ready.\n"));
6909 /* If we have a blacklist file, read from it */
6910 read_blacklist_file(cfg);
6911 /* process client requests */
6912 GNUNET_SERVER_add_handlers (server, handlers);
6917 * The main function for the transport service.
6919 * @param argc number of arguments from the command line
6920 * @param argv command line arguments
6921 * @return 0 ok, 1 on error
6924 main (int argc, char *const *argv)
6926 a2s (NULL, NULL, 0); /* make compiler happy */
6927 return (GNUNET_OK ==
6928 GNUNET_SERVICE_run (argc,
6931 GNUNET_SERVICE_OPTION_NONE,
6932 &run, NULL)) ? 0 : 1;
6935 /* end of gnunet-service-transport.c */