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 * - if we do not receive an ACK in response to our
28 * HELLO, retransmit HELLO!
31 #include "gnunet_client_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_hello_lib.h"
34 #include "gnunet_os_lib.h"
35 #include "gnunet_peerinfo_service.h"
36 #include "gnunet_plugin_lib.h"
37 #include "gnunet_protocols.h"
38 #include "gnunet_service_lib.h"
39 #include "gnunet_signatures.h"
40 #include "plugin_transport.h"
41 #include "transport.h"
44 * How many messages can we have pending for a given client process
45 * before we start to drop incoming messages? We typically should
46 * have only one client and so this would be the primary buffer for
47 * messages, so the number should be chosen rather generously.
49 * The expectation here is that most of the time the queue is large
50 * enough so that a drop is virtually never required.
52 #define MAX_PENDING 128
55 * How often should we try to reconnect to a peer using a particular
56 * transport plugin before giving up? Note that the plugin may be
57 * added back to the list after PLUGIN_RETRY_FREQUENCY expires.
59 #define MAX_CONNECT_RETRY 3
62 * How often must a peer violate bandwidth quotas before we start
63 * to simply drop its messages?
65 #define QUOTA_VIOLATION_DROP_THRESHOLD 100
68 * How long until a HELLO verification attempt should time out?
70 #define HELLO_VERIFICATION_TIMEOUT GNUNET_TIME_UNIT_MINUTES
73 * How often do we re-add (cheaper) plugins to our list of plugins
74 * to try for a given connected peer?
76 #define PLUGIN_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
79 * After how long do we expire an address in a HELLO
80 * that we just validated? This value is also used
81 * for our own addresses when we create a HELLO.
83 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
86 * After how long do we consider a connection to a peer dead
87 * if we don't receive messages from the peer?
89 #define IDLE_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
93 * Entry in linked list of network addresses.
98 * This is a linked list.
100 struct AddressList *next;
103 * The address, actually a pointer to the end
104 * of this struct. Do not free!
109 * How long until we auto-expire this address (unless it is
110 * re-confirmed by the transport)?
112 struct GNUNET_TIME_Absolute expires;
123 * Entry in linked list of all of our plugins.
125 struct TransportPlugin
129 * This is a linked list.
131 struct TransportPlugin *next;
134 * API of the transport as returned by the plugin's
135 * initialization function.
137 struct GNUNET_TRANSPORT_PluginFunctions *api;
140 * Short name for the plugin (i.e. "tcp").
145 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
150 * List of our known addresses for this transport.
152 struct AddressList *addresses;
155 * Environment this transport service is using
158 struct GNUNET_TRANSPORT_PluginEnvironment env;
161 * ID of task that is used to clean up expired addresses.
163 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
167 * Set to GNUNET_YES if we need to scrap the existing
168 * list of "addresses" and start fresh when we receive
169 * the next address update from a transport. Set to
170 * GNUNET_NO if we should just add the new address
171 * to the list and wait for the commit call.
176 struct NeighbourList;
179 * For each neighbour we keep a list of messages
180 * that we still want to transmit to the neighbour.
186 * This is a linked list.
188 struct MessageQueue *next;
191 * The message we want to transmit.
193 struct GNUNET_MessageHeader *message;
196 * Client responsible for queueing the message;
197 * used to check that a client has not two messages
198 * pending for the same target. Can be NULL.
200 struct TransportClient *client;
203 * Neighbour this entry belongs to.
205 struct NeighbourList *neighbour;
208 * Plugin that we used for the transmission.
209 * NULL until we scheduled a transmission.
211 struct TransportPlugin *plugin;
214 * Internal message of the transport system that should not be
215 * included in the usual SEND-SEND_OK transmission confirmation
216 * traffic management scheme. Typically, "internal_msg" will
217 * be set whenever "client" is NULL (but it is not strictly
226 * For a given Neighbour, which plugins are available
227 * to talk to this peer and what are their costs?
233 * This is a linked list.
235 struct ReadyList *next;
238 * Which of our transport plugins does this entry
241 struct TransportPlugin *plugin;
244 * Neighbour this entry belongs to.
246 struct NeighbourList *neighbour;
249 * Opaque handle (specific to the plugin) for the
250 * connection to our target; can be NULL.
255 * What was the last latency observed for this plugin
256 * and peer? Invalid if connected is GNUNET_NO.
258 struct GNUNET_TIME_Relative latency;
261 * If we did not successfully transmit a message to the
262 * given peer via this connection during the specified
263 * time, we should consider the connection to be dead.
264 * This is used in the case that a TCP transport simply
265 * stalls writing to the stream but does not formerly
266 * get a signal that the other peer died.
268 struct GNUNET_TIME_Absolute timeout;
271 * Is this plugin currently connected? The first time
272 * we transmit or send data to a peer via a particular
273 * plugin, we set this to GNUNET_YES. If we later get
274 * an error (disconnect notification or transmission
275 * failure), we set it back to GNUNET_NO. Each time the
276 * value is set to GNUNET_YES, we increment the
277 * "connect_attempts" counter. If that one reaches a
278 * particular threshold, we consider the plugin to not
279 * be working properly at this time for the given peer
280 * and remove it from the eligible list.
285 * How often have we tried to connect using this plugin?
287 unsigned int connect_attempts;
290 * Is this plugin ready to transmit to the specific
291 * target? GNUNET_NO if not. Initially, all plugins
292 * are marked ready. If a transmission is in progress,
293 * "transmit_ready" is set to GNUNET_NO.
301 * Entry in linked list of all of our current neighbours.
307 * This is a linked list.
309 struct NeighbourList *next;
312 * Which of our transports is connected to this peer
313 * and what is their status?
315 struct ReadyList *plugins;
318 * List of messages we would like to send to this peer;
319 * must contain at most one message per client.
321 struct MessageQueue *messages;
324 * Identity of this neighbour.
326 struct GNUNET_PeerIdentity id;
329 * ID of task scheduled to run when this peer is about to
330 * time out (will free resources associated with the peer).
332 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
335 * How long until we should consider this peer dead
336 * (if we don't receive another message in the
339 struct GNUNET_TIME_Absolute peer_timeout;
342 * At what time did we reset last_received last?
344 struct GNUNET_TIME_Absolute last_quota_update;
347 * At what time should we try to again add plugins to
350 struct GNUNET_TIME_Absolute retry_plugins_time;
353 * How many bytes have we received since the "last_quota_update"
356 uint64_t last_received;
359 * Global quota for outbound traffic for the neighbour in bytes/ms.
364 * What is the latest version of our HELLO that we have
365 * sent to this neighbour?
367 unsigned int hello_version_sent;
370 * How often has the other peer (recently) violated the
371 * inbound traffic limit? Incremented by 10 per violation,
372 * decremented by 1 per non-violation (for each
375 unsigned int quota_violation_count;
378 * Have we seen an ACK from this neighbour in the past?
379 * (used to make up a fake ACK for clients connecting after
380 * the neighbour connected to us).
388 * Linked list of messages to be transmitted to
389 * the client. Each entry is followed by the
392 struct ClientMessageQueueEntry
395 * This is a linked list.
397 struct ClientMessageQueueEntry *next;
402 * Client connected to the transport service.
404 struct TransportClient
408 * This is a linked list.
410 struct TransportClient *next;
413 * Handle to the client.
415 struct GNUNET_SERVER_Client *client;
418 * Linked list of messages yet to be transmitted to
421 struct ClientMessageQueueEntry *message_queue_head;
424 * Tail of linked list of messages yet to be transmitted to the
427 struct ClientMessageQueueEntry *message_queue_tail;
430 * Is a call to "transmit_send_continuation" pending? If so, we
431 * must not free this struct (even if the corresponding client
432 * disconnects) and instead only remove it from the linked list and
433 * set the "client" field to NULL.
438 * Length of the list of messages pending for this client.
440 unsigned int message_count;
446 * Message used to ask a peer to validate receipt (to check an address
447 * from a HELLO). Followed by the address used. Note that the
448 * recipients response does not affirm that he has this address,
449 * only that he got the challenge message.
451 struct ValidationChallengeMessage
455 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
457 struct GNUNET_MessageHeader header;
460 * What are we signing and why?
462 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
465 * Random challenge number (in network byte order).
467 uint32_t challenge GNUNET_PACKED;
470 * Who is the intended recipient?
472 struct GNUNET_PeerIdentity target;
477 * Message used to validate a HELLO. If this was
478 * the right recipient, the response is a signature
479 * of the original validation request. The
480 * challenge is included in the confirmation to make
481 * matching of replies to requests possible.
483 struct ValidationChallengeResponse
487 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
489 struct GNUNET_MessageHeader header;
492 * Random challenge number (in network byte order).
494 uint32_t challenge GNUNET_PACKED;
497 * Who signed this message?
499 struct GNUNET_PeerIdentity sender;
504 struct GNUNET_CRYPTO_RsaSignature signature;
510 * For each HELLO, we may have to validate multiple addresses;
511 * each address gets its own request entry.
513 struct ValidationAddress
516 * This is a linked list.
518 struct ValidationAddress *next;
521 * Our challenge message. Points to after this
522 * struct, so this field should not be freed.
524 struct ValidationChallengeMessage *msg;
527 * Name of the transport.
529 char *transport_name;
532 * When should this validated address expire?
534 struct GNUNET_TIME_Absolute expiration;
537 * Length of the address we are validating.
542 * Set to GNUNET_YES if the challenge was met,
543 * GNUNET_SYSERR if we know it failed, GNUNET_NO
544 * if we are waiting on a response.
551 * Entry in linked list of all HELLOs awaiting validation.
553 struct ValidationList
557 * This is a linked list.
559 struct ValidationList *next;
562 * Linked list with one entry per address from the HELLO
563 * that needs to be validated.
565 struct ValidationAddress *addresses;
568 * The public key of the peer.
570 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
573 * When does this record time-out? (assuming the
574 * challenge goes unanswered)
576 struct GNUNET_TIME_Absolute timeout;
582 * HELLOs awaiting validation.
584 static struct ValidationList *pending_validations;
589 static struct GNUNET_HELLO_Message *our_hello;
592 * "version" of "our_hello". Used to see if a given
593 * neighbour has already been sent the latest version
594 * of our HELLO message.
596 static unsigned int our_hello_version;
601 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
606 static struct GNUNET_PeerIdentity my_identity;
611 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
616 struct GNUNET_SCHEDULER_Handle *sched;
621 struct GNUNET_CONFIGURATION_Handle *cfg;
624 * Linked list of all clients to this service.
626 static struct TransportClient *clients;
629 * All loaded plugins.
631 static struct TransportPlugin *plugins;
636 static struct GNUNET_SERVER_Handle *server;
639 * All known neighbours and their HELLOs.
641 static struct NeighbourList *neighbours;
644 * Default bandwidth quota for receiving for new peers in bytes/ms.
646 static uint32_t default_quota_in;
649 * Default bandwidth quota for sending for new peers in bytes/ms.
651 static uint32_t default_quota_out;
654 * Number of neighbours we'd like to have.
656 static uint32_t max_connect_per_transport;
660 * Find an entry in the neighbour list for a particular peer.
662 * @return NULL if not found.
664 static struct NeighbourList *
665 find_neighbour (const struct GNUNET_PeerIdentity *key)
667 struct NeighbourList *head = neighbours;
668 while ((head != NULL) &&
669 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
676 * Find an entry in the transport list for a particular transport.
678 * @return NULL if not found.
680 static struct TransportPlugin *
681 find_transport (const char *short_name)
683 struct TransportPlugin *head = plugins;
684 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
691 * Update the quota values for the given neighbour now.
694 update_quota (struct NeighbourList *n)
696 struct GNUNET_TIME_Relative delta;
700 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
701 if (delta.value < MIN_QUOTA_REFRESH_TIME)
702 return; /* not enough time passed for doing quota update */
703 allowed = delta.value * n->quota_in;
704 if (n->last_received < allowed)
706 remaining = allowed - n->last_received;
708 remaining /= n->quota_in;
711 if (remaining > MAX_BANDWIDTH_CARRY)
712 remaining = MAX_BANDWIDTH_CARRY;
713 n->last_received = 0;
714 n->last_quota_update = GNUNET_TIME_absolute_get ();
715 n->last_quota_update.value -= remaining;
716 if (n->quota_violation_count > 0)
717 n->quota_violation_count--;
721 n->last_received -= allowed;
722 n->last_quota_update = GNUNET_TIME_absolute_get ();
723 if (n->last_received > allowed)
725 /* more than twice the allowed rate! */
726 n->quota_violation_count += 10;
733 * Function called to notify a client about the socket
734 * being ready to queue more data. "buf" will be
735 * NULL and "size" zero if the socket was closed for
736 * writing in the meantime.
739 * @param size number of bytes available in buf
740 * @param buf where the callee should write the message
741 * @return number of bytes written to buf
744 transmit_to_client_callback (void *cls, size_t size, void *buf)
746 struct TransportClient *client = cls;
747 struct ClientMessageQueueEntry *q;
750 const struct GNUNET_MessageHeader *msg;
751 struct GNUNET_NETWORK_TransmitHandle *th;
756 /* fatal error with client, free message queue! */
757 while (NULL != (q = client->message_queue_head))
759 client->message_queue_head = q->next;
762 client->message_queue_tail = NULL;
763 client->message_count = 0;
768 while (NULL != (q = client->message_queue_head))
770 msg = (const struct GNUNET_MessageHeader *) &q[1];
771 msize = ntohs (msg->size);
772 if (msize + tsize > size)
774 client->message_queue_head = q->next;
776 client->message_queue_tail = NULL;
777 memcpy (&cbuf[tsize], msg, msize);
780 client->message_count--;
782 GNUNET_assert (tsize > 0);
785 th = GNUNET_SERVER_notify_transmit_ready (client->client,
787 GNUNET_TIME_UNIT_FOREVER_REL,
788 &transmit_to_client_callback,
790 GNUNET_assert (th != NULL);
797 * Send the specified message to the specified client. Since multiple
798 * messages may be pending for the same client at a time, this code
799 * makes sure that no message is lost.
801 * @param client client to transmit the message to
802 * @param msg the message to send
803 * @param may_drop can this message be dropped if the
804 * message queue for this client is getting far too large?
807 transmit_to_client (struct TransportClient *client,
808 const struct GNUNET_MessageHeader *msg, int may_drop)
810 struct ClientMessageQueueEntry *q;
812 struct GNUNET_NETWORK_TransmitHandle *th;
814 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
816 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
818 ("Dropping message, have %u messages pending (%u is the soft limit)\n"),
819 client->message_count, MAX_PENDING);
820 /* TODO: call to statistics... */
823 client->message_count++;
824 msize = ntohs (msg->size);
825 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
826 memcpy (&q[1], msg, msize);
827 /* append to message queue */
828 if (client->message_queue_tail == NULL)
830 client->message_queue_tail = q;
834 client->message_queue_tail->next = q;
835 client->message_queue_tail = q;
837 if (client->message_queue_head == NULL)
839 client->message_queue_head = q;
840 th = GNUNET_SERVER_notify_transmit_ready (client->client,
842 GNUNET_TIME_UNIT_FOREVER_REL,
843 &transmit_to_client_callback,
845 GNUNET_assert (th != NULL);
851 * Find alternative plugins for communication.
853 * @param neighbour for which neighbour should we try to find
857 try_alternative_plugins (struct NeighbourList *neighbour)
859 struct ReadyList *rl;
861 if ((neighbour->plugins != NULL) &&
862 (neighbour->retry_plugins_time.value >
863 GNUNET_TIME_absolute_get ().value))
864 return; /* don't try right now */
865 neighbour->retry_plugins_time
866 = GNUNET_TIME_relative_to_absolute (PLUGIN_RETRY_FREQUENCY);
868 rl = neighbour->plugins;
871 if (rl->connect_attempts > 0)
872 rl->connect_attempts--; /* amnesty */
880 * Check the ready list for the given neighbour and
881 * if a plugin is ready for transmission (and if we
882 * have a message), do so!
884 * @param neighbour target peer for which to check the plugins
886 static void try_transmission_to_peer (struct NeighbourList *neighbour);
890 * Function called by the GNUNET_TRANSPORT_TransmitFunction
891 * upon "completion" of a send request. This tells the API
892 * that it is now legal to send another message to the given
895 * @param cls closure, identifies the entry on the
896 * message queue that was transmitted and the
897 * client responsible for queueing the message
898 * @param rl identifies plugin used for the transmission for
899 * this neighbour; needs to be re-enabled for
900 * future transmissions
901 * @param target the peer receiving the message
902 * @param result GNUNET_OK on success, if the transmission
903 * failed, we should not tell the client to transmit
907 transmit_send_continuation (void *cls,
908 struct ReadyList *rl,
909 const struct GNUNET_PeerIdentity *target,
912 struct MessageQueue *mq = cls;
913 struct SendOkMessage send_ok_msg;
914 struct NeighbourList *n;
916 GNUNET_assert (mq != NULL);
919 memcmp (&n->id, target,
920 sizeof (struct GNUNET_PeerIdentity)));
924 while ((rl != NULL) && (rl->plugin != mq->plugin))
926 GNUNET_assert (rl != NULL);
928 if (result == GNUNET_OK)
929 rl->timeout = GNUNET_TIME_relative_to_absolute (IDLE_CONNECTION_TIMEOUT);
931 rl->connected = GNUNET_NO;
932 if (!mq->internal_msg)
933 rl->transmit_ready = GNUNET_YES;
934 if (mq->client != NULL)
936 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
937 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
938 send_ok_msg.success = htonl (result);
939 send_ok_msg.peer = n->id;
940 transmit_to_client (mq->client, &send_ok_msg.header, GNUNET_NO);
942 GNUNET_free (mq->message);
944 /* one plugin just became ready again, try transmitting
945 another message (if available) */
946 try_transmission_to_peer (n);
953 * We could not use an existing (or validated) connection to
954 * talk to a peer. Try addresses that have not yet been
957 * @param n neighbour we want to communicate with
958 * @return plugin ready to talk, or NULL if none is available
960 static struct ReadyList *
961 try_unvalidated_addresses (struct NeighbourList *n)
963 struct ValidationList *vl;
964 struct ValidationAddress *va;
965 struct GNUNET_PeerIdentity id;
966 struct GNUNET_TIME_Absolute now;
969 struct ReadyList *rl;
970 struct TransportPlugin *plugin;
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
974 "Trying to connect to `%4s' using unvalidated addresses\n",
975 GNUNET_i2s (&n->id));
977 /* NOTE: this function needs to not only identify the
978 plugin but also setup "plugin_handle", binding it to the
979 right address using the plugin's "send_to" API */
980 now = GNUNET_TIME_absolute_get ();
981 vl = pending_validations;
984 GNUNET_CRYPTO_hash (&vl->publicKey,
986 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
988 if (0 == memcmp (&id, &n->id, sizeof (struct GNUNET_PeerIdentity)))
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
996 "No unvalidated address found for peer `%4s'\n",
997 GNUNET_i2s (&n->id));
1007 if (va->expiration.value > now.value)
1014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1015 "All %u unvalidated addresses for peer have expired\n",
1020 total = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
1021 for (va = vl->addresses; va != NULL; va = va->next)
1023 if (va->expiration.value <= now.value)
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1032 "Trying unvalidated address of `%s' transport\n",
1033 va->transport_name);
1035 plugin = find_transport (va->transport_name);
1041 rl = GNUNET_malloc (sizeof (struct ReadyList));
1042 rl->next = n->plugins;
1044 rl->plugin = plugin;
1045 rl->plugin_handle = plugin->api->send_to (plugin->api->cls,
1049 GNUNET_TIME_UNIT_ZERO,
1050 &va->msg[1], va->addr_len);
1051 rl->transmit_ready = GNUNET_YES;
1059 * Check the ready list for the given neighbour and
1060 * if a plugin is ready for transmission (and if we
1061 * have a message), do so!
1064 try_transmission_to_peer (struct NeighbourList *neighbour)
1066 struct ReadyList *pos;
1067 struct GNUNET_TIME_Relative min_latency;
1068 struct ReadyList *rl;
1069 struct MessageQueue *mq;
1070 struct GNUNET_TIME_Absolute now;
1072 if (neighbour->messages == NULL)
1073 return; /* nothing to do */
1074 try_alternative_plugins (neighbour);
1075 min_latency = GNUNET_TIME_UNIT_FOREVER_REL;
1077 mq = neighbour->messages;
1078 now = GNUNET_TIME_absolute_get ();
1079 pos = neighbour->plugins;
1082 /* set plugins that are inactive for a long time back to disconnected */
1083 if ((pos->timeout.value < now.value) && (pos->connected == GNUNET_YES))
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1087 "Marking long-time inactive connection to `%4s' as down.\n",
1088 GNUNET_i2s (&neighbour->id));
1090 pos->connected = GNUNET_NO;
1092 if (((GNUNET_YES == pos->transmit_ready) ||
1093 (mq->internal_msg)) &&
1094 (pos->connect_attempts < MAX_CONNECT_RETRY) &&
1095 ((rl == NULL) || (min_latency.value > pos->latency.value)))
1098 min_latency = pos->latency;
1103 rl = try_unvalidated_addresses (neighbour);
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1108 "No plugin ready to transmit message\n");
1110 return; /* nobody ready */
1112 if (GNUNET_NO == rl->connected)
1114 rl->connect_attempts++;
1115 rl->connected = GNUNET_YES;
1117 neighbour->messages = mq->next;
1118 mq->plugin = rl->plugin;
1119 if (!mq->internal_msg)
1120 rl->transmit_ready = GNUNET_NO;
1122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1123 "Giving message of type `%u' for `%4s' to plugin `%s'\n",
1124 ntohs (mq->message->type),
1125 GNUNET_i2s (&neighbour->id), rl->plugin->short_name);
1128 = rl->plugin->api->send (rl->plugin->api->cls,
1133 IDLE_CONNECTION_TIMEOUT,
1134 &transmit_send_continuation, mq);
1139 * Send the specified message to the specified peer.
1141 * @param client source of the transmission request (can be NULL)
1142 * @param msg message to send
1143 * @param is_internal is this an internal message
1144 * @param neighbour handle to the neighbour for transmission
1147 transmit_to_peer (struct TransportClient *client,
1148 const struct GNUNET_MessageHeader *msg,
1149 int is_internal, struct NeighbourList *neighbour)
1151 struct MessageQueue *mq;
1152 struct MessageQueue *mqe;
1153 struct GNUNET_MessageHeader *m;
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1157 _("Sending message of type %u to peer `%4s'\n"),
1158 ntohs (msg->type), GNUNET_i2s (&neighbour->id));
1162 /* check for duplicate submission */
1163 mq = neighbour->messages;
1166 if (mq->client == client)
1168 /* client transmitted to same peer twice
1169 before getting SendOk! */
1176 mq = GNUNET_malloc (sizeof (struct MessageQueue));
1177 mq->client = client;
1178 m = GNUNET_malloc (ntohs (msg->size));
1179 memcpy (m, msg, ntohs (msg->size));
1181 mq->neighbour = neighbour;
1182 mq->internal_msg = is_internal;
1185 mqe = neighbour->messages;
1187 while (mqe->next != NULL)
1192 neighbour->messages = mq;
1193 try_transmission_to_peer (neighbour);
1203 struct GeneratorContext
1205 struct TransportPlugin *plug_pos;
1206 struct AddressList *addr_pos;
1207 struct GNUNET_TIME_Absolute expiration;
1212 address_generator (void *cls, size_t max, void *buf)
1214 struct GeneratorContext *gc = cls;
1217 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
1219 gc->plug_pos = gc->plug_pos->next;
1220 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
1222 if (NULL == gc->plug_pos)
1224 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
1227 gc->addr_pos->addrlen, buf, max);
1228 gc->addr_pos = gc->addr_pos->next;
1234 * Construct our HELLO message from all of the addresses of
1235 * all of the transports.
1240 struct GNUNET_HELLO_Message *hello;
1241 struct TransportClient *cpos;
1242 struct NeighbourList *npos;
1243 struct GeneratorContext gc;
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1247 "Refreshing my HELLO\n");
1249 gc.plug_pos = plugins;
1250 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
1251 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1252 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
1254 while (cpos != NULL)
1256 transmit_to_client (cpos,
1257 (const struct GNUNET_MessageHeader *) hello,
1262 GNUNET_free_non_null (our_hello);
1264 our_hello_version++;
1266 while (npos != NULL)
1268 transmit_to_peer (NULL,
1269 (const struct GNUNET_MessageHeader *) our_hello,
1277 * Task used to clean up expired addresses for a plugin.
1279 * @param cls closure
1283 expire_address_task (void *cls,
1284 const struct GNUNET_SCHEDULER_TaskContext *tc);
1288 * Update the list of addresses for this plugin,
1289 * expiring those that are past their expiration date.
1291 * @param plugin addresses of which plugin should be recomputed?
1292 * @param fresh set to GNUNET_YES if a new address was added
1293 * and we need to regenerate the HELLO even if nobody
1297 update_addresses (struct TransportPlugin *plugin, int fresh)
1299 struct GNUNET_TIME_Relative min_remaining;
1300 struct GNUNET_TIME_Relative remaining;
1301 struct GNUNET_TIME_Absolute now;
1302 struct AddressList *pos;
1303 struct AddressList *prev;
1304 struct AddressList *next;
1307 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1308 GNUNET_SCHEDULER_cancel (plugin->env.sched, plugin->address_update_task);
1309 plugin->address_update_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1310 now = GNUNET_TIME_absolute_get ();
1311 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
1312 expired = GNUNET_NO;
1314 pos = plugin->addresses;
1318 if (pos->expires.value < now.value)
1320 expired = GNUNET_YES;
1322 plugin->addresses = pos->next;
1324 prev->next = pos->next;
1329 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
1330 if (remaining.value < min_remaining.value)
1331 min_remaining = remaining;
1337 if (expired || fresh)
1339 if (min_remaining.value < GNUNET_TIME_UNIT_FOREVER_REL.value)
1340 plugin->address_update_task
1341 = GNUNET_SCHEDULER_add_delayed (plugin->env.sched,
1343 GNUNET_SCHEDULER_PRIORITY_IDLE,
1344 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1346 &expire_address_task, plugin);
1352 * Task used to clean up expired addresses for a plugin.
1354 * @param cls closure
1358 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1360 struct TransportPlugin *plugin = cls;
1361 plugin->address_update_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1362 update_addresses (plugin, GNUNET_NO);
1367 * Function that must be called by each plugin to notify the
1368 * transport service about the addresses under which the transport
1369 * provided by the plugin can be reached.
1371 * @param cls closure
1372 * @param name name of the transport that generated the address
1373 * @param addr one of the addresses of the host, NULL for the last address
1374 * the specific address format depends on the transport
1375 * @param addrlen length of the address
1376 * @param expires when should this address automatically expire?
1379 plugin_env_notify_address (void *cls,
1383 struct GNUNET_TIME_Relative expires)
1385 struct TransportPlugin *p = cls;
1386 struct AddressList *al;
1387 struct GNUNET_TIME_Absolute abex;
1390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1391 "Plugin `%s' informs us about a new address\n", name);
1393 abex = GNUNET_TIME_relative_to_absolute (expires);
1394 GNUNET_assert (p == find_transport (name));
1399 if ((addrlen == al->addrlen) && (0 == memcmp (addr, &al[1], addrlen)))
1401 if (al->expires.value < abex.value)
1407 al = GNUNET_malloc (sizeof (struct AddressList) + addrlen);
1409 al->next = p->addresses;
1412 al->addrlen = addrlen;
1413 memcpy (&al[1], addr, addrlen);
1414 update_addresses (p, GNUNET_YES);
1418 struct LookupHelloContext
1420 GNUNET_TRANSPORT_AddressCallback iterator;
1427 lookup_address_callback (void *cls,
1429 struct GNUNET_TIME_Absolute expiration,
1430 const void *addr, size_t addrlen)
1432 struct LookupHelloContext *lhc = cls;
1433 lhc->iterator (lhc->iterator_cls, tname, addr, addrlen);
1439 lookup_hello_callback (void *cls,
1440 const struct GNUNET_PeerIdentity *peer,
1441 const struct GNUNET_HELLO_Message *h, uint32_t trust)
1443 struct LookupHelloContext *lhc = cls;
1447 lhc->iterator (lhc->iterator_cls, NULL, NULL, 0);
1453 GNUNET_HELLO_iterate_addresses (h,
1454 GNUNET_NO, &lookup_address_callback, lhc);
1459 * Function that allows a transport to query the known
1460 * network addresses for a given peer.
1462 * @param cls closure
1463 * @param timeout after how long should we time out?
1464 * @param target which peer are we looking for?
1465 * @param iter function to call for each known address
1466 * @param iter_cls closure for iter
1469 plugin_env_lookup_address (void *cls,
1470 struct GNUNET_TIME_Relative timeout,
1471 const struct GNUNET_PeerIdentity *target,
1472 GNUNET_TRANSPORT_AddressCallback iter,
1475 struct LookupHelloContext *lhc;
1477 lhc = GNUNET_malloc (sizeof (struct LookupHelloContext));
1478 lhc->iterator = iter;
1479 lhc->iterator_cls = iter_cls;
1480 GNUNET_PEERINFO_for_all (cfg,
1482 target, 0, timeout, &lookup_hello_callback, &lhc);
1487 * Notify all of our clients about a peer connecting.
1490 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
1491 struct GNUNET_TIME_Relative latency)
1493 struct ConnectInfoMessage cim;
1494 struct TransportClient *cpos;
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1498 "Informing clients about peer `%4s' connecting to us\n",
1501 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
1502 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
1503 cim.quota_out = htonl (default_quota_out);
1504 cim.latency = GNUNET_TIME_relative_hton (latency);
1505 memcpy (&cim.id, peer, sizeof (struct GNUNET_PeerIdentity));
1507 while (cpos != NULL)
1509 transmit_to_client (cpos, &cim.header, GNUNET_NO);
1516 * Notify all of our clients about a peer disconnecting.
1519 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
1521 struct DisconnectInfoMessage dim;
1522 struct TransportClient *cpos;
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1526 "Informing clients about peer `%4s' disconnecting\n",
1529 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
1530 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1531 dim.reserved = htonl (0);
1532 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
1534 while (cpos != NULL)
1536 transmit_to_client (cpos, &dim.header, GNUNET_NO);
1543 * Copy any validated addresses to buf.
1545 * @return 0 once all addresses have been
1549 list_validated_addresses (void *cls, size_t max, void *buf)
1551 struct ValidationAddress **va = cls;
1554 while ((NULL != *va) && ((*va)->ok != GNUNET_YES))
1558 ret = GNUNET_HELLO_add_address ((*va)->transport_name,
1560 &(*va)->msg[1], (*va)->addr_len, buf, max);
1567 * HELLO validation cleanup task.
1570 cleanup_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1572 struct ValidationAddress *va;
1573 struct ValidationList *pos;
1574 struct ValidationList *prev;
1575 struct GNUNET_TIME_Absolute now;
1576 struct GNUNET_HELLO_Message *hello;
1577 struct GNUNET_PeerIdentity pid;
1580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1581 "HELLO validation cleanup background task running...\n");
1583 now = GNUNET_TIME_absolute_get ();
1585 pos = pending_validations;
1588 if (pos->timeout.value < now.value)
1591 pending_validations = pos->next;
1593 prev->next = pos->next;
1594 va = pos->addresses;
1595 hello = GNUNET_HELLO_create (&pos->publicKey,
1596 &list_validated_addresses, &va);
1597 GNUNET_CRYPTO_hash (&pos->publicKey,
1599 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "Creating persistent `%s' message for peer `%4s' based on confirmed addresses.\n",
1604 "HELLO", GNUNET_i2s (&pid));
1606 GNUNET_PEERINFO_add_peer (cfg, sched, &pid, hello);
1607 GNUNET_free (hello);
1608 while (NULL != (va = pos->addresses))
1610 pos->addresses = va->next;
1611 GNUNET_free (va->transport_name);
1616 pos = pending_validations;
1625 /* finally, reschedule cleanup if needed; list is
1626 ordered by timeout, so we need the last element... */
1627 pos = pending_validations;
1628 while ((pos != NULL) && (pos->next != NULL))
1631 GNUNET_SCHEDULER_add_delayed (sched,
1633 GNUNET_SCHEDULER_PRIORITY_IDLE,
1634 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1635 GNUNET_TIME_absolute_get_remaining (pos->
1637 &cleanup_validation, NULL);
1641 struct CheckHelloValidatedContext
1644 * Plugin for which we are validating.
1646 struct TransportPlugin *plugin;
1649 * Hello that we are validating.
1651 struct GNUNET_HELLO_Message *hello;
1654 * Validation list being build.
1656 struct ValidationList *e;
1661 * Append the given address to the list of entries
1662 * that need to be validated.
1665 run_validation (void *cls,
1667 struct GNUNET_TIME_Absolute expiration,
1668 const void *addr, size_t addrlen)
1670 struct ValidationList *e = cls;
1671 struct TransportPlugin *tp;
1672 struct ValidationAddress *va;
1673 struct ValidationChallengeMessage *vcm;
1675 tp = find_transport (tname);
1678 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
1679 GNUNET_ERROR_TYPE_BULK,
1681 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
1685 va = GNUNET_malloc (sizeof (struct ValidationAddress) +
1686 sizeof (struct ValidationChallengeMessage) + addrlen);
1687 va->next = e->addresses;
1689 vcm = (struct ValidationChallengeMessage *) &va[1];
1691 va->transport_name = GNUNET_strdup (tname);
1692 va->addr_len = addrlen;
1694 htons (sizeof (struct ValidationChallengeMessage) + addrlen);
1695 vcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
1697 htonl (sizeof (struct ValidationChallengeMessage) + addrlen -
1698 sizeof (struct GNUNET_MessageHeader));
1699 vcm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO);
1700 vcm->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1702 /* Note: vcm->target is set in check_hello_validated */
1703 memcpy (&vcm[1], addr, addrlen);
1709 * Check if addresses in validated hello "h" overlap with
1710 * those in "chvc->hello" and update "chvc->hello" accordingly,
1711 * removing those addresses that have already been validated.
1714 check_hello_validated (void *cls,
1715 const struct GNUNET_PeerIdentity *peer,
1716 const struct GNUNET_HELLO_Message *h, uint32_t trust)
1718 struct CheckHelloValidatedContext *chvc = cls;
1719 struct ValidationAddress *va;
1720 struct TransportPlugin *tp;
1723 first_call = GNUNET_NO;
1724 if (chvc->e == NULL)
1726 first_call = GNUNET_YES;
1727 chvc->e = GNUNET_malloc (sizeof (struct ValidationList));
1728 GNUNET_HELLO_get_key (h != NULL ? h : chvc->hello, &chvc->e->publicKey);
1730 GNUNET_TIME_relative_to_absolute (HELLO_VERIFICATION_TIMEOUT);
1731 chvc->e->next = pending_validations;
1732 pending_validations = chvc->e;
1736 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
1738 GNUNET_TIME_absolute_get (),
1739 &run_validation, chvc->e);
1741 else if (GNUNET_YES == first_call)
1743 /* no existing HELLO, all addresses are new */
1744 GNUNET_HELLO_iterate_addresses (chvc->hello,
1745 GNUNET_NO, &run_validation, chvc->e);
1748 return; /* wait for next call */
1749 /* finally, transmit validation attempts */
1750 va = chvc->e->addresses;
1753 GNUNET_CRYPTO_hash (&chvc->e->publicKey,
1755 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1756 &va->msg->target.hashPubKey);
1758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1759 "Establishing `%s' connection to validate `%s' of `%4s' (sending our `%s')\n",
1761 "HELLO", GNUNET_i2s (&va->msg->target), "HELLO");
1763 tp = find_transport (va->transport_name);
1764 GNUNET_assert (tp != NULL);
1766 tp->api->send_to (tp->api->cls,
1768 (const struct GNUNET_MessageHeader *) our_hello,
1770 HELLO_VERIFICATION_TIMEOUT,
1771 &va->msg[1], va->addr_len))
1772 va->ok = GNUNET_SYSERR;
1775 if (chvc->e->next == NULL)
1776 GNUNET_SCHEDULER_add_delayed (sched,
1778 GNUNET_SCHEDULER_PRIORITY_IDLE,
1779 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1780 GNUNET_TIME_absolute_get_remaining (chvc->
1783 &cleanup_validation, NULL);
1789 * Process HELLO-message.
1791 * @param plugin transport involved, may be NULL
1792 * @param message the actual message
1793 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
1796 process_hello (struct TransportPlugin *plugin,
1797 const struct GNUNET_MessageHeader *message)
1799 struct ValidationList *e;
1801 struct GNUNET_PeerIdentity target;
1802 const struct GNUNET_HELLO_Message *hello;
1803 struct CheckHelloValidatedContext *chvc;
1804 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
1806 hsize = ntohs (message->size);
1807 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
1808 (hsize < sizeof (struct GNUNET_MessageHeader)))
1811 return GNUNET_SYSERR;
1813 /* first, check if load is too high */
1814 if (GNUNET_OS_load_cpu_get (cfg) > 100)
1816 /* TODO: call to stats? */
1819 hello = (const struct GNUNET_HELLO_Message *) message;
1820 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
1822 GNUNET_break_op (0);
1823 return GNUNET_SYSERR;
1825 GNUNET_CRYPTO_hash (&publicKey,
1826 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1827 &target.hashPubKey);
1829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1830 "Processing `%s' message for `%4s'\n",
1831 "HELLO", GNUNET_i2s (&target));
1833 /* check if a HELLO for this peer is already on the validation list */
1834 e = pending_validations;
1837 if (0 == memcmp (&e->publicKey,
1840 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
1842 /* TODO: call to stats? */
1847 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
1848 chvc->plugin = plugin;
1849 chvc->hello = (struct GNUNET_HELLO_Message *) &chvc[1];
1850 memcpy (chvc->hello, hello, hsize);
1851 /* finally, check if HELLO was previously validated
1852 (continuation will then schedule actual validation) */
1853 GNUNET_PEERINFO_for_all (cfg,
1857 HELLO_VERIFICATION_TIMEOUT,
1858 &check_hello_validated, chvc);
1864 * Handle PING-message. If the plugin that gave us the message is
1865 * able to queue the PONG immediately, we only queue one PONG.
1866 * Otherwise we send at most TWO PONG messages, one via an unconfirmed
1867 * transport and one via a confirmed transport. Both addresses are
1868 * selected randomly among those available.
1870 * @param plugin plugin that gave us the message
1871 * @param sender claimed sender of the PING
1872 * @param plugin_context context that might be used to send response
1873 * @param message the actual message
1876 process_ping (struct TransportPlugin *plugin,
1877 const struct GNUNET_PeerIdentity *sender,
1878 void *plugin_context,
1879 const struct GNUNET_MessageHeader *message)
1881 const struct ValidationChallengeMessage *vcm;
1882 struct ValidationChallengeResponse vcr;
1884 struct NeighbourList *n;
1887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1888 "Processing PING\n");
1890 msize = ntohs (message->size);
1891 if (msize < sizeof (struct ValidationChallengeMessage))
1893 GNUNET_break_op (0);
1896 vcm = (const struct ValidationChallengeMessage *) message;
1897 if (0 != memcmp (&vcm->target,
1898 &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1900 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1901 _("Received `%s' message not destined for me!\n"), "PING");
1902 /* TODO: call statistics */
1905 if ((ntohl (vcm->purpose.size) !=
1906 msize - sizeof (struct GNUNET_MessageHeader))
1907 || (ntohl (vcm->purpose.purpose) !=
1908 GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO))
1910 GNUNET_break_op (0);
1913 msize -= sizeof (struct ValidationChallengeMessage);
1915 plugin->api->address_suggested (plugin->api->cls, &vcm[1], msize))
1917 GNUNET_break_op (0);
1920 vcr.header.size = htons (sizeof (struct ValidationChallengeResponse));
1921 vcr.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
1922 vcr.challenge = vcm->challenge;
1923 vcr.sender = my_identity;
1924 GNUNET_assert (GNUNET_OK ==
1925 GNUNET_CRYPTO_rsa_sign (my_private_key,
1926 &vcm->purpose, &vcr.signature));
1928 GNUNET_assert (GNUNET_OK ==
1929 GNUNET_CRYPTO_rsa_verify
1930 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO, &vcm->purpose,
1931 &vcr.signature, &my_public_key));
1934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1935 "Trying to transmit PONG using inbound connection\n");
1937 n = find_neighbour (sender);
1938 transmit_to_peer (NULL, &vcr.header, GNUNET_YES, n);
1943 * Handle PONG-message.
1945 * @param message the actual message
1948 process_pong (struct TransportPlugin *plugin,
1949 const struct GNUNET_MessageHeader *message)
1951 const struct ValidationChallengeResponse *vcr;
1952 struct ValidationList *pos;
1953 struct GNUNET_PeerIdentity peer;
1954 struct ValidationAddress *va;
1959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1960 "Processing PONG\n");
1962 vcr = (const struct ValidationChallengeResponse *) message;
1963 pos = pending_validations;
1966 GNUNET_CRYPTO_hash (&pos->publicKey,
1968 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1971 memcmp (&peer, &vcr->sender, sizeof (struct GNUNET_PeerIdentity)))
1977 /* TODO: call statistics (unmatched PONG) */
1978 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1980 ("Received `%s' message but have no record of a matching `%s' message. Ignoring.\n"),
1984 all_done = GNUNET_YES;
1985 matched = GNUNET_NO;
1986 va = pos->addresses;
1989 if (va->msg->challenge == vcr->challenge)
1992 GNUNET_CRYPTO_rsa_verify
1993 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO, &va->msg->purpose,
1994 &vcr->signature, &pos->publicKey))
1996 /* this could rarely happen if we used the same
1997 challenge number for the peer for two different
1998 transports / addresses, but the likelihood is
2000 GNUNET_break_op (0);
2005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2006 "Confirmed validity of peer address.\n");
2008 va->ok = GNUNET_YES;
2010 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2011 matched = GNUNET_YES;
2014 if (va->ok != GNUNET_YES)
2015 all_done = GNUNET_NO;
2018 if (GNUNET_NO == matched)
2020 /* TODO: call statistics (unmatched PONG) */
2021 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2023 ("Received `%s' message but have no record of a matching `%s' message. Ignoring.\n"),
2026 if (GNUNET_YES == all_done)
2028 pos->timeout.value = 0;
2029 GNUNET_SCHEDULER_add_delayed (sched,
2031 GNUNET_SCHEDULER_PRIORITY_IDLE,
2032 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
2033 GNUNET_TIME_UNIT_ZERO,
2034 &cleanup_validation, NULL);
2040 * The peer specified by the given neighbour has timed-out. Update
2041 * our state and do the necessary notifications. Also notifies
2042 * our clients that the neighbour is now officially gone.
2044 * @param n the neighbour list entry for the peer
2047 disconnect_neighbour (struct NeighbourList *n)
2049 struct ReadyList *rpos;
2050 struct NeighbourList *npos;
2051 struct NeighbourList *nprev;
2052 struct MessageQueue *mq;
2055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2056 "Disconnecting from neighbour\n");
2058 /* remove n from neighbours list */
2061 while ((npos != NULL) && (npos != n))
2066 GNUNET_assert (npos != NULL);
2068 neighbours = n->next;
2070 nprev->next = n->next;
2072 /* notify all clients about disconnect */
2073 notify_clients_disconnect (&n->id);
2075 /* clean up all plugins, cancel connections & pending transmissions */
2076 while (NULL != (rpos = n->plugins))
2078 n->plugins = rpos->next;
2079 GNUNET_assert (rpos->neighbour == n);
2080 rpos->plugin->api->cancel (rpos->plugin->api->cls,
2081 rpos->plugin_handle, rpos, &n->id);
2085 /* free all messages on the queue */
2086 while (NULL != (mq = n->messages))
2088 n->messages = mq->next;
2089 GNUNET_assert (mq->neighbour == n);
2093 /* finally, free n itself */
2099 * Add an entry for each of our transport plugins
2100 * (that are able to send) to the list of plugins
2101 * for this neighbour.
2103 * @param neighbour to initialize
2106 add_plugins (struct NeighbourList *neighbour)
2108 struct TransportPlugin *tp;
2109 struct ReadyList *rl;
2111 neighbour->retry_plugins_time
2112 = GNUNET_TIME_relative_to_absolute (PLUGIN_RETRY_FREQUENCY);
2116 if (tp->api->send != NULL)
2118 rl = GNUNET_malloc (sizeof (struct ReadyList));
2119 rl->next = neighbour->plugins;
2120 neighbour->plugins = rl;
2122 rl->neighbour = neighbour;
2123 rl->transmit_ready = GNUNET_YES;
2131 neighbour_timeout_task (void *cls,
2132 const struct GNUNET_SCHEDULER_TaskContext *tc)
2134 struct NeighbourList *n = cls;
2137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2138 "Neighbour has timed out!\n");
2140 n->timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
2141 disconnect_neighbour (n);
2147 * Create a fresh entry in our neighbour list for the given peer.
2148 * Will try to transmit our current HELLO to the new neighbour. Also
2149 * notifies our clients about the new "connection".
2151 * @param peer the peer for which we create the entry
2152 * @return the new neighbour list entry
2154 static struct NeighbourList *
2155 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer)
2157 struct NeighbourList *n;
2160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2161 "Setting up new neighbour `%4s', sending our HELLO to introduce ourselves\n",
2164 GNUNET_assert (our_hello != NULL);
2165 n = GNUNET_malloc (sizeof (struct NeighbourList));
2166 n->next = neighbours;
2169 n->last_quota_update = GNUNET_TIME_absolute_get ();
2171 GNUNET_TIME_relative_to_absolute (IDLE_CONNECTION_TIMEOUT);
2172 n->quota_in = default_quota_in;
2174 n->hello_version_sent = our_hello_version;
2175 n->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
2177 GNUNET_SCHEDULER_PRIORITY_IDLE,
2178 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
2179 IDLE_CONNECTION_TIMEOUT,
2180 &neighbour_timeout_task, n);
2181 transmit_to_peer (NULL,
2182 (const struct GNUNET_MessageHeader *) our_hello,
2184 notify_clients_connect (peer, GNUNET_TIME_UNIT_FOREVER_REL);
2190 * Function called by the plugin for each received message.
2191 * Update data volumes, possibly notify plugins about
2192 * reducing the rate at which they read from the socket
2193 * and generally forward to our receive callback.
2195 * @param plugin_context value to pass to this plugin
2196 * to respond to the given peer (use is optional,
2197 * but may speed up processing)
2198 * @param service_context value passed to the transport-service
2199 * to identify the neighbour; will be NULL on the first
2200 * call for a given peer
2201 * @param latency estimated latency for communicating with the
2203 * @param peer (claimed) identity of the other peer
2204 * @param message the message, NULL if peer was disconnected
2205 * @return the new service_context that the plugin should use
2206 * for future receive calls for messages from this
2209 static struct ReadyList *
2210 plugin_env_receive (void *cls,
2211 void *plugin_context,
2212 struct ReadyList *service_context,
2213 struct GNUNET_TIME_Relative latency,
2214 const struct GNUNET_PeerIdentity *peer,
2215 const struct GNUNET_MessageHeader *message)
2217 const struct GNUNET_MessageHeader ack = {
2218 htons (sizeof (struct GNUNET_MessageHeader)),
2219 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ACK)
2221 struct TransportPlugin *plugin = cls;
2222 struct TransportClient *cpos;
2223 struct InboundMessage *im;
2225 struct NeighbourList *n;
2227 if (service_context != NULL)
2229 n = service_context->neighbour;
2230 GNUNET_assert (n != NULL);
2234 n = find_neighbour (peer);
2237 if (message == NULL)
2238 return NULL; /* disconnect of peer already marked down */
2239 n = setup_new_neighbour (peer);
2241 service_context = n->plugins;
2242 while ((service_context != NULL) && (plugin != service_context->plugin))
2243 service_context = service_context->next;
2244 GNUNET_assert ((plugin->api->send == NULL) ||
2245 (service_context != NULL));
2247 if (message == NULL)
2249 if ((service_context != NULL) &&
2250 (service_context->plugin_handle == plugin_context))
2252 service_context->connected = GNUNET_NO;
2253 service_context->plugin_handle = NULL;
2255 /* TODO: call stats */
2259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2260 "Processing message of type `%u' received by plugin...\n",
2261 ntohs (message->type));
2263 if (service_context != NULL)
2265 if (service_context->connected == GNUNET_NO)
2267 service_context->connected = GNUNET_YES;
2268 service_context->transmit_ready = GNUNET_YES;
2269 service_context->connect_attempts++;
2271 service_context->timeout
2272 = GNUNET_TIME_relative_to_absolute (IDLE_CONNECTION_TIMEOUT);
2273 service_context->plugin_handle = plugin_context;
2274 service_context->latency = latency;
2276 /* update traffic received amount ... */
2277 msize = ntohs (message->size);
2278 n->last_received += msize;
2279 GNUNET_SCHEDULER_cancel (sched, n->timeout_task);
2281 GNUNET_TIME_relative_to_absolute (IDLE_CONNECTION_TIMEOUT);
2283 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_NO,
2284 GNUNET_SCHEDULER_PRIORITY_IDLE,
2285 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
2286 IDLE_CONNECTION_TIMEOUT,
2287 &neighbour_timeout_task, n);
2289 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2291 /* dropping message due to frequent inbound volume violations! */
2292 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
2293 GNUNET_ERROR_TYPE_BULK,
2295 ("Dropping incoming message due to repeated bandwidth quota violations.\n"));
2296 /* TODO: call stats */
2297 return service_context;
2299 switch (ntohs (message->type))
2301 case GNUNET_MESSAGE_TYPE_HELLO:
2303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2304 "Receiving `%s' message from other peer.\n", "HELLO");
2306 process_hello (plugin, message);
2308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2309 "Sending `%s' message to connecting peer.\n", "ACK");
2311 transmit_to_peer (NULL, &ack, GNUNET_YES, n);
2313 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
2314 process_ping (plugin, peer, plugin_context, message);
2316 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
2317 process_pong (plugin, message);
2319 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
2320 n->saw_ack = GNUNET_YES;
2321 /* intentional fall-through! */
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 "Received message of type %u from other peer, sending to all clients.\n",
2326 ntohs (message->type));
2328 /* transmit message to all clients */
2329 im = GNUNET_malloc (sizeof (struct InboundMessage) + msize);
2330 im->header.size = htons (sizeof (struct InboundMessage) + msize);
2331 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
2332 im->latency = GNUNET_TIME_relative_hton (latency);
2334 memcpy (&im[1], message, msize);
2337 while (cpos != NULL)
2339 transmit_to_client (cpos, &im->header, GNUNET_YES);
2344 return service_context;
2349 * Handle START-message. This is the first message sent to us
2350 * by any client which causes us to add it to our list.
2352 * @param cls closure (always NULL)
2353 * @param server the server handling the message
2354 * @param client identification of the client
2355 * @param message the actual message
2358 handle_start (void *cls,
2359 struct GNUNET_SERVER_Handle *server,
2360 struct GNUNET_SERVER_Client *client,
2361 const struct GNUNET_MessageHeader *message)
2363 struct TransportClient *c;
2364 struct ConnectInfoMessage cim;
2365 struct NeighbourList *n;
2366 struct InboundMessage *im;
2367 struct GNUNET_MessageHeader *ack;
2370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2371 "Received `%s' request from client\n", "START");
2376 if (c->client == client)
2378 /* client already on our list! */
2380 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2385 c = GNUNET_malloc (sizeof (struct TransportClient));
2389 if (our_hello != NULL)
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2393 "Sending our own HELLO to new client\n");
2395 transmit_to_client (c,
2396 (const struct GNUNET_MessageHeader *) our_hello,
2398 /* tell new client about all existing connections */
2399 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
2400 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2401 cim.quota_out = htonl (default_quota_out);
2402 cim.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO); /* FIXME? */
2403 im = GNUNET_malloc (sizeof (struct InboundMessage) +
2404 sizeof (struct GNUNET_MessageHeader));
2405 im->header.size = htons (sizeof (struct InboundMessage) +
2406 sizeof (struct GNUNET_MessageHeader));
2407 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
2408 im->latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO); /* FIXME? */
2409 ack = (struct GNUNET_MessageHeader *) &im[1];
2410 ack->size = htons (sizeof (struct GNUNET_MessageHeader));
2411 ack->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ACK);
2412 for (n = neighbours; n != NULL; n = n->next)
2415 transmit_to_client (c, &cim.header, GNUNET_NO);
2419 transmit_to_client (c, &im->header, GNUNET_NO);
2424 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2429 * Handle HELLO-message.
2431 * @param cls closure (always NULL)
2432 * @param server the server handling the message
2433 * @param client identification of the client
2434 * @param message the actual message
2437 handle_hello (void *cls,
2438 struct GNUNET_SERVER_Handle *server,
2439 struct GNUNET_SERVER_Client *client,
2440 const struct GNUNET_MessageHeader *message)
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 "Received `%s' request from client\n", "HELLO");
2448 ret = process_hello (NULL, message);
2449 GNUNET_SERVER_receive_done (client, ret);
2454 * Handle SEND-message.
2456 * @param cls closure (always NULL)
2457 * @param server the server handling the message
2458 * @param client identification of the client
2459 * @param message the actual message
2462 handle_send (void *cls,
2463 struct GNUNET_SERVER_Handle *server,
2464 struct GNUNET_SERVER_Client *client,
2465 const struct GNUNET_MessageHeader *message)
2467 struct TransportClient *tc;
2468 struct NeighbourList *n;
2469 const struct OutboundMessage *obm;
2470 const struct GNUNET_MessageHeader *obmm;
2474 size = ntohs (message->size);
2476 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
2479 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2482 obm = (const struct OutboundMessage *) message;
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2485 "Received `%s' request from client with target `%4s'\n",
2486 "SEND", GNUNET_i2s (&obm->peer));
2488 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
2489 msize = ntohs (obmm->size);
2490 if (size != msize + sizeof (struct OutboundMessage))
2493 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2496 n = find_neighbour (&obm->peer);
2498 n = setup_new_neighbour (&obm->peer);
2500 while ((tc != NULL) && (tc->client != client))
2504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2505 "Client asked to transmit %u-byte message of type %u to `%4s'\n",
2507 ntohs (obmm->type), GNUNET_i2s (&obm->peer));
2509 transmit_to_peer (tc, obmm, GNUNET_NO, n);
2510 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2515 * Handle SET_QUOTA-message.
2517 * @param cls closure (always NULL)
2518 * @param server the server handling the message
2519 * @param client identification of the client
2520 * @param message the actual message
2523 handle_set_quota (void *cls,
2524 struct GNUNET_SERVER_Handle *server,
2525 struct GNUNET_SERVER_Client *client,
2526 const struct GNUNET_MessageHeader *message)
2528 const struct QuotaSetMessage *qsm =
2529 (const struct QuotaSetMessage *) message;
2530 struct NeighbourList *n;
2531 struct TransportPlugin *p;
2532 struct ReadyList *rl;
2535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2536 "Received `%s' request from client for peer `%4s'\n",
2537 "SET_QUOTA", GNUNET_i2s (&qsm->peer));
2539 n = find_neighbour (&qsm->peer);
2542 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2546 if (n->quota_in < ntohl (qsm->quota_in))
2547 n->last_quota_update = GNUNET_TIME_absolute_get ();
2548 n->quota_in = ntohl (qsm->quota_in);
2553 p->api->set_receive_quota (p->api->cls,
2554 &qsm->peer, ntohl (qsm->quota_in));
2557 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2562 * Handle TRY_CONNECT-message.
2564 * @param cls closure (always NULL)
2565 * @param server the server handling the message
2566 * @param client identification of the client
2567 * @param message the actual message
2570 handle_try_connect (void *cls,
2571 struct GNUNET_SERVER_Handle *server,
2572 struct GNUNET_SERVER_Client *client,
2573 const struct GNUNET_MessageHeader *message)
2575 const struct TryConnectMessage *tcm;
2577 tcm = (const struct TryConnectMessage *) message;
2579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2580 "Received `%s' request from client asking to connect to `%4s'\n",
2581 "TRY_CONNECT", GNUNET_i2s (&tcm->peer));
2583 if (NULL == find_neighbour (&tcm->peer))
2584 setup_new_neighbour (&tcm->peer);
2585 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2590 * List of handlers for the messages understood by this
2593 static struct GNUNET_SERVER_MessageHandler handlers[] = {
2594 {&handle_start, NULL,
2595 GNUNET_MESSAGE_TYPE_TRANSPORT_START, 0},
2596 {&handle_hello, NULL,
2597 GNUNET_MESSAGE_TYPE_HELLO, 0},
2598 {&handle_send, NULL,
2599 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
2600 {&handle_set_quota, NULL,
2601 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
2602 {&handle_try_connect, NULL,
2603 GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT,
2604 sizeof (struct TryConnectMessage)},
2610 * Setup the environment for this plugin.
2613 create_environment (struct TransportPlugin *plug)
2615 plug->env.cfg = cfg;
2616 plug->env.sched = sched;
2617 plug->env.my_public_key = &my_public_key;
2618 plug->env.cls = plug;
2619 plug->env.receive = &plugin_env_receive;
2620 plug->env.lookup = &plugin_env_lookup_address;
2621 plug->env.notify_address = &plugin_env_notify_address;
2622 plug->env.default_quota_in = default_quota_in;
2623 plug->env.max_connections = max_connect_per_transport;
2628 * Start the specified transport (load the plugin).
2631 start_transport (struct GNUNET_SERVER_Handle *server, const char *name)
2633 struct TransportPlugin *plug;
2636 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2637 _("Loading `%s' transport plugin\n"), name);
2638 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
2639 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
2640 create_environment (plug);
2641 plug->short_name = GNUNET_strdup (name);
2642 plug->lib_name = libname;
2643 plug->next = plugins;
2645 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
2646 if (plug->api == NULL)
2648 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2649 _("Failed to load transport plugin for `%s'\n"), name);
2650 GNUNET_free (plug->short_name);
2651 plugins = plug->next;
2652 GNUNET_free (libname);
2659 * Called whenever a client is disconnected. Frees our
2660 * resources associated with that client.
2662 * @param cls closure
2663 * @param client identification of the client
2666 client_disconnect_notification (void *cls,
2667 struct GNUNET_SERVER_Client *client)
2669 struct TransportClient *pos;
2670 struct TransportClient *prev;
2671 struct ClientMessageQueueEntry *mqe;
2674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2675 "Client disconnected, cleaning up.\n");
2679 while ((pos != NULL) && (pos->client != client))
2686 while (NULL != (mqe = pos->message_queue_head))
2688 pos->message_queue_head = mqe->next;
2691 pos->message_queue_head = NULL;
2693 clients = pos->next;
2695 prev->next = pos->next;
2696 if (GNUNET_YES == pos->tcs_pending)
2706 * Initiate transport service.
2708 * @param cls closure
2709 * @param s scheduler to use
2710 * @param serv the initialized server
2711 * @param c configuration to use
2715 struct GNUNET_SCHEDULER_Handle *s,
2716 struct GNUNET_SERVER_Handle *serv, struct GNUNET_CONFIGURATION_Handle *c)
2721 unsigned long long qin;
2722 unsigned long long qout;
2723 unsigned long long tneigh;
2728 /* parse configuration */
2730 GNUNET_CONFIGURATION_get_value_number (c,
2735 GNUNET_CONFIGURATION_get_value_number (c,
2737 "DEFAULT_QUOTA_OUT",
2740 GNUNET_CONFIGURATION_get_value_number (c,
2745 GNUNET_CONFIGURATION_get_value_filename (c,
2747 "HOSTKEY", &keyfile)))
2749 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2751 ("Transport service is lacking key configuration settings. Exiting.\n"));
2752 GNUNET_SCHEDULER_shutdown (s);
2755 max_connect_per_transport = (uint32_t) tneigh;
2756 default_quota_in = (uint32_t) qin;
2757 default_quota_out = (uint32_t) qout;
2758 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2759 GNUNET_free (keyfile);
2760 if (my_private_key == NULL)
2762 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2764 ("Transport service could not access hostkey. Exiting.\n"));
2765 GNUNET_SCHEDULER_shutdown (s);
2768 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
2769 GNUNET_CRYPTO_hash (&my_public_key,
2770 sizeof (my_public_key), &my_identity.hashPubKey);
2771 /* setup notification */
2773 GNUNET_SERVER_disconnect_notify (server,
2774 &client_disconnect_notification, NULL);
2775 /* load plugins... */
2778 GNUNET_CONFIGURATION_get_value_string (c,
2779 "TRANSPORT", "PLUGINS", &plugs))
2781 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2782 _("Starting transport plugins `%s'\n"), plugs);
2783 pos = strtok (plugs, " ");
2786 start_transport (server, pos);
2788 pos = strtok (NULL, " ");
2790 GNUNET_free (plugs);
2794 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport service ready.\n"));
2795 /* process client requests */
2796 GNUNET_SERVER_add_handlers (server, handlers);
2801 * Function called when the service shuts
2802 * down. Unloads our plugins.
2804 * @param cls closure
2805 * @param cfg configuration to use
2808 unload_plugins (void *cls, struct GNUNET_CONFIGURATION_Handle *cfg)
2810 struct TransportPlugin *plug;
2811 struct AddressList *al;
2814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2815 "Transport service is unloading plugins...\n");
2817 while (NULL != (plug = plugins))
2819 plugins = plug->next;
2820 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
2821 GNUNET_free (plug->lib_name);
2822 GNUNET_free (plug->short_name);
2823 while (NULL != (al = plug->addresses))
2825 plug->addresses = al->next;
2830 if (my_private_key != NULL)
2831 GNUNET_CRYPTO_rsa_key_free (my_private_key);
2836 * The main function for the transport service.
2838 * @param argc number of arguments from the command line
2839 * @param argv command line arguments
2840 * @return 0 ok, 1 on error
2843 main (int argc, char *const *argv)
2845 return (GNUNET_OK ==
2846 GNUNET_SERVICE_run (argc,
2849 &run, NULL, &unload_plugins, NULL)) ? 0 : 1;
2852 /* end of gnunet-service-transport.c */