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_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 struct GNUNET_SERVER_NotificationContext *nc = NULL;
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;
221 if (tc->client == client)
230 * Create the internal handle for the given server client handle
232 * @param client server's client handle to create our internal handle for
233 * @return fresh internal client handle
235 static struct TransportClient *
236 setup_client (struct GNUNET_SERVER_Client *client)
238 struct TransportClient *tc;
240 GNUNET_assert (lookup_client (client) == NULL);
241 tc = GNUNET_malloc (sizeof (struct TransportClient));
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", tc);
249 * Find the handle to the monitoring client associated with the given
252 * @param client server's client handle to look up
253 * @return handle to the monitoring client
255 static struct MonitoringClient *
256 lookup_monitoring_client (struct GNUNET_SERVER_Client *client)
258 struct MonitoringClient *mc;
260 mc = monitoring_clients_head;
263 if (mc->client == client)
272 * Setup a new monitoring client using the given server client handle and
275 * @param client server's client handle to create our internal handle for
276 * @param peer identity of the peer to monitor the addresses of,
277 * zero to monitor all neighrours.
278 * @return handle to the new monitoring client
280 static struct MonitoringClient *
281 setup_monitoring_client (struct GNUNET_SERVER_Client *client,
282 struct GNUNET_PeerIdentity *peer)
284 struct MonitoringClient *mc;
285 static struct GNUNET_PeerIdentity all_zeros;
287 GNUNET_assert (lookup_monitoring_client (client) == NULL);
288 mc = GNUNET_malloc (sizeof (struct MonitoringClient));
291 GNUNET_CONTAINER_DLL_insert (monitoring_clients_head,
292 monitoring_clients_tail,
294 GNUNET_SERVER_notification_context_add (nc, client);
296 if (0 != memcmp (peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "Client %p started monitoring of the peer `%s'\n",
299 mc, GNUNET_i2s (peer));
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Client %p started monitoring all peers\n", mc);
308 * Function called to notify a client about the socket being ready to
309 * queue more data. "buf" will be NULL and "size" zero if the socket
310 * was closed for writing in the meantime.
313 * @param size number of bytes available in buf
314 * @param buf where the callee should write the message
315 * @return number of bytes written to buf
318 transmit_to_client_callback (void *cls, size_t size, void *buf)
320 struct TransportClient *tc = cls;
321 struct ClientMessageQueueEntry *q;
322 const struct GNUNET_MessageHeader *msg;
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "Transmission to client failed, closing connection.\n");
336 while (NULL != (q = tc->message_queue_head))
338 msg = (const struct GNUNET_MessageHeader *) &q[1];
339 msize = ntohs (msg->size);
340 if (msize + tsize > size)
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "Transmitting message of type %u to client %p.\n",
344 ntohs (msg->type), tc);
345 GNUNET_CONTAINER_DLL_remove (tc->message_queue_head, tc->message_queue_tail,
348 memcpy (&cbuf[tsize], msg, msize);
354 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
356 GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
357 GNUNET_TIME_UNIT_FOREVER_REL,
358 &transmit_to_client_callback, tc);
359 GNUNET_assert (tc->th != NULL);
366 * Queue the given message for transmission to the given client
368 * @param tc target of the message
369 * @param msg message to transmit
370 * @param may_drop GNUNET_YES if the message can be dropped
373 unicast (struct TransportClient *tc, const struct GNUNET_MessageHeader *msg,
376 struct ClientMessageQueueEntry *q;
385 if ((tc->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
387 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
389 ("Dropping message of type %u and size %u, have %u/%u messages pending\n"),
390 ntohs (msg->type), ntohs (msg->size), tc->message_count,
392 GNUNET_STATISTICS_update (GST_stats,
394 ("# messages dropped due to slow client"), 1,
398 msize = ntohs (msg->size);
399 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
400 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
401 memcpy (&q[1], msg, msize);
402 GNUNET_CONTAINER_DLL_insert_tail (tc->message_queue_head,
403 tc->message_queue_tail, q);
408 GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
409 GNUNET_TIME_UNIT_FOREVER_REL,
410 &transmit_to_client_callback, tc);
411 GNUNET_assert (tc->th != NULL);
416 * Called whenever a client is disconnected. Frees our
417 * resources associated with that client.
420 * @param client identification of the client
423 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
425 struct TransportClient *tc;
426 struct MonitoringClient *mc;
427 struct ClientMessageQueueEntry *mqe;
431 mc = lookup_monitoring_client (client);
434 GNUNET_CONTAINER_DLL_remove (monitoring_clients_head,
435 monitoring_clients_tail,
439 tc = lookup_client (client);
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
443 "Client %p disconnected, cleaning up.\n", tc);
444 while (NULL != (mqe = tc->message_queue_head))
446 GNUNET_CONTAINER_DLL_remove (tc->message_queue_head, tc->message_queue_tail,
451 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, tc);
454 GNUNET_SERVER_notify_transmit_ready_cancel (tc->th);
457 GNUNET_break (0 == tc->message_count);
463 * Function called for each of our connected neighbours. Notify the
464 * client about the existing neighbour.
466 * @param cls the 'struct TransportClient' to notify
467 * @param peer identity of the neighbour
468 * @param address the address
469 * @param bandwidth_in inbound bandwidth in NBO
470 * @param bandwidth_out outbound bandwidth in NBO
473 notify_client_about_neighbour (void *cls,
474 const struct GNUNET_PeerIdentity *peer,
475 const struct GNUNET_HELLO_Address *address,
476 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
477 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
479 struct TransportClient *tc = cls;
480 struct ConnectInfoMessage *cim;
481 size_t size = sizeof (struct ConnectInfoMessage);
482 char buf[size] GNUNET_ALIGN;
484 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
485 cim = (struct ConnectInfoMessage *) buf;
486 cim->header.size = htons (size);
487 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
489 cim->quota_in = bandwidth_in;
490 cim->quota_out = bandwidth_out;
491 unicast (tc, &cim->header, GNUNET_NO);
496 * Initialize a normal client. We got a start message from this
497 * client, add him to the list of clients for broadcasting of inbound
501 * @param client the client
502 * @param message the start message that was sent
505 clients_handle_start (void *cls, struct GNUNET_SERVER_Client *client,
506 const struct GNUNET_MessageHeader *message)
508 const struct StartMessage *start;
509 struct TransportClient *tc;
512 tc = lookup_client (client);
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
515 "Client %p sent START\n", tc);
518 /* got 'start' twice from the same client, not allowed */
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
520 "TransportClient %p ServerClient %p sent multiple START messages\n",
523 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
526 start = (const struct StartMessage *) message;
527 options = ntohl (start->options);
528 if ((0 != (1 & options)) &&
530 memcmp (&start->self, &GST_my_identity,
531 sizeof (struct GNUNET_PeerIdentity))))
533 /* client thinks this is a different peer, reject */
534 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
536 ("Rejecting control connection from peer `%s', which is not me!\n"),
537 GNUNET_i2s (&start->self));
538 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
541 tc = setup_client (client);
542 tc->send_payload = (0 != (2 & options));
543 unicast (tc, GST_hello_get (), GNUNET_NO);
544 GST_neighbours_iterate (¬ify_client_about_neighbour, tc);
545 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, tc);
546 GNUNET_SERVER_receive_done (client, GNUNET_OK);
551 * Client sent us a HELLO. Process the request.
554 * @param client the client
555 * @param message the HELLO message
558 clients_handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
559 const struct GNUNET_MessageHeader *message)
561 GST_validation_handle_hello (message);
562 GNUNET_SERVER_receive_done (client, GNUNET_OK);
567 * Closure for 'handle_send_transmit_continuation'
569 struct SendTransmitContinuationContext
572 * Client that made the request.
574 struct GNUNET_SERVER_Client *client;
577 * Peer that was the target.
579 struct GNUNET_PeerIdentity target;
584 * Function called after the transmission is done. Notify the client that it is
585 * OK to send the next message.
588 * @param success GNUNET_OK on success, GNUNET_NO on failure, GNUNET_SYSERR if we're not connected
589 * @param bytes_payload bytes payload sent
590 * @param bytes_on_wire bytes sent on wire
593 handle_send_transmit_continuation (void *cls, int success,
594 size_t bytes_payload, size_t bytes_on_wire)
596 struct SendTransmitContinuationContext *stcc = cls;
597 struct SendOkMessage send_ok_msg;
599 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Payload: %u, On wire %u result: %i\n", bytes_payload, bytes_on_wire, success);
601 if (GNUNET_OK == success)
602 GNUNET_assert (bytes_on_wire >= bytes_payload);
605 GNUNET_assert (bytes_on_wire <= bytes_payload);
607 send_ok_msg.header.size = htons (sizeof (send_ok_msg));
608 send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
609 send_ok_msg.bytes_msg = htonl (bytes_payload);
610 send_ok_msg.bytes_physical = htonl (bytes_on_wire);
611 send_ok_msg.success = htonl (success);
612 send_ok_msg.latency =
613 GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
614 send_ok_msg.peer = stcc->target;
615 GST_clients_unicast (stcc->client, &send_ok_msg.header, GNUNET_NO);
616 GNUNET_SERVER_client_drop (stcc->client);
622 * Client asked for transmission to a peer. Process the request.
625 * @param client the client
626 * @param message the send message that was sent
629 clients_handle_send (void *cls, struct GNUNET_SERVER_Client *client,
630 const struct GNUNET_MessageHeader *message)
632 const struct OutboundMessage *obm;
633 const struct GNUNET_MessageHeader *obmm;
634 struct SendTransmitContinuationContext *stcc;
637 struct TransportClient *tc;
639 tc = lookup_client (client);
642 /* client asked for transmission before 'START' */
644 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
648 size = ntohs (message->size);
650 sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
653 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
656 obm = (const struct OutboundMessage *) message;
657 obmm = (const struct GNUNET_MessageHeader *) &obm[1];
658 msize = size - sizeof (struct OutboundMessage);
659 if (msize < sizeof (struct GNUNET_MessageHeader))
662 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "Received `%s' request from client with target `%4s' and first message of type %u and total size %u\n",
668 "SEND", GNUNET_i2s (&obm->peer), ntohs (obmm->type), msize);
669 if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
671 /* not connected, not allowed to send; can happen due to asynchronous operations */
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
673 "Could not send message to peer `%s': not connected\n",
674 GNUNET_i2s (&obm->peer));
675 GNUNET_STATISTICS_update (GST_stats,
677 ("# bytes payload dropped (other peer was not connected)"),
679 GNUNET_SERVER_receive_done (client, GNUNET_OK);
682 GNUNET_SERVER_receive_done (client, GNUNET_OK);
683 stcc = GNUNET_malloc (sizeof (struct SendTransmitContinuationContext));
684 stcc->target = obm->peer;
685 stcc->client = client;
686 GNUNET_SERVER_client_keep (client);
687 GST_manipulation_send (&obm->peer, obmm, msize,
688 GNUNET_TIME_relative_ntoh (obm->timeout),
689 &handle_send_transmit_continuation, stcc);
694 * Try to initiate a connection to the given peer if the blacklist
697 * @param cls closure (unused, NULL)
698 * @param peer identity of peer that was tested
699 * @param result GNUNET_OK if the connection is allowed,
703 try_connect_if_allowed (void *cls, const struct GNUNET_PeerIdentity *peer,
706 if (GNUNET_OK != result)
708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
709 "Blacklist refuses connection attempt to peer `%s'\n",
711 return; /* not allowed */
713 GST_neighbours_try_connect (peer);
718 * Handle request connect message
720 * @param cls closure (always NULL)
721 * @param client identification of the client
722 * @param message the actual message
725 clients_handle_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
726 const struct GNUNET_MessageHeader *message)
728 const struct TransportRequestConnectMessage *trcm =
729 (const struct TransportRequestConnectMessage *) message;
731 GNUNET_STATISTICS_update (GST_stats,
733 ("# REQUEST CONNECT messages received"), 1,
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "Received a request connect message for peer `%s'\n",
737 GNUNET_i2s (&trcm->peer));
738 (void) GST_blacklist_test_allowed (&trcm->peer, NULL, &try_connect_if_allowed,
740 GNUNET_SERVER_receive_done (client, GNUNET_OK);
745 * Take the given address and append it to the set of results sent back to
748 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
749 * @param buf text to transmit
752 transmit_address_to_client (void *cls, const char *buf)
754 struct AddressToStringContext *actx = cls;
757 GNUNET_SERVER_transmit_context_append_data (actx->tc, NULL, 0,
758 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
759 GNUNET_SERVER_transmit_context_run (actx->tc, GNUNET_TIME_UNIT_FOREVER_REL);
760 GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, actx);
764 GNUNET_SERVER_transmit_context_append_data (actx->tc, buf, strlen (buf) + 1,
765 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
770 * Client asked to resolve an address. Process the request.
773 * @param client the client
774 * @param message the resolution request
777 clients_handle_address_to_string (void *cls,
778 struct GNUNET_SERVER_Client *client,
779 const struct GNUNET_MessageHeader *message)
781 const struct AddressLookupMessage *alum;
782 struct GNUNET_TRANSPORT_PluginFunctions *papi;
783 const char *plugin_name;
785 uint32_t address_len;
787 struct GNUNET_SERVER_TransmitContext *tc;
788 struct AddressToStringContext *actx;
789 struct GNUNET_TIME_Relative rtimeout;
792 size = ntohs (message->size);
793 if (size < sizeof (struct AddressLookupMessage))
796 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
799 alum = (const struct AddressLookupMessage *) message;
800 address_len = ntohs (alum->addrlen);
801 if (size <= sizeof (struct AddressLookupMessage) + address_len)
804 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
807 address = (const char *) &alum[1];
808 plugin_name = (const char *) &address[address_len];
809 if (plugin_name[size - sizeof (struct AddressLookupMessage) - address_len - 1]
813 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
816 rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
817 numeric = ntohs (alum->numeric_only);
818 tc = GNUNET_SERVER_transmit_context_create (client);
819 papi = GST_plugins_printer_find (plugin_name);
822 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
823 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
824 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
827 actx = GNUNET_malloc (sizeof (struct AddressToStringContext));
829 GNUNET_CONTAINER_DLL_insert (a2s_head, a2s_tail, actx);
830 GNUNET_SERVER_disable_receive_done_warning (client);
831 papi->address_pretty_printer (papi->cls, plugin_name, address, address_len,
832 numeric, rtimeout, &transmit_address_to_client,
838 * Compose AddressIterateResponseMessage using the given peer and address.
840 * @param peer identity of the peer
841 * @param address the address, NULL on disconnect
842 * @return composed message
844 static struct AddressIterateResponseMessage *
845 compose_address_iterate_response_message (const struct GNUNET_PeerIdentity
847 const struct GNUNET_HELLO_Address
850 struct AddressIterateResponseMessage *msg;
856 GNUNET_assert (NULL != peer);
859 tlen = strlen (address->transport_name) + 1;
860 alen = address->address_length;
864 size = (sizeof (struct AddressIterateResponseMessage) + alen + tlen);
865 msg = GNUNET_malloc (size);
866 msg->header.size = htons (size);
868 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
869 msg->reserved = htonl (0);
871 msg->addrlen = htonl (alen);
872 msg->pluginlen = htonl (tlen);
875 addr = (char *) &msg[1];
876 memcpy (addr, address->address, alen);
877 memcpy (&addr[alen], address->transport_name, tlen);
884 * Output the active address of connected neighbours to the given client.
886 * @param cls the 'struct GNUNET_SERVER_TransmitContext' for transmission to the client
887 * @param peer identity of the neighbour
888 * @param address the address
889 * @param bandwidth_in inbound quota in NBO
890 * @param bandwidth_out outbound quota in NBO
893 output_address (void *cls, const struct GNUNET_PeerIdentity *peer,
894 const struct GNUNET_HELLO_Address *address,
895 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
896 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
898 struct GNUNET_SERVER_TransmitContext *tc = cls;
899 struct AddressIterateResponseMessage *msg;
901 msg = compose_address_iterate_response_message (peer, address);
902 GNUNET_SERVER_transmit_context_append_message (tc, &msg->header);
908 * Client asked to obtain information about all actively used addresses
910 * Process the request.
913 * @param client the client
914 * @param message the peer address information request
917 clients_handle_address_iterate (void *cls, struct GNUNET_SERVER_Client *client,
918 const struct GNUNET_MessageHeader *message)
920 static struct GNUNET_PeerIdentity all_zeros;
921 struct GNUNET_SERVER_TransmitContext *tc;
922 struct AddressIterateMessage *msg;
923 struct GNUNET_HELLO_Address *address;
925 if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE)
928 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
931 if (ntohs (message->size) != sizeof (struct AddressIterateMessage))
934 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
937 msg = (struct AddressIterateMessage *) message;
938 if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
939 (NULL != lookup_monitoring_client (client)) )
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
942 "ServerClient %p tried to start monitoring twice\n",
945 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
948 GNUNET_SERVER_disable_receive_done_warning (client);
949 tc = GNUNET_SERVER_transmit_context_create (client);
950 if (0 == memcmp (&msg->peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
952 /* iterate over all neighbours */
953 GST_neighbours_iterate (&output_address, tc);
957 /* just return one neighbour */
958 address = GST_neighbour_get_current_address (&msg->peer);
960 output_address (tc, &msg->peer, address,
961 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
962 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT);
964 if (GNUNET_YES != ntohl (msg->one_shot))
965 setup_monitoring_client (client, &msg->peer);
967 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
968 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
969 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
974 * Start handling requests from clients.
976 * @param server server used to accept clients from.
979 GST_clients_start (struct GNUNET_SERVER_Handle *server)
981 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
982 {&clients_handle_start, NULL,
983 GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
984 {&clients_handle_hello, NULL,
985 GNUNET_MESSAGE_TYPE_HELLO, 0},
986 {&clients_handle_send, NULL,
987 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
988 {&clients_handle_request_connect, NULL,
989 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT,
990 sizeof (struct TransportRequestConnectMessage)},
991 {&clients_handle_address_to_string, NULL,
992 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING, 0},
993 {&clients_handle_address_iterate, NULL,
994 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE,
995 sizeof (struct AddressIterateMessage)},
996 {&GST_blacklist_handle_init, NULL,
997 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT,
998 sizeof (struct GNUNET_MessageHeader)},
999 {&GST_blacklist_handle_reply, NULL,
1000 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY,
1001 sizeof (struct BlacklistMessage)},
1002 {&GST_manipulation_set_metric, NULL,
1003 GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC, 0},
1006 nc = GNUNET_SERVER_notification_context_create (server, 0);
1007 GNUNET_SERVER_add_handlers (server, handlers);
1008 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
1014 * Stop processing clients.
1019 struct AddressToStringContext *cur;
1021 while (NULL != (cur = a2s_head))
1023 GNUNET_SERVER_transmit_context_destroy (cur->tc, GNUNET_NO);
1024 GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, cur);
1030 GNUNET_SERVER_notification_context_destroy (nc);
1036 * Broadcast the given message to all of our clients.
1038 * @param msg message to broadcast
1039 * @param may_drop GNUNET_YES if the message can be dropped / is payload
1042 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg, int may_drop)
1044 struct TransportClient *tc;
1046 for (tc = clients_head; tc != NULL; tc = tc->next)
1048 if ((GNUNET_YES == may_drop) && (GNUNET_YES != tc->send_payload))
1049 continue; /* skip, this client does not care about payload */
1050 unicast (tc, msg, may_drop);
1056 * Send the given message to a particular client
1058 * @param client target of the message
1059 * @param msg message to transmit
1060 * @param may_drop GNUNET_YES if the message can be dropped
1063 GST_clients_unicast (struct GNUNET_SERVER_Client *client,
1064 const struct GNUNET_MessageHeader *msg, int may_drop)
1066 struct TransportClient *tc;
1068 tc = lookup_client (client);
1070 return; /* client got disconnected in the meantime, drop message */
1071 unicast (tc, msg, may_drop);
1076 * Broadcast the new active address to all clients monitoring the peer.
1078 * @param peer peer this update is about (never NULL)
1079 * @param address address, NULL on disconnect
1082 GST_clients_broadcast_address_notification (const struct GNUNET_PeerIdentity
1084 const struct GNUNET_HELLO_Address
1087 struct AddressIterateResponseMessage *msg;
1088 struct MonitoringClient *mc;
1089 static struct GNUNET_PeerIdentity all_zeros;
1090 msg = compose_address_iterate_response_message (peer, address);
1091 mc = monitoring_clients_head;
1094 if ((0 == memcmp (&mc->peer, &all_zeros,
1095 sizeof (struct GNUNET_PeerIdentity))) ||
1096 (0 == memcmp (&mc->peer, peer,
1097 sizeof (struct GNUNET_PeerIdentity))))
1099 GNUNET_SERVER_notification_context_unicast (nc, mc->client,
1100 &msg->header, GNUNET_NO);
1109 /* end of file gnunet-service-transport_clients.c */