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 * How often has the other peer (recently) violated the
365 * inbound traffic limit? Incremented by 10 per violation,
366 * decremented by 1 per non-violation (for each
369 unsigned int quota_violation_count;
372 * Have we seen an ACK from this neighbour in the past?
373 * (used to make up a fake ACK for clients connecting after
374 * the neighbour connected to us).
382 * Linked list of messages to be transmitted to
383 * the client. Each entry is followed by the
386 struct ClientMessageQueueEntry
389 * This is a linked list.
391 struct ClientMessageQueueEntry *next;
396 * Client connected to the transport service.
398 struct TransportClient
402 * This is a linked list.
404 struct TransportClient *next;
407 * Handle to the client.
409 struct GNUNET_SERVER_Client *client;
412 * Linked list of messages yet to be transmitted to
415 struct ClientMessageQueueEntry *message_queue_head;
418 * Tail of linked list of messages yet to be transmitted to the
421 struct ClientMessageQueueEntry *message_queue_tail;
424 * Is a call to "transmit_send_continuation" pending? If so, we
425 * must not free this struct (even if the corresponding client
426 * disconnects) and instead only remove it from the linked list and
427 * set the "client" field to NULL.
432 * Length of the list of messages pending for this client.
434 unsigned int message_count;
440 * Message used to ask a peer to validate receipt (to check an address
441 * from a HELLO). Followed by the address used. Note that the
442 * recipients response does not affirm that he has this address,
443 * only that he got the challenge message.
445 struct ValidationChallengeMessage
449 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
451 struct GNUNET_MessageHeader header;
454 * What are we signing and why?
456 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
459 * Random challenge number (in network byte order).
461 uint32_t challenge GNUNET_PACKED;
464 * Who is the intended recipient?
466 struct GNUNET_PeerIdentity target;
471 * Message used to validate a HELLO. If this was
472 * the right recipient, the response is a signature
473 * of the original validation request. The
474 * challenge is included in the confirmation to make
475 * matching of replies to requests possible.
477 struct ValidationChallengeResponse
481 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
483 struct GNUNET_MessageHeader header;
486 * Random challenge number (in network byte order).
488 uint32_t challenge GNUNET_PACKED;
491 * Who signed this message?
493 struct GNUNET_PeerIdentity sender;
498 struct GNUNET_CRYPTO_RsaSignature signature;
504 * For each HELLO, we may have to validate multiple addresses;
505 * each address gets its own request entry.
507 struct ValidationAddress
510 * This is a linked list.
512 struct ValidationAddress *next;
515 * Our challenge message. Points to after this
516 * struct, so this field should not be freed.
518 struct ValidationChallengeMessage *msg;
521 * Name of the transport.
523 char *transport_name;
526 * When should this validated address expire?
528 struct GNUNET_TIME_Absolute expiration;
531 * Length of the address we are validating.
536 * Set to GNUNET_YES if the challenge was met,
537 * GNUNET_SYSERR if we know it failed, GNUNET_NO
538 * if we are waiting on a response.
545 * Entry in linked list of all HELLOs awaiting validation.
547 struct ValidationList
551 * This is a linked list.
553 struct ValidationList *next;
556 * Linked list with one entry per address from the HELLO
557 * that needs to be validated.
559 struct ValidationAddress *addresses;
562 * The public key of the peer.
564 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
567 * When does this record time-out? (assuming the
568 * challenge goes unanswered)
570 struct GNUNET_TIME_Absolute timeout;
576 * HELLOs awaiting validation.
578 static struct ValidationList *pending_validations;
583 static struct GNUNET_HELLO_Message *our_hello;
586 * "version" of "our_hello". Used to see if a given
587 * neighbour has already been sent the latest version
588 * of our HELLO message.
590 static unsigned int our_hello_version;
595 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
600 static struct GNUNET_PeerIdentity my_identity;
605 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
610 struct GNUNET_SCHEDULER_Handle *sched;
615 struct GNUNET_CONFIGURATION_Handle *cfg;
618 * Linked list of all clients to this service.
620 static struct TransportClient *clients;
623 * All loaded plugins.
625 static struct TransportPlugin *plugins;
630 static struct GNUNET_SERVER_Handle *server;
633 * All known neighbours and their HELLOs.
635 static struct NeighbourList *neighbours;
638 * Number of neighbours we'd like to have.
640 static uint32_t max_connect_per_transport;
644 * Find an entry in the neighbour list for a particular peer.
646 * @return NULL if not found.
648 static struct NeighbourList *
649 find_neighbour (const struct GNUNET_PeerIdentity *key)
651 struct NeighbourList *head = neighbours;
652 while ((head != NULL) &&
653 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
660 * Find an entry in the transport list for a particular transport.
662 * @return NULL if not found.
664 static struct TransportPlugin *
665 find_transport (const char *short_name)
667 struct TransportPlugin *head = plugins;
668 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
675 * Update the quota values for the given neighbour now.
678 update_quota (struct NeighbourList *n)
680 struct GNUNET_TIME_Relative delta;
684 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
685 if (delta.value < MIN_QUOTA_REFRESH_TIME)
686 return; /* not enough time passed for doing quota update */
687 allowed = delta.value * n->quota_in;
688 if (n->last_received < allowed)
690 remaining = allowed - n->last_received;
692 remaining /= n->quota_in;
695 if (remaining > MAX_BANDWIDTH_CARRY)
696 remaining = MAX_BANDWIDTH_CARRY;
697 n->last_received = 0;
698 n->last_quota_update = GNUNET_TIME_absolute_get ();
699 n->last_quota_update.value -= remaining;
700 if (n->quota_violation_count > 0)
701 n->quota_violation_count--;
705 n->last_received -= allowed;
706 n->last_quota_update = GNUNET_TIME_absolute_get ();
707 if (n->last_received > allowed)
709 /* more than twice the allowed rate! */
710 n->quota_violation_count += 10;
717 * Function called to notify a client about the socket
718 * being ready to queue more data. "buf" will be
719 * NULL and "size" zero if the socket was closed for
720 * writing in the meantime.
723 * @param size number of bytes available in buf
724 * @param buf where the callee should write the message
725 * @return number of bytes written to buf
728 transmit_to_client_callback (void *cls, size_t size, void *buf)
730 struct TransportClient *client = cls;
731 struct ClientMessageQueueEntry *q;
734 const struct GNUNET_MessageHeader *msg;
735 struct GNUNET_NETWORK_TransmitHandle *th;
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
741 "Transmission to client failed, closing connection.\n");
742 /* fatal error with client, free message queue! */
743 while (NULL != (q = client->message_queue_head))
745 client->message_queue_head = q->next;
748 client->message_queue_tail = NULL;
749 client->message_count = 0;
754 while (NULL != (q = client->message_queue_head))
756 msg = (const struct GNUNET_MessageHeader *) &q[1];
757 msize = ntohs (msg->size);
758 if (msize + tsize > size)
760 client->message_queue_head = q->next;
762 client->message_queue_tail = NULL;
763 memcpy (&cbuf[tsize], msg, msize);
766 client->message_count--;
768 GNUNET_assert (tsize > 0);
771 th = GNUNET_SERVER_notify_transmit_ready (client->client,
773 GNUNET_TIME_UNIT_FOREVER_REL,
774 &transmit_to_client_callback,
776 GNUNET_assert (th != NULL);
783 * Send the specified message to the specified client. Since multiple
784 * messages may be pending for the same client at a time, this code
785 * makes sure that no message is lost.
787 * @param client client to transmit the message to
788 * @param msg the message to send
789 * @param may_drop can this message be dropped if the
790 * message queue for this client is getting far too large?
793 transmit_to_client (struct TransportClient *client,
794 const struct GNUNET_MessageHeader *msg, int may_drop)
796 struct ClientMessageQueueEntry *q;
798 struct GNUNET_NETWORK_TransmitHandle *th;
800 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
802 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
804 ("Dropping message, have %u messages pending (%u is the soft limit)\n"),
805 client->message_count, MAX_PENDING);
806 /* TODO: call to statistics... */
809 client->message_count++;
810 msize = ntohs (msg->size);
811 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
812 memcpy (&q[1], msg, msize);
813 /* append to message queue */
814 if (client->message_queue_tail == NULL)
816 client->message_queue_tail = q;
820 client->message_queue_tail->next = q;
821 client->message_queue_tail = q;
823 if (client->message_queue_head == NULL)
825 client->message_queue_head = q;
826 th = GNUNET_SERVER_notify_transmit_ready (client->client,
828 GNUNET_TIME_UNIT_FOREVER_REL,
829 &transmit_to_client_callback,
831 GNUNET_assert (th != NULL);
837 * Find alternative plugins for communication.
839 * @param neighbour for which neighbour should we try to find
843 try_alternative_plugins (struct NeighbourList *neighbour)
845 struct ReadyList *rl;
847 if ((neighbour->plugins != NULL) &&
848 (neighbour->retry_plugins_time.value >
849 GNUNET_TIME_absolute_get ().value))
850 return; /* don't try right now */
851 neighbour->retry_plugins_time
852 = GNUNET_TIME_relative_to_absolute (PLUGIN_RETRY_FREQUENCY);
854 rl = neighbour->plugins;
857 if (rl->connect_attempts > 0)
858 rl->connect_attempts--; /* amnesty */
866 * Check the ready list for the given neighbour and
867 * if a plugin is ready for transmission (and if we
868 * have a message), do so!
870 * @param neighbour target peer for which to check the plugins
872 static void try_transmission_to_peer (struct NeighbourList *neighbour);
876 * Function called by the GNUNET_TRANSPORT_TransmitFunction
877 * upon "completion" of a send request. This tells the API
878 * that it is now legal to send another message to the given
881 * @param cls closure, identifies the entry on the
882 * message queue that was transmitted and the
883 * client responsible for queueing the message
884 * @param rl identifies plugin used for the transmission for
885 * this neighbour; needs to be re-enabled for
886 * future transmissions
887 * @param target the peer receiving the message
888 * @param result GNUNET_OK on success, if the transmission
889 * failed, we should not tell the client to transmit
893 transmit_send_continuation (void *cls,
894 struct ReadyList *rl,
895 const struct GNUNET_PeerIdentity *target,
898 struct MessageQueue *mq = cls;
899 struct SendOkMessage send_ok_msg;
900 struct NeighbourList *n;
902 GNUNET_assert (mq != NULL);
904 GNUNET_assert (n != NULL);
906 memcmp (&n->id, target,
907 sizeof (struct GNUNET_PeerIdentity)));
911 while ((rl != NULL) && (rl->plugin != mq->plugin))
913 GNUNET_assert (rl != NULL);
915 if (result == GNUNET_OK)
917 rl->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 "Transmission failed, marking connection as down.\n");
923 rl->connected = GNUNET_NO;
925 if (!mq->internal_msg)
926 rl->transmit_ready = GNUNET_YES;
927 if (mq->client != NULL)
929 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
930 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
931 send_ok_msg.success = htonl (result);
932 send_ok_msg.peer = n->id;
933 transmit_to_client (mq->client, &send_ok_msg.header, GNUNET_NO);
935 GNUNET_free (mq->message);
937 /* one plugin just became ready again, try transmitting
938 another message (if available) */
939 try_transmission_to_peer (n);
946 * We could not use an existing (or validated) connection to
947 * talk to a peer. Try addresses that have not yet been
950 * @param n neighbour we want to communicate with
951 * @return plugin ready to talk, or NULL if none is available
953 static struct ReadyList *
954 try_unvalidated_addresses (struct NeighbourList *n)
956 struct ValidationList *vl;
957 struct ValidationAddress *va;
958 struct GNUNET_PeerIdentity id;
959 struct GNUNET_TIME_Absolute now;
962 struct ReadyList *rl;
963 struct TransportPlugin *plugin;
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
967 "Trying to connect to `%4s' using unvalidated addresses\n",
968 GNUNET_i2s (&n->id));
970 /* NOTE: this function needs to not only identify the
971 plugin but also setup "plugin_handle", binding it to the
972 right address using the plugin's "send_to" API */
973 now = GNUNET_TIME_absolute_get ();
974 vl = pending_validations;
977 GNUNET_CRYPTO_hash (&vl->publicKey,
979 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
981 if (0 == memcmp (&id, &n->id, sizeof (struct GNUNET_PeerIdentity)))
988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
989 "No unvalidated address found for peer `%4s'\n",
990 GNUNET_i2s (&n->id));
1000 if (va->expiration.value > now.value)
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 "All %u unvalidated addresses for peer have expired\n",
1013 total = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
1014 for (va = vl->addresses; va != NULL; va = va->next)
1016 if (va->expiration.value <= now.value)
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1025 "Trying unvalidated address of `%s' transport\n",
1026 va->transport_name);
1028 plugin = find_transport (va->transport_name);
1034 rl = GNUNET_malloc (sizeof (struct ReadyList));
1035 rl->next = n->plugins;
1037 rl->plugin = plugin;
1038 rl->plugin_handle = plugin->api->send_to (plugin->api->cls,
1043 GNUNET_TIME_UNIT_ZERO,
1044 &va->msg[1], va->addr_len);
1045 rl->transmit_ready = GNUNET_YES;
1053 * Check the ready list for the given neighbour and
1054 * if a plugin is ready for transmission (and if we
1055 * have a message), do so!
1058 try_transmission_to_peer (struct NeighbourList *neighbour)
1060 struct ReadyList *pos;
1061 struct GNUNET_TIME_Relative min_latency;
1062 struct ReadyList *rl;
1063 struct MessageQueue *mq;
1064 struct GNUNET_TIME_Absolute now;
1066 if (neighbour->messages == NULL)
1067 return; /* nothing to do */
1068 try_alternative_plugins (neighbour);
1069 min_latency = GNUNET_TIME_UNIT_FOREVER_REL;
1071 mq = neighbour->messages;
1072 now = GNUNET_TIME_absolute_get ();
1073 pos = neighbour->plugins;
1076 /* set plugins that are inactive for a long time back to disconnected */
1077 if ((pos->timeout.value < now.value) && (pos->connected == GNUNET_YES))
1080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081 "Marking long-time inactive connection to `%4s' as down.\n",
1082 GNUNET_i2s (&neighbour->id));
1084 pos->connected = GNUNET_NO;
1086 if (((GNUNET_YES == pos->transmit_ready) ||
1087 (mq->internal_msg)) &&
1088 (pos->connect_attempts < MAX_CONNECT_RETRY) &&
1089 ((rl == NULL) || (min_latency.value > pos->latency.value)))
1092 min_latency = pos->latency;
1097 rl = try_unvalidated_addresses (neighbour);
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1102 "No plugin ready to transmit message\n");
1104 return; /* nobody ready */
1106 if (GNUNET_NO == rl->connected)
1108 rl->connect_attempts++;
1109 rl->connected = GNUNET_YES;
1111 neighbour->messages = mq->next;
1112 mq->plugin = rl->plugin;
1113 if (!mq->internal_msg)
1114 rl->transmit_ready = GNUNET_NO;
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 "Giving message of type `%u' for `%4s' to plugin `%s'\n",
1118 ntohs (mq->message->type),
1119 GNUNET_i2s (&neighbour->id), rl->plugin->short_name);
1122 = rl->plugin->api->send (rl->plugin->api->cls,
1128 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1129 &transmit_send_continuation, mq);
1134 * Send the specified message to the specified peer.
1136 * @param client source of the transmission request (can be NULL)
1137 * @param priority how important is the message
1138 * @param msg message to send
1139 * @param is_internal is this an internal message
1140 * @param neighbour handle to the neighbour for transmission
1143 transmit_to_peer (struct TransportClient *client,
1144 unsigned int priority,
1145 const struct GNUNET_MessageHeader *msg,
1146 int is_internal, struct NeighbourList *neighbour)
1148 struct MessageQueue *mq;
1149 struct MessageQueue *mqe;
1150 struct GNUNET_MessageHeader *m;
1153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1154 _("Sending message of type %u to peer `%4s'\n"),
1155 ntohs (msg->type), GNUNET_i2s (&neighbour->id));
1159 /* check for duplicate submission */
1160 mq = neighbour->messages;
1163 if (mq->client == client)
1165 /* client transmitted to same peer twice
1166 before getting SendOk! */
1173 mq = GNUNET_malloc (sizeof (struct MessageQueue));
1174 mq->client = client;
1175 m = GNUNET_malloc (ntohs (msg->size));
1176 memcpy (m, msg, ntohs (msg->size));
1178 mq->neighbour = neighbour;
1179 mq->internal_msg = is_internal;
1180 mq->priority = priority;
1183 mqe = neighbour->messages;
1185 while (mqe->next != NULL)
1190 neighbour->messages = mq;
1191 try_transmission_to_peer (neighbour);
1201 struct GeneratorContext
1203 struct TransportPlugin *plug_pos;
1204 struct AddressList *addr_pos;
1205 struct GNUNET_TIME_Absolute expiration;
1210 address_generator (void *cls, size_t max, void *buf)
1212 struct GeneratorContext *gc = cls;
1215 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
1217 gc->plug_pos = gc->plug_pos->next;
1218 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
1220 if (NULL == gc->plug_pos)
1222 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
1225 gc->addr_pos->addrlen, buf, max);
1226 gc->addr_pos = gc->addr_pos->next;
1232 * Construct our HELLO message from all of the addresses of
1233 * all of the transports.
1238 struct GNUNET_HELLO_Message *hello;
1239 struct TransportClient *cpos;
1240 struct NeighbourList *npos;
1241 struct GeneratorContext gc;
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1245 "Refreshing my HELLO\n");
1247 gc.plug_pos = plugins;
1248 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
1249 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1250 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
1252 while (cpos != NULL)
1254 transmit_to_client (cpos,
1255 (const struct GNUNET_MessageHeader *) hello,
1260 GNUNET_free_non_null (our_hello);
1262 our_hello_version++;
1264 while (npos != NULL)
1266 transmit_to_peer (NULL, 0,
1267 (const struct GNUNET_MessageHeader *) our_hello,
1275 * Task used to clean up expired addresses for a plugin.
1277 * @param cls closure
1281 expire_address_task (void *cls,
1282 const struct GNUNET_SCHEDULER_TaskContext *tc);
1286 * Update the list of addresses for this plugin,
1287 * expiring those that are past their expiration date.
1289 * @param plugin addresses of which plugin should be recomputed?
1290 * @param fresh set to GNUNET_YES if a new address was added
1291 * and we need to regenerate the HELLO even if nobody
1295 update_addresses (struct TransportPlugin *plugin, int fresh)
1297 struct GNUNET_TIME_Relative min_remaining;
1298 struct GNUNET_TIME_Relative remaining;
1299 struct GNUNET_TIME_Absolute now;
1300 struct AddressList *pos;
1301 struct AddressList *prev;
1302 struct AddressList *next;
1305 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1306 GNUNET_SCHEDULER_cancel (plugin->env.sched, plugin->address_update_task);
1307 plugin->address_update_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1308 now = GNUNET_TIME_absolute_get ();
1309 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
1310 expired = GNUNET_NO;
1312 pos = plugin->addresses;
1316 if (pos->expires.value < now.value)
1318 expired = GNUNET_YES;
1320 plugin->addresses = pos->next;
1322 prev->next = pos->next;
1327 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
1328 if (remaining.value < min_remaining.value)
1329 min_remaining = remaining;
1335 if (expired || fresh)
1337 if (min_remaining.value < GNUNET_TIME_UNIT_FOREVER_REL.value)
1338 plugin->address_update_task
1339 = GNUNET_SCHEDULER_add_delayed (plugin->env.sched,
1341 GNUNET_SCHEDULER_PRIORITY_IDLE,
1342 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1344 &expire_address_task, plugin);
1350 * Task used to clean up expired addresses for a plugin.
1352 * @param cls closure
1356 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1358 struct TransportPlugin *plugin = cls;
1359 plugin->address_update_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1360 update_addresses (plugin, GNUNET_NO);
1365 * Function that must be called by each plugin to notify the
1366 * transport service about the addresses under which the transport
1367 * provided by the plugin can be reached.
1369 * @param cls closure
1370 * @param name name of the transport that generated the address
1371 * @param addr one of the addresses of the host, NULL for the last address
1372 * the specific address format depends on the transport
1373 * @param addrlen length of the address
1374 * @param expires when should this address automatically expire?
1377 plugin_env_notify_address (void *cls,
1381 struct GNUNET_TIME_Relative expires)
1383 struct TransportPlugin *p = cls;
1384 struct AddressList *al;
1385 struct GNUNET_TIME_Absolute abex;
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1389 "Plugin `%s' informs us about a new address `%s'\n", name,
1390 GNUNET_a2s(addr, addrlen));
1392 abex = GNUNET_TIME_relative_to_absolute (expires);
1393 GNUNET_assert (p == find_transport (name));
1398 if ((addrlen == al->addrlen) && (0 == memcmp (addr, &al[1], addrlen)))
1400 if (al->expires.value < abex.value)
1406 al = GNUNET_malloc (sizeof (struct AddressList) + addrlen);
1408 al->next = p->addresses;
1411 al->addrlen = addrlen;
1412 memcpy (&al[1], addr, addrlen);
1413 update_addresses (p, GNUNET_YES);
1417 struct LookupHelloContext
1419 GNUNET_TRANSPORT_AddressCallback iterator;
1426 lookup_address_callback (void *cls,
1428 struct GNUNET_TIME_Absolute expiration,
1429 const void *addr, size_t addrlen)
1431 struct LookupHelloContext *lhc = cls;
1432 lhc->iterator (lhc->iterator_cls, tname, addr, addrlen);
1438 lookup_hello_callback (void *cls,
1439 const struct GNUNET_PeerIdentity *peer,
1440 const struct GNUNET_HELLO_Message *h, uint32_t trust)
1442 struct LookupHelloContext *lhc = cls;
1446 lhc->iterator (lhc->iterator_cls, NULL, NULL, 0);
1452 GNUNET_HELLO_iterate_addresses (h,
1453 GNUNET_NO, &lookup_address_callback, lhc);
1458 * Function that allows a transport to query the known
1459 * network addresses for a given peer.
1461 * @param cls closure
1462 * @param timeout after how long should we time out?
1463 * @param target which peer are we looking for?
1464 * @param iter function to call for each known address
1465 * @param iter_cls closure for iter
1468 plugin_env_lookup_address (void *cls,
1469 struct GNUNET_TIME_Relative timeout,
1470 const struct GNUNET_PeerIdentity *target,
1471 GNUNET_TRANSPORT_AddressCallback iter,
1474 struct LookupHelloContext *lhc;
1476 lhc = GNUNET_malloc (sizeof (struct LookupHelloContext));
1477 lhc->iterator = iter;
1478 lhc->iterator_cls = iter_cls;
1479 GNUNET_PEERINFO_for_all (cfg,
1481 target, 0, timeout, &lookup_hello_callback, &lhc);
1486 * Notify all of our clients about a peer connecting.
1489 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
1490 struct GNUNET_TIME_Relative latency)
1492 struct ConnectInfoMessage cim;
1493 struct TransportClient *cpos;
1496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1497 "Informing clients about peer `%4s' connecting to us\n",
1500 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
1501 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
1502 cim.quota_out = htonl (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT / (60*1000));
1503 cim.latency = GNUNET_TIME_relative_hton (latency);
1504 memcpy (&cim.id, peer, sizeof (struct GNUNET_PeerIdentity));
1506 while (cpos != NULL)
1508 transmit_to_client (cpos, &cim.header, GNUNET_NO);
1515 * Notify all of our clients about a peer disconnecting.
1518 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
1520 struct DisconnectInfoMessage dim;
1521 struct TransportClient *cpos;
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1525 "Informing clients about peer `%4s' disconnecting\n",
1528 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
1529 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1530 dim.reserved = htonl (0);
1531 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
1533 while (cpos != NULL)
1535 transmit_to_client (cpos, &dim.header, GNUNET_NO);
1542 * Copy any validated addresses to buf.
1544 * @return 0 once all addresses have been
1548 list_validated_addresses (void *cls, size_t max, void *buf)
1550 struct ValidationAddress **va = cls;
1553 while ((NULL != *va) && ((*va)->ok != GNUNET_YES))
1557 ret = GNUNET_HELLO_add_address ((*va)->transport_name,
1559 &(*va)->msg[1], (*va)->addr_len, buf, max);
1566 * HELLO validation cleanup task.
1569 cleanup_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1571 struct ValidationAddress *va;
1572 struct ValidationList *pos;
1573 struct ValidationList *prev;
1574 struct GNUNET_TIME_Absolute now;
1575 struct GNUNET_HELLO_Message *hello;
1576 struct GNUNET_PeerIdentity pid;
1579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1580 "HELLO validation cleanup background task running...\n");
1582 now = GNUNET_TIME_absolute_get ();
1584 pos = pending_validations;
1587 if (pos->timeout.value < now.value)
1590 pending_validations = pos->next;
1592 prev->next = pos->next;
1593 va = pos->addresses;
1594 hello = GNUNET_HELLO_create (&pos->publicKey,
1595 &list_validated_addresses, &va);
1596 GNUNET_CRYPTO_hash (&pos->publicKey,
1598 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1602 "Creating persistent `%s' message for peer `%4s' based on confirmed addresses.\n",
1603 "HELLO", GNUNET_i2s (&pid));
1605 GNUNET_PEERINFO_add_peer (cfg, sched, &pid, hello);
1606 GNUNET_free (hello);
1607 while (NULL != (va = pos->addresses))
1609 pos->addresses = va->next;
1610 GNUNET_free (va->transport_name);
1615 pos = pending_validations;
1624 /* finally, reschedule cleanup if needed; list is
1625 ordered by timeout, so we need the last element... */
1626 pos = pending_validations;
1627 while ((pos != NULL) && (pos->next != NULL))
1630 GNUNET_SCHEDULER_add_delayed (sched,
1632 GNUNET_SCHEDULER_PRIORITY_IDLE,
1633 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1634 GNUNET_TIME_absolute_get_remaining
1635 (pos->timeout), &cleanup_validation, NULL);
1639 struct CheckHelloValidatedContext
1642 * Plugin for which we are validating.
1644 struct TransportPlugin *plugin;
1647 * Hello that we are validating.
1649 struct GNUNET_HELLO_Message *hello;
1652 * Validation list being build.
1654 struct ValidationList *e;
1660 * Append the given address to the list of entries
1661 * that need to be validated.
1664 run_validation (void *cls,
1666 struct GNUNET_TIME_Absolute expiration,
1667 const void *addr, size_t addrlen)
1669 struct ValidationList *e = cls;
1670 struct TransportPlugin *tp;
1671 struct ValidationAddress *va;
1672 struct ValidationChallengeMessage *vcm;
1673 struct GNUNET_PeerIdentity id;
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 GNUNET_CRYPTO_hash (&e->publicKey,
1687 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1690 "Scheduling validation of address `%s' via `%s' for `%4s'\n",
1691 GNUNET_a2s(addr, addrlen),
1695 va = GNUNET_malloc (sizeof (struct ValidationAddress) +
1696 sizeof (struct ValidationChallengeMessage) + addrlen);
1697 va->next = e->addresses;
1699 vcm = (struct ValidationChallengeMessage *) &va[1];
1701 va->transport_name = GNUNET_strdup (tname);
1702 va->addr_len = addrlen;
1704 htons (sizeof (struct ValidationChallengeMessage) + addrlen);
1705 vcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
1707 htonl (sizeof (struct ValidationChallengeMessage) + addrlen -
1708 sizeof (struct GNUNET_MessageHeader));
1709 vcm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO);
1710 vcm->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1713 memcpy (&vcm[1], addr, addrlen);
1719 * Check if addresses in validated hello "h" overlap with
1720 * those in "chvc->hello" and update "chvc->hello" accordingly,
1721 * removing those addresses that have already been validated.
1724 check_hello_validated (void *cls,
1725 const struct GNUNET_PeerIdentity *peer,
1726 const struct GNUNET_HELLO_Message *h, uint32_t trust)
1728 struct CheckHelloValidatedContext *chvc = cls;
1729 struct ValidationAddress *va;
1730 struct TransportPlugin *tp;
1733 first_call = GNUNET_NO;
1734 if (chvc->e == NULL)
1736 first_call = GNUNET_YES;
1737 chvc->e = GNUNET_malloc (sizeof (struct ValidationList));
1738 GNUNET_assert (GNUNET_OK ==
1739 GNUNET_HELLO_get_key (h != NULL ? h : chvc->hello,
1740 &chvc->e->publicKey));
1742 GNUNET_TIME_relative_to_absolute (HELLO_VERIFICATION_TIMEOUT);
1743 chvc->e->next = pending_validations;
1744 pending_validations = chvc->e;
1748 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
1750 GNUNET_TIME_absolute_get (),
1751 &run_validation, chvc->e);
1753 else if (GNUNET_YES == first_call)
1755 /* no existing HELLO, all addresses are new */
1756 GNUNET_HELLO_iterate_addresses (chvc->hello,
1757 GNUNET_NO, &run_validation, chvc->e);
1760 return; /* wait for next call */
1761 /* finally, transmit validation attempts */
1762 va = chvc->e->addresses;
1766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1767 "Establishing `%s' connection to validate `%s' of `%4s' (sending our `%s')\n",
1769 "HELLO", GNUNET_i2s (&va->msg->target), "HELLO");
1771 tp = find_transport (va->transport_name);
1772 GNUNET_assert (tp != NULL);
1774 tp->api->send_to (tp->api->cls,
1777 (const struct GNUNET_MessageHeader *) our_hello,
1779 HELLO_VERIFICATION_TIMEOUT,
1780 &va->msg[1], va->addr_len))
1781 va->ok = GNUNET_SYSERR;
1784 if (chvc->e->next == NULL)
1785 GNUNET_SCHEDULER_add_delayed (sched,
1787 GNUNET_SCHEDULER_PRIORITY_IDLE,
1788 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1789 GNUNET_TIME_absolute_get_remaining
1790 (chvc->e->timeout), &cleanup_validation,
1797 * Process HELLO-message.
1799 * @param plugin transport involved, may be NULL
1800 * @param message the actual message
1801 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
1804 process_hello (struct TransportPlugin *plugin,
1805 const struct GNUNET_MessageHeader *message)
1807 struct ValidationList *e;
1809 struct GNUNET_PeerIdentity target;
1810 const struct GNUNET_HELLO_Message *hello;
1811 struct CheckHelloValidatedContext *chvc;
1812 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
1814 hsize = ntohs (message->size);
1815 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
1816 (hsize < sizeof (struct GNUNET_MessageHeader)))
1819 return GNUNET_SYSERR;
1821 /* first, check if load is too high */
1822 if (GNUNET_OS_load_cpu_get (cfg) > 100)
1824 /* TODO: call to stats? */
1827 hello = (const struct GNUNET_HELLO_Message *) message;
1828 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
1830 GNUNET_break_op (0);
1831 return GNUNET_SYSERR;
1833 GNUNET_CRYPTO_hash (&publicKey,
1834 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1835 &target.hashPubKey);
1837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1838 "Processing `%s' message for `%4s'\n",
1839 "HELLO", GNUNET_i2s (&target));
1841 /* check if a HELLO for this peer is already on the validation list */
1842 e = pending_validations;
1845 if (0 == memcmp (&e->publicKey,
1848 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
1850 /* TODO: call to stats? */
1852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1853 "`%s' message for peer `%4s' is already pending; ignoring new message\n",
1854 "HELLO", GNUNET_i2s (&target));
1860 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
1861 chvc->plugin = plugin;
1862 chvc->hello = (struct GNUNET_HELLO_Message *) &chvc[1];
1863 memcpy (chvc->hello, hello, hsize);
1864 /* finally, check if HELLO was previously validated
1865 (continuation will then schedule actual validation) */
1866 GNUNET_PEERINFO_for_all (cfg,
1870 HELLO_VERIFICATION_TIMEOUT,
1871 &check_hello_validated, chvc);
1877 * Handle PING-message. If the plugin that gave us the message is
1878 * able to queue the PONG immediately, we only queue one PONG.
1879 * Otherwise we send at most TWO PONG messages, one via an unconfirmed
1880 * transport and one via a confirmed transport. Both addresses are
1881 * selected randomly among those available.
1883 * @param plugin plugin that gave us the message
1884 * @param sender claimed sender of the PING
1885 * @param plugin_context context that might be used to send response
1886 * @param message the actual message
1889 process_ping (struct TransportPlugin *plugin,
1890 const struct GNUNET_PeerIdentity *sender,
1891 void *plugin_context,
1892 const struct GNUNET_MessageHeader *message)
1894 const struct ValidationChallengeMessage *vcm;
1895 struct ValidationChallengeResponse vcr;
1897 struct NeighbourList *n;
1900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1901 "Processing PING\n");
1903 msize = ntohs (message->size);
1904 if (msize < sizeof (struct ValidationChallengeMessage))
1906 GNUNET_break_op (0);
1909 vcm = (const struct ValidationChallengeMessage *) message;
1910 if (0 != memcmp (&vcm->target,
1911 &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1913 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1914 _("Received `%s' message not destined for me!\n"), "PING");
1915 /* TODO: call statistics */
1918 if ((ntohl (vcm->purpose.size) !=
1919 msize - sizeof (struct GNUNET_MessageHeader))
1920 || (ntohl (vcm->purpose.purpose) !=
1921 GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO))
1923 GNUNET_break_op (0);
1926 msize -= sizeof (struct ValidationChallengeMessage);
1928 plugin->api->address_suggested (plugin->api->cls, &vcm[1], msize))
1930 GNUNET_break_op (0);
1933 vcr.header.size = htons (sizeof (struct ValidationChallengeResponse));
1934 vcr.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
1935 vcr.challenge = vcm->challenge;
1936 vcr.sender = my_identity;
1937 GNUNET_assert (GNUNET_OK ==
1938 GNUNET_CRYPTO_rsa_sign (my_private_key,
1939 &vcm->purpose, &vcr.signature));
1941 GNUNET_assert (GNUNET_OK ==
1942 GNUNET_CRYPTO_rsa_verify
1943 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO, &vcm->purpose,
1944 &vcr.signature, &my_public_key));
1947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1948 "Trying to transmit PONG using inbound connection\n");
1950 n = find_neighbour (sender);
1956 transmit_to_peer (NULL, 0, &vcr.header, GNUNET_YES, n);
1961 * Handle PONG-message.
1963 * @param message the actual message
1966 process_pong (struct TransportPlugin *plugin,
1967 const struct GNUNET_MessageHeader *message)
1969 const struct ValidationChallengeResponse *vcr;
1970 struct ValidationList *pos;
1971 struct GNUNET_PeerIdentity peer;
1972 struct ValidationAddress *va;
1977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1978 "Processing PONG\n");
1980 vcr = (const struct ValidationChallengeResponse *) message;
1981 pos = pending_validations;
1984 GNUNET_CRYPTO_hash (&pos->publicKey,
1986 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1989 memcmp (&peer, &vcr->sender, sizeof (struct GNUNET_PeerIdentity)))
1995 /* TODO: call statistics (unmatched PONG) */
1996 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1998 ("Received `%s' message but have no record of a matching `%s' message. Ignoring.\n"),
2002 all_done = GNUNET_YES;
2003 matched = GNUNET_NO;
2004 va = pos->addresses;
2007 if (va->msg->challenge == vcr->challenge)
2010 GNUNET_CRYPTO_rsa_verify
2011 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO, &va->msg->purpose,
2012 &vcr->signature, &pos->publicKey))
2014 /* this could rarely happen if we used the same
2015 challenge number for the peer for two different
2016 transports / addresses, but the likelihood is
2018 GNUNET_break_op (0);
2023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2024 "Confirmed validity of peer address.\n");
2026 va->ok = GNUNET_YES;
2028 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2029 matched = GNUNET_YES;
2032 if (va->ok != GNUNET_YES)
2033 all_done = GNUNET_NO;
2036 if (GNUNET_NO == matched)
2038 /* TODO: call statistics (unmatched PONG) */
2039 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2041 ("Received `%s' message but have no record of a matching `%s' message. Ignoring.\n"),
2044 if (GNUNET_YES == all_done)
2046 pos->timeout.value = 0;
2047 GNUNET_SCHEDULER_add_delayed (sched,
2049 GNUNET_SCHEDULER_PRIORITY_IDLE,
2050 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
2051 GNUNET_TIME_UNIT_ZERO,
2052 &cleanup_validation, NULL);
2058 * The peer specified by the given neighbour has timed-out. Update
2059 * our state and do the necessary notifications. Also notifies
2060 * our clients that the neighbour is now officially gone.
2062 * @param n the neighbour list entry for the peer
2065 disconnect_neighbour (struct NeighbourList *n)
2067 struct ReadyList *rpos;
2068 struct NeighbourList *npos;
2069 struct NeighbourList *nprev;
2070 struct MessageQueue *mq;
2073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2074 "Disconnecting from neighbour\n");
2076 /* remove n from neighbours list */
2079 while ((npos != NULL) && (npos != n))
2084 GNUNET_assert (npos != NULL);
2086 neighbours = n->next;
2088 nprev->next = n->next;
2090 /* notify all clients about disconnect */
2091 notify_clients_disconnect (&n->id);
2093 /* clean up all plugins, cancel connections & pending transmissions */
2094 while (NULL != (rpos = n->plugins))
2096 n->plugins = rpos->next;
2097 GNUNET_assert (rpos->neighbour == n);
2098 rpos->plugin->api->cancel (rpos->plugin->api->cls,
2099 rpos->plugin_handle, rpos, &n->id);
2103 /* free all messages on the queue */
2104 while (NULL != (mq = n->messages))
2106 n->messages = mq->next;
2107 GNUNET_assert (mq->neighbour == n);
2111 /* finally, free n itself */
2117 * Add an entry for each of our transport plugins
2118 * (that are able to send) to the list of plugins
2119 * for this neighbour.
2121 * @param neighbour to initialize
2124 add_plugins (struct NeighbourList *neighbour)
2126 struct TransportPlugin *tp;
2127 struct ReadyList *rl;
2129 neighbour->retry_plugins_time
2130 = GNUNET_TIME_relative_to_absolute (PLUGIN_RETRY_FREQUENCY);
2134 if (tp->api->send != NULL)
2136 rl = GNUNET_malloc (sizeof (struct ReadyList));
2137 rl->next = neighbour->plugins;
2138 neighbour->plugins = rl;
2140 rl->neighbour = neighbour;
2141 rl->transmit_ready = GNUNET_YES;
2149 neighbour_timeout_task (void *cls,
2150 const struct GNUNET_SCHEDULER_TaskContext *tc)
2152 struct NeighbourList *n = cls;
2155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2156 "Neighbour has timed out!\n");
2158 n->timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
2159 disconnect_neighbour (n);
2165 * Create a fresh entry in our neighbour list for the given peer.
2166 * Will try to transmit our current HELLO to the new neighbour. Also
2167 * notifies our clients about the new "connection".
2169 * @param peer the peer for which we create the entry
2170 * @return the new neighbour list entry
2172 static struct NeighbourList *
2173 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer)
2175 struct NeighbourList *n;
2178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2179 "Setting up new neighbour `%4s', sending our HELLO to introduce ourselves\n",
2182 GNUNET_assert (our_hello != NULL);
2183 n = GNUNET_malloc (sizeof (struct NeighbourList));
2184 n->next = neighbours;
2187 n->last_quota_update = GNUNET_TIME_absolute_get ();
2189 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2190 n->quota_in = (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT + 59999) / (60 * 1000);
2192 n->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
2194 GNUNET_SCHEDULER_PRIORITY_IDLE,
2195 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
2196 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2197 &neighbour_timeout_task, n);
2198 transmit_to_peer (NULL, 0,
2199 (const struct GNUNET_MessageHeader *) our_hello,
2201 notify_clients_connect (peer, GNUNET_TIME_UNIT_FOREVER_REL);
2207 * Function called by the plugin for each received message.
2208 * Update data volumes, possibly notify plugins about
2209 * reducing the rate at which they read from the socket
2210 * and generally forward to our receive callback.
2212 * @param plugin_context value to pass to this plugin
2213 * to respond to the given peer (use is optional,
2214 * but may speed up processing)
2215 * @param service_context value passed to the transport-service
2216 * to identify the neighbour; will be NULL on the first
2217 * call for a given peer
2218 * @param latency estimated latency for communicating with the
2220 * @param peer (claimed) identity of the other peer
2221 * @param message the message, NULL if peer was disconnected
2222 * @return the new service_context that the plugin should use
2223 * for future receive calls for messages from this
2226 static struct ReadyList *
2227 plugin_env_receive (void *cls,
2228 void *plugin_context,
2229 struct ReadyList *service_context,
2230 struct GNUNET_TIME_Relative latency,
2231 const struct GNUNET_PeerIdentity *peer,
2232 const struct GNUNET_MessageHeader *message)
2234 const struct GNUNET_MessageHeader ack = {
2235 htons (sizeof (struct GNUNET_MessageHeader)),
2236 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ACK)
2238 struct TransportPlugin *plugin = cls;
2239 struct TransportClient *cpos;
2240 struct InboundMessage *im;
2242 struct NeighbourList *n;
2244 if (service_context != NULL)
2246 n = service_context->neighbour;
2247 GNUNET_assert (n != NULL);
2251 n = find_neighbour (peer);
2254 if (message == NULL)
2255 return NULL; /* disconnect of peer already marked down */
2256 n = setup_new_neighbour (peer);
2258 service_context = n->plugins;
2259 while ((service_context != NULL) && (plugin != service_context->plugin))
2260 service_context = service_context->next;
2261 GNUNET_assert ((plugin->api->send == NULL) ||
2262 (service_context != NULL));
2264 if (message == NULL)
2266 if ((service_context != NULL) &&
2267 (service_context->plugin_handle == plugin_context))
2269 service_context->connected = GNUNET_NO;
2270 service_context->plugin_handle = NULL;
2272 /* TODO: call stats */
2276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2277 "Processing message of type `%u' received by plugin...\n",
2278 ntohs (message->type));
2280 if (service_context != NULL)
2282 if (service_context->connected == GNUNET_NO)
2284 service_context->connected = GNUNET_YES;
2285 service_context->transmit_ready = GNUNET_YES;
2286 service_context->connect_attempts++;
2288 service_context->timeout
2289 = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2290 service_context->plugin_handle = plugin_context;
2291 service_context->latency = latency;
2293 /* update traffic received amount ... */
2294 msize = ntohs (message->size);
2295 n->last_received += msize;
2296 GNUNET_SCHEDULER_cancel (sched, n->timeout_task);
2298 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2300 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_NO,
2301 GNUNET_SCHEDULER_PRIORITY_IDLE,
2302 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
2303 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2304 &neighbour_timeout_task, n);
2306 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2308 /* dropping message due to frequent inbound volume violations! */
2309 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
2310 GNUNET_ERROR_TYPE_BULK,
2312 ("Dropping incoming message due to repeated bandwidth quota violations.\n"));
2313 /* TODO: call stats */
2314 GNUNET_assert (NULL != service_context->neighbour);
2315 return service_context;
2317 switch (ntohs (message->type))
2319 case GNUNET_MESSAGE_TYPE_HELLO:
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2322 "Receiving `%s' message from other peer.\n", "HELLO");
2324 process_hello (plugin, message);
2326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2327 "Sending `%s' message to connecting peer.\n", "ACK");
2329 transmit_to_peer (NULL, 0, &ack, GNUNET_YES, n);
2331 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
2332 process_ping (plugin, peer, plugin_context, message);
2334 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
2335 process_pong (plugin, message);
2337 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
2338 n->saw_ack = GNUNET_YES;
2339 /* intentional fall-through! */
2342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2343 "Received message of type %u from other peer, sending to all clients.\n",
2344 ntohs (message->type));
2346 /* transmit message to all clients */
2347 im = GNUNET_malloc (sizeof (struct InboundMessage) + msize);
2348 im->header.size = htons (sizeof (struct InboundMessage) + msize);
2349 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
2350 im->latency = GNUNET_TIME_relative_hton (latency);
2352 memcpy (&im[1], message, msize);
2355 while (cpos != NULL)
2357 transmit_to_client (cpos, &im->header, GNUNET_YES);
2362 GNUNET_assert (NULL != service_context->neighbour);
2363 return service_context;
2368 * Handle START-message. This is the first message sent to us
2369 * by any client which causes us to add it to our list.
2371 * @param cls closure (always NULL)
2372 * @param client identification of the client
2373 * @param message the actual message
2376 handle_start (void *cls,
2377 struct GNUNET_SERVER_Client *client,
2378 const struct GNUNET_MessageHeader *message)
2380 struct TransportClient *c;
2381 struct ConnectInfoMessage cim;
2382 struct NeighbourList *n;
2383 struct InboundMessage *im;
2384 struct GNUNET_MessageHeader *ack;
2387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2388 "Received `%s' request from client\n", "START");
2393 if (c->client == client)
2395 /* client already on our list! */
2397 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2402 c = GNUNET_malloc (sizeof (struct TransportClient));
2406 if (our_hello != NULL)
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410 "Sending our own HELLO to new client\n");
2412 transmit_to_client (c,
2413 (const struct GNUNET_MessageHeader *) our_hello,
2415 /* tell new client about all existing connections */
2416 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
2417 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2418 cim.quota_out = htonl (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT / (60 * 1000));
2419 cim.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO); /* FIXME? */
2420 im = GNUNET_malloc (sizeof (struct InboundMessage) +
2421 sizeof (struct GNUNET_MessageHeader));
2422 im->header.size = htons (sizeof (struct InboundMessage) +
2423 sizeof (struct GNUNET_MessageHeader));
2424 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
2425 im->latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO); /* FIXME? */
2426 ack = (struct GNUNET_MessageHeader *) &im[1];
2427 ack->size = htons (sizeof (struct GNUNET_MessageHeader));
2428 ack->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ACK);
2429 for (n = neighbours; n != NULL; n = n->next)
2432 transmit_to_client (c, &cim.header, GNUNET_NO);
2436 transmit_to_client (c, &im->header, GNUNET_NO);
2441 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2446 * Handle HELLO-message.
2448 * @param cls closure (always NULL)
2449 * @param client identification of the client
2450 * @param message the actual message
2453 handle_hello (void *cls,
2454 struct GNUNET_SERVER_Client *client,
2455 const struct GNUNET_MessageHeader *message)
2460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2461 "Received `%s' request from client\n", "HELLO");
2463 ret = process_hello (NULL, message);
2464 GNUNET_SERVER_receive_done (client, ret);
2469 * Handle SEND-message.
2471 * @param cls closure (always NULL)
2472 * @param client identification of the client
2473 * @param message the actual message
2476 handle_send (void *cls,
2477 struct GNUNET_SERVER_Client *client,
2478 const struct GNUNET_MessageHeader *message)
2480 struct TransportClient *tc;
2481 struct NeighbourList *n;
2482 const struct OutboundMessage *obm;
2483 const struct GNUNET_MessageHeader *obmm;
2487 size = ntohs (message->size);
2489 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
2492 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2495 obm = (const struct OutboundMessage *) message;
2497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2498 "Received `%s' request from client with target `%4s'\n",
2499 "SEND", GNUNET_i2s (&obm->peer));
2501 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
2502 msize = ntohs (obmm->size);
2503 if (size != msize + sizeof (struct OutboundMessage))
2506 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2509 n = find_neighbour (&obm->peer);
2511 n = setup_new_neighbour (&obm->peer);
2513 while ((tc != NULL) && (tc->client != client))
2517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2518 "Client asked to transmit %u-byte message of type %u to `%4s'\n",
2520 ntohs (obmm->type), GNUNET_i2s (&obm->peer));
2522 transmit_to_peer (tc, ntohl(obm->priority), obmm, GNUNET_NO, n);
2523 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2528 * Handle SET_QUOTA-message.
2530 * @param cls closure (always NULL)
2531 * @param client identification of the client
2532 * @param message the actual message
2535 handle_set_quota (void *cls,
2536 struct GNUNET_SERVER_Client *client,
2537 const struct GNUNET_MessageHeader *message)
2539 const struct QuotaSetMessage *qsm =
2540 (const struct QuotaSetMessage *) message;
2541 struct NeighbourList *n;
2542 struct TransportPlugin *p;
2543 struct ReadyList *rl;
2546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2547 "Received `%s' request from client for peer `%4s'\n",
2548 "SET_QUOTA", GNUNET_i2s (&qsm->peer));
2550 n = find_neighbour (&qsm->peer);
2553 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2557 if (n->quota_in < ntohl (qsm->quota_in))
2558 n->last_quota_update = GNUNET_TIME_absolute_get ();
2559 n->quota_in = ntohl (qsm->quota_in);
2564 p->api->set_receive_quota (p->api->cls,
2565 &qsm->peer, ntohl (qsm->quota_in));
2568 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2573 * Handle TRY_CONNECT-message.
2575 * @param cls closure (always NULL)
2576 * @param client identification of the client
2577 * @param message the actual message
2580 handle_try_connect (void *cls,
2581 struct GNUNET_SERVER_Client *client,
2582 const struct GNUNET_MessageHeader *message)
2584 const struct TryConnectMessage *tcm;
2586 tcm = (const struct TryConnectMessage *) message;
2588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2589 "Received `%s' request from client asking to connect to `%4s'\n",
2590 "TRY_CONNECT", GNUNET_i2s (&tcm->peer));
2592 if (NULL == find_neighbour (&tcm->peer))
2593 setup_new_neighbour (&tcm->peer);
2594 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2599 * List of handlers for the messages understood by this
2602 static struct GNUNET_SERVER_MessageHandler handlers[] = {
2603 {&handle_start, NULL,
2604 GNUNET_MESSAGE_TYPE_TRANSPORT_START, 0},
2605 {&handle_hello, NULL,
2606 GNUNET_MESSAGE_TYPE_HELLO, 0},
2607 {&handle_send, NULL,
2608 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
2609 {&handle_set_quota, NULL,
2610 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
2611 {&handle_try_connect, NULL,
2612 GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT,
2613 sizeof (struct TryConnectMessage)},
2619 * Setup the environment for this plugin.
2622 create_environment (struct TransportPlugin *plug)
2624 plug->env.cfg = cfg;
2625 plug->env.sched = sched;
2626 plug->env.my_public_key = &my_public_key;
2627 plug->env.cls = plug;
2628 plug->env.receive = &plugin_env_receive;
2629 plug->env.lookup = &plugin_env_lookup_address;
2630 plug->env.notify_address = &plugin_env_notify_address;
2631 plug->env.default_quota_in = (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT + 59999) / (60 * 1000);
2632 plug->env.max_connections = max_connect_per_transport;
2637 * Start the specified transport (load the plugin).
2640 start_transport (struct GNUNET_SERVER_Handle *server, const char *name)
2642 struct TransportPlugin *plug;
2645 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2646 _("Loading `%s' transport plugin\n"), name);
2647 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
2648 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
2649 create_environment (plug);
2650 plug->short_name = GNUNET_strdup (name);
2651 plug->lib_name = libname;
2652 plug->next = plugins;
2654 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
2655 if (plug->api == NULL)
2657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2658 _("Failed to load transport plugin for `%s'\n"), name);
2659 GNUNET_free (plug->short_name);
2660 plugins = plug->next;
2661 GNUNET_free (libname);
2668 * Called whenever a client is disconnected. Frees our
2669 * resources associated with that client.
2671 * @param cls closure
2672 * @param client identification of the client
2675 client_disconnect_notification (void *cls,
2676 struct GNUNET_SERVER_Client *client)
2678 struct TransportClient *pos;
2679 struct TransportClient *prev;
2680 struct ClientMessageQueueEntry *mqe;
2683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2684 "Client disconnected, cleaning up.\n");
2688 while ((pos != NULL) && (pos->client != client))
2695 while (NULL != (mqe = pos->message_queue_head))
2697 pos->message_queue_head = mqe->next;
2700 pos->message_queue_head = NULL;
2702 clients = pos->next;
2704 prev->next = pos->next;
2705 if (GNUNET_YES == pos->tcs_pending)
2715 * Initiate transport service.
2717 * @param cls closure
2718 * @param s scheduler to use
2719 * @param serv the initialized server
2720 * @param c configuration to use
2724 struct GNUNET_SCHEDULER_Handle *s,
2725 struct GNUNET_SERVER_Handle *serv, struct GNUNET_CONFIGURATION_Handle *c)
2730 unsigned long long tneigh;
2735 /* parse configuration */
2737 GNUNET_CONFIGURATION_get_value_number (c,
2742 GNUNET_CONFIGURATION_get_value_filename (c,
2744 "HOSTKEY", &keyfile)))
2746 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2748 ("Transport service is lacking key configuration settings. Exiting.\n"));
2749 GNUNET_SCHEDULER_shutdown (s);
2752 max_connect_per_transport = (uint32_t) tneigh;
2753 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2754 GNUNET_free (keyfile);
2755 if (my_private_key == NULL)
2757 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2759 ("Transport service could not access hostkey. Exiting.\n"));
2760 GNUNET_SCHEDULER_shutdown (s);
2763 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
2764 GNUNET_CRYPTO_hash (&my_public_key,
2765 sizeof (my_public_key), &my_identity.hashPubKey);
2766 /* setup notification */
2768 GNUNET_SERVER_disconnect_notify (server,
2769 &client_disconnect_notification, NULL);
2770 /* load plugins... */
2773 GNUNET_CONFIGURATION_get_value_string (c,
2774 "TRANSPORT", "PLUGINS", &plugs))
2776 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2777 _("Starting transport plugins `%s'\n"), plugs);
2778 pos = strtok (plugs, " ");
2781 start_transport (server, pos);
2783 pos = strtok (NULL, " ");
2785 GNUNET_free (plugs);
2790 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2791 _("Transport service ready.\n"));
2793 /* process client requests */
2794 GNUNET_SERVER_add_handlers (server, handlers);
2799 * Function called when the service shuts
2800 * down. Unloads our plugins.
2802 * @param cls closure
2803 * @param cfg configuration to use
2806 unload_plugins (void *cls, struct GNUNET_CONFIGURATION_Handle *cfg)
2808 struct TransportPlugin *plug;
2809 struct AddressList *al;
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2813 "Transport service is unloading plugins...\n");
2815 while (NULL != (plug = plugins))
2817 plugins = plug->next;
2818 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
2819 GNUNET_free (plug->lib_name);
2820 GNUNET_free (plug->short_name);
2821 while (NULL != (al = plug->addresses))
2823 plug->addresses = al->next;
2828 if (my_private_key != NULL)
2829 GNUNET_CRYPTO_rsa_key_free (my_private_key);
2834 * The main function for the transport service.
2836 * @param argc number of arguments from the command line
2837 * @param argv command line arguments
2838 * @return 0 ok, 1 on error
2841 main (int argc, char *const *argv)
2843 return (GNUNET_OK ==
2844 GNUNET_SERVICE_run (argc,
2847 &run, NULL, &unload_plugins, NULL)) ? 0 : 1;
2850 /* end of gnunet-service-transport.c */