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 * - bi-directional nature of TCP is not exploited
28 * - re-validation is broken (triggered only on successful validation,
29 * does not consider expiration times
33 * - This code uses 'GNUNET_a2s' for debug printing in many places,
34 * which is technically wrong since it assumes we have IP+Port
35 * (v4/v6) addresses. Once we add transports like http or smtp
36 * this will have to be changed!
39 #include "gnunet_client_lib.h"
40 #include "gnunet_container_lib.h"
41 #include "gnunet_constants.h"
42 #include "gnunet_getopt_lib.h"
43 #include "gnunet_hello_lib.h"
44 #include "gnunet_os_lib.h"
45 #include "gnunet_peerinfo_service.h"
46 #include "gnunet_plugin_lib.h"
47 #include "gnunet_protocols.h"
48 #include "gnunet_service_lib.h"
49 #include "gnunet_signatures.h"
50 #include "plugin_transport.h"
51 #include "transport.h"
54 * Should we do some additional checks (to validate behavior
57 #define EXTRA_CHECKS GNUNET_YES
60 * How many messages can we have pending for a given client process
61 * before we start to drop incoming messages? We typically should
62 * have only one client and so this would be the primary buffer for
63 * messages, so the number should be chosen rather generously.
65 * The expectation here is that most of the time the queue is large
66 * enough so that a drop is virtually never required.
68 #define MAX_PENDING 128
71 * How often should we try to reconnect to a peer using a particular
72 * transport plugin before giving up? Note that the plugin may be
73 * added back to the list after PLUGIN_RETRY_FREQUENCY expires.
75 #define MAX_CONNECT_RETRY 3
78 * Limit on the number of ready-to-run tasks when validating
79 * HELLOs. If more tasks are ready to run, we will drop
80 * HELLOs instead of validating them.
82 #define MAX_HELLO_LOAD 4
85 * How often must a peer violate bandwidth quotas before we start
86 * to simply drop its messages?
88 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
91 * How long until a HELLO verification attempt should time out?
92 * Must be rather small, otherwise a partially successful HELLO
93 * validation (some addresses working) might not be available
94 * before a client's request for a connection fails for good.
95 * Besides, if a single request to an address takes a long time,
96 * then the peer is unlikely worthwhile anyway.
98 #define HELLO_VERIFICATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
101 * How long will we allow sending of a ping to be delayed?
103 #define TRANSPORT_DEFAULT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
106 * How often will we re-validate for latency information
108 #define TRANSPORT_DEFAULT_REVALIDATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
111 * Priority to use for PONG messages.
113 #define TRANSPORT_PONG_PRIORITY 4
116 * How often do we re-add (cheaper) plugins to our list of plugins
117 * to try for a given connected peer?
119 #define PLUGIN_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
122 * After how long do we expire an address in a HELLO that we just
123 * validated? This value is also used for our own addresses when we
126 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
130 * How long before an existing address expires should we again try to
131 * validate it? Must be (significantly) smaller than
132 * HELLO_ADDRESS_EXPIRATION.
134 #define HELLO_REVALIDATION_START_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
138 * List of addresses of other peers
140 struct ForeignAddressList
143 * This is a linked list.
145 struct ForeignAddressList *next;
148 * Which ready list does this entry belong to.
150 struct ReadyList *ready_list;
153 * How long until we auto-expire this address (unless it is
154 * re-confirmed by the transport)?
156 struct GNUNET_TIME_Absolute expires;
159 * Task used to re-validate addresses, updates latencies and
162 GNUNET_SCHEDULER_TaskIdentifier revalidate_task;
175 * What was the last latency observed for this plugin
176 * and peer? Invalid if connected is GNUNET_NO.
178 struct GNUNET_TIME_Relative latency;
181 * If we did not successfully transmit a message to the given peer
182 * via this connection during the specified time, we should consider
183 * the connection to be dead. This is used in the case that a TCP
184 * transport simply stalls writing to the stream but does not
185 * formerly get a signal that the other peer died.
187 struct GNUNET_TIME_Absolute timeout;
190 * Are we currently connected via this address? The first time we
191 * successfully transmit or receive data to a peer via a particular
192 * address, we set this to GNUNET_YES. If we later get an error
193 * (disconnect notification, transmission failure, timeout), we set
194 * it back to GNUNET_NO.
199 * Is this plugin currently busy transmitting to the specific target?
200 * GNUNET_NO if not (initial, default state is GNUNET_NO). Internal
201 * messages do not count as 'in transmit'.
206 * Has this address been validated yet?
211 * How often have we tried to connect using this plugin? Used to
212 * discriminate against addresses that do not work well.
213 * FIXME: not yet used, but should be!
215 unsigned int connect_attempts;
218 * DV distance to this peer (1 if no DV is used).
219 * FIXME: need to set this from transport plugins!
227 * Entry in linked list of network addresses for ourselves.
229 struct OwnAddressList
232 * This is a linked list.
234 struct OwnAddressList *next;
237 * The address, actually a pointer to the end
238 * of this struct. Do not free!
243 * How long until we auto-expire this address (unless it is
244 * re-confirmed by the transport)?
246 struct GNUNET_TIME_Absolute expires;
257 * Entry in linked list of all of our plugins.
259 struct TransportPlugin
263 * This is a linked list.
265 struct TransportPlugin *next;
268 * API of the transport as returned by the plugin's
269 * initialization function.
271 struct GNUNET_TRANSPORT_PluginFunctions *api;
274 * Short name for the plugin (i.e. "tcp").
279 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
284 * List of our known addresses for this transport.
286 struct OwnAddressList *addresses;
289 * Environment this transport service is using
292 struct GNUNET_TRANSPORT_PluginEnvironment env;
295 * ID of task that is used to clean up expired addresses.
297 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
300 * Set to GNUNET_YES if we need to scrap the existing
301 * list of "addresses" and start fresh when we receive
302 * the next address update from a transport. Set to
303 * GNUNET_NO if we should just add the new address
304 * to the list and wait for the commit call.
310 struct NeighbourList;
313 * For each neighbour we keep a list of messages
314 * that we still want to transmit to the neighbour.
320 * This is a doubly linked list.
322 struct MessageQueue *next;
325 * This is a doubly linked list.
327 struct MessageQueue *prev;
330 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
331 * stuck together in memory. Allocated at the end of this struct.
333 const char *message_buf;
336 * Size of the message buf
338 size_t message_buf_size;
341 * Client responsible for queueing the message;
342 * used to check that a client has no two messages
343 * pending for the same target. Can be NULL.
345 struct TransportClient *client;
348 * Using which specific address should we send this message?
350 struct ForeignAddressList *specific_address;
353 * Peer ID of the Neighbour this entry belongs to.
355 struct GNUNET_PeerIdentity neighbour_id;
358 * Plugin that we used for the transmission.
359 * NULL until we scheduled a transmission.
361 struct TransportPlugin *plugin;
364 * At what time should we fail?
366 struct GNUNET_TIME_Absolute timeout;
369 * Internal message of the transport system that should not be
370 * included in the usual SEND-SEND_OK transmission confirmation
371 * traffic management scheme. Typically, "internal_msg" will
372 * be set whenever "client" is NULL (but it is not strictly
378 * How important is the message?
380 unsigned int priority;
386 * For a given Neighbour, which plugins are available
387 * to talk to this peer and what are their costs?
392 * This is a linked list.
394 struct ReadyList *next;
397 * Which of our transport plugins does this entry
400 struct TransportPlugin *plugin;
403 * Transport addresses, latency, and readiness for
404 * this particular plugin.
406 struct ForeignAddressList *addresses;
412 * Entry in linked list of all of our current neighbours.
418 * This is a linked list.
420 struct NeighbourList *next;
423 * Which of our transports is connected to this peer
424 * and what is their status?
426 struct ReadyList *plugins;
429 * Head of list of messages we would like to send to this peer;
430 * must contain at most one message per client.
432 struct MessageQueue *messages_head;
435 * Tail of list of messages we would like to send to this peer; must
436 * contain at most one message per client.
438 struct MessageQueue *messages_tail;
441 * Identity of this neighbour.
443 struct GNUNET_PeerIdentity id;
446 * ID of task scheduled to run when this peer is about to
447 * time out (will free resources associated with the peer).
449 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
452 * ID of task scheduled to run when we should retry transmitting
453 * the head of the message queue. Actually triggered when the
454 * transmission is timing out (we trigger instantly when we have
455 * a chance of success).
457 GNUNET_SCHEDULER_TaskIdentifier retry_task;
460 * How long until we should consider this peer dead
461 * (if we don't receive another message in the
464 struct GNUNET_TIME_Absolute peer_timeout;
467 * Tracker for inbound bandwidth.
469 struct GNUNET_BANDWIDTH_Tracker in_tracker;
472 * The latency we have seen for this particular address for
473 * this particular peer. This latency may have been calculated
474 * over multiple transports. This value reflects how long it took
475 * us to receive a response when SENDING via this particular
476 * transport/neighbour/address combination!
478 * FIXME: we need to periodically send PINGs to update this
479 * latency (at least more often than the current "huge" (11h?)
482 struct GNUNET_TIME_Relative latency;
485 * How often has the other peer (recently) violated the
486 * inbound traffic limit? Incremented by 10 per violation,
487 * decremented by 1 per non-violation (for each
490 unsigned int quota_violation_count;
493 * DV distance to this peer (1 if no DV is used).
498 * Have we seen an PONG from this neighbour in the past (and
499 * not had a disconnect since)?
506 * Message used to ask a peer to validate receipt (to check an address
509 struct TransportPingMessage
513 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
515 struct GNUNET_MessageHeader header;
518 * Random challenge number (in network byte order).
520 uint32_t challenge GNUNET_PACKED;
523 * Who is the intended recipient?
525 struct GNUNET_PeerIdentity target;
531 * Message used to validate a HELLO. The challenge is included in the
532 * confirmation to make matching of replies to requests possible. The
533 * signature signs the original challenge number, our public key, the
534 * sender's address (so that the sender can check that the address we
535 * saw is plausible for him and possibly detect a MiM attack) and a
536 * timestamp (to limit replay).<p>
538 * This message is followed by the address of the
539 * client that we are observing (which is part of what
542 struct TransportPongMessage
546 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
548 struct GNUNET_MessageHeader header;
551 * For padding, always zero.
553 uint32_t reserved GNUNET_PACKED;
558 struct GNUNET_CRYPTO_RsaSignature signature;
561 * What are we signing and why?
563 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
566 * Random challenge number (in network byte order).
568 uint32_t challenge GNUNET_PACKED;
571 * Who signed this message?
573 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded signer;
576 * Size of address appended to this message
584 * Linked list of messages to be transmitted to the client. Each
585 * entry is followed by the actual message.
587 struct ClientMessageQueueEntry
590 * This is a doubly-linked list.
592 struct ClientMessageQueueEntry *next;
595 * This is a doubly-linked list.
597 struct ClientMessageQueueEntry *prev;
602 * Client connected to the transport service.
604 struct TransportClient
608 * This is a linked list.
610 struct TransportClient *next;
613 * Handle to the client.
615 struct GNUNET_SERVER_Client *client;
618 * Linked list of messages yet to be transmitted to
621 struct ClientMessageQueueEntry *message_queue_head;
624 * Tail of linked list of messages yet to be transmitted to the
627 struct ClientMessageQueueEntry *message_queue_tail;
630 * Current transmit request handle.
632 struct GNUNET_CONNECTION_TransmitHandle *th;
635 * Is a call to "transmit_send_continuation" pending? If so, we
636 * must not free this struct (even if the corresponding client
637 * disconnects) and instead only remove it from the linked list and
638 * set the "client" field to NULL.
643 * Length of the list of messages pending for this client.
645 unsigned int message_count;
651 * Entry in map of all HELLOs awaiting validation.
653 struct ValidationEntry
657 * The address, actually a pointer to the end
658 * of this struct. Do not free!
663 * Name of the transport.
665 char *transport_name;
668 * The public key of the peer.
670 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
673 * ID of task that will clean up this entry if we don't succeed
674 * with the validation first.
676 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
679 * At what time did we send this validation?
681 struct GNUNET_TIME_Absolute send_time;
689 * Challenge number we used.
697 * Context of currently active requests to peerinfo
698 * for validation of HELLOs.
700 struct CheckHelloValidatedContext
704 * This is a doubly-linked list.
706 struct CheckHelloValidatedContext *next;
709 * This is a doubly-linked list.
711 struct CheckHelloValidatedContext *prev;
714 * Hello that we are validating.
716 const struct GNUNET_HELLO_Message *hello;
719 * Context for peerinfo iteration.
720 * NULL after we are done processing peerinfo's information.
722 struct GNUNET_PEERINFO_IteratorContext *piter;
725 * Was a HELLO known for this peer to peerinfo?
732 * Struct for keeping information about addresses to validate
733 * so that we can re-use for sending around ping's and receiving
734 * pongs periodically to keep connections alive and also better
735 * estimate latency of connections.
738 struct PeriodicValidationContext
742 * The address we are keeping alive
744 struct ForeignAddressList *foreign_address;
747 * The name of the transport
752 * Public Key of the peer to re-validate
754 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
761 static struct GNUNET_HELLO_Message *our_hello;
764 * "version" of "our_hello". Used to see if a given neighbour has
765 * already been sent the latest version of our HELLO message.
767 static unsigned int our_hello_version;
772 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
777 static struct GNUNET_PeerIdentity my_identity;
782 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
787 struct GNUNET_SCHEDULER_Handle *sched;
792 const struct GNUNET_CONFIGURATION_Handle *cfg;
795 * Linked list of all clients to this service.
797 static struct TransportClient *clients;
800 * All loaded plugins.
802 static struct TransportPlugin *plugins;
807 static struct GNUNET_SERVER_Handle *server;
810 * All known neighbours and their HELLOs.
812 static struct NeighbourList *neighbours;
815 * Number of neighbours we'd like to have.
817 static uint32_t max_connect_per_transport;
820 * Head of linked list.
822 static struct CheckHelloValidatedContext *chvc_head;
825 * Tail of linked list.
827 static struct CheckHelloValidatedContext *chvc_tail;
830 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
831 * of the given peer that we are currently validating).
833 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
836 * Handle for reporting statistics.
838 static struct GNUNET_STATISTICS_Handle *stats;
842 * The peer specified by the given neighbour has timed-out or a plugin
843 * has disconnected. We may either need to do nothing (other plugins
844 * still up), or trigger a full disconnect and clean up. This
845 * function updates our state and do the necessary notifications.
846 * Also notifies our clients that the neighbour is now officially
849 * @param n the neighbour list entry for the peer
850 * @param check should we just check if all plugins
851 * disconnected or must we ask all plugins to
854 static void disconnect_neighbour (struct NeighbourList *n, int check);
857 * Check the ready list for the given neighbour and if a plugin is
858 * ready for transmission (and if we have a message), do so!
860 * @param neighbour target peer for which to transmit
862 static void try_transmission_to_peer (struct NeighbourList *neighbour);
866 * Find an entry in the neighbour list for a particular peer.
867 * if sender_address is not specified (NULL) then return the
868 * first matching entry. If sender_address is specified, then
869 * make sure that the address and address_len also matches.
871 * FIXME: This description does not fit the function.
873 * @return NULL if not found.
875 static struct NeighbourList *
876 find_neighbour (const struct GNUNET_PeerIdentity *key)
878 struct NeighbourList *head = neighbours;
880 while ((head != NULL) &&
881 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
888 * Find an entry in the transport list for a particular transport.
890 * @return NULL if not found.
892 static struct TransportPlugin *
893 find_transport (const char *short_name)
895 struct TransportPlugin *head = plugins;
896 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
903 * Function called to notify a client about the socket being ready to
904 * queue more data. "buf" will be NULL and "size" zero if the socket
905 * was closed for writing in the meantime.
908 * @param size number of bytes available in buf
909 * @param buf where the callee should write the message
910 * @return number of bytes written to buf
913 transmit_to_client_callback (void *cls, size_t size, void *buf)
915 struct TransportClient *client = cls;
916 struct ClientMessageQueueEntry *q;
919 const struct GNUNET_MessageHeader *msg;
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 "Transmission to client failed, closing connection.\n");
927 /* fatal error with client, free message queue! */
928 while (NULL != (q = client->message_queue_head))
930 GNUNET_STATISTICS_update (stats,
931 gettext_noop ("# bytes discarded (could not transmit to client)"),
932 ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
934 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
935 client->message_queue_tail,
939 client->message_count = 0;
944 while (NULL != (q = client->message_queue_head))
946 msg = (const struct GNUNET_MessageHeader *) &q[1];
947 msize = ntohs (msg->size);
948 if (msize + tsize > size)
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
952 "Transmitting message of type %u to client.\n",
955 GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
956 client->message_queue_tail,
958 memcpy (&cbuf[tsize], msg, msize);
961 client->message_count--;
965 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
966 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
968 GNUNET_TIME_UNIT_FOREVER_REL,
969 &transmit_to_client_callback,
971 GNUNET_assert (client->th != NULL);
978 * Send the specified message to the specified client. Since multiple
979 * messages may be pending for the same client at a time, this code
980 * makes sure that no message is lost.
982 * @param client client to transmit the message to
983 * @param msg the message to send
984 * @param may_drop can this message be dropped if the
985 * message queue for this client is getting far too large?
988 transmit_to_client (struct TransportClient *client,
989 const struct GNUNET_MessageHeader *msg, int may_drop)
991 struct ClientMessageQueueEntry *q;
994 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
996 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
998 ("Dropping message, have %u messages pending (%u is the soft limit)\n"),
999 client->message_count, MAX_PENDING);
1000 /* TODO: call to statistics... */
1003 msize = ntohs (msg->size);
1004 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1005 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1006 memcpy (&q[1], msg, msize);
1007 GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1008 client->message_queue_tail,
1009 client->message_queue_tail,
1011 client->message_count++;
1012 if (client->th == NULL)
1014 client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1016 GNUNET_TIME_UNIT_FOREVER_REL,
1017 &transmit_to_client_callback,
1019 GNUNET_assert (client->th != NULL);
1025 * Transmit a 'SEND_OK' notification to the given client for the
1028 * @param client who to notify
1029 * @param n neighbour to notify about
1030 * @param result status code for the transmission request
1033 transmit_send_ok (struct TransportClient *client,
1034 struct NeighbourList *n,
1037 struct SendOkMessage send_ok_msg;
1039 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1040 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1041 send_ok_msg.success = htonl (result);
1042 send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1043 send_ok_msg.peer = n->id;
1044 transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1049 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1050 * upon "completion" of a send request. This tells the API
1051 * that it is now legal to send another message to the given
1054 * @param cls closure, identifies the entry on the
1055 * message queue that was transmitted and the
1056 * client responsible for queueing the message
1057 * @param target the peer receiving the message
1058 * @param result GNUNET_OK on success, if the transmission
1059 * failed, we should not tell the client to transmit
1063 transmit_send_continuation (void *cls,
1064 const struct GNUNET_PeerIdentity *target,
1067 struct MessageQueue *mq = cls;
1068 struct NeighbourList *n;
1070 if (result == GNUNET_OK)
1072 GNUNET_STATISTICS_update (stats,
1073 gettext_noop ("# bytes successfully transmitted by plugins"),
1074 mq->message_buf_size,
1079 GNUNET_STATISTICS_update (stats,
1080 gettext_noop ("# bytes with transmission failure by plugins"),
1081 mq->message_buf_size,
1084 n = find_neighbour(&mq->neighbour_id);
1085 GNUNET_assert (n != NULL);
1086 if (mq->specific_address != NULL)
1088 if (result == GNUNET_OK)
1090 mq->specific_address->timeout =
1091 GNUNET_TIME_relative_to_absolute
1092 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1093 if (mq->specific_address->connected != GNUNET_YES)
1095 GNUNET_STATISTICS_update (stats,
1096 gettext_noop ("# connected addresses"),
1099 mq->specific_address->connected = GNUNET_YES;
1104 if (mq->specific_address->connected != GNUNET_NO)
1106 GNUNET_STATISTICS_update (stats,
1107 gettext_noop ("# connected addresses"),
1110 mq->specific_address->connected = GNUNET_NO;
1113 if (! mq->internal_msg)
1114 mq->specific_address->in_transmit = GNUNET_NO;
1116 if (mq->client != NULL)
1117 transmit_send_ok (mq->client, n, result);
1119 try_transmission_to_peer (n);
1124 * Find an address in any of the available transports for
1125 * the given neighbour that would be good for message
1126 * transmission. This is essentially the transport selection
1129 * @param neighbour for whom to select an address
1130 * @return selected address, NULL if we have none
1132 struct ForeignAddressList *
1133 find_ready_address(struct NeighbourList *neighbour)
1135 struct ReadyList *head = neighbour->plugins;
1136 struct ForeignAddressList *addresses;
1137 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1138 struct ForeignAddressList *best_address;
1140 best_address = NULL;
1141 while (head != NULL)
1143 addresses = head->addresses;
1144 while (addresses != NULL)
1146 if ( (addresses->timeout.value < now.value) &&
1147 (addresses->connected == GNUNET_YES) )
1150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1151 "Marking long-time inactive connection to `%4s' as down.\n",
1152 GNUNET_i2s (&neighbour->id));
1154 GNUNET_STATISTICS_update (stats,
1155 gettext_noop ("# connected addresses"),
1158 addresses->connected = GNUNET_NO;
1160 addresses = addresses->next;
1163 addresses = head->addresses;
1164 while (addresses != NULL)
1166 if ( ( (best_address == NULL) ||
1167 (addresses->connected == GNUNET_YES) ||
1168 (best_address->connected == GNUNET_NO) ) &&
1169 (addresses->in_transmit == GNUNET_NO) &&
1170 ( (best_address == NULL) ||
1171 (addresses->latency.value < best_address->latency.value)) )
1172 best_address = addresses;
1173 /* FIXME: also give lower-latency addresses that are not
1174 connected a chance some times... */
1175 addresses = addresses->next;
1179 if (best_address != NULL)
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "Best address found has latency of %llu ms.\n",
1184 best_address->latency.value);
1189 GNUNET_STATISTICS_update (stats,
1190 gettext_noop ("# transmission attempts failed (no validated address)"),
1194 return best_address;
1200 * We should re-try transmitting to the given peer,
1201 * hopefully we've learned something in the meantime.
1204 retry_transmission_task (void *cls,
1205 const struct GNUNET_SCHEDULER_TaskContext *tc)
1207 struct NeighbourList *n = cls;
1209 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1210 try_transmission_to_peer (n);
1215 * Check the ready list for the given neighbour and if a plugin is
1216 * ready for transmission (and if we have a message), do so!
1218 * @param neighbour target peer for which to transmit
1221 try_transmission_to_peer (struct NeighbourList *neighbour)
1223 struct ReadyList *rl;
1224 struct MessageQueue *mq;
1225 struct GNUNET_TIME_Relative timeout;
1227 if (neighbour->messages_head == NULL)
1228 return; /* nothing to do */
1230 mq = neighbour->messages_head;
1231 /* FIXME: support bi-directional use of TCP */
1232 if (mq->specific_address == NULL)
1233 mq->specific_address = find_ready_address(neighbour);
1234 if (mq->specific_address == NULL)
1236 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1237 if (timeout.value == 0)
1240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1241 "No destination address available to transmit message of size %u to peer `%4s'\n",
1242 mq->message_buf_size,
1243 GNUNET_i2s (&mq->neighbour_id));
1245 GNUNET_STATISTICS_update (stats,
1246 gettext_noop ("# bytes in message queue for other peers"),
1247 -mq->message_buf_size,
1249 GNUNET_STATISTICS_update (stats,
1250 gettext_noop ("# bytes discarded (no destination address available)"),
1251 mq->message_buf_size,
1253 if (mq->client != NULL)
1254 transmit_send_ok (mq->client, neighbour, GNUNET_NO);
1255 GNUNET_CONTAINER_DLL_remove (neighbour->messages_head,
1256 neighbour->messages_tail,
1259 return; /* nobody ready */
1261 GNUNET_STATISTICS_update (stats,
1262 gettext_noop ("# message delivery deferred (no validated address)"),
1265 if (neighbour->retry_task != GNUNET_SCHEDULER_NO_TASK)
1266 GNUNET_SCHEDULER_cancel (sched,
1267 neighbour->retry_task);
1268 neighbour->retry_task = GNUNET_SCHEDULER_add_delayed (sched,
1270 &retry_transmission_task,
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1274 "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
1275 mq->message_buf_size,
1276 GNUNET_i2s (&mq->neighbour_id),
1279 /* FIXME: might want to trigger peerinfo lookup here
1280 (unless that's already pending...) */
1283 GNUNET_CONTAINER_DLL_remove (neighbour->messages_head,
1284 neighbour->messages_tail,
1286 if (mq->specific_address->connected == GNUNET_NO)
1287 mq->specific_address->connect_attempts++;
1288 rl = mq->specific_address->ready_list;
1289 mq->plugin = rl->plugin;
1290 if (!mq->internal_msg)
1291 mq->specific_address->in_transmit = GNUNET_YES;
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1294 "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
1295 mq->message_buf_size,
1296 GNUNET_i2s (&neighbour->id),
1297 GNUNET_a2s (mq->specific_address->addr,
1298 mq->specific_address->addrlen),
1299 rl->plugin->short_name);
1301 GNUNET_STATISTICS_update (stats,
1302 gettext_noop ("# bytes in message queue for other peers"),
1303 -mq->message_buf_size,
1305 GNUNET_STATISTICS_update (stats,
1306 gettext_noop ("# bytes transmitted to other peers"),
1307 mq->message_buf_size,
1309 rl->plugin->api->send (rl->plugin->api->cls,
1312 mq->message_buf_size,
1314 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1315 mq->specific_address->addr,
1316 mq->specific_address->addrlen,
1317 GNUNET_YES /* FIXME: sometimes, we want to be more tolerant here! */,
1318 &transmit_send_continuation, mq);
1323 * Send the specified message to the specified peer.
1325 * @param client source of the transmission request (can be NULL)
1326 * @param peer_address ForeignAddressList where we should send this message
1327 * @param priority how important is the message
1328 * @param timeout how long do we have to transmit?
1329 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
1330 * @param message_buf_size total size of all messages in message_buf
1331 * @param is_internal is this an internal message; these are pre-pended and
1332 * also do not count for plugins being "ready" to transmit
1333 * @param neighbour handle to the neighbour for transmission
1336 transmit_to_peer (struct TransportClient *client,
1337 struct ForeignAddressList *peer_address,
1338 unsigned int priority,
1339 struct GNUNET_TIME_Relative timeout,
1340 const char *message_buf,
1341 size_t message_buf_size,
1342 int is_internal, struct NeighbourList *neighbour)
1344 struct MessageQueue *mq;
1349 /* check for duplicate submission */
1350 mq = neighbour->messages_head;
1353 if (mq->client == client)
1355 /* client transmitted to same peer twice
1356 before getting SEND_OK! */
1364 GNUNET_STATISTICS_update (stats,
1365 gettext_noop ("# bytes in message queue for other peers"),
1368 mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
1369 mq->specific_address = peer_address;
1370 mq->client = client;
1371 memcpy (&mq[1], message_buf, message_buf_size);
1372 mq->message_buf = (const char*) &mq[1];
1373 mq->message_buf_size = message_buf_size;
1374 memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
1375 mq->internal_msg = is_internal;
1376 mq->priority = priority;
1377 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1379 GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
1380 neighbour->messages_tail,
1383 GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
1384 neighbour->messages_tail,
1385 neighbour->messages_tail,
1387 try_transmission_to_peer (neighbour);
1394 struct GeneratorContext
1396 struct TransportPlugin *plug_pos;
1397 struct OwnAddressList *addr_pos;
1398 struct GNUNET_TIME_Absolute expiration;
1406 address_generator (void *cls, size_t max, void *buf)
1408 struct GeneratorContext *gc = cls;
1411 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
1413 gc->plug_pos = gc->plug_pos->next;
1414 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
1416 if (NULL == gc->plug_pos)
1421 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
1424 gc->addr_pos->addrlen, buf, max);
1425 gc->addr_pos = gc->addr_pos->next;
1431 * Construct our HELLO message from all of the addresses of
1432 * all of the transports.
1437 struct GNUNET_HELLO_Message *hello;
1438 struct TransportClient *cpos;
1439 struct NeighbourList *npos;
1440 struct GeneratorContext gc;
1442 gc.plug_pos = plugins;
1443 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
1444 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1445 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1448 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
1450 GNUNET_STATISTICS_update (stats,
1451 gettext_noop ("# refreshed my HELLO"),
1455 while (cpos != NULL)
1457 transmit_to_client (cpos,
1458 (const struct GNUNET_MessageHeader *) hello,
1463 GNUNET_free_non_null (our_hello);
1465 our_hello_version++;
1466 GNUNET_PEERINFO_add_peer (cfg, sched, &my_identity, our_hello);
1468 while (npos != NULL)
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1472 "Transmitting updated `%s' to neighbour `%4s'\n",
1473 "HELLO", GNUNET_i2s (&npos->id));
1475 GNUNET_STATISTICS_update (stats,
1476 gettext_noop ("# transmitted my HELLO to other peers"),
1479 transmit_to_peer (NULL, NULL, 0,
1480 HELLO_ADDRESS_EXPIRATION,
1481 (const char *) our_hello,
1482 GNUNET_HELLO_size(our_hello),
1490 * Task used to clean up expired addresses for a plugin.
1492 * @param cls closure
1496 expire_address_task (void *cls,
1497 const struct GNUNET_SCHEDULER_TaskContext *tc);
1501 * Update the list of addresses for this plugin,
1502 * expiring those that are past their expiration date.
1504 * @param plugin addresses of which plugin should be recomputed?
1505 * @param fresh set to GNUNET_YES if a new address was added
1506 * and we need to regenerate the HELLO even if nobody
1510 update_addresses (struct TransportPlugin *plugin, int fresh)
1512 struct GNUNET_TIME_Relative min_remaining;
1513 struct GNUNET_TIME_Relative remaining;
1514 struct GNUNET_TIME_Absolute now;
1515 struct OwnAddressList *pos;
1516 struct OwnAddressList *prev;
1517 struct OwnAddressList *next;
1520 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_TASK)
1521 GNUNET_SCHEDULER_cancel (plugin->env.sched, plugin->address_update_task);
1522 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1523 now = GNUNET_TIME_absolute_get ();
1524 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
1525 expired = GNUNET_NO;
1527 pos = plugin->addresses;
1531 if (pos->expires.value < now.value)
1533 expired = GNUNET_YES;
1535 plugin->addresses = pos->next;
1537 prev->next = pos->next;
1542 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
1543 if (remaining.value < min_remaining.value)
1544 min_remaining = remaining;
1550 if (expired || fresh)
1552 if (min_remaining.value < GNUNET_TIME_UNIT_FOREVER_REL.value)
1553 plugin->address_update_task
1554 = GNUNET_SCHEDULER_add_delayed (plugin->env.sched,
1556 &expire_address_task, plugin);
1562 * Task used to clean up expired addresses for a plugin.
1564 * @param cls closure
1568 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1570 struct TransportPlugin *plugin = cls;
1571 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1572 update_addresses (plugin, GNUNET_NO);
1577 * Function that must be called by each plugin to notify the
1578 * transport service about the addresses under which the transport
1579 * provided by the plugin can be reached.
1581 * @param cls closure
1582 * @param name name of the transport that generated the address
1583 * @param addr one of the addresses of the host, NULL for the last address
1584 * the specific address format depends on the transport
1585 * @param addrlen length of the address
1586 * @param expires when should this address automatically expire?
1589 plugin_env_notify_address (void *cls,
1593 struct GNUNET_TIME_Relative expires)
1595 struct TransportPlugin *p = cls;
1596 struct OwnAddressList *al;
1597 struct GNUNET_TIME_Absolute abex;
1599 abex = GNUNET_TIME_relative_to_absolute (expires);
1600 GNUNET_assert (p == find_transport (name));
1605 if ((addrlen == al->addrlen) && (0 == memcmp (addr, &al[1], addrlen)))
1607 if (al->expires.value < abex.value)
1614 al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
1616 al->next = p->addresses;
1619 al->addrlen = addrlen;
1620 memcpy (&al[1], addr, addrlen);
1621 update_addresses (p, GNUNET_YES);
1626 * Notify all of our clients about a peer connecting.
1629 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
1630 struct GNUNET_TIME_Relative latency,
1633 struct ConnectInfoMessage cim;
1634 struct TransportClient *cpos;
1637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1638 "Notifying clients about connection from `%s'\n",
1641 GNUNET_STATISTICS_update (stats,
1642 gettext_noop ("# peers connected"),
1645 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
1646 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
1647 cim.distance = htonl (distance);
1648 cim.latency = GNUNET_TIME_relative_hton (latency);
1649 memcpy (&cim.id, peer, sizeof (struct GNUNET_PeerIdentity));
1651 while (cpos != NULL)
1653 transmit_to_client (cpos, &cim.header, GNUNET_NO);
1660 * Notify all of our clients about a peer disconnecting.
1663 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
1665 struct DisconnectInfoMessage dim;
1666 struct TransportClient *cpos;
1669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1670 "Notifying clients about lost connection to `%s'\n",
1673 GNUNET_STATISTICS_update (stats,
1674 gettext_noop ("# peers connected"),
1677 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
1678 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1679 dim.reserved = htonl (0);
1680 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
1682 while (cpos != NULL)
1684 transmit_to_client (cpos, &dim.header, GNUNET_NO);
1691 * Find a ForeignAddressList entry for the given neighbour
1692 * that matches the given address and transport.
1694 * @param neighbour which peer we care about
1695 * @param tname name of the transport plugin
1696 * @param addr binary address
1697 * @param addrlen length of addr
1698 * @return NULL if no such entry exists
1700 static struct ForeignAddressList *
1701 find_peer_address(struct NeighbourList *neighbour,
1706 struct ReadyList *head;
1707 struct ForeignAddressList *address_head;
1709 head = neighbour->plugins;
1710 while (head != NULL)
1712 if (0 == strcmp (tname, head->plugin->short_name))
1719 address_head = head->addresses;
1720 while ( (address_head != NULL) &&
1721 ( (address_head->addrlen != addrlen) ||
1722 (memcmp(address_head->addr, addr, addrlen) != 0) ) )
1723 address_head = address_head->next;
1724 return address_head;
1729 * Get the peer address struct for the given neighbour and
1730 * address. If it doesn't yet exist, create it.
1732 * @param neighbour which peer we care about
1733 * @param tname name of the transport plugin
1734 * @param addr binary address
1735 * @param addrlen length of addr
1736 * @return NULL if we do not have a transport plugin for 'tname'
1738 static struct ForeignAddressList *
1739 add_peer_address(struct NeighbourList *neighbour,
1744 struct ReadyList *head;
1745 struct ForeignAddressList *ret;
1747 ret = find_peer_address (neighbour, tname, addr, addrlen);
1750 head = neighbour->plugins;
1751 while (head != NULL)
1753 if (0 == strcmp (tname, head->plugin->short_name))
1759 ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
1760 ret->addr = (const char*) &ret[1];
1761 memcpy (&ret[1], addr, addrlen);
1762 ret->addrlen = addrlen;
1763 ret->expires = GNUNET_TIME_relative_to_absolute
1764 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1765 ret->latency = GNUNET_TIME_relative_get_forever();
1767 ret->timeout = GNUNET_TIME_relative_to_absolute
1768 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1769 ret->ready_list = head;
1770 ret->next = head->addresses;
1771 head->addresses = ret;
1777 * Closure for 'add_validated_address'.
1779 struct AddValidatedAddressContext
1782 * Entry that has been validated.
1784 const struct ValidationEntry *ve;
1787 * Flag set after we have added the address so
1788 * that we terminate the iteration next time.
1795 * Callback function used to fill a buffer of max bytes with a list of
1796 * addresses in the format used by HELLOs. Should use
1797 * "GNUNET_HELLO_add_address" as a helper function.
1799 * @param cls the 'struct AddValidatedAddressContext' with the validated address
1800 * @param max maximum number of bytes that can be written to buf
1801 * @param buf where to write the address information
1802 * @return number of bytes written, 0 to signal the
1803 * end of the iteration.
1806 add_validated_address (void *cls,
1807 size_t max, void *buf)
1809 struct AddValidatedAddressContext *avac = cls;
1810 const struct ValidationEntry *ve = avac->ve;
1812 if (GNUNET_YES == avac->done)
1814 avac->done = GNUNET_YES;
1815 return GNUNET_HELLO_add_address (ve->transport_name,
1816 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
1826 * Closure for 'check_address_exists'.
1828 struct CheckAddressExistsClosure
1831 * Address to check for.
1836 * Name of the transport.
1846 * Set to GNUNET_YES if the address exists.
1853 * Iterator over hash map entries. Checks if the given
1854 * validation entry is for the same address as what is given
1857 * @param cls the 'struct CheckAddressExistsClosure*'
1858 * @param key current key code (ignored)
1859 * @param value value in the hash map ('struct ValidationEntry')
1860 * @return GNUNET_YES if we should continue to
1861 * iterate (mismatch), GNUNET_NO if not (entry matched)
1864 check_address_exists (void *cls,
1865 const GNUNET_HashCode * key,
1868 struct CheckAddressExistsClosure *caec = cls;
1869 struct ValidationEntry *ve = value;
1870 if ( (0 == strcmp (caec->tname,
1871 ve->transport_name)) &&
1872 (caec->addrlen == ve->addrlen) &&
1873 (0 == memcmp (caec->addr,
1877 caec->exists = GNUNET_YES;
1885 * HELLO validation cleanup task (validation failed).
1887 * @param cls the 'struct ValidationEntry' that failed
1888 * @param tc scheduler context (unused)
1891 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1893 struct ValidationEntry *va = cls;
1894 struct GNUNET_PeerIdentity pid;
1896 GNUNET_STATISTICS_update (stats,
1897 gettext_noop ("# address validation timeouts"),
1900 GNUNET_CRYPTO_hash (&va->publicKey,
1902 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1904 GNUNET_CONTAINER_multihashmap_remove (validation_map,
1907 GNUNET_free (va->transport_name);
1913 neighbour_timeout_task (void *cls,
1914 const struct GNUNET_SCHEDULER_TaskContext *tc)
1916 struct NeighbourList *n = cls;
1919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1920 "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
1922 GNUNET_STATISTICS_update (stats,
1923 gettext_noop ("# disconnects due to timeout"),
1926 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1927 disconnect_neighbour (n, GNUNET_NO);
1932 * Create a fresh entry in our neighbour list for the given peer.
1933 * Will try to transmit our current HELLO to the new neighbour. Also
1934 * notifies our clients about the new "connection".
1936 * @param peer the peer for which we create the entry
1937 * @return the new neighbour list entry
1939 static struct NeighbourList *
1940 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer)
1942 struct NeighbourList *n;
1943 struct TransportPlugin *tp;
1944 struct ReadyList *rl;
1946 GNUNET_assert (our_hello != NULL);
1947 GNUNET_STATISTICS_update (stats,
1948 gettext_noop ("# active neighbours"),
1951 n = GNUNET_malloc (sizeof (struct NeighbourList));
1952 n->next = neighbours;
1956 GNUNET_TIME_relative_to_absolute
1957 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1958 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1959 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1960 MAX_BANDWIDTH_CARRY_S);
1964 if (tp->api->send != NULL)
1966 rl = GNUNET_malloc (sizeof (struct ReadyList));
1967 rl->next = n->plugins;
1970 rl->addresses = NULL;
1974 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1976 n->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
1977 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1978 &neighbour_timeout_task, n);
1979 transmit_to_peer (NULL, NULL, 0,
1980 HELLO_ADDRESS_EXPIRATION,
1981 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
1988 * Send periodic PING messages to a give foreign address.
1990 * @param cls our 'struct PeriodicValidationContext*'
1991 * @param tc task context
1994 send_periodic_ping (void *cls,
1995 const struct GNUNET_SCHEDULER_TaskContext *tc)
1997 struct PeriodicValidationContext *pvc = cls;
1998 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey = pvc->publicKey;
1999 char *tname = pvc->transport;
2000 const void *addr = pvc->foreign_address->addr;
2001 size_t addrlen = pvc->foreign_address->addrlen;
2002 struct GNUNET_PeerIdentity id;
2003 struct TransportPlugin *tp;
2004 struct ValidationEntry *va;
2005 struct NeighbourList *neighbour;
2006 struct ForeignAddressList *peer_address;
2007 struct TransportPingMessage ping;
2008 struct CheckAddressExistsClosure caec;
2010 uint16_t hello_size;
2014 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2016 /* We have been shutdown, don't do anything! */
2017 GNUNET_free (tname);
2020 tp = find_transport (tname);
2023 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
2024 GNUNET_ERROR_TYPE_BULK,
2026 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
2028 GNUNET_free (tname);
2032 GNUNET_CRYPTO_hash (&publicKey,
2034 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2037 caec.addrlen = addrlen;
2039 caec.exists = GNUNET_NO;
2040 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2041 &check_address_exists,
2043 if (caec.exists == GNUNET_YES)
2045 /* During validation attempts we will likely trigger the other
2046 peer trying to validate our address which in turn will cause
2047 it to send us its HELLO, so we expect to hit this case rather
2048 frequently. Only print something if we are very verbose. */
2049 #if DEBUG_TRANSPORT > 1
2050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2051 "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
2052 GNUNET_a2s (addr, addrlen),
2056 GNUNET_free (tname);
2059 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
2060 va->transport_name = tname;
2061 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2063 va->send_time = GNUNET_TIME_absolute_get();
2064 va->addr = (const void*) &va[1];
2065 memcpy (&va[1], addr, addrlen);
2066 va->addrlen = addrlen;
2067 memcpy(&va->publicKey, &publicKey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2068 va->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
2069 HELLO_VERIFICATION_TIMEOUT,
2070 &timeout_hello_validation,
2072 GNUNET_CONTAINER_multihashmap_put (validation_map,
2075 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2076 neighbour = find_neighbour(&id);
2077 if (neighbour == NULL)
2078 neighbour = setup_new_neighbour(&id);
2079 peer_address = add_peer_address(neighbour, tname, addr, addrlen);
2080 GNUNET_assert(peer_address != NULL);
2081 hello_size = GNUNET_HELLO_size(our_hello);
2082 tsize = sizeof(struct TransportPingMessage) + hello_size;
2083 message_buf = GNUNET_malloc(tsize);
2084 ping.challenge = htonl(va->challenge);
2085 ping.header.size = htons(sizeof(struct TransportPingMessage));
2086 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
2087 memcpy(&ping.target, &id, sizeof(struct GNUNET_PeerIdentity));
2088 memcpy(message_buf, our_hello, hello_size);
2089 memcpy(&message_buf[hello_size],
2091 sizeof(struct TransportPingMessage));
2092 #if DEBUG_TRANSPORT_REVALIDATION
2093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2094 "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
2095 GNUNET_a2s (addr, addrlen),
2098 "HELLO", hello_size,
2099 "PING", sizeof (struct TransportPingMessage));
2101 GNUNET_STATISTICS_update (stats,
2102 gettext_noop ("# PING messages sent for re-validation"),
2105 transmit_to_peer (NULL, peer_address,
2106 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2107 HELLO_VERIFICATION_TIMEOUT,
2109 GNUNET_YES, neighbour);
2110 GNUNET_free(message_buf);
2115 * Iterator over hash map entries. Checks if the given validation
2116 * entry is for the same challenge as what is given in the PONG.
2118 * @param cls the 'struct TransportPongMessage*'
2119 * @param key peer identity
2120 * @param value value in the hash map ('struct ValidationEntry')
2121 * @return GNUNET_YES if we should continue to
2122 * iterate (mismatch), GNUNET_NO if not (entry matched)
2125 check_pending_validation (void *cls,
2126 const GNUNET_HashCode * key,
2129 const struct TransportPongMessage *pong = cls;
2130 struct ValidationEntry *ve = value;
2131 struct AddValidatedAddressContext avac;
2132 unsigned int challenge = ntohl(pong->challenge);
2133 struct GNUNET_HELLO_Message *hello;
2134 struct GNUNET_PeerIdentity target;
2135 struct NeighbourList *n;
2136 struct ForeignAddressList *fal;
2137 struct PeriodicValidationContext *periodic_validation_context;
2139 if (ve->challenge != challenge)
2143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2144 "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
2146 GNUNET_a2s ((const struct sockaddr *) ve->addr,
2148 ve->transport_name);
2150 GNUNET_STATISTICS_update (stats,
2151 gettext_noop ("# address validation successes"),
2154 /* create the updated HELLO */
2155 GNUNET_CRYPTO_hash (&ve->publicKey,
2156 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2157 &target.hashPubKey);
2158 avac.done = GNUNET_NO;
2160 hello = GNUNET_HELLO_create (&ve->publicKey,
2161 &add_validated_address,
2163 GNUNET_PEERINFO_add_peer (cfg, sched,
2166 GNUNET_free (hello);
2167 n = find_neighbour (&target);
2170 fal = add_peer_address (n,
2174 GNUNET_assert (fal != NULL);
2175 fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2176 fal->validated = GNUNET_YES;
2177 fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
2178 periodic_validation_context = GNUNET_malloc(sizeof(struct PeriodicValidationContext));
2179 periodic_validation_context->foreign_address = fal;
2180 periodic_validation_context->transport = strdup(ve->transport_name);
2181 memcpy(&periodic_validation_context->publicKey,
2183 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2184 /* FIXME: this causes all of the revalidation PINGs for the same HELLO
2185 to be transmitted in bulk, which is not nice; also,
2186 triggering these HERE means that revalidations do NOT happen AT ALL
2187 for HELLOs a previous instance of this process validated (since
2188 there is no "initial" validation PING => no revalidation => BUG! */
2189 fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(sched,
2190 TRANSPORT_DEFAULT_REVALIDATION,
2191 &send_periodic_ping,
2192 periodic_validation_context);
2193 if (n->latency.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
2194 n->latency = fal->latency;
2196 n->latency.value = (fal->latency.value + n->latency.value) / 2;
2197 n->distance = fal->distance;
2198 if (GNUNET_NO == n->received_pong)
2200 notify_clients_connect (&target, n->latency, n->distance);
2201 n->received_pong = GNUNET_YES;
2203 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
2205 GNUNET_SCHEDULER_cancel (sched,
2207 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
2208 try_transmission_to_peer (n);
2212 /* clean up validation entry */
2213 GNUNET_assert (GNUNET_YES ==
2214 GNUNET_CONTAINER_multihashmap_remove (validation_map,
2217 GNUNET_SCHEDULER_cancel (sched,
2219 GNUNET_free (ve->transport_name);
2226 * Function that will be called if we receive a validation
2227 * of an address challenge that we transmitted to another
2228 * peer. Note that the validation should only be considered
2229 * acceptable if the challenge matches AND if the sender
2230 * address is at least a plausible address for this peer
2231 * (otherwise we may be seeing a MiM attack).
2233 * @param cls closure
2234 * @param message the pong message
2235 * @param peer who responded to our challenge
2236 * @param sender_address string describing our sender address (as observed
2237 * by the other peer in binary format)
2238 * @param sender_address_len number of bytes in 'sender_address'
2241 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
2242 const struct GNUNET_PeerIdentity *peer,
2243 const char *sender_address,
2244 size_t sender_address_len)
2246 #if DEBUG_TRANSPORT > 1
2247 /* we get tons of these that just get discarded, only log
2248 if we are quite verbose */
2249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2250 "Receiving `%s' message from `%4s'.\n", "PONG",
2253 GNUNET_STATISTICS_update (stats,
2254 gettext_noop ("# PONG messages received"),
2257 if (GNUNET_SYSERR !=
2258 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
2260 &check_pending_validation,
2263 /* This is *expected* to happen a lot since we send
2264 PONGs to *all* known addresses of the sender of
2265 the PING, so most likely we get multiple PONGs
2266 per PING, and all but the first PONG will end up
2267 here. So really we should not print anything here
2268 unless we want to be very, very verbose... */
2269 #if DEBUG_TRANSPORT > 2
2270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2271 "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
2280 /* FIXME: add given address to potential pool of our addresses
2282 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
2283 _("Another peer saw us using the address `%s' via `%s'.\n"),
2284 GNUNET_a2s ((const struct sockaddr *) &pong[1],
2285 ntohs(pong->addrlen)),
2286 va->transport_name);
2292 * Check if the given address is already being validated; if not,
2293 * append the given address to the list of entries that are being be
2294 * validated and initiate validation.
2296 * @param cls closure ('struct CheckHelloValidatedContext *')
2297 * @param tname name of the transport
2298 * @param expiration expiration time
2299 * @param addr the address
2300 * @param addrlen length of the address
2301 * @return GNUNET_OK (always)
2304 run_validation (void *cls,
2306 struct GNUNET_TIME_Absolute expiration,
2307 const void *addr, size_t addrlen)
2309 struct CheckHelloValidatedContext *chvc = cls;
2310 struct GNUNET_PeerIdentity id;
2311 struct TransportPlugin *tp;
2312 struct ValidationEntry *va;
2313 struct NeighbourList *neighbour;
2314 struct ForeignAddressList *peer_address;
2315 struct TransportPingMessage ping;
2316 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
2317 struct CheckAddressExistsClosure caec;
2319 uint16_t hello_size;
2322 GNUNET_STATISTICS_update (stats,
2323 gettext_noop ("# peer addresses scheduled for validation"),
2326 tp = find_transport (tname);
2329 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
2330 GNUNET_ERROR_TYPE_BULK,
2332 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
2334 GNUNET_STATISTICS_update (stats,
2335 gettext_noop ("# peer addresses not validated (no applicable transport plugin available)"),
2340 GNUNET_HELLO_get_key (chvc->hello, &pk);
2341 GNUNET_CRYPTO_hash (&pk,
2343 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2346 caec.addrlen = addrlen;
2348 caec.exists = GNUNET_NO;
2349 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2350 &check_address_exists,
2352 if (caec.exists == GNUNET_YES)
2354 /* During validation attempts we will likely trigger the other
2355 peer trying to validate our address which in turn will cause
2356 it to send us its HELLO, so we expect to hit this case rather
2357 frequently. Only print something if we are very verbose. */
2358 #if DEBUG_TRANSPORT > 1
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2360 "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
2361 GNUNET_a2s (addr, addrlen),
2365 GNUNET_STATISTICS_update (stats,
2366 gettext_noop ("# peer addresses not validated (in progress)"),
2371 va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
2372 va->transport_name = GNUNET_strdup (tname);
2373 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2375 va->send_time = GNUNET_TIME_absolute_get();
2376 va->addr = (const void*) &va[1];
2377 memcpy (&va[1], addr, addrlen);
2378 va->addrlen = addrlen;
2379 GNUNET_HELLO_get_key (chvc->hello,
2381 va->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
2382 HELLO_VERIFICATION_TIMEOUT,
2383 &timeout_hello_validation,
2385 GNUNET_CONTAINER_multihashmap_put (validation_map,
2388 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2389 neighbour = find_neighbour(&id);
2390 if (neighbour == NULL)
2391 neighbour = setup_new_neighbour(&id);
2392 peer_address = add_peer_address(neighbour, tname, addr, addrlen);
2393 GNUNET_assert(peer_address != NULL);
2394 hello_size = GNUNET_HELLO_size(our_hello);
2395 tsize = sizeof(struct TransportPingMessage) + hello_size;
2396 message_buf = GNUNET_malloc(tsize);
2397 ping.challenge = htonl(va->challenge);
2398 ping.header.size = htons(sizeof(struct TransportPingMessage));
2399 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
2400 memcpy(&ping.target, &id, sizeof(struct GNUNET_PeerIdentity));
2401 memcpy(message_buf, our_hello, hello_size);
2402 memcpy(&message_buf[hello_size],
2404 sizeof(struct TransportPingMessage));
2406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2407 "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
2408 GNUNET_a2s (addr, addrlen),
2411 "HELLO", hello_size,
2412 "PING", sizeof (struct TransportPingMessage));
2414 GNUNET_STATISTICS_update (stats,
2415 gettext_noop ("# PING messages sent for initial validation"),
2418 transmit_to_peer (NULL, peer_address,
2419 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2420 HELLO_VERIFICATION_TIMEOUT,
2422 GNUNET_YES, neighbour);
2423 GNUNET_free(message_buf);
2429 * Add the given address to the list of foreign addresses
2430 * available for the given peer (check for duplicates).
2432 * @param cls the respective 'struct NeighbourList' to update
2433 * @param tname name of the transport
2434 * @param expiration expiration time
2435 * @param addr the address
2436 * @param addrlen length of the address
2437 * @return GNUNET_OK (always)
2440 add_to_foreign_address_list (void *cls,
2442 struct GNUNET_TIME_Absolute expiration,
2443 const void *addr, size_t addrlen)
2445 struct NeighbourList *n = cls;
2446 struct ForeignAddressList *fal;
2450 fal = find_peer_address (n, tname, addr, addrlen);
2454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2455 "Adding address `%s' (%s) for peer `%4s' due to peerinfo data for %llums.\n",
2456 GNUNET_a2s (addr, addrlen),
2458 GNUNET_i2s (&n->id),
2461 fal = add_peer_address (n, tname, addr, addrlen);
2466 fal->expires = GNUNET_TIME_absolute_max (expiration,
2468 fal->validated = GNUNET_YES;
2469 if (try == GNUNET_YES)
2470 try_transmission_to_peer (n);
2476 * Check if addresses in validated hello "h" overlap with
2477 * those in "chvc->hello" and validate the rest.
2479 * @param cls closure
2480 * @param peer id of the peer, NULL for last call
2481 * @param h hello message for the peer (can be NULL)
2482 * @param trust amount of trust we have in the peer (not used)
2485 check_hello_validated (void *cls,
2486 const struct GNUNET_PeerIdentity *peer,
2487 const struct GNUNET_HELLO_Message *h,
2490 struct CheckHelloValidatedContext *chvc = cls;
2491 struct GNUNET_HELLO_Message *plain_hello;
2492 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
2493 struct GNUNET_PeerIdentity target;
2494 struct NeighbourList *n;
2499 GNUNET_CONTAINER_DLL_remove (chvc_head,
2502 if (GNUNET_NO == chvc->hello_known)
2504 /* notify PEERINFO about the peer now, so that we at least
2505 have the public key if some other component needs it */
2506 GNUNET_HELLO_get_key (chvc->hello, &pk);
2507 GNUNET_CRYPTO_hash (&pk,
2508 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2509 &target.hashPubKey);
2510 plain_hello = GNUNET_HELLO_create (&pk,
2513 GNUNET_PEERINFO_add_peer (cfg, sched, &target, plain_hello);
2514 GNUNET_free (plain_hello);
2516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2517 "Peerinfo had no `%s' message for peer `%4s', full validation needed.\n",
2519 GNUNET_i2s (&target));
2521 GNUNET_STATISTICS_update (stats,
2522 gettext_noop ("# new HELLOs requiring full validation"),
2525 GNUNET_HELLO_iterate_addresses (chvc->hello,
2532 GNUNET_STATISTICS_update (stats,
2533 gettext_noop ("# duplicate HELLO (peer known)"),
2543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2544 "Peerinfo had `%s' message for peer `%4s', validating only new addresses.\n",
2548 chvc->hello_known = GNUNET_YES;
2549 n = find_neighbour (peer);
2552 GNUNET_HELLO_iterate_addresses (h,
2554 &add_to_foreign_address_list,
2556 try_transmission_to_peer (n);
2558 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
2560 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
2566 * Process HELLO-message.
2568 * @param plugin transport involved, may be NULL
2569 * @param message the actual message
2570 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
2573 process_hello (struct TransportPlugin *plugin,
2574 const struct GNUNET_MessageHeader *message)
2577 struct GNUNET_PeerIdentity target;
2578 const struct GNUNET_HELLO_Message *hello;
2579 struct CheckHelloValidatedContext *chvc;
2580 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
2582 hsize = ntohs (message->size);
2583 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
2584 (hsize < sizeof (struct GNUNET_MessageHeader)))
2587 return GNUNET_SYSERR;
2589 /* first, check if load is too high */
2590 if (GNUNET_SCHEDULER_get_load (sched,
2591 GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
2593 GNUNET_STATISTICS_update (stats,
2594 gettext_noop ("# HELLOs ignored due to high load"),
2599 hello = (const struct GNUNET_HELLO_Message *) message;
2600 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
2602 GNUNET_break_op (0);
2603 return GNUNET_SYSERR;
2605 GNUNET_CRYPTO_hash (&publicKey,
2606 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2607 &target.hashPubKey);
2608 if (0 == memcmp (&my_identity,
2610 sizeof (struct GNUNET_PeerIdentity)))
2612 GNUNET_STATISTICS_update (stats,
2613 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
2618 #if DEBUG_TRANSPORT > 1
2619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2620 "Processing `%s' message for `%4s' of size %u\n",
2622 GNUNET_i2s (&target),
2623 GNUNET_HELLO_size(hello));
2625 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
2626 chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
2627 memcpy (&chvc[1], hello, hsize);
2628 GNUNET_CONTAINER_DLL_insert (chvc_head,
2631 /* finally, check if HELLO was previously validated
2632 (continuation will then schedule actual validation) */
2633 chvc->piter = GNUNET_PEERINFO_iterate (cfg,
2637 HELLO_VERIFICATION_TIMEOUT,
2638 &check_hello_validated, chvc);
2644 * The peer specified by the given neighbour has timed-out or a plugin
2645 * has disconnected. We may either need to do nothing (other plugins
2646 * still up), or trigger a full disconnect and clean up. This
2647 * function updates our state and does the necessary notifications.
2648 * Also notifies our clients that the neighbour is now officially
2651 * @param n the neighbour list entry for the peer
2652 * @param check should we just check if all plugins
2653 * disconnected or must we ask all plugins to
2657 disconnect_neighbour (struct NeighbourList *n, int check)
2659 struct ReadyList *rpos;
2660 struct NeighbourList *npos;
2661 struct NeighbourList *nprev;
2662 struct MessageQueue *mq;
2663 struct ForeignAddressList *peer_addresses;
2664 struct ForeignAddressList *peer_pos;
2666 if (GNUNET_YES == check)
2669 while (NULL != rpos)
2671 peer_addresses = rpos->addresses;
2672 while (peer_addresses != NULL)
2674 if (GNUNET_YES == peer_addresses->connected)
2675 return; /* still connected */
2676 peer_addresses = peer_addresses->next;
2682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2683 "Disconnecting from `%4s'\n",
2684 GNUNET_i2s (&n->id));
2686 /* remove n from neighbours list */
2689 while ((npos != NULL) && (npos != n))
2694 GNUNET_assert (npos != NULL);
2696 neighbours = n->next;
2698 nprev->next = n->next;
2700 /* notify all clients about disconnect */
2701 if (GNUNET_YES == n->received_pong)
2702 notify_clients_disconnect (&n->id);
2704 /* clean up all plugins, cancel connections and pending transmissions */
2705 while (NULL != (rpos = n->plugins))
2707 n->plugins = rpos->next;
2708 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
2709 while (rpos->addresses != NULL)
2711 peer_pos = rpos->addresses;
2712 rpos->addresses = peer_pos->next;
2713 if (peer_pos->connected == GNUNET_YES)
2714 GNUNET_STATISTICS_update (stats,
2715 gettext_noop ("# connected addresses"),
2718 GNUNET_free(peer_pos);
2723 /* free all messages on the queue */
2724 while (NULL != (mq = n->messages_head))
2726 GNUNET_STATISTICS_update (stats,
2727 gettext_noop ("# bytes in message queue for other peers"),
2728 -mq->message_buf_size,
2730 GNUNET_STATISTICS_update (stats,
2731 gettext_noop ("# bytes discarded due to disconnect"),
2732 mq->message_buf_size,
2734 GNUNET_CONTAINER_DLL_remove (n->messages_head,
2737 GNUNET_assert (0 == memcmp(&mq->neighbour_id,
2739 sizeof(struct GNUNET_PeerIdentity)));
2742 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2744 GNUNET_SCHEDULER_cancel (sched, n->timeout_task);
2745 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2747 if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
2749 GNUNET_SCHEDULER_cancel (sched, n->retry_task);
2750 n->retry_task = GNUNET_SCHEDULER_NO_TASK;
2752 /* finally, free n itself */
2753 GNUNET_STATISTICS_update (stats,
2754 gettext_noop ("# active neighbours"),
2762 * We have received a PING message from someone. Need to send a PONG message
2763 * in response to the peer by any means necessary.
2765 * FIXME: With something like TCP where a connection exists, we may
2766 * want to send it that way. But the current API does not seem to
2767 * allow us to do so (can't tell this to the transport!)
2770 handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
2771 const struct GNUNET_PeerIdentity *peer,
2772 const char *sender_address,
2773 size_t sender_address_len)
2775 struct TransportPlugin *plugin = cls;
2776 struct TransportPingMessage *ping;
2777 struct TransportPongMessage *pong;
2778 struct NeighbourList *n;
2779 struct ReadyList *rl;
2780 struct ForeignAddressList *fal;
2782 if (ntohs (message->size) != sizeof (struct TransportPingMessage))
2784 GNUNET_break_op (0);
2785 return GNUNET_SYSERR;
2787 ping = (struct TransportPingMessage *) message;
2788 if (0 != memcmp (&ping->target,
2789 plugin->env.my_identity,
2790 sizeof (struct GNUNET_PeerIdentity)))
2792 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2793 _("Received `%s' message not destined for me!\n"),
2795 return GNUNET_SYSERR;
2798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2799 "Processing `%s' from `%s'\n",
2801 GNUNET_a2s ((const struct sockaddr *)sender_address,
2802 sender_address_len));
2804 GNUNET_STATISTICS_update (stats,
2805 gettext_noop ("# PING messages received"),
2808 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len);
2809 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len);
2810 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
2811 pong->purpose.size =
2812 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2814 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + sender_address_len);
2815 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING);
2816 pong->challenge = ping->challenge;
2817 pong->addrlen = htons(sender_address_len);
2818 memcpy(&pong->signer,
2820 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2821 memcpy (&pong[1], sender_address, sender_address_len);
2822 GNUNET_assert (GNUNET_OK ==
2823 GNUNET_CRYPTO_rsa_sign (my_private_key,
2824 &pong->purpose, &pong->signature));
2826 n = find_neighbour(peer);
2828 n = setup_new_neighbour(peer);
2829 /* broadcast 'PONG' to all available addresses */
2833 fal = rl->addresses;
2836 transmit_to_peer(NULL, fal,
2837 TRANSPORT_PONG_PRIORITY,
2838 HELLO_VERIFICATION_TIMEOUT,
2840 ntohs(pong->header.size),
2853 * Function called by the plugin for each received message.
2854 * Update data volumes, possibly notify plugins about
2855 * reducing the rate at which they read from the socket
2856 * and generally forward to our receive callback.
2858 * @param cls the "struct TransportPlugin *" we gave to the plugin
2859 * @param peer (claimed) identity of the other peer
2860 * @param message the message, NULL if we only care about
2861 * learning about the delay until we should receive again
2862 * @param distance in overlay hops; use 1 unless DV (or 0 if message == NULL)
2863 * @param sender_address binary address of the sender (if observed)
2864 * @param sender_address_len number of bytes in sender_address
2865 * @return how long the plugin should wait until receiving more data
2866 * (plugins that do not support this, can ignore the return value)
2868 static struct GNUNET_TIME_Relative
2869 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
2870 const struct GNUNET_MessageHeader *message,
2871 unsigned int distance, const char *sender_address,
2872 size_t sender_address_len)
2874 struct ReadyList *service_context;
2875 struct TransportPlugin *plugin = cls;
2876 struct TransportClient *cpos;
2877 struct InboundMessage *im;
2878 struct ForeignAddressList *peer_address;
2880 struct NeighbourList *n;
2881 struct GNUNET_TIME_Relative ret;
2883 n = find_neighbour (peer);
2885 n = setup_new_neighbour (peer);
2886 service_context = n->plugins;
2887 while ((service_context != NULL) && (plugin != service_context->plugin))
2888 service_context = service_context->next;
2889 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
2890 if (message != NULL)
2892 peer_address = add_peer_address(n,
2895 sender_address_len);
2896 if (peer_address != NULL)
2898 peer_address->distance = distance;
2899 if (peer_address->connected == GNUNET_NO)
2901 peer_address->connected = GNUNET_YES;
2902 peer_address->connect_attempts++;
2903 GNUNET_STATISTICS_update (stats,
2904 gettext_noop ("# connected addresses"),
2908 peer_address->timeout
2910 GNUNET_TIME_relative_to_absolute
2911 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2913 /* update traffic received amount ... */
2914 msize = ntohs (message->size);
2915 GNUNET_STATISTICS_update (stats,
2916 gettext_noop ("# bytes received from other peers"),
2919 n->distance = distance;
2921 GNUNET_TIME_relative_to_absolute
2922 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2923 GNUNET_SCHEDULER_cancel (sched,
2926 GNUNET_SCHEDULER_add_delayed (sched,
2927 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2928 &neighbour_timeout_task, n);
2929 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2931 /* dropping message due to frequent inbound volume violations! */
2932 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
2933 GNUNET_ERROR_TYPE_BULK,
2935 ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
2936 n->in_tracker.available_bytes_per_s__,
2937 n->quota_violation_count);
2938 GNUNET_STATISTICS_update (stats,
2939 gettext_noop ("# bandwidth quota violations by other peers"),
2942 return GNUNET_TIME_UNIT_MINUTES; /* minimum penalty, likely ignored (UDP...) */
2944 switch (ntohs (message->type))
2946 case GNUNET_MESSAGE_TYPE_HELLO:
2947 GNUNET_STATISTICS_update (stats,
2948 gettext_noop ("# HELLO messages received from other peers"),
2951 process_hello (plugin, message);
2953 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
2954 handle_ping(plugin, message, peer, sender_address, sender_address_len);
2956 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
2957 handle_pong(plugin, message, peer, sender_address, sender_address_len);
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2962 "Received message of type %u from `%4s', sending to all clients.\n",
2963 ntohs (message->type), GNUNET_i2s (peer));
2965 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
2967 n->quota_violation_count++;
2969 n->quota_violation_count = 0; /* back within limits */
2970 GNUNET_STATISTICS_update (stats,
2971 gettext_noop ("# payload received from other peers"),
2974 /* transmit message to all clients */
2975 im = GNUNET_malloc (sizeof (struct InboundMessage) + msize);
2976 im->header.size = htons (sizeof (struct InboundMessage) + msize);
2977 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
2978 im->latency = GNUNET_TIME_relative_hton (n->latency);
2980 memcpy (&im[1], message, msize);
2982 while (cpos != NULL)
2984 transmit_to_client (cpos, &im->header, GNUNET_YES);
2990 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
2993 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2994 "Throttling read (%llu bytes excess at %u b/s), waiting %llums before reading more.\n",
2995 (unsigned long long) n->in_tracker.consumption_since_last_update__,
2996 (unsigned int) n->in_tracker.available_bytes_per_s__,
2997 (unsigned long long) ret.value);
2998 GNUNET_STATISTICS_update (stats,
2999 gettext_noop ("# ms throttling suggested"),
3000 (int64_t) ret.value,
3008 * Handle START-message. This is the first message sent to us
3009 * by any client which causes us to add it to our list.
3011 * @param cls closure (always NULL)
3012 * @param client identification of the client
3013 * @param message the actual message
3016 handle_start (void *cls,
3017 struct GNUNET_SERVER_Client *client,
3018 const struct GNUNET_MessageHeader *message)
3020 struct TransportClient *c;
3021 struct ConnectInfoMessage cim;
3022 struct NeighbourList *n;
3025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3026 "Received `%s' request from client\n", "START");
3031 if (c->client == client)
3033 /* client already on our list! */
3035 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3040 c = GNUNET_malloc (sizeof (struct TransportClient));
3044 if (our_hello != NULL)
3047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3048 "Sending our own `%s' to new client\n", "HELLO");
3050 transmit_to_client (c,
3051 (const struct GNUNET_MessageHeader *) our_hello,
3053 /* tell new client about all existing connections */
3054 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
3055 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
3059 if (GNUNET_YES == n->received_pong)
3062 cim.latency = GNUNET_TIME_relative_hton (n->latency);
3063 cim.distance = htonl (n->distance);
3064 transmit_to_client (c, &cim.header, GNUNET_NO);
3069 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3074 * Handle HELLO-message.
3076 * @param cls closure (always NULL)
3077 * @param client identification of the client
3078 * @param message the actual message
3081 handle_hello (void *cls,
3082 struct GNUNET_SERVER_Client *client,
3083 const struct GNUNET_MessageHeader *message)
3087 GNUNET_STATISTICS_update (stats,
3088 gettext_noop ("# HELLOs received from clients"),
3091 ret = process_hello (NULL, message);
3092 GNUNET_SERVER_receive_done (client, ret);
3097 * Handle SEND-message.
3099 * @param cls closure (always NULL)
3100 * @param client identification of the client
3101 * @param message the actual message
3104 handle_send (void *cls,
3105 struct GNUNET_SERVER_Client *client,
3106 const struct GNUNET_MessageHeader *message)
3108 struct TransportClient *tc;
3109 struct NeighbourList *n;
3110 const struct OutboundMessage *obm;
3111 const struct GNUNET_MessageHeader *obmm;
3115 size = ntohs (message->size);
3117 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
3120 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3123 GNUNET_STATISTICS_update (stats,
3124 gettext_noop ("# payload received for other peers"),
3127 obm = (const struct OutboundMessage *) message;
3129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3130 "Received `%s' request from client with target `%4s'\n",
3131 "SEND", GNUNET_i2s (&obm->peer));
3133 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
3134 msize = ntohs (obmm->size);
3135 if (size != msize + sizeof (struct OutboundMessage))
3138 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3141 n = find_neighbour (&obm->peer);
3143 n = setup_new_neighbour (&obm->peer);
3145 while ((tc != NULL) && (tc->client != client))
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3150 "Client asked to transmit %u-byte message of type %u to `%4s'\n",
3152 ntohs (obmm->type), GNUNET_i2s (&obm->peer));
3154 transmit_to_peer (tc, NULL, ntohl (obm->priority),
3155 GNUNET_TIME_relative_ntoh (obm->timeout),
3157 ntohs (obmm->size), GNUNET_NO, n);
3158 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3163 * Handle SET_QUOTA-message.
3165 * @param cls closure (always NULL)
3166 * @param client identification of the client
3167 * @param message the actual message
3170 handle_set_quota (void *cls,
3171 struct GNUNET_SERVER_Client *client,
3172 const struct GNUNET_MessageHeader *message)
3174 const struct QuotaSetMessage *qsm =
3175 (const struct QuotaSetMessage *) message;
3176 struct NeighbourList *n;
3178 GNUNET_STATISTICS_update (stats,
3179 gettext_noop ("# SET QUOTA messages received"),
3182 n = find_neighbour (&qsm->peer);
3185 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3186 GNUNET_STATISTICS_update (stats,
3187 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
3193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3194 "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
3196 (unsigned int) ntohl (qsm->quota.value__),
3197 (unsigned int) n->in_tracker.available_bytes_per_s__,
3198 GNUNET_i2s (&qsm->peer));
3200 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
3202 if (0 == ntohl (qsm->quota.value__))
3203 disconnect_neighbour (n, GNUNET_NO);
3204 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3209 transmit_address_to_client (void *cls, const char *address)
3211 struct GNUNET_SERVER_TransmitContext *tc = cls;
3214 if (NULL == address)
3217 slen = strlen (address) + 1;
3218 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
3219 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
3220 if (NULL == address)
3221 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
3226 * Handle AddressLookup-message.
3228 * @param cls closure (always NULL)
3229 * @param client identification of the client
3230 * @param message the actual message
3233 handle_address_lookup (void *cls,
3234 struct GNUNET_SERVER_Client *client,
3235 const struct GNUNET_MessageHeader *message)
3237 const struct AddressLookupMessage *alum;
3238 struct TransportPlugin *lsPlugin;
3239 const char *nameTransport;
3240 const char *address;
3242 struct GNUNET_SERVER_TransmitContext *tc;
3244 size = ntohs (message->size);
3245 if (size < sizeof (struct AddressLookupMessage))
3247 GNUNET_break_op (0);
3248 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3251 alum = (const struct AddressLookupMessage *) message;
3252 uint32_t addressLen = ntohl (alum->addrlen);
3253 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
3255 GNUNET_break_op (0);
3256 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3259 address = (const char *) &alum[1];
3260 nameTransport = (const char *) &address[addressLen];
3262 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
3264 GNUNET_break_op (0);
3265 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3268 struct GNUNET_TIME_Absolute timeout =
3269 GNUNET_TIME_absolute_ntoh (alum->timeout);
3270 struct GNUNET_TIME_Relative rtimeout =
3271 GNUNET_TIME_absolute_get_remaining (timeout);
3272 lsPlugin = find_transport (nameTransport);
3273 if (NULL == lsPlugin)
3275 tc = GNUNET_SERVER_transmit_context_create (client);
3276 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
3277 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
3278 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
3281 tc = GNUNET_SERVER_transmit_context_create (client);
3282 lsPlugin->api->address_pretty_printer (cls, nameTransport,
3283 address, addressLen, GNUNET_YES,
3285 &transmit_address_to_client, tc);
3289 * List of handlers for the messages understood by this
3292 static struct GNUNET_SERVER_MessageHandler handlers[] = {
3293 {&handle_start, NULL,
3294 GNUNET_MESSAGE_TYPE_TRANSPORT_START, 0},
3295 {&handle_hello, NULL,
3296 GNUNET_MESSAGE_TYPE_HELLO, 0},
3297 {&handle_send, NULL,
3298 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
3299 {&handle_set_quota, NULL,
3300 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
3301 {&handle_address_lookup, NULL,
3302 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
3309 * Setup the environment for this plugin.
3312 create_environment (struct TransportPlugin *plug)
3314 plug->env.cfg = cfg;
3315 plug->env.sched = sched;
3316 plug->env.my_identity = &my_identity;
3317 plug->env.cls = plug;
3318 plug->env.receive = &plugin_env_receive;
3319 plug->env.notify_address = &plugin_env_notify_address;
3320 plug->env.max_connections = max_connect_per_transport;
3321 plug->env.stats = stats;
3326 * Start the specified transport (load the plugin).
3329 start_transport (struct GNUNET_SERVER_Handle *server, const char *name)
3331 struct TransportPlugin *plug;
3334 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3335 _("Loading `%s' transport plugin\n"), name);
3336 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
3337 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
3338 create_environment (plug);
3339 plug->short_name = GNUNET_strdup (name);
3340 plug->lib_name = libname;
3341 plug->next = plugins;
3343 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
3344 if (plug->api == NULL)
3346 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3347 _("Failed to load transport plugin for `%s'\n"), name);
3348 GNUNET_free (plug->short_name);
3349 plugins = plug->next;
3350 GNUNET_free (libname);
3357 * Called whenever a client is disconnected. Frees our
3358 * resources associated with that client.
3360 * @param cls closure
3361 * @param client identification of the client
3364 client_disconnect_notification (void *cls,
3365 struct GNUNET_SERVER_Client *client)
3367 struct TransportClient *pos;
3368 struct TransportClient *prev;
3369 struct ClientMessageQueueEntry *mqe;
3374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
3375 "Client disconnected, cleaning up.\n");
3379 while ((pos != NULL) && (pos->client != client))
3386 while (NULL != (mqe = pos->message_queue_head))
3388 GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
3389 pos->message_queue_tail,
3391 pos->message_count--;
3395 clients = pos->next;
3397 prev->next = pos->next;
3398 if (GNUNET_YES == pos->tcs_pending)
3403 if (pos->th != NULL)
3405 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
3408 GNUNET_break (0 == pos->message_count);
3414 * Iterator to free entries in the validation_map.
3416 * @param cls closure (unused)
3417 * @param key current key code
3418 * @param value value in the hash map (validation to abort)
3419 * @return GNUNET_YES (always)
3422 abort_validation (void *cls,
3423 const GNUNET_HashCode * key,
3426 struct ValidationEntry *va = value;
3428 GNUNET_SCHEDULER_cancel (sched, va->timeout_task);
3429 GNUNET_free (va->transport_name);
3436 * Function called when the service shuts down. Unloads our plugins
3437 * and cancels pending validations.
3439 * @param cls closure, unused
3440 * @param tc task context (unused)
3443 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3445 struct TransportPlugin *plug;
3446 struct OwnAddressList *al;
3447 struct CheckHelloValidatedContext *chvc;
3449 while (neighbours != NULL)
3450 disconnect_neighbour (neighbours, GNUNET_NO);
3452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3453 "Transport service is unloading plugins...\n");
3455 while (NULL != (plug = plugins))
3457 plugins = plug->next;
3458 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
3459 GNUNET_free (plug->lib_name);
3460 GNUNET_free (plug->short_name);
3461 while (NULL != (al = plug->addresses))
3463 plug->addresses = al->next;
3468 if (my_private_key != NULL)
3469 GNUNET_CRYPTO_rsa_key_free (my_private_key);
3470 GNUNET_free_non_null (our_hello);
3472 /* free 'chvc' data structure */
3473 while (NULL != (chvc = chvc_head))
3475 chvc_head = chvc->next;
3476 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
3481 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3484 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
3485 validation_map = NULL;
3488 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
3495 * Initiate transport service.
3497 * @param cls closure
3498 * @param s scheduler to use
3499 * @param serv the initialized server
3500 * @param c configuration to use
3504 struct GNUNET_SCHEDULER_Handle *s,
3505 struct GNUNET_SERVER_Handle *serv,
3506 const struct GNUNET_CONFIGURATION_Handle *c)
3511 unsigned long long tneigh;
3516 stats = GNUNET_STATISTICS_create (sched, "transport", cfg);
3517 validation_map = GNUNET_CONTAINER_multihashmap_create (64);
3518 /* parse configuration */
3520 GNUNET_CONFIGURATION_get_value_number (c,
3525 GNUNET_CONFIGURATION_get_value_filename (c,
3527 "HOSTKEY", &keyfile)))
3529 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3531 ("Transport service is lacking key configuration settings. Exiting.\n"));
3532 GNUNET_SCHEDULER_shutdown (s);
3535 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
3538 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
3539 validation_map = NULL;
3542 max_connect_per_transport = (uint32_t) tneigh;
3543 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3544 GNUNET_free (keyfile);
3545 if (my_private_key == NULL)
3547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3549 ("Transport service could not access hostkey. Exiting.\n"));
3550 GNUNET_SCHEDULER_shutdown (s);
3553 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
3556 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
3557 validation_map = NULL;
3560 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3561 GNUNET_CRYPTO_hash (&my_public_key,
3562 sizeof (my_public_key), &my_identity.hashPubKey);
3563 /* setup notification */
3565 GNUNET_SERVER_disconnect_notify (server,
3566 &client_disconnect_notification, NULL);
3567 /* load plugins... */
3570 GNUNET_CONFIGURATION_get_value_string (c,
3571 "TRANSPORT", "PLUGINS", &plugs))
3573 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3574 _("Starting transport plugins `%s'\n"), plugs);
3575 pos = strtok (plugs, " ");
3578 start_transport (server, pos);
3580 pos = strtok (NULL, " ");
3582 GNUNET_free (plugs);
3584 GNUNET_SCHEDULER_add_delayed (sched,
3585 GNUNET_TIME_UNIT_FOREVER_REL,
3586 &shutdown_task, NULL);
3591 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport service ready.\n"));
3593 /* process client requests */
3594 GNUNET_SERVER_add_handlers (server, handlers);
3599 * The main function for the transport service.
3601 * @param argc number of arguments from the command line
3602 * @param argv command line arguments
3603 * @return 0 ok, 1 on error
3606 main (int argc, char *const *argv)
3608 return (GNUNET_OK ==
3609 GNUNET_SERVICE_run (argc,
3612 GNUNET_SERVICE_OPTION_NONE,
3613 &run, NULL)) ? 0 : 1;
3616 /* end of gnunet-service-transport.c */