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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112 "Establishing fresh connection with `%4s' via plugin `%s'\n",
1113 GNUNET_i2s (&neighbour->id), rl->plugin->short_name);
1116 neighbour->messages = mq->next;
1117 mq->plugin = rl->plugin;
1118 if (!mq->internal_msg)
1119 rl->transmit_ready = GNUNET_NO;
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 "Giving message of type `%u' for `%4s' to plugin `%s'\n",
1123 ntohs (mq->message->type),
1124 GNUNET_i2s (&neighbour->id), rl->plugin->short_name);
1127 = rl->plugin->api->send (rl->plugin->api->cls,
1133 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1134 &transmit_send_continuation, mq);
1139 * Send the specified message to the specified peer.
1141 * @param client source of the transmission request (can be NULL)
1142 * @param priority how important is the message
1143 * @param msg message to send
1144 * @param is_internal is this an internal message
1145 * @param neighbour handle to the neighbour for transmission
1148 transmit_to_peer (struct TransportClient *client,
1149 unsigned int priority,
1150 const struct GNUNET_MessageHeader *msg,
1151 int is_internal, struct NeighbourList *neighbour)
1153 struct MessageQueue *mq;
1154 struct MessageQueue *mqe;
1155 struct GNUNET_MessageHeader *m;
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1159 _("Sending message of type %u to peer `%4s'\n"),
1160 ntohs (msg->type), GNUNET_i2s (&neighbour->id));
1164 /* check for duplicate submission */
1165 mq = neighbour->messages;
1168 if (mq->client == client)
1170 /* client transmitted to same peer twice
1171 before getting SendOk! */
1178 mq = GNUNET_malloc (sizeof (struct MessageQueue));
1179 mq->client = client;
1180 m = GNUNET_malloc (ntohs (msg->size));
1181 memcpy (m, msg, ntohs (msg->size));
1183 mq->neighbour = neighbour;
1184 mq->internal_msg = is_internal;
1185 mq->priority = priority;
1188 mqe = neighbour->messages;
1190 while (mqe->next != NULL)
1195 neighbour->messages = mq;
1196 try_transmission_to_peer (neighbour);
1206 struct GeneratorContext
1208 struct TransportPlugin *plug_pos;
1209 struct AddressList *addr_pos;
1210 struct GNUNET_TIME_Absolute expiration;
1215 address_generator (void *cls, size_t max, void *buf)
1217 struct GeneratorContext *gc = cls;
1220 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
1222 gc->plug_pos = gc->plug_pos->next;
1223 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
1225 if (NULL == gc->plug_pos)
1227 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
1230 gc->addr_pos->addrlen, buf, max);
1231 gc->addr_pos = gc->addr_pos->next;
1237 * Construct our HELLO message from all of the addresses of
1238 * all of the transports.
1243 struct GNUNET_HELLO_Message *hello;
1244 struct TransportClient *cpos;
1245 struct NeighbourList *npos;
1246 struct GeneratorContext gc;
1249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1250 "Refreshing my `%s'\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)
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1274 "Transmitting updated `%s' to neighbour `%4s'\n",
1276 GNUNET_i2s(&npos->id));
1278 transmit_to_peer (NULL, 0,
1279 (const struct GNUNET_MessageHeader *) our_hello,
1287 * Task used to clean up expired addresses for a plugin.
1289 * @param cls closure
1293 expire_address_task (void *cls,
1294 const struct GNUNET_SCHEDULER_TaskContext *tc);
1298 * Update the list of addresses for this plugin,
1299 * expiring those that are past their expiration date.
1301 * @param plugin addresses of which plugin should be recomputed?
1302 * @param fresh set to GNUNET_YES if a new address was added
1303 * and we need to regenerate the HELLO even if nobody
1307 update_addresses (struct TransportPlugin *plugin, int fresh)
1309 struct GNUNET_TIME_Relative min_remaining;
1310 struct GNUNET_TIME_Relative remaining;
1311 struct GNUNET_TIME_Absolute now;
1312 struct AddressList *pos;
1313 struct AddressList *prev;
1314 struct AddressList *next;
1317 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1318 GNUNET_SCHEDULER_cancel (plugin->env.sched, plugin->address_update_task);
1319 plugin->address_update_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1320 now = GNUNET_TIME_absolute_get ();
1321 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
1322 expired = GNUNET_NO;
1324 pos = plugin->addresses;
1328 if (pos->expires.value < now.value)
1330 expired = GNUNET_YES;
1332 plugin->addresses = pos->next;
1334 prev->next = pos->next;
1339 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
1340 if (remaining.value < min_remaining.value)
1341 min_remaining = remaining;
1347 if (expired || fresh)
1349 if (min_remaining.value < GNUNET_TIME_UNIT_FOREVER_REL.value)
1350 plugin->address_update_task
1351 = GNUNET_SCHEDULER_add_delayed (plugin->env.sched,
1353 GNUNET_SCHEDULER_PRIORITY_IDLE,
1354 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1356 &expire_address_task, plugin);
1362 * Task used to clean up expired addresses for a plugin.
1364 * @param cls closure
1368 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1370 struct TransportPlugin *plugin = cls;
1371 plugin->address_update_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1372 update_addresses (plugin, GNUNET_NO);
1377 * Function that must be called by each plugin to notify the
1378 * transport service about the addresses under which the transport
1379 * provided by the plugin can be reached.
1381 * @param cls closure
1382 * @param name name of the transport that generated the address
1383 * @param addr one of the addresses of the host, NULL for the last address
1384 * the specific address format depends on the transport
1385 * @param addrlen length of the address
1386 * @param expires when should this address automatically expire?
1389 plugin_env_notify_address (void *cls,
1393 struct GNUNET_TIME_Relative expires)
1395 struct TransportPlugin *p = cls;
1396 struct AddressList *al;
1397 struct GNUNET_TIME_Absolute abex;
1399 abex = GNUNET_TIME_relative_to_absolute (expires);
1400 GNUNET_assert (p == find_transport (name));
1405 if ((addrlen == al->addrlen) && (0 == memcmp (addr, &al[1], addrlen)))
1407 if (al->expires.value < abex.value)
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1415 "Plugin `%s' informs us about a new address `%s'\n", name,
1416 GNUNET_a2s(addr, addrlen));
1418 al = GNUNET_malloc (sizeof (struct AddressList) + addrlen);
1420 al->next = p->addresses;
1423 al->addrlen = addrlen;
1424 memcpy (&al[1], addr, addrlen);
1425 update_addresses (p, GNUNET_YES);
1429 struct LookupHelloContext
1431 GNUNET_TRANSPORT_AddressCallback iterator;
1438 lookup_address_callback (void *cls,
1440 struct GNUNET_TIME_Absolute expiration,
1441 const void *addr, size_t addrlen)
1443 struct LookupHelloContext *lhc = cls;
1444 lhc->iterator (lhc->iterator_cls, tname, addr, addrlen);
1450 lookup_hello_callback (void *cls,
1451 const struct GNUNET_PeerIdentity *peer,
1452 const struct GNUNET_HELLO_Message *h, uint32_t trust)
1454 struct LookupHelloContext *lhc = cls;
1458 lhc->iterator (lhc->iterator_cls, NULL, NULL, 0);
1464 GNUNET_HELLO_iterate_addresses (h,
1465 GNUNET_NO, &lookup_address_callback, lhc);
1470 * Function that allows a transport to query the known
1471 * network addresses for a given peer.
1473 * @param cls closure
1474 * @param timeout after how long should we time out?
1475 * @param target which peer are we looking for?
1476 * @param iter function to call for each known address
1477 * @param iter_cls closure for iter
1480 plugin_env_lookup_address (void *cls,
1481 struct GNUNET_TIME_Relative timeout,
1482 const struct GNUNET_PeerIdentity *target,
1483 GNUNET_TRANSPORT_AddressCallback iter,
1486 struct LookupHelloContext *lhc;
1488 lhc = GNUNET_malloc (sizeof (struct LookupHelloContext));
1489 lhc->iterator = iter;
1490 lhc->iterator_cls = iter_cls;
1491 GNUNET_PEERINFO_for_all (cfg,
1493 target, 0, timeout, &lookup_hello_callback, &lhc);
1498 * Notify all of our clients about a peer connecting.
1501 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
1502 struct GNUNET_TIME_Relative latency)
1504 struct ConnectInfoMessage cim;
1505 struct TransportClient *cpos;
1508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1509 "Informing clients about peer `%4s' connecting to us\n",
1512 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
1513 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
1514 cim.quota_out = htonl (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT / (60*1000));
1515 cim.latency = GNUNET_TIME_relative_hton (latency);
1516 memcpy (&cim.id, peer, sizeof (struct GNUNET_PeerIdentity));
1518 while (cpos != NULL)
1520 transmit_to_client (cpos, &cim.header, GNUNET_NO);
1527 * Notify all of our clients about a peer disconnecting.
1530 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
1532 struct DisconnectInfoMessage dim;
1533 struct TransportClient *cpos;
1536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1537 "Informing clients about peer `%4s' disconnecting\n",
1540 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
1541 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1542 dim.reserved = htonl (0);
1543 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
1545 while (cpos != NULL)
1547 transmit_to_client (cpos, &dim.header, GNUNET_NO);
1554 * Copy any validated addresses to buf.
1556 * @return 0 once all addresses have been
1560 list_validated_addresses (void *cls, size_t max, void *buf)
1562 struct ValidationAddress **va = cls;
1565 while ((NULL != *va) && ((*va)->ok != GNUNET_YES))
1569 ret = GNUNET_HELLO_add_address ((*va)->transport_name,
1571 &(*va)->msg[1], (*va)->addr_len, buf, max);
1578 * HELLO validation cleanup task.
1581 cleanup_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1583 struct ValidationAddress *va;
1584 struct ValidationList *pos;
1585 struct ValidationList *prev;
1586 struct GNUNET_TIME_Absolute now;
1587 struct GNUNET_HELLO_Message *hello;
1588 struct GNUNET_PeerIdentity pid;
1591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1592 "HELLO validation cleanup background task running...\n");
1594 now = GNUNET_TIME_absolute_get ();
1596 pos = pending_validations;
1599 if (pos->timeout.value < now.value)
1602 pending_validations = pos->next;
1604 prev->next = pos->next;
1605 va = pos->addresses;
1606 hello = GNUNET_HELLO_create (&pos->publicKey,
1607 &list_validated_addresses, &va);
1608 GNUNET_CRYPTO_hash (&pos->publicKey,
1610 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1614 "Creating persistent `%s' message for peer `%4s' based on confirmed addresses.\n",
1615 "HELLO", GNUNET_i2s (&pid));
1617 GNUNET_PEERINFO_add_peer (cfg, sched, &pid, hello);
1618 GNUNET_free (hello);
1619 while (NULL != (va = pos->addresses))
1621 pos->addresses = va->next;
1622 GNUNET_free (va->transport_name);
1627 pos = pending_validations;
1636 /* finally, reschedule cleanup if needed; list is
1637 ordered by timeout, so we need the last element... */
1638 pos = pending_validations;
1639 while ((pos != NULL) && (pos->next != NULL))
1642 GNUNET_SCHEDULER_add_delayed (sched,
1644 GNUNET_SCHEDULER_PRIORITY_IDLE,
1645 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1646 GNUNET_TIME_absolute_get_remaining
1647 (pos->timeout), &cleanup_validation, NULL);
1651 struct CheckHelloValidatedContext
1654 * Plugin for which we are validating.
1656 struct TransportPlugin *plugin;
1659 * Hello that we are validating.
1661 struct GNUNET_HELLO_Message *hello;
1664 * Validation list being build.
1666 struct ValidationList *e;
1672 * Append the given address to the list of entries
1673 * that need to be validated.
1676 run_validation (void *cls,
1678 struct GNUNET_TIME_Absolute expiration,
1679 const void *addr, size_t addrlen)
1681 struct ValidationList *e = cls;
1682 struct TransportPlugin *tp;
1683 struct ValidationAddress *va;
1684 struct ValidationChallengeMessage *vcm;
1685 struct GNUNET_PeerIdentity id;
1687 tp = find_transport (tname);
1690 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
1691 GNUNET_ERROR_TYPE_BULK,
1693 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
1697 GNUNET_CRYPTO_hash (&e->publicKey,
1699 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1702 "Scheduling validation of address `%s' via `%s' for `%4s'\n",
1703 GNUNET_a2s(addr, addrlen),
1707 va = GNUNET_malloc (sizeof (struct ValidationAddress) +
1708 sizeof (struct ValidationChallengeMessage) + addrlen);
1709 va->next = e->addresses;
1711 vcm = (struct ValidationChallengeMessage *) &va[1];
1713 va->transport_name = GNUNET_strdup (tname);
1714 va->addr_len = addrlen;
1716 htons (sizeof (struct ValidationChallengeMessage) + addrlen);
1717 vcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
1719 htonl (sizeof (struct ValidationChallengeMessage) + addrlen -
1720 sizeof (struct GNUNET_MessageHeader));
1721 vcm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO);
1722 vcm->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1725 memcpy (&vcm[1], addr, addrlen);
1731 * Check if addresses in validated hello "h" overlap with
1732 * those in "chvc->hello" and update "chvc->hello" accordingly,
1733 * removing those addresses that have already been validated.
1736 check_hello_validated (void *cls,
1737 const struct GNUNET_PeerIdentity *peer,
1738 const struct GNUNET_HELLO_Message *h, uint32_t trust)
1740 struct CheckHelloValidatedContext *chvc = cls;
1741 struct ValidationAddress *va;
1742 struct TransportPlugin *tp;
1745 first_call = GNUNET_NO;
1746 if (chvc->e == NULL)
1748 first_call = GNUNET_YES;
1749 chvc->e = GNUNET_malloc (sizeof (struct ValidationList));
1750 GNUNET_assert (GNUNET_OK ==
1751 GNUNET_HELLO_get_key (h != NULL ? h : chvc->hello,
1752 &chvc->e->publicKey));
1754 GNUNET_TIME_relative_to_absolute (HELLO_VERIFICATION_TIMEOUT);
1755 chvc->e->next = pending_validations;
1756 pending_validations = chvc->e;
1760 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
1762 GNUNET_TIME_absolute_get (),
1763 &run_validation, chvc->e);
1765 else if (GNUNET_YES == first_call)
1767 /* no existing HELLO, all addresses are new */
1768 GNUNET_HELLO_iterate_addresses (chvc->hello,
1769 GNUNET_NO, &run_validation, chvc->e);
1772 return; /* wait for next call */
1773 /* finally, transmit validation attempts */
1774 va = chvc->e->addresses;
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1779 "Establishing `%s' connection to validate `%s' of `%4s' (sending our `%s')\n",
1781 "HELLO", GNUNET_i2s (&va->msg->target), "HELLO");
1783 tp = find_transport (va->transport_name);
1784 GNUNET_assert (tp != NULL);
1786 tp->api->send_to (tp->api->cls,
1789 (const struct GNUNET_MessageHeader *) our_hello,
1791 HELLO_VERIFICATION_TIMEOUT,
1792 &va->msg[1], va->addr_len))
1793 va->ok = GNUNET_SYSERR;
1796 if (chvc->e->next == NULL)
1797 GNUNET_SCHEDULER_add_delayed (sched,
1799 GNUNET_SCHEDULER_PRIORITY_IDLE,
1800 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1801 GNUNET_TIME_absolute_get_remaining
1802 (chvc->e->timeout), &cleanup_validation,
1809 * Process HELLO-message.
1811 * @param plugin transport involved, may be NULL
1812 * @param message the actual message
1813 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
1816 process_hello (struct TransportPlugin *plugin,
1817 const struct GNUNET_MessageHeader *message)
1819 struct ValidationList *e;
1821 struct GNUNET_PeerIdentity target;
1822 const struct GNUNET_HELLO_Message *hello;
1823 struct CheckHelloValidatedContext *chvc;
1824 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
1826 hsize = ntohs (message->size);
1827 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
1828 (hsize < sizeof (struct GNUNET_MessageHeader)))
1831 return GNUNET_SYSERR;
1833 /* first, check if load is too high */
1834 if (GNUNET_OS_load_cpu_get (cfg) > 100)
1836 /* TODO: call to stats? */
1839 hello = (const struct GNUNET_HELLO_Message *) message;
1840 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
1842 GNUNET_break_op (0);
1843 return GNUNET_SYSERR;
1845 GNUNET_CRYPTO_hash (&publicKey,
1846 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1847 &target.hashPubKey);
1849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1850 "Processing `%s' message for `%4s'\n",
1851 "HELLO", GNUNET_i2s (&target));
1853 /* check if a HELLO for this peer is already on the validation list */
1854 e = pending_validations;
1857 if (0 == memcmp (&e->publicKey,
1860 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
1862 /* TODO: call to stats? */
1864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1865 "`%s' message for peer `%4s' is already pending; ignoring new message\n",
1866 "HELLO", GNUNET_i2s (&target));
1872 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
1873 chvc->plugin = plugin;
1874 chvc->hello = (struct GNUNET_HELLO_Message *) &chvc[1];
1875 memcpy (chvc->hello, hello, hsize);
1876 /* finally, check if HELLO was previously validated
1877 (continuation will then schedule actual validation) */
1878 GNUNET_PEERINFO_for_all (cfg,
1882 HELLO_VERIFICATION_TIMEOUT,
1883 &check_hello_validated, chvc);
1889 * Handle PING-message. If the plugin that gave us the message is
1890 * able to queue the PONG immediately, we only queue one PONG.
1891 * Otherwise we send at most TWO PONG messages, one via an unconfirmed
1892 * transport and one via a confirmed transport. Both addresses are
1893 * selected randomly among those available.
1895 * @param plugin plugin that gave us the message
1896 * @param sender claimed sender of the PING
1897 * @param plugin_context context that might be used to send response
1898 * @param message the actual message
1901 process_ping (struct TransportPlugin *plugin,
1902 const struct GNUNET_PeerIdentity *sender,
1903 void *plugin_context,
1904 const struct GNUNET_MessageHeader *message)
1906 const struct ValidationChallengeMessage *vcm;
1907 struct ValidationChallengeResponse vcr;
1909 struct NeighbourList *n;
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1913 "Processing PING\n");
1915 msize = ntohs (message->size);
1916 if (msize < sizeof (struct ValidationChallengeMessage))
1918 GNUNET_break_op (0);
1921 vcm = (const struct ValidationChallengeMessage *) message;
1922 if (0 != memcmp (&vcm->target,
1923 &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1925 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1926 _("Received `%s' message not destined for me!\n"), "PING");
1927 /* TODO: call statistics */
1930 if ((ntohl (vcm->purpose.size) !=
1931 msize - sizeof (struct GNUNET_MessageHeader))
1932 || (ntohl (vcm->purpose.purpose) !=
1933 GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO))
1935 GNUNET_break_op (0);
1938 msize -= sizeof (struct ValidationChallengeMessage);
1940 plugin->api->address_suggested (plugin->api->cls, &vcm[1], msize))
1942 GNUNET_break_op (0);
1945 vcr.header.size = htons (sizeof (struct ValidationChallengeResponse));
1946 vcr.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
1947 vcr.challenge = vcm->challenge;
1948 vcr.sender = my_identity;
1949 GNUNET_assert (GNUNET_OK ==
1950 GNUNET_CRYPTO_rsa_sign (my_private_key,
1951 &vcm->purpose, &vcr.signature));
1953 GNUNET_assert (GNUNET_OK ==
1954 GNUNET_CRYPTO_rsa_verify
1955 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO, &vcm->purpose,
1956 &vcr.signature, &my_public_key));
1959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1960 "Trying to transmit PONG using inbound connection\n");
1962 n = find_neighbour (sender);
1968 transmit_to_peer (NULL, 0, &vcr.header, GNUNET_YES, n);
1973 * Handle PONG-message.
1975 * @param message the actual message
1978 process_pong (struct TransportPlugin *plugin,
1979 const struct GNUNET_MessageHeader *message)
1981 const struct ValidationChallengeResponse *vcr;
1982 struct ValidationList *pos;
1983 struct GNUNET_PeerIdentity peer;
1984 struct ValidationAddress *va;
1989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1990 "Processing PONG\n");
1992 vcr = (const struct ValidationChallengeResponse *) message;
1993 pos = pending_validations;
1996 GNUNET_CRYPTO_hash (&pos->publicKey,
1998 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2001 memcmp (&peer, &vcr->sender, sizeof (struct GNUNET_PeerIdentity)))
2007 /* TODO: call statistics (unmatched PONG) */
2008 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2010 ("Received `%s' message but have no record of a matching `%s' message. Ignoring.\n"),
2014 all_done = GNUNET_YES;
2015 matched = GNUNET_NO;
2016 va = pos->addresses;
2019 if (va->msg->challenge == vcr->challenge)
2022 GNUNET_CRYPTO_rsa_verify
2023 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO, &va->msg->purpose,
2024 &vcr->signature, &pos->publicKey))
2026 /* this could rarely happen if we used the same
2027 challenge number for the peer for two different
2028 transports / addresses, but the likelihood is
2030 GNUNET_break_op (0);
2035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2036 "Confirmed validity of peer address.\n");
2038 va->ok = GNUNET_YES;
2040 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2041 matched = GNUNET_YES;
2044 if (va->ok != GNUNET_YES)
2045 all_done = GNUNET_NO;
2048 if (GNUNET_NO == matched)
2050 /* TODO: call statistics (unmatched PONG) */
2051 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2053 ("Received `%s' message but have no record of a matching `%s' message. Ignoring.\n"),
2056 if (GNUNET_YES == all_done)
2058 pos->timeout.value = 0;
2059 GNUNET_SCHEDULER_add_delayed (sched,
2061 GNUNET_SCHEDULER_PRIORITY_IDLE,
2062 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
2063 GNUNET_TIME_UNIT_ZERO,
2064 &cleanup_validation, NULL);
2070 * The peer specified by the given neighbour has timed-out. Update
2071 * our state and do the necessary notifications. Also notifies
2072 * our clients that the neighbour is now officially gone.
2074 * @param n the neighbour list entry for the peer
2077 disconnect_neighbour (struct NeighbourList *n)
2079 struct ReadyList *rpos;
2080 struct NeighbourList *npos;
2081 struct NeighbourList *nprev;
2082 struct MessageQueue *mq;
2085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2086 "Disconnecting from neighbour\n");
2088 /* remove n from neighbours list */
2091 while ((npos != NULL) && (npos != n))
2096 GNUNET_assert (npos != NULL);
2098 neighbours = n->next;
2100 nprev->next = n->next;
2102 /* notify all clients about disconnect */
2103 notify_clients_disconnect (&n->id);
2105 /* clean up all plugins, cancel connections & pending transmissions */
2106 while (NULL != (rpos = n->plugins))
2108 n->plugins = rpos->next;
2109 GNUNET_assert (rpos->neighbour == n);
2110 rpos->plugin->api->cancel (rpos->plugin->api->cls,
2111 rpos->plugin_handle, rpos, &n->id);
2115 /* free all messages on the queue */
2116 while (NULL != (mq = n->messages))
2118 n->messages = mq->next;
2119 GNUNET_assert (mq->neighbour == n);
2123 /* finally, free n itself */
2129 * Add an entry for each of our transport plugins
2130 * (that are able to send) to the list of plugins
2131 * for this neighbour.
2133 * @param neighbour to initialize
2136 add_plugins (struct NeighbourList *neighbour)
2138 struct TransportPlugin *tp;
2139 struct ReadyList *rl;
2141 neighbour->retry_plugins_time
2142 = GNUNET_TIME_relative_to_absolute (PLUGIN_RETRY_FREQUENCY);
2146 if (tp->api->send != NULL)
2148 rl = GNUNET_malloc (sizeof (struct ReadyList));
2149 rl->next = neighbour->plugins;
2150 neighbour->plugins = rl;
2152 rl->neighbour = neighbour;
2153 rl->transmit_ready = GNUNET_YES;
2161 neighbour_timeout_task (void *cls,
2162 const struct GNUNET_SCHEDULER_TaskContext *tc)
2164 struct NeighbourList *n = cls;
2167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2168 "Neighbour has timed out!\n");
2170 n->timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
2171 disconnect_neighbour (n);
2177 * Create a fresh entry in our neighbour list for the given peer.
2178 * Will try to transmit our current HELLO to the new neighbour. Also
2179 * notifies our clients about the new "connection".
2181 * @param peer the peer for which we create the entry
2182 * @return the new neighbour list entry
2184 static struct NeighbourList *
2185 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer)
2187 struct NeighbourList *n;
2190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2191 "Setting up new neighbour `%4s', sending our HELLO to introduce ourselves\n",
2194 GNUNET_assert (our_hello != NULL);
2195 n = GNUNET_malloc (sizeof (struct NeighbourList));
2196 n->next = neighbours;
2199 n->last_quota_update = GNUNET_TIME_absolute_get ();
2201 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2202 n->quota_in = (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT + 59999) / (60 * 1000);
2204 n->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
2206 GNUNET_SCHEDULER_PRIORITY_IDLE,
2207 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
2208 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2209 &neighbour_timeout_task, n);
2210 transmit_to_peer (NULL, 0,
2211 (const struct GNUNET_MessageHeader *) our_hello,
2213 notify_clients_connect (peer, GNUNET_TIME_UNIT_FOREVER_REL);
2219 * Function called by the plugin for each received message.
2220 * Update data volumes, possibly notify plugins about
2221 * reducing the rate at which they read from the socket
2222 * and generally forward to our receive callback.
2224 * @param plugin_context value to pass to this plugin
2225 * to respond to the given peer (use is optional,
2226 * but may speed up processing)
2227 * @param service_context value passed to the transport-service
2228 * to identify the neighbour; will be NULL on the first
2229 * call for a given peer
2230 * @param latency estimated latency for communicating with the
2232 * @param peer (claimed) identity of the other peer
2233 * @param message the message, NULL if peer was disconnected
2234 * @return the new service_context that the plugin should use
2235 * for future receive calls for messages from this
2238 static struct ReadyList *
2239 plugin_env_receive (void *cls,
2240 void *plugin_context,
2241 struct ReadyList *service_context,
2242 struct GNUNET_TIME_Relative latency,
2243 const struct GNUNET_PeerIdentity *peer,
2244 const struct GNUNET_MessageHeader *message)
2246 const struct GNUNET_MessageHeader ack = {
2247 htons (sizeof (struct GNUNET_MessageHeader)),
2248 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ACK)
2250 struct TransportPlugin *plugin = cls;
2251 struct TransportClient *cpos;
2252 struct InboundMessage *im;
2254 struct NeighbourList *n;
2256 if (service_context != NULL)
2258 n = service_context->neighbour;
2259 GNUNET_assert (n != NULL);
2263 n = find_neighbour (peer);
2266 if (message == NULL)
2267 return NULL; /* disconnect of peer already marked down */
2268 n = setup_new_neighbour (peer);
2270 service_context = n->plugins;
2271 while ((service_context != NULL) && (plugin != service_context->plugin))
2272 service_context = service_context->next;
2273 GNUNET_assert ((plugin->api->send == NULL) ||
2274 (service_context != NULL));
2276 if (message == NULL)
2278 if ((service_context != NULL) &&
2279 (service_context->plugin_handle == plugin_context))
2281 service_context->connected = GNUNET_NO;
2282 service_context->plugin_handle = NULL;
2284 /* TODO: call stats */
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2289 "Processing message of type `%u' received by plugin...\n",
2290 ntohs (message->type));
2292 if (service_context != NULL)
2294 if (service_context->connected == GNUNET_NO)
2296 service_context->connected = GNUNET_YES;
2297 service_context->transmit_ready = GNUNET_YES;
2298 service_context->connect_attempts++;
2300 service_context->timeout
2301 = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2302 service_context->plugin_handle = plugin_context;
2303 service_context->latency = latency;
2305 /* update traffic received amount ... */
2306 msize = ntohs (message->size);
2307 n->last_received += msize;
2308 GNUNET_SCHEDULER_cancel (sched, n->timeout_task);
2310 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2312 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_NO,
2313 GNUNET_SCHEDULER_PRIORITY_IDLE,
2314 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
2315 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2316 &neighbour_timeout_task, n);
2318 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2320 /* dropping message due to frequent inbound volume violations! */
2321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
2322 GNUNET_ERROR_TYPE_BULK,
2324 ("Dropping incoming message due to repeated bandwidth quota violations.\n"));
2325 /* TODO: call stats */
2326 GNUNET_assert (NULL != service_context->neighbour);
2327 return service_context;
2329 switch (ntohs (message->type))
2331 case GNUNET_MESSAGE_TYPE_HELLO:
2333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2334 "Receiving `%s' message from `%4s'.\n", "HELLO",
2337 process_hello (plugin, message);
2339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2340 "Sending `%s' message to connecting peer `%4s'.\n", "ACK",
2343 transmit_to_peer (NULL, 0, &ack, GNUNET_YES, n);
2345 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
2346 process_ping (plugin, peer, plugin_context, message);
2348 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
2349 process_pong (plugin, message);
2351 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
2352 n->saw_ack = GNUNET_YES;
2353 /* intentional fall-through! */
2356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2357 "Received message of type %u from `%4s', sending to all clients.\n",
2358 ntohs (message->type),
2361 /* transmit message to all clients */
2362 im = GNUNET_malloc (sizeof (struct InboundMessage) + msize);
2363 im->header.size = htons (sizeof (struct InboundMessage) + msize);
2364 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
2365 im->latency = GNUNET_TIME_relative_hton (latency);
2367 memcpy (&im[1], message, msize);
2370 while (cpos != NULL)
2372 transmit_to_client (cpos, &im->header, GNUNET_YES);
2377 GNUNET_assert (NULL != service_context->neighbour);
2378 return service_context;
2383 * Handle START-message. This is the first message sent to us
2384 * by any client which causes us to add it to our list.
2386 * @param cls closure (always NULL)
2387 * @param client identification of the client
2388 * @param message the actual message
2391 handle_start (void *cls,
2392 struct GNUNET_SERVER_Client *client,
2393 const struct GNUNET_MessageHeader *message)
2395 struct TransportClient *c;
2396 struct ConnectInfoMessage cim;
2397 struct NeighbourList *n;
2398 struct InboundMessage *im;
2399 struct GNUNET_MessageHeader *ack;
2402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2403 "Received `%s' request from client\n", "START");
2408 if (c->client == client)
2410 /* client already on our list! */
2412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2417 c = GNUNET_malloc (sizeof (struct TransportClient));
2421 if (our_hello != NULL)
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2425 "Sending our own HELLO to new client\n");
2427 transmit_to_client (c,
2428 (const struct GNUNET_MessageHeader *) our_hello,
2430 /* tell new client about all existing connections */
2431 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
2432 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2433 cim.quota_out = htonl (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT / (60 * 1000));
2434 cim.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO); /* FIXME? */
2435 im = GNUNET_malloc (sizeof (struct InboundMessage) +
2436 sizeof (struct GNUNET_MessageHeader));
2437 im->header.size = htons (sizeof (struct InboundMessage) +
2438 sizeof (struct GNUNET_MessageHeader));
2439 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
2440 im->latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO); /* FIXME? */
2441 ack = (struct GNUNET_MessageHeader *) &im[1];
2442 ack->size = htons (sizeof (struct GNUNET_MessageHeader));
2443 ack->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ACK);
2444 for (n = neighbours; n != NULL; n = n->next)
2447 transmit_to_client (c, &cim.header, GNUNET_NO);
2451 transmit_to_client (c, &im->header, GNUNET_NO);
2456 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2461 * Handle HELLO-message.
2463 * @param cls closure (always NULL)
2464 * @param client identification of the client
2465 * @param message the actual message
2468 handle_hello (void *cls,
2469 struct GNUNET_SERVER_Client *client,
2470 const struct GNUNET_MessageHeader *message)
2475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2476 "Received `%s' request from client\n", "HELLO");
2478 ret = process_hello (NULL, message);
2479 GNUNET_SERVER_receive_done (client, ret);
2484 * Handle SEND-message.
2486 * @param cls closure (always NULL)
2487 * @param client identification of the client
2488 * @param message the actual message
2491 handle_send (void *cls,
2492 struct GNUNET_SERVER_Client *client,
2493 const struct GNUNET_MessageHeader *message)
2495 struct TransportClient *tc;
2496 struct NeighbourList *n;
2497 const struct OutboundMessage *obm;
2498 const struct GNUNET_MessageHeader *obmm;
2502 size = ntohs (message->size);
2504 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
2507 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2510 obm = (const struct OutboundMessage *) message;
2512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2513 "Received `%s' request from client with target `%4s'\n",
2514 "SEND", GNUNET_i2s (&obm->peer));
2516 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
2517 msize = ntohs (obmm->size);
2518 if (size != msize + sizeof (struct OutboundMessage))
2521 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2524 n = find_neighbour (&obm->peer);
2526 n = setup_new_neighbour (&obm->peer);
2528 while ((tc != NULL) && (tc->client != client))
2532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2533 "Client asked to transmit %u-byte message of type %u to `%4s'\n",
2535 ntohs (obmm->type), GNUNET_i2s (&obm->peer));
2537 transmit_to_peer (tc, ntohl(obm->priority), obmm, GNUNET_NO, n);
2538 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2543 * Handle SET_QUOTA-message.
2545 * @param cls closure (always NULL)
2546 * @param client identification of the client
2547 * @param message the actual message
2550 handle_set_quota (void *cls,
2551 struct GNUNET_SERVER_Client *client,
2552 const struct GNUNET_MessageHeader *message)
2554 const struct QuotaSetMessage *qsm =
2555 (const struct QuotaSetMessage *) message;
2556 struct NeighbourList *n;
2557 struct TransportPlugin *p;
2558 struct ReadyList *rl;
2561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2562 "Received `%s' request from client for peer `%4s'\n",
2563 "SET_QUOTA", GNUNET_i2s (&qsm->peer));
2565 n = find_neighbour (&qsm->peer);
2568 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2572 if (n->quota_in < ntohl (qsm->quota_in))
2573 n->last_quota_update = GNUNET_TIME_absolute_get ();
2574 n->quota_in = ntohl (qsm->quota_in);
2579 p->api->set_receive_quota (p->api->cls,
2580 &qsm->peer, ntohl (qsm->quota_in));
2583 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2588 * Handle TRY_CONNECT-message.
2590 * @param cls closure (always NULL)
2591 * @param client identification of the client
2592 * @param message the actual message
2595 handle_try_connect (void *cls,
2596 struct GNUNET_SERVER_Client *client,
2597 const struct GNUNET_MessageHeader *message)
2599 const struct TryConnectMessage *tcm;
2601 tcm = (const struct TryConnectMessage *) message;
2603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2604 "Received `%s' request from client asking to connect to `%4s'\n",
2605 "TRY_CONNECT", GNUNET_i2s (&tcm->peer));
2607 if (NULL == find_neighbour (&tcm->peer))
2608 setup_new_neighbour (&tcm->peer);
2609 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2614 * List of handlers for the messages understood by this
2617 static struct GNUNET_SERVER_MessageHandler handlers[] = {
2618 {&handle_start, NULL,
2619 GNUNET_MESSAGE_TYPE_TRANSPORT_START, 0},
2620 {&handle_hello, NULL,
2621 GNUNET_MESSAGE_TYPE_HELLO, 0},
2622 {&handle_send, NULL,
2623 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
2624 {&handle_set_quota, NULL,
2625 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
2626 {&handle_try_connect, NULL,
2627 GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT,
2628 sizeof (struct TryConnectMessage)},
2634 * Setup the environment for this plugin.
2637 create_environment (struct TransportPlugin *plug)
2639 plug->env.cfg = cfg;
2640 plug->env.sched = sched;
2641 plug->env.my_public_key = &my_public_key;
2642 plug->env.cls = plug;
2643 plug->env.receive = &plugin_env_receive;
2644 plug->env.lookup = &plugin_env_lookup_address;
2645 plug->env.notify_address = &plugin_env_notify_address;
2646 plug->env.default_quota_in = (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT + 59999) / (60 * 1000);
2647 plug->env.max_connections = max_connect_per_transport;
2652 * Start the specified transport (load the plugin).
2655 start_transport (struct GNUNET_SERVER_Handle *server, const char *name)
2657 struct TransportPlugin *plug;
2660 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2661 _("Loading `%s' transport plugin\n"), name);
2662 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
2663 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
2664 create_environment (plug);
2665 plug->short_name = GNUNET_strdup (name);
2666 plug->lib_name = libname;
2667 plug->next = plugins;
2669 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
2670 if (plug->api == NULL)
2672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2673 _("Failed to load transport plugin for `%s'\n"), name);
2674 GNUNET_free (plug->short_name);
2675 plugins = plug->next;
2676 GNUNET_free (libname);
2683 * Called whenever a client is disconnected. Frees our
2684 * resources associated with that client.
2686 * @param cls closure
2687 * @param client identification of the client
2690 client_disconnect_notification (void *cls,
2691 struct GNUNET_SERVER_Client *client)
2693 struct TransportClient *pos;
2694 struct TransportClient *prev;
2695 struct ClientMessageQueueEntry *mqe;
2698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2699 "Client disconnected, cleaning up.\n");
2703 while ((pos != NULL) && (pos->client != client))
2710 while (NULL != (mqe = pos->message_queue_head))
2712 pos->message_queue_head = mqe->next;
2715 pos->message_queue_head = NULL;
2717 clients = pos->next;
2719 prev->next = pos->next;
2720 if (GNUNET_YES == pos->tcs_pending)
2730 * Initiate transport service.
2732 * @param cls closure
2733 * @param s scheduler to use
2734 * @param serv the initialized server
2735 * @param c configuration to use
2739 struct GNUNET_SCHEDULER_Handle *s,
2740 struct GNUNET_SERVER_Handle *serv, struct GNUNET_CONFIGURATION_Handle *c)
2745 unsigned long long tneigh;
2750 /* parse configuration */
2752 GNUNET_CONFIGURATION_get_value_number (c,
2757 GNUNET_CONFIGURATION_get_value_filename (c,
2759 "HOSTKEY", &keyfile)))
2761 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2763 ("Transport service is lacking key configuration settings. Exiting.\n"));
2764 GNUNET_SCHEDULER_shutdown (s);
2767 max_connect_per_transport = (uint32_t) tneigh;
2768 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2769 GNUNET_free (keyfile);
2770 if (my_private_key == NULL)
2772 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2774 ("Transport service could not access hostkey. Exiting.\n"));
2775 GNUNET_SCHEDULER_shutdown (s);
2778 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
2779 GNUNET_CRYPTO_hash (&my_public_key,
2780 sizeof (my_public_key), &my_identity.hashPubKey);
2781 /* setup notification */
2783 GNUNET_SERVER_disconnect_notify (server,
2784 &client_disconnect_notification, NULL);
2785 /* load plugins... */
2788 GNUNET_CONFIGURATION_get_value_string (c,
2789 "TRANSPORT", "PLUGINS", &plugs))
2791 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2792 _("Starting transport plugins `%s'\n"), plugs);
2793 pos = strtok (plugs, " ");
2796 start_transport (server, pos);
2798 pos = strtok (NULL, " ");
2800 GNUNET_free (plugs);
2805 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2806 _("Transport service ready.\n"));
2808 /* process client requests */
2809 GNUNET_SERVER_add_handlers (server, handlers);
2814 * Function called when the service shuts
2815 * down. Unloads our plugins.
2817 * @param cls closure
2818 * @param cfg configuration to use
2821 unload_plugins (void *cls, struct GNUNET_CONFIGURATION_Handle *cfg)
2823 struct TransportPlugin *plug;
2824 struct AddressList *al;
2827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2828 "Transport service is unloading plugins...\n");
2830 while (NULL != (plug = plugins))
2832 plugins = plug->next;
2833 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
2834 GNUNET_free (plug->lib_name);
2835 GNUNET_free (plug->short_name);
2836 while (NULL != (al = plug->addresses))
2838 plug->addresses = al->next;
2843 if (my_private_key != NULL)
2844 GNUNET_CRYPTO_rsa_key_free (my_private_key);
2849 * The main function for the transport service.
2851 * @param argc number of arguments from the command line
2852 * @param argv command line arguments
2853 * @return 0 ok, 1 on error
2856 main (int argc, char *const *argv)
2858 return (GNUNET_OK ==
2859 GNUNET_SERVICE_run (argc,
2862 &run, NULL, &unload_plugins, NULL)) ? 0 : 1;
2865 /* end of gnunet-service-transport.c */