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 *monitoring_clients_head;
196 * Tail of linked list of monitoring clients.
198 static struct MonitoringClient *monitoring_clients_tail;
201 * Notification context, to send updates on changes to active addresses
204 static struct GNUNET_SERVER_NotificationContext *nc;
208 * Find the internal handle associated with the given client handle
210 * @param client server's client handle to look up
211 * @return internal client handle
213 static struct TransportClient *
214 lookup_client (struct GNUNET_SERVER_Client *client)
216 struct TransportClient *tc;
218 for (tc = clients_head; NULL != tc; tc = tc->next)
219 if (tc->client == client)
226 * Create the internal handle for the given server client handle
228 * @param client server's client handle to create our internal handle for
229 * @return fresh internal client handle
231 static struct TransportClient *
232 setup_client (struct GNUNET_SERVER_Client *client)
234 struct TransportClient *tc;
236 GNUNET_assert (NULL == lookup_client (client));
237 tc = GNUNET_new (struct TransportClient);
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240 "Client %p connected\n",
247 * Find the handle to the monitoring client associated with the given
250 * @param client server's client handle to look up
251 * @return handle to the monitoring client
253 static struct MonitoringClient *
254 lookup_monitoring_client (struct GNUNET_SERVER_Client *client)
256 struct MonitoringClient *mc;
258 for (mc = monitoring_clients_head; NULL != mc; mc = mc->next)
259 if (mc->client == client)
266 * Setup a new monitoring client using the given server client handle and
269 * @param client server's client handle to create our internal handle for
270 * @param peer identity of the peer to monitor the addresses of,
271 * zero to monitor all neighrours.
272 * @return handle to the new monitoring client
274 static struct MonitoringClient *
275 setup_monitoring_client (struct GNUNET_SERVER_Client *client,
276 struct GNUNET_PeerIdentity *peer)
278 struct MonitoringClient *mc;
279 static struct GNUNET_PeerIdentity all_zeros;
281 GNUNET_assert (lookup_monitoring_client (client) == NULL);
282 mc = GNUNET_new (struct MonitoringClient);
285 GNUNET_CONTAINER_DLL_insert (monitoring_clients_head,
286 monitoring_clients_tail,
288 GNUNET_SERVER_notification_context_add (nc, client);
290 if (0 != memcmp (peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Client %p started monitoring of the peer `%s'\n",
293 mc, GNUNET_i2s (peer));
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Client %p started monitoring all peers\n", mc);
302 * Function called to notify a client about the socket being ready to
303 * queue more data. "buf" will be NULL and "size" zero if the socket
304 * was closed for writing in the meantime.
307 * @param size number of bytes available in @a buf
308 * @param buf where the callee should write the message
309 * @return number of bytes written to @a buf
312 transmit_to_client_callback (void *cls, size_t size, void *buf)
314 struct TransportClient *tc = cls;
315 struct ClientMessageQueueEntry *q;
316 const struct GNUNET_MessageHeader *msg;
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325 "Transmission to client failed, closing connection.\n");
330 while (NULL != (q = tc->message_queue_head))
332 msg = (const struct GNUNET_MessageHeader *) &q[1];
333 msize = ntohs (msg->size);
334 if (msize + tsize > size)
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "Transmitting message of type %u to client %p.\n",
338 ntohs (msg->type), tc);
339 GNUNET_CONTAINER_DLL_remove (tc->message_queue_head,
340 tc->message_queue_tail,
343 memcpy (&cbuf[tsize], msg, msize);
349 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
351 GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
352 GNUNET_TIME_UNIT_FOREVER_REL,
353 &transmit_to_client_callback, tc);
354 GNUNET_assert (NULL != tc->th);
361 * Queue the given message for transmission to the given client
363 * @param tc target of the message
364 * @param msg message to transmit
365 * @param may_drop #GNUNET_YES if the message can be dropped
368 unicast (struct TransportClient *tc,
369 const struct GNUNET_MessageHeader *msg,
372 struct ClientMessageQueueEntry *q;
381 if ((tc->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
383 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
384 _("Dropping message of type %u and size %u, have %u/%u messages pending\n"),
389 GNUNET_STATISTICS_update (GST_stats,
391 ("# messages dropped due to slow client"), 1,
395 msize = ntohs (msg->size);
396 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
397 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
398 memcpy (&q[1], msg, msize);
399 GNUNET_CONTAINER_DLL_insert_tail (tc->message_queue_head,
400 tc->message_queue_tail, q);
405 GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
406 GNUNET_TIME_UNIT_FOREVER_REL,
407 &transmit_to_client_callback, tc);
408 GNUNET_assert (NULL != tc->th);
413 * Called whenever a client is disconnected. Frees our
414 * resources associated with that client.
417 * @param client identification of the client
420 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
422 struct TransportClient *tc;
423 struct MonitoringClient *mc;
424 struct ClientMessageQueueEntry *mqe;
428 mc = lookup_monitoring_client (client);
431 GNUNET_CONTAINER_DLL_remove (monitoring_clients_head,
432 monitoring_clients_tail,
436 tc = lookup_client (client);
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
440 "Client %p disconnected, cleaning up.\n", tc);
441 while (NULL != (mqe = tc->message_queue_head))
443 GNUNET_CONTAINER_DLL_remove (tc->message_queue_head, tc->message_queue_tail,
448 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, tc);
451 GNUNET_SERVER_notify_transmit_ready_cancel (tc->th);
454 GNUNET_break (0 == tc->message_count);
460 * Function called for each of our connected neighbours. Notify the
461 * client about the existing neighbour.
463 * @param cls the `struct TransportClient *` to notify
464 * @param peer identity of the neighbour
465 * @param address the address
466 * @param bandwidth_in inbound bandwidth in NBO
467 * @param bandwidth_out outbound bandwidth in NBO
470 notify_client_about_neighbour (void *cls,
471 const struct GNUNET_PeerIdentity *peer,
472 const struct GNUNET_HELLO_Address *address,
473 enum GNUNET_TRANSPORT_PeerState state,
474 struct GNUNET_TIME_Absolute state_timeout,
475 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
476 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
478 struct TransportClient *tc = cls;
479 struct ConnectInfoMessage *cim;
480 size_t size = sizeof (struct ConnectInfoMessage);
481 char buf[size] GNUNET_ALIGN;
483 if (GNUNET_NO == GST_neighbours_test_connected (peer))
486 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
487 cim = (struct ConnectInfoMessage *) buf;
488 cim->header.size = htons (size);
489 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
491 cim->quota_in = bandwidth_in;
492 cim->quota_out = bandwidth_out;
493 unicast (tc, &cim->header, GNUNET_NO);
498 * Initialize a normal client. We got a start message from this
499 * client, add him to the list of clients for broadcasting of inbound
503 * @param client the client
504 * @param message the start message that was sent
507 clients_handle_start (void *cls, struct GNUNET_SERVER_Client *client,
508 const struct GNUNET_MessageHeader *message)
510 const struct StartMessage *start;
511 struct TransportClient *tc;
514 tc = lookup_client (client);
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
517 "Client %p sent START\n", tc);
520 /* got 'start' twice from the same client, not allowed */
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
522 "TransportClient %p ServerClient %p sent multiple START messages\n",
525 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
528 start = (const struct StartMessage *) message;
529 options = ntohl (start->options);
530 if ((0 != (1 & options)) &&
532 memcmp (&start->self, &GST_my_identity,
533 sizeof (struct GNUNET_PeerIdentity))))
535 /* client thinks this is a different peer, reject */
536 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
538 ("Rejecting control connection from peer `%s', which is not me!\n"),
539 GNUNET_i2s (&start->self));
540 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
543 tc = setup_client (client);
544 tc->send_payload = (0 != (2 & options));
545 unicast (tc, GST_hello_get (), GNUNET_NO);
546 GST_neighbours_iterate (¬ify_client_about_neighbour, tc);
547 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, tc);
548 GNUNET_SERVER_receive_done (client, GNUNET_OK);
553 * Client sent us a HELLO. Process the request.
556 * @param client the client
557 * @param message the HELLO message
560 clients_handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
561 const struct GNUNET_MessageHeader *message)
563 GST_validation_handle_hello (message);
564 GNUNET_SERVER_receive_done (client, GNUNET_OK);
569 * Closure for 'handle_send_transmit_continuation'
571 struct SendTransmitContinuationContext
574 * Client that made the request.
576 struct GNUNET_SERVER_Client *client;
579 * Peer that was the target.
581 struct GNUNET_PeerIdentity target;
586 * Function called after the transmission is done. Notify the client that it is
587 * OK to send the next message.
590 * @param success #GNUNET_OK on success, #GNUNET_NO on failure, #GNUNET_SYSERR if we're not connected
591 * @param bytes_payload bytes payload sent
592 * @param bytes_on_wire bytes sent on wire
595 handle_send_transmit_continuation (void *cls, int success,
596 size_t bytes_payload,
597 size_t bytes_on_wire)
599 struct SendTransmitContinuationContext *stcc = cls;
600 struct SendOkMessage send_ok_msg;
602 if (GNUNET_OK == success)
603 GST_neighbours_notify_payload_sent (&stcc->target, bytes_payload);
605 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
606 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
607 send_ok_msg.bytes_msg = htonl (bytes_payload);
608 send_ok_msg.bytes_physical = htonl (bytes_on_wire);
609 send_ok_msg.success = htonl (success);
610 send_ok_msg.latency =
611 GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
612 send_ok_msg.peer = stcc->target;
613 GST_clients_unicast (stcc->client, &send_ok_msg.header, GNUNET_NO);
614 GNUNET_SERVER_client_drop (stcc->client);
620 * Client asked for transmission to a peer. Process the request.
623 * @param client the client
624 * @param message the send message that was sent
627 clients_handle_send (void *cls,
628 struct GNUNET_SERVER_Client *client,
629 const struct GNUNET_MessageHeader *message)
631 const struct OutboundMessage *obm;
632 const struct GNUNET_MessageHeader *obmm;
633 struct SendTransmitContinuationContext *stcc;
636 struct TransportClient *tc;
638 tc = lookup_client (client);
641 /* client asked for transmission before 'START' */
643 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
647 size = ntohs (message->size);
649 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
652 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
655 obm = (const struct OutboundMessage *) message;
656 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
657 msize = size - sizeof (struct OutboundMessage);
658 if (msize < sizeof (struct GNUNET_MessageHeader))
661 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "Received `%s' request from client with target `%4s' and first message of type %u and total size %u\n",
668 GNUNET_i2s (&obm->peer),
671 if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
673 /* not connected, not allowed to send; can happen due to asynchronous operations */
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675 "Could not send message to peer `%s': not connected\n",
676 GNUNET_i2s (&obm->peer));
677 GNUNET_STATISTICS_update (GST_stats,
679 ("# bytes payload dropped (other peer was not connected)"),
681 GNUNET_SERVER_receive_done (client, GNUNET_OK);
684 GNUNET_SERVER_receive_done (client, GNUNET_OK);
685 stcc = GNUNET_new (struct SendTransmitContinuationContext);
686 stcc->target = obm->peer;
687 stcc->client = client;
688 GNUNET_SERVER_client_keep (client);
689 GST_manipulation_send (&obm->peer, obmm, msize,
690 GNUNET_TIME_relative_ntoh (obm->timeout),
691 &handle_send_transmit_continuation, stcc);
696 * Try to initiate a connection to the given peer if the blacklist
699 * @param cls closure (unused, NULL)
700 * @param peer identity of peer that was tested
701 * @param result #GNUNET_OK if the connection is allowed,
705 try_connect_if_allowed (void *cls,
706 const struct GNUNET_PeerIdentity *peer,
709 if (GNUNET_OK != result)
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "Blacklist refuses connection attempt to peer `%s'\n",
714 return; /* not allowed */
716 GST_neighbours_try_connect (peer);
721 * Handle request connect message
723 * @param cls closure (always NULL)
724 * @param client identification of the client
725 * @param message the actual message
728 clients_handle_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
729 const struct GNUNET_MessageHeader *message)
731 const struct TransportRequestConnectMessage *trcm =
732 (const struct TransportRequestConnectMessage *) message;
734 GNUNET_STATISTICS_update (GST_stats,
736 ("# REQUEST CONNECT messages received"), 1,
739 if (0 == memcmp (&trcm->peer, &GST_my_identity,
740 sizeof (struct GNUNET_PeerIdentity)))
743 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
744 "Received a request connect message myself `%s'\n",
745 GNUNET_i2s (&trcm->peer));
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
750 "Received a request connect message for peer `%s'\n",
751 GNUNET_i2s (&trcm->peer));
753 (void) GST_blacklist_test_allowed (&trcm->peer, NULL, &try_connect_if_allowed,
756 GNUNET_SERVER_receive_done (client, GNUNET_OK);
761 * Take the given address and append it to the set of results sent back to
764 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
765 * @param buf text to transmit
768 transmit_address_to_client (void *cls, const char *buf)
770 struct AddressToStringContext *actx = cls;
773 GNUNET_SERVER_transmit_context_append_data (actx->tc, NULL, 0,
774 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
775 GNUNET_SERVER_transmit_context_run (actx->tc, GNUNET_TIME_UNIT_FOREVER_REL);
776 GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, actx);
780 GNUNET_SERVER_transmit_context_append_data (actx->tc, buf, strlen (buf) + 1,
781 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
786 * Client asked to resolve an address. Process the request.
789 * @param client the client
790 * @param message the resolution request
793 clients_handle_address_to_string (void *cls,
794 struct GNUNET_SERVER_Client *client,
795 const struct GNUNET_MessageHeader *message)
797 const struct AddressLookupMessage *alum;
798 struct GNUNET_TRANSPORT_PluginFunctions *papi;
799 const char *plugin_name;
801 uint32_t address_len;
803 struct GNUNET_SERVER_TransmitContext *tc;
804 struct AddressToStringContext *actx;
805 struct GNUNET_TIME_Relative rtimeout;
808 size = ntohs (message->size);
809 if (size < sizeof (struct AddressLookupMessage))
812 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
815 alum = (const struct AddressLookupMessage *) message;
816 address_len = ntohs (alum->addrlen);
817 if (size <= sizeof (struct AddressLookupMessage) + address_len)
820 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
823 address = (const char *) &alum[1];
824 plugin_name = (const char *) &address[address_len];
825 if ('\0' != plugin_name[size - sizeof (struct AddressLookupMessage) - address_len - 1])
828 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
831 rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
832 numeric = ntohs (alum->numeric_only);
833 tc = GNUNET_SERVER_transmit_context_create (client);
834 papi = GST_plugins_printer_find (plugin_name);
837 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
838 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
839 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
842 actx = GNUNET_new (struct AddressToStringContext);
844 GNUNET_CONTAINER_DLL_insert (a2s_head, a2s_tail, actx);
845 GNUNET_SERVER_disable_receive_done_warning (client);
846 papi->address_pretty_printer (papi->cls, plugin_name, address, address_len,
847 numeric, rtimeout, &transmit_address_to_client,
853 * Compose AddressIterateResponseMessage using the given peer and address.
855 * @param peer identity of the peer
856 * @param address the address, NULL on disconnect
857 * @return composed message
859 static struct PeerIterateResponseMessage *
860 compose_address_iterate_response_message (const struct GNUNET_PeerIdentity *peer,
861 const struct GNUNET_HELLO_Address *address)
863 struct PeerIterateResponseMessage *msg;
869 GNUNET_assert (NULL != peer);
872 tlen = strlen (address->transport_name) + 1;
873 alen = address->address_length;
877 size = (sizeof (struct PeerIterateResponseMessage) + alen + tlen);
878 msg = GNUNET_malloc (size);
879 msg->header.size = htons (size);
881 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
882 msg->reserved = htonl (0);
884 msg->addrlen = htonl (alen);
885 msg->pluginlen = htonl (tlen);
888 addr = (char *) &msg[1];
889 memcpy (addr, address->address, alen);
890 memcpy (&addr[alen], address->transport_name, tlen);
896 struct PeerIterationContext
898 struct GNUNET_SERVER_TransmitContext *tc;
900 struct GNUNET_PeerIdentity id;
906 * Output information of neighbours to the given client.
908 * @param cls the 'struct PeerIterationContext'
909 * @param peer identity of the neighbour
910 * @param address the address
911 * @param bandwidth_in inbound quota in NBO
912 * @param bandwidth_out outbound quota in NBO
915 send_peer_information (void *cls,
916 const struct GNUNET_PeerIdentity *peer,
917 const struct GNUNET_HELLO_Address *address,
918 enum GNUNET_TRANSPORT_PeerState state,
919 struct GNUNET_TIME_Absolute state_timeout,
920 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
921 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
923 struct PeerIterationContext *pc = cls;
924 struct PeerIterateResponseMessage *msg;
926 if ( (GNUNET_YES == pc->all) ||
927 (0 == memcmp (peer, &pc->id, sizeof (pc->id))) )
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
930 "Sending information about `%s' using address `%s' in state `%s'\n",
931 GNUNET_i2s(peer), address);
932 msg = compose_address_iterate_response_message (peer, address);
933 msg->state = htonl (state);
934 msg->state_timeout = GNUNET_TIME_absolute_hton(state_timeout);
935 GNUNET_SERVER_transmit_context_append_message (pc->tc, &msg->header);
944 * Client asked to obtain information about a specific or all peers
945 * Process the request.
948 * @param client the client
949 * @param message the peer address information request
952 clients_handle_monitor_peers (void *cls, struct GNUNET_SERVER_Client *client,
953 const struct GNUNET_MessageHeader *message)
955 static struct GNUNET_PeerIdentity all_zeros;
956 struct GNUNET_SERVER_TransmitContext *tc;
957 struct PeerMonitorMessage *msg;
958 struct PeerIterationContext pc;
960 if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST)
963 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
966 if (ntohs (message->size) != sizeof (struct PeerMonitorMessage))
969 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
972 msg = (struct PeerMonitorMessage *) message;
973 if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
974 (NULL != lookup_monitoring_client (client)) )
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
977 "ServerClient %p tried to start monitoring twice\n",
980 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
983 GNUNET_SERVER_disable_receive_done_warning (client);
984 pc.tc = tc = GNUNET_SERVER_transmit_context_create (client);
986 /* Send initial list */
987 if (0 == memcmp (&msg->peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
989 /* iterate over all neighbours */
995 /* just return one neighbour */
999 GST_neighbours_iterate (&send_peer_information, &pc);
1001 if (GNUNET_YES != ntohl (msg->one_shot))
1003 setup_monitoring_client (client, &msg->peer);
1007 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
1008 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
1011 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1016 * Start handling requests from clients.
1018 * @param server server used to accept clients from.
1021 GST_clients_start (struct GNUNET_SERVER_Handle *server)
1023 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1024 {&clients_handle_start, NULL,
1025 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
1026 {&clients_handle_hello, NULL,
1027 GNUNET_MESSAGE_TYPE_HELLO, 0},
1028 {&clients_handle_send, NULL,
1029 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
1030 {&clients_handle_request_connect, NULL,
1031 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT,
1032 sizeof (struct TransportRequestConnectMessage)},
1033 {&clients_handle_address_to_string, NULL,
1034 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING, 0},
1035 {&clients_handle_monitor_peers, NULL,
1036 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST,
1037 sizeof (struct PeerMonitorMessage)},
1038 {&GST_blacklist_handle_init, NULL,
1039 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT,
1040 sizeof (struct GNUNET_MessageHeader)},
1041 {&GST_blacklist_handle_reply, NULL,
1042 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY,
1043 sizeof (struct BlacklistMessage)},
1044 {&GST_manipulation_set_metric, NULL,
1045 GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC, 0},
1048 nc = GNUNET_SERVER_notification_context_create (server, 0);
1049 GNUNET_SERVER_add_handlers (server, handlers);
1050 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
1056 * Stop processing clients.
1061 struct AddressToStringContext *cur;
1063 while (NULL != (cur = a2s_head))
1065 GNUNET_SERVER_transmit_context_destroy (cur->tc, GNUNET_NO);
1066 GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, cur);
1071 GNUNET_SERVER_notification_context_destroy (nc);
1078 * Broadcast the given message to all of our clients.
1080 * @param msg message to broadcast
1081 * @param may_drop #GNUNET_YES if the message can be dropped / is payload
1084 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg, int may_drop)
1086 struct TransportClient *tc;
1088 for (tc = clients_head; NULL != tc; tc = tc->next)
1090 if ((GNUNET_YES == may_drop) && (GNUNET_YES != tc->send_payload))
1091 continue; /* skip, this client does not care about payload */
1092 unicast (tc, msg, may_drop);
1098 * Send the given message to a particular client
1100 * @param client target of the message
1101 * @param msg message to transmit
1102 * @param may_drop #GNUNET_YES if the message can be dropped
1105 GST_clients_unicast (struct GNUNET_SERVER_Client *client,
1106 const struct GNUNET_MessageHeader *msg, int may_drop)
1108 struct TransportClient *tc;
1110 tc = lookup_client (client);
1112 return; /* client got disconnected in the meantime, drop message */
1113 unicast (tc, msg, may_drop);
1118 * Broadcast the new active address to all clients monitoring the peer.
1120 * @param peer peer this update is about (never NULL)
1121 * @param address address, NULL on disconnect
1124 GST_clients_broadcast_address_notification (const struct GNUNET_PeerIdentity *peer,
1125 const struct GNUNET_HELLO_Address *address)
1127 struct PeerIterateResponseMessage *msg;
1128 struct MonitoringClient *mc;
1129 static struct GNUNET_PeerIdentity all_zeros;
1130 msg = compose_address_iterate_response_message (peer, address);
1131 mc = monitoring_clients_head;
1134 if ((0 == memcmp (&mc->peer, &all_zeros,
1135 sizeof (struct GNUNET_PeerIdentity))) ||
1136 (0 == memcmp (&mc->peer, peer,
1137 sizeof (struct GNUNET_PeerIdentity))))
1139 GNUNET_SERVER_notification_context_unicast (nc, mc->client,
1140 &msg->header, GNUNET_NO);
1149 /* end of file gnunet-service-transport_clients.c */