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?
70 * Must be rather small, otherwise a partially successful HELLO
71 * validation (some addresses working) might not be available
72 * before a client's request for a connection fails for good.
73 * Besides, if a single request to an address takes a long time,
74 * then the peer is unlikely worthwhile anyway.
76 #define HELLO_VERIFICATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
79 * How often do we re-add (cheaper) plugins to our list of plugins
80 * to try for a given connected peer?
82 #define PLUGIN_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
85 * After how long do we expire an address in a HELLO
86 * that we just validated? This value is also used
87 * for our own addresses when we create a HELLO.
89 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
93 * Entry in linked list of network addresses.
98 * This is a linked list.
100 struct AddressList *next;
103 * The address, actually a pointer to the end
104 * of this struct. Do not free!
109 * How long until we auto-expire this address (unless it is
110 * re-confirmed by the transport)?
112 struct GNUNET_TIME_Absolute expires;
123 * Entry in linked list of all of our plugins.
125 struct TransportPlugin
129 * This is a linked list.
131 struct TransportPlugin *next;
134 * API of the transport as returned by the plugin's
135 * initialization function.
137 struct GNUNET_TRANSPORT_PluginFunctions *api;
140 * Short name for the plugin (i.e. "tcp").
145 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
150 * List of our known addresses for this transport.
152 struct AddressList *addresses;
155 * Environment this transport service is using
158 struct GNUNET_TRANSPORT_PluginEnvironment env;
161 * ID of task that is used to clean up expired addresses.
163 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
167 * Set to GNUNET_YES if we need to scrap the existing
168 * list of "addresses" and start fresh when we receive
169 * the next address update from a transport. Set to
170 * GNUNET_NO if we should just add the new address
171 * to the list and wait for the commit call.
176 struct NeighbourList;
179 * For each neighbour we keep a list of messages
180 * that we still want to transmit to the neighbour.
186 * This is a linked list.
188 struct MessageQueue *next;
191 * The message we want to transmit.
193 struct GNUNET_MessageHeader *message;
196 * Client responsible for queueing the message;
197 * used to check that a client has not two messages
198 * pending for the same target. Can be NULL.
200 struct TransportClient *client;
203 * Neighbour this entry belongs to.
205 struct NeighbourList *neighbour;
208 * Plugin that we used for the transmission.
209 * NULL until we scheduled a transmission.
211 struct TransportPlugin *plugin;
214 * Internal message of the transport system that should not be
215 * included in the usual SEND-SEND_OK transmission confirmation
216 * traffic management scheme. Typically, "internal_msg" will
217 * be set whenever "client" is NULL (but it is not strictly
223 * How important is the message?
225 unsigned int priority;
231 * For a given Neighbour, which plugins are available
232 * to talk to this peer and what are their costs?
238 * This is a linked list.
240 struct ReadyList *next;
243 * Which of our transport plugins does this entry
246 struct TransportPlugin *plugin;
249 * Neighbour this entry belongs to.
251 struct NeighbourList *neighbour;
254 * What was the last latency observed for this plugin
255 * and peer? Invalid if connected is GNUNET_NO.
257 struct GNUNET_TIME_Relative latency;
260 * If we did not successfully transmit a message to the
261 * given peer via this connection during the specified
262 * time, we should consider the connection to be dead.
263 * This is used in the case that a TCP transport simply
264 * stalls writing to the stream but does not formerly
265 * get a signal that the other peer died.
267 struct GNUNET_TIME_Absolute timeout;
270 * Is this plugin currently connected? The first time
271 * we transmit or send data to a peer via a particular
272 * plugin, we set this to GNUNET_YES. If we later get
273 * an error (disconnect notification or transmission
274 * failure), we set it back to GNUNET_NO. Each time the
275 * value is set to GNUNET_YES, we increment the
276 * "connect_attempts" counter. If that one reaches a
277 * particular threshold, we consider the plugin to not
278 * be working properly at this time for the given peer
279 * and remove it from the eligible list.
284 * How often have we tried to connect using this plugin?
286 unsigned int connect_attempts;
289 * Is this plugin ready to transmit to the specific
290 * target? GNUNET_NO if not. Initially, all plugins
291 * are marked ready. If a transmission is in progress,
292 * "transmit_ready" is set to GNUNET_NO.
300 * Entry in linked list of all of our current neighbours.
306 * This is a linked list.
308 struct NeighbourList *next;
311 * Which of our transports is connected to this peer
312 * and what is their status?
314 struct ReadyList *plugins;
317 * List of messages we would like to send to this peer;
318 * must contain at most one message per client.
320 struct MessageQueue *messages;
323 * Identity of this neighbour.
325 struct GNUNET_PeerIdentity id;
328 * ID of task scheduled to run when this peer is about to
329 * time out (will free resources associated with the peer).
331 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
334 * How long until we should consider this peer dead
335 * (if we don't receive another message in the
338 struct GNUNET_TIME_Absolute peer_timeout;
341 * At what time did we reset last_received last?
343 struct GNUNET_TIME_Absolute last_quota_update;
346 * At what time should we try to again add plugins to
349 struct GNUNET_TIME_Absolute retry_plugins_time;
352 * How many bytes have we received since the "last_quota_update"
355 uint64_t last_received;
358 * Global quota for inbound traffic for the neighbour in bytes/ms.
363 * How often has the other peer (recently) violated the
364 * inbound traffic limit? Incremented by 10 per violation,
365 * decremented by 1 per non-violation (for each
368 unsigned int quota_violation_count;
371 * Have we seen an ACK from this neighbour in the past?
372 * (used to make up a fake ACK for clients connecting after
373 * the neighbour connected to us).
381 * Linked list of messages to be transmitted to
382 * the client. Each entry is followed by the
385 struct ClientMessageQueueEntry
388 * This is a linked list.
390 struct ClientMessageQueueEntry *next;
395 * Client connected to the transport service.
397 struct TransportClient
401 * This is a linked list.
403 struct TransportClient *next;
406 * Handle to the client.
408 struct GNUNET_SERVER_Client *client;
411 * Linked list of messages yet to be transmitted to
414 struct ClientMessageQueueEntry *message_queue_head;
417 * Tail of linked list of messages yet to be transmitted to the
420 struct ClientMessageQueueEntry *message_queue_tail;
423 * Is a call to "transmit_send_continuation" pending? If so, we
424 * must not free this struct (even if the corresponding client
425 * disconnects) and instead only remove it from the linked list and
426 * set the "client" field to NULL.
431 * Length of the list of messages pending for this client.
433 unsigned int message_count;
439 * For each HELLO, we may have to validate multiple addresses;
440 * each address gets its own request entry.
442 struct ValidationAddress
445 * This is a linked list.
447 struct ValidationAddress *next;
450 * Name of the transport.
452 char *transport_name;
455 * When should this validated address expire?
457 struct GNUNET_TIME_Absolute expiration;
460 * Length of the address we are validating.
465 * Challenge number we used.
470 * Set to GNUNET_YES if the challenge was met,
471 * GNUNET_SYSERR if we know it failed, GNUNET_NO
472 * if we are waiting on a response.
479 * Entry in linked list of all HELLOs awaiting validation.
481 struct ValidationList
485 * This is a linked list.
487 struct ValidationList *next;
490 * Linked list with one entry per address from the HELLO
491 * that needs to be validated.
493 struct ValidationAddress *addresses;
496 * The public key of the peer.
498 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
501 * When does this record time-out? (assuming the
502 * challenge goes unanswered)
504 struct GNUNET_TIME_Absolute timeout;
510 * HELLOs awaiting validation.
512 static struct ValidationList *pending_validations;
517 static struct GNUNET_HELLO_Message *our_hello;
520 * "version" of "our_hello". Used to see if a given
521 * neighbour has already been sent the latest version
522 * of our HELLO message.
524 static unsigned int our_hello_version;
529 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
534 static struct GNUNET_PeerIdentity my_identity;
539 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
544 struct GNUNET_SCHEDULER_Handle *sched;
549 const struct GNUNET_CONFIGURATION_Handle *cfg;
552 * Linked list of all clients to this service.
554 static struct TransportClient *clients;
557 * All loaded plugins.
559 static struct TransportPlugin *plugins;
564 static struct GNUNET_SERVER_Handle *server;
567 * All known neighbours and their HELLOs.
569 static struct NeighbourList *neighbours;
572 * Number of neighbours we'd like to have.
574 static uint32_t max_connect_per_transport;
578 * Find an entry in the neighbour list for a particular peer.
580 * @return NULL if not found.
582 static struct NeighbourList *
583 find_neighbour (const struct GNUNET_PeerIdentity *key)
585 struct NeighbourList *head = neighbours;
586 while ((head != NULL) &&
587 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
594 * Find an entry in the transport list for a particular transport.
596 * @return NULL if not found.
598 static struct TransportPlugin *
599 find_transport (const char *short_name)
601 struct TransportPlugin *head = plugins;
602 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
609 * Update the quota values for the given neighbour now.
612 update_quota (struct NeighbourList *n)
614 struct GNUNET_TIME_Relative delta;
618 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
619 if (delta.value < MIN_QUOTA_REFRESH_TIME)
620 return; /* not enough time passed for doing quota update */
621 allowed = delta.value * n->quota_in;
622 if (n->last_received < allowed)
624 remaining = allowed - n->last_received;
626 remaining /= n->quota_in;
629 if (remaining > MAX_BANDWIDTH_CARRY)
630 remaining = MAX_BANDWIDTH_CARRY;
631 n->last_received = 0;
632 n->last_quota_update = GNUNET_TIME_absolute_get ();
633 n->last_quota_update.value -= remaining;
634 if (n->quota_violation_count > 0)
635 n->quota_violation_count--;
639 n->last_received -= allowed;
640 n->last_quota_update = GNUNET_TIME_absolute_get ();
641 if (n->last_received > allowed)
643 /* more than twice the allowed rate! */
644 n->quota_violation_count += 10;
651 * Function called to notify a client about the socket
652 * being ready to queue more data. "buf" will be
653 * NULL and "size" zero if the socket was closed for
654 * writing in the meantime.
657 * @param size number of bytes available in buf
658 * @param buf where the callee should write the message
659 * @return number of bytes written to buf
662 transmit_to_client_callback (void *cls, size_t size, void *buf)
664 struct TransportClient *client = cls;
665 struct ClientMessageQueueEntry *q;
668 const struct GNUNET_MessageHeader *msg;
669 struct GNUNET_CONNECTION_TransmitHandle *th;
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675 "Transmission to client failed, closing connection.\n");
676 /* fatal error with client, free message queue! */
677 while (NULL != (q = client->message_queue_head))
679 client->message_queue_head = q->next;
682 client->message_queue_tail = NULL;
683 client->message_count = 0;
688 while (NULL != (q = client->message_queue_head))
690 msg = (const struct GNUNET_MessageHeader *) &q[1];
691 msize = ntohs (msg->size);
692 if (msize + tsize > size)
695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
696 "Transmitting message of type %u to client.\n",
699 client->message_queue_head = q->next;
701 client->message_queue_tail = NULL;
702 memcpy (&cbuf[tsize], msg, msize);
705 client->message_count--;
709 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
710 th = GNUNET_SERVER_notify_transmit_ready (client->client,
712 GNUNET_TIME_UNIT_FOREVER_REL,
713 &transmit_to_client_callback,
715 GNUNET_assert (th != NULL);
722 * Send the specified message to the specified client. Since multiple
723 * messages may be pending for the same client at a time, this code
724 * makes sure that no message is lost.
726 * @param client client to transmit the message to
727 * @param msg the message to send
728 * @param may_drop can this message be dropped if the
729 * message queue for this client is getting far too large?
732 transmit_to_client (struct TransportClient *client,
733 const struct GNUNET_MessageHeader *msg, int may_drop)
735 struct ClientMessageQueueEntry *q;
737 struct GNUNET_CONNECTION_TransmitHandle *th;
739 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
741 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
743 ("Dropping message, have %u messages pending (%u is the soft limit)\n"),
744 client->message_count, MAX_PENDING);
745 /* TODO: call to statistics... */
748 client->message_count++;
749 msize = ntohs (msg->size);
750 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
751 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
752 memcpy (&q[1], msg, msize);
753 /* append to message queue */
754 if (client->message_queue_tail == NULL)
756 client->message_queue_tail = q;
760 client->message_queue_tail->next = q;
761 client->message_queue_tail = q;
763 if (client->message_queue_head == NULL)
765 client->message_queue_head = q;
766 th = GNUNET_SERVER_notify_transmit_ready (client->client,
768 GNUNET_TIME_UNIT_FOREVER_REL,
769 &transmit_to_client_callback,
771 GNUNET_assert (th != NULL);
777 * Find alternative plugins for communication.
779 * @param neighbour for which neighbour should we try to find
783 try_alternative_plugins (struct NeighbourList *neighbour)
785 struct ReadyList *rl;
787 if ((neighbour->plugins != NULL) &&
788 (neighbour->retry_plugins_time.value >
789 GNUNET_TIME_absolute_get ().value))
790 return; /* don't try right now */
791 neighbour->retry_plugins_time
792 = GNUNET_TIME_relative_to_absolute (PLUGIN_RETRY_FREQUENCY);
794 rl = neighbour->plugins;
797 if (rl->connect_attempts > 0)
798 rl->connect_attempts--; /* amnesty */
806 * The peer specified by the given neighbour has timed-out or a plugin
807 * has disconnected. We may either need to do nothing (other plugins
808 * still up), or trigger a full disconnect and clean up. This
809 * function updates our state and do the necessary notifications.
810 * Also notifies our clients that the neighbour is now officially
813 * @param n the neighbour list entry for the peer
814 * @param check should we just check if all plugins
815 * disconnected or must we ask all plugins to
819 disconnect_neighbour (struct NeighbourList *n,
824 * Check the ready list for the given neighbour and
825 * if a plugin is ready for transmission (and if we
826 * have a message), do so!
828 * @param neighbour target peer for which to check the plugins
831 try_transmission_to_peer (struct NeighbourList *neighbour);
835 * Function called by the GNUNET_TRANSPORT_TransmitFunction
836 * upon "completion" of a send request. This tells the API
837 * that it is now legal to send another message to the given
840 * @param cls closure, identifies the entry on the
841 * message queue that was transmitted and the
842 * client responsible for queueing the message
843 * @param rl identifies plugin used for the transmission for
844 * this neighbour; needs to be re-enabled for
845 * future transmissions
846 * @param target the peer receiving the message
847 * @param result GNUNET_OK on success, if the transmission
848 * failed, we should not tell the client to transmit
852 transmit_send_continuation (void *cls,
853 struct ReadyList *rl,
854 const struct GNUNET_PeerIdentity *target,
857 struct MessageQueue *mq = cls;
858 struct SendOkMessage send_ok_msg;
859 struct NeighbourList *n;
861 GNUNET_assert (mq != NULL);
863 GNUNET_assert (n != NULL);
865 memcmp (&n->id, target,
866 sizeof (struct GNUNET_PeerIdentity)));
870 while ((rl != NULL) && (rl->plugin != mq->plugin))
872 GNUNET_assert (rl != NULL);
874 if (result == GNUNET_OK)
876 rl->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 "Transmission to peer `%s' failed, marking connection as down.\n",
883 rl->connected = GNUNET_NO;
885 if (!mq->internal_msg)
886 rl->transmit_ready = GNUNET_YES;
887 if (mq->client != NULL)
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
890 "Notifying client %p about failed transission to peer `%4s'.\n",
893 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
894 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
895 send_ok_msg.success = htonl (result);
896 send_ok_msg.peer = n->id;
897 transmit_to_client (mq->client, &send_ok_msg.header, GNUNET_NO);
899 GNUNET_free (mq->message);
901 /* one plugin just became ready again, try transmitting
902 another message (if available) */
903 if (result == GNUNET_OK)
904 try_transmission_to_peer (n);
906 disconnect_neighbour (n, GNUNET_YES);
911 * Check the ready list for the given neighbour and
912 * if a plugin is ready for transmission (and if we
913 * have a message), do so!
916 try_transmission_to_peer (struct NeighbourList *neighbour)
918 struct ReadyList *pos;
919 struct GNUNET_TIME_Relative min_latency;
920 struct ReadyList *rl;
921 struct MessageQueue *mq;
922 struct GNUNET_TIME_Absolute now;
924 if (neighbour->messages == NULL)
925 return; /* nothing to do */
926 try_alternative_plugins (neighbour);
927 min_latency = GNUNET_TIME_UNIT_FOREVER_REL;
929 mq = neighbour->messages;
930 now = GNUNET_TIME_absolute_get ();
931 pos = neighbour->plugins;
934 /* set plugins that are inactive for a long time back to disconnected */
935 if ((pos->timeout.value < now.value) && (pos->connected == GNUNET_YES))
938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
939 "Marking long-time inactive connection to `%4s' as down.\n",
940 GNUNET_i2s (&neighbour->id));
942 pos->connected = GNUNET_NO;
944 if (((GNUNET_YES == pos->transmit_ready) ||
945 (mq->internal_msg)) &&
946 (pos->connect_attempts < MAX_CONNECT_RETRY) &&
947 ((rl == NULL) || (min_latency.value > pos->latency.value)))
950 min_latency = pos->latency;
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 "No plugin ready to transmit message\n");
960 return; /* nobody ready */
962 if (GNUNET_NO == rl->connected)
964 rl->connect_attempts++;
965 rl->connected = GNUNET_YES;
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968 "Establishing fresh connection with `%4s' via plugin `%s'\n",
969 GNUNET_i2s (&neighbour->id), rl->plugin->short_name);
972 neighbour->messages = mq->next;
973 mq->plugin = rl->plugin;
974 if (!mq->internal_msg)
975 rl->transmit_ready = GNUNET_NO;
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "Giving message of type `%u' for `%4s' to plugin `%s'\n",
979 ntohs (mq->message->type),
980 GNUNET_i2s (&neighbour->id), rl->plugin->short_name);
982 rl->plugin->api->send (rl->plugin->api->cls,
987 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
988 &transmit_send_continuation, mq);
993 * Send the specified message to the specified peer.
995 * @param client source of the transmission request (can be NULL)
996 * @param priority how important is the message
997 * @param msg message to send
998 * @param is_internal is this an internal message
999 * @param neighbour handle to the neighbour for transmission
1002 transmit_to_peer (struct TransportClient *client,
1003 unsigned int priority,
1004 const struct GNUNET_MessageHeader *msg,
1005 int is_internal, struct NeighbourList *neighbour)
1007 struct MessageQueue *mq;
1008 struct MessageQueue *mqe;
1009 struct GNUNET_MessageHeader *m;
1012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1013 _("Sending message of type %u to peer `%4s'\n"),
1014 ntohs (msg->type), GNUNET_i2s (&neighbour->id));
1018 /* check for duplicate submission */
1019 mq = neighbour->messages;
1022 if (mq->client == client)
1024 /* client transmitted to same peer twice
1025 before getting SendOk! */
1032 mq = GNUNET_malloc (sizeof (struct MessageQueue));
1033 mq->client = client;
1034 m = GNUNET_malloc (ntohs (msg->size));
1035 memcpy (m, msg, ntohs (msg->size));
1037 mq->neighbour = neighbour;
1038 mq->internal_msg = is_internal;
1039 mq->priority = priority;
1042 mqe = neighbour->messages;
1044 while (mqe->next != NULL)
1049 neighbour->messages = mq;
1050 try_transmission_to_peer (neighbour);
1063 struct GeneratorContext
1065 struct TransportPlugin *plug_pos;
1066 struct AddressList *addr_pos;
1067 struct GNUNET_TIME_Absolute expiration;
1075 address_generator (void *cls, size_t max, void *buf)
1077 struct GeneratorContext *gc = cls;
1080 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
1082 gc->plug_pos = gc->plug_pos->next;
1083 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
1085 if (NULL == gc->plug_pos)
1087 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
1090 gc->addr_pos->addrlen, buf, max);
1091 gc->addr_pos = gc->addr_pos->next;
1097 * Construct our HELLO message from all of the addresses of
1098 * all of the transports.
1103 struct GNUNET_HELLO_Message *hello;
1104 struct TransportClient *cpos;
1105 struct NeighbourList *npos;
1106 struct GeneratorContext gc;
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1110 "Refreshing my `%s'\n",
1113 gc.plug_pos = plugins;
1114 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
1115 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1116 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
1118 while (cpos != NULL)
1120 transmit_to_client (cpos,
1121 (const struct GNUNET_MessageHeader *) hello,
1126 GNUNET_free_non_null (our_hello);
1128 our_hello_version++;
1129 GNUNET_PEERINFO_add_peer (cfg, sched, &my_identity, our_hello);
1131 while (npos != NULL)
1134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1135 "Transmitting updated `%s' to neighbour `%4s'\n",
1137 GNUNET_i2s(&npos->id));
1139 transmit_to_peer (NULL, 0,
1140 (const struct GNUNET_MessageHeader *) our_hello,
1148 * Task used to clean up expired addresses for a plugin.
1150 * @param cls closure
1154 expire_address_task (void *cls,
1155 const struct GNUNET_SCHEDULER_TaskContext *tc);
1159 * Update the list of addresses for this plugin,
1160 * expiring those that are past their expiration date.
1162 * @param plugin addresses of which plugin should be recomputed?
1163 * @param fresh set to GNUNET_YES if a new address was added
1164 * and we need to regenerate the HELLO even if nobody
1168 update_addresses (struct TransportPlugin *plugin, int fresh)
1170 struct GNUNET_TIME_Relative min_remaining;
1171 struct GNUNET_TIME_Relative remaining;
1172 struct GNUNET_TIME_Absolute now;
1173 struct AddressList *pos;
1174 struct AddressList *prev;
1175 struct AddressList *next;
1178 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_TASK)
1179 GNUNET_SCHEDULER_cancel (plugin->env.sched, plugin->address_update_task);
1180 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1181 now = GNUNET_TIME_absolute_get ();
1182 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
1183 expired = GNUNET_NO;
1185 pos = plugin->addresses;
1189 if (pos->expires.value < now.value)
1191 expired = GNUNET_YES;
1193 plugin->addresses = pos->next;
1195 prev->next = pos->next;
1200 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
1201 if (remaining.value < min_remaining.value)
1202 min_remaining = remaining;
1208 if (expired || fresh)
1210 if (min_remaining.value < GNUNET_TIME_UNIT_FOREVER_REL.value)
1211 plugin->address_update_task
1212 = GNUNET_SCHEDULER_add_delayed (plugin->env.sched,
1214 &expire_address_task, plugin);
1220 * Task used to clean up expired addresses for a plugin.
1222 * @param cls closure
1226 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1228 struct TransportPlugin *plugin = cls;
1229 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1230 update_addresses (plugin, GNUNET_NO);
1235 * Function that must be called by each plugin to notify the
1236 * transport service about the addresses under which the transport
1237 * provided by the plugin can be reached.
1239 * @param cls closure
1240 * @param name name of the transport that generated the address
1241 * @param addr one of the addresses of the host, NULL for the last address
1242 * the specific address format depends on the transport
1243 * @param addrlen length of the address
1244 * @param expires when should this address automatically expire?
1247 plugin_env_notify_address (void *cls,
1251 struct GNUNET_TIME_Relative expires)
1253 struct TransportPlugin *p = cls;
1254 struct AddressList *al;
1255 struct GNUNET_TIME_Absolute abex;
1257 abex = GNUNET_TIME_relative_to_absolute (expires);
1258 GNUNET_assert (p == find_transport (name));
1263 if ((addrlen == al->addrlen) && (0 == memcmp (addr, &al[1], addrlen)))
1265 if (al->expires.value < abex.value)
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1273 "Plugin `%s' informs us about a new address `%s'\n", name,
1274 GNUNET_a2s(addr, addrlen));
1276 al = GNUNET_malloc (sizeof (struct AddressList) + addrlen);
1278 al->next = p->addresses;
1281 al->addrlen = addrlen;
1282 memcpy (&al[1], addr, addrlen);
1283 update_addresses (p, GNUNET_YES);
1288 * Notify all of our clients about a peer connecting.
1291 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
1292 struct GNUNET_TIME_Relative latency)
1294 struct ConnectInfoMessage cim;
1295 struct TransportClient *cpos;
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1299 "Informing clients about peer `%4s' connecting to us\n",
1302 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
1303 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
1304 cim.quota_out = htonl (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT / (60*1000));
1305 cim.latency = GNUNET_TIME_relative_hton (latency);
1306 memcpy (&cim.id, peer, sizeof (struct GNUNET_PeerIdentity));
1308 while (cpos != NULL)
1310 transmit_to_client (cpos, &cim.header, GNUNET_NO);
1317 * Notify all of our clients about a peer disconnecting.
1320 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
1322 struct DisconnectInfoMessage dim;
1323 struct TransportClient *cpos;
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327 "Informing clients about peer `%4s' disconnecting\n",
1330 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
1331 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1332 dim.reserved = htonl (0);
1333 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
1335 while (cpos != NULL)
1337 transmit_to_client (cpos, &dim.header, GNUNET_NO);
1344 * Copy any validated addresses to buf.
1346 * @return 0 once all addresses have been
1350 list_validated_addresses (void *cls, size_t max, void *buf)
1352 struct ValidationAddress **va = cls;
1355 while ((NULL != *va) && ((*va)->ok != GNUNET_YES))
1359 ret = GNUNET_HELLO_add_address ((*va)->transport_name,
1361 &(*va)[1], (*va)->addr_len, buf, max);
1368 * HELLO validation cleanup task.
1371 cleanup_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1373 struct ValidationAddress *va;
1374 struct ValidationList *pos;
1375 struct ValidationList *prev;
1376 struct GNUNET_TIME_Absolute now;
1377 struct GNUNET_TIME_Absolute first;
1378 struct GNUNET_HELLO_Message *hello;
1379 struct GNUNET_PeerIdentity pid;
1380 struct NeighbourList *n;
1383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1384 "HELLO validation cleanup background task running...\n");
1386 now = GNUNET_TIME_absolute_get ();
1388 pos = pending_validations;
1391 if (pos->timeout.value < now.value)
1394 pending_validations = pos->next;
1396 prev->next = pos->next;
1397 va = pos->addresses;
1398 hello = GNUNET_HELLO_create (&pos->publicKey,
1399 &list_validated_addresses, &va);
1400 GNUNET_CRYPTO_hash (&pos->publicKey,
1402 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1406 "Creating persistent `%s' message for peer `%4s' based on confirmed addresses.\n",
1407 "HELLO", GNUNET_i2s (&pid));
1409 GNUNET_PEERINFO_add_peer (cfg, sched, &pid, hello);
1410 n = find_neighbour (&pid);
1412 try_transmission_to_peer (n);
1413 GNUNET_free (hello);
1414 while (NULL != (va = pos->addresses))
1416 pos->addresses = va->next;
1417 GNUNET_free (va->transport_name);
1422 pos = pending_validations;
1431 /* finally, reschedule cleanup if needed; list is
1432 ordered by timeout, so we need the last element... */
1433 if (NULL != pending_validations)
1435 first = pending_validations->timeout;
1436 pos = pending_validations;
1439 first = GNUNET_TIME_absolute_min (first, pos->timeout);
1442 GNUNET_SCHEDULER_add_delayed (sched,
1443 GNUNET_TIME_absolute_get_remaining (first),
1444 &cleanup_validation, NULL);
1452 * Function that will be called if we receive a validation
1453 * of an address challenge that we transmitted to another
1454 * peer. Note that the validation should only be considered
1455 * acceptable if the challenge matches AND if the sender
1456 * address is at least a plausible address for this peer
1457 * (otherwise we may be seeing a MiM attack).
1459 * @param cls closure
1460 * @param name name of the transport that generated the address
1461 * @param peer who responded to our challenge
1462 * @param challenge the challenge number we presumably used
1463 * @param sender_addr string describing our sender address (as observed
1464 * by the other peer in human-readable format)
1467 plugin_env_notify_validation (void *cls,
1469 const struct GNUNET_PeerIdentity *peer,
1471 const char *sender_addr)
1473 unsigned int not_done;
1475 struct ValidationList *pos;
1476 struct ValidationAddress *va;
1477 struct GNUNET_PeerIdentity id;
1479 pos = pending_validations;
1482 GNUNET_CRYPTO_hash (&pos->publicKey,
1484 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1487 memcmp (peer, &id, sizeof (struct GNUNET_PeerIdentity)))
1493 /* TODO: call statistics (unmatched PONG) */
1494 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1496 ("Received validation response but have no record of any validation request for `%4s'. Ignoring.\n"),
1501 matched = GNUNET_NO;
1502 va = pos->addresses;
1505 if (va->challenge == challenge)
1508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1509 "Confirmed validity of address, peer `%4s' has address `%s'.\n",
1511 GNUNET_a2s ((const struct sockaddr*) &va[1],
1514 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
1515 _("Another peer saw us using the address `%s' via `%s'. If this is not plausible, this address should be listed in the configuration as implausible to avoid MiM attacks.\n"),
1518 va->ok = GNUNET_YES;
1520 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1521 matched = GNUNET_YES;
1523 if (va->ok != GNUNET_YES)
1527 if (GNUNET_NO == matched)
1529 /* TODO: call statistics (unmatched PONG) */
1530 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1532 ("Received `%s' message but have no record of a matching `%s' message. Ignoring.\n"),
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1539 "All addresses validated, will now construct `%s' for `%4s'.\n",
1543 pos->timeout.value = 0;
1544 GNUNET_SCHEDULER_add_with_priority (sched,
1545 GNUNET_SCHEDULER_PRIORITY_IDLE,
1546 &cleanup_validation, NULL);
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "Still waiting for %u additional `%s' messages before constructing `%s' for `%4s'.\n",
1562 struct CheckHelloValidatedContext
1565 * Plugin for which we are validating.
1567 struct TransportPlugin *plugin;
1570 * Hello that we are validating.
1572 struct GNUNET_HELLO_Message *hello;
1575 * Validation list being build.
1577 struct ValidationList *e;
1580 * Context for peerinfo iteration.
1581 * NULL after we are done processing peerinfo's information.
1583 struct GNUNET_PEERINFO_IteratorContext *piter;
1589 * Append the given address to the list of entries
1590 * that need to be validated.
1593 run_validation (void *cls,
1595 struct GNUNET_TIME_Absolute expiration,
1596 const void *addr, size_t addrlen)
1598 struct ValidationList *e = cls;
1599 struct TransportPlugin *tp;
1600 struct ValidationAddress *va;
1601 struct GNUNET_PeerIdentity id;
1603 tp = find_transport (tname);
1606 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
1607 GNUNET_ERROR_TYPE_BULK,
1609 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
1613 GNUNET_CRYPTO_hash (&e->publicKey,
1615 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1618 "Scheduling validation of address `%s' via `%s' for `%4s'\n",
1619 GNUNET_a2s(addr, addrlen),
1623 va = GNUNET_malloc (sizeof (struct ValidationAddress) + addrlen);
1624 va->next = e->addresses;
1626 va->transport_name = GNUNET_strdup (tname);
1627 va->addr_len = addrlen;
1628 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1630 memcpy (&va[1], addr, addrlen);
1636 * Check if addresses in validated hello "h" overlap with
1637 * those in "chvc->hello" and update "chvc->hello" accordingly,
1638 * removing those addresses that have already been validated.
1641 check_hello_validated (void *cls,
1642 const struct GNUNET_PeerIdentity *peer,
1643 const struct GNUNET_HELLO_Message *h,
1646 struct CheckHelloValidatedContext *chvc = cls;
1647 struct ValidationAddress *va;
1648 struct TransportPlugin *tp;
1650 struct GNUNET_PeerIdentity apeer;
1652 first_call = GNUNET_NO;
1653 if (chvc->e == NULL)
1656 first_call = GNUNET_YES;
1657 chvc->e = GNUNET_malloc (sizeof (struct ValidationList));
1658 GNUNET_assert (GNUNET_OK ==
1659 GNUNET_HELLO_get_key (h != NULL ? h : chvc->hello,
1660 &chvc->e->publicKey));
1662 GNUNET_TIME_relative_to_absolute (HELLO_VERIFICATION_TIMEOUT);
1663 chvc->e->next = pending_validations;
1664 pending_validations = chvc->e;
1668 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
1670 GNUNET_TIME_absolute_get (),
1671 &run_validation, chvc->e);
1673 else if (GNUNET_YES == first_call)
1675 /* no existing HELLO, all addresses are new */
1676 GNUNET_HELLO_iterate_addresses (chvc->hello,
1677 GNUNET_NO, &run_validation, chvc->e);
1680 return; /* wait for next call */
1681 /* finally, transmit validation attempts */
1682 GNUNET_assert (GNUNET_OK ==
1683 GNUNET_HELLO_get_id (chvc->hello,
1686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1687 "Ready to validate addresses from `%s' message for peer `%4s'\n",
1688 "HELLO", GNUNET_i2s (&apeer));
1690 va = chvc->e->addresses;
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "Establishing `%s' connection to validate `%s' address `%s' of `%4s'\n",
1698 GNUNET_a2s ((const struct sockaddr*) &va[1],
1700 GNUNET_i2s (&apeer));
1702 tp = find_transport (va->transport_name);
1703 GNUNET_assert (tp != NULL);
1705 tp->api->validate (tp->api->cls,
1708 HELLO_VERIFICATION_TIMEOUT,
1711 va->ok = GNUNET_SYSERR;
1714 GNUNET_SCHEDULER_add_delayed (sched,
1715 GNUNET_TIME_absolute_get_remaining (chvc->e->timeout),
1716 &cleanup_validation, NULL);
1722 * Process HELLO-message.
1724 * @param plugin transport involved, may be NULL
1725 * @param message the actual message
1726 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
1729 process_hello (struct TransportPlugin *plugin,
1730 const struct GNUNET_MessageHeader *message)
1732 struct ValidationList *e;
1734 struct GNUNET_PeerIdentity target;
1735 const struct GNUNET_HELLO_Message *hello;
1736 struct CheckHelloValidatedContext *chvc;
1737 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
1739 hsize = ntohs (message->size);
1740 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
1741 (hsize < sizeof (struct GNUNET_MessageHeader)))
1744 return GNUNET_SYSERR;
1746 /* first, check if load is too high */
1747 if (GNUNET_OS_load_cpu_get (cfg) > 100)
1749 /* TODO: call to stats? */
1752 hello = (const struct GNUNET_HELLO_Message *) message;
1753 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
1755 GNUNET_break_op (0);
1756 return GNUNET_SYSERR;
1758 GNUNET_CRYPTO_hash (&publicKey,
1759 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1760 &target.hashPubKey);
1762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1763 "Processing `%s' message for `%4s'\n",
1764 "HELLO", GNUNET_i2s (&target));
1766 /* check if a HELLO for this peer is already on the validation list */
1767 e = pending_validations;
1770 if (0 == memcmp (&e->publicKey,
1773 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
1775 /* TODO: call to stats? */
1777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1778 "`%s' message for peer `%4s' is already pending; ignoring new message\n",
1779 "HELLO", GNUNET_i2s (&target));
1785 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
1786 chvc->plugin = plugin;
1787 chvc->hello = (struct GNUNET_HELLO_Message *) &chvc[1];
1788 memcpy (chvc->hello, hello, hsize);
1789 /* finally, check if HELLO was previously validated
1790 (continuation will then schedule actual validation) */
1791 chvc->piter = GNUNET_PEERINFO_iterate (cfg,
1795 HELLO_VERIFICATION_TIMEOUT,
1796 &check_hello_validated, chvc);
1802 * The peer specified by the given neighbour has timed-out or a plugin
1803 * has disconnected. We may either need to do nothing (other plugins
1804 * still up), or trigger a full disconnect and clean up. This
1805 * function updates our state and do the necessary notifications.
1806 * Also notifies our clients that the neighbour is now officially
1809 * @param n the neighbour list entry for the peer
1810 * @param check should we just check if all plugins
1811 * disconnected or must we ask all plugins to
1815 disconnect_neighbour (struct NeighbourList *n,
1818 struct ReadyList *rpos;
1819 struct NeighbourList *npos;
1820 struct NeighbourList *nprev;
1821 struct MessageQueue *mq;
1823 if (GNUNET_YES == check)
1826 while (NULL != rpos)
1828 if (GNUNET_YES == rpos->connected)
1829 return; /* still connected */
1835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1836 "Disconnecting from `%4s'\n",
1837 GNUNET_i2s(&n->id));
1839 /* remove n from neighbours list */
1842 while ((npos != NULL) && (npos != n))
1847 GNUNET_assert (npos != NULL);
1849 neighbours = n->next;
1851 nprev->next = n->next;
1853 /* notify all clients about disconnect */
1854 notify_clients_disconnect (&n->id);
1856 /* clean up all plugins, cancel connections and pending transmissions */
1857 while (NULL != (rpos = n->plugins))
1859 n->plugins = rpos->next;
1860 GNUNET_assert (rpos->neighbour == n);
1861 if (GNUNET_YES == rpos->connected)
1862 rpos->plugin->api->cancel (rpos->plugin->api->cls,
1868 /* free all messages on the queue */
1869 while (NULL != (mq = n->messages))
1871 n->messages = mq->next;
1872 GNUNET_assert (mq->neighbour == n);
1875 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1876 GNUNET_SCHEDULER_cancel (sched,
1878 /* finally, free n itself */
1884 * Add an entry for each of our transport plugins
1885 * (that are able to send) to the list of plugins
1886 * for this neighbour.
1888 * @param neighbour to initialize
1891 add_plugins (struct NeighbourList *neighbour)
1893 struct TransportPlugin *tp;
1894 struct ReadyList *rl;
1896 neighbour->retry_plugins_time
1897 = GNUNET_TIME_relative_to_absolute (PLUGIN_RETRY_FREQUENCY);
1901 if (tp->api->send != NULL)
1903 rl = GNUNET_malloc (sizeof (struct ReadyList));
1904 rl->next = neighbour->plugins;
1905 neighbour->plugins = rl;
1907 rl->neighbour = neighbour;
1908 rl->transmit_ready = GNUNET_YES;
1916 neighbour_timeout_task (void *cls,
1917 const struct GNUNET_SCHEDULER_TaskContext *tc)
1919 struct NeighbourList *n = cls;
1922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1923 "Neighbour `%4s' has timed out!\n",
1924 GNUNET_i2s(&n->id));
1926 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1927 disconnect_neighbour (n, GNUNET_NO);
1932 * Create a fresh entry in our neighbour list for the given peer.
1933 * Will try to transmit our current HELLO to the new neighbour. Also
1934 * notifies our clients about the new "connection".
1936 * @param peer the peer for which we create the entry
1937 * @return the new neighbour list entry
1939 static struct NeighbourList *
1940 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer)
1942 struct NeighbourList *n;
1945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1946 "Setting up new neighbour `%4s', sending our HELLO to introduce ourselves\n",
1949 GNUNET_assert (our_hello != NULL);
1950 n = GNUNET_malloc (sizeof (struct NeighbourList));
1951 n->next = neighbours;
1954 n->last_quota_update = GNUNET_TIME_absolute_get ();
1956 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1957 n->quota_in = (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT + 59999) / (60 * 1000);
1959 n->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
1960 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1961 &neighbour_timeout_task, n);
1962 transmit_to_peer (NULL, 0,
1963 (const struct GNUNET_MessageHeader *) our_hello,
1965 notify_clients_connect (peer, GNUNET_TIME_UNIT_FOREVER_REL);
1971 * Function called by the plugin for each received message.
1972 * Update data volumes, possibly notify plugins about
1973 * reducing the rate at which they read from the socket
1974 * and generally forward to our receive callback.
1976 * @param cls the "struct TransportPlugin *" we gave to the plugin
1977 * @param service_context value passed to the transport-service
1978 * to identify the neighbour; will be NULL on the first
1979 * call for a given peer
1980 * @param latency estimated latency for communicating with the
1982 * @param peer (claimed) identity of the other peer
1983 * @param message the message, NULL if peer was disconnected
1984 * @return the new service_context that the plugin should use
1985 * for future receive calls for messages from this
1988 static struct ReadyList *
1989 plugin_env_receive (void *cls,
1990 struct ReadyList *service_context,
1991 struct GNUNET_TIME_Relative latency,
1992 const struct GNUNET_PeerIdentity *peer,
1993 const struct GNUNET_MessageHeader *message)
1995 const struct GNUNET_MessageHeader ack = {
1996 htons (sizeof (struct GNUNET_MessageHeader)),
1997 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ACK)
1999 struct TransportPlugin *plugin = cls;
2000 struct TransportClient *cpos;
2001 struct InboundMessage *im;
2003 struct NeighbourList *n;
2005 if (service_context != NULL)
2007 n = service_context->neighbour;
2008 GNUNET_assert (n != NULL);
2012 n = find_neighbour (peer);
2015 if (message == NULL)
2016 return NULL; /* disconnect of peer already marked down */
2017 n = setup_new_neighbour (peer);
2019 service_context = n->plugins;
2020 while ((service_context != NULL) && (plugin != service_context->plugin))
2021 service_context = service_context->next;
2022 GNUNET_assert ((plugin->api->send == NULL) ||
2023 (service_context != NULL));
2025 if (message == NULL)
2028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2029 "Receive failed from `%4s', triggering disconnect\n",
2030 GNUNET_i2s(&n->id));
2032 /* TODO: call stats */
2033 if (service_context != NULL)
2034 service_context->connected = GNUNET_NO;
2035 disconnect_neighbour (n, GNUNET_YES);
2039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2040 "Processing message of type `%u' received by plugin...\n",
2041 ntohs (message->type));
2043 if (service_context != NULL)
2045 if (service_context->connected == GNUNET_NO)
2047 service_context->connected = GNUNET_YES;
2048 service_context->transmit_ready = GNUNET_YES;
2049 service_context->connect_attempts++;
2051 service_context->timeout
2052 = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2053 service_context->latency = latency;
2055 /* update traffic received amount ... */
2056 msize = ntohs (message->size);
2057 n->last_received += msize;
2058 GNUNET_SCHEDULER_cancel (sched, n->timeout_task);
2060 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2062 GNUNET_SCHEDULER_add_delayed (sched,
2063 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2064 &neighbour_timeout_task, n);
2066 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2068 /* dropping message due to frequent inbound volume violations! */
2069 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
2070 GNUNET_ERROR_TYPE_BULK,
2072 ("Dropping incoming message due to repeated bandwidth quota violations.\n"));
2073 /* TODO: call stats */
2074 GNUNET_assert ( (service_context == NULL) ||
2075 (NULL != service_context->neighbour) );
2076 return service_context;
2078 switch (ntohs (message->type))
2080 case GNUNET_MESSAGE_TYPE_HELLO:
2082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2083 "Receiving `%s' message from `%4s'.\n", "HELLO",
2086 process_hello (plugin, message);
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2089 "Sending `%s' message to connecting peer `%4s'.\n", "ACK",
2092 transmit_to_peer (NULL, 0, &ack, GNUNET_YES, n);
2094 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
2095 n->saw_ack = GNUNET_YES;
2096 /* intentional fall-through! */
2099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2100 "Received message of type %u from `%4s', sending to all clients.\n",
2101 ntohs (message->type),
2104 /* transmit message to all clients */
2105 im = GNUNET_malloc (sizeof (struct InboundMessage) + msize);
2106 im->header.size = htons (sizeof (struct InboundMessage) + msize);
2107 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
2108 im->latency = GNUNET_TIME_relative_hton (latency);
2110 memcpy (&im[1], message, msize);
2113 while (cpos != NULL)
2115 transmit_to_client (cpos, &im->header, GNUNET_YES);
2120 GNUNET_assert ( (service_context == NULL) ||
2121 (NULL != service_context->neighbour) );
2122 return service_context;
2127 * Handle START-message. This is the first message sent to us
2128 * by any client which causes us to add it to our list.
2130 * @param cls closure (always NULL)
2131 * @param client identification of the client
2132 * @param message the actual message
2135 handle_start (void *cls,
2136 struct GNUNET_SERVER_Client *client,
2137 const struct GNUNET_MessageHeader *message)
2139 struct TransportClient *c;
2140 struct ConnectInfoMessage cim;
2141 struct NeighbourList *n;
2142 struct InboundMessage *im;
2143 struct GNUNET_MessageHeader *ack;
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2147 "Received `%s' request from client\n", "START");
2152 if (c->client == client)
2154 /* client already on our list! */
2156 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2161 c = GNUNET_malloc (sizeof (struct TransportClient));
2165 if (our_hello != NULL)
2168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2169 "Sending our own `%s' to new client\n",
2172 transmit_to_client (c,
2173 (const struct GNUNET_MessageHeader *) our_hello,
2175 /* tell new client about all existing connections */
2176 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
2177 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2178 cim.quota_out = htonl (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT / (60 * 1000));
2179 cim.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO); /* FIXME? */
2180 im = GNUNET_malloc (sizeof (struct InboundMessage) +
2181 sizeof (struct GNUNET_MessageHeader));
2182 im->header.size = htons (sizeof (struct InboundMessage) +
2183 sizeof (struct GNUNET_MessageHeader));
2184 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
2185 im->latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO); /* FIXME? */
2186 ack = (struct GNUNET_MessageHeader *) &im[1];
2187 ack->size = htons (sizeof (struct GNUNET_MessageHeader));
2188 ack->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ACK);
2189 for (n = neighbours; n != NULL; n = n->next)
2192 transmit_to_client (c, &cim.header, GNUNET_NO);
2196 transmit_to_client (c, &im->header, GNUNET_NO);
2201 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2206 * Handle HELLO-message.
2208 * @param cls closure (always NULL)
2209 * @param client identification of the client
2210 * @param message the actual message
2213 handle_hello (void *cls,
2214 struct GNUNET_SERVER_Client *client,
2215 const struct GNUNET_MessageHeader *message)
2220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2221 "Received `%s' request from client\n", "HELLO");
2223 ret = process_hello (NULL, message);
2224 GNUNET_SERVER_receive_done (client, ret);
2229 * Handle SEND-message.
2231 * @param cls closure (always NULL)
2232 * @param client identification of the client
2233 * @param message the actual message
2236 handle_send (void *cls,
2237 struct GNUNET_SERVER_Client *client,
2238 const struct GNUNET_MessageHeader *message)
2240 struct TransportClient *tc;
2241 struct NeighbourList *n;
2242 const struct OutboundMessage *obm;
2243 const struct GNUNET_MessageHeader *obmm;
2247 size = ntohs (message->size);
2249 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
2252 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2255 obm = (const struct OutboundMessage *) message;
2257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2258 "Received `%s' request from client with target `%4s'\n",
2259 "SEND", GNUNET_i2s (&obm->peer));
2261 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
2262 msize = ntohs (obmm->size);
2263 if (size != msize + sizeof (struct OutboundMessage))
2266 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2269 n = find_neighbour (&obm->peer);
2271 n = setup_new_neighbour (&obm->peer);
2273 while ((tc != NULL) && (tc->client != client))
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2278 "Client asked to transmit %u-byte message of type %u to `%4s'\n",
2280 ntohs (obmm->type), GNUNET_i2s (&obm->peer));
2282 transmit_to_peer (tc, ntohl(obm->priority), obmm, GNUNET_NO, n);
2283 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2288 * Handle SET_QUOTA-message.
2290 * @param cls closure (always NULL)
2291 * @param client identification of the client
2292 * @param message the actual message
2295 handle_set_quota (void *cls,
2296 struct GNUNET_SERVER_Client *client,
2297 const struct GNUNET_MessageHeader *message)
2299 const struct QuotaSetMessage *qsm =
2300 (const struct QuotaSetMessage *) message;
2301 struct NeighbourList *n;
2302 struct TransportPlugin *p;
2303 struct ReadyList *rl;
2306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2307 "Received `%s' request from client for peer `%4s'\n",
2308 "SET_QUOTA", GNUNET_i2s (&qsm->peer));
2310 n = find_neighbour (&qsm->peer);
2313 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2317 if (n->quota_in < ntohl (qsm->quota_in))
2318 n->last_quota_update = GNUNET_TIME_absolute_get ();
2319 n->quota_in = ntohl (qsm->quota_in);
2324 p->api->set_receive_quota (p->api->cls,
2325 &qsm->peer, ntohl (qsm->quota_in));
2328 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2333 * Handle TRY_CONNECT-message.
2335 * @param cls closure (always NULL)
2336 * @param client identification of the client
2337 * @param message the actual message
2340 handle_try_connect (void *cls,
2341 struct GNUNET_SERVER_Client *client,
2342 const struct GNUNET_MessageHeader *message)
2344 const struct TryConnectMessage *tcm;
2346 tcm = (const struct TryConnectMessage *) message;
2348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2349 "Received `%s' request from client %p asking to connect to `%4s'\n",
2352 GNUNET_i2s (&tcm->peer));
2354 if (NULL == find_neighbour (&tcm->peer))
2355 setup_new_neighbour (&tcm->peer);
2358 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2359 "Client asked to connect to `%4s', but connection already exists\n",
2361 GNUNET_i2s (&tcm->peer));
2363 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2367 transmit_address_to_client (void *cls, const char *address)
2369 struct GNUNET_SERVER_TransmitContext *tc = cls;
2372 if (NULL == address)
2375 slen = strlen (address) + 1;
2376 GNUNET_SERVER_transmit_context_append (tc, address, slen,
2377 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
2378 if (NULL == address)
2379 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
2383 * Handle AddressLookup-message.
2385 * @param cls closure (always NULL)
2386 * @param client identification of the client
2387 * @param message the actual message
2390 handle_address_lookup (void *cls,
2391 struct GNUNET_SERVER_Client *client,
2392 const struct GNUNET_MessageHeader *message)
2394 const struct AddressLookupMessage *alum;
2395 struct TransportPlugin *lsPlugin;
2396 const char *nameTransport;
2397 const char *address;
2399 struct GNUNET_SERVER_TransmitContext *tc;
2401 size = ntohs (message->size);
2402 if (size < sizeof(struct AddressLookupMessage))
2404 GNUNET_break_op (0);
2405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2408 alum = (const struct AddressLookupMessage *) message;
2409 uint32_t addressLen = ntohl(alum->addrlen);
2410 if (size <= sizeof(struct AddressLookupMessage) + addressLen)
2412 GNUNET_break_op (0);
2413 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2416 address = (const char *)&alum[1];
2417 nameTransport = (const char*)&address[addressLen];
2418 if (nameTransport [size - sizeof (struct AddressLookupMessage) - addressLen -1] != '\0')
2420 GNUNET_break_op (0);
2421 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2424 struct GNUNET_TIME_Absolute timeout= GNUNET_TIME_absolute_ntoh(alum->timeout);
2425 struct GNUNET_TIME_Relative rtimeout = GNUNET_TIME_absolute_get_remaining(timeout);
2426 lsPlugin = find_transport(nameTransport);
2427 if (NULL == lsPlugin)
2429 tc = GNUNET_SERVER_transmit_context_create (client);
2430 GNUNET_SERVER_transmit_context_append (tc, NULL, 0, GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
2431 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
2434 tc = GNUNET_SERVER_transmit_context_create (client);
2435 lsPlugin->api->address_pretty_printer(cls, nameTransport,
2436 address, addressLen, GNUNET_YES, rtimeout, &transmit_address_to_client, tc);
2440 * List of handlers for the messages understood by this
2443 static struct GNUNET_SERVER_MessageHandler handlers[] = {
2444 {&handle_start, NULL,
2445 GNUNET_MESSAGE_TYPE_TRANSPORT_START, 0},
2446 {&handle_hello, NULL,
2447 GNUNET_MESSAGE_TYPE_HELLO, 0},
2448 {&handle_send, NULL,
2449 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
2450 {&handle_set_quota, NULL,
2451 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
2452 {&handle_try_connect, NULL,
2453 GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT,
2454 sizeof (struct TryConnectMessage)},
2455 {&handle_address_lookup, NULL,
2456 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
2463 * Setup the environment for this plugin.
2466 create_environment (struct TransportPlugin *plug)
2468 plug->env.cfg = cfg;
2469 plug->env.sched = sched;
2470 plug->env.my_public_key = &my_public_key;
2471 plug->env.my_private_key = my_private_key;
2472 plug->env.my_identity = &my_identity;
2473 plug->env.cls = plug;
2474 plug->env.receive = &plugin_env_receive;
2475 plug->env.notify_address = &plugin_env_notify_address;
2476 plug->env.notify_validation = &plugin_env_notify_validation;
2477 plug->env.default_quota_in = (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT + 59999) / (60 * 1000);
2478 plug->env.max_connections = max_connect_per_transport;
2483 * Start the specified transport (load the plugin).
2486 start_transport (struct GNUNET_SERVER_Handle *server, const char *name)
2488 struct TransportPlugin *plug;
2491 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2492 _("Loading `%s' transport plugin\n"), name);
2493 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
2494 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
2495 create_environment (plug);
2496 plug->short_name = GNUNET_strdup (name);
2497 plug->lib_name = libname;
2498 plug->next = plugins;
2500 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
2501 if (plug->api == NULL)
2503 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2504 _("Failed to load transport plugin for `%s'\n"), name);
2505 GNUNET_free (plug->short_name);
2506 plugins = plug->next;
2507 GNUNET_free (libname);
2514 * Called whenever a client is disconnected. Frees our
2515 * resources associated with that client.
2517 * @param cls closure
2518 * @param client identification of the client
2521 client_disconnect_notification (void *cls,
2522 struct GNUNET_SERVER_Client *client)
2524 struct TransportClient *pos;
2525 struct TransportClient *prev;
2526 struct ClientMessageQueueEntry *mqe;
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2530 "Client disconnected, cleaning up.\n");
2534 while ((pos != NULL) && (pos->client != client))
2541 while (NULL != (mqe = pos->message_queue_head))
2543 pos->message_queue_head = mqe->next;
2546 pos->message_queue_head = NULL;
2548 clients = pos->next;
2550 prev->next = pos->next;
2551 if (GNUNET_YES == pos->tcs_pending)
2561 * Function called when the service shuts down. Unloads our plugins.
2563 * @param cls closure, unused
2564 * @param tc task context (unused)
2567 unload_plugins (void *cls,
2568 const struct GNUNET_SCHEDULER_TaskContext *tc)
2570 struct TransportPlugin *plug;
2571 struct AddressList *al;
2574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2575 "Transport service is unloading plugins...\n");
2577 while (NULL != (plug = plugins))
2579 plugins = plug->next;
2580 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
2581 GNUNET_free (plug->lib_name);
2582 GNUNET_free (plug->short_name);
2583 while (NULL != (al = plug->addresses))
2585 plug->addresses = al->next;
2590 if (my_private_key != NULL)
2591 GNUNET_CRYPTO_rsa_key_free (my_private_key);
2592 GNUNET_free_non_null (our_hello);
2597 * Initiate transport service.
2599 * @param cls closure
2600 * @param s scheduler to use
2601 * @param serv the initialized server
2602 * @param c configuration to use
2606 struct GNUNET_SCHEDULER_Handle *s,
2607 struct GNUNET_SERVER_Handle *serv,
2608 const struct GNUNET_CONFIGURATION_Handle *c)
2613 unsigned long long tneigh;
2618 /* parse configuration */
2620 GNUNET_CONFIGURATION_get_value_number (c,
2625 GNUNET_CONFIGURATION_get_value_filename (c,
2627 "HOSTKEY", &keyfile)))
2629 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2631 ("Transport service is lacking key configuration settings. Exiting.\n"));
2632 GNUNET_SCHEDULER_shutdown (s);
2635 max_connect_per_transport = (uint32_t) tneigh;
2636 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2637 GNUNET_free (keyfile);
2638 if (my_private_key == NULL)
2640 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2642 ("Transport service could not access hostkey. Exiting.\n"));
2643 GNUNET_SCHEDULER_shutdown (s);
2646 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
2647 GNUNET_CRYPTO_hash (&my_public_key,
2648 sizeof (my_public_key), &my_identity.hashPubKey);
2649 /* setup notification */
2651 GNUNET_SERVER_disconnect_notify (server,
2652 &client_disconnect_notification, NULL);
2653 /* load plugins... */
2656 GNUNET_CONFIGURATION_get_value_string (c,
2657 "TRANSPORT", "PLUGINS", &plugs))
2659 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2660 _("Starting transport plugins `%s'\n"), plugs);
2661 pos = strtok (plugs, " ");
2664 start_transport (server, pos);
2666 pos = strtok (NULL, " ");
2668 GNUNET_free (plugs);
2670 GNUNET_SCHEDULER_add_delayed (sched,
2671 GNUNET_TIME_UNIT_FOREVER_REL,
2672 &unload_plugins, NULL);
2676 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2677 _("Transport service ready.\n"));
2679 /* process client requests */
2680 GNUNET_SERVER_add_handlers (server, handlers);
2685 * The main function for the transport service.
2687 * @param argc number of arguments from the command line
2688 * @param argv command line arguments
2689 * @return 0 ok, 1 on error
2692 main (int argc, char *const *argv)
2694 return (GNUNET_OK ==
2695 GNUNET_SERVICE_run (argc,
2698 GNUNET_SERVICE_OPTION_NONE,
2699 &run, NULL)) ? 0 : 1;
2702 /* end of gnunet-service-transport.c */