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_constants.h"
33 #include "gnunet_getopt_lib.h"
34 #include "gnunet_hello_lib.h"
35 #include "gnunet_os_lib.h"
36 #include "gnunet_peerinfo_service.h"
37 #include "gnunet_plugin_lib.h"
38 #include "gnunet_protocols.h"
39 #include "gnunet_service_lib.h"
40 #include "gnunet_signatures.h"
41 #include "plugin_transport.h"
42 #include "transport.h"
45 * How many messages can we have pending for a given client process
46 * before we start to drop incoming messages? We typically should
47 * have only one client and so this would be the primary buffer for
48 * messages, so the number should be chosen rather generously.
50 * The expectation here is that most of the time the queue is large
51 * enough so that a drop is virtually never required.
53 #define MAX_PENDING 128
56 * How often should we try to reconnect to a peer using a particular
57 * transport plugin before giving up? Note that the plugin may be
58 * added back to the list after PLUGIN_RETRY_FREQUENCY expires.
60 #define MAX_CONNECT_RETRY 3
63 * How often must a peer violate bandwidth quotas before we start
64 * to simply drop its messages?
66 #define QUOTA_VIOLATION_DROP_THRESHOLD 100
69 * How long until a HELLO verification attempt should time out?
71 #define HELLO_VERIFICATION_TIMEOUT GNUNET_TIME_UNIT_MINUTES
74 * How often do we re-add (cheaper) plugins to our list of plugins
75 * to try for a given connected peer?
77 #define PLUGIN_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
80 * After how long do we expire an address in a HELLO
81 * that we just validated? This value is also used
82 * for our own addresses when we create a HELLO.
84 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
88 * Entry in linked list of network addresses.
93 * This is a linked list.
95 struct AddressList *next;
98 * The address, actually a pointer to the end
99 * of this struct. Do not free!
104 * How long until we auto-expire this address (unless it is
105 * re-confirmed by the transport)?
107 struct GNUNET_TIME_Absolute expires;
118 * Entry in linked list of all of our plugins.
120 struct TransportPlugin
124 * This is a linked list.
126 struct TransportPlugin *next;
129 * API of the transport as returned by the plugin's
130 * initialization function.
132 struct GNUNET_TRANSPORT_PluginFunctions *api;
135 * Short name for the plugin (i.e. "tcp").
140 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
145 * List of our known addresses for this transport.
147 struct AddressList *addresses;
150 * Environment this transport service is using
153 struct GNUNET_TRANSPORT_PluginEnvironment env;
156 * ID of task that is used to clean up expired addresses.
158 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
162 * Set to GNUNET_YES if we need to scrap the existing
163 * list of "addresses" and start fresh when we receive
164 * the next address update from a transport. Set to
165 * GNUNET_NO if we should just add the new address
166 * to the list and wait for the commit call.
171 struct NeighbourList;
174 * For each neighbour we keep a list of messages
175 * that we still want to transmit to the neighbour.
181 * This is a linked list.
183 struct MessageQueue *next;
186 * The message we want to transmit.
188 struct GNUNET_MessageHeader *message;
191 * Client responsible for queueing the message;
192 * used to check that a client has not two messages
193 * pending for the same target. Can be NULL.
195 struct TransportClient *client;
198 * Neighbour this entry belongs to.
200 struct NeighbourList *neighbour;
203 * Plugin that we used for the transmission.
204 * NULL until we scheduled a transmission.
206 struct TransportPlugin *plugin;
209 * Internal message of the transport system that should not be
210 * included in the usual SEND-SEND_OK transmission confirmation
211 * traffic management scheme. Typically, "internal_msg" will
212 * be set whenever "client" is NULL (but it is not strictly
218 * How important is the message?
220 unsigned int priority;
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 inbound 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 * Number of neighbours we'd like to have.
646 static uint32_t max_connect_per_transport;
650 * Find an entry in the neighbour list for a particular peer.
652 * @return NULL if not found.
654 static struct NeighbourList *
655 find_neighbour (const struct GNUNET_PeerIdentity *key)
657 struct NeighbourList *head = neighbours;
658 while ((head != NULL) &&
659 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
666 * Find an entry in the transport list for a particular transport.
668 * @return NULL if not found.
670 static struct TransportPlugin *
671 find_transport (const char *short_name)
673 struct TransportPlugin *head = plugins;
674 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
681 * Update the quota values for the given neighbour now.
684 update_quota (struct NeighbourList *n)
686 struct GNUNET_TIME_Relative delta;
690 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
691 if (delta.value < MIN_QUOTA_REFRESH_TIME)
692 return; /* not enough time passed for doing quota update */
693 allowed = delta.value * n->quota_in;
694 if (n->last_received < allowed)
696 remaining = allowed - n->last_received;
698 remaining /= n->quota_in;
701 if (remaining > MAX_BANDWIDTH_CARRY)
702 remaining = MAX_BANDWIDTH_CARRY;
703 n->last_received = 0;
704 n->last_quota_update = GNUNET_TIME_absolute_get ();
705 n->last_quota_update.value -= remaining;
706 if (n->quota_violation_count > 0)
707 n->quota_violation_count--;
711 n->last_received -= allowed;
712 n->last_quota_update = GNUNET_TIME_absolute_get ();
713 if (n->last_received > allowed)
715 /* more than twice the allowed rate! */
716 n->quota_violation_count += 10;
723 * Function called to notify a client about the socket
724 * being ready to queue more data. "buf" will be
725 * NULL and "size" zero if the socket was closed for
726 * writing in the meantime.
729 * @param size number of bytes available in buf
730 * @param buf where the callee should write the message
731 * @return number of bytes written to buf
734 transmit_to_client_callback (void *cls, size_t size, void *buf)
736 struct TransportClient *client = cls;
737 struct ClientMessageQueueEntry *q;
740 const struct GNUNET_MessageHeader *msg;
741 struct GNUNET_NETWORK_TransmitHandle *th;
746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
747 "Transmission to client failed, closing connection.\n");
748 /* fatal error with client, free message queue! */
749 while (NULL != (q = client->message_queue_head))
751 client->message_queue_head = q->next;
754 client->message_queue_tail = NULL;
755 client->message_count = 0;
760 while (NULL != (q = client->message_queue_head))
762 msg = (const struct GNUNET_MessageHeader *) &q[1];
763 msize = ntohs (msg->size);
764 if (msize + tsize > size)
766 client->message_queue_head = q->next;
768 client->message_queue_tail = NULL;
769 memcpy (&cbuf[tsize], msg, msize);
772 client->message_count--;
774 GNUNET_assert (tsize > 0);
777 th = GNUNET_SERVER_notify_transmit_ready (client->client,
779 GNUNET_TIME_UNIT_FOREVER_REL,
780 &transmit_to_client_callback,
782 GNUNET_assert (th != NULL);
789 * Send the specified message to the specified client. Since multiple
790 * messages may be pending for the same client at a time, this code
791 * makes sure that no message is lost.
793 * @param client client to transmit the message to
794 * @param msg the message to send
795 * @param may_drop can this message be dropped if the
796 * message queue for this client is getting far too large?
799 transmit_to_client (struct TransportClient *client,
800 const struct GNUNET_MessageHeader *msg, int may_drop)
802 struct ClientMessageQueueEntry *q;
804 struct GNUNET_NETWORK_TransmitHandle *th;
806 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
808 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
810 ("Dropping message, have %u messages pending (%u is the soft limit)\n"),
811 client->message_count, MAX_PENDING);
812 /* TODO: call to statistics... */
815 client->message_count++;
816 msize = ntohs (msg->size);
817 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
818 memcpy (&q[1], msg, msize);
819 /* append to message queue */
820 if (client->message_queue_tail == NULL)
822 client->message_queue_tail = q;
826 client->message_queue_tail->next = q;
827 client->message_queue_tail = q;
829 if (client->message_queue_head == NULL)
831 client->message_queue_head = q;
832 th = GNUNET_SERVER_notify_transmit_ready (client->client,
834 GNUNET_TIME_UNIT_FOREVER_REL,
835 &transmit_to_client_callback,
837 GNUNET_assert (th != NULL);
843 * Find alternative plugins for communication.
845 * @param neighbour for which neighbour should we try to find
849 try_alternative_plugins (struct NeighbourList *neighbour)
851 struct ReadyList *rl;
853 if ((neighbour->plugins != NULL) &&
854 (neighbour->retry_plugins_time.value >
855 GNUNET_TIME_absolute_get ().value))
856 return; /* don't try right now */
857 neighbour->retry_plugins_time
858 = GNUNET_TIME_relative_to_absolute (PLUGIN_RETRY_FREQUENCY);
860 rl = neighbour->plugins;
863 if (rl->connect_attempts > 0)
864 rl->connect_attempts--; /* amnesty */
872 * Check the ready list for the given neighbour and
873 * if a plugin is ready for transmission (and if we
874 * have a message), do so!
876 * @param neighbour target peer for which to check the plugins
878 static void try_transmission_to_peer (struct NeighbourList *neighbour);
882 * Function called by the GNUNET_TRANSPORT_TransmitFunction
883 * upon "completion" of a send request. This tells the API
884 * that it is now legal to send another message to the given
887 * @param cls closure, identifies the entry on the
888 * message queue that was transmitted and the
889 * client responsible for queueing the message
890 * @param rl identifies plugin used for the transmission for
891 * this neighbour; needs to be re-enabled for
892 * future transmissions
893 * @param target the peer receiving the message
894 * @param result GNUNET_OK on success, if the transmission
895 * failed, we should not tell the client to transmit
899 transmit_send_continuation (void *cls,
900 struct ReadyList *rl,
901 const struct GNUNET_PeerIdentity *target,
904 struct MessageQueue *mq = cls;
905 struct SendOkMessage send_ok_msg;
906 struct NeighbourList *n;
908 GNUNET_assert (mq != NULL);
910 GNUNET_assert (n != NULL);
912 memcmp (&n->id, target,
913 sizeof (struct GNUNET_PeerIdentity)));
917 while ((rl != NULL) && (rl->plugin != mq->plugin))
919 GNUNET_assert (rl != NULL);
921 if (result == GNUNET_OK)
923 rl->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
928 "Transmission failed, marking connection as down.\n");
929 rl->connected = GNUNET_NO;
931 if (!mq->internal_msg)
932 rl->transmit_ready = GNUNET_YES;
933 if (mq->client != NULL)
935 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
936 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
937 send_ok_msg.success = htonl (result);
938 send_ok_msg.peer = n->id;
939 transmit_to_client (mq->client, &send_ok_msg.header, GNUNET_NO);
941 GNUNET_free (mq->message);
943 /* one plugin just became ready again, try transmitting
944 another message (if available) */
945 try_transmission_to_peer (n);
952 * We could not use an existing (or validated) connection to
953 * talk to a peer. Try addresses that have not yet been
956 * @param n neighbour we want to communicate with
957 * @return plugin ready to talk, or NULL if none is available
959 static struct ReadyList *
960 try_unvalidated_addresses (struct NeighbourList *n)
962 struct ValidationList *vl;
963 struct ValidationAddress *va;
964 struct GNUNET_PeerIdentity id;
965 struct GNUNET_TIME_Absolute now;
968 struct ReadyList *rl;
969 struct TransportPlugin *plugin;
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "Trying to connect to `%4s' using unvalidated addresses\n",
974 GNUNET_i2s (&n->id));
976 /* NOTE: this function needs to not only identify the
977 plugin but also setup "plugin_handle", binding it to the
978 right address using the plugin's "send_to" API */
979 now = GNUNET_TIME_absolute_get ();
980 vl = pending_validations;
983 GNUNET_CRYPTO_hash (&vl->publicKey,
985 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
987 if (0 == memcmp (&id, &n->id, sizeof (struct GNUNET_PeerIdentity)))
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "No unvalidated address found for peer `%4s'\n",
996 GNUNET_i2s (&n->id));
1006 if (va->expiration.value > now.value)
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1014 "All %u unvalidated addresses for peer have expired\n",
1019 total = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
1020 for (va = vl->addresses; va != NULL; va = va->next)
1022 if (va->expiration.value <= now.value)
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1031 "Trying unvalidated address of `%s' transport\n",
1032 va->transport_name);
1034 plugin = find_transport (va->transport_name);
1040 rl = GNUNET_malloc (sizeof (struct ReadyList));
1041 rl->next = n->plugins;
1043 rl->plugin = plugin;
1044 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,
1134 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1135 &transmit_send_continuation, mq);
1140 * Send the specified message to the specified peer.
1142 * @param client source of the transmission request (can be NULL)
1143 * @param priority how important is the message
1144 * @param msg message to send
1145 * @param is_internal is this an internal message
1146 * @param neighbour handle to the neighbour for transmission
1149 transmit_to_peer (struct TransportClient *client,
1150 unsigned int priority,
1151 const struct GNUNET_MessageHeader *msg,
1152 int is_internal, struct NeighbourList *neighbour)
1154 struct MessageQueue *mq;
1155 struct MessageQueue *mqe;
1156 struct GNUNET_MessageHeader *m;
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160 _("Sending message of type %u to peer `%4s'\n"),
1161 ntohs (msg->type), GNUNET_i2s (&neighbour->id));
1165 /* check for duplicate submission */
1166 mq = neighbour->messages;
1169 if (mq->client == client)
1171 /* client transmitted to same peer twice
1172 before getting SendOk! */
1179 mq = GNUNET_malloc (sizeof (struct MessageQueue));
1180 mq->client = client;
1181 m = GNUNET_malloc (ntohs (msg->size));
1182 memcpy (m, msg, ntohs (msg->size));
1184 mq->neighbour = neighbour;
1185 mq->internal_msg = is_internal;
1186 mq->priority = priority;
1189 mqe = neighbour->messages;
1191 while (mqe->next != NULL)
1196 neighbour->messages = mq;
1197 try_transmission_to_peer (neighbour);
1207 struct GeneratorContext
1209 struct TransportPlugin *plug_pos;
1210 struct AddressList *addr_pos;
1211 struct GNUNET_TIME_Absolute expiration;
1216 address_generator (void *cls, size_t max, void *buf)
1218 struct GeneratorContext *gc = cls;
1221 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
1223 gc->plug_pos = gc->plug_pos->next;
1224 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
1226 if (NULL == gc->plug_pos)
1228 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
1231 gc->addr_pos->addrlen, buf, max);
1232 gc->addr_pos = gc->addr_pos->next;
1238 * Construct our HELLO message from all of the addresses of
1239 * all of the transports.
1244 struct GNUNET_HELLO_Message *hello;
1245 struct TransportClient *cpos;
1246 struct NeighbourList *npos;
1247 struct GeneratorContext gc;
1250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1251 "Refreshing my HELLO\n");
1253 gc.plug_pos = plugins;
1254 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
1255 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1256 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
1258 while (cpos != NULL)
1260 transmit_to_client (cpos,
1261 (const struct GNUNET_MessageHeader *) hello,
1266 GNUNET_free_non_null (our_hello);
1268 our_hello_version++;
1270 while (npos != NULL)
1272 transmit_to_peer (NULL, 0,
1273 (const struct GNUNET_MessageHeader *) our_hello,
1281 * Task used to clean up expired addresses for a plugin.
1283 * @param cls closure
1287 expire_address_task (void *cls,
1288 const struct GNUNET_SCHEDULER_TaskContext *tc);
1292 * Update the list of addresses for this plugin,
1293 * expiring those that are past their expiration date.
1295 * @param plugin addresses of which plugin should be recomputed?
1296 * @param fresh set to GNUNET_YES if a new address was added
1297 * and we need to regenerate the HELLO even if nobody
1301 update_addresses (struct TransportPlugin *plugin, int fresh)
1303 struct GNUNET_TIME_Relative min_remaining;
1304 struct GNUNET_TIME_Relative remaining;
1305 struct GNUNET_TIME_Absolute now;
1306 struct AddressList *pos;
1307 struct AddressList *prev;
1308 struct AddressList *next;
1311 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1312 GNUNET_SCHEDULER_cancel (plugin->env.sched, plugin->address_update_task);
1313 plugin->address_update_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1314 now = GNUNET_TIME_absolute_get ();
1315 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
1316 expired = GNUNET_NO;
1318 pos = plugin->addresses;
1322 if (pos->expires.value < now.value)
1324 expired = GNUNET_YES;
1326 plugin->addresses = pos->next;
1328 prev->next = pos->next;
1333 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
1334 if (remaining.value < min_remaining.value)
1335 min_remaining = remaining;
1341 if (expired || fresh)
1343 if (min_remaining.value < GNUNET_TIME_UNIT_FOREVER_REL.value)
1344 plugin->address_update_task
1345 = GNUNET_SCHEDULER_add_delayed (plugin->env.sched,
1347 GNUNET_SCHEDULER_PRIORITY_IDLE,
1348 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1350 &expire_address_task, plugin);
1356 * Task used to clean up expired addresses for a plugin.
1358 * @param cls closure
1362 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1364 struct TransportPlugin *plugin = cls;
1365 plugin->address_update_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1366 update_addresses (plugin, GNUNET_NO);
1371 * Function that must be called by each plugin to notify the
1372 * transport service about the addresses under which the transport
1373 * provided by the plugin can be reached.
1375 * @param cls closure
1376 * @param name name of the transport that generated the address
1377 * @param addr one of the addresses of the host, NULL for the last address
1378 * the specific address format depends on the transport
1379 * @param addrlen length of the address
1380 * @param expires when should this address automatically expire?
1383 plugin_env_notify_address (void *cls,
1387 struct GNUNET_TIME_Relative expires)
1389 struct TransportPlugin *p = cls;
1390 struct AddressList *al;
1391 struct GNUNET_TIME_Absolute abex;
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "Plugin `%s' informs us about a new address `%s'\n", name,
1396 GNUNET_a2s(addr, addrlen));
1398 abex = GNUNET_TIME_relative_to_absolute (expires);
1399 GNUNET_assert (p == find_transport (name));
1404 if ((addrlen == al->addrlen) && (0 == memcmp (addr, &al[1], addrlen)))
1406 if (al->expires.value < abex.value)
1412 al = GNUNET_malloc (sizeof (struct AddressList) + addrlen);
1414 al->next = p->addresses;
1417 al->addrlen = addrlen;
1418 memcpy (&al[1], addr, addrlen);
1419 update_addresses (p, GNUNET_YES);
1423 struct LookupHelloContext
1425 GNUNET_TRANSPORT_AddressCallback iterator;
1432 lookup_address_callback (void *cls,
1434 struct GNUNET_TIME_Absolute expiration,
1435 const void *addr, size_t addrlen)
1437 struct LookupHelloContext *lhc = cls;
1438 lhc->iterator (lhc->iterator_cls, tname, addr, addrlen);
1444 lookup_hello_callback (void *cls,
1445 const struct GNUNET_PeerIdentity *peer,
1446 const struct GNUNET_HELLO_Message *h, uint32_t trust)
1448 struct LookupHelloContext *lhc = cls;
1452 lhc->iterator (lhc->iterator_cls, NULL, NULL, 0);
1458 GNUNET_HELLO_iterate_addresses (h,
1459 GNUNET_NO, &lookup_address_callback, lhc);
1464 * Function that allows a transport to query the known
1465 * network addresses for a given peer.
1467 * @param cls closure
1468 * @param timeout after how long should we time out?
1469 * @param target which peer are we looking for?
1470 * @param iter function to call for each known address
1471 * @param iter_cls closure for iter
1474 plugin_env_lookup_address (void *cls,
1475 struct GNUNET_TIME_Relative timeout,
1476 const struct GNUNET_PeerIdentity *target,
1477 GNUNET_TRANSPORT_AddressCallback iter,
1480 struct LookupHelloContext *lhc;
1482 lhc = GNUNET_malloc (sizeof (struct LookupHelloContext));
1483 lhc->iterator = iter;
1484 lhc->iterator_cls = iter_cls;
1485 GNUNET_PEERINFO_for_all (cfg,
1487 target, 0, timeout, &lookup_hello_callback, &lhc);
1492 * Notify all of our clients about a peer connecting.
1495 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
1496 struct GNUNET_TIME_Relative latency)
1498 struct ConnectInfoMessage cim;
1499 struct TransportClient *cpos;
1502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1503 "Informing clients about peer `%4s' connecting to us\n",
1506 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
1507 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
1508 cim.quota_out = htonl (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT / (60*1000));
1509 cim.latency = GNUNET_TIME_relative_hton (latency);
1510 memcpy (&cim.id, peer, sizeof (struct GNUNET_PeerIdentity));
1512 while (cpos != NULL)
1514 transmit_to_client (cpos, &cim.header, GNUNET_NO);
1521 * Notify all of our clients about a peer disconnecting.
1524 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
1526 struct DisconnectInfoMessage dim;
1527 struct TransportClient *cpos;
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1531 "Informing clients about peer `%4s' disconnecting\n",
1534 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
1535 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1536 dim.reserved = htonl (0);
1537 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
1539 while (cpos != NULL)
1541 transmit_to_client (cpos, &dim.header, GNUNET_NO);
1548 * Copy any validated addresses to buf.
1550 * @return 0 once all addresses have been
1554 list_validated_addresses (void *cls, size_t max, void *buf)
1556 struct ValidationAddress **va = cls;
1559 while ((NULL != *va) && ((*va)->ok != GNUNET_YES))
1563 ret = GNUNET_HELLO_add_address ((*va)->transport_name,
1565 &(*va)->msg[1], (*va)->addr_len, buf, max);
1572 * HELLO validation cleanup task.
1575 cleanup_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1577 struct ValidationAddress *va;
1578 struct ValidationList *pos;
1579 struct ValidationList *prev;
1580 struct GNUNET_TIME_Absolute now;
1581 struct GNUNET_HELLO_Message *hello;
1582 struct GNUNET_PeerIdentity pid;
1585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1586 "HELLO validation cleanup background task running...\n");
1588 now = GNUNET_TIME_absolute_get ();
1590 pos = pending_validations;
1593 if (pos->timeout.value < now.value)
1596 pending_validations = pos->next;
1598 prev->next = pos->next;
1599 va = pos->addresses;
1600 hello = GNUNET_HELLO_create (&pos->publicKey,
1601 &list_validated_addresses, &va);
1602 GNUNET_CRYPTO_hash (&pos->publicKey,
1604 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1608 "Creating persistent `%s' message for peer `%4s' based on confirmed addresses.\n",
1609 "HELLO", GNUNET_i2s (&pid));
1611 GNUNET_PEERINFO_add_peer (cfg, sched, &pid, hello);
1612 GNUNET_free (hello);
1613 while (NULL != (va = pos->addresses))
1615 pos->addresses = va->next;
1616 GNUNET_free (va->transport_name);
1621 pos = pending_validations;
1630 /* finally, reschedule cleanup if needed; list is
1631 ordered by timeout, so we need the last element... */
1632 pos = pending_validations;
1633 while ((pos != NULL) && (pos->next != NULL))
1636 GNUNET_SCHEDULER_add_delayed (sched,
1638 GNUNET_SCHEDULER_PRIORITY_IDLE,
1639 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1640 GNUNET_TIME_absolute_get_remaining
1641 (pos->timeout), &cleanup_validation, NULL);
1645 struct CheckHelloValidatedContext
1648 * Plugin for which we are validating.
1650 struct TransportPlugin *plugin;
1653 * Hello that we are validating.
1655 struct GNUNET_HELLO_Message *hello;
1658 * Validation list being build.
1660 struct ValidationList *e;
1666 * Append the given address to the list of entries
1667 * that need to be validated.
1670 run_validation (void *cls,
1672 struct GNUNET_TIME_Absolute expiration,
1673 const void *addr, size_t addrlen)
1675 struct ValidationList *e = cls;
1676 struct TransportPlugin *tp;
1677 struct ValidationAddress *va;
1678 struct ValidationChallengeMessage *vcm;
1679 struct GNUNET_PeerIdentity id;
1681 tp = find_transport (tname);
1684 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
1685 GNUNET_ERROR_TYPE_BULK,
1687 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
1691 GNUNET_CRYPTO_hash (&e->publicKey,
1693 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1696 "Scheduling validation of address `%s' via `%s' for `%4s'\n",
1697 GNUNET_a2s(addr, addrlen),
1701 va = GNUNET_malloc (sizeof (struct ValidationAddress) +
1702 sizeof (struct ValidationChallengeMessage) + addrlen);
1703 va->next = e->addresses;
1705 vcm = (struct ValidationChallengeMessage *) &va[1];
1707 va->transport_name = GNUNET_strdup (tname);
1708 va->addr_len = addrlen;
1710 htons (sizeof (struct ValidationChallengeMessage) + addrlen);
1711 vcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
1713 htonl (sizeof (struct ValidationChallengeMessage) + addrlen -
1714 sizeof (struct GNUNET_MessageHeader));
1715 vcm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO);
1716 vcm->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1719 memcpy (&vcm[1], addr, addrlen);
1725 * Check if addresses in validated hello "h" overlap with
1726 * those in "chvc->hello" and update "chvc->hello" accordingly,
1727 * removing those addresses that have already been validated.
1730 check_hello_validated (void *cls,
1731 const struct GNUNET_PeerIdentity *peer,
1732 const struct GNUNET_HELLO_Message *h, uint32_t trust)
1734 struct CheckHelloValidatedContext *chvc = cls;
1735 struct ValidationAddress *va;
1736 struct TransportPlugin *tp;
1739 first_call = GNUNET_NO;
1740 if (chvc->e == NULL)
1742 first_call = GNUNET_YES;
1743 chvc->e = GNUNET_malloc (sizeof (struct ValidationList));
1744 GNUNET_assert (GNUNET_OK ==
1745 GNUNET_HELLO_get_key (h != NULL ? h : chvc->hello,
1746 &chvc->e->publicKey));
1748 GNUNET_TIME_relative_to_absolute (HELLO_VERIFICATION_TIMEOUT);
1749 chvc->e->next = pending_validations;
1750 pending_validations = chvc->e;
1754 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
1756 GNUNET_TIME_absolute_get (),
1757 &run_validation, chvc->e);
1759 else if (GNUNET_YES == first_call)
1761 /* no existing HELLO, all addresses are new */
1762 GNUNET_HELLO_iterate_addresses (chvc->hello,
1763 GNUNET_NO, &run_validation, chvc->e);
1766 return; /* wait for next call */
1767 /* finally, transmit validation attempts */
1768 va = chvc->e->addresses;
1772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1773 "Establishing `%s' connection to validate `%s' of `%4s' (sending our `%s')\n",
1775 "HELLO", GNUNET_i2s (&va->msg->target), "HELLO");
1777 tp = find_transport (va->transport_name);
1778 GNUNET_assert (tp != NULL);
1780 tp->api->send_to (tp->api->cls,
1783 (const struct GNUNET_MessageHeader *) our_hello,
1785 HELLO_VERIFICATION_TIMEOUT,
1786 &va->msg[1], va->addr_len))
1787 va->ok = GNUNET_SYSERR;
1790 if (chvc->e->next == NULL)
1791 GNUNET_SCHEDULER_add_delayed (sched,
1793 GNUNET_SCHEDULER_PRIORITY_IDLE,
1794 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1795 GNUNET_TIME_absolute_get_remaining
1796 (chvc->e->timeout), &cleanup_validation,
1803 * Process HELLO-message.
1805 * @param plugin transport involved, may be NULL
1806 * @param message the actual message
1807 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
1810 process_hello (struct TransportPlugin *plugin,
1811 const struct GNUNET_MessageHeader *message)
1813 struct ValidationList *e;
1815 struct GNUNET_PeerIdentity target;
1816 const struct GNUNET_HELLO_Message *hello;
1817 struct CheckHelloValidatedContext *chvc;
1818 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
1820 hsize = ntohs (message->size);
1821 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
1822 (hsize < sizeof (struct GNUNET_MessageHeader)))
1825 return GNUNET_SYSERR;
1827 /* first, check if load is too high */
1828 if (GNUNET_OS_load_cpu_get (cfg) > 100)
1830 /* TODO: call to stats? */
1833 hello = (const struct GNUNET_HELLO_Message *) message;
1834 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
1836 GNUNET_break_op (0);
1837 return GNUNET_SYSERR;
1839 GNUNET_CRYPTO_hash (&publicKey,
1840 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1841 &target.hashPubKey);
1843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1844 "Processing `%s' message for `%4s'\n",
1845 "HELLO", GNUNET_i2s (&target));
1847 /* check if a HELLO for this peer is already on the validation list */
1848 e = pending_validations;
1851 if (0 == memcmp (&e->publicKey,
1854 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
1856 /* TODO: call to stats? */
1858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1859 "`%s' message for peer `%4s' is already pending; ignoring new message\n",
1860 "HELLO", GNUNET_i2s (&target));
1866 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
1867 chvc->plugin = plugin;
1868 chvc->hello = (struct GNUNET_HELLO_Message *) &chvc[1];
1869 memcpy (chvc->hello, hello, hsize);
1870 /* finally, check if HELLO was previously validated
1871 (continuation will then schedule actual validation) */
1872 GNUNET_PEERINFO_for_all (cfg,
1876 HELLO_VERIFICATION_TIMEOUT,
1877 &check_hello_validated, chvc);
1883 * Handle PING-message. If the plugin that gave us the message is
1884 * able to queue the PONG immediately, we only queue one PONG.
1885 * Otherwise we send at most TWO PONG messages, one via an unconfirmed
1886 * transport and one via a confirmed transport. Both addresses are
1887 * selected randomly among those available.
1889 * @param plugin plugin that gave us the message
1890 * @param sender claimed sender of the PING
1891 * @param plugin_context context that might be used to send response
1892 * @param message the actual message
1895 process_ping (struct TransportPlugin *plugin,
1896 const struct GNUNET_PeerIdentity *sender,
1897 void *plugin_context,
1898 const struct GNUNET_MessageHeader *message)
1900 const struct ValidationChallengeMessage *vcm;
1901 struct ValidationChallengeResponse vcr;
1903 struct NeighbourList *n;
1906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1907 "Processing PING\n");
1909 msize = ntohs (message->size);
1910 if (msize < sizeof (struct ValidationChallengeMessage))
1912 GNUNET_break_op (0);
1915 vcm = (const struct ValidationChallengeMessage *) message;
1916 if (0 != memcmp (&vcm->target,
1917 &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1919 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1920 _("Received `%s' message not destined for me!\n"), "PING");
1921 /* TODO: call statistics */
1924 if ((ntohl (vcm->purpose.size) !=
1925 msize - sizeof (struct GNUNET_MessageHeader))
1926 || (ntohl (vcm->purpose.purpose) !=
1927 GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO))
1929 GNUNET_break_op (0);
1932 msize -= sizeof (struct ValidationChallengeMessage);
1934 plugin->api->address_suggested (plugin->api->cls, &vcm[1], msize))
1936 GNUNET_break_op (0);
1939 vcr.header.size = htons (sizeof (struct ValidationChallengeResponse));
1940 vcr.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
1941 vcr.challenge = vcm->challenge;
1942 vcr.sender = my_identity;
1943 GNUNET_assert (GNUNET_OK ==
1944 GNUNET_CRYPTO_rsa_sign (my_private_key,
1945 &vcm->purpose, &vcr.signature));
1947 GNUNET_assert (GNUNET_OK ==
1948 GNUNET_CRYPTO_rsa_verify
1949 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO, &vcm->purpose,
1950 &vcr.signature, &my_public_key));
1953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1954 "Trying to transmit PONG using inbound connection\n");
1956 n = find_neighbour (sender);
1962 transmit_to_peer (NULL, 0, &vcr.header, GNUNET_YES, n);
1967 * Handle PONG-message.
1969 * @param message the actual message
1972 process_pong (struct TransportPlugin *plugin,
1973 const struct GNUNET_MessageHeader *message)
1975 const struct ValidationChallengeResponse *vcr;
1976 struct ValidationList *pos;
1977 struct GNUNET_PeerIdentity peer;
1978 struct ValidationAddress *va;
1983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1984 "Processing PONG\n");
1986 vcr = (const struct ValidationChallengeResponse *) message;
1987 pos = pending_validations;
1990 GNUNET_CRYPTO_hash (&pos->publicKey,
1992 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1995 memcmp (&peer, &vcr->sender, sizeof (struct GNUNET_PeerIdentity)))
2001 /* TODO: call statistics (unmatched PONG) */
2002 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2004 ("Received `%s' message but have no record of a matching `%s' message. Ignoring.\n"),
2008 all_done = GNUNET_YES;
2009 matched = GNUNET_NO;
2010 va = pos->addresses;
2013 if (va->msg->challenge == vcr->challenge)
2016 GNUNET_CRYPTO_rsa_verify
2017 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO, &va->msg->purpose,
2018 &vcr->signature, &pos->publicKey))
2020 /* this could rarely happen if we used the same
2021 challenge number for the peer for two different
2022 transports / addresses, but the likelihood is
2024 GNUNET_break_op (0);
2029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2030 "Confirmed validity of peer address.\n");
2032 va->ok = GNUNET_YES;
2034 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2035 matched = GNUNET_YES;
2038 if (va->ok != GNUNET_YES)
2039 all_done = GNUNET_NO;
2042 if (GNUNET_NO == matched)
2044 /* TODO: call statistics (unmatched PONG) */
2045 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2047 ("Received `%s' message but have no record of a matching `%s' message. Ignoring.\n"),
2050 if (GNUNET_YES == all_done)
2052 pos->timeout.value = 0;
2053 GNUNET_SCHEDULER_add_delayed (sched,
2055 GNUNET_SCHEDULER_PRIORITY_IDLE,
2056 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
2057 GNUNET_TIME_UNIT_ZERO,
2058 &cleanup_validation, NULL);
2064 * The peer specified by the given neighbour has timed-out. Update
2065 * our state and do the necessary notifications. Also notifies
2066 * our clients that the neighbour is now officially gone.
2068 * @param n the neighbour list entry for the peer
2071 disconnect_neighbour (struct NeighbourList *n)
2073 struct ReadyList *rpos;
2074 struct NeighbourList *npos;
2075 struct NeighbourList *nprev;
2076 struct MessageQueue *mq;
2079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2080 "Disconnecting from neighbour\n");
2082 /* remove n from neighbours list */
2085 while ((npos != NULL) && (npos != n))
2090 GNUNET_assert (npos != NULL);
2092 neighbours = n->next;
2094 nprev->next = n->next;
2096 /* notify all clients about disconnect */
2097 notify_clients_disconnect (&n->id);
2099 /* clean up all plugins, cancel connections & pending transmissions */
2100 while (NULL != (rpos = n->plugins))
2102 n->plugins = rpos->next;
2103 GNUNET_assert (rpos->neighbour == n);
2104 rpos->plugin->api->cancel (rpos->plugin->api->cls,
2105 rpos->plugin_handle, rpos, &n->id);
2109 /* free all messages on the queue */
2110 while (NULL != (mq = n->messages))
2112 n->messages = mq->next;
2113 GNUNET_assert (mq->neighbour == n);
2117 /* finally, free n itself */
2123 * Add an entry for each of our transport plugins
2124 * (that are able to send) to the list of plugins
2125 * for this neighbour.
2127 * @param neighbour to initialize
2130 add_plugins (struct NeighbourList *neighbour)
2132 struct TransportPlugin *tp;
2133 struct ReadyList *rl;
2135 neighbour->retry_plugins_time
2136 = GNUNET_TIME_relative_to_absolute (PLUGIN_RETRY_FREQUENCY);
2140 if (tp->api->send != NULL)
2142 rl = GNUNET_malloc (sizeof (struct ReadyList));
2143 rl->next = neighbour->plugins;
2144 neighbour->plugins = rl;
2146 rl->neighbour = neighbour;
2147 rl->transmit_ready = GNUNET_YES;
2155 neighbour_timeout_task (void *cls,
2156 const struct GNUNET_SCHEDULER_TaskContext *tc)
2158 struct NeighbourList *n = cls;
2161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2162 "Neighbour has timed out!\n");
2164 n->timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
2165 disconnect_neighbour (n);
2171 * Create a fresh entry in our neighbour list for the given peer.
2172 * Will try to transmit our current HELLO to the new neighbour. Also
2173 * notifies our clients about the new "connection".
2175 * @param peer the peer for which we create the entry
2176 * @return the new neighbour list entry
2178 static struct NeighbourList *
2179 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer)
2181 struct NeighbourList *n;
2184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2185 "Setting up new neighbour `%4s', sending our HELLO to introduce ourselves\n",
2188 GNUNET_assert (our_hello != NULL);
2189 n = GNUNET_malloc (sizeof (struct NeighbourList));
2190 n->next = neighbours;
2193 n->last_quota_update = GNUNET_TIME_absolute_get ();
2195 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2196 n->quota_in = (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT + 59999) / (60 * 1000);
2198 n->hello_version_sent = our_hello_version;
2199 n->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
2201 GNUNET_SCHEDULER_PRIORITY_IDLE,
2202 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
2203 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2204 &neighbour_timeout_task, n);
2205 transmit_to_peer (NULL, 0,
2206 (const struct GNUNET_MessageHeader *) our_hello,
2208 notify_clients_connect (peer, GNUNET_TIME_UNIT_FOREVER_REL);
2214 * Function called by the plugin for each received message.
2215 * Update data volumes, possibly notify plugins about
2216 * reducing the rate at which they read from the socket
2217 * and generally forward to our receive callback.
2219 * @param plugin_context value to pass to this plugin
2220 * to respond to the given peer (use is optional,
2221 * but may speed up processing)
2222 * @param service_context value passed to the transport-service
2223 * to identify the neighbour; will be NULL on the first
2224 * call for a given peer
2225 * @param latency estimated latency for communicating with the
2227 * @param peer (claimed) identity of the other peer
2228 * @param message the message, NULL if peer was disconnected
2229 * @return the new service_context that the plugin should use
2230 * for future receive calls for messages from this
2233 static struct ReadyList *
2234 plugin_env_receive (void *cls,
2235 void *plugin_context,
2236 struct ReadyList *service_context,
2237 struct GNUNET_TIME_Relative latency,
2238 const struct GNUNET_PeerIdentity *peer,
2239 const struct GNUNET_MessageHeader *message)
2241 const struct GNUNET_MessageHeader ack = {
2242 htons (sizeof (struct GNUNET_MessageHeader)),
2243 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ACK)
2245 struct TransportPlugin *plugin = cls;
2246 struct TransportClient *cpos;
2247 struct InboundMessage *im;
2249 struct NeighbourList *n;
2251 if (service_context != NULL)
2253 n = service_context->neighbour;
2254 GNUNET_assert (n != NULL);
2258 n = find_neighbour (peer);
2261 if (message == NULL)
2262 return NULL; /* disconnect of peer already marked down */
2263 n = setup_new_neighbour (peer);
2265 service_context = n->plugins;
2266 while ((service_context != NULL) && (plugin != service_context->plugin))
2267 service_context = service_context->next;
2268 GNUNET_assert ((plugin->api->send == NULL) ||
2269 (service_context != NULL));
2271 if (message == NULL)
2273 if ((service_context != NULL) &&
2274 (service_context->plugin_handle == plugin_context))
2276 service_context->connected = GNUNET_NO;
2277 service_context->plugin_handle = NULL;
2279 /* TODO: call stats */
2283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2284 "Processing message of type `%u' received by plugin...\n",
2285 ntohs (message->type));
2287 if (service_context != NULL)
2289 if (service_context->connected == GNUNET_NO)
2291 service_context->connected = GNUNET_YES;
2292 service_context->transmit_ready = GNUNET_YES;
2293 service_context->connect_attempts++;
2295 service_context->timeout
2296 = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2297 service_context->plugin_handle = plugin_context;
2298 service_context->latency = latency;
2300 /* update traffic received amount ... */
2301 msize = ntohs (message->size);
2302 n->last_received += msize;
2303 GNUNET_SCHEDULER_cancel (sched, n->timeout_task);
2305 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2307 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_NO,
2308 GNUNET_SCHEDULER_PRIORITY_IDLE,
2309 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
2310 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2311 &neighbour_timeout_task, n);
2313 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2315 /* dropping message due to frequent inbound volume violations! */
2316 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
2317 GNUNET_ERROR_TYPE_BULK,
2319 ("Dropping incoming message due to repeated bandwidth quota violations.\n"));
2320 /* TODO: call stats */
2321 GNUNET_assert (NULL != service_context->neighbour);
2322 return service_context;
2324 switch (ntohs (message->type))
2326 case GNUNET_MESSAGE_TYPE_HELLO:
2328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2329 "Receiving `%s' message from other peer.\n", "HELLO");
2331 process_hello (plugin, message);
2333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2334 "Sending `%s' message to connecting peer.\n", "ACK");
2336 transmit_to_peer (NULL, 0, &ack, GNUNET_YES, n);
2338 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
2339 process_ping (plugin, peer, plugin_context, message);
2341 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
2342 process_pong (plugin, message);
2344 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
2345 n->saw_ack = GNUNET_YES;
2346 /* intentional fall-through! */
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2350 "Received message of type %u from other peer, sending to all clients.\n",
2351 ntohs (message->type));
2353 /* transmit message to all clients */
2354 im = GNUNET_malloc (sizeof (struct InboundMessage) + msize);
2355 im->header.size = htons (sizeof (struct InboundMessage) + msize);
2356 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
2357 im->latency = GNUNET_TIME_relative_hton (latency);
2359 memcpy (&im[1], message, msize);
2362 while (cpos != NULL)
2364 transmit_to_client (cpos, &im->header, GNUNET_YES);
2369 GNUNET_assert (NULL != service_context->neighbour);
2370 return service_context;
2375 * Handle START-message. This is the first message sent to us
2376 * by any client which causes us to add it to our list.
2378 * @param cls closure (always NULL)
2379 * @param client identification of the client
2380 * @param message the actual message
2383 handle_start (void *cls,
2384 struct GNUNET_SERVER_Client *client,
2385 const struct GNUNET_MessageHeader *message)
2387 struct TransportClient *c;
2388 struct ConnectInfoMessage cim;
2389 struct NeighbourList *n;
2390 struct InboundMessage *im;
2391 struct GNUNET_MessageHeader *ack;
2394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2395 "Received `%s' request from client\n", "START");
2400 if (c->client == client)
2402 /* client already on our list! */
2404 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2409 c = GNUNET_malloc (sizeof (struct TransportClient));
2413 if (our_hello != NULL)
2416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2417 "Sending our own HELLO to new client\n");
2419 transmit_to_client (c,
2420 (const struct GNUNET_MessageHeader *) our_hello,
2422 /* tell new client about all existing connections */
2423 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
2424 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2425 cim.quota_out = htonl (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT / (60 * 1000));
2426 cim.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO); /* FIXME? */
2427 im = GNUNET_malloc (sizeof (struct InboundMessage) +
2428 sizeof (struct GNUNET_MessageHeader));
2429 im->header.size = htons (sizeof (struct InboundMessage) +
2430 sizeof (struct GNUNET_MessageHeader));
2431 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
2432 im->latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO); /* FIXME? */
2433 ack = (struct GNUNET_MessageHeader *) &im[1];
2434 ack->size = htons (sizeof (struct GNUNET_MessageHeader));
2435 ack->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ACK);
2436 for (n = neighbours; n != NULL; n = n->next)
2439 transmit_to_client (c, &cim.header, GNUNET_NO);
2443 transmit_to_client (c, &im->header, GNUNET_NO);
2448 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2453 * Handle HELLO-message.
2455 * @param cls closure (always NULL)
2456 * @param client identification of the client
2457 * @param message the actual message
2460 handle_hello (void *cls,
2461 struct GNUNET_SERVER_Client *client,
2462 const struct GNUNET_MessageHeader *message)
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2468 "Received `%s' request from client\n", "HELLO");
2470 ret = process_hello (NULL, message);
2471 GNUNET_SERVER_receive_done (client, ret);
2476 * Handle SEND-message.
2478 * @param cls closure (always NULL)
2479 * @param client identification of the client
2480 * @param message the actual message
2483 handle_send (void *cls,
2484 struct GNUNET_SERVER_Client *client,
2485 const struct GNUNET_MessageHeader *message)
2487 struct TransportClient *tc;
2488 struct NeighbourList *n;
2489 const struct OutboundMessage *obm;
2490 const struct GNUNET_MessageHeader *obmm;
2494 size = ntohs (message->size);
2496 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
2499 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2502 obm = (const struct OutboundMessage *) message;
2504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2505 "Received `%s' request from client with target `%4s'\n",
2506 "SEND", GNUNET_i2s (&obm->peer));
2508 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
2509 msize = ntohs (obmm->size);
2510 if (size != msize + sizeof (struct OutboundMessage))
2513 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2516 n = find_neighbour (&obm->peer);
2518 n = setup_new_neighbour (&obm->peer);
2520 while ((tc != NULL) && (tc->client != client))
2524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2525 "Client asked to transmit %u-byte message of type %u to `%4s'\n",
2527 ntohs (obmm->type), GNUNET_i2s (&obm->peer));
2529 transmit_to_peer (tc, ntohl(obm->priority), obmm, GNUNET_NO, n);
2530 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2535 * Handle SET_QUOTA-message.
2537 * @param cls closure (always NULL)
2538 * @param client identification of the client
2539 * @param message the actual message
2542 handle_set_quota (void *cls,
2543 struct GNUNET_SERVER_Client *client,
2544 const struct GNUNET_MessageHeader *message)
2546 const struct QuotaSetMessage *qsm =
2547 (const struct QuotaSetMessage *) message;
2548 struct NeighbourList *n;
2549 struct TransportPlugin *p;
2550 struct ReadyList *rl;
2553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2554 "Received `%s' request from client for peer `%4s'\n",
2555 "SET_QUOTA", GNUNET_i2s (&qsm->peer));
2557 n = find_neighbour (&qsm->peer);
2560 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2564 if (n->quota_in < ntohl (qsm->quota_in))
2565 n->last_quota_update = GNUNET_TIME_absolute_get ();
2566 n->quota_in = ntohl (qsm->quota_in);
2571 p->api->set_receive_quota (p->api->cls,
2572 &qsm->peer, ntohl (qsm->quota_in));
2575 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2580 * Handle TRY_CONNECT-message.
2582 * @param cls closure (always NULL)
2583 * @param client identification of the client
2584 * @param message the actual message
2587 handle_try_connect (void *cls,
2588 struct GNUNET_SERVER_Client *client,
2589 const struct GNUNET_MessageHeader *message)
2591 const struct TryConnectMessage *tcm;
2593 tcm = (const struct TryConnectMessage *) message;
2595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2596 "Received `%s' request from client asking to connect to `%4s'\n",
2597 "TRY_CONNECT", GNUNET_i2s (&tcm->peer));
2599 if (NULL == find_neighbour (&tcm->peer))
2600 setup_new_neighbour (&tcm->peer);
2601 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2606 * List of handlers for the messages understood by this
2609 static struct GNUNET_SERVER_MessageHandler handlers[] = {
2610 {&handle_start, NULL,
2611 GNUNET_MESSAGE_TYPE_TRANSPORT_START, 0},
2612 {&handle_hello, NULL,
2613 GNUNET_MESSAGE_TYPE_HELLO, 0},
2614 {&handle_send, NULL,
2615 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
2616 {&handle_set_quota, NULL,
2617 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
2618 {&handle_try_connect, NULL,
2619 GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT,
2620 sizeof (struct TryConnectMessage)},
2626 * Setup the environment for this plugin.
2629 create_environment (struct TransportPlugin *plug)
2631 plug->env.cfg = cfg;
2632 plug->env.sched = sched;
2633 plug->env.my_public_key = &my_public_key;
2634 plug->env.cls = plug;
2635 plug->env.receive = &plugin_env_receive;
2636 plug->env.lookup = &plugin_env_lookup_address;
2637 plug->env.notify_address = &plugin_env_notify_address;
2638 plug->env.default_quota_in = (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT + 59999) / (60 * 1000);
2639 plug->env.max_connections = max_connect_per_transport;
2644 * Start the specified transport (load the plugin).
2647 start_transport (struct GNUNET_SERVER_Handle *server, const char *name)
2649 struct TransportPlugin *plug;
2652 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2653 _("Loading `%s' transport plugin\n"), name);
2654 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
2655 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
2656 create_environment (plug);
2657 plug->short_name = GNUNET_strdup (name);
2658 plug->lib_name = libname;
2659 plug->next = plugins;
2661 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
2662 if (plug->api == NULL)
2664 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2665 _("Failed to load transport plugin for `%s'\n"), name);
2666 GNUNET_free (plug->short_name);
2667 plugins = plug->next;
2668 GNUNET_free (libname);
2675 * Called whenever a client is disconnected. Frees our
2676 * resources associated with that client.
2678 * @param cls closure
2679 * @param client identification of the client
2682 client_disconnect_notification (void *cls,
2683 struct GNUNET_SERVER_Client *client)
2685 struct TransportClient *pos;
2686 struct TransportClient *prev;
2687 struct ClientMessageQueueEntry *mqe;
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2691 "Client disconnected, cleaning up.\n");
2695 while ((pos != NULL) && (pos->client != client))
2702 while (NULL != (mqe = pos->message_queue_head))
2704 pos->message_queue_head = mqe->next;
2707 pos->message_queue_head = NULL;
2709 clients = pos->next;
2711 prev->next = pos->next;
2712 if (GNUNET_YES == pos->tcs_pending)
2722 * Initiate transport service.
2724 * @param cls closure
2725 * @param s scheduler to use
2726 * @param serv the initialized server
2727 * @param c configuration to use
2731 struct GNUNET_SCHEDULER_Handle *s,
2732 struct GNUNET_SERVER_Handle *serv, struct GNUNET_CONFIGURATION_Handle *c)
2737 unsigned long long tneigh;
2742 /* parse configuration */
2744 GNUNET_CONFIGURATION_get_value_number (c,
2749 GNUNET_CONFIGURATION_get_value_filename (c,
2751 "HOSTKEY", &keyfile)))
2753 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2755 ("Transport service is lacking key configuration settings. Exiting.\n"));
2756 GNUNET_SCHEDULER_shutdown (s);
2759 max_connect_per_transport = (uint32_t) tneigh;
2760 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2761 GNUNET_free (keyfile);
2762 if (my_private_key == NULL)
2764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2766 ("Transport service could not access hostkey. Exiting.\n"));
2767 GNUNET_SCHEDULER_shutdown (s);
2770 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
2771 GNUNET_CRYPTO_hash (&my_public_key,
2772 sizeof (my_public_key), &my_identity.hashPubKey);
2773 /* setup notification */
2775 GNUNET_SERVER_disconnect_notify (server,
2776 &client_disconnect_notification, NULL);
2777 /* load plugins... */
2780 GNUNET_CONFIGURATION_get_value_string (c,
2781 "TRANSPORT", "PLUGINS", &plugs))
2783 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2784 _("Starting transport plugins `%s'\n"), plugs);
2785 pos = strtok (plugs, " ");
2788 start_transport (server, pos);
2790 pos = strtok (NULL, " ");
2792 GNUNET_free (plugs);
2797 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2798 _("Transport service ready.\n"));
2800 /* process client requests */
2801 GNUNET_SERVER_add_handlers (server, handlers);
2806 * Function called when the service shuts
2807 * down. Unloads our plugins.
2809 * @param cls closure
2810 * @param cfg configuration to use
2813 unload_plugins (void *cls, struct GNUNET_CONFIGURATION_Handle *cfg)
2815 struct TransportPlugin *plug;
2816 struct AddressList *al;
2819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2820 "Transport service is unloading plugins...\n");
2822 while (NULL != (plug = plugins))
2824 plugins = plug->next;
2825 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
2826 GNUNET_free (plug->lib_name);
2827 GNUNET_free (plug->short_name);
2828 while (NULL != (al = plug->addresses))
2830 plug->addresses = al->next;
2835 if (my_private_key != NULL)
2836 GNUNET_CRYPTO_rsa_key_free (my_private_key);
2841 * The main function for the transport service.
2843 * @param argc number of arguments from the command line
2844 * @param argv command line arguments
2845 * @return 0 ok, 1 on error
2848 main (int argc, char *const *argv)
2850 return (GNUNET_OK ==
2851 GNUNET_SERVICE_run (argc,
2854 &run, NULL, &unload_plugins, NULL)) ? 0 : 1;
2857 /* end of gnunet-service-transport.c */