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, 30)
79 * How long will we allow sending of a ping to be delayed?
81 #define TRANSPORT_DEFAULT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
83 #define TRANSPORT_DEFAULT_PRIORITY 4 /* Tired of remembering arbitrary priority names */
86 * How often do we re-add (cheaper) plugins to our list of plugins
87 * to try for a given connected peer?
89 #define PLUGIN_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
92 * After how long do we expire an address in a HELLO
93 * that we just validated? This value is also used
94 * for our own addresses when we create a HELLO.
96 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
100 * List of addresses of other peers
102 struct PeerAddressList
105 * This is a linked list.
107 struct PeerAddressList *next;
110 * Pointer to the validation associated with this
111 * address. May be NULL if already validated!
113 struct ValidationAddress *validation;
116 * Which of our transport plugins does this entry
119 struct TransportPlugin *plugin;
122 * Neighbor this entry belongs to.
124 struct NeighborList *neighbor;
127 * Ready list (transport) that this peer belongs to
129 struct ReadyList *ready_list;
131 * How long until we auto-expire this address (unless it is
132 * re-confirmed by the transport)?
134 struct GNUNET_TIME_Absolute expires;
147 * Is this plugin ready to transmit to the specific target?
148 * GNUNET_NO if not. Initially, all plugins are marked ready. If a
149 * transmission is in progress, "transmit_ready" is set to
155 * What was the last latency observed for this plugin
156 * and peer? Invalid if connected is GNUNET_NO.
158 struct GNUNET_TIME_Relative latency;
161 * If we did not successfully transmit a message to the given peer
162 * via this connection during the specified time, we should consider
163 * the connection to be dead. This is used in the case that a TCP
164 * transport simply stalls writing to the stream but does not
165 * formerly get a signal that the other peer died.
167 struct GNUNET_TIME_Absolute timeout;
170 * Is this plugin currently connected? The first time
171 * we transmit or send data to a peer via a particular
172 * plugin, we set this to GNUNET_YES. If we later get
173 * an error (disconnect notification or transmission
174 * failure), we set it back to GNUNET_NO. Each time the
175 * value is set to GNUNET_YES, we increment the
176 * "connect_attempts" counter. If that one reaches a
177 * particular threshold, we consider the plugin to not
178 * be working properly at this time for the given peer
179 * and remove it from the eligible list.
184 * How often have we tried to connect using this plugin?
186 unsigned int connect_attempts;
192 * Entry in linked list of network addresses.
197 * This is a linked list.
199 struct AddressList *next;
202 * The address, actually a pointer to the end
203 * of this struct. Do not free!
208 * How long until we auto-expire this address (unless it is
209 * re-confirmed by the transport)?
211 struct GNUNET_TIME_Absolute expires;
222 * Entry in linked list of all of our plugins.
224 struct TransportPlugin
228 * This is a linked list.
230 struct TransportPlugin *next;
233 * API of the transport as returned by the plugin's
234 * initialization function.
236 struct GNUNET_TRANSPORT_PluginFunctions *api;
239 * Short name for the plugin (i.e. "tcp").
244 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
249 * List of our known addresses for this transport.
251 struct AddressList *addresses;
254 * Environment this transport service is using
257 struct GNUNET_TRANSPORT_PluginEnvironment env;
260 * ID of task that is used to clean up expired addresses.
262 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
266 * Set to GNUNET_YES if we need to scrap the existing
267 * list of "addresses" and start fresh when we receive
268 * the next address update from a transport. Set to
269 * GNUNET_NO if we should just add the new address
270 * to the list and wait for the commit call.
278 * For each neighbor we keep a list of messages
279 * that we still want to transmit to the neighbor.
285 * This is a linked list.
287 struct MessageQueue *next;
290 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
291 * stuck together in memory.
296 * Size of the message buf
298 size_t message_buf_size;
301 * Client responsible for queueing the message;
302 * used to check that a client has no two messages
303 * pending for the same target. Can be NULL.
305 struct TransportClient *client;
308 * Neighbor this entry belongs to.
310 struct NeighborList *neighbor;
313 * Plugin that we used for the transmission.
314 * NULL until we scheduled a transmission.
316 struct TransportPlugin *plugin;
319 * Internal message of the transport system that should not be
320 * included in the usual SEND-SEND_OK transmission confirmation
321 * traffic management scheme. Typically, "internal_msg" will
322 * be set whenever "client" is NULL (but it is not strictly
328 * How important is the message?
330 unsigned int priority;
333 * Using which specific address should we send this message?
335 struct PeerAddressList *specific_peer;
341 * For a given Neighbor, which plugins are available
342 * to talk to this peer and what are their costs?
347 * This is a linked list.
349 struct ReadyList *next;
352 * Which of our transport plugins does this entry
355 struct TransportPlugin *plugin;
358 * Neighbor this entry belongs to.
360 struct NeighborList *neighbor;
363 * Transport addresses, latency, and readiness for
364 * this particular plugin.
366 struct PeerAddressList *addresses;
369 * Is this plugin ready to transmit to the specific target?
370 * GNUNET_NO if not. Initially, all plugins are marked ready. If a
371 * transmission is in progress, "transmit_ready" is set to
374 int plugin_transmit_ready;
377 * Are any of our PeerAddressList addresses still connected?
379 int connected; /* FIXME: dynamically check PeerAddressList addresses when asked to! */
384 * Entry in linked list of all of our current neighbors.
390 * This is a linked list.
392 struct NeighborList *next;
395 * Which of our transports is connected to this peer
396 * and what is their status?
398 struct ReadyList *plugins;
401 * List of messages we would like to send to this peer;
402 * must contain at most one message per client.
404 struct MessageQueue *messages;
407 * Identity of this neighbor.
409 struct GNUNET_PeerIdentity id;
412 * ID of task scheduled to run when this peer is about to
413 * time out (will free resources associated with the peer).
415 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
418 * How long until we should consider this peer dead
419 * (if we don't receive another message in the
422 struct GNUNET_TIME_Absolute peer_timeout;
425 * At what time did we reset last_received last?
427 struct GNUNET_TIME_Absolute last_quota_update;
430 * At what time should we try to again add plugins to
433 struct GNUNET_TIME_Absolute retry_plugins_time;
436 * How many bytes have we received since the "last_quota_update"
439 uint64_t last_received;
442 * Global quota for inbound traffic for the neighbor in bytes/ms.
447 * How often has the other peer (recently) violated the
448 * inbound traffic limit? Incremented by 10 per violation,
449 * decremented by 1 per non-violation (for each
452 unsigned int quota_violation_count;
455 * Have we seen an ACK from this neighbor in the past?
456 * (used to make up a fake ACK for clients connecting after
457 * the neighbor connected to us).
461 /* The latency we have seen for this particular address for
462 * this particular peer. This latency may have been calculated
463 * over multiple transports. This value reflects how long it took
464 * us to receive a response when SENDING via this particular
465 * transport/neighbor/address combination!
467 struct GNUNET_TIME_RelativeNBO latency;
472 * Message used to ask a peer to validate receipt (to check an address
473 * from a HELLO). Followed by the address used. Note that the
474 * recipients response does not affirm that he has this address,
475 * only that he got the challenge message.
477 struct TransportPingMessage
481 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
483 struct GNUNET_MessageHeader header;
486 * Random challenge number (in network byte order).
488 uint32_t challenge GNUNET_PACKED;
491 * Who is the intended recipient?
493 struct GNUNET_PeerIdentity target;
499 * Message used to validate a HELLO. The challenge is included in the
500 * confirmation to make matching of replies to requests possible. The
501 * signature signs the original challenge number, our public key, the
502 * sender's address (so that the sender can check that the address we
503 * saw is plausible for him and possibly detect a MiM attack) and a
504 * timestamp (to limit replay).<p>
506 * This message is followed by the address of the
507 * client that we are observing (which is part of what
510 struct TransportPongMessage
514 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
516 struct GNUNET_MessageHeader header;
519 * For padding, always zero.
521 uint32_t reserved GNUNET_PACKED;
526 struct GNUNET_CRYPTO_RsaSignature signature;
529 * What are we signing and why?
531 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
534 * Random challenge number (in network byte order).
536 uint32_t challenge GNUNET_PACKED;
539 * Who signed this message?
541 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded signer;
544 * Size of address appended to this message
551 * Linked list of messages to be transmitted to
552 * the client. Each entry is followed by the
555 struct ClientMessageQueueEntry
558 * This is a linked list.
560 struct ClientMessageQueueEntry *next;
565 * Client connected to the transport service.
567 struct TransportClient
571 * This is a linked list.
573 struct TransportClient *next;
576 * Handle to the client.
578 struct GNUNET_SERVER_Client *client;
581 * Linked list of messages yet to be transmitted to
584 struct ClientMessageQueueEntry *message_queue_head;
587 * Tail of linked list of messages yet to be transmitted to the
590 struct ClientMessageQueueEntry *message_queue_tail;
593 * Is a call to "transmit_send_continuation" pending? If so, we
594 * must not free this struct (even if the corresponding client
595 * disconnects) and instead only remove it from the linked list and
596 * set the "client" field to NULL.
601 * Length of the list of messages pending for this client.
603 unsigned int message_count;
609 * For each HELLO, we may have to validate multiple addresses;
610 * each address gets its own request entry.
612 struct ValidationAddress
615 * This is a linked list.
617 struct ValidationAddress *next;
620 * What peer_address does this validation belong to?
622 struct PeerAddressList *peer_address;
625 * Name of the transport.
627 char *transport_name;
630 * When should this validated address expire?
632 struct GNUNET_TIME_Absolute expiration;
635 * At what time did we send this validation?
637 struct GNUNET_TIME_Absolute send_time;
640 * Challenge number we used.
645 * Set to GNUNET_YES if the challenge was met,
646 * GNUNET_SYSERR if we know it failed, GNUNET_NO
647 * if we are waiting on a response.
654 * Entry in linked list of all HELLOs awaiting validation.
656 struct ValidationList
660 * This is a linked list.
662 struct ValidationList *next;
665 * Linked list with one entry per address from the HELLO
666 * that needs to be validated.
668 struct ValidationAddress *addresses;
671 * The public key of the peer.
673 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
676 * When does this record time-out? (assuming the
677 * challenge goes unanswered)
679 struct GNUNET_TIME_Absolute timeout;
684 struct CheckHelloValidatedContext
687 * Plugin for which we are validating.
689 struct TransportPlugin *plugin;
692 * Hello that we are validating.
694 struct GNUNET_HELLO_Message *hello;
697 * Validation list being built.
699 struct ValidationList *e;
702 * Context for peerinfo iteration.
703 * NULL after we are done processing peerinfo's information.
705 struct GNUNET_PEERINFO_IteratorContext *piter;
712 * HELLOs awaiting validation.
714 static struct ValidationList *pending_validations;
719 static struct GNUNET_HELLO_Message *our_hello;
722 * "version" of "our_hello". Used to see if a given
723 * neighbor has already been sent the latest version
724 * of our HELLO message.
726 static unsigned int our_hello_version;
731 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
736 static struct GNUNET_PeerIdentity my_identity;
741 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
746 struct GNUNET_SCHEDULER_Handle *sched;
751 const struct GNUNET_CONFIGURATION_Handle *cfg;
754 * Linked list of all clients to this service.
756 static struct TransportClient *clients;
759 * All loaded plugins.
761 static struct TransportPlugin *plugins;
766 static struct GNUNET_SERVER_Handle *server;
769 * All known neighbors and their HELLOs.
771 static struct NeighborList *neighbors;
774 * Number of neighbors we'd like to have.
776 static uint32_t max_connect_per_transport;
779 * The peer specified by the given neighbor has timed-out or a plugin
780 * has disconnected. We may either need to do nothing (other plugins
781 * still up), or trigger a full disconnect and clean up. This
782 * function updates our state and do the necessary notifications.
783 * Also notifies our clients that the neighbor is now officially
786 * @param n the neighbor list entry for the peer
787 * @param check should we just check if all plugins
788 * disconnected or must we ask all plugins to
791 static void disconnect_neighbor (struct NeighborList *n, int check);
795 * Check the ready list for the given neighbor and
796 * if a plugin is ready for transmission (and if we
797 * have a message), do so!
799 * @param neighbor target peer for which to check the plugins
801 static ssize_t try_transmission_to_peer (struct NeighborList *neighbor);
805 * Find an entry in the neighbor list for a particular peer.
806 * if sender_address is not specified (NULL) then return the
807 * first matching entry. If sender_address is specified, then
808 * make sure that the address and address_len also matches.
810 * @return NULL if not found.
812 static struct NeighborList *
813 find_neighbor (const struct GNUNET_PeerIdentity *key)
815 struct NeighborList *head = neighbors;
817 while ((head != NULL) &&
818 (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
826 * Find an entry in the transport list for a particular transport.
828 * @return NULL if not found.
830 static struct TransportPlugin *
831 find_transport (const char *short_name)
833 struct TransportPlugin *head = plugins;
834 while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
841 * Update the quota values for the given neighbor now.
844 update_quota (struct NeighborList *n)
846 struct GNUNET_TIME_Relative delta;
850 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
851 if (delta.value < MIN_QUOTA_REFRESH_TIME)
852 return; /* not enough time passed for doing quota update */
853 allowed = delta.value * n->quota_in;
854 if (n->last_received < allowed)
856 remaining = allowed - n->last_received;
858 remaining /= n->quota_in;
861 if (remaining > MAX_BANDWIDTH_CARRY)
862 remaining = MAX_BANDWIDTH_CARRY;
863 n->last_received = 0;
864 n->last_quota_update = GNUNET_TIME_absolute_get ();
865 n->last_quota_update.value -= remaining;
866 if (n->quota_violation_count > 0)
867 n->quota_violation_count--;
871 n->last_received -= allowed;
872 n->last_quota_update = GNUNET_TIME_absolute_get ();
873 if (n->last_received > allowed)
875 /* more than twice the allowed rate! */
876 n->quota_violation_count += 10;
883 * Function called to notify a client about the socket
884 * being ready to queue more data. "buf" will be
885 * NULL and "size" zero if the socket was closed for
886 * writing in the meantime.
889 * @param size number of bytes available in buf
890 * @param buf where the callee should write the message
891 * @return number of bytes written to buf
894 transmit_to_client_callback (void *cls, size_t size, void *buf)
896 struct TransportClient *client = cls;
897 struct ClientMessageQueueEntry *q;
900 const struct GNUNET_MessageHeader *msg;
901 struct GNUNET_CONNECTION_TransmitHandle *th;
906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907 "Transmission to client failed, closing connection.\n");
908 /* fatal error with client, free message queue! */
909 while (NULL != (q = client->message_queue_head))
911 client->message_queue_head = q->next;
914 client->message_queue_tail = NULL;
915 client->message_count = 0;
920 while (NULL != (q = client->message_queue_head))
922 msg = (const struct GNUNET_MessageHeader *) &q[1];
923 msize = ntohs (msg->size);
924 if (msize + tsize > size)
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
928 "Transmitting message of type %u to client.\n",
931 client->message_queue_head = q->next;
933 client->message_queue_tail = NULL;
934 memcpy (&cbuf[tsize], msg, msize);
937 client->message_count--;
941 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
942 th = GNUNET_SERVER_notify_transmit_ready (client->client,
944 GNUNET_TIME_UNIT_FOREVER_REL,
945 &transmit_to_client_callback,
947 GNUNET_assert (th != NULL);
954 * Send the specified message to the specified client. Since multiple
955 * messages may be pending for the same client at a time, this code
956 * makes sure that no message is lost.
958 * @param client client to transmit the message to
959 * @param msg the message to send
960 * @param may_drop can this message be dropped if the
961 * message queue for this client is getting far too large?
964 transmit_to_client (struct TransportClient *client,
965 const struct GNUNET_MessageHeader *msg, int may_drop)
967 struct ClientMessageQueueEntry *q;
969 struct GNUNET_CONNECTION_TransmitHandle *th;
971 if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
973 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
975 ("Dropping message, have %u messages pending (%u is the soft limit)\n"),
976 client->message_count, MAX_PENDING);
977 /* TODO: call to statistics... */
980 client->message_count++;
981 msize = ntohs (msg->size);
982 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
983 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
984 memcpy (&q[1], msg, msize);
985 /* append to message queue */
986 if (client->message_queue_tail == NULL)
988 client->message_queue_tail = q;
992 client->message_queue_tail->next = q;
993 client->message_queue_tail = q;
995 if (client->message_queue_head == NULL)
997 client->message_queue_head = q;
998 th = GNUNET_SERVER_notify_transmit_ready (client->client,
1000 GNUNET_TIME_UNIT_FOREVER_REL,
1001 &transmit_to_client_callback,
1003 GNUNET_assert (th != NULL);
1009 * Find alternative plugins for communication.
1011 * @param neighbor for which neighbor should we try to find
1015 try_alternative_plugins (struct NeighborList *neighbor)
1017 struct ReadyList *rl;
1019 if ((neighbor->plugins != NULL) &&
1020 (neighbor->retry_plugins_time.value >
1021 GNUNET_TIME_absolute_get ().value))
1022 return; /* don't try right now */
1023 neighbor->retry_plugins_time
1024 = GNUNET_TIME_relative_to_absolute (PLUGIN_RETRY_FREQUENCY);
1026 rl = neighbor->plugins;
1027 #if WTF /* FIXME: What is this supposed to do? */
1030 if (rl->connect_attempts > 0)
1031 rl->connect_attempts--; /* amnesty */
1039 * Function called by the GNUNET_TRANSPORT_TransmitFunction
1040 * upon "completion" of a send request. This tells the API
1041 * that it is now legal to send another message to the given
1044 * @param cls closure, identifies the entry on the
1045 * message queue that was transmitted and the
1046 * client responsible for queueing the message
1047 * @param target the peer receiving the message
1048 * @param result GNUNET_OK on success, if the transmission
1049 * failed, we should not tell the client to transmit
1053 transmit_send_continuation (void *cls,
1054 const struct GNUNET_PeerIdentity *target,
1057 struct MessageQueue *mq = cls;
1058 /*struct ReadyList *rl;*/ /* We no longer use the ReadyList for anything here, safe to remove? */
1059 struct SendOkMessage send_ok_msg;
1060 struct NeighborList *n;
1062 GNUNET_assert (mq != NULL);
1064 GNUNET_assert (n != NULL);
1066 memcmp (&n->id, target,
1067 sizeof (struct GNUNET_PeerIdentity)));
1069 while ((rl != NULL) && (rl->plugin != mq->plugin))
1071 GNUNET_assert (rl != NULL);
1074 if (result == GNUNET_OK)
1076 mq->specific_peer->timeout =
1077 GNUNET_TIME_relative_to_absolute
1078 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1083 "Transmission to peer `%s' failed, marking connection as down.\n",
1084 GNUNET_i2s (target));
1085 mq->specific_peer->connected = GNUNET_NO;
1087 if (!mq->internal_msg)
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1091 "Setting transmit_ready on transport!\n");
1093 mq->specific_peer->transmit_ready = GNUNET_YES;
1096 if (mq->client != NULL)
1098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1099 "Notifying client %p about transmission to peer `%4s'.\n",
1100 mq->client, GNUNET_i2s (target));
1101 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1102 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1103 send_ok_msg.success = htonl (result);
1104 send_ok_msg.peer = n->id;
1105 transmit_to_client (mq->client, &send_ok_msg.header, GNUNET_NO);
1107 GNUNET_free (mq->message_buf);
1109 /* one plugin just became ready again, try transmitting
1110 another message (if available) */
1111 if (result == GNUNET_OK)
1112 try_transmission_to_peer (n);
1114 disconnect_neighbor (n, GNUNET_YES);
1120 struct PeerAddressList *
1121 find_ready_address(struct NeighborList *neighbor)
1123 struct ReadyList *head = neighbor->plugins;
1124 struct PeerAddressList *addresses;
1125 while (head != NULL)
1127 addresses = head->addresses;
1128 while ((addresses != NULL) &&
1129 ((addresses->connected != GNUNET_YES) ||
1130 (addresses->transmit_ready != GNUNET_YES)))
1132 addresses = addresses->next;
1135 if (addresses != NULL)
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1139 "Found ready address, connected is %d\n",
1140 addresses->connected);
1150 #if 0 /* Do some checks to keep everything sane, return lowest latency connection */
1153 /* set plugins that are inactive for a long time back to disconnected */
1154 if ((pos->timeout.value < now.value) && (pos->connected == GNUNET_YES))
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158 "Marking long-time inactive connection to `%4s' as down.\n",
1159 GNUNET_i2s (&neighbor->id));
1161 pos->connected = GNUNET_NO;
1163 if (GNUNET_YES == pos->transmit_ready)
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1167 "Found transmit_ready flag...\n");
1170 if (((GNUNET_YES == pos->transmit_ready) ||
1171 (mq->internal_msg)) &&
1172 (pos->connect_attempts < MAX_CONNECT_RETRY) &&
1173 ((rl == NULL) || (min_latency.value > pos->latency.value)))
1176 min_latency = pos->latency;
1184 * Check the ready list for the given neighbor and
1185 * if a plugin is ready for transmission (and if we
1186 * have a message), do so!
1189 try_transmission_to_peer (struct NeighborList *neighbor)
1191 struct GNUNET_TIME_Relative min_latency;
1192 struct ReadyList *rl;
1193 struct MessageQueue *mq;
1194 struct GNUNET_TIME_Absolute now;
1196 if (neighbor->messages == NULL)
1197 return 0; /* nothing to do */
1198 try_alternative_plugins (neighbor);
1199 min_latency = GNUNET_TIME_UNIT_FOREVER_REL;
1201 mq = neighbor->messages;
1202 now = GNUNET_TIME_absolute_get ();
1204 if (mq->specific_peer == NULL)
1205 mq->specific_peer = find_ready_address(neighbor); /* Find first available (or best!) address to transmit to */
1207 if (mq->specific_peer == NULL)
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1211 "No plugin ready to transmit message\n");
1213 return 0; /* nobody ready */
1216 rl = mq->specific_peer->ready_list;
1217 neighbor->messages = mq->next;
1218 mq->plugin = rl->plugin;
1219 if (!mq->internal_msg)
1220 mq->specific_peer->transmit_ready = GNUNET_NO;
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "Giving message of size `%u' for `%4s' to plugin `%s'\n",
1224 mq->message_buf_size,
1225 GNUNET_i2s (&neighbor->id), rl->plugin->short_name);
1228 return rl->plugin->api->send (rl->plugin->api->cls,
1231 mq->message_buf_size,
1233 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1234 mq->specific_peer->addr,
1235 mq->specific_peer->addrlen,
1237 &transmit_send_continuation, mq);
1243 * Send the specified message to the specified peer.
1245 * @param client source of the transmission request (can be NULL)
1246 * @param peer_address PeerAddressList where we should send this message
1247 * @param priority how important is the message
1248 * @param message_buf message(s) to send GNUNET_MessageHeader(s)
1249 * @param message_buf_size total size of all messages in message_buf
1250 * @param is_internal is this an internal message
1251 * @param neighbor handle to the neighbor for transmission
1254 transmit_to_peer (struct TransportClient *client,
1255 struct PeerAddressList *peer_address,
1256 unsigned int priority,
1257 const char *message_buf,
1258 size_t message_buf_size,
1259 int is_internal, struct NeighborList *neighbor)
1261 struct MessageQueue *mq;
1262 struct MessageQueue *mqe;
1267 /* check for duplicate submission */
1268 mq = neighbor->messages;
1271 if (mq->client == client)
1273 /* client transmitted to same peer twice
1274 before getting SendOk! */
1281 mq = GNUNET_malloc (sizeof (struct MessageQueue));
1282 mq->specific_peer = peer_address;
1283 mq->client = client;
1284 m = GNUNET_malloc (message_buf_size);
1285 memcpy (m, message_buf, message_buf_size);
1286 mq->message_buf = m;
1287 mq->message_buf_size = message_buf_size;
1288 mq->neighbor = neighbor;
1289 mq->internal_msg = is_internal;
1290 mq->priority = priority;
1293 mqe = neighbor->messages;
1295 while (mqe->next != NULL)
1300 neighbor->messages = mq;
1307 return try_transmission_to_peer (neighbor);
1314 struct GeneratorContext
1316 struct TransportPlugin *plug_pos;
1317 struct AddressList *addr_pos;
1318 struct GNUNET_TIME_Absolute expiration;
1326 address_generator (void *cls, size_t max, void *buf)
1328 struct GeneratorContext *gc = cls;
1331 while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
1333 gc->plug_pos = gc->plug_pos->next;
1334 gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
1336 if (NULL == gc->plug_pos)
1341 ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
1344 gc->addr_pos->addrlen, buf, max);
1345 gc->addr_pos = gc->addr_pos->next;
1351 * Construct our HELLO message from all of the addresses of
1352 * all of the transports.
1357 struct GNUNET_HELLO_Message *hello;
1358 struct TransportClient *cpos;
1359 struct NeighborList *npos;
1360 struct GeneratorContext gc;
1362 gc.plug_pos = plugins;
1363 gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
1364 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1365 hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1368 "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
1371 while (cpos != NULL)
1373 transmit_to_client (cpos,
1374 (const struct GNUNET_MessageHeader *) hello,
1379 GNUNET_free_non_null (our_hello);
1381 our_hello_version++;
1382 GNUNET_PEERINFO_add_peer (cfg, sched, &my_identity, our_hello);
1384 while (npos != NULL)
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1388 "Transmitting updated `%s' to neighbor `%4s'\n",
1389 "HELLO", GNUNET_i2s (&npos->id));
1390 #endif // FIXME: just testing
1391 //transmit_to_peer (NULL, NULL, 0,
1392 // (const char *) our_hello, GNUNET_HELLO_size(our_hello),
1393 // GNUNET_YES, npos);
1400 * Task used to clean up expired addresses for a plugin.
1402 * @param cls closure
1406 expire_address_task (void *cls,
1407 const struct GNUNET_SCHEDULER_TaskContext *tc);
1411 * Update the list of addresses for this plugin,
1412 * expiring those that are past their expiration date.
1414 * @param plugin addresses of which plugin should be recomputed?
1415 * @param fresh set to GNUNET_YES if a new address was added
1416 * and we need to regenerate the HELLO even if nobody
1420 update_addresses (struct TransportPlugin *plugin, int fresh)
1422 struct GNUNET_TIME_Relative min_remaining;
1423 struct GNUNET_TIME_Relative remaining;
1424 struct GNUNET_TIME_Absolute now;
1425 struct AddressList *pos;
1426 struct AddressList *prev;
1427 struct AddressList *next;
1430 if (plugin->address_update_task != GNUNET_SCHEDULER_NO_TASK)
1431 GNUNET_SCHEDULER_cancel (plugin->env.sched, plugin->address_update_task);
1432 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1433 now = GNUNET_TIME_absolute_get ();
1434 min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
1435 expired = GNUNET_NO;
1437 pos = plugin->addresses;
1441 if (pos->expires.value < now.value)
1443 expired = GNUNET_YES;
1445 plugin->addresses = pos->next;
1447 prev->next = pos->next;
1452 remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
1453 if (remaining.value < min_remaining.value)
1454 min_remaining = remaining;
1460 if (expired || fresh)
1462 if (min_remaining.value < GNUNET_TIME_UNIT_FOREVER_REL.value)
1463 plugin->address_update_task
1464 = GNUNET_SCHEDULER_add_delayed (plugin->env.sched,
1466 &expire_address_task, plugin);
1472 * Task used to clean up expired addresses for a plugin.
1474 * @param cls closure
1478 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1480 struct TransportPlugin *plugin = cls;
1481 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1482 update_addresses (plugin, GNUNET_NO);
1487 * Function that must be called by each plugin to notify the
1488 * transport service about the addresses under which the transport
1489 * provided by the plugin can be reached.
1491 * @param cls closure
1492 * @param name name of the transport that generated the address
1493 * @param addr one of the addresses of the host, NULL for the last address
1494 * the specific address format depends on the transport
1495 * @param addrlen length of the address
1496 * @param expires when should this address automatically expire?
1499 plugin_env_notify_address (void *cls,
1503 struct GNUNET_TIME_Relative expires)
1505 struct TransportPlugin *p = cls;
1506 struct AddressList *al;
1507 struct GNUNET_TIME_Absolute abex;
1509 abex = GNUNET_TIME_relative_to_absolute (expires);
1510 GNUNET_assert (p == find_transport (name));
1515 if ((addrlen == al->addrlen) && (0 == memcmp (addr, &al[1], addrlen)))
1517 if (al->expires.value < abex.value)
1524 al = GNUNET_malloc (sizeof (struct AddressList) + addrlen);
1526 al->next = p->addresses;
1529 al->addrlen = addrlen;
1530 memcpy (&al[1], addr, addrlen);
1531 update_addresses (p, GNUNET_YES);
1536 * Notify all of our clients about a peer connecting.
1539 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
1540 struct GNUNET_TIME_Relative latency)
1542 struct ConnectInfoMessage cim;
1543 struct TransportClient *cpos;
1545 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
1546 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
1547 cim.quota_out = htonl (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT / (60 * 1000));
1548 cim.latency = GNUNET_TIME_relative_hton (latency);
1549 memcpy (&cim.id, peer, sizeof (struct GNUNET_PeerIdentity));
1551 while (cpos != NULL)
1553 transmit_to_client (cpos, &cim.header, GNUNET_NO);
1560 * Notify all of our clients about a peer disconnecting.
1563 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
1565 struct DisconnectInfoMessage dim;
1566 struct TransportClient *cpos;
1568 dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
1569 dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1570 dim.reserved = htonl (0);
1571 memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
1573 while (cpos != NULL)
1575 transmit_to_client (cpos, &dim.header, GNUNET_NO);
1582 * Copy any validated addresses to buf.
1584 * @return 0 once all addresses have been
1588 list_validated_addresses (void *cls, size_t max, void *buf)
1590 struct ValidationAddress **va = cls;
1593 while ((NULL != *va) && ((*va)->ok != GNUNET_YES))
1597 ret = GNUNET_HELLO_add_address ((*va)->transport_name,
1599 (*va)->peer_address->addr, (*va)->peer_address->addrlen, buf, max);
1606 * HELLO validation cleanup task.
1609 cleanup_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1611 struct ValidationAddress *va;
1612 struct ValidationList *pos;
1613 struct ValidationList *prev;
1614 struct GNUNET_TIME_Absolute now;
1615 struct GNUNET_TIME_Absolute first;
1616 struct GNUNET_HELLO_Message *hello;
1617 struct GNUNET_PeerIdentity pid;
1618 struct NeighborList *n;
1620 now = GNUNET_TIME_absolute_get ();
1622 pos = pending_validations;
1625 if (pos->timeout.value < now.value)
1628 pending_validations = pos->next;
1630 prev->next = pos->next;
1631 va = pos->addresses;
1632 hello = GNUNET_HELLO_create (&pos->publicKey,
1633 &list_validated_addresses, &va);
1634 GNUNET_CRYPTO_hash (&pos->publicKey,
1636 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1640 "Creating persistent `%s' message for peer `%4s' based on confirmed addresses.\n",
1641 "HELLO", GNUNET_i2s (&pid));
1643 GNUNET_PEERINFO_add_peer (cfg, sched, &pid, hello);
1644 n = find_neighbor (&pid);
1647 try_transmission_to_peer (n);
1649 GNUNET_free (hello);
1650 while (NULL != (va = pos->addresses))
1652 pos->addresses = va->next;
1653 GNUNET_free (va->transport_name);
1658 pos = pending_validations;
1667 /* finally, reschedule cleanup if needed; list is
1668 ordered by timeout, so we need the last element... */
1669 if (NULL != pending_validations)
1671 first = pending_validations->timeout;
1672 pos = pending_validations;
1675 first = GNUNET_TIME_absolute_min (first, pos->timeout);
1678 GNUNET_SCHEDULER_add_delayed (sched,
1679 GNUNET_TIME_absolute_get_remaining
1680 (first), &cleanup_validation, NULL);
1686 * Function that will be called if we receive a validation
1687 * of an address challenge that we transmitted to another
1688 * peer. Note that the validation should only be considered
1689 * acceptable if the challenge matches AND if the sender
1690 * address is at least a plausible address for this peer
1691 * (otherwise we may be seeing a MiM attack).
1693 * @param cls closure
1694 * @param name name of the transport that generated the address
1695 * @param peer who responded to our challenge
1696 * @param challenge the challenge number we presumably used
1697 * @param sender_addr string describing our sender address (as observed
1698 * by the other peer in human-readable format)
1701 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
1702 const struct GNUNET_PeerIdentity *peer,
1703 const char *sender_address,
1704 size_t sender_address_len)
1706 unsigned int not_done;
1708 struct ValidationList *pos;
1709 struct ValidationAddress *va;
1710 struct GNUNET_PeerIdentity id;
1711 struct TransportPongMessage *pong = (struct TransportPongMessage *)message;
1713 unsigned int challenge = ntohl(pong->challenge);
1714 pos = pending_validations;
1718 GNUNET_CRYPTO_hash (&pos->publicKey,
1720 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1722 if (0 == memcmp (peer, &id, sizeof (struct GNUNET_PeerIdentity)))
1729 /* TODO: call statistics (unmatched PONG) */
1730 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1732 ("Received validation response but have no record of any validation request for `%4s' (out of %d). Ignoring.\n"),
1733 GNUNET_i2s (peer), count);
1737 matched = GNUNET_NO;
1738 va = pos->addresses;
1741 if (va->challenge == challenge)
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1745 "Confirmed validity of address, peer `%4s' has address `%s'.\n",
1747 GNUNET_a2s ((const struct sockaddr *) sender_address,
1748 sender_address_len));
1750 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
1752 ("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"),
1753 GNUNET_a2s ((const struct sockaddr *) &pong[1],
1754 ntohs(pong->addrlen)), va->transport_name);
1755 va->ok = GNUNET_YES;
1757 GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1758 matched = GNUNET_YES;
1759 va->peer_address->connected = GNUNET_YES;
1760 va->peer_address->latency = GNUNET_TIME_absolute_get_difference(va->peer_address->validation->send_time, GNUNET_TIME_absolute_get());
1762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1763 "Confirmed validity of address, peer `%4s' has address `%s', latency of %llu\n",
1765 GNUNET_a2s ((const struct sockaddr *) sender_address,
1766 sender_address_len), (unsigned long long)va->peer_address->latency.value);
1768 va->peer_address->transmit_ready = GNUNET_YES;
1769 va->peer_address->expires = GNUNET_TIME_relative_to_absolute
1770 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1772 if (va->ok != GNUNET_YES)
1776 if (GNUNET_NO == matched)
1778 /* TODO: call statistics (unmatched PONG) */
1779 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1781 ("Received `%s' message but have no record of a matching `%s' message. Ignoring.\n"),
1787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1788 "All addresses validated, will now construct `%s' for `%4s'.\n",
1789 "HELLO", GNUNET_i2s (peer));
1791 pos->timeout.value = 0;
1792 GNUNET_SCHEDULER_add_with_priority (sched,
1793 GNUNET_SCHEDULER_PRIORITY_IDLE,
1794 &cleanup_validation, NULL);
1800 * Add an entry for each of our transport plugins
1801 * (that are able to send) to the list of plugins
1802 * for this neighbor.
1804 * @param neighbor to initialize
1807 add_plugins (struct NeighborList *neighbor)
1809 struct TransportPlugin *tp;
1810 struct ReadyList *rl;
1812 neighbor->retry_plugins_time
1813 = GNUNET_TIME_relative_to_absolute (PLUGIN_RETRY_FREQUENCY);
1817 if (tp->api->send != NULL)
1819 rl = GNUNET_malloc (sizeof (struct ReadyList));
1820 rl->next = neighbor->plugins;
1821 neighbor->plugins = rl;
1823 rl->neighbor = neighbor;
1824 rl->addresses = NULL;
1831 neighbor_timeout_task (void *cls,
1832 const struct GNUNET_SCHEDULER_TaskContext *tc)
1834 struct NeighborList *n = cls;
1837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1838 "Neighbor `%4s' has timed out!\n", GNUNET_i2s (&n->id));
1840 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1841 disconnect_neighbor (n, GNUNET_NO);
1845 * Create a fresh entry in our neighbor list for the given peer.
1846 * Will try to transmit our current HELLO to the new neighbor. Also
1847 * notifies our clients about the new "connection".
1849 * @param peer the peer for which we create the entry
1850 * @return the new neighbor list entry
1852 static struct NeighborList *
1853 setup_new_neighbor (const struct GNUNET_PeerIdentity *peer)
1855 struct NeighborList *n;
1857 GNUNET_assert (our_hello != NULL);
1858 n = GNUNET_malloc (sizeof (struct NeighborList));
1859 n->next = neighbors;
1862 n->last_quota_update = GNUNET_TIME_absolute_get ();
1864 GNUNET_TIME_relative_to_absolute
1865 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1866 n->quota_in = (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT + 59999) / (60 * 1000);
1868 n->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
1869 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1870 &neighbor_timeout_task, n);
1871 transmit_to_peer (NULL, NULL, 0,
1872 (const char *) our_hello, GNUNET_HELLO_size(our_hello),
1874 notify_clients_connect (peer, GNUNET_TIME_UNIT_FOREVER_REL);
1878 static struct PeerAddressList *
1879 add_peer_address(struct NeighborList *neighbor, const char *addr, size_t addrlen)
1881 /* FIXME: should return a list of PeerAddressLists, support for multiple transports! */
1882 struct ReadyList *head = neighbor->plugins;
1883 struct PeerAddressList * new_address;
1885 GNUNET_assert(addr != NULL);
1887 while (head != NULL)
1889 new_address = GNUNET_malloc(sizeof(struct PeerAddressList));
1890 new_address->addr = GNUNET_malloc(addrlen);
1891 memcpy(new_address->addr, addr, addrlen);
1892 new_address->addrlen = addrlen;
1893 new_address->connect_attempts = 0;
1894 new_address->connected = GNUNET_YES; /* Set connected to GNUNET_YES, assuming that we're good */
1895 new_address->expires = GNUNET_TIME_relative_to_absolute
1896 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1897 new_address->latency = GNUNET_TIME_relative_get_forever();
1898 new_address->neighbor = neighbor;
1899 new_address->plugin = head->plugin;
1900 new_address->transmit_ready = GNUNET_YES;
1901 new_address->timeout = GNUNET_TIME_relative_to_absolute
1902 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); /* FIXME: Do we need this? */
1903 new_address->ready_list = head;
1904 new_address->next = head->addresses;
1905 head->addresses = new_address;
1912 static struct PeerAddressList *
1913 find_peer_address(struct NeighborList *neighbor, const char *addr, size_t addrlen)
1915 struct ReadyList *head = neighbor->plugins;
1916 struct PeerAddressList *address_head;
1917 while (head != NULL)
1919 address_head = head->addresses;
1920 while ((address_head != NULL) &&
1921 (address_head->addrlen != addrlen) &&
1922 (memcmp(address_head->addr, addr, addrlen) != 0))
1924 address_head = address_head->next;
1926 if (address_head != NULL)
1927 return address_head;
1935 * Append the given address to the list of entries
1936 * that need to be validated.
1939 run_validation (void *cls,
1941 struct GNUNET_TIME_Absolute expiration,
1942 const void *addr, size_t addrlen)
1944 struct ValidationList *e = cls;
1945 struct TransportPlugin *tp;
1946 struct ValidationAddress *va;
1947 struct GNUNET_PeerIdentity id;
1948 struct NeighborList *neighbor;
1949 struct PeerAddressList *peer_address;
1951 struct TransportPingMessage *ping;
1956 tp = find_transport (tname);
1959 GNUNET_log (GNUNET_ERROR_TYPE_INFO |
1960 GNUNET_ERROR_TYPE_BULK,
1962 ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
1966 GNUNET_CRYPTO_hash (&e->publicKey,
1968 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1972 "Scheduling validation of address `%s' via `%s' for `%4s'\n",
1973 GNUNET_a2s (addr, addrlen), tname, GNUNET_i2s (&id));
1975 va = GNUNET_malloc (sizeof (struct ValidationAddress));
1976 va->next = e->addresses;
1978 va->transport_name = GNUNET_strdup (tname);
1979 va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1981 va->send_time = GNUNET_TIME_absolute_get();
1983 neighbor = find_neighbor(&id);
1985 if (neighbor == NULL)
1986 neighbor = setup_new_neighbor(&id);
1988 peer_address = find_peer_address(neighbor, addr, addrlen);
1989 if (peer_address == NULL)
1991 peer_address = add_peer_address(neighbor, addr, addrlen);
1994 GNUNET_assert(peer_address != NULL);
1996 va->peer_address = peer_address; /* Back pointer FIXME: remove this nonsense! */
1997 peer_address->validation = va;
1999 hello_size = GNUNET_HELLO_size(our_hello);
2000 tsize = sizeof(struct TransportPingMessage) + hello_size;
2002 message_buf = GNUNET_malloc(tsize);
2004 ping = GNUNET_malloc(sizeof(struct TransportPingMessage));
2005 ping->challenge = htonl(va->challenge);
2006 ping->header.size = htons(sizeof(struct TransportPingMessage));
2007 ping->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
2008 memcpy(&ping->target, &id, sizeof(struct GNUNET_PeerIdentity));
2011 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "hello size is %d, ping size is %d, total size is %d", hello_size, sizeof(struct TransportPingMessage), tsize);
2013 memcpy(message_buf, our_hello, hello_size);
2014 memcpy(&message_buf[hello_size], ping, sizeof(struct TransportPingMessage));
2017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending ping message of size %d to address `%s' via `%s' for `%4s'\n",
2018 tsize, GNUNET_a2s (addr, addrlen), tname, GNUNET_i2s (&id));
2020 sent = transmit_to_peer(NULL, peer_address, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2021 message_buf, tsize, GNUNET_NO, neighbor);
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport returned %d from send!\n", sent);
2028 GNUNET_free(message_buf);
2034 * @param cls handle to the plugin (for sending)
2035 * @param target the peer identity of the peer we are sending to
2036 * @param challenge the challenge number
2037 * @param timeout how long to await validation?
2038 * @param addr the address to validate
2039 * @param addrlen the length of the address
2041 * Perform address validation, which means sending a PING PONG to
2042 * the address via the transport plugin. If not validated, then
2043 * do not count this as a good peer/address...
2045 * Currently this function is not used, ping/pongs get sent from the
2046 * run_validation function. Haven't decided yet how to do this.
2049 validate_address (void *cls, struct ValidationAddress *va,
2050 const struct GNUNET_PeerIdentity *target,
2051 struct GNUNET_TIME_Relative timeout,
2052 const void *addr, size_t addrlen)
2054 /* struct Plugin *plugin = cls;
2055 int challenge = va->challenge; */
2063 * Check if addresses in validated hello "h" overlap with
2064 * those in "chvc->hello" and update "chvc->hello" accordingly,
2065 * removing those addresses that have already been validated.
2068 check_hello_validated (void *cls,
2069 const struct GNUNET_PeerIdentity *peer,
2070 const struct GNUNET_HELLO_Message *h, uint32_t trust)
2072 struct CheckHelloValidatedContext *chvc = cls;
2073 struct ValidationAddress *va;
2074 struct TransportPlugin *tp;
2077 struct GNUNET_PeerIdentity apeer;
2079 first_call = GNUNET_NO;
2080 if (chvc->e == NULL)
2083 first_call = GNUNET_YES;
2084 chvc->e = GNUNET_malloc (sizeof (struct ValidationList));
2085 GNUNET_assert (GNUNET_OK ==
2086 GNUNET_HELLO_get_key (h != NULL ? h : chvc->hello,
2087 &chvc->e->publicKey));
2089 GNUNET_TIME_relative_to_absolute (HELLO_VERIFICATION_TIMEOUT);
2090 chvc->e->next = pending_validations;
2091 pending_validations = chvc->e;
2096 GNUNET_HELLO_iterate_new_addresses (chvc->hello,
2098 GNUNET_TIME_absolute_get (),
2099 &run_validation, chvc->e);
2101 else if (GNUNET_YES == first_call)
2103 /* no existing HELLO, all addresses are new */
2104 GNUNET_HELLO_iterate_addresses (chvc->hello,
2105 GNUNET_NO, &run_validation, chvc->e);
2109 return; /* wait for next call */
2110 /* finally, transmit validation attempts */
2111 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (chvc->hello, &apeer));
2113 va = chvc->e->addresses;
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2119 "Establishing `%s' connection to validate `%s' address `%s' of `%4s'\n",
2122 GNUNET_a2s ((const struct sockaddr *) va->peer_address->addr,
2123 va->peer_address->addrlen), GNUNET_i2s (&apeer));
2125 tp = find_transport (va->transport_name);
2126 GNUNET_assert (tp != NULL);
2127 /* This validation should happen inside the transport, not from the plugin! */
2128 va->ok = GNUNET_SYSERR;
2133 GNUNET_SCHEDULER_add_delayed (sched,
2134 GNUNET_TIME_absolute_get_remaining (chvc->
2136 &cleanup_validation, NULL);
2142 * Process HELLO-message.
2144 * @param plugin transport involved, may be NULL
2145 * @param message the actual message
2146 * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
2149 process_hello (struct TransportPlugin *plugin,
2150 const struct GNUNET_MessageHeader *message)
2152 struct ValidationList *e;
2154 struct GNUNET_PeerIdentity target;
2155 const struct GNUNET_HELLO_Message *hello;
2156 struct CheckHelloValidatedContext *chvc;
2157 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
2159 hsize = ntohs (message->size);
2160 if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
2161 (hsize < sizeof (struct GNUNET_MessageHeader)))
2164 return GNUNET_SYSERR;
2166 /* first, check if load is too high */
2167 if (GNUNET_OS_load_cpu_get (cfg) > 100)
2169 /* TODO: call to stats? */
2172 hello = (const struct GNUNET_HELLO_Message *) message;
2173 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
2175 GNUNET_break_op (0);
2176 return GNUNET_SYSERR;
2178 GNUNET_CRYPTO_hash (&publicKey,
2179 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2180 &target.hashPubKey);
2182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2183 "Processing `%s' message for `%4s' of size %d (hsize is %d)\n",
2184 "HELLO", GNUNET_i2s (&target), GNUNET_HELLO_size(hello), hsize);
2186 /* check if a HELLO for this peer is already on the validation list */
2187 e = pending_validations;
2190 if (0 == memcmp (&e->publicKey,
2193 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
2195 /* TODO: call to stats? */
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 "`%s' message for peer `%4s' is already pending; ignoring new message\n",
2199 "HELLO", GNUNET_i2s (&target));
2205 chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
2206 chvc->plugin = plugin;
2207 chvc->hello = (struct GNUNET_HELLO_Message *) &chvc[1];
2209 memcpy (chvc->hello, hello, hsize);
2210 /* finally, check if HELLO was previously validated
2211 (continuation will then schedule actual validation) */
2212 chvc->piter = GNUNET_PEERINFO_iterate (cfg,
2216 HELLO_VERIFICATION_TIMEOUT,
2217 &check_hello_validated, chvc);
2223 * The peer specified by the given neighbor has timed-out or a plugin
2224 * has disconnected. We may either need to do nothing (other plugins
2225 * still up), or trigger a full disconnect and clean up. This
2226 * function updates our state and do the necessary notifications.
2227 * Also notifies our clients that the neighbor is now officially
2230 * @param n the neighbor list entry for the peer
2231 * @param check should we just check if all plugins
2232 * disconnected or must we ask all plugins to
2236 disconnect_neighbor (struct NeighborList *n, int check)
2238 struct ReadyList *rpos;
2239 struct NeighborList *npos;
2240 struct NeighborList *nprev;
2241 struct MessageQueue *mq;
2242 struct PeerAddressList *peer_addresses;
2244 if (GNUNET_YES == check)
2247 while (NULL != rpos)
2249 peer_addresses = rpos->addresses;
2250 while (peer_addresses != NULL)
2252 if (GNUNET_YES == peer_addresses->connected)
2253 return; /* still connected */
2254 peer_addresses = peer_addresses->next;
2261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2262 "Disconnecting from `%4s'\n", GNUNET_i2s (&n->id));
2264 /* remove n from neighbors list */
2267 while ((npos != NULL) && (npos != n))
2272 GNUNET_assert (npos != NULL);
2274 neighbors = n->next;
2276 nprev->next = n->next;
2278 /* notify all clients about disconnect */
2279 notify_clients_disconnect (&n->id);
2281 /* clean up all plugins, cancel connections and pending transmissions */
2282 while (NULL != (rpos = n->plugins))
2284 n->plugins = rpos->next;
2285 GNUNET_assert (rpos->neighbor == n);
2286 if (GNUNET_YES == rpos->connected)
2287 rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
2291 /* free all messages on the queue */
2292 while (NULL != (mq = n->messages))
2294 n->messages = mq->next;
2295 GNUNET_assert (mq->neighbor == n);
2298 if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2299 GNUNET_SCHEDULER_cancel (sched, n->timeout_task);
2300 /* finally, free n itself */
2306 * We have received a PING message from someone. Need to send a PONG message
2307 * in response to the peer by any means necessary. Of course, with something
2308 * like TCP where a connection exists, we may want to send it that way. But
2309 * we may not be able to make that distinction...
2311 static int handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
2312 const struct GNUNET_PeerIdentity *peer,
2313 const char *sender_address,
2314 size_t sender_address_len)
2316 struct TransportPlugin *plugin = cls;
2317 struct TransportPingMessage *ping;
2318 struct TransportPongMessage *pong;
2319 struct PeerAddressList *peer_address;
2321 struct NeighborList *n;
2322 pong = GNUNET_malloc(sizeof(struct TransportPongMessage));
2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2326 "Processing `%s' from `%s'\n",
2327 "PING", GNUNET_a2s ((const struct sockaddr *)sender_address, sender_address_len));
2330 msize = ntohs (message->size);
2331 if (msize < sizeof (struct TransportPingMessage))
2333 GNUNET_break_op (0);
2334 return GNUNET_SYSERR;
2336 ping = (struct TransportPingMessage *) message;
2337 if (0 != memcmp (&ping->target,
2338 plugin->env.my_identity,
2339 sizeof (struct GNUNET_PeerIdentity)))
2341 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2342 _("Received `%s' message not destined for me!\n"), "PING");
2343 return GNUNET_SYSERR;
2346 msize -= sizeof (struct TransportPingMessage);
2348 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len);
2349 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len);
2350 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
2351 pong->purpose.size =
2352 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2354 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + sender_address_len);
2355 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING);
2356 pong->challenge = ping->challenge;
2357 pong->addrlen = htons(sender_address_len);
2359 memcpy(&pong->signer, &my_public_key, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2360 memcpy (&pong[1], sender_address, sender_address_len);
2361 GNUNET_assert (GNUNET_OK ==
2362 GNUNET_CRYPTO_rsa_sign (my_private_key,
2363 &pong->purpose, &pong->signature));
2365 n = find_neighbor(peer);
2367 n = setup_new_neighbor(peer);
2369 peer_address = find_peer_address(n, sender_address, sender_address_len);
2370 if (peer_address == NULL)
2371 peer_address = add_peer_address(n, sender_address, sender_address_len);
2373 transmit_to_peer(NULL, NULL, TRANSPORT_DEFAULT_PRIORITY, (char *)pong, ntohs(pong->header.size), GNUNET_NO, n);
2380 * Function called by the plugin for each received message.
2381 * Update data volumes, possibly notify plugins about
2382 * reducing the rate at which they read from the socket
2383 * and generally forward to our receive callback.
2385 * @param cls the "struct TransportPlugin *" we gave to the plugin
2386 * @param message the message, NULL if peer was disconnected
2387 * @param distance the transport cost to this peer (not latency!)
2388 * @param sender_address the address that the sender reported
2389 * (opaque to transport service)
2390 * @param sender_address_len the length of the sender address
2391 * @param peer (claimed) identity of the other peer
2392 * @return the new service_context that the plugin should use
2393 * for future receive calls for messages from this
2398 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
2399 const struct GNUNET_MessageHeader *message,
2400 unsigned int distance, const char *sender_address,
2401 size_t sender_address_len)
2403 struct ReadyList *service_context;
2404 struct TransportPlugin *plugin = cls;
2405 struct TransportClient *cpos;
2406 struct InboundMessage *im;
2407 struct PeerAddressList *peer_address;
2409 struct NeighborList *n;
2411 n = find_neighbor (peer);
2414 if (message == NULL)
2415 return; /* disconnect of peer already marked down */
2416 n = setup_new_neighbor (peer);
2420 peer_address = find_peer_address(n, sender_address, sender_address_len);
2421 if (peer_address == NULL)
2422 peer_address = add_peer_address(n, sender_address, sender_address_len);
2424 service_context = n->plugins;
2425 while ((service_context != NULL) && (plugin != service_context->plugin))
2426 service_context = service_context->next;
2427 GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
2428 if (message == NULL)
2431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2432 "Receive failed from `%4s', triggering disconnect\n",
2433 GNUNET_i2s (&n->id));
2435 /* TODO: call stats */
2436 if (service_context != NULL)
2437 service_context->connected = GNUNET_NO;
2438 disconnect_neighbor (n, GNUNET_YES);
2442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2443 "Processing message of type `%u' received by plugin...\n",
2444 ntohs (message->type));
2446 if (service_context != NULL)
2448 if (service_context->connected == GNUNET_NO)
2450 /*service_context->connected = GNUNET_YES;*/
2451 /* FIXME: What to do here? Should we use these as well, to specify some Address
2452 * in the AddressList should be available?
2454 peer_address->transmit_ready = GNUNET_YES;
2455 peer_address->connect_attempts++;
2457 peer_address->timeout
2459 GNUNET_TIME_relative_to_absolute
2460 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2462 /* update traffic received amount ... */
2463 msize = ntohs (message->size);
2464 n->last_received += msize;
2465 GNUNET_SCHEDULER_cancel (sched, n->timeout_task);
2467 GNUNET_TIME_relative_to_absolute
2468 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2470 GNUNET_SCHEDULER_add_delayed (sched,
2471 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2472 &neighbor_timeout_task, n);
2474 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2476 /* dropping message due to frequent inbound volume violations! */
2477 GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
2478 GNUNET_ERROR_TYPE_BULK,
2480 ("Dropping incoming message due to repeated bandwidth quota violations.\n"));
2481 /* TODO: call stats */
2482 GNUNET_assert ((service_context == NULL) ||
2483 (NULL != service_context->neighbor));
2486 switch (ntohs (message->type))
2488 case GNUNET_MESSAGE_TYPE_HELLO:
2490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2491 "Receiving `%s' message from `%4s'.\n", "HELLO",
2494 process_hello (plugin, message);
2496 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
2497 handle_ping(plugin, message, peer, sender_address, sender_address_len);
2499 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
2500 handle_pong(plugin, message, peer, sender_address, sender_address_len);
2504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2505 "Received REAL MESSAGE type %u from `%4s', sending to all clients.\n",
2506 ntohs (message->type), GNUNET_i2s (peer));
2508 /* transmit message to all clients */
2509 im = GNUNET_malloc (sizeof (struct InboundMessage) + msize);
2510 im->header.size = htons (sizeof (struct InboundMessage) + msize);
2511 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
2512 im->latency = n->latency;
2514 memcpy (&im[1], message, msize);
2517 while (cpos != NULL)
2519 transmit_to_client (cpos, &im->header, GNUNET_YES);
2524 GNUNET_assert ((service_context == NULL) ||
2525 (NULL != service_context->neighbor));
2530 * Handle START-message. This is the first message sent to us
2531 * by any client which causes us to add it to our list.
2533 * @param cls closure (always NULL)
2534 * @param client identification of the client
2535 * @param message the actual message
2538 handle_start (void *cls,
2539 struct GNUNET_SERVER_Client *client,
2540 const struct GNUNET_MessageHeader *message)
2542 struct TransportClient *c;
2543 struct ConnectInfoMessage cim;
2544 struct NeighborList *n;
2545 struct InboundMessage *im;
2546 struct GNUNET_MessageHeader *ack;
2549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2550 "Received `%s' request from client\n", "START");
2555 if (c->client == client)
2557 /* client already on our list! */
2559 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2564 c = GNUNET_malloc (sizeof (struct TransportClient));
2568 if (our_hello != NULL)
2571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2572 "Sending our own `%s' to new client\n", "HELLO");
2574 transmit_to_client (c,
2575 (const struct GNUNET_MessageHeader *) our_hello,
2577 /* tell new client about all existing connections */
2578 cim.header.size = htons (sizeof (struct ConnectInfoMessage));
2579 cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2581 htonl (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT / (60 * 1000));
2582 cim.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO); /* FIXME? */
2583 im = GNUNET_malloc (sizeof (struct InboundMessage) +
2584 sizeof (struct GNUNET_MessageHeader));
2585 im->header.size = htons (sizeof (struct InboundMessage) +
2586 sizeof (struct GNUNET_MessageHeader));
2587 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
2588 im->latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO); /* FIXME? */
2589 ack = (struct GNUNET_MessageHeader *) &im[1];
2590 ack->size = htons (sizeof (struct GNUNET_MessageHeader));
2591 ack->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ACK);
2592 for (n = neighbors; n != NULL; n = n->next)
2595 transmit_to_client (c, &cim.header, GNUNET_NO);
2596 if (n->received_pong)
2599 transmit_to_client (c, &im->header, GNUNET_NO);
2606 fprintf(stderr, "Our hello is NULL!\n");
2608 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2613 * Handle HELLO-message.
2615 * @param cls closure (always NULL)
2616 * @param client identification of the client
2617 * @param message the actual message
2620 handle_hello (void *cls,
2621 struct GNUNET_SERVER_Client *client,
2622 const struct GNUNET_MessageHeader *message)
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2628 "Received `%s' request from client\n", "HELLO");
2630 ret = process_hello (NULL, message);
2631 GNUNET_SERVER_receive_done (client, ret);
2636 * Handle SEND-message.
2638 * @param cls closure (always NULL)
2639 * @param client identification of the client
2640 * @param message the actual message
2643 handle_send (void *cls,
2644 struct GNUNET_SERVER_Client *client,
2645 const struct GNUNET_MessageHeader *message)
2647 struct TransportClient *tc;
2648 struct NeighborList *n;
2649 const struct OutboundMessage *obm;
2650 const struct GNUNET_MessageHeader *obmm;
2654 size = ntohs (message->size);
2656 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
2659 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2662 obm = (const struct OutboundMessage *) message;
2664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2665 "Received `%s' request from client with target `%4s'\n",
2666 "SEND", GNUNET_i2s (&obm->peer));
2668 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
2669 msize = ntohs (obmm->size);
2670 if (size != msize + sizeof (struct OutboundMessage))
2673 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2676 n = find_neighbor (&obm->peer);
2678 n = setup_new_neighbor (&obm->peer); /* But won't ever add address, we have none! */
2680 while ((tc != NULL) && (tc->client != client))
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2685 "Client asked to transmit %u-byte message of type %u to `%4s'\n",
2687 ntohs (obmm->type), GNUNET_i2s (&obm->peer));
2689 transmit_to_peer (tc, NULL, ntohl (obm->priority), (char *)obmm, ntohs (obmm->size), GNUNET_NO, n);
2690 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2695 * Handle SET_QUOTA-message.
2697 * @param cls closure (always NULL)
2698 * @param client identification of the client
2699 * @param message the actual message
2702 handle_set_quota (void *cls,
2703 struct GNUNET_SERVER_Client *client,
2704 const struct GNUNET_MessageHeader *message)
2706 const struct QuotaSetMessage *qsm =
2707 (const struct QuotaSetMessage *) message;
2708 struct NeighborList *n;
2709 struct TransportPlugin *p;
2710 struct ReadyList *rl;
2713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2714 "Received `%s' request from client for peer `%4s'\n",
2715 "SET_QUOTA", GNUNET_i2s (&qsm->peer));
2717 n = find_neighbor (&qsm->peer);
2720 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2724 if (n->quota_in < ntohl (qsm->quota_in))
2725 n->last_quota_update = GNUNET_TIME_absolute_get ();
2726 n->quota_in = ntohl (qsm->quota_in);
2731 p->api->set_receive_quota (p->api->cls,
2732 &qsm->peer, ntohl (qsm->quota_in));
2735 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2740 * Handle TRY_CONNECT-message.
2742 * @param cls closure (always NULL)
2743 * @param client identification of the client
2744 * @param message the actual message
2747 handle_try_connect (void *cls,
2748 struct GNUNET_SERVER_Client *client,
2749 const struct GNUNET_MessageHeader *message)
2751 const struct TryConnectMessage *tcm;
2753 tcm = (const struct TryConnectMessage *) message;
2755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2756 "Received `%s' request from client %p asking to connect to `%4s'\n",
2757 "TRY_CONNECT", client, GNUNET_i2s (&tcm->peer));
2759 if (NULL == find_neighbor (&tcm->peer))
2760 setup_new_neighbor (&tcm->peer);
2763 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2764 "Client asked to connect to `%4s', but connection already exists\n",
2765 "TRY_CONNECT", GNUNET_i2s (&tcm->peer));
2767 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2771 transmit_address_to_client (void *cls, const char *address)
2773 struct GNUNET_SERVER_TransmitContext *tc = cls;
2776 if (NULL == address)
2779 slen = strlen (address) + 1;
2780 GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
2781 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
2782 if (NULL == address)
2783 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
2787 * Handle AddressLookup-message.
2789 * @param cls closure (always NULL)
2790 * @param client identification of the client
2791 * @param message the actual message
2794 handle_address_lookup (void *cls,
2795 struct GNUNET_SERVER_Client *client,
2796 const struct GNUNET_MessageHeader *message)
2798 const struct AddressLookupMessage *alum;
2799 struct TransportPlugin *lsPlugin;
2800 const char *nameTransport;
2801 const char *address;
2803 struct GNUNET_SERVER_TransmitContext *tc;
2805 size = ntohs (message->size);
2806 if (size < sizeof (struct AddressLookupMessage))
2808 GNUNET_break_op (0);
2809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2812 alum = (const struct AddressLookupMessage *) message;
2813 uint32_t addressLen = ntohl (alum->addrlen);
2814 if (size <= sizeof (struct AddressLookupMessage) + addressLen)
2816 GNUNET_break_op (0);
2817 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2820 address = (const char *) &alum[1];
2821 nameTransport = (const char *) &address[addressLen];
2823 [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
2825 GNUNET_break_op (0);
2826 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2829 struct GNUNET_TIME_Absolute timeout =
2830 GNUNET_TIME_absolute_ntoh (alum->timeout);
2831 struct GNUNET_TIME_Relative rtimeout =
2832 GNUNET_TIME_absolute_get_remaining (timeout);
2833 lsPlugin = find_transport (nameTransport);
2834 if (NULL == lsPlugin)
2836 tc = GNUNET_SERVER_transmit_context_create (client);
2837 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
2838 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
2839 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
2842 tc = GNUNET_SERVER_transmit_context_create (client);
2843 lsPlugin->api->address_pretty_printer (cls, nameTransport,
2844 address, addressLen, GNUNET_YES,
2846 &transmit_address_to_client, tc);
2850 * List of handlers for the messages understood by this
2853 static struct GNUNET_SERVER_MessageHandler handlers[] = {
2854 {&handle_start, NULL,
2855 GNUNET_MESSAGE_TYPE_TRANSPORT_START, 0},
2856 {&handle_hello, NULL,
2857 GNUNET_MESSAGE_TYPE_HELLO, 0},
2858 {&handle_send, NULL,
2859 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
2860 {&handle_set_quota, NULL,
2861 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
2862 {&handle_try_connect, NULL,
2863 GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT,
2864 sizeof (struct TryConnectMessage)},
2865 {&handle_address_lookup, NULL,
2866 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
2873 * Setup the environment for this plugin.
2876 create_environment (struct TransportPlugin *plug)
2878 plug->env.cfg = cfg;
2879 plug->env.sched = sched;
2880 plug->env.my_identity = &my_identity;
2881 plug->env.cls = plug;
2882 plug->env.receive = &plugin_env_receive;
2883 plug->env.notify_address = &plugin_env_notify_address;
2884 plug->env.default_quota_in =
2885 (GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT + 59999) / (60 * 1000);
2886 plug->env.max_connections = max_connect_per_transport;
2891 * Start the specified transport (load the plugin).
2894 start_transport (struct GNUNET_SERVER_Handle *server, const char *name)
2896 struct TransportPlugin *plug;
2899 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2900 _("Loading `%s' transport plugin\n"), name);
2901 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
2902 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
2903 create_environment (plug);
2904 plug->short_name = GNUNET_strdup (name);
2905 plug->lib_name = libname;
2906 plug->next = plugins;
2908 plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
2909 if (plug->api == NULL)
2911 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2912 _("Failed to load transport plugin for `%s'\n"), name);
2913 GNUNET_free (plug->short_name);
2914 plugins = plug->next;
2915 GNUNET_free (libname);
2922 * Called whenever a client is disconnected. Frees our
2923 * resources associated with that client.
2925 * @param cls closure
2926 * @param client identification of the client
2929 client_disconnect_notification (void *cls,
2930 struct GNUNET_SERVER_Client *client)
2932 struct TransportClient *pos;
2933 struct TransportClient *prev;
2934 struct ClientMessageQueueEntry *mqe;
2939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2940 "Client disconnected, cleaning up.\n");
2944 while ((pos != NULL) && (pos->client != client))
2951 while (NULL != (mqe = pos->message_queue_head))
2953 pos->message_queue_head = mqe->next;
2956 pos->message_queue_head = NULL;
2958 clients = pos->next;
2960 prev->next = pos->next;
2961 if (GNUNET_YES == pos->tcs_pending)
2971 * Function called when the service shuts down. Unloads our plugins.
2973 * @param cls closure, unused
2974 * @param tc task context (unused)
2977 unload_plugins (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2979 struct TransportPlugin *plug;
2980 struct AddressList *al;
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2984 "Transport service is unloading plugins...\n");
2986 while (NULL != (plug = plugins))
2988 plugins = plug->next;
2989 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
2990 GNUNET_free (plug->lib_name);
2991 GNUNET_free (plug->short_name);
2992 while (NULL != (al = plug->addresses))
2994 plug->addresses = al->next;
2999 if (my_private_key != NULL)
3000 GNUNET_CRYPTO_rsa_key_free (my_private_key);
3001 GNUNET_free_non_null (our_hello);
3006 * Initiate transport service.
3008 * @param cls closure
3009 * @param s scheduler to use
3010 * @param serv the initialized server
3011 * @param c configuration to use
3015 struct GNUNET_SCHEDULER_Handle *s,
3016 struct GNUNET_SERVER_Handle *serv,
3017 const struct GNUNET_CONFIGURATION_Handle *c)
3022 unsigned long long tneigh;
3027 /* parse configuration */
3029 GNUNET_CONFIGURATION_get_value_number (c,
3034 GNUNET_CONFIGURATION_get_value_filename (c,
3036 "HOSTKEY", &keyfile)))
3038 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3040 ("Transport service is lacking key configuration settings. Exiting.\n"));
3041 GNUNET_SCHEDULER_shutdown (s);
3044 max_connect_per_transport = (uint32_t) tneigh;
3045 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3046 GNUNET_free (keyfile);
3047 if (my_private_key == NULL)
3049 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3051 ("Transport service could not access hostkey. Exiting.\n"));
3052 GNUNET_SCHEDULER_shutdown (s);
3055 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3056 GNUNET_CRYPTO_hash (&my_public_key,
3057 sizeof (my_public_key), &my_identity.hashPubKey);
3058 /* setup notification */
3060 GNUNET_SERVER_disconnect_notify (server,
3061 &client_disconnect_notification, NULL);
3062 /* load plugins... */
3065 GNUNET_CONFIGURATION_get_value_string (c,
3066 "TRANSPORT", "PLUGINS", &plugs))
3068 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3069 _("Starting transport plugins `%s'\n"), plugs);
3070 pos = strtok (plugs, " ");
3073 start_transport (server, pos);
3075 pos = strtok (NULL, " ");
3077 GNUNET_free (plugs);
3079 GNUNET_SCHEDULER_add_delayed (sched,
3080 GNUNET_TIME_UNIT_FOREVER_REL,
3081 &unload_plugins, NULL);
3086 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport service ready.\n"));
3088 /* process client requests */
3089 GNUNET_SERVER_add_handlers (server, handlers);
3094 * The main function for the transport service.
3096 * @param argc number of arguments from the command line
3097 * @param argv command line arguments
3098 * @return 0 ok, 1 on error
3101 main (int argc, char *const *argv)
3103 return (GNUNET_OK ==
3104 GNUNET_SERVICE_run (argc,
3107 GNUNET_SERVICE_OPTION_NONE,
3108 &run, NULL)) ? 0 : 1;
3111 /* end of gnunet-service-transport.c */