2 This file is part of GNUnet.
3 (C) 2010,2011 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.h"
34 #include "transport.h"
38 * How many messages can we have pending for a given client process
39 * before we start to drop incoming messages? We typically should
40 * have only one client and so this would be the primary buffer for
41 * messages, so the number should be chosen rather generously.
43 * The expectation here is that most of the time the queue is large
44 * enough so that a drop is virtually never required. Note that
45 * this value must be about as large as 'TOTAL_MSGS' in the
46 * 'test_transport_api_reliability.c', otherwise that testcase may
49 #define MAX_PENDING (128 * 1024)
53 * Linked list of messages to be transmitted to the client. Each
54 * entry is followed by the actual message.
56 struct ClientMessageQueueEntry
59 * This is a doubly-linked list.
61 struct ClientMessageQueueEntry *next;
64 * This is a doubly-linked list.
66 struct ClientMessageQueueEntry *prev;
71 * Client connected to the transport service.
73 struct TransportClient
77 * This is a doubly-linked list.
79 struct TransportClient *next;
82 * This is a doubly-linked list.
84 struct TransportClient *prev;
87 * Handle to the client.
89 struct GNUNET_SERVER_Client *client;
92 * Linked list of messages yet to be transmitted to
95 struct ClientMessageQueueEntry *message_queue_head;
98 * Tail of linked list of messages yet to be transmitted to the
101 struct ClientMessageQueueEntry *message_queue_tail;
104 * Current transmit request handle.
106 struct GNUNET_SERVER_TransmitHandle *th;
109 * Length of the list of messages pending for this client.
111 unsigned int message_count;
114 * Is this client interested in payload messages?
120 * Context for address to string operations
122 struct AddressToStringContext
125 * This is a doubly-linked list.
127 struct AddressToStringContext *next;
130 * This is a doubly-linked list.
132 struct AddressToStringContext *prev;
135 * Transmission context
137 struct GNUNET_SERVER_TransmitContext* tc;
141 * Client monitoring changes of active addresses of our neighbours.
143 struct MonitoringClient
146 * This is a doubly-linked list.
148 struct MonitoringClient *next;
151 * This is a doubly-linked list.
153 struct MonitoringClient *prev;
156 * Handle to the client.
158 struct GNUNET_SERVER_Client *client;
161 * Peer identity to monitor the addresses of.
162 * Zero to monitor all neighrours.
164 struct GNUNET_PeerIdentity peer;
170 * Head of linked list of all clients to this service.
172 static struct TransportClient *clients_head;
175 * Tail of linked list of all clients to this service.
177 static struct TransportClient *clients_tail;
180 * Head of linked list of all pending address iterations
182 struct AddressToStringContext *a2s_head;
185 * Tail of linked list of all pending address iterations
187 struct AddressToStringContext *a2s_tail;
190 * Head of linked list of monitoring clients.
192 static struct MonitoringClient *monitoring_clients_head;
195 * Tail of linked list of monitoring clients.
197 static struct MonitoringClient *monitoring_clients_tail;
200 * Notification context, to send updates on changes to active addresses
203 struct GNUNET_SERVER_NotificationContext *nc = NULL;
207 * Find the internal handle associated with the given client handle
209 * @param client server's client handle to look up
210 * @return internal client handle
212 static struct TransportClient *
213 lookup_client (struct GNUNET_SERVER_Client *client)
215 struct TransportClient *tc;
220 if (tc->client == client)
229 * Create the internal handle for the given server client handle
231 * @param client server's client handle to create our internal handle for
232 * @return fresh internal client handle
234 static struct TransportClient *
235 setup_client (struct GNUNET_SERVER_Client *client)
237 struct TransportClient *tc;
239 GNUNET_assert (lookup_client (client) == NULL);
240 tc = GNUNET_malloc (sizeof (struct TransportClient));
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", tc);
248 * Find the handle to the monitoring client associated with the given
251 * @param client server's client handle to look up
252 * @return handle to the monitoring client
254 static struct MonitoringClient *
255 lookup_monitoring_client (struct GNUNET_SERVER_Client *client)
257 struct MonitoringClient *mc;
259 mc = monitoring_clients_head;
262 if (mc->client == client)
271 * Setup a new monitoring client using the given server client handle and
274 * @param client server's client handle to create our internal handle for
275 * @param peer identity of the peer to monitor the addresses of,
276 * zero to monitor all neighrours.
277 * @return handle to the new monitoring client
279 static struct MonitoringClient *
280 setup_monitoring_client (struct GNUNET_SERVER_Client *client,
281 struct GNUNET_PeerIdentity *peer)
283 struct MonitoringClient *mc;
284 static struct GNUNET_PeerIdentity all_zeros;
286 GNUNET_assert (lookup_monitoring_client (client) == NULL);
287 mc = GNUNET_malloc (sizeof (struct MonitoringClient));
290 GNUNET_CONTAINER_DLL_insert (monitoring_clients_head,
291 monitoring_clients_tail,
293 GNUNET_SERVER_notification_context_add (nc, client);
295 if (0 != memcmp (peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297 "Client %p started monitoring of the peer `%s'\n",
298 mc, GNUNET_i2s (peer));
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "Client %p started monitoring all peers\n", mc);
307 * Function called to notify a client about the socket being ready to
308 * queue more data. "buf" will be NULL and "size" zero if the socket
309 * was closed for writing in the meantime.
312 * @param size number of bytes available in buf
313 * @param buf where the callee should write the message
314 * @return number of bytes written to buf
317 transmit_to_client_callback (void *cls, size_t size, void *buf)
319 struct TransportClient *tc = cls;
320 struct ClientMessageQueueEntry *q;
321 const struct GNUNET_MessageHeader *msg;
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
330 "Transmission to client failed, closing connection.\n");
335 while (NULL != (q = tc->message_queue_head))
337 msg = (const struct GNUNET_MessageHeader *) &q[1];
338 msize = ntohs (msg->size);
339 if (msize + tsize > size)
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 "Transmitting message of type %u to client %p.\n",
343 ntohs (msg->type), tc);
344 GNUNET_CONTAINER_DLL_remove (tc->message_queue_head, tc->message_queue_tail,
347 memcpy (&cbuf[tsize], msg, msize);
353 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
355 GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
356 GNUNET_TIME_UNIT_FOREVER_REL,
357 &transmit_to_client_callback, tc);
358 GNUNET_assert (tc->th != NULL);
365 * Queue the given message for transmission to the given client
367 * @param tc target of the message
368 * @param msg message to transmit
369 * @param may_drop GNUNET_YES if the message can be dropped
372 unicast (struct TransportClient *tc, const struct GNUNET_MessageHeader *msg,
375 struct ClientMessageQueueEntry *q;
384 if ((tc->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
386 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
388 ("Dropping message of type %u and size %u, have %u/%u messages pending\n"),
389 ntohs (msg->type), ntohs (msg->size), tc->message_count,
391 GNUNET_STATISTICS_update (GST_stats,
393 ("# messages dropped due to slow client"), 1,
397 msize = ntohs (msg->size);
398 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
399 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
400 memcpy (&q[1], msg, msize);
401 GNUNET_CONTAINER_DLL_insert_tail (tc->message_queue_head,
402 tc->message_queue_tail, q);
407 GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
408 GNUNET_TIME_UNIT_FOREVER_REL,
409 &transmit_to_client_callback, tc);
410 GNUNET_assert (tc->th != NULL);
415 * Called whenever a client is disconnected. Frees our
416 * resources associated with that client.
419 * @param client identification of the client
422 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
424 struct TransportClient *tc;
425 struct MonitoringClient *mc;
426 struct ClientMessageQueueEntry *mqe;
430 mc = lookup_monitoring_client (client);
433 GNUNET_CONTAINER_DLL_remove (monitoring_clients_head,
434 monitoring_clients_tail,
438 tc = lookup_client (client);
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
442 "Client %p disconnected, cleaning up.\n", tc);
443 while (NULL != (mqe = tc->message_queue_head))
445 GNUNET_CONTAINER_DLL_remove (tc->message_queue_head, tc->message_queue_tail,
450 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, tc);
453 GNUNET_SERVER_notify_transmit_ready_cancel (tc->th);
456 GNUNET_break (0 == tc->message_count);
462 * Function called for each of our connected neighbours. Notify the
463 * client about the existing neighbour.
465 * @param cls the 'struct TransportClient' to notify
466 * @param peer identity of the neighbour
467 * @param ats performance data
468 * @param ats_count number of entries in ats (excluding 0-termination)
469 * @param address the address
470 * @param bandwidth_in inbound bandwidth in NBO
471 * @param bandwidth_out outbound bandwidth in NBO
474 notify_client_about_neighbour (void *cls,
475 const struct GNUNET_PeerIdentity *peer,
476 const struct GNUNET_ATS_Information *ats,
478 const struct GNUNET_HELLO_Address *address,
479 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
480 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
482 struct TransportClient *tc = cls;
483 struct ConnectInfoMessage *cim;
484 struct GNUNET_ATS_Information *ap;
486 sizeof (struct ConnectInfoMessage) +
487 ats_count * sizeof (struct GNUNET_ATS_Information);
488 char buf[size] GNUNET_ALIGN;
490 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
491 cim = (struct ConnectInfoMessage *) buf;
492 cim->header.size = htons (size);
493 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
494 cim->ats_count = htonl (ats_count);
496 cim->quota_in = bandwidth_in;
497 cim->quota_out = bandwidth_out;
498 ap = (struct GNUNET_ATS_Information *) &cim[1];
499 memcpy (ap, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
500 unicast (tc, &cim->header, GNUNET_NO);
505 * Initialize a normal client. We got a start message from this
506 * client, add him to the list of clients for broadcasting of inbound
510 * @param client the client
511 * @param message the start message that was sent
514 clients_handle_start (void *cls, struct GNUNET_SERVER_Client *client,
515 const struct GNUNET_MessageHeader *message)
517 const struct StartMessage *start;
518 struct TransportClient *tc;
521 tc = lookup_client (client);
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
524 "Client %p sent START\n", tc);
527 /* got 'start' twice from the same client, not allowed */
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
529 "TransportClient %p ServerClient %p sent multiple START messages\n",
532 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
535 start = (const struct StartMessage *) message;
536 options = ntohl (start->options);
537 if ((0 != (1 & options)) &&
539 memcmp (&start->self, &GST_my_identity,
540 sizeof (struct GNUNET_PeerIdentity))))
542 /* client thinks this is a different peer, reject */
543 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
545 ("Rejecting control connection from peer `%s', which is not me!\n"),
546 GNUNET_i2s (&start->self));
547 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
550 tc = setup_client (client);
551 tc->send_payload = (0 != (2 & options));
552 unicast (tc, GST_hello_get (), GNUNET_NO);
553 GST_neighbours_iterate (¬ify_client_about_neighbour, tc);
554 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, tc);
555 GNUNET_SERVER_receive_done (client, GNUNET_OK);
560 * Client sent us a HELLO. Process the request.
563 * @param client the client
564 * @param message the HELLO message
567 clients_handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
568 const struct GNUNET_MessageHeader *message)
570 GST_validation_handle_hello (message);
571 GNUNET_SERVER_receive_done (client, GNUNET_OK);
576 * Closure for 'handle_send_transmit_continuation'
578 struct SendTransmitContinuationContext
581 * Client that made the request.
583 struct GNUNET_SERVER_Client *client;
586 * Peer that was the target.
588 struct GNUNET_PeerIdentity target;
593 * Function called after the transmission is done. Notify the client that it is
594 * OK to send the next message.
597 * @param success GNUNET_OK on success, GNUNET_NO on failure, GNUNET_SYSERR if we're not connected
598 * @param bytes_payload bytes payload sent
599 * @param bytes_on_wire bytes sent on wire
602 handle_send_transmit_continuation (void *cls, int success,
603 size_t bytes_payload, size_t bytes_on_wire)
605 struct SendTransmitContinuationContext *stcc = cls;
606 struct SendOkMessage send_ok_msg;
608 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Payload: %u, On wire %u result: %i\n", bytes_payload, bytes_on_wire, success);
610 if (GNUNET_OK == success)
611 GNUNET_assert (bytes_on_wire >= bytes_payload);
614 GNUNET_assert (bytes_on_wire <= bytes_payload);
616 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
617 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
618 send_ok_msg.bytes_msg = htonl (bytes_payload);
619 send_ok_msg.bytes_physical = htonl (bytes_on_wire);
620 send_ok_msg.success = htonl (success);
621 send_ok_msg.latency =
622 GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
623 send_ok_msg.peer = stcc->target;
624 GST_clients_unicast (stcc->client, &send_ok_msg.header, GNUNET_NO);
625 GNUNET_SERVER_client_drop (stcc->client);
631 * Client asked for transmission to a peer. Process the request.
634 * @param client the client
635 * @param message the send message that was sent
638 clients_handle_send (void *cls, struct GNUNET_SERVER_Client *client,
639 const struct GNUNET_MessageHeader *message)
641 const struct OutboundMessage *obm;
642 const struct GNUNET_MessageHeader *obmm;
643 struct SendTransmitContinuationContext *stcc;
646 struct TransportClient *tc;
648 tc = lookup_client (client);
651 /* client asked for transmission before 'START' */
653 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
657 size = ntohs (message->size);
659 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
662 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
665 obm = (const struct OutboundMessage *) message;
666 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
667 msize = size - sizeof (struct OutboundMessage);
668 if (msize < sizeof (struct GNUNET_MessageHeader))
671 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "Received `%s' request from client with target `%4s' and first message of type %u and total size %u\n",
677 "SEND", GNUNET_i2s (&obm->peer), ntohs (obmm->type), msize);
678 if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
680 /* not connected, not allowed to send; can happen due to asynchronous operations */
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682 "Could not send message to peer `%s': not connected\n",
683 GNUNET_i2s (&obm->peer));
684 GNUNET_STATISTICS_update (GST_stats,
686 ("# bytes payload dropped (other peer was not connected)"),
688 GNUNET_SERVER_receive_done (client, GNUNET_OK);
691 GNUNET_SERVER_receive_done (client, GNUNET_OK);
692 stcc = GNUNET_malloc (sizeof (struct SendTransmitContinuationContext));
693 stcc->target = obm->peer;
694 stcc->client = client;
695 GNUNET_SERVER_client_keep (client);
696 GST_neighbours_send (&obm->peer, obmm, msize,
697 GNUNET_TIME_relative_ntoh (obm->timeout),
698 &handle_send_transmit_continuation, stcc);
703 * Try to initiate a connection to the given peer if the blacklist
706 * @param cls closure (unused, NULL)
707 * @param peer identity of peer that was tested
708 * @param result GNUNET_OK if the connection is allowed,
712 try_connect_if_allowed (void *cls, const struct GNUNET_PeerIdentity *peer,
715 if (GNUNET_OK != result)
716 return; /* not allowed */
717 GST_neighbours_try_connect (peer);
722 * Handle request connect message
724 * @param cls closure (always NULL)
725 * @param client identification of the client
726 * @param message the actual message
729 clients_handle_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
730 const struct GNUNET_MessageHeader *message)
732 const struct TransportRequestConnectMessage *trcm =
733 (const struct TransportRequestConnectMessage *) message;
735 GNUNET_STATISTICS_update (GST_stats,
737 ("# REQUEST CONNECT messages received"), 1,
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 "Received a request connect message for peer `%s'\n",
741 GNUNET_i2s (&trcm->peer));
742 (void) GST_blacklist_test_allowed (&trcm->peer, NULL, &try_connect_if_allowed,
744 GNUNET_SERVER_receive_done (client, GNUNET_OK);
749 * Take the given address and append it to the set of results sent back to
752 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
753 * @param buf text to transmit
756 transmit_address_to_client (void *cls, const char *buf)
758 struct AddressToStringContext *actx = cls;
761 GNUNET_SERVER_transmit_context_append_data (actx->tc, NULL, 0,
762 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
763 GNUNET_SERVER_transmit_context_run (actx->tc, GNUNET_TIME_UNIT_FOREVER_REL);
764 GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, actx);
768 GNUNET_SERVER_transmit_context_append_data (actx->tc, buf, strlen (buf) + 1,
769 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
774 * Client asked to resolve an address. Process the request.
777 * @param client the client
778 * @param message the resolution request
781 clients_handle_address_to_string (void *cls,
782 struct GNUNET_SERVER_Client *client,
783 const struct GNUNET_MessageHeader *message)
785 const struct AddressLookupMessage *alum;
786 struct GNUNET_TRANSPORT_PluginFunctions *papi;
787 const char *plugin_name;
789 uint32_t address_len;
791 struct GNUNET_SERVER_TransmitContext *tc;
792 struct AddressToStringContext *actx;
793 struct GNUNET_TIME_Relative rtimeout;
796 size = ntohs (message->size);
797 if (size < sizeof (struct AddressLookupMessage))
800 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
803 alum = (const struct AddressLookupMessage *) message;
804 address_len = ntohs (alum->addrlen);
805 if (size <= sizeof (struct AddressLookupMessage) + address_len)
808 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
811 address = (const char *) &alum[1];
812 plugin_name = (const char *) &address[address_len];
813 if (plugin_name[size - sizeof (struct AddressLookupMessage) - address_len - 1]
817 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
820 rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
821 numeric = ntohs (alum->numeric_only);
822 tc = GNUNET_SERVER_transmit_context_create (client);
823 papi = GST_plugins_printer_find (plugin_name);
826 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
827 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
828 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
831 actx = GNUNET_malloc (sizeof (struct AddressToStringContext));
833 GNUNET_CONTAINER_DLL_insert (a2s_head, a2s_tail, actx);
834 GNUNET_SERVER_disable_receive_done_warning (client);
835 papi->address_pretty_printer (papi->cls, plugin_name, address, address_len,
836 numeric, rtimeout, &transmit_address_to_client,
842 * Compose AddressIterateResponseMessage using the given peer and address.
844 * @param peer identity of the peer
845 * @param address the address, NULL on disconnect
846 * @return composed message
848 static struct AddressIterateResponseMessage *
849 compose_address_iterate_response_message (const struct GNUNET_PeerIdentity
851 const struct GNUNET_HELLO_Address
854 struct AddressIterateResponseMessage *msg;
860 GNUNET_assert (NULL != peer);
863 tlen = strlen (address->transport_name) + 1;
864 alen = address->address_length;
868 size = (sizeof (struct AddressIterateResponseMessage) + alen + tlen);
869 msg = GNUNET_malloc (size);
870 msg->header.size = htons (size);
872 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
873 msg->reserved = htonl (0);
875 msg->addrlen = htonl (alen);
876 msg->pluginlen = htonl (tlen);
879 addr = (char *) &msg[1];
880 memcpy (addr, address->address, alen);
881 memcpy (&addr[alen], address->transport_name, tlen);
888 * Output the active address of connected neighbours to the given client.
890 * @param cls the 'struct GNUNET_SERVER_TransmitContext' for transmission to the client
891 * @param peer identity of the neighbour
892 * @param ats performance data
893 * @param ats_count number of entries in ats (excluding 0-termination)
894 * @param address the address
895 * @param bandwidth_in inbound quota in NBO
896 * @param bandwidth_out outbound quota in NBO
899 output_address (void *cls, const struct GNUNET_PeerIdentity *peer,
900 const struct GNUNET_ATS_Information *ats, uint32_t ats_count,
901 const struct GNUNET_HELLO_Address *address,
902 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
903 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
905 struct GNUNET_SERVER_TransmitContext *tc = cls;
906 struct AddressIterateResponseMessage *msg;
908 msg = compose_address_iterate_response_message (peer, address);
909 GNUNET_SERVER_transmit_context_append_message (tc, &msg->header);
915 * Client asked to obtain information about all actively used addresses
917 * Process the request.
920 * @param client the client
921 * @param message the peer address information request
924 clients_handle_address_iterate (void *cls, struct GNUNET_SERVER_Client *client,
925 const struct GNUNET_MessageHeader *message)
927 static struct GNUNET_PeerIdentity all_zeros;
928 struct GNUNET_SERVER_TransmitContext *tc;
929 struct AddressIterateMessage *msg;
930 struct GNUNET_HELLO_Address *address;
932 if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE)
935 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
938 if (ntohs (message->size) != sizeof (struct AddressIterateMessage))
941 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
944 msg = (struct AddressIterateMessage *) message;
945 if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
946 (NULL != lookup_monitoring_client (client)) )
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
949 "ServerClient %p tried to start monitoring twice\n",
952 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
955 GNUNET_SERVER_disable_receive_done_warning (client);
956 tc = GNUNET_SERVER_transmit_context_create (client);
957 if (0 == memcmp (&msg->peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
959 /* iterate over all neighbours */
960 GST_neighbours_iterate (&output_address, tc);
964 /* just return one neighbour */
965 address = GST_neighbour_get_current_address (&msg->peer);
967 output_address (tc, &msg->peer, NULL, 0, address,
968 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
969 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT);
971 if (GNUNET_YES != ntohl (msg->one_shot))
972 setup_monitoring_client (client, &msg->peer);
974 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
975 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
976 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
981 * Start handling requests from clients.
983 * @param server server used to accept clients from.
986 GST_clients_start (struct GNUNET_SERVER_Handle *server)
988 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
989 {&clients_handle_start, NULL,
990 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
991 {&clients_handle_hello, NULL,
992 GNUNET_MESSAGE_TYPE_HELLO, 0},
993 {&clients_handle_send, NULL,
994 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
995 {&clients_handle_request_connect, NULL,
996 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT,
997 sizeof (struct TransportRequestConnectMessage)},
998 {&clients_handle_address_to_string, NULL,
999 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING, 0},
1000 {&clients_handle_address_iterate, NULL,
1001 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE,
1002 sizeof (struct AddressIterateMessage)},
1003 {&GST_blacklist_handle_init, NULL,
1004 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT,
1005 sizeof (struct GNUNET_MessageHeader)},
1006 {&GST_blacklist_handle_reply, NULL,
1007 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY,
1008 sizeof (struct BlacklistMessage)},
1011 nc = GNUNET_SERVER_notification_context_create (server, 0);
1012 GNUNET_SERVER_add_handlers (server, handlers);
1013 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
1019 * Stop processing clients.
1024 struct AddressToStringContext *cur;
1026 while (NULL != (cur = a2s_head))
1028 GNUNET_SERVER_transmit_context_destroy (cur->tc, GNUNET_NO);
1029 GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, cur);
1035 GNUNET_SERVER_notification_context_destroy (nc);
1041 * Broadcast the given message to all of our clients.
1043 * @param msg message to broadcast
1044 * @param may_drop GNUNET_YES if the message can be dropped / is payload
1047 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg, int may_drop)
1049 struct TransportClient *tc;
1051 for (tc = clients_head; tc != NULL; tc = tc->next)
1053 if ((GNUNET_YES == may_drop) && (GNUNET_YES != tc->send_payload))
1054 continue; /* skip, this client does not care about payload */
1055 unicast (tc, msg, may_drop);
1061 * Send the given message to a particular client
1063 * @param client target of the message
1064 * @param msg message to transmit
1065 * @param may_drop GNUNET_YES if the message can be dropped
1068 GST_clients_unicast (struct GNUNET_SERVER_Client *client,
1069 const struct GNUNET_MessageHeader *msg, int may_drop)
1071 struct TransportClient *tc;
1073 tc = lookup_client (client);
1075 return; /* client got disconnected in the meantime, drop message */
1076 unicast (tc, msg, may_drop);
1081 * Broadcast the new active address to all clients monitoring the peer.
1083 * @param peer peer this update is about (never NULL)
1084 * @param address address, NULL on disconnect
1087 GST_clients_broadcast_address_notification (const struct GNUNET_PeerIdentity
1089 const struct GNUNET_HELLO_Address
1092 struct AddressIterateResponseMessage *msg;
1093 struct MonitoringClient *mc;
1094 static struct GNUNET_PeerIdentity all_zeros;
1095 msg = compose_address_iterate_response_message (peer, address);
1096 mc = monitoring_clients_head;
1099 if ((0 == memcmp (&mc->peer, &all_zeros,
1100 sizeof (struct GNUNET_PeerIdentity))) ||
1101 (0 == memcmp (&mc->peer, peer,
1102 sizeof (struct GNUNET_PeerIdentity))))
1104 GNUNET_SERVER_notification_context_unicast (nc, mc->client,
1105 &msg->header, GNUNET_NO);
1114 /* end of file gnunet-service-transport_clients.c */