Check that you are not present in trail twice
[oweals/gnunet.git] / src / transport / gnunet-service-transport_clients.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010-2014 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_manipulation.h"
34 #include "gnunet-service-transport.h"
35 #include "transport.h"
36
37
38 /**
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.
43  *
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
48  * fail.
49  */
50 #define MAX_PENDING (128 * 1024)
51
52
53 /**
54  * Linked list of messages to be transmitted to the client.  Each
55  * entry is followed by the actual message.
56  */
57 struct ClientMessageQueueEntry
58 {
59   /**
60    * This is a doubly-linked list.
61    */
62   struct ClientMessageQueueEntry *next;
63
64   /**
65    * This is a doubly-linked list.
66    */
67   struct ClientMessageQueueEntry *prev;
68 };
69
70
71 /**
72  * Client connected to the transport service.
73  */
74 struct TransportClient
75 {
76
77   /**
78    * This is a doubly-linked list.
79    */
80   struct TransportClient *next;
81
82   /**
83    * This is a doubly-linked list.
84    */
85   struct TransportClient *prev;
86
87   /**
88    * Handle to the client.
89    */
90   struct GNUNET_SERVER_Client *client;
91
92   /**
93    * Linked list of messages yet to be transmitted to
94    * the client.
95    */
96   struct ClientMessageQueueEntry *message_queue_head;
97
98   /**
99    * Tail of linked list of messages yet to be transmitted to the
100    * client.
101    */
102   struct ClientMessageQueueEntry *message_queue_tail;
103
104   /**
105    * Current transmit request handle.
106    */
107   struct GNUNET_SERVER_TransmitHandle *th;
108
109   /**
110    * Length of the list of messages pending for this client.
111    */
112   unsigned int message_count;
113
114   /**
115    * Is this client interested in payload messages?
116    */
117   int send_payload;
118 };
119
120 /**
121  * Context for address to string operations
122  */
123 struct AddressToStringContext
124 {
125   /**
126    * This is a doubly-linked list.
127    */
128   struct AddressToStringContext *next;
129
130   /**
131    * This is a doubly-linked list.
132    */
133   struct AddressToStringContext *prev;
134
135   /**
136    * Transmission context
137    */
138   struct GNUNET_SERVER_TransmitContext* tc;
139 };
140
141 /**
142  * Client monitoring changes of active addresses of our neighbours.
143  */
144 struct MonitoringClient
145 {
146   /**
147    * This is a doubly-linked list.
148    */
149   struct MonitoringClient *next;
150
151   /**
152    * This is a doubly-linked list.
153    */
154   struct MonitoringClient *prev;
155
156   /**
157    * Handle to the client.
158    */
159   struct GNUNET_SERVER_Client *client;
160
161   /**
162    * Peer identity to monitor the addresses of.
163    * Zero to monitor all neighrours.
164    */
165   struct GNUNET_PeerIdentity peer;
166
167 };
168
169
170 /**
171  * Head of linked list of all clients to this service.
172  */
173 static struct TransportClient *clients_head;
174
175 /**
176  * Tail of linked list of all clients to this service.
177  */
178 static struct TransportClient *clients_tail;
179
180 /**
181  * Head of linked list of all pending address iterations
182  */
183 struct AddressToStringContext *a2s_head;
184
185 /**
186  * Tail of linked list of all pending address iterations
187  */
188 struct AddressToStringContext *a2s_tail;
189
190 /**
191  * Head of linked list of monitoring clients.
192  */
193 static struct MonitoringClient *peer_monitoring_clients_head;
194
195 /**
196  * Tail of linked list of monitoring clients.
197  */
198 static struct MonitoringClient *peer_monitoring_clients_tail;
199
200 /**
201  * Head of linked list of validation monitoring clients.
202  */
203 static struct MonitoringClient *val_monitoring_clients_head;
204
205 /**
206  * Tail of linked list of validation monitoring clients.
207  */
208 static struct MonitoringClient *val_monitoring_clients_tail;
209
210 /**
211  * Notification context, to send updates on changes to active addresses
212  * of our neighbours.
213  */
214 static struct GNUNET_SERVER_NotificationContext *peer_nc;
215
216 /**
217  * Notification context, to send updates on changes to active addresses
218  * of our neighbours.
219  */
220 static struct GNUNET_SERVER_NotificationContext *val_nc;
221
222 /**
223  * Find the internal handle associated with the given client handle
224  *
225  * @param client server's client handle to look up
226  * @return internal client handle
227  */
228 static struct TransportClient *
229 lookup_client (struct GNUNET_SERVER_Client *client)
230 {
231   struct TransportClient *tc;
232
233   for (tc = clients_head; NULL != tc; tc = tc->next)
234     if (tc->client == client)
235       return tc;
236   return NULL;
237 }
238
239
240 /**
241  * Create the internal handle for the given server client handle
242  *
243  * @param client server's client handle to create our internal handle for
244  * @return fresh internal client handle
245  */
246 static struct TransportClient *
247 setup_client (struct GNUNET_SERVER_Client *client)
248 {
249   struct TransportClient *tc;
250
251   GNUNET_assert (NULL == lookup_client (client));
252   tc = GNUNET_new (struct TransportClient);
253   tc->client = client;
254   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255               "Client %p connected\n",
256               tc);
257   return tc;
258 }
259
260
261 /**
262  * Find the handle to the monitoring client associated with the given
263  * client handle
264  *
265  * @param head the head of the client queue to look in
266  * @param client server's client handle to look up
267  * @return handle to the monitoring client
268  */
269 static struct MonitoringClient *
270 lookup_monitoring_client (struct MonitoringClient *head,
271                           struct GNUNET_SERVER_Client *client)
272 {
273   struct MonitoringClient *mc;
274
275   for (mc = head; NULL != mc; mc = mc->next)
276     if (mc->client == client)
277       return mc;
278   return NULL;
279 }
280
281
282 /**
283  * Setup a new monitoring client using the given server client handle and
284  * the peer identity.
285  *
286  * @param client server's client handle to create our internal handle for
287  * @param peer identity of the peer to monitor the addresses of,
288  *             zero to monitor all neighrours.
289  * @return handle to the new monitoring client
290  */
291 static struct MonitoringClient *
292 setup_peer_monitoring_client (struct GNUNET_SERVER_Client *client,
293                               struct GNUNET_PeerIdentity *peer)
294 {
295   struct MonitoringClient *mc;
296   static struct GNUNET_PeerIdentity all_zeros;
297
298   GNUNET_assert (lookup_monitoring_client (peer_monitoring_clients_head, client) == NULL);
299   mc = GNUNET_new (struct MonitoringClient);
300   mc->client = client;
301   mc->peer = *peer;
302   GNUNET_CONTAINER_DLL_insert (peer_monitoring_clients_head, peer_monitoring_clients_tail, mc);
303   GNUNET_SERVER_notification_context_add (peer_nc, client);
304
305   if (0 != memcmp (peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
306     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
307                 "Client %p started monitoring of the peer `%s'\n",
308                 mc, GNUNET_i2s (peer));
309   else
310     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
311               "Client %p started monitoring all peers\n", mc);
312   return mc;
313 }
314
315
316 /**
317  * Setup a new monitoring client using the given server client handle and
318  * the peer identity.
319  *
320  * @param client server's client handle to create our internal handle for
321  * @param peer identity of the peer to monitor the addresses of,
322  *             zero to monitor all neighrours.
323  * @return handle to the new monitoring client
324  */
325 static struct MonitoringClient *
326 setup_val_monitoring_client (struct GNUNET_SERVER_Client *client,
327                              struct GNUNET_PeerIdentity *peer)
328 {
329   struct MonitoringClient *mc;
330   static struct GNUNET_PeerIdentity all_zeros;
331
332   GNUNET_assert (lookup_monitoring_client (val_monitoring_clients_head, client) == NULL);
333   mc = GNUNET_new (struct MonitoringClient);
334   mc->client = client;
335   mc->peer = *peer;
336   GNUNET_CONTAINER_DLL_insert (val_monitoring_clients_head, val_monitoring_clients_tail, mc);
337   GNUNET_SERVER_notification_context_add (val_nc, client);
338
339   if (0 != memcmp (peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
340     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
341                 "Client %p started monitoring of the peer `%s'\n",
342                 mc, GNUNET_i2s (peer));
343   else
344     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
345               "Client %p started monitoring all peers\n", mc);
346   return mc;
347 }
348
349
350 /**
351  * Function called to notify a client about the socket being ready to
352  * queue more data.  "buf" will be NULL and "size" zero if the socket
353  * was closed for writing in the meantime.
354  *
355  * @param cls closure
356  * @param size number of bytes available in @a buf
357  * @param buf where the callee should write the message
358  * @return number of bytes written to @a buf
359  */
360 static size_t
361 transmit_to_client_callback (void *cls,
362                              size_t size,
363                              void *buf)
364 {
365   struct TransportClient *tc = cls;
366   struct ClientMessageQueueEntry *q;
367   const struct GNUNET_MessageHeader *msg;
368   char *cbuf;
369   uint16_t msize;
370   size_t tsize;
371
372   tc->th = NULL;
373   if (NULL == buf)
374   {
375     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376                 "Transmission to client failed, closing connection.\n");
377     return 0;
378   }
379   cbuf = buf;
380   tsize = 0;
381   while (NULL != (q = tc->message_queue_head))
382   {
383     msg = (const struct GNUNET_MessageHeader *) &q[1];
384     msize = ntohs (msg->size);
385     if (msize + tsize > size)
386       break;
387     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388                 "Transmitting message of type %u to client %p.\n",
389                 ntohs (msg->type), tc);
390     GNUNET_CONTAINER_DLL_remove (tc->message_queue_head,
391                                  tc->message_queue_tail,
392                                  q);
393     tc->message_count--;
394     memcpy (&cbuf[tsize], msg, msize);
395     GNUNET_free (q);
396     tsize += msize;
397   }
398   if (NULL != q)
399   {
400     GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
401     tc->th =
402         GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
403                                              GNUNET_TIME_UNIT_FOREVER_REL,
404                                              &transmit_to_client_callback, tc);
405     GNUNET_assert (NULL != tc->th);
406   }
407   return tsize;
408 }
409
410
411 /**
412  * Queue the given message for transmission to the given client
413  *
414  * @param tc target of the message
415  * @param msg message to transmit
416  * @param may_drop #GNUNET_YES if the message can be dropped
417  */
418 static void
419 unicast (struct TransportClient *tc,
420          const struct GNUNET_MessageHeader *msg,
421          int may_drop)
422 {
423   struct ClientMessageQueueEntry *q;
424   uint16_t msize;
425
426   if (NULL == msg)
427   {
428     GNUNET_break (0);
429     return;
430   }
431
432   if ((tc->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
433   {
434     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
435                 _("Dropping message of type %u and size %u, have %u/%u messages pending\n"),
436                 ntohs (msg->type),
437                 ntohs (msg->size),
438                 tc->message_count,
439                 MAX_PENDING);
440     GNUNET_STATISTICS_update (GST_stats,
441                               gettext_noop
442                               ("# messages dropped due to slow client"), 1,
443                               GNUNET_NO);
444     return;
445   }
446   msize = ntohs (msg->size);
447   GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
448   q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
449   memcpy (&q[1], msg, msize);
450   GNUNET_CONTAINER_DLL_insert_tail (tc->message_queue_head,
451                                     tc->message_queue_tail, q);
452   tc->message_count++;
453   if (NULL != tc->th)
454     return;
455   tc->th =
456       GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
457                                            GNUNET_TIME_UNIT_FOREVER_REL,
458                                            &transmit_to_client_callback, tc);
459   GNUNET_assert (NULL != tc->th);
460 }
461
462
463 /**
464  * Called whenever a client is disconnected.  Frees our
465  * resources associated with that client.
466  *
467  * @param cls closure
468  * @param client identification of the client
469  */
470 static void
471 client_disconnect_notification (void *cls,
472                                 struct GNUNET_SERVER_Client *client)
473 {
474   struct TransportClient *tc;
475   struct MonitoringClient *mc;
476   struct ClientMessageQueueEntry *mqe;
477
478   if (client == NULL)
479     return;
480   mc = lookup_monitoring_client (peer_monitoring_clients_head, client);
481   if (mc != NULL)
482   {
483     GNUNET_CONTAINER_DLL_remove (peer_monitoring_clients_head,
484                                  peer_monitoring_clients_tail,
485                                  mc);
486     GNUNET_free (mc);
487   }
488   mc = lookup_monitoring_client (val_monitoring_clients_head, client);
489   if (mc != NULL)
490   {
491     GNUNET_CONTAINER_DLL_remove (val_monitoring_clients_head,
492                                  val_monitoring_clients_tail,
493                                  mc);
494     GNUNET_free (mc);
495   }
496   tc = lookup_client (client);
497   if (tc == NULL)
498     return;
499   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
500               "Client %p disconnected, cleaning up.\n", tc);
501   while (NULL != (mqe = tc->message_queue_head))
502   {
503     GNUNET_CONTAINER_DLL_remove (tc->message_queue_head, tc->message_queue_tail,
504                                  mqe);
505     tc->message_count--;
506     GNUNET_free (mqe);
507   }
508   GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, tc);
509   if (tc->th != NULL)
510   {
511     GNUNET_SERVER_notify_transmit_ready_cancel (tc->th);
512     tc->th = NULL;
513   }
514   GNUNET_break (0 == tc->message_count);
515   GNUNET_free (tc);
516 }
517
518
519 /**
520  * Function called for each of our connected neighbours.  Notify the
521  * client about the existing neighbour.
522  *
523  * @param cls the `struct TransportClient *` to notify
524  * @param peer identity of the neighbour
525  * @param address the address
526  * @param state the current state of the peer
527  * @param state_timeout the time out for the state
528  * @param bandwidth_in inbound bandwidth in NBO
529  * @param bandwidth_out outbound bandwidth in NBO
530  */
531 static void
532 notify_client_about_neighbour (void *cls,
533                                const struct GNUNET_PeerIdentity *peer,
534                                const struct GNUNET_HELLO_Address *address,
535                                enum GNUNET_TRANSPORT_PeerState state,
536                                struct GNUNET_TIME_Absolute state_timeout,
537                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
538                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
539 {
540   struct TransportClient *tc = cls;
541   struct ConnectInfoMessage *cim;
542   size_t size = sizeof (struct ConnectInfoMessage);
543   char buf[size] GNUNET_ALIGN;
544
545   if (GNUNET_NO == GST_neighbours_test_connected (peer))
546     return;
547
548   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
549   cim = (struct ConnectInfoMessage *) buf;
550   cim->header.size = htons (size);
551   cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
552   cim->id = *peer;
553   cim->quota_in = bandwidth_in;
554   cim->quota_out = bandwidth_out;
555   unicast (tc, &cim->header, GNUNET_NO);
556 }
557
558
559 /**
560  * Initialize a normal client.  We got a start message from this
561  * client, add him to the list of clients for broadcasting of inbound
562  * messages.
563  *
564  * @param cls unused
565  * @param client the client
566  * @param message the start message that was sent
567  */
568 static void
569 clients_handle_start (void *cls,
570                       struct GNUNET_SERVER_Client *client,
571                       const struct GNUNET_MessageHeader *message)
572 {
573   const struct StartMessage *start;
574   struct TransportClient *tc;
575   uint32_t options;
576
577   tc = lookup_client (client);
578
579   GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
580               "Client %p sent START\n", tc);
581   if (tc != NULL)
582   {
583     /* got 'start' twice from the same client, not allowed */
584     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
585                 "TransportClient %p ServerClient %p sent multiple START messages\n",
586                 tc, tc->client);
587     GNUNET_break (0);
588     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
589     return;
590   }
591   start = (const struct StartMessage *) message;
592   options = ntohl (start->options);
593   if ((0 != (1 & options)) &&
594       (0 !=
595        memcmp (&start->self, &GST_my_identity,
596                sizeof (struct GNUNET_PeerIdentity))))
597   {
598     /* client thinks this is a different peer, reject */
599     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
600                 _
601                 ("Rejecting control connection from peer `%s', which is not me!\n"),
602                 GNUNET_i2s (&start->self));
603     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
604     return;
605   }
606   tc = setup_client (client);
607   tc->send_payload = (0 != (2 & options));
608   unicast (tc, GST_hello_get (), GNUNET_NO);
609   GST_neighbours_iterate (&notify_client_about_neighbour, tc);
610   GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, tc);
611   GNUNET_SERVER_receive_done (client, GNUNET_OK);
612 }
613
614
615 /**
616  * Client sent us a HELLO.  Process the request.
617  *
618  * @param cls unused
619  * @param client the client
620  * @param message the HELLO message
621  */
622 static void
623 clients_handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
624                       const struct GNUNET_MessageHeader *message)
625 {
626   GST_validation_handle_hello (message);
627   GNUNET_SERVER_receive_done (client, GNUNET_OK);
628 }
629
630
631 /**
632  * Closure for 'handle_send_transmit_continuation'
633  */
634 struct SendTransmitContinuationContext
635 {
636   /**
637    * Client that made the request.
638    */
639   struct GNUNET_SERVER_Client *client;
640
641   /**
642    * Peer that was the target.
643    */
644   struct GNUNET_PeerIdentity target;
645 };
646
647
648 /**
649  * Function called after the transmission is done.  Notify the client that it is
650  * OK to send the next message.
651  *
652  * @param cls closure
653  * @param success #GNUNET_OK on success, #GNUNET_NO on failure, #GNUNET_SYSERR if we're not connected
654  * @param bytes_payload bytes payload sent
655  * @param bytes_on_wire bytes sent on wire
656  */
657 static void
658 handle_send_transmit_continuation (void *cls, int success,
659                                    size_t bytes_payload,
660                                    size_t bytes_on_wire)
661 {
662   struct SendTransmitContinuationContext *stcc = cls;
663   struct SendOkMessage send_ok_msg;
664
665   if (GNUNET_OK == success)
666     GST_neighbours_notify_payload_sent (&stcc->target, bytes_payload);
667
668   send_ok_msg.header.size = htons (sizeof (send_ok_msg));
669   send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
670   send_ok_msg.bytes_msg = htonl (bytes_payload);
671   send_ok_msg.bytes_physical = htonl (bytes_on_wire);
672   send_ok_msg.success = htonl (success);
673   send_ok_msg.latency =
674       GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
675   send_ok_msg.peer = stcc->target;
676   GST_clients_unicast (stcc->client, &send_ok_msg.header, GNUNET_NO);
677   GNUNET_SERVER_client_drop (stcc->client);
678   GNUNET_free (stcc);
679 }
680
681
682 /**
683  * Client asked for transmission to a peer.  Process the request.
684  *
685  * @param cls unused
686  * @param client the client
687  * @param message the send message that was sent
688  */
689 static void
690 clients_handle_send (void *cls,
691                      struct GNUNET_SERVER_Client *client,
692                      const struct GNUNET_MessageHeader *message)
693 {
694   const struct OutboundMessage *obm;
695   const struct GNUNET_MessageHeader *obmm;
696   struct SendTransmitContinuationContext *stcc;
697   uint16_t size;
698   uint16_t msize;
699   struct TransportClient *tc;
700
701   tc = lookup_client (client);
702   if (NULL == tc)
703   {
704     /* client asked for transmission before 'START' */
705     GNUNET_break (0);
706     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
707     return;
708   }
709
710   size = ntohs (message->size);
711   if (size <
712       sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
713   {
714     GNUNET_break (0);
715     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
716     return;
717   }
718   obm = (const struct OutboundMessage *) message;
719   obmm = (const struct GNUNET_MessageHeader *) &obm[1];
720   msize = size - sizeof (struct OutboundMessage);
721   if (msize < sizeof (struct GNUNET_MessageHeader))
722   {
723     GNUNET_break (0);
724     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
725     return;
726   }
727
728   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
729               "Received `%s' request from client with target `%4s' and first message of type %u and total size %u\n",
730               "SEND",
731               GNUNET_i2s (&obm->peer),
732               ntohs (obmm->type),
733               msize);
734   if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
735   {
736     /* not connected, not allowed to send; can happen due to asynchronous operations */
737     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738                 "Could not send message to peer `%s': not connected\n",
739                 GNUNET_i2s (&obm->peer));
740     GNUNET_STATISTICS_update (GST_stats,
741                               gettext_noop
742                               ("# bytes payload dropped (other peer was not connected)"),
743                               msize, GNUNET_NO);
744     GNUNET_SERVER_receive_done (client, GNUNET_OK);
745     return;
746   }
747   GNUNET_SERVER_receive_done (client, GNUNET_OK);
748   stcc = GNUNET_new (struct SendTransmitContinuationContext);
749   stcc->target = obm->peer;
750   stcc->client = client;
751   GNUNET_SERVER_client_keep (client);
752   GST_manipulation_send (&obm->peer, obmm, msize,
753                        GNUNET_TIME_relative_ntoh (obm->timeout),
754                        &handle_send_transmit_continuation, stcc);
755 }
756
757
758 /**
759  * Try to initiate a connection to the given peer if the blacklist
760  * allowed it.
761  *
762  * @param cls closure (unused, NULL)
763  * @param peer identity of peer that was tested
764  * @param result #GNUNET_OK if the connection is allowed,
765  *               #GNUNET_NO if not
766  */
767 static void
768 try_connect_if_allowed (void *cls,
769                         const struct GNUNET_PeerIdentity *peer,
770                         int result)
771 {
772   if (GNUNET_OK != result)
773   {
774     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
775                 _("Blacklist refuses connection attempt to peer `%s'\n"),
776                 GNUNET_i2s (peer));
777     return;                     /* not allowed */
778   }
779
780   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781               _("Blacklist allows connection attempt to peer `%s'\n"),
782               GNUNET_i2s (peer));
783
784   GST_neighbours_try_connect (peer);
785 }
786
787
788 /**
789  * Handle request connect message
790  *
791  * @param cls closure (always NULL)
792  * @param client identification of the client
793  * @param message the actual message
794  */
795 static void
796 clients_handle_request_connect (void *cls,
797                                 struct GNUNET_SERVER_Client *client,
798                                 const struct GNUNET_MessageHeader *message)
799 {
800   const struct TransportRequestConnectMessage *trcm =
801       (const struct TransportRequestConnectMessage *) message;
802
803   if (GNUNET_YES == ntohl (trcm->connect))
804   {
805     GNUNET_STATISTICS_update (GST_stats,
806                               gettext_noop
807                               ("# REQUEST CONNECT messages received"), 1,
808                               GNUNET_NO);
809
810     if (0 == memcmp (&trcm->peer, &GST_my_identity,
811                   sizeof (struct GNUNET_PeerIdentity)))
812     {
813       GNUNET_break_op (0);
814       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
815                   "Received a request connect message myself `%s'\n",
816                   GNUNET_i2s (&trcm->peer));
817     }
818     else
819     {
820       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
821                   _("Received a request connect message for peer `%s'\n"),
822                   GNUNET_i2s (&trcm->peer));
823
824       (void) GST_blacklist_test_allowed (&trcm->peer, NULL, &try_connect_if_allowed,
825                                        NULL);
826     }
827   }
828   else if (GNUNET_NO == ntohl (trcm->connect))
829   {
830     GNUNET_STATISTICS_update (GST_stats,
831                               gettext_noop
832                               ("# REQUEST DISCONNECT messages received"), 1,
833                               GNUNET_NO);
834
835     if (0 == memcmp (&trcm->peer, &GST_my_identity,
836                   sizeof (struct GNUNET_PeerIdentity)))
837     {
838       GNUNET_break_op (0);
839       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
840                   "Received a request disconnect message myself `%s'\n",
841                   GNUNET_i2s (&trcm->peer));
842     }
843     else
844     {
845       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
846                   _("Received a request disconnect message for peer `%s'\n"),
847                   GNUNET_i2s (&trcm->peer));
848       (void) GST_neighbours_force_disconnect (&trcm->peer);
849     }
850   }
851   else
852   {
853     GNUNET_break_op (0);
854     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
855     return;
856   }
857   GNUNET_SERVER_receive_done (client, GNUNET_OK);
858 }
859
860
861 /**
862  * Take the given address and append it to the set of results sent back to
863  * the client.  This function may be called serveral times for a single
864  * conversion.   The last invocation will be with a @a address of
865  * NULL and a @a res of #GNUNET_OK.  Thus, to indicate conversion
866  * errors, the callback might be called first with @a address NULL and
867  * @a res being #GNUNET_SYSERR.  In that case, there will still be a
868  * subsequent call later with @a address NULL and @a res #GNUNET_OK.
869  *
870  * @param cls the transmission context used (`struct GNUNET_SERVER_TransmitContext *`)
871  * @param buf text to transmit (contains the human-readable address, or NULL)
872  * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on error,
873  *            never #GNUNET_NO
874  */
875 static void
876 transmit_address_to_client (void *cls,
877                             const char *buf,
878                             int res)
879 {
880   struct AddressToStringContext *actx = cls;
881   struct AddressToStringResultMessage *atsm;
882   size_t len;
883   size_t slen;
884
885   GNUNET_assert ( (GNUNET_OK == res) ||
886                   (GNUNET_SYSERR == res) );
887   if (NULL == buf)
888   {
889     len = sizeof (struct AddressToStringResultMessage);
890     atsm = GNUNET_malloc (len);
891     atsm->header.size = ntohs (len);
892     atsm->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
893     if (GNUNET_OK == res)
894     {
895       /* this was the last call, transmit */
896       atsm->res = htonl (GNUNET_OK);
897       atsm->addr_len = htonl (0);
898       GNUNET_SERVER_transmit_context_append_message (actx->tc,
899                                                      (const struct GNUNET_MessageHeader *) atsm);
900       GNUNET_SERVER_transmit_context_run (actx->tc,
901                                           GNUNET_TIME_UNIT_FOREVER_REL);
902       GNUNET_CONTAINER_DLL_remove (a2s_head,
903                                    a2s_tail,
904                                    actx);
905       GNUNET_free (atsm);
906       GNUNET_free (actx);
907       return;
908     }
909     if (GNUNET_SYSERR == res)
910     {
911       /* address conversion failed, but there will be more callbacks */
912       atsm->res = htonl (GNUNET_SYSERR);
913       atsm->addr_len = htonl (0);
914       GNUNET_SERVER_transmit_context_append_message (actx->tc,
915                                                      (const struct GNUNET_MessageHeader *) atsm);
916       GNUNET_free (atsm);
917       return;
918     }
919   }
920   GNUNET_assert (GNUNET_OK == res);
921   /* succesful conversion, append*/
922   slen = strlen (buf) + 1;
923   len = sizeof (struct AddressToStringResultMessage) + slen;
924   atsm = GNUNET_malloc (len);
925   atsm->header.size = ntohs (len);
926   atsm->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
927   atsm->res = htonl (GNUNET_YES);
928   atsm->addr_len = htonl (slen);
929   memcpy (&atsm[1],
930           buf,
931           slen);
932   GNUNET_SERVER_transmit_context_append_message (actx->tc,
933                                                  (const struct GNUNET_MessageHeader *) atsm);
934   GNUNET_free (atsm);
935 }
936
937
938 /**
939  * Client asked to resolve an address.  Process the request.
940  *
941  * @param cls unused
942  * @param client the client
943  * @param message the resolution request
944  */
945 static void
946 clients_handle_address_to_string (void *cls,
947                                   struct GNUNET_SERVER_Client *client,
948                                   const struct GNUNET_MessageHeader *message)
949 {
950   const struct AddressLookupMessage *alum;
951   struct GNUNET_TRANSPORT_PluginFunctions *papi;
952   const char *plugin_name;
953   const char *address;
954   uint32_t address_len;
955   uint16_t size;
956   struct GNUNET_SERVER_TransmitContext *tc;
957   struct AddressToStringContext *actx;
958   struct AddressToStringResultMessage atsm;
959   struct GNUNET_TIME_Relative rtimeout;
960   int32_t numeric;
961
962   size = ntohs (message->size);
963   if (size < sizeof (struct AddressLookupMessage))
964   {
965     GNUNET_break (0);
966     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
967     return;
968   }
969   alum = (const struct AddressLookupMessage *) message;
970   address_len = ntohs (alum->addrlen);
971   if (size <= sizeof (struct AddressLookupMessage) + address_len)
972   {
973     GNUNET_break (0);
974     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
975     return;
976   }
977   address = (const char *) &alum[1];
978   plugin_name = (const char *) &address[address_len];
979   if ('\0' != plugin_name[size - sizeof (struct AddressLookupMessage) - address_len - 1])
980   {
981     GNUNET_break (0);
982     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
983     return;
984   }
985   rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
986   numeric = ntohs (alum->numeric_only);
987   tc = GNUNET_SERVER_transmit_context_create (client);
988   papi = GST_plugins_printer_find (plugin_name);
989   if (NULL == papi)
990   {
991     atsm.header.size = ntohs (sizeof (struct AddressToStringResultMessage));
992     atsm.header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
993     atsm.res = htonl (GNUNET_SYSERR);
994     atsm.addr_len = htonl (0);
995     GNUNET_SERVER_transmit_context_append_message (tc,
996                                                    &atsm.header);
997     atsm.header.size = ntohs (sizeof (struct AddressToStringResultMessage));
998     atsm.header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
999     atsm.res = htonl (GNUNET_OK);
1000     atsm.addr_len = htonl (0);
1001     GNUNET_SERVER_transmit_context_append_message (tc,
1002                                                    &atsm.header);
1003     GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1004     return;
1005   }
1006   actx = GNUNET_new (struct AddressToStringContext);
1007   actx->tc = tc;
1008   GNUNET_CONTAINER_DLL_insert (a2s_head, a2s_tail, actx);
1009   GNUNET_SERVER_disable_receive_done_warning (client);
1010   papi->address_pretty_printer (papi->cls,
1011                                 plugin_name,
1012                                 address, address_len,
1013                                 numeric,
1014                                 rtimeout,
1015                                 &transmit_address_to_client,
1016                                 actx);
1017 }
1018
1019
1020 /**
1021  * Compose #PeerIterateResponseMessage using the given peer and address.
1022  *
1023  * @param peer identity of the peer
1024  * @param address the address, NULL on disconnect
1025  * @return composed message
1026  */
1027 static struct PeerIterateResponseMessage *
1028 compose_address_iterate_response_message (const struct GNUNET_PeerIdentity *peer,
1029                                           const struct GNUNET_HELLO_Address *address)
1030 {
1031   struct PeerIterateResponseMessage *msg;
1032   size_t size;
1033   size_t tlen;
1034   size_t alen;
1035   char *addr;
1036
1037   GNUNET_assert (NULL != peer);
1038   if (NULL != address)
1039   {
1040     tlen = strlen (address->transport_name) + 1;
1041     alen = address->address_length;
1042   }
1043   else
1044     tlen = alen = 0;
1045   size = (sizeof (struct PeerIterateResponseMessage) + alen + tlen);
1046   msg = GNUNET_malloc (size);
1047   msg->header.size = htons (size);
1048   msg->header.type =
1049       htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
1050   msg->reserved = htonl (0);
1051   msg->peer = *peer;
1052   msg->addrlen = htonl (alen);
1053   msg->pluginlen = htonl (tlen);
1054
1055   if (NULL != address)
1056   {
1057     msg->local_address_info = htonl((uint32_t) address->local_info);
1058     addr = (char *) &msg[1];
1059     memcpy (addr, address->address, alen);
1060     memcpy (&addr[alen], address->transport_name, tlen);
1061   }
1062   return msg;
1063 }
1064
1065 /**
1066  * Compose #PeerIterateResponseMessage using the given peer and address.
1067  *
1068  * @param peer identity of the peer
1069  * @param address the address, NULL on disconnect
1070  * @return composed message
1071  */
1072 static struct ValidationIterateResponseMessage *
1073 compose_validation_iterate_response_message (const struct GNUNET_PeerIdentity *peer,
1074                                           const struct GNUNET_HELLO_Address *address)
1075 {
1076   struct ValidationIterateResponseMessage *msg;
1077   size_t size;
1078   size_t tlen;
1079   size_t alen;
1080   char *addr;
1081
1082   GNUNET_assert (NULL != peer);
1083   if (NULL != address)
1084   {
1085     tlen = strlen (address->transport_name) + 1;
1086     alen = address->address_length;
1087   }
1088   else
1089     tlen = alen = 0;
1090   size = (sizeof (struct ValidationIterateResponseMessage) + alen + tlen);
1091   msg = GNUNET_malloc (size);
1092   msg->header.size = htons (size);
1093   msg->header.type =
1094       htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE);
1095   msg->reserved = htonl (0);
1096   msg->peer = *peer;
1097   msg->addrlen = htonl (alen);
1098   msg->pluginlen = htonl (tlen);
1099
1100   if (NULL != address)
1101   {
1102     msg->local_address_info = htonl((uint32_t) address->local_info);
1103     addr = (char *) &msg[1];
1104     memcpy (addr, address->address, alen);
1105     memcpy (&addr[alen], address->transport_name, tlen);
1106   }
1107   return msg;
1108 }
1109
1110 struct IterationContext
1111 {
1112   struct GNUNET_SERVER_TransmitContext *tc;
1113
1114   struct GNUNET_PeerIdentity id;
1115
1116   int all;
1117 };
1118
1119
1120 /**
1121  * Output information of validation entries to the given client.
1122  *
1123  * @param cls the `struct IterationContext *`
1124  * @param peer identity of the neighbour
1125  * @param address the address
1126  * @param last_validation point in time when last validation was performed
1127  * @param valid_until point in time how long address is valid
1128  * @param next_validation point in time when next validation will be performed
1129  * @param state state of validation notification
1130  */
1131 static void
1132 send_validation_information (void *cls,
1133                              const struct GNUNET_PeerIdentity *peer,
1134                              const struct GNUNET_HELLO_Address *address,
1135                              struct GNUNET_TIME_Absolute last_validation,
1136                              struct GNUNET_TIME_Absolute valid_until,
1137                              struct GNUNET_TIME_Absolute next_validation,
1138                              enum GNUNET_TRANSPORT_ValidationState state)
1139 {
1140   struct IterationContext *pc = cls;
1141   struct ValidationIterateResponseMessage *msg;
1142
1143   if ( (GNUNET_YES == pc->all) ||
1144        (0 == memcmp (peer, &pc->id, sizeof (pc->id))) )
1145   {
1146     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147         "Sending information about for validation entry for peer `%s' using address `%s'\n",
1148         GNUNET_i2s(peer), (address != NULL) ? GST_plugins_a2s (address) : "<none>");
1149     msg = compose_validation_iterate_response_message (peer, address);
1150     msg->last_validation = GNUNET_TIME_absolute_hton(last_validation);
1151     msg->valid_until = GNUNET_TIME_absolute_hton(valid_until);
1152     msg->next_validation = GNUNET_TIME_absolute_hton(next_validation);
1153     msg->state = htonl ((uint32_t) state);
1154     GNUNET_SERVER_transmit_context_append_message (pc->tc, &msg->header);
1155     GNUNET_free (msg);
1156   }
1157 }
1158
1159
1160 /**
1161  * Output information of neighbours to the given client.
1162  *
1163  * @param cls the `struct PeerIterationContext *`
1164  * @param peer identity of the neighbour
1165  * @param address the address
1166  * @param state current state this peer is in
1167  * @param state_timeout timeout for the current state of the peer
1168  * @param bandwidth_in inbound quota in NBO
1169  * @param bandwidth_out outbound quota in NBO
1170  */
1171 static void
1172 send_peer_information (void *cls,
1173                        const struct GNUNET_PeerIdentity *peer,
1174                        const struct GNUNET_HELLO_Address *address,
1175                        enum GNUNET_TRANSPORT_PeerState state,
1176                        struct GNUNET_TIME_Absolute state_timeout,
1177                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1178                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1179 {
1180   struct IterationContext *pc = cls;
1181   struct PeerIterateResponseMessage *msg;
1182
1183   if ( (GNUNET_YES == pc->all) ||
1184        (0 == memcmp (peer, &pc->id, sizeof (pc->id))) )
1185   {
1186     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1187         "Sending information about `%s' using address `%s' in state `%s'\n",
1188         GNUNET_i2s(peer),
1189         (address != NULL) ? GST_plugins_a2s (address) : "<none>",
1190         GNUNET_TRANSPORT_ps2s (state));
1191     msg = compose_address_iterate_response_message (peer, address);
1192     msg->state = htonl (state);
1193     msg->state_timeout = GNUNET_TIME_absolute_hton(state_timeout);
1194     GNUNET_SERVER_transmit_context_append_message (pc->tc, &msg->header);
1195     GNUNET_free (msg);
1196   }
1197 }
1198
1199
1200
1201 /**
1202  * Client asked to obtain information about a specific or all peers
1203  * Process the request.
1204  *
1205  * @param cls unused
1206  * @param client the client
1207  * @param message the peer address information request
1208  */
1209 static void
1210 clients_handle_monitor_peers (void *cls, struct GNUNET_SERVER_Client *client,
1211                                 const struct GNUNET_MessageHeader *message)
1212 {
1213   static struct GNUNET_PeerIdentity all_zeros;
1214   struct GNUNET_SERVER_TransmitContext *tc;
1215   struct PeerMonitorMessage *msg;
1216   struct IterationContext pc;
1217
1218   if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST)
1219   {
1220     GNUNET_break (0);
1221     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1222     return;
1223   }
1224   if (ntohs (message->size) != sizeof (struct PeerMonitorMessage))
1225   {
1226     GNUNET_break (0);
1227     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1228     return;
1229   }
1230   msg = (struct PeerMonitorMessage *) message;
1231   if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
1232        (NULL != lookup_monitoring_client (peer_monitoring_clients_head, client)) )
1233   {
1234     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1235                 "ServerClient %p tried to start monitoring twice\n",
1236                 client);
1237     GNUNET_break (0);
1238     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1239     return;
1240   }
1241   GNUNET_SERVER_disable_receive_done_warning (client);
1242   pc.tc = tc = GNUNET_SERVER_transmit_context_create (client);
1243
1244   /* Send initial list */
1245   if (0 == memcmp (&msg->peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
1246   {
1247     /* iterate over all neighbours */
1248     pc.all = GNUNET_YES;
1249     pc.id = msg->peer;
1250   }
1251   else
1252   {
1253     /* just return one neighbour */
1254     pc.all = GNUNET_NO;
1255     pc.id = msg->peer;
1256   }
1257   GST_neighbours_iterate (&send_peer_information, &pc);
1258
1259   if (GNUNET_YES != ntohl (msg->one_shot))
1260   {
1261     setup_peer_monitoring_client (client, &msg->peer);
1262   }
1263   else
1264   {
1265     GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
1266         GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
1267   }
1268
1269   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1270 }
1271
1272
1273 /**
1274  * Client asked to obtain information about a specific or all validation
1275  * processes
1276  *
1277  * @param cls unused
1278  * @param client the client
1279  * @param message the peer address information request
1280  */
1281 static void
1282 clients_handle_monitor_validation (void *cls, struct GNUNET_SERVER_Client *client,
1283                                 const struct GNUNET_MessageHeader *message)
1284 {
1285   static struct GNUNET_PeerIdentity all_zeros;
1286   struct GNUNET_SERVER_TransmitContext *tc;
1287   struct PeerMonitorMessage *msg;
1288   struct IterationContext pc;
1289
1290   if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_REQUEST)
1291   {
1292     GNUNET_break (0);
1293     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1294     return;
1295   }
1296   if (ntohs (message->size) != sizeof (struct ValidationMonitorMessage))
1297   {
1298     GNUNET_break (0);
1299     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1300     return;
1301   }
1302   msg = (struct PeerMonitorMessage *) message;
1303   if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
1304        (NULL != lookup_monitoring_client (val_monitoring_clients_head, client)) )
1305   {
1306     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1307                 "ServerClient %p tried to start monitoring twice\n",
1308                 client);
1309     GNUNET_break (0);
1310     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1311     return;
1312   }
1313   GNUNET_SERVER_disable_receive_done_warning (client);
1314   pc.tc = tc = GNUNET_SERVER_transmit_context_create (client);
1315
1316   /* Send initial list */
1317   if (0 == memcmp (&msg->peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
1318   {
1319     /* iterate over all neighbours */
1320     pc.all = GNUNET_YES;
1321     pc.id = msg->peer;
1322   }
1323   else
1324   {
1325     /* just return one neighbour */
1326     pc.all = GNUNET_NO;
1327     pc.id = msg->peer;
1328   }
1329
1330   GST_validation_iterate (&send_validation_information, &pc);
1331
1332   if (GNUNET_YES != ntohl (msg->one_shot))
1333   {
1334     setup_val_monitoring_client (client, &msg->peer);
1335   }
1336   else
1337   {
1338     GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
1339         GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE);
1340   }
1341   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1342 }
1343
1344 /**
1345  * Start handling requests from clients.
1346  *
1347  * @param server server used to accept clients from.
1348  */
1349 void
1350 GST_clients_start (struct GNUNET_SERVER_Handle *server)
1351 {
1352   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1353     {&clients_handle_start, NULL,
1354      GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
1355     {&clients_handle_hello, NULL,
1356      GNUNET_MESSAGE_TYPE_HELLO, 0},
1357     {&clients_handle_send, NULL,
1358      GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
1359     {&clients_handle_request_connect, NULL,
1360      GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT,
1361      sizeof (struct TransportRequestConnectMessage)},
1362     {&clients_handle_address_to_string, NULL,
1363      GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING, 0},
1364     {&clients_handle_monitor_peers, NULL,
1365      GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST,
1366      sizeof (struct PeerMonitorMessage)},
1367     {&clients_handle_monitor_validation, NULL,
1368      GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_REQUEST,
1369      sizeof (struct ValidationMonitorMessage)},
1370     {&GST_blacklist_handle_init, NULL,
1371      GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT,
1372      sizeof (struct GNUNET_MessageHeader)},
1373     {&GST_blacklist_handle_reply, NULL,
1374      GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY,
1375      sizeof (struct BlacklistMessage)},
1376     {&GST_manipulation_set_metric, NULL,
1377      GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC, 0},
1378     {NULL, NULL, 0, 0}
1379   };
1380   peer_nc = GNUNET_SERVER_notification_context_create (server, 0);
1381   val_nc = GNUNET_SERVER_notification_context_create (server, 0);
1382   GNUNET_SERVER_add_handlers (server, handlers);
1383   GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
1384                                    NULL);
1385 }
1386
1387
1388 /**
1389  * Stop processing clients.
1390  */
1391 void
1392 GST_clients_stop ()
1393 {
1394   struct AddressToStringContext *cur;
1395
1396   while (NULL != (cur = a2s_head))
1397   {
1398     GNUNET_SERVER_transmit_context_destroy (cur->tc, GNUNET_NO);
1399     GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, cur);
1400     GNUNET_free (cur);
1401   }
1402   if (NULL != peer_nc)
1403   {
1404     GNUNET_SERVER_notification_context_destroy (peer_nc);
1405     peer_nc = NULL;
1406   }
1407   if (NULL != val_nc)
1408   {
1409     GNUNET_SERVER_notification_context_destroy (val_nc);
1410     val_nc = NULL;
1411   }
1412 }
1413
1414
1415 /**
1416  * Broadcast the given message to all of our clients.
1417  *
1418  * @param msg message to broadcast
1419  * @param may_drop #GNUNET_YES if the message can be dropped / is payload
1420  */
1421 void
1422 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg, int may_drop)
1423 {
1424   struct TransportClient *tc;
1425
1426   for (tc = clients_head; NULL != tc; tc = tc->next)
1427   {
1428     if ((GNUNET_YES == may_drop) && (GNUNET_YES != tc->send_payload))
1429       continue;                 /* skip, this client does not care about payload */
1430     unicast (tc, msg, may_drop);
1431   }
1432 }
1433
1434
1435 /**
1436  * Send the given message to a particular client
1437  *
1438  * @param client target of the message
1439  * @param msg message to transmit
1440  * @param may_drop #GNUNET_YES if the message can be dropped
1441  */
1442 void
1443 GST_clients_unicast (struct GNUNET_SERVER_Client *client,
1444                      const struct GNUNET_MessageHeader *msg, int may_drop)
1445 {
1446   struct TransportClient *tc;
1447
1448   tc = lookup_client (client);
1449   if (NULL == tc)
1450     return;                     /* client got disconnected in the meantime, drop message */
1451   unicast (tc, msg, may_drop);
1452 }
1453
1454
1455 /**
1456  * Broadcast the new active address to all clients monitoring the peer.
1457  *
1458  * @param peer peer this update is about (never NULL)
1459  * @param address address, NULL on disconnect
1460  * @param state the current state of the peer
1461  * @param state_timeout the time out for the state
1462  */
1463 void
1464 GST_clients_broadcast_peer_notification (const struct GNUNET_PeerIdentity *peer,
1465     const struct GNUNET_HELLO_Address *address,
1466     enum GNUNET_TRANSPORT_PeerState state,
1467     struct GNUNET_TIME_Absolute state_timeout)
1468 {
1469   struct PeerIterateResponseMessage *msg;
1470   struct MonitoringClient *mc;
1471   static struct GNUNET_PeerIdentity all_zeros;
1472   msg = compose_address_iterate_response_message (peer, address);
1473   msg->state = htonl (state);
1474   msg->state_timeout = GNUNET_TIME_absolute_hton (state_timeout);
1475   mc = peer_monitoring_clients_head;
1476   while (mc != NULL)
1477   {
1478     if ((0 == memcmp (&mc->peer, &all_zeros,
1479                       sizeof (struct GNUNET_PeerIdentity))) ||
1480         (0 == memcmp (&mc->peer, peer,
1481                       sizeof (struct GNUNET_PeerIdentity))))
1482     {
1483       GNUNET_SERVER_notification_context_unicast (peer_nc, mc->client,
1484                                                   &msg->header, GNUNET_NO);
1485     }
1486
1487     mc = mc->next;
1488   }
1489   GNUNET_free (msg);
1490 }
1491
1492 /**
1493  * Broadcast the new validation changes to all clients monitoring the peer.
1494  *
1495  * @param peer peer this update is about (never NULL)
1496  * @param address address, NULL on disconnect
1497  * @param last_validation point in time when last validation was performed
1498  * @param valid_until point in time how long address is valid
1499  * @param next_validation point in time when next validation will be performed
1500  * @param state state of validation notification
1501  */
1502 void
1503 GST_clients_broadcast_validation_notification (const struct GNUNET_PeerIdentity *peer,
1504                                                const struct GNUNET_HELLO_Address *address,
1505                                                struct GNUNET_TIME_Absolute last_validation,
1506                                                struct GNUNET_TIME_Absolute valid_until,
1507                                                struct GNUNET_TIME_Absolute next_validation,
1508                                                enum GNUNET_TRANSPORT_ValidationState state)
1509 {
1510   struct ValidationIterateResponseMessage *msg;
1511   struct MonitoringClient *mc;
1512   static struct GNUNET_PeerIdentity all_zeros;
1513
1514   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1515               "Sending information about for validation entry for peer `%s' using address `%s'\n",
1516               GNUNET_i2s(peer), (address != NULL) ? GST_plugins_a2s (address) : "<none>");
1517
1518   msg = compose_validation_iterate_response_message (peer, address);
1519   msg->last_validation = GNUNET_TIME_absolute_hton(last_validation);
1520   msg->valid_until = GNUNET_TIME_absolute_hton(valid_until);
1521   msg->next_validation = GNUNET_TIME_absolute_hton(next_validation);
1522   msg->state = htonl ((uint32_t) state);
1523   mc = val_monitoring_clients_head;
1524   while (mc != NULL)
1525   {
1526     if ((0 == memcmp (&mc->peer, &all_zeros,
1527                       sizeof (struct GNUNET_PeerIdentity))) ||
1528         (0 == memcmp (&mc->peer, peer,
1529                       sizeof (struct GNUNET_PeerIdentity))))
1530     {
1531       GNUNET_SERVER_notification_context_unicast (val_nc, mc->client,
1532                                                   &msg->header, GNUNET_NO);
1533
1534     }
1535     mc = mc->next;
1536   }
1537   GNUNET_free (msg);
1538 }
1539
1540
1541 /* end of file gnunet-service-transport_clients.c */