2 This file is part of GNUnet.
3 (C) 2010-2013 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 3, 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_clients.c
23 * @brief plugin management API
24 * @author Christian Grothoff
27 #include "gnunet-service-transport_blacklist.h"
28 #include "gnunet-service-transport_clients.h"
29 #include "gnunet-service-transport_hello.h"
30 #include "gnunet-service-transport_neighbours.h"
31 #include "gnunet-service-transport_plugins.h"
32 #include "gnunet-service-transport_validation.h"
33 #include "gnunet-service-transport_manipulation.h"
34 #include "gnunet-service-transport.h"
35 #include "transport.h"
39 * How many messages can we have pending for a given client process
40 * before we start to drop incoming messages? We typically should
41 * have only one client and so this would be the primary buffer for
42 * messages, so the number should be chosen rather generously.
44 * The expectation here is that most of the time the queue is large
45 * enough so that a drop is virtually never required. Note that
46 * this value must be about as large as 'TOTAL_MSGS' in the
47 * 'test_transport_api_reliability.c', otherwise that testcase may
50 #define MAX_PENDING (128 * 1024)
54 * Linked list of messages to be transmitted to the client. Each
55 * entry is followed by the actual message.
57 struct ClientMessageQueueEntry
60 * This is a doubly-linked list.
62 struct ClientMessageQueueEntry *next;
65 * This is a doubly-linked list.
67 struct ClientMessageQueueEntry *prev;
72 * Client connected to the transport service.
74 struct TransportClient
78 * This is a doubly-linked list.
80 struct TransportClient *next;
83 * This is a doubly-linked list.
85 struct TransportClient *prev;
88 * Handle to the client.
90 struct GNUNET_SERVER_Client *client;
93 * Linked list of messages yet to be transmitted to
96 struct ClientMessageQueueEntry *message_queue_head;
99 * Tail of linked list of messages yet to be transmitted to the
102 struct ClientMessageQueueEntry *message_queue_tail;
105 * Current transmit request handle.
107 struct GNUNET_SERVER_TransmitHandle *th;
110 * Length of the list of messages pending for this client.
112 unsigned int message_count;
115 * Is this client interested in payload messages?
121 * Context for address to string operations
123 struct AddressToStringContext
126 * This is a doubly-linked list.
128 struct AddressToStringContext *next;
131 * This is a doubly-linked list.
133 struct AddressToStringContext *prev;
136 * Transmission context
138 struct GNUNET_SERVER_TransmitContext* tc;
142 * Client monitoring changes of active addresses of our neighbours.
144 struct MonitoringClient
147 * This is a doubly-linked list.
149 struct MonitoringClient *next;
152 * This is a doubly-linked list.
154 struct MonitoringClient *prev;
157 * Handle to the client.
159 struct GNUNET_SERVER_Client *client;
162 * Peer identity to monitor the addresses of.
163 * Zero to monitor all neighrours.
165 struct GNUNET_PeerIdentity peer;
171 * Head of linked list of all clients to this service.
173 static struct TransportClient *clients_head;
176 * Tail of linked list of all clients to this service.
178 static struct TransportClient *clients_tail;
181 * Head of linked list of all pending address iterations
183 struct AddressToStringContext *a2s_head;
186 * Tail of linked list of all pending address iterations
188 struct AddressToStringContext *a2s_tail;
191 * Head of linked list of monitoring clients.
193 static struct MonitoringClient *peer_monitoring_clients_head;
196 * Tail of linked list of monitoring clients.
198 static struct MonitoringClient *peer_monitoring_clients_tail;
201 * Head of linked list of validation monitoring clients.
203 static struct MonitoringClient *val_monitoring_clients_head;
206 * Tail of linked list of validation monitoring clients.
208 static struct MonitoringClient *val_monitoring_clients_tail;
211 * Notification context, to send updates on changes to active addresses
214 static struct GNUNET_SERVER_NotificationContext *peer_nc;
217 * Notification context, to send updates on changes to active addresses
220 static struct GNUNET_SERVER_NotificationContext *val_nc;
223 * Find the internal handle associated with the given client handle
225 * @param client server's client handle to look up
226 * @return internal client handle
228 static struct TransportClient *
229 lookup_client (struct GNUNET_SERVER_Client *client)
231 struct TransportClient *tc;
233 for (tc = clients_head; NULL != tc; tc = tc->next)
234 if (tc->client == client)
241 * Create the internal handle for the given server client handle
243 * @param client server's client handle to create our internal handle for
244 * @return fresh internal client handle
246 static struct TransportClient *
247 setup_client (struct GNUNET_SERVER_Client *client)
249 struct TransportClient *tc;
251 GNUNET_assert (NULL == lookup_client (client));
252 tc = GNUNET_new (struct TransportClient);
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Client %p connected\n",
262 * Find the handle to the monitoring client associated with the given
265 * @param head the head of the client queue to look in
266 * @param client server's client handle to look up
267 * @return handle to the monitoring client
269 static struct MonitoringClient *
270 lookup_monitoring_client (struct MonitoringClient *head,
271 struct GNUNET_SERVER_Client *client)
273 struct MonitoringClient *mc;
275 for (mc = head; NULL != mc; mc = mc->next)
276 if (mc->client == client)
283 * Setup a new monitoring client using the given server client handle and
286 * @param client server's client handle to create our internal handle for
287 * @param peer identity of the peer to monitor the addresses of,
288 * zero to monitor all neighrours.
289 * @return handle to the new monitoring client
291 static struct MonitoringClient *
292 setup_peer_monitoring_client (struct GNUNET_SERVER_Client *client,
293 struct GNUNET_PeerIdentity *peer)
295 struct MonitoringClient *mc;
296 static struct GNUNET_PeerIdentity all_zeros;
298 GNUNET_assert (lookup_monitoring_client (peer_monitoring_clients_head, client) == NULL);
299 mc = GNUNET_new (struct MonitoringClient);
302 GNUNET_CONTAINER_DLL_insert (peer_monitoring_clients_head, peer_monitoring_clients_tail, mc);
303 GNUNET_SERVER_notification_context_add (peer_nc, client);
305 if (0 != memcmp (peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
306 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
307 "Client %p started monitoring of the peer `%s'\n",
308 mc, GNUNET_i2s (peer));
310 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
311 "Client %p started monitoring all peers\n", mc);
316 * Setup a new monitoring client using the given server client handle and
319 * @param client server's client handle to create our internal handle for
320 * @param peer identity of the peer to monitor the addresses of,
321 * zero to monitor all neighrours.
322 * @return handle to the new monitoring client
324 static struct MonitoringClient *
325 setup_val_monitoring_client (struct GNUNET_SERVER_Client *client,
326 struct GNUNET_PeerIdentity *peer)
328 struct MonitoringClient *mc;
329 static struct GNUNET_PeerIdentity all_zeros;
331 GNUNET_assert (lookup_monitoring_client (val_monitoring_clients_head, client) == NULL);
332 mc = GNUNET_new (struct MonitoringClient);
335 GNUNET_CONTAINER_DLL_insert (val_monitoring_clients_head, val_monitoring_clients_tail, mc);
336 GNUNET_SERVER_notification_context_add (val_nc, client);
338 if (0 != memcmp (peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
339 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
340 "Client %p started monitoring of the peer `%s'\n",
341 mc, GNUNET_i2s (peer));
343 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
344 "Client %p started monitoring all peers\n", mc);
350 * Function called to notify a client about the socket being ready to
351 * queue more data. "buf" will be NULL and "size" zero if the socket
352 * was closed for writing in the meantime.
355 * @param size number of bytes available in @a buf
356 * @param buf where the callee should write the message
357 * @return number of bytes written to @a buf
360 transmit_to_client_callback (void *cls, size_t size, void *buf)
362 struct TransportClient *tc = cls;
363 struct ClientMessageQueueEntry *q;
364 const struct GNUNET_MessageHeader *msg;
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "Transmission to client failed, closing connection.\n");
378 while (NULL != (q = tc->message_queue_head))
380 msg = (const struct GNUNET_MessageHeader *) &q[1];
381 msize = ntohs (msg->size);
382 if (msize + tsize > size)
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "Transmitting message of type %u to client %p.\n",
386 ntohs (msg->type), tc);
387 GNUNET_CONTAINER_DLL_remove (tc->message_queue_head,
388 tc->message_queue_tail,
391 memcpy (&cbuf[tsize], msg, msize);
397 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
399 GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
400 GNUNET_TIME_UNIT_FOREVER_REL,
401 &transmit_to_client_callback, tc);
402 GNUNET_assert (NULL != tc->th);
409 * Queue the given message for transmission to the given client
411 * @param tc target of the message
412 * @param msg message to transmit
413 * @param may_drop #GNUNET_YES if the message can be dropped
416 unicast (struct TransportClient *tc,
417 const struct GNUNET_MessageHeader *msg,
420 struct ClientMessageQueueEntry *q;
429 if ((tc->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
431 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
432 _("Dropping message of type %u and size %u, have %u/%u messages pending\n"),
437 GNUNET_STATISTICS_update (GST_stats,
439 ("# messages dropped due to slow client"), 1,
443 msize = ntohs (msg->size);
444 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
445 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
446 memcpy (&q[1], msg, msize);
447 GNUNET_CONTAINER_DLL_insert_tail (tc->message_queue_head,
448 tc->message_queue_tail, q);
453 GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
454 GNUNET_TIME_UNIT_FOREVER_REL,
455 &transmit_to_client_callback, tc);
456 GNUNET_assert (NULL != tc->th);
461 * Called whenever a client is disconnected. Frees our
462 * resources associated with that client.
465 * @param client identification of the client
468 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
470 struct TransportClient *tc;
471 struct MonitoringClient *mc;
472 struct ClientMessageQueueEntry *mqe;
476 mc = lookup_monitoring_client (peer_monitoring_clients_head, client);
479 GNUNET_CONTAINER_DLL_remove (peer_monitoring_clients_head,
480 peer_monitoring_clients_tail,
484 mc = lookup_monitoring_client (val_monitoring_clients_head, client);
487 GNUNET_CONTAINER_DLL_remove (val_monitoring_clients_head,
488 val_monitoring_clients_tail,
492 tc = lookup_client (client);
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
496 "Client %p disconnected, cleaning up.\n", tc);
497 while (NULL != (mqe = tc->message_queue_head))
499 GNUNET_CONTAINER_DLL_remove (tc->message_queue_head, tc->message_queue_tail,
504 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, tc);
507 GNUNET_SERVER_notify_transmit_ready_cancel (tc->th);
510 GNUNET_break (0 == tc->message_count);
516 * Function called for each of our connected neighbours. Notify the
517 * client about the existing neighbour.
519 * @param cls the `struct TransportClient *` to notify
520 * @param peer identity of the neighbour
521 * @param address the address
522 * @param state the current state of the peer
523 * @param state_timeout the time out for the state
524 * @param bandwidth_in inbound bandwidth in NBO
525 * @param bandwidth_out outbound bandwidth in NBO
528 notify_client_about_neighbour (void *cls,
529 const struct GNUNET_PeerIdentity *peer,
530 const struct GNUNET_HELLO_Address *address,
531 enum GNUNET_TRANSPORT_PeerState state,
532 struct GNUNET_TIME_Absolute state_timeout,
533 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
534 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
536 struct TransportClient *tc = cls;
537 struct ConnectInfoMessage *cim;
538 size_t size = sizeof (struct ConnectInfoMessage);
539 char buf[size] GNUNET_ALIGN;
541 if (GNUNET_NO == GST_neighbours_test_connected (peer))
544 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
545 cim = (struct ConnectInfoMessage *) buf;
546 cim->header.size = htons (size);
547 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
549 cim->quota_in = bandwidth_in;
550 cim->quota_out = bandwidth_out;
551 unicast (tc, &cim->header, GNUNET_NO);
556 * Initialize a normal client. We got a start message from this
557 * client, add him to the list of clients for broadcasting of inbound
561 * @param client the client
562 * @param message the start message that was sent
565 clients_handle_start (void *cls, struct GNUNET_SERVER_Client *client,
566 const struct GNUNET_MessageHeader *message)
568 const struct StartMessage *start;
569 struct TransportClient *tc;
572 tc = lookup_client (client);
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
575 "Client %p sent START\n", tc);
578 /* got 'start' twice from the same client, not allowed */
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
580 "TransportClient %p ServerClient %p sent multiple START messages\n",
583 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
586 start = (const struct StartMessage *) message;
587 options = ntohl (start->options);
588 if ((0 != (1 & options)) &&
590 memcmp (&start->self, &GST_my_identity,
591 sizeof (struct GNUNET_PeerIdentity))))
593 /* client thinks this is a different peer, reject */
594 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
596 ("Rejecting control connection from peer `%s', which is not me!\n"),
597 GNUNET_i2s (&start->self));
598 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
601 tc = setup_client (client);
602 tc->send_payload = (0 != (2 & options));
603 unicast (tc, GST_hello_get (), GNUNET_NO);
604 GST_neighbours_iterate (¬ify_client_about_neighbour, tc);
605 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, tc);
606 GNUNET_SERVER_receive_done (client, GNUNET_OK);
611 * Client sent us a HELLO. Process the request.
614 * @param client the client
615 * @param message the HELLO message
618 clients_handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
619 const struct GNUNET_MessageHeader *message)
621 GST_validation_handle_hello (message);
622 GNUNET_SERVER_receive_done (client, GNUNET_OK);
627 * Closure for 'handle_send_transmit_continuation'
629 struct SendTransmitContinuationContext
632 * Client that made the request.
634 struct GNUNET_SERVER_Client *client;
637 * Peer that was the target.
639 struct GNUNET_PeerIdentity target;
644 * Function called after the transmission is done. Notify the client that it is
645 * OK to send the next message.
648 * @param success #GNUNET_OK on success, #GNUNET_NO on failure, #GNUNET_SYSERR if we're not connected
649 * @param bytes_payload bytes payload sent
650 * @param bytes_on_wire bytes sent on wire
653 handle_send_transmit_continuation (void *cls, int success,
654 size_t bytes_payload,
655 size_t bytes_on_wire)
657 struct SendTransmitContinuationContext *stcc = cls;
658 struct SendOkMessage send_ok_msg;
660 if (GNUNET_OK == success)
661 GST_neighbours_notify_payload_sent (&stcc->target, bytes_payload);
663 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
664 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
665 send_ok_msg.bytes_msg = htonl (bytes_payload);
666 send_ok_msg.bytes_physical = htonl (bytes_on_wire);
667 send_ok_msg.success = htonl (success);
668 send_ok_msg.latency =
669 GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
670 send_ok_msg.peer = stcc->target;
671 GST_clients_unicast (stcc->client, &send_ok_msg.header, GNUNET_NO);
672 GNUNET_SERVER_client_drop (stcc->client);
678 * Client asked for transmission to a peer. Process the request.
681 * @param client the client
682 * @param message the send message that was sent
685 clients_handle_send (void *cls,
686 struct GNUNET_SERVER_Client *client,
687 const struct GNUNET_MessageHeader *message)
689 const struct OutboundMessage *obm;
690 const struct GNUNET_MessageHeader *obmm;
691 struct SendTransmitContinuationContext *stcc;
694 struct TransportClient *tc;
696 tc = lookup_client (client);
699 /* client asked for transmission before 'START' */
701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
705 size = ntohs (message->size);
707 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
710 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
713 obm = (const struct OutboundMessage *) message;
714 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
715 msize = size - sizeof (struct OutboundMessage);
716 if (msize < sizeof (struct GNUNET_MessageHeader))
719 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724 "Received `%s' request from client with target `%4s' and first message of type %u and total size %u\n",
726 GNUNET_i2s (&obm->peer),
729 if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
731 /* not connected, not allowed to send; can happen due to asynchronous operations */
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 "Could not send message to peer `%s': not connected\n",
734 GNUNET_i2s (&obm->peer));
735 GNUNET_STATISTICS_update (GST_stats,
737 ("# bytes payload dropped (other peer was not connected)"),
739 GNUNET_SERVER_receive_done (client, GNUNET_OK);
742 GNUNET_SERVER_receive_done (client, GNUNET_OK);
743 stcc = GNUNET_new (struct SendTransmitContinuationContext);
744 stcc->target = obm->peer;
745 stcc->client = client;
746 GNUNET_SERVER_client_keep (client);
747 GST_manipulation_send (&obm->peer, obmm, msize,
748 GNUNET_TIME_relative_ntoh (obm->timeout),
749 &handle_send_transmit_continuation, stcc);
754 * Try to initiate a connection to the given peer if the blacklist
757 * @param cls closure (unused, NULL)
758 * @param peer identity of peer that was tested
759 * @param result #GNUNET_OK if the connection is allowed,
763 try_connect_if_allowed (void *cls,
764 const struct GNUNET_PeerIdentity *peer,
767 if (GNUNET_OK != result)
769 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
770 _("Blacklist refuses connection attempt to peer `%s'\n"),
772 return; /* not allowed */
775 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
776 _("Blacklist allows connection attempt to peer `%s'\n"),
779 GST_neighbours_try_connect (peer);
784 * Handle request connect message
786 * @param cls closure (always NULL)
787 * @param client identification of the client
788 * @param message the actual message
791 clients_handle_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
792 const struct GNUNET_MessageHeader *message)
794 const struct TransportRequestConnectMessage *trcm =
795 (const struct TransportRequestConnectMessage *) message;
797 if (GNUNET_YES == ntohl (trcm->connect))
799 GNUNET_STATISTICS_update (GST_stats,
801 ("# REQUEST CONNECT messages received"), 1,
804 if (0 == memcmp (&trcm->peer, &GST_my_identity,
805 sizeof (struct GNUNET_PeerIdentity)))
808 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
809 "Received a request connect message myself `%s'\n",
810 GNUNET_i2s (&trcm->peer));
814 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
815 _("Received a request connect message for peer `%s'\n"),
816 GNUNET_i2s (&trcm->peer));
818 (void) GST_blacklist_test_allowed (&trcm->peer, NULL, &try_connect_if_allowed,
822 else if (GNUNET_NO == ntohl (trcm->connect))
824 GNUNET_STATISTICS_update (GST_stats,
826 ("# REQUEST DISCONNECT messages received"), 1,
829 if (0 == memcmp (&trcm->peer, &GST_my_identity,
830 sizeof (struct GNUNET_PeerIdentity)))
833 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
834 "Received a request disconnect message myself `%s'\n",
835 GNUNET_i2s (&trcm->peer));
839 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
840 _("Received a request disconnect message for peer `%s'\n"),
841 GNUNET_i2s (&trcm->peer));
842 (void) GST_neighbours_force_disconnect (&trcm->peer);
848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
851 GNUNET_SERVER_receive_done (client, GNUNET_OK);
856 * Take the given address and append it to the set of results sent back to
859 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
860 * @param buf text to transmit
863 transmit_address_to_client (void *cls, const char *buf)
865 struct AddressToStringContext *actx = cls;
866 struct AddressToStringResultMessage *atsm;
871 len = sizeof (struct AddressToStringResultMessage) + strlen (buf) + 1;
872 atsm = GNUNET_malloc (len);
873 atsm->header.size = ntohs (len);
874 atsm->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
875 atsm->res = htonl (GNUNET_YES);
876 atsm->addr_len = htonl (strlen (buf) + 1);
877 memcpy (&atsm[1], buf, strlen (buf) + 1);
881 len = sizeof (struct AddressToStringResultMessage);
882 atsm = GNUNET_malloc (len);
883 atsm->header.size = ntohs (len);
884 atsm->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
885 atsm->res = htonl (GNUNET_NO);
886 atsm->addr_len = htonl (0);
891 /* Address could not be converted */
892 GNUNET_SERVER_transmit_context_append_message (actx->tc, (const struct GNUNET_MessageHeader *)atsm);
893 GNUNET_SERVER_transmit_context_run (actx->tc, GNUNET_TIME_UNIT_FOREVER_REL);
894 GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, actx);
898 GNUNET_SERVER_transmit_context_append_message (actx->tc, (const struct GNUNET_MessageHeader *) atsm);
903 * Client asked to resolve an address. Process the request.
906 * @param client the client
907 * @param message the resolution request
910 clients_handle_address_to_string (void *cls,
911 struct GNUNET_SERVER_Client *client,
912 const struct GNUNET_MessageHeader *message)
914 const struct AddressLookupMessage *alum;
915 struct GNUNET_TRANSPORT_PluginFunctions *papi;
916 const char *plugin_name;
918 uint32_t address_len;
920 struct GNUNET_SERVER_TransmitContext *tc;
921 struct AddressToStringContext *actx;
922 struct AddressToStringResultMessage atsm;
923 struct GNUNET_TIME_Relative rtimeout;
926 size = ntohs (message->size);
927 if (size < sizeof (struct AddressLookupMessage))
930 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
933 alum = (const struct AddressLookupMessage *) message;
934 address_len = ntohs (alum->addrlen);
935 if (size <= sizeof (struct AddressLookupMessage) + address_len)
938 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
941 address = (const char *) &alum[1];
942 plugin_name = (const char *) &address[address_len];
943 if ('\0' != plugin_name[size - sizeof (struct AddressLookupMessage) - address_len - 1])
946 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
949 rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
950 numeric = ntohs (alum->numeric_only);
951 tc = GNUNET_SERVER_transmit_context_create (client);
952 papi = GST_plugins_printer_find (plugin_name);
955 atsm.header.size = ntohs (sizeof (struct AddressToStringResultMessage));
956 atsm.header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
957 atsm.res = htonl (GNUNET_NO);
958 atsm.addr_len = htonl (0);
959 GNUNET_SERVER_transmit_context_append_message (tc,
960 (const struct GNUNET_MessageHeader *) &atsm);
961 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
964 actx = GNUNET_new (struct AddressToStringContext);
966 GNUNET_CONTAINER_DLL_insert (a2s_head, a2s_tail, actx);
967 GNUNET_SERVER_disable_receive_done_warning (client);
968 papi->address_pretty_printer (papi->cls, plugin_name, address, address_len,
969 numeric, rtimeout, &transmit_address_to_client,
975 * Compose #PeerIterateResponseMessage using the given peer and address.
977 * @param peer identity of the peer
978 * @param address the address, NULL on disconnect
979 * @return composed message
981 static struct PeerIterateResponseMessage *
982 compose_address_iterate_response_message (const struct GNUNET_PeerIdentity *peer,
983 const struct GNUNET_HELLO_Address *address)
985 struct PeerIterateResponseMessage *msg;
991 GNUNET_assert (NULL != peer);
994 tlen = strlen (address->transport_name) + 1;
995 alen = address->address_length;
999 size = (sizeof (struct PeerIterateResponseMessage) + alen + tlen);
1000 msg = GNUNET_malloc (size);
1001 msg->header.size = htons (size);
1003 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
1004 msg->reserved = htonl (0);
1006 msg->addrlen = htonl (alen);
1007 msg->pluginlen = htonl (tlen);
1009 if (NULL != address)
1011 msg->local_address_info = htonl((uint32_t) address->local_info);
1012 addr = (char *) &msg[1];
1013 memcpy (addr, address->address, alen);
1014 memcpy (&addr[alen], address->transport_name, tlen);
1020 * Compose #PeerIterateResponseMessage using the given peer and address.
1022 * @param peer identity of the peer
1023 * @param address the address, NULL on disconnect
1024 * @return composed message
1026 static struct ValidationIterateResponseMessage *
1027 compose_validation_iterate_response_message (const struct GNUNET_PeerIdentity *peer,
1028 const struct GNUNET_HELLO_Address *address)
1030 struct ValidationIterateResponseMessage *msg;
1036 GNUNET_assert (NULL != peer);
1037 if (NULL != address)
1039 tlen = strlen (address->transport_name) + 1;
1040 alen = address->address_length;
1044 size = (sizeof (struct ValidationIterateResponseMessage) + alen + tlen);
1045 msg = GNUNET_malloc (size);
1046 msg->header.size = htons (size);
1048 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE);
1049 msg->reserved = htonl (0);
1051 msg->addrlen = htonl (alen);
1052 msg->pluginlen = htonl (tlen);
1054 if (NULL != address)
1056 msg->local_address_info = htonl((uint32_t) address->local_info);
1057 addr = (char *) &msg[1];
1058 memcpy (addr, address->address, alen);
1059 memcpy (&addr[alen], address->transport_name, tlen);
1064 struct IterationContext
1066 struct GNUNET_SERVER_TransmitContext *tc;
1068 struct GNUNET_PeerIdentity id;
1074 * Output information of validation entries to the given client.
1076 * @param cls the 'struct IterationContext'
1077 * @param peer identity of the neighbour
1078 * @param address the address
1079 * @param last_validation point in time when last validation was performed
1080 * @param valid_until point in time how long address is valid
1081 * @param next_validation point in time when next validation will be performed
1082 * @param state state of validation notification
1085 send_validation_information (void *cls,
1086 const struct GNUNET_PeerIdentity *peer,
1087 const struct GNUNET_HELLO_Address *address,
1088 struct GNUNET_TIME_Absolute last_validation,
1089 struct GNUNET_TIME_Absolute valid_until,
1090 struct GNUNET_TIME_Absolute next_validation,
1091 enum GNUNET_TRANSPORT_ValidationState state)
1093 struct IterationContext *pc = cls;
1094 struct ValidationIterateResponseMessage *msg;
1096 if ( (GNUNET_YES == pc->all) ||
1097 (0 == memcmp (peer, &pc->id, sizeof (pc->id))) )
1099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1100 "Sending information about for validation entry for peer `%s' using address `%s'\n",
1101 GNUNET_i2s(peer), (address != NULL) ? GST_plugins_a2s (address) : "<none>");
1102 msg = compose_validation_iterate_response_message (peer, address);
1103 msg->last_validation = GNUNET_TIME_absolute_hton(last_validation);
1104 msg->valid_until = GNUNET_TIME_absolute_hton(valid_until);
1105 msg->next_validation = GNUNET_TIME_absolute_hton(next_validation);
1106 msg->state = htonl ((uint32_t) state);
1107 GNUNET_SERVER_transmit_context_append_message (pc->tc, &msg->header);
1114 * Output information of neighbours to the given client.
1116 * @param cls the 'struct PeerIterationContext'
1117 * @param peer identity of the neighbour
1118 * @param address the address
1119 * @param state current state this peer is in
1120 * @param state_timeout timeout for the current state of the peer
1121 * @param bandwidth_in inbound quota in NBO
1122 * @param bandwidth_out outbound quota in NBO
1125 send_peer_information (void *cls,
1126 const struct GNUNET_PeerIdentity *peer,
1127 const struct GNUNET_HELLO_Address *address,
1128 enum GNUNET_TRANSPORT_PeerState state,
1129 struct GNUNET_TIME_Absolute state_timeout,
1130 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1131 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1133 struct IterationContext *pc = cls;
1134 struct PeerIterateResponseMessage *msg;
1136 if ( (GNUNET_YES == pc->all) ||
1137 (0 == memcmp (peer, &pc->id, sizeof (pc->id))) )
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 "Sending information about `%s' using address `%s' in state `%s'\n",
1142 (address != NULL) ? GST_plugins_a2s (address) : "<none>",
1143 GNUNET_TRANSPORT_ps2s (state));
1144 msg = compose_address_iterate_response_message (peer, address);
1145 msg->state = htonl (state);
1146 msg->state_timeout = GNUNET_TIME_absolute_hton(state_timeout);
1147 GNUNET_SERVER_transmit_context_append_message (pc->tc, &msg->header);
1155 * Client asked to obtain information about a specific or all peers
1156 * Process the request.
1159 * @param client the client
1160 * @param message the peer address information request
1163 clients_handle_monitor_peers (void *cls, struct GNUNET_SERVER_Client *client,
1164 const struct GNUNET_MessageHeader *message)
1166 static struct GNUNET_PeerIdentity all_zeros;
1167 struct GNUNET_SERVER_TransmitContext *tc;
1168 struct PeerMonitorMessage *msg;
1169 struct IterationContext pc;
1171 if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST)
1174 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1177 if (ntohs (message->size) != sizeof (struct PeerMonitorMessage))
1180 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1183 msg = (struct PeerMonitorMessage *) message;
1184 if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
1185 (NULL != lookup_monitoring_client (peer_monitoring_clients_head, client)) )
1187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1188 "ServerClient %p tried to start monitoring twice\n",
1191 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1194 GNUNET_SERVER_disable_receive_done_warning (client);
1195 pc.tc = tc = GNUNET_SERVER_transmit_context_create (client);
1197 /* Send initial list */
1198 if (0 == memcmp (&msg->peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
1200 /* iterate over all neighbours */
1201 pc.all = GNUNET_YES;
1206 /* just return one neighbour */
1210 GST_neighbours_iterate (&send_peer_information, &pc);
1212 if (GNUNET_YES != ntohl (msg->one_shot))
1214 setup_peer_monitoring_client (client, &msg->peer);
1218 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
1219 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
1222 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1227 * Client asked to obtain information about a specific or all validation
1231 * @param client the client
1232 * @param message the peer address information request
1235 clients_handle_monitor_validation (void *cls, struct GNUNET_SERVER_Client *client,
1236 const struct GNUNET_MessageHeader *message)
1238 static struct GNUNET_PeerIdentity all_zeros;
1239 struct GNUNET_SERVER_TransmitContext *tc;
1240 struct PeerMonitorMessage *msg;
1241 struct IterationContext pc;
1243 if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_REQUEST)
1246 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1249 if (ntohs (message->size) != sizeof (struct ValidationMonitorMessage))
1252 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1255 msg = (struct PeerMonitorMessage *) message;
1256 if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
1257 (NULL != lookup_monitoring_client (val_monitoring_clients_head, client)) )
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1260 "ServerClient %p tried to start monitoring twice\n",
1263 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1266 GNUNET_SERVER_disable_receive_done_warning (client);
1267 pc.tc = tc = GNUNET_SERVER_transmit_context_create (client);
1269 /* Send initial list */
1270 if (0 == memcmp (&msg->peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
1272 /* iterate over all neighbours */
1273 pc.all = GNUNET_YES;
1278 /* just return one neighbour */
1283 GST_validation_iterate (&send_validation_information, &pc);
1285 if (GNUNET_YES != ntohl (msg->one_shot))
1287 setup_val_monitoring_client (client, &msg->peer);
1291 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
1292 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE);
1294 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1298 * Start handling requests from clients.
1300 * @param server server used to accept clients from.
1303 GST_clients_start (struct GNUNET_SERVER_Handle *server)
1305 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1306 {&clients_handle_start, NULL,
1307 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
1308 {&clients_handle_hello, NULL,
1309 GNUNET_MESSAGE_TYPE_HELLO, 0},
1310 {&clients_handle_send, NULL,
1311 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
1312 {&clients_handle_request_connect, NULL,
1313 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT,
1314 sizeof (struct TransportRequestConnectMessage)},
1315 {&clients_handle_address_to_string, NULL,
1316 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING, 0},
1317 {&clients_handle_monitor_peers, NULL,
1318 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST,
1319 sizeof (struct PeerMonitorMessage)},
1320 {&clients_handle_monitor_validation, NULL,
1321 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_REQUEST,
1322 sizeof (struct ValidationMonitorMessage)},
1323 {&GST_blacklist_handle_init, NULL,
1324 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT,
1325 sizeof (struct GNUNET_MessageHeader)},
1326 {&GST_blacklist_handle_reply, NULL,
1327 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY,
1328 sizeof (struct BlacklistMessage)},
1329 {&GST_manipulation_set_metric, NULL,
1330 GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC, 0},
1333 peer_nc = GNUNET_SERVER_notification_context_create (server, 0);
1334 val_nc = GNUNET_SERVER_notification_context_create (server, 0);
1335 GNUNET_SERVER_add_handlers (server, handlers);
1336 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
1342 * Stop processing clients.
1347 struct AddressToStringContext *cur;
1349 while (NULL != (cur = a2s_head))
1351 GNUNET_SERVER_transmit_context_destroy (cur->tc, GNUNET_NO);
1352 GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, cur);
1355 if (NULL != peer_nc)
1357 GNUNET_SERVER_notification_context_destroy (peer_nc);
1362 GNUNET_SERVER_notification_context_destroy (val_nc);
1369 * Broadcast the given message to all of our clients.
1371 * @param msg message to broadcast
1372 * @param may_drop #GNUNET_YES if the message can be dropped / is payload
1375 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg, int may_drop)
1377 struct TransportClient *tc;
1379 for (tc = clients_head; NULL != tc; tc = tc->next)
1381 if ((GNUNET_YES == may_drop) && (GNUNET_YES != tc->send_payload))
1382 continue; /* skip, this client does not care about payload */
1383 unicast (tc, msg, may_drop);
1389 * Send the given message to a particular client
1391 * @param client target of the message
1392 * @param msg message to transmit
1393 * @param may_drop #GNUNET_YES if the message can be dropped
1396 GST_clients_unicast (struct GNUNET_SERVER_Client *client,
1397 const struct GNUNET_MessageHeader *msg, int may_drop)
1399 struct TransportClient *tc;
1401 tc = lookup_client (client);
1403 return; /* client got disconnected in the meantime, drop message */
1404 unicast (tc, msg, may_drop);
1409 * Broadcast the new active address to all clients monitoring the peer.
1411 * @param peer peer this update is about (never NULL)
1412 * @param address address, NULL on disconnect
1413 * @param state the current state of the peer
1414 * @param state_timeout the time out for the state
1417 GST_clients_broadcast_peer_notification (const struct GNUNET_PeerIdentity *peer,
1418 const struct GNUNET_HELLO_Address *address,
1419 enum GNUNET_TRANSPORT_PeerState state,
1420 struct GNUNET_TIME_Absolute state_timeout)
1422 struct PeerIterateResponseMessage *msg;
1423 struct MonitoringClient *mc;
1424 static struct GNUNET_PeerIdentity all_zeros;
1425 msg = compose_address_iterate_response_message (peer, address);
1426 msg->state = htonl (state);
1427 msg->state_timeout = GNUNET_TIME_absolute_hton (state_timeout);
1428 mc = peer_monitoring_clients_head;
1431 if ((0 == memcmp (&mc->peer, &all_zeros,
1432 sizeof (struct GNUNET_PeerIdentity))) ||
1433 (0 == memcmp (&mc->peer, peer,
1434 sizeof (struct GNUNET_PeerIdentity))))
1436 GNUNET_SERVER_notification_context_unicast (peer_nc, mc->client,
1437 &msg->header, GNUNET_NO);
1446 * Broadcast the new validation changes to all clients monitoring the peer.
1448 * @param peer peer this update is about (never NULL)
1449 * @param address address, NULL on disconnect
1450 * @param last_validation point in time when last validation was performed
1451 * @param valid_until point in time how long address is valid
1452 * @param next_validation point in time when next validation will be performed
1453 * @param state state of validation notification
1456 GST_clients_broadcast_validation_notification (
1457 const struct GNUNET_PeerIdentity *peer,
1458 const struct GNUNET_HELLO_Address *address,
1459 struct GNUNET_TIME_Absolute last_validation,
1460 struct GNUNET_TIME_Absolute valid_until,
1461 struct GNUNET_TIME_Absolute next_validation,
1462 enum GNUNET_TRANSPORT_ValidationState state)
1464 struct ValidationIterateResponseMessage *msg;
1465 struct MonitoringClient *mc;
1466 static struct GNUNET_PeerIdentity all_zeros;
1468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1469 "Sending information about for validation entry for peer `%s' using address `%s'\n",
1470 GNUNET_i2s(peer), (address != NULL) ? GST_plugins_a2s (address) : "<none>");
1472 msg = compose_validation_iterate_response_message (peer, address);
1473 msg->last_validation = GNUNET_TIME_absolute_hton(last_validation);
1474 msg->valid_until = GNUNET_TIME_absolute_hton(valid_until);
1475 msg->next_validation = GNUNET_TIME_absolute_hton(next_validation);
1476 msg->state = htonl ((uint32_t) state);
1477 mc = val_monitoring_clients_head;
1480 if ((0 == memcmp (&mc->peer, &all_zeros,
1481 sizeof (struct GNUNET_PeerIdentity))) ||
1482 (0 == memcmp (&mc->peer, peer,
1483 sizeof (struct GNUNET_PeerIdentity))))
1485 GNUNET_SERVER_notification_context_unicast (val_nc, mc->client,
1486 &msg->header, GNUNET_NO);
1495 /* end of file gnunet-service-transport_clients.c */