overhead reporting
[oweals/gnunet.git] / src / transport / gnunet-service-transport_clients.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file transport/gnunet-service-transport_clients.c
23  * @brief plugin management API
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
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"
35
36
37 /**
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.
42  *
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
47  * fail.
48  */
49 #define MAX_PENDING (128 * 1024)
50
51
52 /**
53  * Linked list of messages to be transmitted to the client.  Each
54  * entry is followed by the actual message.
55  */
56 struct ClientMessageQueueEntry
57 {
58   /**
59    * This is a doubly-linked list.
60    */
61   struct ClientMessageQueueEntry *next;
62
63   /**
64    * This is a doubly-linked list.
65    */
66   struct ClientMessageQueueEntry *prev;
67 };
68
69
70 /**
71  * Client connected to the transport service.
72  */
73 struct TransportClient
74 {
75
76   /**
77    * This is a doubly-linked list.
78    */
79   struct TransportClient *next;
80
81   /**
82    * This is a doubly-linked list.
83    */
84   struct TransportClient *prev;
85
86   /**
87    * Handle to the client.
88    */
89   struct GNUNET_SERVER_Client *client;
90
91   /**
92    * Linked list of messages yet to be transmitted to
93    * the client.
94    */
95   struct ClientMessageQueueEntry *message_queue_head;
96
97   /**
98    * Tail of linked list of messages yet to be transmitted to the
99    * client.
100    */
101   struct ClientMessageQueueEntry *message_queue_tail;
102
103   /**
104    * Current transmit request handle.
105    */
106   struct GNUNET_SERVER_TransmitHandle *th;
107
108   /**
109    * Length of the list of messages pending for this client.
110    */
111   unsigned int message_count;
112
113   /**
114    * Is this client interested in payload messages?
115    */
116   int send_payload;
117 };
118
119
120 /**
121  * Client monitoring changes of active addresses of our neighbours.
122  */
123 struct MonitoringClient
124 {
125   /**
126    * This is a doubly-linked list.
127    */
128   struct MonitoringClient *next;
129
130   /**
131    * This is a doubly-linked list.
132    */
133   struct MonitoringClient *prev;
134
135   /**
136    * Handle to the client.
137    */
138   struct GNUNET_SERVER_Client *client;
139
140   /**
141    * Peer identity to monitor the addresses of.
142    * Zero to monitor all neighrours.
143    */
144   struct GNUNET_PeerIdentity peer;
145
146 };
147
148
149 /**
150  * Head of linked list of all clients to this service.
151  */
152 static struct TransportClient *clients_head;
153
154 /**
155  * Tail of linked list of all clients to this service.
156  */
157 static struct TransportClient *clients_tail;
158
159 /**
160  * Head of linked list of monitoring clients.
161  */
162 static struct MonitoringClient *monitoring_clients_head;
163
164 /**
165  * Tail of linked list of monitoring clients.
166  */
167 static struct MonitoringClient *monitoring_clients_tail;
168
169 /**
170  * Notification context, to send updates on changes to active addresses
171  * of our neighbours.
172  */
173 struct GNUNET_SERVER_NotificationContext *nc = NULL;
174
175
176 /**
177  * Find the internal handle associated with the given client handle
178  *
179  * @param client server's client handle to look up
180  * @return internal client handle
181  */
182 static struct TransportClient *
183 lookup_client (struct GNUNET_SERVER_Client *client)
184 {
185   struct TransportClient *tc;
186
187   tc = clients_head;
188   while (tc != NULL)
189   {
190     if (tc->client == client)
191       return tc;
192     tc = tc->next;
193   }
194   return NULL;
195 }
196
197
198 /**
199  * Create the internal handle for the given server client handle
200  *
201  * @param client server's client handle to create our internal handle for
202  * @return fresh internal client handle
203  */
204 static struct TransportClient *
205 setup_client (struct GNUNET_SERVER_Client *client)
206 {
207   struct TransportClient *tc;
208
209   GNUNET_assert (lookup_client (client) == NULL);
210   tc = GNUNET_malloc (sizeof (struct TransportClient));
211   tc->client = client;
212   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", tc);
213   return tc;
214 }
215
216
217 /**
218  * Find the handle to the monitoring client associated with the given
219  * client handle
220  *
221  * @param client server's client handle to look up
222  * @return handle to the monitoring client
223  */
224 static struct MonitoringClient *
225 lookup_monitoring_client (struct GNUNET_SERVER_Client *client)
226 {
227   struct MonitoringClient *mc;
228
229   mc = monitoring_clients_head;
230   while (mc != NULL)
231   {
232     if (mc->client == client)
233       return mc;
234     mc = mc->next;
235   }
236   return NULL;
237 }
238
239
240 /**
241  * Setup a new monitoring client using the given server client handle and
242  * the peer identity.
243  *
244  * @param client server's client handle to create our internal handle for
245  * @param peer identity of the peer to monitor the addresses of,
246  *             zero to monitor all neighrours.
247  * @return handle to the new monitoring client
248  */
249 static struct MonitoringClient *
250 setup_monitoring_client (struct GNUNET_SERVER_Client *client,
251                          struct GNUNET_PeerIdentity *peer)
252 {
253   struct MonitoringClient *mc;
254
255   GNUNET_assert (lookup_monitoring_client (client) == NULL);
256   mc = GNUNET_malloc (sizeof (struct MonitoringClient));
257   mc->client = client;
258   mc->peer = *peer;
259   GNUNET_CONTAINER_DLL_insert (monitoring_clients_head,
260                                monitoring_clients_tail,
261                                mc);
262   GNUNET_SERVER_notification_context_add (nc, client);
263
264   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265               "Client %p started monitoring of the peer `%s'\n",
266               mc, GNUNET_i2s (peer));
267   return mc;
268 }
269
270
271 /**
272  * Function called to notify a client about the socket being ready to
273  * queue more data.  "buf" will be NULL and "size" zero if the socket
274  * was closed for writing in the meantime.
275  *
276  * @param cls closure
277  * @param size number of bytes available in buf
278  * @param buf where the callee should write the message
279  * @return number of bytes written to buf
280  */
281 static size_t
282 transmit_to_client_callback (void *cls, size_t size, void *buf)
283 {
284   struct TransportClient *tc = cls;
285   struct ClientMessageQueueEntry *q;
286   const struct GNUNET_MessageHeader *msg;
287   char *cbuf;
288   uint16_t msize;
289   size_t tsize;
290
291   tc->th = NULL;
292   if (buf == NULL)
293   {
294     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295                 "Transmission to client failed, closing connection.\n");
296     return 0;
297   }
298   cbuf = buf;
299   tsize = 0;
300   while (NULL != (q = tc->message_queue_head))
301   {
302     msg = (const struct GNUNET_MessageHeader *) &q[1];
303     msize = ntohs (msg->size);
304     if (msize + tsize > size)
305       break;
306     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307                 "Transmitting message of type %u to client %p.\n",
308                 ntohs (msg->type), tc);
309     GNUNET_CONTAINER_DLL_remove (tc->message_queue_head, tc->message_queue_tail,
310                                  q);
311     tc->message_count--;
312     memcpy (&cbuf[tsize], msg, msize);
313     GNUNET_free (q);
314     tsize += msize;
315   }
316   if (NULL != q)
317   {
318     GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
319     tc->th =
320         GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
321                                              GNUNET_TIME_UNIT_FOREVER_REL,
322                                              &transmit_to_client_callback, tc);
323     GNUNET_assert (tc->th != NULL);
324   }
325   return tsize;
326 }
327
328
329 /**
330  * Queue the given message for transmission to the given client
331  *
332  * @param tc target of the message
333  * @param msg message to transmit
334  * @param may_drop GNUNET_YES if the message can be dropped
335  */
336 static void
337 unicast (struct TransportClient *tc, const struct GNUNET_MessageHeader *msg,
338          int may_drop)
339 {
340   struct ClientMessageQueueEntry *q;
341   uint16_t msize;
342
343   if (msg == NULL)
344   {
345     GNUNET_break (0);
346     return;
347   }
348
349   if ((tc->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
350   {
351     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
352                 _
353                 ("Dropping message of type %u and size %u, have %u/%u messages pending\n"),
354                 ntohs (msg->type), ntohs (msg->size), tc->message_count,
355                 MAX_PENDING);
356     GNUNET_STATISTICS_update (GST_stats,
357                               gettext_noop
358                               ("# messages dropped due to slow client"), 1,
359                               GNUNET_NO);
360     return;
361   }
362   msize = ntohs (msg->size);
363   GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
364   q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
365   memcpy (&q[1], msg, msize);
366   GNUNET_CONTAINER_DLL_insert_tail (tc->message_queue_head,
367                                     tc->message_queue_tail, q);
368   tc->message_count++;
369   if (tc->th != NULL)
370     return;
371   tc->th =
372       GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
373                                            GNUNET_TIME_UNIT_FOREVER_REL,
374                                            &transmit_to_client_callback, tc);
375   GNUNET_assert (tc->th != NULL);
376 }
377
378
379 /**
380  * Called whenever a client is disconnected.  Frees our
381  * resources associated with that client.
382  *
383  * @param cls closure
384  * @param client identification of the client
385  */
386 static void
387 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
388 {
389   struct TransportClient *tc;
390   struct MonitoringClient *mc;
391   struct ClientMessageQueueEntry *mqe;
392
393   if (client == NULL)
394     return;
395   mc = lookup_monitoring_client (client);
396   if (mc != NULL)
397   {
398     GNUNET_CONTAINER_DLL_remove (monitoring_clients_head,
399                                  monitoring_clients_tail,
400                                  mc);
401     GNUNET_free (mc);
402   }
403   tc = lookup_client (client);
404   if (tc == NULL)
405     return;
406   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
407               "Client %p disconnected, cleaning up.\n", tc);
408   while (NULL != (mqe = tc->message_queue_head))
409   {
410     GNUNET_CONTAINER_DLL_remove (tc->message_queue_head, tc->message_queue_tail,
411                                  mqe);
412     tc->message_count--;
413     GNUNET_free (mqe);
414   }
415   GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, tc);
416   if (tc->th != NULL)
417   {
418     GNUNET_SERVER_notify_transmit_ready_cancel (tc->th);
419     tc->th = NULL;
420   }
421   GNUNET_break (0 == tc->message_count);
422   GNUNET_free (tc);
423 }
424
425
426 /**
427  * Function called for each of our connected neighbours.  Notify the
428  * client about the existing neighbour.
429  *
430  * @param cls the 'struct TransportClient' to notify
431  * @param peer identity of the neighbour
432  * @param ats performance data
433  * @param ats_count number of entries in ats (excluding 0-termination)
434  * @param address the address
435  * @param bandwidth_in inbound bandwidth in NBO
436  * @param bandwidth_out outbound bandwidth in NBO
437  */
438 static void
439 notify_client_about_neighbour (void *cls,
440                                const struct GNUNET_PeerIdentity *peer,
441                                const struct GNUNET_ATS_Information *ats,
442                                uint32_t ats_count,
443                                const struct GNUNET_HELLO_Address *address,
444                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
445                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
446 {
447   struct TransportClient *tc = cls;
448   struct ConnectInfoMessage *cim;
449   struct GNUNET_ATS_Information *ap;
450   size_t size =
451       sizeof (struct ConnectInfoMessage) +
452       ats_count * sizeof (struct GNUNET_ATS_Information);
453   char buf[size] GNUNET_ALIGN;
454
455   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
456   cim = (struct ConnectInfoMessage *) buf;
457   cim->header.size = htons (size);
458   cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
459   cim->ats_count = htonl (ats_count);
460   cim->id = *peer;
461   cim->quota_in = bandwidth_in;
462   cim->quota_out = bandwidth_out;
463   ap = (struct GNUNET_ATS_Information *) &cim[1];
464   memcpy (ap, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
465   unicast (tc, &cim->header, GNUNET_NO);
466 }
467
468
469 /**
470  * Initialize a normal client.  We got a start message from this
471  * client, add him to the list of clients for broadcasting of inbound
472  * messages.
473  *
474  * @param cls unused
475  * @param client the client
476  * @param message the start message that was sent
477  */
478 static void
479 clients_handle_start (void *cls, struct GNUNET_SERVER_Client *client,
480                       const struct GNUNET_MessageHeader *message)
481 {
482   const struct StartMessage *start;
483   struct TransportClient *tc;
484   uint32_t options;
485
486   tc = lookup_client (client);
487
488   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
489               "Client %p sent START\n", tc);
490   if (tc != NULL)
491   {
492     /* got 'start' twice from the same client, not allowed */
493     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
494                 "TransportClient %p ServerClient %p sent multiple START messages\n",
495                 tc, tc->client);
496     GNUNET_break (0);
497     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
498     return;
499   }
500   start = (const struct StartMessage *) message;
501   options = ntohl (start->options);
502   if ((0 != (1 & options)) &&
503       (0 !=
504        memcmp (&start->self, &GST_my_identity,
505                sizeof (struct GNUNET_PeerIdentity))))
506   {
507     /* client thinks this is a different peer, reject */
508     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
509                 _
510                 ("Rejecting control connection from peer `%s', which is not me!\n"),
511                 GNUNET_i2s (&start->self));
512     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
513     return;
514   }
515   tc = setup_client (client);
516   tc->send_payload = (0 != (2 & options));
517   unicast (tc, GST_hello_get (), GNUNET_NO);
518   GST_neighbours_iterate (&notify_client_about_neighbour, tc);
519   GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, tc);
520   GNUNET_SERVER_receive_done (client, GNUNET_OK);
521 }
522
523
524 /**
525  * Client sent us a HELLO.  Process the request.
526  *
527  * @param cls unused
528  * @param client the client
529  * @param message the HELLO message
530  */
531 static void
532 clients_handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
533                       const struct GNUNET_MessageHeader *message)
534 {
535   GST_validation_handle_hello (message);
536   GNUNET_SERVER_receive_done (client, GNUNET_OK);
537 }
538
539
540 /**
541  * Closure for 'handle_send_transmit_continuation'
542  */
543 struct SendTransmitContinuationContext
544 {
545   /**
546    * Client that made the request.
547    */
548   struct GNUNET_SERVER_Client *client;
549
550   /**
551    * Peer that was the target.
552    */
553   struct GNUNET_PeerIdentity target;
554 };
555
556
557 /**
558  * Function called after the transmission is done.  Notify the client that it is
559  * OK to send the next message.
560  *
561  * @param cls closure
562  * @param success GNUNET_OK on success, GNUNET_NO on failure, GNUNET_SYSERR if we're not connected
563  */
564 static void
565 handle_send_transmit_continuation (void *cls, int success,
566                                    size_t bytes_payload, size_t bytes_on_wire)
567 {
568   struct SendTransmitContinuationContext *stcc = cls;
569   struct SendOkMessage send_ok_msg;
570
571   //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Payload: %u, On wire %u \n", bytes_payload, bytes_on_wire);
572
573   send_ok_msg.header.size = htons (sizeof (send_ok_msg));
574   send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
575   send_ok_msg.success = htonl (success);
576   send_ok_msg.latency =
577       GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
578   send_ok_msg.peer = stcc->target;
579   GST_clients_unicast (stcc->client, &send_ok_msg.header, GNUNET_NO);
580   GNUNET_SERVER_client_drop (stcc->client);
581   GNUNET_free (stcc);
582 }
583
584
585 /**
586  * Client asked for transmission to a peer.  Process the request.
587  *
588  * @param cls unused
589  * @param client the client
590  * @param message the send message that was sent
591  */
592 static void
593 clients_handle_send (void *cls, struct GNUNET_SERVER_Client *client,
594                      const struct GNUNET_MessageHeader *message)
595 {
596   const struct OutboundMessage *obm;
597   const struct GNUNET_MessageHeader *obmm;
598   struct SendTransmitContinuationContext *stcc;
599   uint16_t size;
600   uint16_t msize;
601   struct TransportClient *tc;
602
603   tc = lookup_client (client);
604   if (NULL == tc)
605   {
606     /* client asked for transmission before 'START' */
607     GNUNET_break (0);
608     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
609     return;
610   }
611
612   size = ntohs (message->size);
613   if (size <
614       sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
615   {
616     GNUNET_break (0);
617     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
618     return;
619   }
620   obm = (const struct OutboundMessage *) message;
621   obmm = (const struct GNUNET_MessageHeader *) &obm[1];
622   msize = size - sizeof (struct OutboundMessage);
623   if (msize < sizeof (struct GNUNET_MessageHeader))
624   {
625     GNUNET_break (0);
626     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
627     return;
628   }
629
630   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
631               "Received `%s' request from client with target `%4s' and first message of type %u and total size %u\n",
632               "SEND", GNUNET_i2s (&obm->peer), ntohs (obmm->type), msize);
633   if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
634   {
635     /* not connected, not allowed to send; can happen due to asynchronous operations */
636     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
637                 "Could not send message to peer `%s': not connected\n",
638                 GNUNET_i2s (&obm->peer));
639     GNUNET_STATISTICS_update (GST_stats,
640                               gettext_noop
641                               ("# bytes payload dropped (other peer was not connected)"),
642                               msize, GNUNET_NO);
643     GNUNET_SERVER_receive_done (client, GNUNET_OK);
644     return;
645   }
646   GNUNET_SERVER_receive_done (client, GNUNET_OK);
647   stcc = GNUNET_malloc (sizeof (struct SendTransmitContinuationContext));
648   stcc->target = obm->peer;
649   stcc->client = client;
650   GNUNET_SERVER_client_keep (client);
651   GST_neighbours_send (&obm->peer, obmm, msize,
652                        GNUNET_TIME_relative_ntoh (obm->timeout),
653                        &handle_send_transmit_continuation, stcc);
654 }
655
656
657 /**
658  * Try to initiate a connection to the given peer if the blacklist
659  * allowed it.
660  *
661  * @param cls closure (unused, NULL)
662  * @param peer identity of peer that was tested
663  * @param result GNUNET_OK if the connection is allowed,
664  *               GNUNET_NO if not
665  */
666 static void
667 try_connect_if_allowed (void *cls, const struct GNUNET_PeerIdentity *peer,
668                         int result)
669 {
670   if (GNUNET_OK != result)
671     return;                     /* not allowed */
672   GST_neighbours_try_connect (peer);
673 }
674
675
676 /**
677  * Handle request connect message
678  *
679  * @param cls closure (always NULL)
680  * @param client identification of the client
681  * @param message the actual message
682  */
683 static void
684 clients_handle_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
685                                 const struct GNUNET_MessageHeader *message)
686 {
687   const struct TransportRequestConnectMessage *trcm =
688       (const struct TransportRequestConnectMessage *) message;
689
690   GNUNET_STATISTICS_update (GST_stats,
691                             gettext_noop
692                             ("# REQUEST CONNECT messages received"), 1,
693                             GNUNET_NO);
694   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695               "Received a request connect message for peer `%s'\n",
696               GNUNET_i2s (&trcm->peer));
697   (void) GST_blacklist_test_allowed (&trcm->peer, NULL, &try_connect_if_allowed,
698                                      NULL);
699   GNUNET_SERVER_receive_done (client, GNUNET_OK);
700 }
701
702
703 /**
704  * Take the given address and append it to the set of results sent back to
705  * the client.
706  *
707  * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
708  * @param buf text to transmit
709  */
710 static void
711 transmit_address_to_client (void *cls, const char *buf)
712 {
713   struct GNUNET_SERVER_TransmitContext *tc = cls;
714
715   if (NULL == buf)
716   {
717     GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
718                                                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
719     GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
720     return;
721   }
722   GNUNET_SERVER_transmit_context_append_data (tc, buf, strlen (buf) + 1,
723                                               GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
724 }
725
726
727 /**
728  * Client asked to resolve an address.  Process the request.
729  *
730  * @param cls unused
731  * @param client the client
732  * @param message the resolution request
733  */
734 static void
735 clients_handle_address_to_string (void *cls,
736                                   struct GNUNET_SERVER_Client *client,
737                                   const struct GNUNET_MessageHeader *message)
738 {
739   const struct AddressLookupMessage *alum;
740   struct GNUNET_TRANSPORT_PluginFunctions *papi;
741   const char *plugin_name;
742   const char *address;
743   uint32_t address_len;
744   uint16_t size;
745   struct GNUNET_SERVER_TransmitContext *tc;
746   struct GNUNET_TIME_Relative rtimeout;
747   int32_t numeric;
748
749   size = ntohs (message->size);
750   if (size < sizeof (struct AddressLookupMessage))
751   {
752     GNUNET_break (0);
753     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
754     return;
755   }
756   alum = (const struct AddressLookupMessage *) message;
757   address_len = ntohs (alum->addrlen);
758   if (size <= sizeof (struct AddressLookupMessage) + address_len)
759   {
760     GNUNET_break (0);
761     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
762     return;
763   }
764   address = (const char *) &alum[1];
765   plugin_name = (const char *) &address[address_len];
766   if (plugin_name[size - sizeof (struct AddressLookupMessage) - address_len - 1]
767       != '\0')
768   {
769     GNUNET_break (0);
770     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
771     return;
772   }
773   rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
774   numeric = ntohs (alum->numeric_only);
775   tc = GNUNET_SERVER_transmit_context_create (client);
776   papi = GST_plugins_printer_find (plugin_name);
777   if (NULL == papi)
778   {
779     GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
780                                                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
781     GNUNET_SERVER_transmit_context_run (tc, rtimeout);
782     return;
783   }
784   GNUNET_SERVER_disable_receive_done_warning (client);
785   papi->address_pretty_printer (papi->cls, plugin_name, address, address_len,
786                                 numeric, rtimeout, &transmit_address_to_client,
787                                 tc);
788 }
789
790
791 /**
792  * Compose AddressIterateResponseMessage using the given peer and address.
793  *
794  * @param peer identity of the peer
795  * @param address the address, NULL on disconnect
796  * @return composed message
797  */
798 static struct AddressIterateResponseMessage *
799 compose_address_iterate_response_message (const struct GNUNET_PeerIdentity
800                                           *peer,
801                                           const struct GNUNET_HELLO_Address
802                                           *address)
803 {
804   struct AddressIterateResponseMessage *msg;
805   size_t size;
806   size_t tlen;
807   size_t alen;
808   char *addr;
809
810   GNUNET_assert (NULL != peer);
811   if (NULL != address)
812   {
813     tlen = strlen (address->transport_name) + 1;
814     alen = address->address_length;
815   }
816   else
817     tlen = alen = 0;
818   size = (sizeof (struct AddressIterateResponseMessage) + alen + tlen);
819   msg = GNUNET_malloc (size);
820   msg->header.size = htons (size);
821   msg->header.type =
822       htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
823   msg->reserved = htonl (0);
824   msg->peer = *peer;
825   msg->addrlen = htonl (alen);
826   msg->pluginlen = htonl (tlen);
827   if (NULL != address)
828   {
829     addr = (char *) &msg[1];
830     memcpy (addr, address->address, alen);
831     memcpy (&addr[alen], address->transport_name, tlen);
832   }
833   return msg;
834 }
835
836
837 /**
838  * Output the active address of connected neighbours to the given client.
839  *
840  * @param cls the 'struct GNUNET_SERVER_TransmitContext' for transmission to the client
841  * @param peer identity of the neighbour
842  * @param ats performance data
843  * @param ats_count number of entries in ats (excluding 0-termination)
844  * @param address the address
845  * @param bandwidth_in inbound quota in NBO
846  * @param bandwidth_out outbound quota in NBO
847  */
848 static void
849 output_address (void *cls, const struct GNUNET_PeerIdentity *peer,
850                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count,
851                 const struct GNUNET_HELLO_Address *address,
852                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
853                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
854 {
855   struct GNUNET_SERVER_TransmitContext *tc = cls;
856   struct AddressIterateResponseMessage *msg;
857
858   msg = compose_address_iterate_response_message (peer, address);
859   GNUNET_SERVER_transmit_context_append_message (tc, &msg->header);
860   GNUNET_free (msg);
861 }
862
863
864 /**
865  * Client asked to obtain information about all actively used addresses
866  * of connected peers
867  * Process the request.
868  *
869  * @param cls unused
870  * @param client the client
871  * @param message the peer address information request
872  */
873 static void
874 clients_handle_address_iterate (void *cls, struct GNUNET_SERVER_Client *client,
875                                 const struct GNUNET_MessageHeader *message)
876 {
877   static struct GNUNET_PeerIdentity all_zeros;
878   struct GNUNET_SERVER_TransmitContext *tc;
879   struct AddressIterateMessage *msg;
880   struct GNUNET_HELLO_Address *address;
881
882   if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE)
883   {
884     GNUNET_break (0);
885     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
886     return;
887   }
888   if (ntohs (message->size) != sizeof (struct AddressIterateMessage))
889   {
890     GNUNET_break (0);
891     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
892     return;
893   }
894   msg = (struct AddressIterateMessage *) message;
895   if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
896        (NULL != lookup_monitoring_client (client)) )
897   {
898     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
899                 "ServerClient %p tried to start monitoring twice\n",
900                 client);
901     GNUNET_break (0);
902     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
903     return;
904   }
905   GNUNET_SERVER_disable_receive_done_warning (client);
906   tc = GNUNET_SERVER_transmit_context_create (client);
907   if (0 == memcmp (&msg->peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
908   {
909     /* iterate over all neighbours */
910     GST_neighbours_iterate (&output_address, tc);
911   }
912   else
913   {
914     /* just return one neighbour */
915     address = GST_neighbour_get_current_address (&msg->peer);
916     if (address != NULL)
917       output_address (tc, &msg->peer, NULL, 0, address,
918                       GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
919                       GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT);
920   }
921   if (GNUNET_YES != ntohl (msg->one_shot))
922     setup_monitoring_client (client, &msg->peer);
923   else
924     GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
925                                                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);  
926   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
927 }
928
929
930 /**
931  * Start handling requests from clients.
932  *
933  * @param server server used to accept clients from.
934  */
935 void
936 GST_clients_start (struct GNUNET_SERVER_Handle *server)
937 {
938   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
939     {&clients_handle_start, NULL,
940      GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
941     {&clients_handle_hello, NULL,
942      GNUNET_MESSAGE_TYPE_HELLO, 0},
943     {&clients_handle_send, NULL,
944      GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
945     {&clients_handle_request_connect, NULL,
946      GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT,
947      sizeof (struct TransportRequestConnectMessage)},
948     {&clients_handle_address_to_string, NULL,
949      GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING, 0},
950     {&clients_handle_address_iterate, NULL,
951      GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE,
952      sizeof (struct AddressIterateMessage)},
953     {&GST_blacklist_handle_init, NULL,
954      GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT,
955      sizeof (struct GNUNET_MessageHeader)},
956     {&GST_blacklist_handle_reply, NULL,
957      GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY,
958      sizeof (struct BlacklistMessage)},
959     {NULL, NULL, 0, 0}
960   };
961   nc = GNUNET_SERVER_notification_context_create (server, 0);
962   GNUNET_SERVER_add_handlers (server, handlers);
963   GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
964                                    NULL);
965 }
966
967
968 /**
969  * Stop processing clients.
970  */
971 void
972 GST_clients_stop ()
973 {
974   if (NULL != nc)
975   {
976     GNUNET_SERVER_notification_context_destroy (nc);
977     nc = NULL;
978   }
979 }
980
981 /**
982  * Broadcast the given message to all of our clients.
983  *
984  * @param msg message to broadcast
985  * @param may_drop GNUNET_YES if the message can be dropped / is payload
986  */
987 void
988 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg, int may_drop)
989 {
990   struct TransportClient *tc;
991
992   for (tc = clients_head; tc != NULL; tc = tc->next)
993   {
994     if ((GNUNET_YES == may_drop) && (GNUNET_YES != tc->send_payload))
995       continue;                 /* skip, this client does not care about payload */
996     unicast (tc, msg, may_drop);
997   }
998 }
999
1000
1001 /**
1002  * Send the given message to a particular client
1003  *
1004  * @param client target of the message
1005  * @param msg message to transmit
1006  * @param may_drop GNUNET_YES if the message can be dropped
1007  */
1008 void
1009 GST_clients_unicast (struct GNUNET_SERVER_Client *client,
1010                      const struct GNUNET_MessageHeader *msg, int may_drop)
1011 {
1012   struct TransportClient *tc;
1013
1014   tc = lookup_client (client);
1015   if (NULL == tc)
1016     return;                     /* client got disconnected in the meantime, drop message */
1017   unicast (tc, msg, may_drop);
1018 }
1019
1020
1021 /**
1022  * Broadcast the new active address to all clients monitoring the peer.
1023  *
1024  * @param peer peer this update is about (never NULL)
1025  * @param address address, NULL on disconnect
1026  */
1027 void
1028 GST_clients_broadcast_address_notification (const struct GNUNET_PeerIdentity
1029                                             *peer,
1030                                             const struct GNUNET_HELLO_Address
1031                                             *address)
1032 {
1033   struct AddressIterateResponseMessage *msg;
1034   struct MonitoringClient *mc;
1035   static struct GNUNET_PeerIdentity all_zeros;
1036
1037   msg = compose_address_iterate_response_message (peer, address);
1038   mc = monitoring_clients_head;
1039   while (mc != NULL)
1040   {
1041     if ((0 == memcmp (&mc->peer, &all_zeros,
1042                       sizeof (struct GNUNET_PeerIdentity))) ||
1043         (0 == memcmp (&mc->peer, peer,
1044                       sizeof (struct GNUNET_PeerIdentity))))
1045     {
1046       GNUNET_SERVER_notification_context_unicast (nc, mc->client,
1047                                                   &msg->header, GNUNET_NO);
1048     }
1049
1050     mc = mc->next;
1051   }
1052   GNUNET_free (msg);
1053 }
1054
1055
1056 /* end of file gnunet-service-transport_clients.c */