2 This file is part of GNUnet.
3 (C) 2010-2014 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);
317 * Setup a new monitoring client using the given server client handle and
320 * @param client server's client handle to create our internal handle for
321 * @param peer identity of the peer to monitor the addresses of,
322 * zero to monitor all neighrours.
323 * @return handle to the new monitoring client
325 static struct MonitoringClient *
326 setup_val_monitoring_client (struct GNUNET_SERVER_Client *client,
327 struct GNUNET_PeerIdentity *peer)
329 struct MonitoringClient *mc;
330 static struct GNUNET_PeerIdentity all_zeros;
332 GNUNET_assert (lookup_monitoring_client (val_monitoring_clients_head, client) == NULL);
333 mc = GNUNET_new (struct MonitoringClient);
336 GNUNET_CONTAINER_DLL_insert (val_monitoring_clients_head, val_monitoring_clients_tail, mc);
337 GNUNET_SERVER_notification_context_add (val_nc, client);
339 if (0 != memcmp (peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
340 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
341 "Client %p started monitoring of the peer `%s'\n",
342 mc, GNUNET_i2s (peer));
344 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
345 "Client %p started monitoring all peers\n", mc);
351 * Function called to notify a client about the socket being ready to
352 * queue more data. "buf" will be NULL and "size" zero if the socket
353 * was closed for writing in the meantime.
356 * @param size number of bytes available in @a buf
357 * @param buf where the callee should write the message
358 * @return number of bytes written to @a buf
361 transmit_to_client_callback (void *cls,
365 struct TransportClient *tc = cls;
366 struct ClientMessageQueueEntry *q;
367 const struct GNUNET_MessageHeader *msg;
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Transmission to client failed, closing connection.\n");
381 while (NULL != (q = tc->message_queue_head))
383 msg = (const struct GNUNET_MessageHeader *) &q[1];
384 msize = ntohs (msg->size);
385 if (msize + tsize > size)
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Transmitting message of type %u to client %p.\n",
389 ntohs (msg->type), tc);
390 GNUNET_CONTAINER_DLL_remove (tc->message_queue_head,
391 tc->message_queue_tail,
394 memcpy (&cbuf[tsize], msg, msize);
400 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
402 GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
403 GNUNET_TIME_UNIT_FOREVER_REL,
404 &transmit_to_client_callback, tc);
405 GNUNET_assert (NULL != tc->th);
412 * Queue the given message for transmission to the given client
414 * @param tc target of the message
415 * @param msg message to transmit
416 * @param may_drop #GNUNET_YES if the message can be dropped
419 unicast (struct TransportClient *tc,
420 const struct GNUNET_MessageHeader *msg,
423 struct ClientMessageQueueEntry *q;
432 if ((tc->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
434 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
435 _("Dropping message of type %u and size %u, have %u/%u messages pending\n"),
440 GNUNET_STATISTICS_update (GST_stats,
442 ("# messages dropped due to slow client"), 1,
446 msize = ntohs (msg->size);
447 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
448 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
449 memcpy (&q[1], msg, msize);
450 GNUNET_CONTAINER_DLL_insert_tail (tc->message_queue_head,
451 tc->message_queue_tail, q);
456 GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
457 GNUNET_TIME_UNIT_FOREVER_REL,
458 &transmit_to_client_callback, tc);
459 GNUNET_assert (NULL != tc->th);
464 * Called whenever a client is disconnected. Frees our
465 * resources associated with that client.
468 * @param client identification of the client
471 client_disconnect_notification (void *cls,
472 struct GNUNET_SERVER_Client *client)
474 struct TransportClient *tc;
475 struct MonitoringClient *mc;
476 struct ClientMessageQueueEntry *mqe;
480 mc = lookup_monitoring_client (peer_monitoring_clients_head, client);
483 GNUNET_CONTAINER_DLL_remove (peer_monitoring_clients_head,
484 peer_monitoring_clients_tail,
488 mc = lookup_monitoring_client (val_monitoring_clients_head, client);
491 GNUNET_CONTAINER_DLL_remove (val_monitoring_clients_head,
492 val_monitoring_clients_tail,
496 tc = lookup_client (client);
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
500 "Client %p disconnected, cleaning up.\n", tc);
501 while (NULL != (mqe = tc->message_queue_head))
503 GNUNET_CONTAINER_DLL_remove (tc->message_queue_head, tc->message_queue_tail,
508 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, tc);
511 GNUNET_SERVER_notify_transmit_ready_cancel (tc->th);
514 GNUNET_break (0 == tc->message_count);
520 * Function called for each of our connected neighbours. Notify the
521 * client about the existing neighbour.
523 * @param cls the `struct TransportClient *` to notify
524 * @param peer identity of the neighbour
525 * @param address the address
526 * @param state the current state of the peer
527 * @param state_timeout the time out for the state
528 * @param bandwidth_in inbound bandwidth in NBO
529 * @param bandwidth_out outbound bandwidth in NBO
532 notify_client_about_neighbour (void *cls,
533 const struct GNUNET_PeerIdentity *peer,
534 const struct GNUNET_HELLO_Address *address,
535 enum GNUNET_TRANSPORT_PeerState state,
536 struct GNUNET_TIME_Absolute state_timeout,
537 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
538 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
540 struct TransportClient *tc = cls;
541 struct ConnectInfoMessage *cim;
542 size_t size = sizeof (struct ConnectInfoMessage);
543 char buf[size] GNUNET_ALIGN;
545 if (GNUNET_NO == GST_neighbours_test_connected (peer))
548 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
549 cim = (struct ConnectInfoMessage *) buf;
550 cim->header.size = htons (size);
551 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
553 cim->quota_in = bandwidth_in;
554 cim->quota_out = bandwidth_out;
555 unicast (tc, &cim->header, GNUNET_NO);
560 * Initialize a normal client. We got a start message from this
561 * client, add him to the list of clients for broadcasting of inbound
565 * @param client the client
566 * @param message the start message that was sent
569 clients_handle_start (void *cls,
570 struct GNUNET_SERVER_Client *client,
571 const struct GNUNET_MessageHeader *message)
573 const struct StartMessage *start;
574 struct TransportClient *tc;
577 tc = lookup_client (client);
579 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
580 "Client %p sent START\n", tc);
583 /* got 'start' twice from the same client, not allowed */
584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
585 "TransportClient %p ServerClient %p sent multiple START messages\n",
588 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
591 start = (const struct StartMessage *) message;
592 options = ntohl (start->options);
593 if ((0 != (1 & options)) &&
595 memcmp (&start->self, &GST_my_identity,
596 sizeof (struct GNUNET_PeerIdentity))))
598 /* client thinks this is a different peer, reject */
599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
601 ("Rejecting control connection from peer `%s', which is not me!\n"),
602 GNUNET_i2s (&start->self));
603 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
606 tc = setup_client (client);
607 tc->send_payload = (0 != (2 & options));
608 unicast (tc, GST_hello_get (), GNUNET_NO);
609 GST_neighbours_iterate (¬ify_client_about_neighbour, tc);
610 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, tc);
611 GNUNET_SERVER_receive_done (client, GNUNET_OK);
616 * Client sent us a HELLO. Process the request.
619 * @param client the client
620 * @param message the HELLO message
623 clients_handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
624 const struct GNUNET_MessageHeader *message)
626 GST_validation_handle_hello (message);
627 GNUNET_SERVER_receive_done (client, GNUNET_OK);
632 * Closure for 'handle_send_transmit_continuation'
634 struct SendTransmitContinuationContext
637 * Client that made the request.
639 struct GNUNET_SERVER_Client *client;
642 * Peer that was the target.
644 struct GNUNET_PeerIdentity target;
649 * Function called after the transmission is done. Notify the client that it is
650 * OK to send the next message.
653 * @param success #GNUNET_OK on success, #GNUNET_NO on failure, #GNUNET_SYSERR if we're not connected
654 * @param bytes_payload bytes payload sent
655 * @param bytes_on_wire bytes sent on wire
658 handle_send_transmit_continuation (void *cls, int success,
659 size_t bytes_payload,
660 size_t bytes_on_wire)
662 struct SendTransmitContinuationContext *stcc = cls;
663 struct SendOkMessage send_ok_msg;
665 if (GNUNET_OK == success)
666 GST_neighbours_notify_payload_sent (&stcc->target, bytes_payload);
668 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
669 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
670 send_ok_msg.bytes_msg = htonl (bytes_payload);
671 send_ok_msg.bytes_physical = htonl (bytes_on_wire);
672 send_ok_msg.success = htonl (success);
673 send_ok_msg.latency =
674 GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
675 send_ok_msg.peer = stcc->target;
676 GST_clients_unicast (stcc->client, &send_ok_msg.header, GNUNET_NO);
677 GNUNET_SERVER_client_drop (stcc->client);
683 * Client asked for transmission to a peer. Process the request.
686 * @param client the client
687 * @param message the send message that was sent
690 clients_handle_send (void *cls,
691 struct GNUNET_SERVER_Client *client,
692 const struct GNUNET_MessageHeader *message)
694 const struct OutboundMessage *obm;
695 const struct GNUNET_MessageHeader *obmm;
696 struct SendTransmitContinuationContext *stcc;
699 struct TransportClient *tc;
701 tc = lookup_client (client);
704 /* client asked for transmission before 'START' */
706 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
710 size = ntohs (message->size);
712 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
715 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
718 obm = (const struct OutboundMessage *) message;
719 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
720 msize = size - sizeof (struct OutboundMessage);
721 if (msize < sizeof (struct GNUNET_MessageHeader))
724 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
729 "Received `%s' request from client with target `%4s' and first message of type %u and total size %u\n",
731 GNUNET_i2s (&obm->peer),
734 if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
736 /* not connected, not allowed to send; can happen due to asynchronous operations */
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738 "Could not send message to peer `%s': not connected\n",
739 GNUNET_i2s (&obm->peer));
740 GNUNET_STATISTICS_update (GST_stats,
742 ("# bytes payload dropped (other peer was not connected)"),
744 GNUNET_SERVER_receive_done (client, GNUNET_OK);
747 GNUNET_SERVER_receive_done (client, GNUNET_OK);
748 stcc = GNUNET_new (struct SendTransmitContinuationContext);
749 stcc->target = obm->peer;
750 stcc->client = client;
751 GNUNET_SERVER_client_keep (client);
752 GST_manipulation_send (&obm->peer, obmm, msize,
753 GNUNET_TIME_relative_ntoh (obm->timeout),
754 &handle_send_transmit_continuation, stcc);
759 * Try to initiate a connection to the given peer if the blacklist
762 * @param cls closure (unused, NULL)
763 * @param peer identity of peer that was tested
764 * @param result #GNUNET_OK if the connection is allowed,
768 try_connect_if_allowed (void *cls,
769 const struct GNUNET_PeerIdentity *peer,
772 if (GNUNET_OK != result)
774 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
775 _("Blacklist refuses connection attempt to peer `%s'\n"),
777 return; /* not allowed */
780 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
781 _("Blacklist allows connection attempt to peer `%s'\n"),
784 GST_neighbours_try_connect (peer);
789 * Handle request connect message
791 * @param cls closure (always NULL)
792 * @param client identification of the client
793 * @param message the actual message
796 clients_handle_request_connect (void *cls,
797 struct GNUNET_SERVER_Client *client,
798 const struct GNUNET_MessageHeader *message)
800 const struct TransportRequestConnectMessage *trcm =
801 (const struct TransportRequestConnectMessage *) message;
803 if (GNUNET_YES == ntohl (trcm->connect))
805 GNUNET_STATISTICS_update (GST_stats,
807 ("# REQUEST CONNECT messages received"), 1,
810 if (0 == memcmp (&trcm->peer, &GST_my_identity,
811 sizeof (struct GNUNET_PeerIdentity)))
814 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
815 "Received a request connect message myself `%s'\n",
816 GNUNET_i2s (&trcm->peer));
820 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
821 _("Received a request connect message for peer `%s'\n"),
822 GNUNET_i2s (&trcm->peer));
824 (void) GST_blacklist_test_allowed (&trcm->peer, NULL, &try_connect_if_allowed,
828 else if (GNUNET_NO == ntohl (trcm->connect))
830 GNUNET_STATISTICS_update (GST_stats,
832 ("# REQUEST DISCONNECT messages received"), 1,
835 if (0 == memcmp (&trcm->peer, &GST_my_identity,
836 sizeof (struct GNUNET_PeerIdentity)))
839 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
840 "Received a request disconnect message myself `%s'\n",
841 GNUNET_i2s (&trcm->peer));
845 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
846 _("Received a request disconnect message for peer `%s'\n"),
847 GNUNET_i2s (&trcm->peer));
848 (void) GST_neighbours_force_disconnect (&trcm->peer);
854 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
857 GNUNET_SERVER_receive_done (client, GNUNET_OK);
862 * Take the given address and append it to the set of results sent back to
863 * the client. This function may be called serveral times for a single
864 * conversion. The last invocation will be with a @a address of
865 * NULL and a @a res of #GNUNET_OK. Thus, to indicate conversion
866 * errors, the callback might be called first with @a address NULL and
867 * @a res being #GNUNET_SYSERR. In that case, there will still be a
868 * subsequent call later with @a address NULL and @a res #GNUNET_OK.
870 * @param cls the transmission context used (`struct GNUNET_SERVER_TransmitContext *`)
871 * @param buf text to transmit (contains the human-readable address, or NULL)
872 * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on error,
876 transmit_address_to_client (void *cls,
880 struct AddressToStringContext *actx = cls;
881 struct AddressToStringResultMessage *atsm;
890 GNUNET_assert ( (GNUNET_OK == res) ||
891 (GNUNET_SYSERR == res) );
894 len = sizeof (struct AddressToStringResultMessage);
895 atsm = GNUNET_malloc (len);
896 atsm->header.size = ntohs (len);
897 atsm->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
898 if (GNUNET_OK == res)
900 /* this was the last call, transmit */
901 atsm->res = htonl (GNUNET_OK);
902 atsm->addr_len = htonl (0);
903 GNUNET_SERVER_transmit_context_append_message (actx->tc,
904 (const struct GNUNET_MessageHeader *) atsm);
905 GNUNET_SERVER_transmit_context_run (actx->tc,
906 GNUNET_TIME_UNIT_FOREVER_REL);
907 GNUNET_CONTAINER_DLL_remove (a2s_head,
913 if (GNUNET_SYSERR == res)
915 /* address conversion failed, but there will be more callbacks */
916 atsm->res = htonl (GNUNET_SYSERR);
917 atsm->addr_len = htonl (0);
918 GNUNET_SERVER_transmit_context_append_message (actx->tc,
919 (const struct GNUNET_MessageHeader *) atsm);
924 GNUNET_assert (GNUNET_OK == res);
925 /* succesful conversion, append*/
926 slen = strlen (buf) + 1;
927 len = sizeof (struct AddressToStringResultMessage) + slen;
928 atsm = GNUNET_malloc (len);
929 atsm->header.size = ntohs (len);
930 atsm->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
931 atsm->res = htonl (GNUNET_YES);
932 atsm->addr_len = htonl (slen);
936 GNUNET_SERVER_transmit_context_append_message (actx->tc,
937 (const struct GNUNET_MessageHeader *) atsm);
943 * Client asked to resolve an address. Process the request.
946 * @param client the client
947 * @param message the resolution request
950 clients_handle_address_to_string (void *cls,
951 struct GNUNET_SERVER_Client *client,
952 const struct GNUNET_MessageHeader *message)
954 const struct AddressLookupMessage *alum;
955 struct GNUNET_TRANSPORT_PluginFunctions *papi;
956 const char *plugin_name;
958 uint32_t address_len;
960 struct GNUNET_SERVER_TransmitContext *tc;
961 struct AddressToStringContext *actx;
962 struct AddressToStringResultMessage atsm;
963 struct GNUNET_TIME_Relative rtimeout;
966 size = ntohs (message->size);
967 if (size < sizeof (struct AddressLookupMessage))
970 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
973 alum = (const struct AddressLookupMessage *) message;
974 address_len = ntohs (alum->addrlen);
975 if (size <= sizeof (struct AddressLookupMessage) + address_len)
978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
981 address = (const char *) &alum[1];
982 plugin_name = (const char *) &address[address_len];
983 if ('\0' != plugin_name[size - sizeof (struct AddressLookupMessage) - address_len - 1])
986 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
989 rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
990 numeric = ntohs (alum->numeric_only);
991 tc = GNUNET_SERVER_transmit_context_create (client);
992 papi = GST_plugins_printer_find (plugin_name);
998 atsm.header.size = ntohs (sizeof (struct AddressToStringResultMessage));
999 atsm.header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
1000 atsm.res = htonl (GNUNET_SYSERR);
1001 atsm.addr_len = htonl (0);
1002 GNUNET_SERVER_transmit_context_append_message (tc,
1004 atsm.header.size = ntohs (sizeof (struct AddressToStringResultMessage));
1005 atsm.header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
1006 atsm.res = htonl (GNUNET_OK);
1007 atsm.addr_len = htonl (0);
1008 GNUNET_SERVER_transmit_context_append_message (tc,
1010 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1013 actx = GNUNET_new (struct AddressToStringContext);
1020 GNUNET_CONTAINER_DLL_insert (a2s_head, a2s_tail, actx);
1021 GNUNET_SERVER_disable_receive_done_warning (client);
1022 papi->address_pretty_printer (papi->cls,
1024 address, address_len,
1027 &transmit_address_to_client,
1033 * Compose #PeerIterateResponseMessage using the given peer and address.
1035 * @param peer identity of the peer
1036 * @param address the address, NULL on disconnect
1037 * @return composed message
1039 static struct PeerIterateResponseMessage *
1040 compose_address_iterate_response_message (const struct GNUNET_PeerIdentity *peer,
1041 const struct GNUNET_HELLO_Address *address)
1043 struct PeerIterateResponseMessage *msg;
1049 GNUNET_assert (NULL != peer);
1050 if (NULL != address)
1052 tlen = strlen (address->transport_name) + 1;
1053 alen = address->address_length;
1057 size = (sizeof (struct PeerIterateResponseMessage) + alen + tlen);
1058 msg = GNUNET_malloc (size);
1059 msg->header.size = htons (size);
1061 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
1062 msg->reserved = htonl (0);
1064 msg->addrlen = htonl (alen);
1065 msg->pluginlen = htonl (tlen);
1067 if (NULL != address)
1069 msg->local_address_info = htonl((uint32_t) address->local_info);
1070 addr = (char *) &msg[1];
1071 memcpy (addr, address->address, alen);
1072 memcpy (&addr[alen], address->transport_name, tlen);
1078 * Compose #PeerIterateResponseMessage using the given peer and address.
1080 * @param peer identity of the peer
1081 * @param address the address, NULL on disconnect
1082 * @return composed message
1084 static struct ValidationIterateResponseMessage *
1085 compose_validation_iterate_response_message (const struct GNUNET_PeerIdentity *peer,
1086 const struct GNUNET_HELLO_Address *address)
1088 struct ValidationIterateResponseMessage *msg;
1094 GNUNET_assert (NULL != peer);
1095 if (NULL != address)
1097 tlen = strlen (address->transport_name) + 1;
1098 alen = address->address_length;
1102 size = (sizeof (struct ValidationIterateResponseMessage) + alen + tlen);
1103 msg = GNUNET_malloc (size);
1104 msg->header.size = htons (size);
1106 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE);
1107 msg->reserved = htonl (0);
1109 msg->addrlen = htonl (alen);
1110 msg->pluginlen = htonl (tlen);
1112 if (NULL != address)
1114 msg->local_address_info = htonl((uint32_t) address->local_info);
1115 addr = (char *) &msg[1];
1116 memcpy (addr, address->address, alen);
1117 memcpy (&addr[alen], address->transport_name, tlen);
1122 struct IterationContext
1124 struct GNUNET_SERVER_TransmitContext *tc;
1126 struct GNUNET_PeerIdentity id;
1132 * Output information of validation entries to the given client.
1134 * @param cls the 'struct IterationContext'
1135 * @param peer identity of the neighbour
1136 * @param address the address
1137 * @param last_validation point in time when last validation was performed
1138 * @param valid_until point in time how long address is valid
1139 * @param next_validation point in time when next validation will be performed
1140 * @param state state of validation notification
1143 send_validation_information (void *cls,
1144 const struct GNUNET_PeerIdentity *peer,
1145 const struct GNUNET_HELLO_Address *address,
1146 struct GNUNET_TIME_Absolute last_validation,
1147 struct GNUNET_TIME_Absolute valid_until,
1148 struct GNUNET_TIME_Absolute next_validation,
1149 enum GNUNET_TRANSPORT_ValidationState state)
1151 struct IterationContext *pc = cls;
1152 struct ValidationIterateResponseMessage *msg;
1154 if ( (GNUNET_YES == pc->all) ||
1155 (0 == memcmp (peer, &pc->id, sizeof (pc->id))) )
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158 "Sending information about for validation entry for peer `%s' using address `%s'\n",
1159 GNUNET_i2s(peer), (address != NULL) ? GST_plugins_a2s (address) : "<none>");
1160 msg = compose_validation_iterate_response_message (peer, address);
1161 msg->last_validation = GNUNET_TIME_absolute_hton(last_validation);
1162 msg->valid_until = GNUNET_TIME_absolute_hton(valid_until);
1163 msg->next_validation = GNUNET_TIME_absolute_hton(next_validation);
1164 msg->state = htonl ((uint32_t) state);
1165 GNUNET_SERVER_transmit_context_append_message (pc->tc, &msg->header);
1172 * Output information of neighbours to the given client.
1174 * @param cls the 'struct PeerIterationContext'
1175 * @param peer identity of the neighbour
1176 * @param address the address
1177 * @param state current state this peer is in
1178 * @param state_timeout timeout for the current state of the peer
1179 * @param bandwidth_in inbound quota in NBO
1180 * @param bandwidth_out outbound quota in NBO
1183 send_peer_information (void *cls,
1184 const struct GNUNET_PeerIdentity *peer,
1185 const struct GNUNET_HELLO_Address *address,
1186 enum GNUNET_TRANSPORT_PeerState state,
1187 struct GNUNET_TIME_Absolute state_timeout,
1188 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1189 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1191 struct IterationContext *pc = cls;
1192 struct PeerIterateResponseMessage *msg;
1194 if ( (GNUNET_YES == pc->all) ||
1195 (0 == memcmp (peer, &pc->id, sizeof (pc->id))) )
1197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1198 "Sending information about `%s' using address `%s' in state `%s'\n",
1200 (address != NULL) ? GST_plugins_a2s (address) : "<none>",
1201 GNUNET_TRANSPORT_ps2s (state));
1202 msg = compose_address_iterate_response_message (peer, address);
1203 msg->state = htonl (state);
1204 msg->state_timeout = GNUNET_TIME_absolute_hton(state_timeout);
1205 GNUNET_SERVER_transmit_context_append_message (pc->tc, &msg->header);
1213 * Client asked to obtain information about a specific or all peers
1214 * Process the request.
1217 * @param client the client
1218 * @param message the peer address information request
1221 clients_handle_monitor_peers (void *cls, struct GNUNET_SERVER_Client *client,
1222 const struct GNUNET_MessageHeader *message)
1224 static struct GNUNET_PeerIdentity all_zeros;
1225 struct GNUNET_SERVER_TransmitContext *tc;
1226 struct PeerMonitorMessage *msg;
1227 struct IterationContext pc;
1229 if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST)
1232 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1235 if (ntohs (message->size) != sizeof (struct PeerMonitorMessage))
1238 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1241 msg = (struct PeerMonitorMessage *) message;
1242 if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
1243 (NULL != lookup_monitoring_client (peer_monitoring_clients_head, client)) )
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1246 "ServerClient %p tried to start monitoring twice\n",
1249 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1252 GNUNET_SERVER_disable_receive_done_warning (client);
1253 pc.tc = tc = GNUNET_SERVER_transmit_context_create (client);
1255 /* Send initial list */
1256 if (0 == memcmp (&msg->peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
1258 /* iterate over all neighbours */
1259 pc.all = GNUNET_YES;
1264 /* just return one neighbour */
1268 GST_neighbours_iterate (&send_peer_information, &pc);
1270 if (GNUNET_YES != ntohl (msg->one_shot))
1272 setup_peer_monitoring_client (client, &msg->peer);
1276 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
1277 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
1280 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1285 * Client asked to obtain information about a specific or all validation
1289 * @param client the client
1290 * @param message the peer address information request
1293 clients_handle_monitor_validation (void *cls, struct GNUNET_SERVER_Client *client,
1294 const struct GNUNET_MessageHeader *message)
1296 static struct GNUNET_PeerIdentity all_zeros;
1297 struct GNUNET_SERVER_TransmitContext *tc;
1298 struct PeerMonitorMessage *msg;
1299 struct IterationContext pc;
1301 if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_REQUEST)
1304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1307 if (ntohs (message->size) != sizeof (struct ValidationMonitorMessage))
1310 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1313 msg = (struct PeerMonitorMessage *) message;
1314 if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
1315 (NULL != lookup_monitoring_client (val_monitoring_clients_head, client)) )
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1318 "ServerClient %p tried to start monitoring twice\n",
1321 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1324 GNUNET_SERVER_disable_receive_done_warning (client);
1325 pc.tc = tc = GNUNET_SERVER_transmit_context_create (client);
1327 /* Send initial list */
1328 if (0 == memcmp (&msg->peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
1330 /* iterate over all neighbours */
1331 pc.all = GNUNET_YES;
1336 /* just return one neighbour */
1341 GST_validation_iterate (&send_validation_information, &pc);
1343 if (GNUNET_YES != ntohl (msg->one_shot))
1345 setup_val_monitoring_client (client, &msg->peer);
1349 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
1350 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE);
1352 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1356 * Start handling requests from clients.
1358 * @param server server used to accept clients from.
1361 GST_clients_start (struct GNUNET_SERVER_Handle *server)
1363 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1364 {&clients_handle_start, NULL,
1365 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
1366 {&clients_handle_hello, NULL,
1367 GNUNET_MESSAGE_TYPE_HELLO, 0},
1368 {&clients_handle_send, NULL,
1369 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
1370 {&clients_handle_request_connect, NULL,
1371 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT,
1372 sizeof (struct TransportRequestConnectMessage)},
1373 {&clients_handle_address_to_string, NULL,
1374 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING, 0},
1375 {&clients_handle_monitor_peers, NULL,
1376 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST,
1377 sizeof (struct PeerMonitorMessage)},
1378 {&clients_handle_monitor_validation, NULL,
1379 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_REQUEST,
1380 sizeof (struct ValidationMonitorMessage)},
1381 {&GST_blacklist_handle_init, NULL,
1382 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT,
1383 sizeof (struct GNUNET_MessageHeader)},
1384 {&GST_blacklist_handle_reply, NULL,
1385 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY,
1386 sizeof (struct BlacklistMessage)},
1387 {&GST_manipulation_set_metric, NULL,
1388 GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC, 0},
1391 peer_nc = GNUNET_SERVER_notification_context_create (server, 0);
1392 val_nc = GNUNET_SERVER_notification_context_create (server, 0);
1393 GNUNET_SERVER_add_handlers (server, handlers);
1394 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
1400 * Stop processing clients.
1405 struct AddressToStringContext *cur;
1407 while (NULL != (cur = a2s_head))
1409 GNUNET_SERVER_transmit_context_destroy (cur->tc, GNUNET_NO);
1410 GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, cur);
1413 if (NULL != peer_nc)
1415 GNUNET_SERVER_notification_context_destroy (peer_nc);
1420 GNUNET_SERVER_notification_context_destroy (val_nc);
1427 * Broadcast the given message to all of our clients.
1429 * @param msg message to broadcast
1430 * @param may_drop #GNUNET_YES if the message can be dropped / is payload
1433 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg, int may_drop)
1435 struct TransportClient *tc;
1437 for (tc = clients_head; NULL != tc; tc = tc->next)
1439 if ((GNUNET_YES == may_drop) && (GNUNET_YES != tc->send_payload))
1440 continue; /* skip, this client does not care about payload */
1441 unicast (tc, msg, may_drop);
1447 * Send the given message to a particular client
1449 * @param client target of the message
1450 * @param msg message to transmit
1451 * @param may_drop #GNUNET_YES if the message can be dropped
1454 GST_clients_unicast (struct GNUNET_SERVER_Client *client,
1455 const struct GNUNET_MessageHeader *msg, int may_drop)
1457 struct TransportClient *tc;
1459 tc = lookup_client (client);
1461 return; /* client got disconnected in the meantime, drop message */
1462 unicast (tc, msg, may_drop);
1467 * Broadcast the new active address to all clients monitoring the peer.
1469 * @param peer peer this update is about (never NULL)
1470 * @param address address, NULL on disconnect
1471 * @param state the current state of the peer
1472 * @param state_timeout the time out for the state
1475 GST_clients_broadcast_peer_notification (const struct GNUNET_PeerIdentity *peer,
1476 const struct GNUNET_HELLO_Address *address,
1477 enum GNUNET_TRANSPORT_PeerState state,
1478 struct GNUNET_TIME_Absolute state_timeout)
1480 struct PeerIterateResponseMessage *msg;
1481 struct MonitoringClient *mc;
1482 static struct GNUNET_PeerIdentity all_zeros;
1483 msg = compose_address_iterate_response_message (peer, address);
1484 msg->state = htonl (state);
1485 msg->state_timeout = GNUNET_TIME_absolute_hton (state_timeout);
1486 mc = peer_monitoring_clients_head;
1489 if ((0 == memcmp (&mc->peer, &all_zeros,
1490 sizeof (struct GNUNET_PeerIdentity))) ||
1491 (0 == memcmp (&mc->peer, peer,
1492 sizeof (struct GNUNET_PeerIdentity))))
1494 GNUNET_SERVER_notification_context_unicast (peer_nc, mc->client,
1495 &msg->header, GNUNET_NO);
1504 * Broadcast the new validation changes to all clients monitoring the peer.
1506 * @param peer peer this update is about (never NULL)
1507 * @param address address, NULL on disconnect
1508 * @param last_validation point in time when last validation was performed
1509 * @param valid_until point in time how long address is valid
1510 * @param next_validation point in time when next validation will be performed
1511 * @param state state of validation notification
1514 GST_clients_broadcast_validation_notification (
1515 const struct GNUNET_PeerIdentity *peer,
1516 const struct GNUNET_HELLO_Address *address,
1517 struct GNUNET_TIME_Absolute last_validation,
1518 struct GNUNET_TIME_Absolute valid_until,
1519 struct GNUNET_TIME_Absolute next_validation,
1520 enum GNUNET_TRANSPORT_ValidationState state)
1522 struct ValidationIterateResponseMessage *msg;
1523 struct MonitoringClient *mc;
1524 static struct GNUNET_PeerIdentity all_zeros;
1526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1527 "Sending information about for validation entry for peer `%s' using address `%s'\n",
1528 GNUNET_i2s(peer), (address != NULL) ? GST_plugins_a2s (address) : "<none>");
1530 msg = compose_validation_iterate_response_message (peer, address);
1531 msg->last_validation = GNUNET_TIME_absolute_hton(last_validation);
1532 msg->valid_until = GNUNET_TIME_absolute_hton(valid_until);
1533 msg->next_validation = GNUNET_TIME_absolute_hton(next_validation);
1534 msg->state = htonl ((uint32_t) state);
1535 mc = val_monitoring_clients_head;
1538 if ((0 == memcmp (&mc->peer, &all_zeros,
1539 sizeof (struct GNUNET_PeerIdentity))) ||
1540 (0 == memcmp (&mc->peer, peer,
1541 sizeof (struct GNUNET_PeerIdentity))))
1543 GNUNET_SERVER_notification_context_unicast (val_nc, mc->client,
1544 &msg->header, GNUNET_NO);
1553 /* end of file gnunet-service-transport_clients.c */