2 This file is part of GNUnet.
3 (C) 2009 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 2, 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
27 * - CHECK that 'address' being NULL in 'struct ForeignAddressList' is
28 * tolerated in the code everywhere (could not happen before)
31 * - This code uses 'GNUNET_a2s' for debug printing in many places,
32 * which is technically wrong since it assumes we have IP+Port
33 * (v4/v6) addresses. Once we add transports like http or smtp
34 * this will have to be changed!
37 #include "gnunet_client_lib.h"
38 #include "gnunet_container_lib.h"
39 #include "gnunet_constants.h"
40 #include "gnunet_getopt_lib.h"
41 #include "gnunet_hello_lib.h"
42 #include "gnunet_os_lib.h"
43 #include "gnunet_peerinfo_service.h"
44 #include "gnunet_plugin_lib.h"
45 #include "gnunet_protocols.h"
46 #include "gnunet_service_lib.h"
47 #include "gnunet_signatures.h"
48 #include "plugin_transport.h"
49 #include "transport.h"
52 * Should we do some additional checks (to validate behavior
55 #define EXTRA_CHECKS GNUNET_YES
58 * How many messages can we have pending for a given client process
59 * before we start to drop incoming messages? We typically should
60 * have only one client and so this would be the primary buffer for
61 * messages, so the number should be chosen rather generously.
63 * The expectation here is that most of the time the queue is large
64 * enough so that a drop is virtually never required.
66 #define MAX_PENDING 128
69 * How often should we try to reconnect to a peer using a particular
70 * transport plugin before giving up? Note that the plugin may be
71 * added back to the list after PLUGIN_RETRY_FREQUENCY expires.
73 #define MAX_CONNECT_RETRY 3
76 * Limit on the number of ready-to-run tasks when validating
77 * HELLOs. If more tasks are ready to run, we will drop
78 * HELLOs instead of validating them.
80 #define MAX_HELLO_LOAD 4
83 * How often must a peer violate bandwidth quotas before we start
84 * to simply drop its messages?
86 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
89 * How long until a HELLO verification attempt should time out?
90 * Must be rather small, otherwise a partially successful HELLO
91 * validation (some addresses working) might not be available
92 * before a client's request for a connection fails for good.
93 * Besides, if a single request to an address takes a long time,
94 * then the peer is unlikely worthwhile anyway.
96 #define HELLO_VERIFICATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
99 * Priority to use for PONG messages.
101 #define TRANSPORT_PONG_PRIORITY 4
104 * How often do we re-add (cheaper) plugins to our list of plugins
105 * to try for a given connected peer?
107 #define PLUGIN_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
110 * After how long do we expire an address in a HELLO that we just
111 * validated? This value is also used for our own addresses when we
114 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
118 * How long before an existing address expires should we again try to
119 * validate it? Must be (significantly) smaller than
120 * HELLO_ADDRESS_EXPIRATION.
122 #define HELLO_REVALIDATION_START_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
125 * Maximum frequency for re-evaluating latencies for all transport addresses.
127 #define LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
130 * Maximum frequency for re-evaluating latencies for connected addresses.
132 #define CONNECTED_LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
136 * List of addresses of other peers
138 struct ForeignAddressList
141 * This is a linked list.
143 struct ForeignAddressList *next;
146 * Which ready list does this entry belong to.
148 struct ReadyList *ready_list;
151 * How long until we auto-expire this address (unless it is
152 * re-confirmed by the transport)?
154 struct GNUNET_TIME_Absolute expires;
157 * Task used to re-validate addresses, updates latencies and
160 GNUNET_SCHEDULER_TaskIdentifier revalidate_task;
173 * Session (or NULL if no valid session currently exists or if the
174 * plugin does not use sessions).
176 struct Session *session;
179 * What was the last latency observed for this address, plugin and peer?
181 struct GNUNET_TIME_Relative latency;
184 * If we did not successfully transmit a message to the given peer
185 * via this connection during the specified time, we should consider
186 * the connection to be dead. This is used in the case that a TCP
187 * transport simply stalls writing to the stream but does not
188 * formerly get a signal that the other peer died.
190 struct GNUNET_TIME_Absolute timeout;
193 * How often have we tried to connect using this plugin? Used to
194 * discriminate against addresses that do not work well.
195 * FIXME: not yet used, but should be!
197 unsigned int connect_attempts;
200 * DV distance to this peer (1 if no DV is used).
201 * FIXME: need to set this from transport plugins!
206 * Have we ever estimated the latency of this address? Used to
207 * ensure that the first time we add an address, we immediately
213 * Are we currently connected via this address? The first time we
214 * successfully transmit or receive data to a peer via a particular
215 * address, we set this to GNUNET_YES. If we later get an error
216 * (disconnect notification, transmission failure, timeout), we set
217 * it back to GNUNET_NO.
222 * Is this plugin currently busy transmitting to the specific target?
223 * GNUNET_NO if not (initial, default state is GNUNET_NO). Internal
224 * messages do not count as 'in transmit'.
229 * Has this address been validated yet?
237 * Entry in linked list of network addresses for ourselves.
239 struct OwnAddressList
242 * This is a linked list.
244 struct OwnAddressList *next;
247 * The address, actually a pointer to the end
248 * of this struct. Do not free!
253 * How long until we auto-expire this address (unless it is
254 * re-confirmed by the transport)?
256 struct GNUNET_TIME_Absolute expires;
267 * Entry in linked list of all of our plugins.
269 struct TransportPlugin
273 * This is a linked list.
275 struct TransportPlugin *next;
278 * API of the transport as returned by the plugin's
279 * initialization function.
281 struct GNUNET_TRANSPORT_PluginFunctions *api;
284 * Short name for the plugin (i.e. "tcp").
289 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
294 * List of our known addresses for this transport.
296 struct OwnAddressList *addresses;
299 * Environment this transport service is using
302 struct GNUNET_TRANSPORT_PluginEnvironment env;
305 * ID of task that is used to clean up expired addresses.
307 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
310 * Set to GNUNET_YES if we need to scrap the existing list of
311 * "addresses" and start fresh when we receive the next address
312 * update from a transport. Set to GNUNET_NO if we should just add
313 * the new address to the list and wait for the commit call.
319 struct NeighbourList;
322 * For each neighbour we keep a list of messages
323 * that we still want to transmit to the neighbour.
329 * This is a doubly linked list.
331 struct MessageQueue *next;
334 * This is a doubly linked list.
336 struct MessageQueue *prev;
339 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
340 * stuck together in memory. Allocated at the end of this struct.
342 const char *message_buf;
345 * Size of the message buf
347 size_t message_buf_size;
350 * Client responsible for queueing the message;
351 * used to check that a client has no two messages
352 * pending for the same target. Can be NULL.
354 struct TransportClient *client;
357 * Using which specific address should we send this message?
359 struct ForeignAddressList *specific_address;
362 * Peer ID of the Neighbour this entry belongs to.
364 struct GNUNET_PeerIdentity neighbour_id;
367 * Plugin that we used for the transmission.
368 * NULL until we scheduled a transmission.
370 struct TransportPlugin *plugin;
373 * At what time should we fail?
375 struct GNUNET_TIME_Absolute timeout;
378 * Internal message of the transport system that should not be
379 * included in the usual SEND-SEND_OK transmission confirmation
380 * traffic management scheme. Typically, "internal_msg" will
381 * be set whenever "client" is NULL (but it is not strictly
387 * How important is the message?
389 unsigned int priority;
395 * For a given Neighbour, which plugins are available
396 * to talk to this peer and what are their costs?
401 * This is a linked list.
403 struct ReadyList *next;
406 * Which of our transport plugins does this entry
409 struct TransportPlugin *plugin;
412 * Transport addresses, latency, and readiness for
413 * this particular plugin.
415 struct ForeignAddressList *addresses;
418 * To which neighbour does this ready list belong to?
420 struct NeighbourList *neighbour;
426 * Entry in linked list of all of our current neighbours.
432 * This is a linked list.
434 struct NeighbourList *next;
437 * Which of our transports is connected to this peer
438 * and what is their status?
440 struct ReadyList *plugins;
443 * Head of list of messages we would like to send to this peer;
444 * must contain at most one message per client.
446 struct MessageQueue *messages_head;
449 * Tail of list of messages we would like to send to this peer; must
450 * contain at most one message per client.
452 struct MessageQueue *messages_tail;
455 * Buffer for at most one payload message used when we receive
456 * payload data before our PING-PONG has succeeded. We then
457 * store such messages in this intermediary buffer until the
458 * connection is fully up.
460 struct GNUNET_MessageHeader *pre_connect_message_buffer;
463 * Context for peerinfo iteration.
464 * NULL after we are done processing peerinfo's information.
466 struct GNUNET_PEERINFO_IteratorContext *piter;
469 * Public key for this peer. Valid only if the respective flag is set below.
471 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
474 * Identity of this neighbour.
476 struct GNUNET_PeerIdentity id;
479 * ID of task scheduled to run when this peer is about to
480 * time out (will free resources associated with the peer).
482 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
485 * ID of task scheduled to run when we should retry transmitting
486 * the head of the message queue. Actually triggered when the
487 * transmission is timing out (we trigger instantly when we have
488 * a chance of success).
490 GNUNET_SCHEDULER_TaskIdentifier retry_task;
493 * How long until we should consider this peer dead
494 * (if we don't receive another message in the
497 struct GNUNET_TIME_Absolute peer_timeout;
500 * Tracker for inbound bandwidth.
502 struct GNUNET_BANDWIDTH_Tracker in_tracker;
505 * The latency we have seen for this particular address for
506 * this particular peer. This latency may have been calculated
507 * over multiple transports. This value reflects how long it took
508 * us to receive a response when SENDING via this particular
509 * transport/neighbour/address combination!
511 * FIXME: we need to periodically send PINGs to update this
512 * latency (at least more often than the current "huge" (11h?)
515 struct GNUNET_TIME_Relative latency;
518 * How often has the other peer (recently) violated the
519 * inbound traffic limit? Incremented by 10 per violation,
520 * decremented by 1 per non-violation (for each
523 unsigned int quota_violation_count;
526 * DV distance to this peer (1 if no DV is used).
531 * Have we seen an PONG from this neighbour in the past (and
532 * not had a disconnect since)?
537 * Do we have a valid public key for this neighbour?
539 int public_key_valid;
544 * Message used to ask a peer to validate receipt (to check an address
547 struct TransportPingMessage
551 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
553 struct GNUNET_MessageHeader header;
556 * Random challenge number (in network byte order).
558 uint32_t challenge GNUNET_PACKED;
561 * Who is the intended recipient?
563 struct GNUNET_PeerIdentity target;
569 * Message used to validate a HELLO. The challenge is included in the
570 * confirmation to make matching of replies to requests possible. The
571 * signature signs the original challenge number, our public key, the
572 * sender's address (so that the sender can check that the address we
573 * saw is plausible for him and possibly detect a MiM attack) and a
574 * timestamp (to limit replay).<p>
576 * This message is followed by the address of the
577 * client that we are observing (which is part of what
580 struct TransportPongMessage
584 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
586 struct GNUNET_MessageHeader header;
589 * For padding, always zero.
591 uint32_t reserved GNUNET_PACKED;
596 struct GNUNET_CRYPTO_RsaSignature signature;
599 * What are we signing and why?
601 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
604 * Random challenge number (in network byte order).
606 uint32_t challenge GNUNET_PACKED;
609 * Who signed this message?
611 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded signer;
614 * Size of address appended to this message
622 * Linked list of messages to be transmitted to the client. Each
623 * entry is followed by the actual message.
625 struct ClientMessageQueueEntry
628 * This is a doubly-linked list.
630 struct ClientMessageQueueEntry *next;
633 * This is a doubly-linked list.
635 struct ClientMessageQueueEntry *prev;
640 * Client connected to the transport service.
642 struct TransportClient
646 * This is a linked list.
648 struct TransportClient *next;
651 * Handle to the client.
653 struct GNUNET_SERVER_Client *client;
656 * Linked list of messages yet to be transmitted to
659 struct ClientMessageQueueEntry *message_queue_head;
662 * Tail of linked list of messages yet to be transmitted to the
665 struct ClientMessageQueueEntry *message_queue_tail;
668 * Current transmit request handle.
670 struct GNUNET_CONNECTION_TransmitHandle *th;
673 * Is a call to "transmit_send_continuation" pending? If so, we
674 * must not free this struct (even if the corresponding client
675 * disconnects) and instead only remove it from the linked list and
676 * set the "client" field to NULL.
681 * Length of the list of messages pending for this client.
683 unsigned int message_count;
689 * Entry in map of all HELLOs awaiting validation.
691 struct ValidationEntry
695 * The address, actually a pointer to the end
696 * of this struct. Do not free!
701 * Name of the transport.
703 char *transport_name;
706 * The public key of the peer.
708 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
711 * ID of task that will clean up this entry if we don't succeed
712 * with the validation first.
714 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
717 * At what time did we send this validation?
719 struct GNUNET_TIME_Absolute send_time;
722 * Session being validated (or NULL for none).
724 struct Session *session;
732 * Challenge number we used.
740 * Context of currently active requests to peerinfo
741 * for validation of HELLOs.
743 struct CheckHelloValidatedContext
747 * This is a doubly-linked list.
749 struct CheckHelloValidatedContext *next;
752 * This is a doubly-linked list.
754 struct CheckHelloValidatedContext *prev;
757 * Hello that we are validating.
759 const struct GNUNET_HELLO_Message *hello;
762 * Context for peerinfo iteration.
763 * NULL after we are done processing peerinfo's information.
765 struct GNUNET_PEERINFO_IteratorContext *piter;
768 * Was a HELLO known for this peer to peerinfo?
778 static struct GNUNET_HELLO_Message *our_hello;
781 * "version" of "our_hello". Used to see if a given neighbour has
782 * already been sent the latest version of our HELLO message.
784 static unsigned int our_hello_version;
789 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
794 static struct GNUNET_PeerIdentity my_identity;
799 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
804 struct GNUNET_SCHEDULER_Handle *sched;
809 const struct GNUNET_CONFIGURATION_Handle *cfg;
812 * Linked list of all clients to this service.
814 static struct TransportClient *clients;
817 * All loaded plugins.
819 static struct TransportPlugin *plugins;
824 static struct GNUNET_SERVER_Handle *server;
827 * All known neighbours and their HELLOs.
829 static struct NeighbourList *neighbours;
832 * Number of neighbours we'd like to have.
834 static uint32_t max_connect_per_transport;
837 * Head of linked list.
839 static struct CheckHelloValidatedContext *chvc_head;
842 * Tail of linked list.
844 static struct CheckHelloValidatedContext *chvc_tail;
847 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
848 * of the given peer that we are currently validating).
850 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
853 * Handle for reporting statistics.
855 static struct GNUNET_STATISTICS_Handle *stats;
859 * The peer specified by the given neighbour has timed-out or a plugin
860 * has disconnected. We may either need to do nothing (other plugins
861 * still up), or trigger a full disconnect and clean up. This
862 * function updates our state and do the necessary notifications.
863 * Also notifies our clients that the neighbour is now officially
866 * @param n the neighbour list entry for the peer
867 * @param check should we just check if all plugins
868 * disconnected or must we ask all plugins to
871 static void disconnect_neighbour (struct NeighbourList *n, int check);
874 * Check the ready list for the given neighbour and if a plugin is
875 * ready for transmission (and if we have a message), do so!
877 * @param neighbour target peer for which to transmit
879 static void try_transmission_to_peer (struct NeighbourList *neighbour);
883 * Find an entry in the neighbour list for a particular peer.
885 * @return NULL if not found.
887 static struct NeighbourList *
888 find_neighbour (const struct GNUNET_PeerIdentity *key)
890 struct NeighbourList *head = neighbours;
892 while ((head != NULL) &&
893 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
900 * Find an entry in the transport list for a particular transport.
902 * @return NULL if not found.
904 static struct TransportPlugin *
905 find_transport (const char *short_name)
907 struct TransportPlugin *head = plugins;
908 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
915 * Function called to notify a client about the socket being ready to
916 * queue more data. "buf" will be NULL and "size" zero if the socket
917 * was closed for writing in the meantime.
920 * @param size number of bytes available in buf
921 * @param buf where the callee should write the message
922 * @return number of bytes written to buf
925 transmit_to_client_callback (void *cls, size_t size, void *buf)
927 struct TransportClient *client = cls;
928 struct ClientMessageQueueEntry *q;
931 const struct GNUNET_MessageHeader *msg;
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938 "Transmission to client failed, closing connection.\n");
939 /* fatal error with client, free message queue! */
940 while (NULL != (q = client->message_queue_head))
942 GNUNET_STATISTICS_update (stats,
943 gettext_noop ("# bytes discarded (could not transmit to client)"),
944 ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
946 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
947 client->message_queue_tail,
951 client->message_count = 0;
956 while (NULL != (q = client->message_queue_head))
958 msg = (const struct GNUNET_MessageHeader *) &q[1];
959 msize = ntohs (msg->size);
960 if (msize + tsize > size)
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "Transmitting message of type %u to client.\n",
967 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
968 client->message_queue_tail,
970 memcpy (&cbuf[tsize], msg, msize);
973 client->message_count--;
977 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
978 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
980 GNUNET_TIME_UNIT_FOREVER_REL,
981 &transmit_to_client_callback,
983 GNUNET_assert (client->th != NULL);
990 * Mark the given FAL entry as 'connected' (and hence preferred for
991 * sending); also mark all others for the same peer as 'not connected'
992 * (since only one can be preferred).
994 * @param fal address to set to 'connected'
997 mark_address_connected (struct ForeignAddressList *fal)
999 struct ForeignAddressList *pos;
1002 GNUNET_assert (GNUNET_YES == fal->validated);
1003 if (fal->connected == GNUNET_YES)
1004 return; /* nothing to do */
1006 pos = fal->ready_list->addresses;
1009 if (GNUNET_YES == pos->connected)
1011 GNUNET_break (cnt == GNUNET_YES);
1013 pos->connected = GNUNET_NO;
1017 fal->connected = GNUNET_YES;
1018 if (GNUNET_YES == cnt)
1020 GNUNET_STATISTICS_update (stats,
1021 gettext_noop ("# connected addresses"),
1029 * Send the specified message to the specified client. Since multiple
1030 * messages may be pending for the same client at a time, this code
1031 * makes sure that no message is lost.
1033 * @param client client to transmit the message to
1034 * @param msg the message to send
1035 * @param may_drop can this message be dropped if the
1036 * message queue for this client is getting far too large?
1039 transmit_to_client (struct TransportClient *client,
1040 const struct GNUNET_MessageHeader *msg, int may_drop)
1042 struct ClientMessageQueueEntry *q;
1045 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1047 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1049 ("Dropping message, have %u messages pending (%u is the soft limit)\n"),
1050 client->message_count, MAX_PENDING);
1051 /* TODO: call to statistics... */
1054 msize = ntohs (msg->size);
1055 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1056 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1057 memcpy (&q[1], msg, msize);
1058 GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1059 client->message_queue_tail,
1060 client->message_queue_tail,
1062 client->message_count++;
1063 if (client->th == NULL)
1065 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1067 GNUNET_TIME_UNIT_FOREVER_REL,
1068 &transmit_to_client_callback,
1070 GNUNET_assert (client->th != NULL);
1076 * Transmit a 'SEND_OK' notification to the given client for the
1079 * @param client who to notify
1080 * @param n neighbour to notify about
1081 * @param result status code for the transmission request
1084 transmit_send_ok (struct TransportClient *client,
1085 struct NeighbourList *n,
1088 struct SendOkMessage send_ok_msg;
1090 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1091 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1092 send_ok_msg.success = htonl (result);
1093 send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1094 send_ok_msg.peer = n->id;
1095 transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1100 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1101 * upon "completion" of a send request. This tells the API
1102 * that it is now legal to send another message to the given
1105 * @param cls closure, identifies the entry on the
1106 * message queue that was transmitted and the
1107 * client responsible for queueing the message
1108 * @param target the peer receiving the message
1109 * @param result GNUNET_OK on success, if the transmission
1110 * failed, we should not tell the client to transmit
1114 transmit_send_continuation (void *cls,
1115 const struct GNUNET_PeerIdentity *target,
1118 struct MessageQueue *mq = cls;
1119 struct NeighbourList *n;
1121 GNUNET_STATISTICS_update (stats,
1122 gettext_noop ("# bytes pending with plugins"),
1123 - (int64_t) mq->message_buf_size,
1125 if (result == GNUNET_OK)
1127 GNUNET_STATISTICS_update (stats,
1128 gettext_noop ("# bytes successfully transmitted by plugins"),
1129 mq->message_buf_size,
1134 GNUNET_STATISTICS_update (stats,
1135 gettext_noop ("# bytes with transmission failure by plugins"),
1136 mq->message_buf_size,
1139 n = find_neighbour(&mq->neighbour_id);
1140 GNUNET_assert (n != NULL);
1141 if (mq->specific_address != NULL)
1143 if (result == GNUNET_OK)
1145 mq->specific_address->timeout =
1146 GNUNET_TIME_relative_to_absolute
1147 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1148 if (mq->specific_address->validated == GNUNET_YES)
1149 mark_address_connected (mq->specific_address);
1153 if (mq->specific_address->connected != GNUNET_NO)
1155 GNUNET_STATISTICS_update (stats,
1156 gettext_noop ("# connected addresses"),
1159 mq->specific_address->connected = GNUNET_NO;
1162 if (! mq->internal_msg)
1163 mq->specific_address->in_transmit = GNUNET_NO;
1165 if (mq->client != NULL)
1166 transmit_send_ok (mq->client, n, result);
1168 try_transmission_to_peer (n);
1173 * Find an address in any of the available transports for
1174 * the given neighbour that would be good for message
1175 * transmission. This is essentially the transport selection
1178 * @param neighbour for whom to select an address
1179 * @return selected address, NULL if we have none
1181 struct ForeignAddressList *
1182 find_ready_address(struct NeighbourList *neighbour)
1184 struct ReadyList *head = neighbour->plugins;
1185 struct ForeignAddressList *addresses;
1186 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1187 struct ForeignAddressList *best_address;
1189 best_address = NULL;
1190 while (head != NULL)
1192 addresses = head->addresses;
1193 while (addresses != NULL)
1195 if ( (addresses->timeout.value < now.value) &&
1196 (addresses->connected == GNUNET_YES) )
1199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1200 "Marking long-time inactive connection to `%4s' as down.\n",
1201 GNUNET_i2s (&neighbour->id));
1203 GNUNET_STATISTICS_update (stats,
1204 gettext_noop ("# connected addresses"),
1207 addresses->connected = GNUNET_NO;
1209 addresses = addresses->next;
1212 addresses = head->addresses;
1213 while (addresses != NULL)
1215 #if DEBUG_TRANSPORT > 1
1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1217 "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
1218 GNUNET_a2s (addresses->addr,
1219 addresses->addrlen),
1220 GNUNET_i2s (&neighbour->id),
1221 addresses->connected,
1222 addresses->in_transmit,
1223 addresses->validated,
1224 addresses->connect_attempts,
1225 (unsigned long long) addresses->timeout.value,
1226 (unsigned int) addresses->distance);
1228 if ( ( (best_address == NULL) ||
1229 (addresses->connected == GNUNET_YES) ||
1230 (best_address->connected == GNUNET_NO) ) &&
1231 (addresses->in_transmit == GNUNET_NO) &&
1232 ( (best_address == NULL) ||
1233 (addresses->latency.value < best_address->latency.value)) )
1234 best_address = addresses;
1235 /* FIXME: also give lower-latency addresses that are not
1236 connected a chance some times... */
1237 addresses = addresses->next;
1241 if (best_address != NULL)
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1245 "Best address found has latency of %llu ms.\n",
1246 best_address->latency.value);
1251 GNUNET_STATISTICS_update (stats,
1252 gettext_noop ("# transmission attempts failed (no address)"),
1256 return best_address;
1262 * We should re-try transmitting to the given peer,
1263 * hopefully we've learned something in the meantime.
1266 retry_transmission_task (void *cls,
1267 const struct GNUNET_SCHEDULER_TaskContext *tc)
1269 struct NeighbourList *n = cls;
1271 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1272 try_transmission_to_peer (n);
1277 * Check the ready list for the given neighbour and if a plugin is
1278 * ready for transmission (and if we have a message), do so!
1280 * @param neighbour target peer for which to transmit
1283 try_transmission_to_peer (struct NeighbourList *neighbour)
1285 struct ReadyList *rl;
1286 struct MessageQueue *mq;
1287 struct GNUNET_TIME_Relative timeout;
1291 if (neighbour->messages_head == NULL)
1294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1295 "Transmission queue for `%4s' is empty\n",
1296 GNUNET_i2s (&neighbour->id));
1298 return; /* nothing to do */
1301 mq = neighbour->messages_head;
1302 force_address = GNUNET_YES;
1303 if (mq->specific_address == NULL)
1305 mq->specific_address = find_ready_address(neighbour);
1306 GNUNET_STATISTICS_update (stats,
1307 gettext_noop ("# transport selected peer address freely"),
1310 force_address = GNUNET_NO;
1312 if (mq->specific_address == NULL)
1314 GNUNET_STATISTICS_update (stats,
1315 gettext_noop ("# transport failed to selected peer address"),
1318 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1319 if (timeout.value == 0)
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323 "No destination address available to transmit message of size %u to peer `%4s'\n",
1324 mq->message_buf_size,
1325 GNUNET_i2s (&mq->neighbour_id));
1327 GNUNET_STATISTICS_update (stats,
1328 gettext_noop ("# bytes in message queue for other peers"),
1329 - (int64_t) mq->message_buf_size,
1331 GNUNET_STATISTICS_update (stats,
1332 gettext_noop ("# bytes discarded (no destination address available)"),
1333 mq->message_buf_size,
1335 if (mq->client != NULL)
1336 transmit_send_ok (mq->client, neighbour, GNUNET_NO);
1337 GNUNET_CONTAINER_DLL_remove (neighbour->messages_head,
1338 neighbour->messages_tail,
1341 return; /* nobody ready */
1343 GNUNET_STATISTICS_update (stats,
1344 gettext_noop ("# message delivery deferred (no address)"),
1347 if (neighbour->retry_task != GNUNET_SCHEDULER_NO_TASK)
1348 GNUNET_SCHEDULER_cancel (sched,
1349 neighbour->retry_task);
1350 neighbour->retry_task = GNUNET_SCHEDULER_add_delayed (sched,
1352 &retry_transmission_task,
1355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1356 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
1357 mq->message_buf_size,
1358 GNUNET_i2s (&mq->neighbour_id),
1361 /* FIXME: might want to trigger peerinfo lookup here
1362 (unless that's already pending...) */
1365 GNUNET_CONTAINER_DLL_remove (neighbour->messages_head,
1366 neighbour->messages_tail,
1368 if (mq->specific_address->connected == GNUNET_NO)
1369 mq->specific_address->connect_attempts++;
1370 rl = mq->specific_address->ready_list;
1371 mq->plugin = rl->plugin;
1372 if (!mq->internal_msg)
1373 mq->specific_address->in_transmit = GNUNET_YES;
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
1377 mq->message_buf_size,
1378 GNUNET_i2s (&neighbour->id),
1379 GNUNET_a2s (mq->specific_address->addr,
1380 mq->specific_address->addrlen),
1381 rl->plugin->short_name);
1383 GNUNET_STATISTICS_update (stats,
1384 gettext_noop ("# bytes in message queue for other peers"),
1385 - (int64_t) mq->message_buf_size,
1387 GNUNET_STATISTICS_update (stats,
1388 gettext_noop ("# bytes pending with plugins"),
1389 mq->message_buf_size,
1391 ret = rl->plugin->api->send (rl->plugin->api->cls,
1394 mq->message_buf_size,
1396 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1397 mq->specific_address->session,
1398 mq->specific_address->addr,
1399 mq->specific_address->addrlen,
1401 &transmit_send_continuation, mq);
1404 /* failure, but 'send' would not call continuation in this case,
1405 so we need to do it here! */
1406 transmit_send_continuation (mq,
1414 * Send the specified message to the specified peer.
1416 * @param client source of the transmission request (can be NULL)
1417 * @param peer_address ForeignAddressList where we should send this message
1418 * @param priority how important is the message
1419 * @param timeout how long do we have to transmit?
1420 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
1421 * @param message_buf_size total size of all messages in message_buf
1422 * @param is_internal is this an internal message; these are pre-pended and
1423 * also do not count for plugins being "ready" to transmit
1424 * @param neighbour handle to the neighbour for transmission
1427 transmit_to_peer (struct TransportClient *client,
1428 struct ForeignAddressList *peer_address,
1429 unsigned int priority,
1430 struct GNUNET_TIME_Relative timeout,
1431 const char *message_buf,
1432 size_t message_buf_size,
1433 int is_internal, struct NeighbourList *neighbour)
1435 struct MessageQueue *mq;
1440 /* check for duplicate submission */
1441 mq = neighbour->messages_head;
1444 if (mq->client == client)
1446 /* client transmitted to same peer twice
1447 before getting SEND_OK! */
1455 GNUNET_STATISTICS_update (stats,
1456 gettext_noop ("# bytes in message queue for other peers"),
1459 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
1460 mq->specific_address = peer_address;
1461 mq->client = client;
1462 memcpy (&mq[1], message_buf, message_buf_size);
1463 mq->message_buf = (const char*) &mq[1];
1464 mq->message_buf_size = message_buf_size;
1465 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
1466 mq->internal_msg = is_internal;
1467 mq->priority = priority;
1468 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1470 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
1471 neighbour->messages_tail,
1474 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
1475 neighbour->messages_tail,
1476 neighbour->messages_tail,
1478 try_transmission_to_peer (neighbour);
1485 struct GeneratorContext
1487 struct TransportPlugin *plug_pos;
1488 struct OwnAddressList *addr_pos;
1489 struct GNUNET_TIME_Absolute expiration;
1497 address_generator (void *cls, size_t max, void *buf)
1499 struct GeneratorContext *gc = cls;
1502 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
1504 gc->plug_pos = gc->plug_pos->next;
1505 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
1507 if (NULL == gc->plug_pos)
1512 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
1515 gc->addr_pos->addrlen, buf, max);
1516 gc->addr_pos = gc->addr_pos->next;
1522 * Construct our HELLO message from all of the addresses of
1523 * all of the transports.
1528 struct GNUNET_HELLO_Message *hello;
1529 struct TransportClient *cpos;
1530 struct NeighbourList *npos;
1531 struct GeneratorContext gc;
1533 gc.plug_pos = plugins;
1534 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
1535 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1536 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1539 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
1541 GNUNET_STATISTICS_update (stats,
1542 gettext_noop ("# refreshed my HELLO"),
1546 while (cpos != NULL)
1548 transmit_to_client (cpos,
1549 (const struct GNUNET_MessageHeader *) hello,
1554 GNUNET_free_non_null (our_hello);
1556 our_hello_version++;
1557 GNUNET_PEERINFO_add_peer (cfg, sched, &my_identity, our_hello);
1559 while (npos != NULL)
1562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1563 "Transmitting updated `%s' to neighbour `%4s'\n",
1564 "HELLO", GNUNET_i2s (&npos->id));
1566 GNUNET_STATISTICS_update (stats,
1567 gettext_noop ("# transmitted my HELLO to other peers"),
1570 transmit_to_peer (NULL, NULL, 0,
1571 HELLO_ADDRESS_EXPIRATION,
1572 (const char *) our_hello,
1573 GNUNET_HELLO_size(our_hello),
1581 * Task used to clean up expired addresses for a plugin.
1583 * @param cls closure
1587 expire_address_task (void *cls,
1588 const struct GNUNET_SCHEDULER_TaskContext *tc);
1592 * Update the list of addresses for this plugin,
1593 * expiring those that are past their expiration date.
1595 * @param plugin addresses of which plugin should be recomputed?
1596 * @param fresh set to GNUNET_YES if a new address was added
1597 * and we need to regenerate the HELLO even if nobody
1601 update_addresses (struct TransportPlugin *plugin, int fresh)
1603 static struct GNUNET_TIME_Absolute last_update;
1604 struct GNUNET_TIME_Relative min_remaining;
1605 struct GNUNET_TIME_Relative remaining;
1606 struct GNUNET_TIME_Absolute now;
1607 struct OwnAddressList *pos;
1608 struct OwnAddressList *prev;
1609 struct OwnAddressList *next;
1612 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_TASK)
1613 GNUNET_SCHEDULER_cancel (plugin->env.sched, plugin->address_update_task);
1614 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1615 now = GNUNET_TIME_absolute_get ();
1616 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
1617 expired = (GNUNET_TIME_absolute_get_duration (last_update).value > (HELLO_ADDRESS_EXPIRATION.value / 4));
1619 pos = plugin->addresses;
1623 if (pos->expires.value < now.value)
1625 expired = GNUNET_YES;
1627 plugin->addresses = pos->next;
1629 prev->next = pos->next;
1634 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
1635 if (remaining.value < min_remaining.value)
1636 min_remaining = remaining;
1642 if (expired || fresh)
1647 min_remaining = GNUNET_TIME_relative_min (min_remaining,
1648 GNUNET_TIME_relative_divide (HELLO_ADDRESS_EXPIRATION,
1650 plugin->address_update_task
1651 = GNUNET_SCHEDULER_add_delayed (plugin->env.sched,
1653 &expire_address_task, plugin);
1658 * Task used to clean up expired addresses for a plugin.
1660 * @param cls closure
1664 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1666 struct TransportPlugin *plugin = cls;
1668 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1669 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1670 update_addresses (plugin, GNUNET_NO);
1675 * Iterator over hash map entries that NULLs the session of validation
1676 * entries that match the given session.
1678 * @param cls closure (the 'struct Session*' to match against)
1679 * @param key current key code (peer ID, not used)
1680 * @param value value in the hash map ('struct ValidationEntry*')
1681 * @return GNUNET_YES (we should continue to iterate)
1684 remove_session_validations (void *cls,
1685 const GNUNET_HashCode * key,
1688 struct Session *session = cls;
1689 struct ValidationEntry *ve = value;
1691 if (session == ve->session)
1698 * Function that will be called whenever the plugin internally
1699 * cleans up a session pointer and hence the service needs to
1700 * discard all of those sessions as well. Plugins that do not
1701 * use sessions can simply omit calling this function and always
1702 * use NULL wherever a session pointer is needed.
1704 * @param cls closure
1705 * @param peer which peer was the session for
1706 * @param session which session is being destoyed
1709 plugin_env_session_end (void *cls,
1710 const struct GNUNET_PeerIdentity *peer,
1711 struct Session *session)
1713 struct TransportPlugin *p = cls;
1714 struct NeighbourList *nl;
1715 struct ReadyList *rl;
1716 struct ForeignAddressList *pos;
1717 struct ForeignAddressList *prev;
1719 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
1720 &remove_session_validations,
1722 nl = find_neighbour (peer);
1728 if (rl->plugin == p)
1735 pos = rl->addresses;
1736 while ( (pos != NULL) &&
1737 (pos->session != session) )
1744 pos->session = NULL;
1745 if (pos->addrlen != 0)
1748 rl->addresses = pos->next;
1750 prev->next = pos->next;
1752 if (nl->received_pong == GNUNET_NO)
1753 return; /* nothing to do */
1754 /* check if we have any validated addresses left */
1755 pos = rl->addresses;
1762 /* no valid addresses left, signal disconnect! */
1763 disconnect_neighbour (nl, GNUNET_NO);
1768 * Function that must be called by each plugin to notify the
1769 * transport service about the addresses under which the transport
1770 * provided by the plugin can be reached.
1772 * @param cls closure
1773 * @param name name of the transport that generated the address
1774 * @param addr one of the addresses of the host, NULL for the last address
1775 * the specific address format depends on the transport
1776 * @param addrlen length of the address
1777 * @param expires when should this address automatically expire?
1780 plugin_env_notify_address (void *cls,
1784 struct GNUNET_TIME_Relative expires)
1786 struct TransportPlugin *p = cls;
1787 struct OwnAddressList *al;
1788 struct GNUNET_TIME_Absolute abex;
1790 abex = GNUNET_TIME_relative_to_absolute (expires);
1791 GNUNET_assert (p == find_transport (name));
1796 if ((addrlen == al->addrlen) && (0 == memcmp (addr, &al[1], addrlen)))
1798 if (al->expires.value < abex.value)
1805 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
1807 al->next = p->addresses;
1810 al->addrlen = addrlen;
1811 memcpy (&al[1], addr, addrlen);
1812 update_addresses (p, GNUNET_YES);
1817 * Notify all of our clients about a peer connecting.
1820 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
1821 struct GNUNET_TIME_Relative latency,
1824 struct ConnectInfoMessage cim;
1825 struct TransportClient *cpos;
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1829 "Notifying clients about connection from `%s'\n",
1832 GNUNET_STATISTICS_update (stats,
1833 gettext_noop ("# peers connected"),
1836 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
1837 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
1838 cim.distance = htonl (distance);
1839 cim.latency = GNUNET_TIME_relative_hton (latency);
1840 memcpy (&cim.id, peer, sizeof (struct GNUNET_PeerIdentity));
1842 while (cpos != NULL)
1844 transmit_to_client (cpos, &cim.header, GNUNET_NO);
1851 * Notify all of our clients about a peer disconnecting.
1854 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
1856 struct DisconnectInfoMessage dim;
1857 struct TransportClient *cpos;
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1861 "Notifying clients about lost connection to `%s'\n",
1864 GNUNET_STATISTICS_update (stats,
1865 gettext_noop ("# peers connected"),
1868 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
1869 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1870 dim.reserved = htonl (0);
1871 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
1873 while (cpos != NULL)
1875 transmit_to_client (cpos, &dim.header, GNUNET_NO);
1882 * Find a ForeignAddressList entry for the given neighbour
1883 * that matches the given address and transport.
1885 * @param neighbour which peer we care about
1886 * @param tname name of the transport plugin
1887 * @param session session to look for, NULL for 'any'; otherwise
1888 * can be used for the service to "learn" this session ID
1889 * @param addr binary address
1890 * @param addrlen length of addr
1891 * @return NULL if no such entry exists
1893 static struct ForeignAddressList *
1894 find_peer_address(struct NeighbourList *neighbour,
1896 struct Session *session,
1900 struct ReadyList *head;
1901 struct ForeignAddressList *address_head;
1903 head = neighbour->plugins;
1904 while (head != NULL)
1906 if (0 == strcmp (tname, head->plugin->short_name))
1913 address_head = head->addresses;
1914 while ( (address_head != NULL) &&
1915 ( (address_head->addrlen != addrlen) ||
1916 (memcmp(address_head->addr, addr, addrlen) != 0) ) )
1917 address_head = address_head->next;
1918 if ( (session != NULL) && (address_head != NULL) )
1919 address_head->session = session; /* learn it! */
1920 return address_head;
1925 * Get the peer address struct for the given neighbour and
1926 * address. If it doesn't yet exist, create it.
1928 * @param neighbour which peer we care about
1929 * @param tname name of the transport plugin
1930 * @param session session of the plugin, or NULL for none
1931 * @param addr binary address
1932 * @param addrlen length of addr
1933 * @return NULL if we do not have a transport plugin for 'tname'
1935 static struct ForeignAddressList *
1936 add_peer_address (struct NeighbourList *neighbour,
1938 struct Session *session,
1942 struct ReadyList *head;
1943 struct ForeignAddressList *ret;
1945 ret = find_peer_address (neighbour, tname, session, addr, addrlen);
1948 head = neighbour->plugins;
1949 while (head != NULL)
1951 if (0 == strcmp (tname, head->plugin->short_name))
1957 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
1958 ret->session = session;
1961 ret->addr = (const char*) &ret[1];
1962 memcpy (&ret[1], addr, addrlen);
1968 ret->addrlen = addrlen;
1969 ret->expires = GNUNET_TIME_relative_to_absolute
1970 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1971 ret->latency = GNUNET_TIME_relative_get_forever();
1973 ret->timeout = GNUNET_TIME_relative_to_absolute
1974 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1975 ret->ready_list = head;
1976 ret->next = head->addresses;
1977 head->addresses = ret;
1983 * Closure for 'add_validated_address'.
1985 struct AddValidatedAddressContext
1988 * Entry that has been validated.
1990 const struct ValidationEntry *ve;
1993 * Flag set after we have added the address so
1994 * that we terminate the iteration next time.
2001 * Callback function used to fill a buffer of max bytes with a list of
2002 * addresses in the format used by HELLOs. Should use
2003 * "GNUNET_HELLO_add_address" as a helper function.
2005 * @param cls the 'struct AddValidatedAddressContext' with the validated address
2006 * @param max maximum number of bytes that can be written to buf
2007 * @param buf where to write the address information
2008 * @return number of bytes written, 0 to signal the
2009 * end of the iteration.
2012 add_validated_address (void *cls,
2013 size_t max, void *buf)
2015 struct AddValidatedAddressContext *avac = cls;
2016 const struct ValidationEntry *ve = avac->ve;
2018 if (GNUNET_YES == avac->done)
2020 avac->done = GNUNET_YES;
2021 return GNUNET_HELLO_add_address (ve->transport_name,
2022 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2032 * Closure for 'check_address_exists'.
2034 struct CheckAddressExistsClosure
2037 * Address to check for.
2042 * Name of the transport.
2052 * Set to GNUNET_YES if the address exists.
2059 * Iterator over hash map entries. Checks if the given
2060 * validation entry is for the same address as what is given
2063 * @param cls the 'struct CheckAddressExistsClosure*'
2064 * @param key current key code (ignored)
2065 * @param value value in the hash map ('struct ValidationEntry')
2066 * @return GNUNET_YES if we should continue to
2067 * iterate (mismatch), GNUNET_NO if not (entry matched)
2070 check_address_exists (void *cls,
2071 const GNUNET_HashCode * key,
2074 struct CheckAddressExistsClosure *caec = cls;
2075 struct ValidationEntry *ve = value;
2076 if ( (0 == strcmp (caec->tname,
2077 ve->transport_name)) &&
2078 (caec->addrlen == ve->addrlen) &&
2079 (0 == memcmp (caec->addr,
2083 caec->exists = GNUNET_YES;
2091 * HELLO validation cleanup task (validation failed).
2093 * @param cls the 'struct ValidationEntry' that failed
2094 * @param tc scheduler context (unused)
2097 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2099 struct ValidationEntry *va = cls;
2100 struct GNUNET_PeerIdentity pid;
2102 GNUNET_STATISTICS_update (stats,
2103 gettext_noop ("# address validation timeouts"),
2106 GNUNET_CRYPTO_hash (&va->publicKey,
2108 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2110 GNUNET_CONTAINER_multihashmap_remove (validation_map,
2113 GNUNET_free (va->transport_name);
2119 neighbour_timeout_task (void *cls,
2120 const struct GNUNET_SCHEDULER_TaskContext *tc)
2122 struct NeighbourList *n = cls;
2125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2126 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
2128 GNUNET_STATISTICS_update (stats,
2129 gettext_noop ("# disconnects due to timeout"),
2132 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2133 disconnect_neighbour (n, GNUNET_NO);
2138 * Schedule the job that will cause us to send a PING to the
2139 * foreign address to evaluate its validity and latency.
2141 * @param fal address to PING
2144 schedule_next_ping (struct ForeignAddressList *fal);
2148 * Add the given address to the list of foreign addresses
2149 * available for the given peer (check for duplicates).
2151 * @param cls the respective 'struct NeighbourList' to update
2152 * @param tname name of the transport
2153 * @param expiration expiration time
2154 * @param addr the address
2155 * @param addrlen length of the address
2156 * @return GNUNET_OK (always)
2159 add_to_foreign_address_list (void *cls,
2161 struct GNUNET_TIME_Absolute expiration,
2162 const void *addr, size_t addrlen)
2164 struct NeighbourList *n = cls;
2165 struct ForeignAddressList *fal;
2168 GNUNET_STATISTICS_update (stats,
2169 gettext_noop ("# valid peer addresses returned by peerinfo"),
2173 fal = find_peer_address (n, tname, NULL, addr, addrlen);
2177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2178 "Adding address `%s' (%s) for peer `%4s' due to peerinfo data for %llums.\n",
2179 GNUNET_a2s (addr, addrlen),
2181 GNUNET_i2s (&n->id),
2184 fal = add_peer_address (n, tname, NULL, addr, addrlen);
2187 GNUNET_STATISTICS_update (stats,
2188 gettext_noop ("# previously validated addresses lacking transport"),
2194 fal->expires = GNUNET_TIME_absolute_max (expiration,
2196 schedule_next_ping (fal);
2202 fal->expires = GNUNET_TIME_absolute_max (expiration,
2207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2208 "Failed to add new address for `%4s'\n",
2209 GNUNET_i2s (&n->id));
2212 if (fal->validated == GNUNET_NO)
2214 fal->validated = GNUNET_YES;
2215 GNUNET_STATISTICS_update (stats,
2216 gettext_noop ("# peer addresses considered valid"),
2220 if (try == GNUNET_YES)
2222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2223 "Have new addresses, will try to trigger transmissions.\n");
2224 try_transmission_to_peer (n);
2231 * Add addresses in validated HELLO "h" to the set of addresses
2232 * we have for this peer.
2234 * @param cls closure ('struct NeighbourList*')
2235 * @param peer id of the peer, NULL for last call
2236 * @param h hello message for the peer (can be NULL)
2237 * @param trust amount of trust we have in the peer (not used)
2240 add_hello_for_peer (void *cls,
2241 const struct GNUNET_PeerIdentity *peer,
2242 const struct GNUNET_HELLO_Message *h,
2245 struct NeighbourList *n = cls;
2253 return; /* no HELLO available */
2255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2256 "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
2260 if (GNUNET_YES != n->public_key_valid)
2262 GNUNET_HELLO_get_key (h, &n->publicKey);
2263 n->public_key_valid = GNUNET_YES;
2265 GNUNET_HELLO_iterate_addresses (h,
2267 &add_to_foreign_address_list,
2273 * Create a fresh entry in our neighbour list for the given peer.
2274 * Will try to transmit our current HELLO to the new neighbour.
2276 * @param peer the peer for which we create the entry
2277 * @return the new neighbour list entry
2279 static struct NeighbourList *
2280 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer)
2282 struct NeighbourList *n;
2283 struct TransportPlugin *tp;
2284 struct ReadyList *rl;
2286 GNUNET_assert (our_hello != NULL);
2287 GNUNET_STATISTICS_update (stats,
2288 gettext_noop ("# active neighbours"),
2291 n = GNUNET_malloc (sizeof (struct NeighbourList));
2292 n->next = neighbours;
2296 GNUNET_TIME_relative_to_absolute
2297 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2298 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2299 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2300 MAX_BANDWIDTH_CARRY_S);
2304 if (tp->api->send != NULL)
2306 rl = GNUNET_malloc (sizeof (struct ReadyList));
2308 rl->next = n->plugins;
2311 rl->addresses = NULL;
2315 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2317 n->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
2318 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2319 &neighbour_timeout_task, n);
2320 n->piter = GNUNET_PEERINFO_iterate (cfg, sched, peer,
2321 0, GNUNET_TIME_UNIT_FOREVER_REL,
2322 &add_hello_for_peer, n);
2323 transmit_to_peer (NULL, NULL, 0,
2324 HELLO_ADDRESS_EXPIRATION,
2325 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
2332 * Send periodic PING messages to a give foreign address.
2334 * @param cls our 'struct PeriodicValidationContext*'
2335 * @param tc task context
2338 send_periodic_ping (void *cls,
2339 const struct GNUNET_SCHEDULER_TaskContext *tc)
2341 struct ForeignAddressList *peer_address = cls;
2342 struct TransportPlugin *tp;
2343 struct ValidationEntry *va;
2344 struct NeighbourList *neighbour;
2345 struct TransportPingMessage ping;
2346 struct CheckAddressExistsClosure caec;
2348 uint16_t hello_size;
2351 peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2352 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2354 tp = peer_address->ready_list->plugin;
2355 neighbour = peer_address->ready_list->neighbour;
2356 if (GNUNET_YES != neighbour->public_key_valid)
2358 /* no public key yet, try again later */
2359 schedule_next_ping (peer_address);
2362 caec.addr = peer_address->addr;
2363 caec.addrlen = peer_address->addrlen;
2364 caec.tname = tp->short_name;
2365 caec.exists = GNUNET_NO;
2366 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2367 &check_address_exists,
2369 if (caec.exists == GNUNET_YES)
2371 /* During validation attempts we will likely trigger the other
2372 peer trying to validate our address which in turn will cause
2373 it to send us its HELLO, so we expect to hit this case rather
2374 frequently. Only print something if we are very verbose. */
2375 #if DEBUG_TRANSPORT > 1
2376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2377 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
2378 GNUNET_a2s (peer_address->addr,
2379 peer_address->addrlen),
2381 GNUNET_i2s (&neighbour->id));
2383 schedule_next_ping (peer_address);
2386 va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
2387 va->transport_name = GNUNET_strdup (tp->short_name);
2388 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2390 va->send_time = GNUNET_TIME_absolute_get();
2391 va->session = peer_address->session;
2392 va->addr = (const void*) &va[1];
2393 memcpy (&va[1], peer_address->addr, peer_address->addrlen);
2394 va->addrlen = peer_address->addrlen;
2396 memcpy(&va->publicKey,
2397 &neighbour->publicKey,
2398 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2400 va->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
2401 HELLO_VERIFICATION_TIMEOUT,
2402 &timeout_hello_validation,
2404 GNUNET_CONTAINER_multihashmap_put (validation_map,
2405 &neighbour->id.hashPubKey,
2407 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2408 hello_size = GNUNET_HELLO_size(our_hello);
2409 tsize = sizeof(struct TransportPingMessage) + hello_size;
2410 message_buf = GNUNET_malloc(tsize);
2411 ping.challenge = htonl(va->challenge);
2412 ping.header.size = htons(sizeof(struct TransportPingMessage));
2413 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
2414 memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
2415 memcpy(message_buf, our_hello, hello_size);
2416 memcpy(&message_buf[hello_size],
2418 sizeof(struct TransportPingMessage));
2419 #if DEBUG_TRANSPORT_REVALIDATION
2420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2421 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
2422 GNUNET_a2s (peer_address->addr,
2423 peer_address->addrlen),
2425 GNUNET_i2s (&neighbour->id),
2426 "HELLO", hello_size,
2427 "PING", sizeof (struct TransportPingMessage));
2429 GNUNET_STATISTICS_update (stats,
2430 gettext_noop ("# PING messages sent for re-validation"),
2433 transmit_to_peer (NULL, peer_address,
2434 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2435 HELLO_VERIFICATION_TIMEOUT,
2437 GNUNET_YES, neighbour);
2438 GNUNET_free(message_buf);
2439 schedule_next_ping (peer_address);
2444 * Schedule the job that will cause us to send a PING to the
2445 * foreign address to evaluate its validity and latency.
2447 * @param fal address to PING
2450 schedule_next_ping (struct ForeignAddressList *fal)
2452 struct GNUNET_TIME_Relative delay;
2454 if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
2456 delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
2457 delay.value /= 2; /* do before expiration */
2458 delay = GNUNET_TIME_relative_min (delay,
2459 LATENCY_EVALUATION_MAX_DELAY);
2460 if (GNUNET_YES != fal->estimated)
2462 delay = GNUNET_TIME_UNIT_ZERO;
2463 fal->estimated = GNUNET_YES;
2465 if (GNUNET_YES == fal->connected)
2467 delay = GNUNET_TIME_relative_min (delay,
2468 CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
2470 /* FIXME: also adjust delay based on how close the last
2471 observed latency is to the latency of the best alternative */
2472 /* bound how fast we can go */
2473 delay = GNUNET_TIME_relative_max (delay,
2474 GNUNET_TIME_UNIT_SECONDS);
2475 /* randomize a bit (to avoid doing all at the same time) */
2476 delay.value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
2477 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(sched,
2479 &send_periodic_ping,
2487 * Function that will be called if we receive some payload
2488 * from another peer.
2490 * @param message the payload
2491 * @param n peer who claimed to be the sender
2494 handle_payload_message (const struct GNUNET_MessageHeader *message,
2495 struct NeighbourList *n)
2497 struct InboundMessage *im;
2498 struct TransportClient *cpos;
2501 msize = ntohs (message->size);
2502 if (n->received_pong == GNUNET_NO)
2504 GNUNET_free_non_null (n->pre_connect_message_buffer);
2505 n->pre_connect_message_buffer = GNUNET_malloc (msize);
2506 memcpy (n->pre_connect_message_buffer, message, msize);
2510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2511 "Received message of type %u from `%4s', sending to all clients.\n",
2512 ntohs (message->type),
2513 GNUNET_i2s (&n->id));
2515 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
2518 n->quota_violation_count++;
2520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2521 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
2522 n->in_tracker.available_bytes_per_s__,
2523 n->quota_violation_count);
2525 /* Discount 32k per violation */
2526 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
2531 if (n->quota_violation_count > 0)
2533 /* try to add 32k back */
2534 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
2536 n->quota_violation_count--;
2539 GNUNET_STATISTICS_update (stats,
2540 gettext_noop ("# payload received from other peers"),
2543 /* transmit message to all clients */
2544 im = GNUNET_malloc (sizeof (struct InboundMessage) + msize);
2545 im->header.size = htons (sizeof (struct InboundMessage) + msize);
2546 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
2547 im->latency = GNUNET_TIME_relative_hton (n->latency);
2549 memcpy (&im[1], message, msize);
2551 while (cpos != NULL)
2553 transmit_to_client (cpos, &im->header, GNUNET_YES);
2561 * Iterator over hash map entries. Checks if the given validation
2562 * entry is for the same challenge as what is given in the PONG.
2564 * @param cls the 'struct TransportPongMessage*'
2565 * @param key peer identity
2566 * @param value value in the hash map ('struct ValidationEntry')
2567 * @return GNUNET_YES if we should continue to
2568 * iterate (mismatch), GNUNET_NO if not (entry matched)
2571 check_pending_validation (void *cls,
2572 const GNUNET_HashCode * key,
2575 const struct TransportPongMessage *pong = cls;
2576 struct ValidationEntry *ve = value;
2577 struct AddValidatedAddressContext avac;
2578 unsigned int challenge = ntohl(pong->challenge);
2579 struct GNUNET_HELLO_Message *hello;
2580 struct GNUNET_PeerIdentity target;
2581 struct NeighbourList *n;
2582 struct ForeignAddressList *fal;
2583 struct GNUNET_MessageHeader *prem;
2585 if (ve->challenge != challenge)
2588 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PING,
2593 GNUNET_break_op (0);
2598 if (ve->addr != NULL)
2599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2600 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
2602 GNUNET_a2s ((const struct sockaddr *) ve->addr,
2604 ve->transport_name);
2606 GNUNET_STATISTICS_update (stats,
2607 gettext_noop ("# address validation successes"),
2610 /* create the updated HELLO */
2611 GNUNET_CRYPTO_hash (&ve->publicKey,
2612 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2613 &target.hashPubKey);
2614 avac.done = GNUNET_NO;
2616 hello = GNUNET_HELLO_create (&ve->publicKey,
2617 &add_validated_address,
2619 GNUNET_PEERINFO_add_peer (cfg, sched,
2622 GNUNET_free (hello);
2623 n = find_neighbour (&target);
2626 n->publicKey = ve->publicKey;
2627 n->public_key_valid = GNUNET_YES;
2628 fal = add_peer_address (n,
2633 GNUNET_assert (fal != NULL);
2634 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2635 fal->validated = GNUNET_YES;
2636 mark_address_connected (fal);
2637 GNUNET_STATISTICS_update (stats,
2638 gettext_noop ("# peer addresses considered valid"),
2641 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
2642 schedule_next_ping (fal);
2643 if (n->latency.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
2644 n->latency = fal->latency;
2646 n->latency.value = (fal->latency.value + n->latency.value) / 2;
2647 n->distance = fal->distance;
2648 if (GNUNET_NO == n->received_pong)
2650 n->received_pong = GNUNET_YES;
2651 notify_clients_connect (&target, n->latency, n->distance);
2652 if (NULL != (prem = n->pre_connect_message_buffer))
2654 n->pre_connect_message_buffer = NULL;
2655 handle_payload_message (prem, n);
2659 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
2661 GNUNET_SCHEDULER_cancel (sched,
2663 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
2664 try_transmission_to_peer (n);
2668 /* clean up validation entry */
2669 GNUNET_assert (GNUNET_YES ==
2670 GNUNET_CONTAINER_multihashmap_remove (validation_map,
2673 GNUNET_SCHEDULER_cancel (sched,
2675 GNUNET_free (ve->transport_name);
2682 * Function that will be called if we receive a validation
2683 * of an address challenge that we transmitted to another
2684 * peer. Note that the validation should only be considered
2685 * acceptable if the challenge matches AND if the sender
2686 * address is at least a plausible address for this peer
2687 * (otherwise we may be seeing a MiM attack).
2689 * @param cls closure
2690 * @param message the pong message
2691 * @param peer who responded to our challenge
2692 * @param sender_address string describing our sender address (as observed
2693 * by the other peer in binary format)
2694 * @param sender_address_len number of bytes in 'sender_address'
2697 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
2698 const struct GNUNET_PeerIdentity *peer,
2699 const char *sender_address,
2700 size_t sender_address_len)
2702 #if DEBUG_TRANSPORT > 1
2703 /* we get tons of these that just get discarded, only log
2704 if we are quite verbose */
2705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2706 "Receiving `%s' message from `%4s'.\n", "PONG",
2709 GNUNET_STATISTICS_update (stats,
2710 gettext_noop ("# PONG messages received"),
2713 if (GNUNET_SYSERR !=
2714 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
2716 &check_pending_validation,
2719 /* This is *expected* to happen a lot since we send
2720 PONGs to *all* known addresses of the sender of
2721 the PING, so most likely we get multiple PONGs
2722 per PING, and all but the first PONG will end up
2723 here. So really we should not print anything here
2724 unless we want to be very, very verbose... */
2725 #if DEBUG_TRANSPORT > 2
2726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2727 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
2736 /* FIXME: add given address to potential pool of our addresses
2738 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
2739 _("Another peer saw us using the address `%s' via `%s'.\n"),
2740 GNUNET_a2s ((const struct sockaddr *) &pong[1],
2741 ntohs(pong->addrlen)),
2742 va->transport_name);
2748 * Check if the given address is already being validated; if not,
2749 * append the given address to the list of entries that are being be
2750 * validated and initiate validation.
2752 * @param cls closure ('struct CheckHelloValidatedContext *')
2753 * @param tname name of the transport
2754 * @param expiration expiration time
2755 * @param addr the address
2756 * @param addrlen length of the address
2757 * @return GNUNET_OK (always)
2760 run_validation (void *cls,
2762 struct GNUNET_TIME_Absolute expiration,
2763 const void *addr, size_t addrlen)
2765 struct CheckHelloValidatedContext *chvc = cls;
2766 struct GNUNET_PeerIdentity id;
2767 struct TransportPlugin *tp;
2768 struct ValidationEntry *va;
2769 struct NeighbourList *neighbour;
2770 struct ForeignAddressList *peer_address;
2771 struct TransportPingMessage ping;
2772 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
2773 struct CheckAddressExistsClosure caec;
2775 uint16_t hello_size;
2778 GNUNET_STATISTICS_update (stats,
2779 gettext_noop ("# peer addresses scheduled for validation"),
2782 tp = find_transport (tname);
2785 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
2786 GNUNET_ERROR_TYPE_BULK,
2788 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
2790 GNUNET_STATISTICS_update (stats,
2791 gettext_noop ("# peer addresses not validated (no applicable transport plugin available)"),
2796 GNUNET_HELLO_get_key (chvc->hello, &pk);
2797 GNUNET_CRYPTO_hash (&pk,
2799 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2802 caec.addrlen = addrlen;
2804 caec.exists = GNUNET_NO;
2805 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2806 &check_address_exists,
2808 if (caec.exists == GNUNET_YES)
2810 /* During validation attempts we will likely trigger the other
2811 peer trying to validate our address which in turn will cause
2812 it to send us its HELLO, so we expect to hit this case rather
2813 frequently. Only print something if we are very verbose. */
2814 #if DEBUG_TRANSPORT > 1
2815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2816 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
2817 GNUNET_a2s (addr, addrlen),
2821 GNUNET_STATISTICS_update (stats,
2822 gettext_noop ("# peer addresses not validated (in progress)"),
2827 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
2828 va->transport_name = GNUNET_strdup (tname);
2829 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2831 va->send_time = GNUNET_TIME_absolute_get();
2832 va->addr = (const void*) &va[1];
2833 memcpy (&va[1], addr, addrlen);
2834 va->addrlen = addrlen;
2835 GNUNET_HELLO_get_key (chvc->hello,
2837 va->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
2838 HELLO_VERIFICATION_TIMEOUT,
2839 &timeout_hello_validation,
2841 GNUNET_CONTAINER_multihashmap_put (validation_map,
2844 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2845 neighbour = find_neighbour(&id);
2846 if (neighbour == NULL)
2847 neighbour = setup_new_neighbour(&id);
2848 neighbour->publicKey = va->publicKey;
2849 neighbour->public_key_valid = GNUNET_YES;
2850 peer_address = add_peer_address (neighbour, tname, NULL, addr, addrlen);
2851 GNUNET_assert(peer_address != NULL);
2852 hello_size = GNUNET_HELLO_size(our_hello);
2853 tsize = sizeof(struct TransportPingMessage) + hello_size;
2854 message_buf = GNUNET_malloc(tsize);
2855 ping.challenge = htonl(va->challenge);
2856 ping.header.size = htons(sizeof(struct TransportPingMessage));
2857 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
2858 memcpy(&ping.target, &id, sizeof(struct GNUNET_PeerIdentity));
2859 memcpy(message_buf, our_hello, hello_size);
2860 memcpy(&message_buf[hello_size],
2862 sizeof(struct TransportPingMessage));
2864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2865 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
2866 GNUNET_a2s (addr, addrlen),
2869 "HELLO", hello_size,
2870 "PING", sizeof (struct TransportPingMessage));
2872 GNUNET_STATISTICS_update (stats,
2873 gettext_noop ("# PING messages sent for initial validation"),
2876 transmit_to_peer (NULL, peer_address,
2877 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2878 HELLO_VERIFICATION_TIMEOUT,
2880 GNUNET_YES, neighbour);
2881 GNUNET_free(message_buf);
2887 * Check if addresses in validated hello "h" overlap with
2888 * those in "chvc->hello" and validate the rest.
2890 * @param cls closure
2891 * @param peer id of the peer, NULL for last call
2892 * @param h hello message for the peer (can be NULL)
2893 * @param trust amount of trust we have in the peer (not used)
2896 check_hello_validated (void *cls,
2897 const struct GNUNET_PeerIdentity *peer,
2898 const struct GNUNET_HELLO_Message *h,
2901 struct CheckHelloValidatedContext *chvc = cls;
2902 struct GNUNET_HELLO_Message *plain_hello;
2903 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
2904 struct GNUNET_PeerIdentity target;
2905 struct NeighbourList *n;
2910 GNUNET_CONTAINER_DLL_remove (chvc_head,
2913 if (GNUNET_NO == chvc->hello_known)
2915 /* notify PEERINFO about the peer now, so that we at least
2916 have the public key if some other component needs it */
2917 GNUNET_HELLO_get_key (chvc->hello, &pk);
2918 GNUNET_CRYPTO_hash (&pk,
2919 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2920 &target.hashPubKey);
2921 plain_hello = GNUNET_HELLO_create (&pk,
2924 GNUNET_PEERINFO_add_peer (cfg, sched, &target, plain_hello);
2925 GNUNET_free (plain_hello);
2927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2928 "Peerinfo had no `%s' message for peer `%4s', full validation needed.\n",
2930 GNUNET_i2s (&target));
2932 GNUNET_STATISTICS_update (stats,
2933 gettext_noop ("# new HELLOs requiring full validation"),
2936 GNUNET_HELLO_iterate_addresses (chvc->hello,
2943 GNUNET_STATISTICS_update (stats,
2944 gettext_noop ("# duplicate HELLO (peer known)"),
2954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2955 "Peerinfo had `%s' message for peer `%4s', validating only new addresses.\n",
2959 chvc->hello_known = GNUNET_YES;
2960 n = find_neighbour (peer);
2963 GNUNET_HELLO_iterate_addresses (h,
2965 &add_to_foreign_address_list,
2967 try_transmission_to_peer (n);
2971 GNUNET_STATISTICS_update (stats,
2972 gettext_noop ("# no existing neighbour record (validating HELLO)"),
2976 GNUNET_STATISTICS_update (stats,
2977 gettext_noop ("# HELLO validations (update case)"),
2980 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
2982 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
2988 * Process HELLO-message.
2990 * @param plugin transport involved, may be NULL
2991 * @param message the actual message
2992 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
2995 process_hello (struct TransportPlugin *plugin,
2996 const struct GNUNET_MessageHeader *message)
2999 struct GNUNET_PeerIdentity target;
3000 const struct GNUNET_HELLO_Message *hello;
3001 struct CheckHelloValidatedContext *chvc;
3002 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
3004 hsize = ntohs (message->size);
3005 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
3006 (hsize < sizeof (struct GNUNET_MessageHeader)))
3009 return GNUNET_SYSERR;
3011 GNUNET_STATISTICS_update (stats,
3012 gettext_noop ("# HELLOs received for validation"),
3015 /* first, check if load is too high */
3016 if (GNUNET_SCHEDULER_get_load (sched,
3017 GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
3019 GNUNET_STATISTICS_update (stats,
3020 gettext_noop ("# HELLOs ignored due to high load"),
3025 hello = (const struct GNUNET_HELLO_Message *) message;
3026 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
3028 GNUNET_break_op (0);
3029 return GNUNET_SYSERR;
3031 GNUNET_CRYPTO_hash (&publicKey,
3032 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3033 &target.hashPubKey);
3034 if (0 == memcmp (&my_identity,
3036 sizeof (struct GNUNET_PeerIdentity)))
3038 GNUNET_STATISTICS_update (stats,
3039 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
3044 #if DEBUG_TRANSPORT > 1
3045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3046 "Processing `%s' message for `%4s' of size %u\n",
3048 GNUNET_i2s (&target),
3049 GNUNET_HELLO_size(hello));
3051 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
3052 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
3053 memcpy (&chvc[1], hello, hsize);
3054 GNUNET_CONTAINER_DLL_insert (chvc_head,
3057 /* finally, check if HELLO was previously validated
3058 (continuation will then schedule actual validation) */
3059 chvc->piter = GNUNET_PEERINFO_iterate (cfg,
3063 HELLO_VERIFICATION_TIMEOUT,
3064 &check_hello_validated, chvc);
3070 * The peer specified by the given neighbour has timed-out or a plugin
3071 * has disconnected. We may either need to do nothing (other plugins
3072 * still up), or trigger a full disconnect and clean up. This
3073 * function updates our state and does the necessary notifications.
3074 * Also notifies our clients that the neighbour is now officially
3077 * @param n the neighbour list entry for the peer
3078 * @param check should we just check if all plugins
3079 * disconnected or must we ask all plugins to
3083 disconnect_neighbour (struct NeighbourList *n, int check)
3085 struct ReadyList *rpos;
3086 struct NeighbourList *npos;
3087 struct NeighbourList *nprev;
3088 struct MessageQueue *mq;
3089 struct ForeignAddressList *peer_addresses;
3090 struct ForeignAddressList *peer_pos;
3092 if (GNUNET_YES == check)
3095 while (NULL != rpos)
3097 peer_addresses = rpos->addresses;
3098 while (peer_addresses != NULL)
3100 if (GNUNET_YES == peer_addresses->connected)
3101 return; /* still connected */
3102 peer_addresses = peer_addresses->next;
3108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
3109 "Disconnecting from `%4s'\n",
3110 GNUNET_i2s (&n->id));
3112 /* remove n from neighbours list */
3115 while ((npos != NULL) && (npos != n))
3120 GNUNET_assert (npos != NULL);
3122 neighbours = n->next;
3124 nprev->next = n->next;
3126 /* notify all clients about disconnect */
3127 if (GNUNET_YES == n->received_pong)
3128 notify_clients_disconnect (&n->id);
3130 /* clean up all plugins, cancel connections and pending transmissions */
3131 while (NULL != (rpos = n->plugins))
3133 n->plugins = rpos->next;
3134 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
3135 while (rpos->addresses != NULL)
3137 peer_pos = rpos->addresses;
3138 rpos->addresses = peer_pos->next;
3139 if (peer_pos->connected == GNUNET_YES)
3140 GNUNET_STATISTICS_update (stats,
3141 gettext_noop ("# connected addresses"),
3144 if (GNUNET_YES == peer_pos->validated)
3145 GNUNET_STATISTICS_update (stats,
3146 gettext_noop ("# peer addresses considered valid"),
3149 if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
3151 GNUNET_SCHEDULER_cancel (sched,
3152 peer_pos->revalidate_task);
3153 peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3155 GNUNET_free(peer_pos);
3160 /* free all messages on the queue */
3161 while (NULL != (mq = n->messages_head))
3163 GNUNET_STATISTICS_update (stats,
3164 gettext_noop ("# bytes in message queue for other peers"),
3165 - (int64_t) mq->message_buf_size,
3167 GNUNET_STATISTICS_update (stats,
3168 gettext_noop ("# bytes discarded due to disconnect"),
3169 mq->message_buf_size,
3171 GNUNET_CONTAINER_DLL_remove (n->messages_head,
3174 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
3176 sizeof(struct GNUNET_PeerIdentity)));
3179 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
3181 GNUNET_SCHEDULER_cancel (sched, n->timeout_task);
3182 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3184 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
3186 GNUNET_SCHEDULER_cancel (sched, n->retry_task);
3187 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
3189 if (n->piter != NULL)
3191 GNUNET_PEERINFO_iterate_cancel (n->piter);
3194 /* finally, free n itself */
3195 GNUNET_STATISTICS_update (stats,
3196 gettext_noop ("# active neighbours"),
3199 GNUNET_free_non_null (n->pre_connect_message_buffer);
3205 * We have received a PING message from someone. Need to send a PONG message
3206 * in response to the peer by any means necessary.
3209 handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
3210 const struct GNUNET_PeerIdentity *peer,
3211 const char *sender_address,
3212 size_t sender_address_len)
3214 struct TransportPlugin *plugin = cls;
3215 struct TransportPingMessage *ping;
3216 struct TransportPongMessage *pong;
3217 struct NeighbourList *n;
3218 struct ReadyList *rl;
3219 struct ForeignAddressList *fal;
3221 if (ntohs (message->size) != sizeof (struct TransportPingMessage))
3223 GNUNET_break_op (0);
3224 return GNUNET_SYSERR;
3226 ping = (struct TransportPingMessage *) message;
3227 if (0 != memcmp (&ping->target,
3228 plugin->env.my_identity,
3229 sizeof (struct GNUNET_PeerIdentity)))
3231 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3232 _("Received `%s' message not destined for me!\n"),
3234 return GNUNET_SYSERR;
3237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
3238 "Processing `%s' from `%s'\n",
3240 GNUNET_a2s ((const struct sockaddr *)sender_address,
3241 sender_address_len));
3243 GNUNET_STATISTICS_update (stats,
3244 gettext_noop ("# PING messages received"),
3247 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len);
3248 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len);
3249 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
3250 pong->purpose.size =
3251 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3253 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + sender_address_len);
3254 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PING);
3255 pong->challenge = ping->challenge;
3256 pong->addrlen = htons(sender_address_len);
3257 memcpy(&pong->signer,
3259 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3260 memcpy (&pong[1], sender_address, sender_address_len);
3261 GNUNET_assert (GNUNET_OK ==
3262 GNUNET_CRYPTO_rsa_sign (my_private_key,
3263 &pong->purpose, &pong->signature));
3264 n = find_neighbour(peer);
3265 GNUNET_assert (n != NULL);
3266 /* first try reliable response transmission */
3270 fal = rl->addresses;
3273 if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
3276 ntohs (pong->header.size),
3277 TRANSPORT_PONG_PRIORITY,
3278 HELLO_VERIFICATION_TIMEOUT,
3286 GNUNET_STATISTICS_update (stats,
3287 gettext_noop ("# PONGs unicast via reliable transport"),
3297 /* no reliable method found, do multicast */
3298 GNUNET_STATISTICS_update (stats,
3299 gettext_noop ("# PONGs multicast to all available addresses"),
3305 fal = rl->addresses;
3308 transmit_to_peer(NULL, fal,
3309 TRANSPORT_PONG_PRIORITY,
3310 HELLO_VERIFICATION_TIMEOUT,
3312 ntohs(pong->header.size),
3325 * Function called by the plugin for each received message.
3326 * Update data volumes, possibly notify plugins about
3327 * reducing the rate at which they read from the socket
3328 * and generally forward to our receive callback.
3330 * @param cls the "struct TransportPlugin *" we gave to the plugin
3331 * @param peer (claimed) identity of the other peer
3332 * @param message the message, NULL if we only care about
3333 * learning about the delay until we should receive again
3334 * @param distance in overlay hops; use 1 unless DV (or 0 if message == NULL)
3335 * @param session identifier used for this session (can be NULL)
3336 * @param sender_address binary address of the sender (if observed)
3337 * @param sender_address_len number of bytes in sender_address
3338 * @return how long the plugin should wait until receiving more data
3339 * (plugins that do not support this, can ignore the return value)
3341 static struct GNUNET_TIME_Relative
3342 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
3343 const struct GNUNET_MessageHeader *message,
3344 unsigned int distance,
3345 struct Session *session,
3346 const char *sender_address,
3347 size_t sender_address_len)
3349 struct TransportPlugin *plugin = cls;
3350 struct ReadyList *service_context;
3351 struct ForeignAddressList *peer_address;
3353 struct NeighbourList *n;
3354 struct GNUNET_TIME_Relative ret;
3356 n = find_neighbour (peer);
3358 n = setup_new_neighbour (peer);
3359 service_context = n->plugins;
3360 while ((service_context != NULL) && (plugin != service_context->plugin))
3361 service_context = service_context->next;
3362 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
3363 peer_address = NULL;
3364 if (message != NULL)
3366 if ( (session != NULL) ||
3367 (sender_address != NULL) )
3368 peer_address = add_peer_address (n,
3372 sender_address_len);
3373 if (peer_address != NULL)
3375 peer_address->distance = distance;
3376 if (GNUNET_YES == peer_address->validated)
3377 mark_address_connected (peer_address);
3378 peer_address->timeout
3380 GNUNET_TIME_relative_to_absolute
3381 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3382 schedule_next_ping (peer_address);
3384 /* update traffic received amount ... */
3385 msize = ntohs (message->size);
3386 GNUNET_STATISTICS_update (stats,
3387 gettext_noop ("# bytes received from other peers"),
3390 n->distance = distance;
3392 GNUNET_TIME_relative_to_absolute
3393 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3394 GNUNET_SCHEDULER_cancel (sched,
3397 GNUNET_SCHEDULER_add_delayed (sched,
3398 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
3399 &neighbour_timeout_task, n);
3400 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
3402 /* dropping message due to frequent inbound volume violations! */
3403 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
3404 GNUNET_ERROR_TYPE_BULK,
3406 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
3407 n->in_tracker.available_bytes_per_s__,
3408 n->quota_violation_count);
3409 GNUNET_STATISTICS_update (stats,
3410 gettext_noop ("# bandwidth quota violations by other peers"),
3413 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
3415 switch (ntohs (message->type))
3417 case GNUNET_MESSAGE_TYPE_HELLO:
3418 GNUNET_STATISTICS_update (stats,
3419 gettext_noop ("# HELLO messages received from other peers"),
3422 process_hello (plugin, message);
3424 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
3425 handle_ping (plugin, message, peer, sender_address, sender_address_len);
3427 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
3428 handle_pong (plugin, message, peer, sender_address, sender_address_len);
3431 handle_payload_message (message, n);
3435 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
3438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3439 "Throttling read (%llu bytes excess at %u b/s), waiting %llums before reading more.\n",
3440 (unsigned long long) n->in_tracker.consumption_since_last_update__,
3441 (unsigned int) n->in_tracker.available_bytes_per_s__,
3442 (unsigned long long) ret.value);
3443 GNUNET_STATISTICS_update (stats,
3444 gettext_noop ("# ms throttling suggested"),
3445 (int64_t) ret.value,
3453 * Handle START-message. This is the first message sent to us
3454 * by any client which causes us to add it to our list.
3456 * @param cls closure (always NULL)
3457 * @param client identification of the client
3458 * @param message the actual message
3461 handle_start (void *cls,
3462 struct GNUNET_SERVER_Client *client,
3463 const struct GNUNET_MessageHeader *message)
3465 struct TransportClient *c;
3466 struct ConnectInfoMessage cim;
3467 struct NeighbourList *n;
3470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3471 "Received `%s' request from client\n", "START");
3476 if (c->client == client)
3478 /* client already on our list! */
3480 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3485 c = GNUNET_malloc (sizeof (struct TransportClient));
3489 if (our_hello != NULL)
3492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3493 "Sending our own `%s' to new client\n", "HELLO");
3495 transmit_to_client (c,
3496 (const struct GNUNET_MessageHeader *) our_hello,
3498 /* tell new client about all existing connections */
3499 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
3500 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
3504 if (GNUNET_YES == n->received_pong)
3507 cim.latency = GNUNET_TIME_relative_hton (n->latency);
3508 cim.distance = htonl (n->distance);
3509 transmit_to_client (c, &cim.header, GNUNET_NO);
3514 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3519 * Handle HELLO-message.
3521 * @param cls closure (always NULL)
3522 * @param client identification of the client
3523 * @param message the actual message
3526 handle_hello (void *cls,
3527 struct GNUNET_SERVER_Client *client,
3528 const struct GNUNET_MessageHeader *message)
3532 GNUNET_STATISTICS_update (stats,
3533 gettext_noop ("# HELLOs received from clients"),
3536 ret = process_hello (NULL, message);
3537 GNUNET_SERVER_receive_done (client, ret);
3542 * Handle SEND-message.
3544 * @param cls closure (always NULL)
3545 * @param client identification of the client
3546 * @param message the actual message
3549 handle_send (void *cls,
3550 struct GNUNET_SERVER_Client *client,
3551 const struct GNUNET_MessageHeader *message)
3553 struct TransportClient *tc;
3554 struct NeighbourList *n;
3555 const struct OutboundMessage *obm;
3556 const struct GNUNET_MessageHeader *obmm;
3560 size = ntohs (message->size);
3562 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
3565 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3568 GNUNET_STATISTICS_update (stats,
3569 gettext_noop ("# payload received for other peers"),
3572 obm = (const struct OutboundMessage *) message;
3574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3575 "Received `%s' request from client with target `%4s'\n",
3576 "SEND", GNUNET_i2s (&obm->peer));
3578 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
3579 msize = ntohs (obmm->size);
3580 if (size != msize + sizeof (struct OutboundMessage))
3583 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3586 n = find_neighbour (&obm->peer);
3588 n = setup_new_neighbour (&obm->peer);
3590 while ((tc != NULL) && (tc->client != client))
3594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3595 "Client asked to transmit %u-byte message of type %u to `%4s'\n",
3597 ntohs (obmm->type), GNUNET_i2s (&obm->peer));
3599 transmit_to_peer (tc, NULL, ntohl (obm->priority),
3600 GNUNET_TIME_relative_ntoh (obm->timeout),
3602 ntohs (obmm->size), GNUNET_NO, n);
3603 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3608 * Handle SET_QUOTA-message.
3610 * @param cls closure (always NULL)
3611 * @param client identification of the client
3612 * @param message the actual message
3615 handle_set_quota (void *cls,
3616 struct GNUNET_SERVER_Client *client,
3617 const struct GNUNET_MessageHeader *message)
3619 const struct QuotaSetMessage *qsm =
3620 (const struct QuotaSetMessage *) message;
3621 struct NeighbourList *n;
3623 GNUNET_STATISTICS_update (stats,
3624 gettext_noop ("# SET QUOTA messages received"),
3627 n = find_neighbour (&qsm->peer);
3630 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3631 GNUNET_STATISTICS_update (stats,
3632 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
3638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3639 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
3641 (unsigned int) ntohl (qsm->quota.value__),
3642 (unsigned int) n->in_tracker.available_bytes_per_s__,
3643 GNUNET_i2s (&qsm->peer));
3645 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
3647 if (0 == ntohl (qsm->quota.value__))
3648 disconnect_neighbour (n, GNUNET_NO);
3649 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3654 * Take the given address and append it to the set of results send back to
3657 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
3658 * @param address the resolved name, NULL to indicate the last response
3661 transmit_address_to_client (void *cls, const char *address)
3663 struct GNUNET_SERVER_TransmitContext *tc = cls;
3666 if (NULL == address)
3669 slen = strlen (address) + 1;
3670 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
3671 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
3672 if (NULL == address)
3673 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
3678 * Handle AddressLookup-message.
3680 * @param cls closure (always NULL)
3681 * @param client identification of the client
3682 * @param message the actual message
3685 handle_address_lookup (void *cls,
3686 struct GNUNET_SERVER_Client *client,
3687 const struct GNUNET_MessageHeader *message)
3689 const struct AddressLookupMessage *alum;
3690 struct TransportPlugin *lsPlugin;
3691 const char *nameTransport;
3692 const char *address;
3694 struct GNUNET_SERVER_TransmitContext *tc;
3695 struct GNUNET_TIME_Absolute timeout;
3696 struct GNUNET_TIME_Relative rtimeout;
3699 size = ntohs (message->size);
3700 if (size < sizeof (struct AddressLookupMessage))
3702 GNUNET_break_op (0);
3703 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3706 alum = (const struct AddressLookupMessage *) message;
3707 uint32_t addressLen = ntohl (alum->addrlen);
3708 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
3710 GNUNET_break_op (0);
3711 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3714 address = (const char *) &alum[1];
3715 nameTransport = (const char *) &address[addressLen];
3717 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
3719 GNUNET_break_op (0);
3720 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3723 timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
3724 rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
3725 numeric = ntohl (alum->numeric_only);
3726 lsPlugin = find_transport (nameTransport);
3727 if (NULL == lsPlugin)
3729 tc = GNUNET_SERVER_transmit_context_create (client);
3730 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
3731 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
3732 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
3735 tc = GNUNET_SERVER_transmit_context_create (client);
3736 lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
3738 address, addressLen,
3741 &transmit_address_to_client, tc);
3745 * List of handlers for the messages understood by this
3748 static struct GNUNET_SERVER_MessageHandler handlers[] = {
3749 {&handle_start, NULL,
3750 GNUNET_MESSAGE_TYPE_TRANSPORT_START, 0},
3751 {&handle_hello, NULL,
3752 GNUNET_MESSAGE_TYPE_HELLO, 0},
3753 {&handle_send, NULL,
3754 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
3755 {&handle_set_quota, NULL,
3756 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
3757 {&handle_address_lookup, NULL,
3758 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
3765 * Setup the environment for this plugin.
3768 create_environment (struct TransportPlugin *plug)
3770 plug->env.cfg = cfg;
3771 plug->env.sched = sched;
3772 plug->env.my_identity = &my_identity;
3773 plug->env.cls = plug;
3774 plug->env.receive = &plugin_env_receive;
3775 plug->env.notify_address = &plugin_env_notify_address;
3776 plug->env.session_end = &plugin_env_session_end;
3777 plug->env.max_connections = max_connect_per_transport;
3778 plug->env.stats = stats;
3783 * Start the specified transport (load the plugin).
3786 start_transport (struct GNUNET_SERVER_Handle *server,
3789 struct TransportPlugin *plug;
3792 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3793 _("Loading `%s' transport plugin\n"), name);
3794 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
3795 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
3796 create_environment (plug);
3797 plug->short_name = GNUNET_strdup (name);
3798 plug->lib_name = libname;
3799 plug->next = plugins;
3801 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
3802 if (plug->api == NULL)
3804 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3805 _("Failed to load transport plugin for `%s'\n"), name);
3806 GNUNET_free (plug->short_name);
3807 plugins = plug->next;
3808 GNUNET_free (libname);
3815 * Called whenever a client is disconnected. Frees our
3816 * resources associated with that client.
3818 * @param cls closure
3819 * @param client identification of the client
3822 client_disconnect_notification (void *cls,
3823 struct GNUNET_SERVER_Client *client)
3825 struct TransportClient *pos;
3826 struct TransportClient *prev;
3827 struct ClientMessageQueueEntry *mqe;
3832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
3833 "Client disconnected, cleaning up.\n");
3837 while ((pos != NULL) && (pos->client != client))
3844 while (NULL != (mqe = pos->message_queue_head))
3846 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
3847 pos->message_queue_tail,
3849 pos->message_count--;
3853 clients = pos->next;
3855 prev->next = pos->next;
3856 if (GNUNET_YES == pos->tcs_pending)
3861 if (pos->th != NULL)
3863 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
3866 GNUNET_break (0 == pos->message_count);
3872 * Iterator to free entries in the validation_map.
3874 * @param cls closure (unused)
3875 * @param key current key code
3876 * @param value value in the hash map (validation to abort)
3877 * @return GNUNET_YES (always)
3880 abort_validation (void *cls,
3881 const GNUNET_HashCode * key,
3884 struct ValidationEntry *va = value;
3886 GNUNET_SCHEDULER_cancel (sched, va->timeout_task);
3887 GNUNET_free (va->transport_name);
3894 * Function called when the service shuts down. Unloads our plugins
3895 * and cancels pending validations.
3897 * @param cls closure, unused
3898 * @param tc task context (unused)
3901 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3903 struct TransportPlugin *plug;
3904 struct OwnAddressList *al;
3905 struct CheckHelloValidatedContext *chvc;
3907 while (neighbours != NULL)
3908 disconnect_neighbour (neighbours, GNUNET_NO);
3910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3911 "Transport service is unloading plugins...\n");
3913 while (NULL != (plug = plugins))
3915 plugins = plug->next;
3916 if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
3918 GNUNET_SCHEDULER_cancel (plug->env.sched,
3919 plug->address_update_task);
3920 plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
3922 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
3923 GNUNET_free (plug->lib_name);
3924 GNUNET_free (plug->short_name);
3925 while (NULL != (al = plug->addresses))
3927 plug->addresses = al->next;
3932 if (my_private_key != NULL)
3933 GNUNET_CRYPTO_rsa_key_free (my_private_key);
3934 GNUNET_free_non_null (our_hello);
3936 /* free 'chvc' data structure */
3937 while (NULL != (chvc = chvc_head))
3939 chvc_head = chvc->next;
3940 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
3945 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3948 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
3949 validation_map = NULL;
3952 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
3959 * Initiate transport service.
3961 * @param cls closure
3962 * @param s scheduler to use
3963 * @param serv the initialized server
3964 * @param c configuration to use
3968 struct GNUNET_SCHEDULER_Handle *s,
3969 struct GNUNET_SERVER_Handle *serv,
3970 const struct GNUNET_CONFIGURATION_Handle *c)
3975 unsigned long long tneigh;
3980 stats = GNUNET_STATISTICS_create (sched, "transport", cfg);
3981 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
3982 /* parse configuration */
3984 GNUNET_CONFIGURATION_get_value_number (c,
3989 GNUNET_CONFIGURATION_get_value_filename (c,
3991 "HOSTKEY", &keyfile)))
3993 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3995 ("Transport service is lacking key configuration settings. Exiting.\n"));
3996 GNUNET_SCHEDULER_shutdown (s);
3999 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
4002 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
4003 validation_map = NULL;
4006 max_connect_per_transport = (uint32_t) tneigh;
4007 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4008 GNUNET_free (keyfile);
4009 if (my_private_key == NULL)
4011 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4013 ("Transport service could not access hostkey. Exiting.\n"));
4014 GNUNET_SCHEDULER_shutdown (s);
4017 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
4020 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
4021 validation_map = NULL;
4024 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4025 GNUNET_CRYPTO_hash (&my_public_key,
4026 sizeof (my_public_key), &my_identity.hashPubKey);
4027 /* setup notification */
4029 GNUNET_SERVER_disconnect_notify (server,
4030 &client_disconnect_notification, NULL);
4031 /* load plugins... */
4034 GNUNET_CONFIGURATION_get_value_string (c,
4035 "TRANSPORT", "PLUGINS", &plugs))
4037 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4038 _("Starting transport plugins `%s'\n"), plugs);
4039 pos = strtok (plugs, " ");
4042 start_transport (server, pos);
4044 pos = strtok (NULL, " ");
4046 GNUNET_free (plugs);
4048 GNUNET_SCHEDULER_add_delayed (sched,
4049 GNUNET_TIME_UNIT_FOREVER_REL,
4050 &shutdown_task, NULL);
4055 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport service ready.\n"));
4057 /* process client requests */
4058 GNUNET_SERVER_add_handlers (server, handlers);
4063 * The main function for the transport service.
4065 * @param argc number of arguments from the command line
4066 * @param argv command line arguments
4067 * @return 0 ok, 1 on error
4070 main (int argc, char *const *argv)
4072 return (GNUNET_OK ==
4073 GNUNET_SERVICE_run (argc,
4076 GNUNET_SERVICE_OPTION_NONE,
4077 &run, NULL)) ? 0 : 1;
4080 /* end of gnunet-service-transport.c */