-allow caller ID to differ from zone used for resolution
[oweals/gnunet.git] / src / transport / transport_api_monitoring.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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/transport_api_monitoring.c
23  * @brief montoring api for transport peer status and validation entries
24  *
25  * This api provides the ability to query the transport service about
26  * the status of a specific or all peers as well as address validation entries.
27  *
28  * Calls back with information about peer(s) including address used, state and
29  * state timeout for peer requests and address, address lifetime and next revalidation
30  * for validation entries.
31  */
32 #include "platform.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_arm_service.h"
35 #include "gnunet_hello_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_transport_service.h"
38 #include "transport.h"
39
40 /**
41  * Context for iterating validation entries.
42  */
43 struct GNUNET_TRANSPORT_PeerMonitoringContext
44 {
45   /**
46    * Function to call with the binary address.
47    */
48   GNUNET_TRANSPORT_PeerIterateCallback cb;
49
50   /**
51    * Closure for cb.
52    */
53   void *cb_cls;
54
55   /**
56    * Connection to the service.
57    */
58   struct GNUNET_CLIENT_Connection *client;
59
60   /**
61    * Configuration we use.
62    */
63   const struct GNUNET_CONFIGURATION_Handle *cfg;
64
65   /**
66    * When should this operation time out?
67    */
68   struct GNUNET_TIME_Absolute timeout;
69
70   /**
71    * Backoff for reconnect.
72    */
73   struct GNUNET_TIME_Relative backoff;
74
75   /**
76    * Task ID for reconnect.
77    */
78   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
79
80   /**
81    * Identity of the peer to monitor.
82    */
83   struct GNUNET_PeerIdentity peer;
84
85   /**
86    * Was this a one-shot request?
87    */
88   int one_shot;
89 };
90
91
92 /**
93  * Context for the address lookup.
94  */
95 struct GNUNET_TRANSPORT_ValidationMonitoringContext
96 {
97   /**
98    * Function to call with the binary address.
99    */
100   GNUNET_TRANSPORT_ValidationIterateCallback cb;
101
102   /**
103    * Closure for cb.
104    */
105   void *cb_cls;
106
107   /**
108    * Connection to the service.
109    */
110   struct GNUNET_CLIENT_Connection *client;
111
112   /**
113    * Configuration we use.
114    */
115   const struct GNUNET_CONFIGURATION_Handle *cfg;
116
117   /**
118    * When should this operation time out?
119    */
120   struct GNUNET_TIME_Absolute timeout;
121
122   /**
123    * Backoff for reconnect.
124    */
125   struct GNUNET_TIME_Relative backoff;
126
127   /**
128    * Task ID for reconnect.
129    */
130   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
131
132   /**
133    * Identity of the peer to monitor.
134    */
135   struct GNUNET_PeerIdentity peer;
136
137   /**
138    * Was this a one-shot request?
139    */
140   int one_shot;
141 };
142
143 /**
144  * Check if a state is defined as connected
145  *
146  * @param state the state value
147  * @return GNUNET_YES or GNUNET_NO
148  */
149 int
150 GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state)
151 {
152   switch (state)
153   {
154   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
155   case GNUNET_TRANSPORT_PS_INIT_ATS:
156   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
157   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
158   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
159     return GNUNET_NO;
160   case GNUNET_TRANSPORT_PS_CONNECTED:
161   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
162   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
163   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
164     return GNUNET_YES;
165   case GNUNET_TRANSPORT_PS_DISCONNECT:
166   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
167     return GNUNET_NO;
168   default:
169     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
170                 "Unhandled state `%s' \n",
171                 GNUNET_TRANSPORT_ps2s (state));
172     GNUNET_break (0);
173     break;
174   }
175   return GNUNET_SYSERR;
176 }
177
178 /**
179  * Convert peer state to human-readable string.
180  *
181  * @param state the state value
182  * @return corresponding string
183  */
184 const char *
185 GNUNET_TRANSPORT_ps2s (enum GNUNET_TRANSPORT_PeerState state)
186 {
187   switch (state)
188   {
189   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
190     return "S_NOT_CONNECTED";
191   case GNUNET_TRANSPORT_PS_INIT_ATS:
192     return "S_INIT_ATS";
193   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
194     return "S_CONNECT_SENT";
195   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
196     return "S_CONNECT_RECV_ATS";
197   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
198     return "S_CONNECT_RECV_ACK";
199   case GNUNET_TRANSPORT_PS_CONNECTED:
200     return "S_CONNECTED";
201   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
202     return "S_RECONNECT_ATS";
203   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
204     return "S_RECONNECT_SENT";
205   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
206     return "S_CONNECTED_SWITCHING_CONNECT_SENT";
207   case GNUNET_TRANSPORT_PS_DISCONNECT:
208     return "S_DISCONNECT";
209   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
210     return "S_DISCONNECT_FINISHED";
211   default:
212     GNUNET_break (0);
213     return "UNDEFINED";
214   }
215 }
216
217 /**
218  * Convert validation state to human-readable string.
219  *
220  * @param state the state value
221  * @return corresponding string
222  */
223 const char *
224 GNUNET_TRANSPORT_vs2s (enum GNUNET_TRANSPORT_ValidationState state)
225 {
226   switch (state)
227   {
228   case GNUNET_TRANSPORT_VS_NONE:
229     return "NONE";
230   case GNUNET_TRANSPORT_VS_NEW:
231     return "NEW";
232   case GNUNET_TRANSPORT_VS_REMOVE:
233     return "REMOVE";
234   case GNUNET_TRANSPORT_VS_TIMEOUT:
235     return "TIMEOUT";
236   case GNUNET_TRANSPORT_VS_UPDATE:
237     return "UPDATE";
238   default:
239     GNUNET_break (0);
240     return "UNDEFINED";
241   }
242 }
243
244
245 /**
246  * Function called with responses from the service.
247  *
248  * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
249  * @param msg NULL on timeout or error, otherwise presumably a
250  *        message with the human-readable address
251  */
252 static void
253 peer_response_processor (void *cls, const struct GNUNET_MessageHeader *msg);
254
255
256 /**
257  * Function called with responses from the service.
258  *
259  * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
260  * @param msg NULL on timeout or error, otherwise presumably a
261  *        message with the human-readable address
262  */
263 static void
264 val_response_processor (void *cls, const struct GNUNET_MessageHeader *msg);
265
266 /**
267  * Send our subscription request to the service.
268  *
269  * @param pal_ctx our context
270  */
271 static void
272 send_peer_mon_request (struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx)
273 {
274   struct PeerMonitorMessage msg;
275
276   msg.header.size = htons (sizeof (struct PeerMonitorMessage));
277   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST);
278   msg.one_shot = htonl (pal_ctx->one_shot);
279   msg.peer = pal_ctx->peer;
280   GNUNET_assert (GNUNET_OK ==
281                  GNUNET_CLIENT_transmit_and_get_response (pal_ctx->client,
282                     &msg.header,
283                     GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout),
284                     GNUNET_YES,
285                     &peer_response_processor,
286                     pal_ctx));
287 }
288
289 /**
290  * Send our subscription request to the service.
291  *
292  * @param val_ctx our context
293  */
294 static void
295 send_val_mon_request (struct GNUNET_TRANSPORT_ValidationMonitoringContext *val_ctx)
296 {
297   struct ValidationMonitorMessage msg;
298
299   msg.header.size = htons (sizeof (struct ValidationMonitorMessage));
300   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_REQUEST);
301   msg.one_shot = htonl (val_ctx->one_shot);
302   msg.peer = val_ctx->peer;
303   GNUNET_assert (GNUNET_OK ==
304                  GNUNET_CLIENT_transmit_and_get_response (val_ctx->client,
305                     &msg.header,
306                     GNUNET_TIME_absolute_get_remaining (val_ctx->timeout),
307                     GNUNET_YES,
308                     &val_response_processor,
309                     val_ctx));
310 }
311
312 /**
313  * Task run to re-establish the connection.
314  *
315  * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
316  * @param tc scheduler context, unused
317  */
318 static void
319 do_peer_connect (void *cls,
320            const struct GNUNET_SCHEDULER_TaskContext *tc)
321 {
322   struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
323
324   pal_ctx->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
325   pal_ctx->client = GNUNET_CLIENT_connect ("transport", pal_ctx->cfg);
326   GNUNET_assert (NULL != pal_ctx->client);
327   send_peer_mon_request (pal_ctx);
328 }
329
330
331 /**
332  * Cut the existing connection and reconnect.
333  *
334  * @param pal_ctx our context
335  */
336 static void
337 reconnect_peer_ctx (struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx)
338 {
339   GNUNET_assert (GNUNET_NO == pal_ctx->one_shot);
340   GNUNET_CLIENT_disconnect (pal_ctx->client);
341   pal_ctx->client = NULL;
342   pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
343                GNUNET_TRANSPORT_PS_NOT_CONNECTED,
344                GNUNET_TIME_UNIT_ZERO_ABS);
345   pal_ctx->backoff = GNUNET_TIME_STD_BACKOFF (pal_ctx->backoff);
346   pal_ctx->reconnect_task = GNUNET_SCHEDULER_add_delayed (pal_ctx->backoff,
347                                                           &do_peer_connect,
348                                                           pal_ctx);
349 }
350
351
352 /**
353  * Task run to re-establish the connection.
354  *
355  * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
356  * @param tc scheduler context, unused
357  */
358 static void
359 do_val_connect (void *cls,
360            const struct GNUNET_SCHEDULER_TaskContext *tc)
361 {
362   struct GNUNET_TRANSPORT_ValidationMonitoringContext *val_ctx = cls;
363
364   val_ctx->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
365   val_ctx->client = GNUNET_CLIENT_connect ("transport", val_ctx->cfg);
366   GNUNET_assert (NULL != val_ctx->client);
367   send_val_mon_request (val_ctx);
368 }
369
370 /**
371  * Cut the existing connection and reconnect.
372  *
373  * @param val_ctx our context
374  */
375 static void
376 reconnect_val_ctx (struct GNUNET_TRANSPORT_ValidationMonitoringContext *val_ctx)
377 {
378   GNUNET_assert (GNUNET_NO == val_ctx->one_shot);
379   GNUNET_CLIENT_disconnect (val_ctx->client);
380   val_ctx->client = NULL;
381   /* notify clients about (re)connect */
382   val_ctx->cb (val_ctx->cb_cls, NULL, NULL,
383                GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TIME_UNIT_ZERO_ABS,
384                GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_VS_TIMEOUT);
385   val_ctx->backoff = GNUNET_TIME_STD_BACKOFF (val_ctx->backoff);
386   val_ctx->reconnect_task = GNUNET_SCHEDULER_add_delayed (val_ctx->backoff,
387                                                           &do_val_connect,
388                                                           val_ctx);
389 }
390
391
392 /**
393  * Function called with responses from the service.
394  *
395  * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
396  * @param msg NULL on timeout or error, otherwise presumably a
397  *        message with the human-readable address
398  */
399 static void
400 val_response_processor (void *cls,
401                         const struct GNUNET_MessageHeader *msg)
402 {
403   struct GNUNET_TRANSPORT_ValidationMonitoringContext *val_ctx = cls;
404   struct ValidationIterateResponseMessage *vr_msg;
405   struct GNUNET_HELLO_Address *address;
406   const char *addr;
407   const char *transport_name;
408   size_t size;
409   size_t tlen;
410   size_t alen;
411
412   if (NULL == msg)
413   {
414     if (val_ctx->one_shot)
415     {
416       /* Disconnect */
417       val_ctx->cb (val_ctx->cb_cls, NULL, NULL,
418           GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TIME_UNIT_ZERO_ABS,
419           GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_VS_TIMEOUT);
420       GNUNET_TRANSPORT_monitor_validation_entries_cancel (val_ctx);
421     }
422     else
423     {
424       reconnect_val_ctx (val_ctx);
425     }
426     return;
427   }
428   size = ntohs (msg->size);
429   GNUNET_break (ntohs (msg->type) ==
430       GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE);
431
432   if (size == sizeof (struct GNUNET_MessageHeader))
433   {
434     /* Done! */
435     if (val_ctx->one_shot)
436     {
437       val_ctx->cb (val_ctx->cb_cls, NULL, NULL,
438           GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TIME_UNIT_ZERO_ABS,
439           GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_VS_NONE);
440       GNUNET_TRANSPORT_monitor_validation_entries_cancel (val_ctx);
441     }
442     else
443     {
444       reconnect_val_ctx (val_ctx);
445     }
446     return;
447   }
448
449   if ((size < sizeof (struct ValidationIterateResponseMessage)) ||
450       (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE))
451   {
452     GNUNET_break (0);
453     if (val_ctx->one_shot)
454     {
455       val_ctx->cb (val_ctx->cb_cls, NULL, NULL,
456           GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TIME_UNIT_ZERO_ABS,
457           GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_VS_NONE);
458       GNUNET_TRANSPORT_monitor_validation_entries_cancel (val_ctx);
459     }
460     else
461     {
462       reconnect_val_ctx (val_ctx);
463     }
464     return;
465   }
466
467   vr_msg = (struct ValidationIterateResponseMessage *) msg;
468   tlen = ntohl (vr_msg->pluginlen);
469   alen = ntohl (vr_msg->addrlen);
470
471   if (size != sizeof (struct ValidationIterateResponseMessage) + tlen + alen)
472   {
473     GNUNET_break (0);
474     if (val_ctx->one_shot)
475     {
476       val_ctx->cb (val_ctx->cb_cls, NULL, NULL,
477           GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TIME_UNIT_ZERO_ABS,
478           GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_VS_NONE);
479       GNUNET_TRANSPORT_monitor_validation_entries_cancel (val_ctx);
480     }
481     else
482     {
483       reconnect_val_ctx (val_ctx);
484     }
485     return;
486   }
487   if ( (0 == tlen) && (0 == alen) )
488   {
489     GNUNET_break (0);
490     if (val_ctx->one_shot)
491     {
492       val_ctx->cb (val_ctx->cb_cls, NULL, NULL,
493           GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TIME_UNIT_ZERO_ABS,
494           GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_VS_NONE);
495       GNUNET_TRANSPORT_monitor_validation_entries_cancel (val_ctx);
496     }
497     else
498     {
499       reconnect_val_ctx (val_ctx);
500     }
501     return;
502   }
503   else
504   {
505     if (0 == tlen)
506     {
507       GNUNET_break (0); /* This must not happen: address without plugin */
508       return;
509     }
510     addr = (const char *) &vr_msg[1];
511     transport_name = &addr[alen];
512
513     if (transport_name[tlen - 1] != '\0')
514     {
515       /* Corrupt plugin name */
516       GNUNET_break (0);
517       if (val_ctx->one_shot)
518       {
519         val_ctx->cb (val_ctx->cb_cls, NULL, NULL,
520             GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TIME_UNIT_ZERO_ABS,
521             GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_VS_NONE);
522         GNUNET_TRANSPORT_monitor_validation_entries_cancel (val_ctx);
523       }
524       else
525       {
526         reconnect_val_ctx (val_ctx);
527       }
528       return;
529     }
530
531     /* notify client */
532     address = GNUNET_HELLO_address_allocate (&vr_msg->peer,
533         transport_name, addr, alen, ntohl(vr_msg->local_address_info));
534     val_ctx->cb (val_ctx->cb_cls, &vr_msg->peer, address,
535         GNUNET_TIME_absolute_ntoh(vr_msg->last_validation),
536         GNUNET_TIME_absolute_ntoh(vr_msg->valid_until),
537         GNUNET_TIME_absolute_ntoh(vr_msg->next_validation),
538         ntohl(vr_msg->state));
539     GNUNET_HELLO_address_free (address);
540   }
541   /* expect more replies */
542   GNUNET_CLIENT_receive (val_ctx->client,
543                          &val_response_processor,
544                          val_ctx,
545                          GNUNET_TIME_absolute_get_remaining (val_ctx->timeout));
546 }
547
548
549 /**
550  * Function called with responses from the service.
551  *
552  * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
553  * @param msg NULL on timeout or error, otherwise presumably a
554  *        message with the human-readable address
555  */
556 static void
557 peer_response_processor (void *cls,
558                          const struct GNUNET_MessageHeader *msg)
559 {
560   struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
561   struct PeerIterateResponseMessage *pir_msg;
562   struct GNUNET_HELLO_Address *address;
563   const char *addr;
564   const char *transport_name;
565   uint16_t size;
566   size_t alen;
567   size_t tlen;
568
569   if (NULL == msg)
570   {
571     if (pal_ctx->one_shot)
572     {
573       /* Disconnect */
574       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
575           GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
576       GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
577     }
578     else
579     {
580       reconnect_peer_ctx (pal_ctx);
581     }
582     return;
583   }
584   size = ntohs (msg->size);
585   GNUNET_break (ntohs (msg->type) ==
586       GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
587   if (size == sizeof (struct GNUNET_MessageHeader))
588   {
589     /* Done! */
590     if (pal_ctx->one_shot)
591     {
592       /* iteration finished */
593       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
594           GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
595       GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
596     }
597     else
598     {
599       reconnect_peer_ctx (pal_ctx);
600     }
601     return;
602   }
603
604   if ((size < sizeof (struct PeerIterateResponseMessage)) ||
605       (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE))
606   {
607     GNUNET_break (0);
608     if (pal_ctx->one_shot)
609     {
610       /* iteration finished (with error) */
611       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
612           GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
613       GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
614     }
615     else
616     {
617       reconnect_peer_ctx (pal_ctx);
618     }
619     return;
620   }
621
622   pir_msg = (struct PeerIterateResponseMessage *) msg;
623   tlen = ntohl (pir_msg->pluginlen);
624   alen = ntohl (pir_msg->addrlen);
625
626   if (size != sizeof (struct PeerIterateResponseMessage) + tlen + alen)
627   {
628     GNUNET_break (0);
629     if (pal_ctx->one_shot)
630     {
631       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
632           GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
633       GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
634     }
635     else
636     {
637       reconnect_peer_ctx (pal_ctx);
638     }
639     return;
640   }
641
642   if ( (0 == tlen) && (0 == alen) )
643   {
644     /* No address available */
645     pal_ctx->cb (pal_ctx->cb_cls, &pir_msg->peer, NULL,
646         ntohl(pir_msg->state),
647         GNUNET_TIME_absolute_ntoh (pir_msg->state_timeout));
648   }
649   else
650   {
651     if (0 == tlen)
652     {
653       GNUNET_break (0); /* This must not happen: address without plugin */
654       return;
655     }
656     addr = (const char *) &pir_msg[1];
657     transport_name = &addr[alen];
658
659     if (transport_name[tlen - 1] != '\0')
660     {
661       /* Corrupt plugin name */
662       GNUNET_break (0);
663       if (pal_ctx->one_shot)
664       {
665         pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
666             GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
667         GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
668       }
669       else
670       {
671         reconnect_peer_ctx (pal_ctx);
672       }
673       return;
674     }
675
676     /* notify client */
677     address = GNUNET_HELLO_address_allocate (&pir_msg->peer,
678         transport_name, addr, alen, ntohl(pir_msg->local_address_info));
679     pal_ctx->cb (pal_ctx->cb_cls, &pir_msg->peer, address,
680         ntohl(pir_msg->state),
681         GNUNET_TIME_absolute_ntoh (pir_msg->state_timeout));
682     GNUNET_HELLO_address_free (address);
683
684   }
685
686   /* expect more replies */
687   GNUNET_CLIENT_receive (pal_ctx->client, &peer_response_processor,
688                          pal_ctx,
689                          GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout));
690 }
691
692
693 /**
694  * Return information about a specific peer or all peers currently known to
695  * transport service once or in monitoring mode. To obtain information about
696  * a specific peer, a peer identity can be passed. To obtain information about
697  * all peers currently known to transport service, NULL can be passed as peer
698  * identity.
699  *
700  * For each peer, the callback is called with information about the address used
701  * to communicate with this peer, the state this peer is currently in and the
702  * the current timeout for this state.
703  *
704  * Upon completion, the 'GNUNET_TRANSPORT_PeerIterateCallback' is called one
705  * more time with 'NULL'. After this, the operation must no longer be
706  * explicitly canceled.
707  *
708  * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
709  * the peer_callback!
710  *
711  * @param cfg configuration to use
712  * @param peer a specific peer identity to obtain information for,
713  *      NULL for all peers
714  * @param one_shot #GNUNET_YES to return the current state and then end (with NULL+NULL),
715  *                 #GNUNET_NO to monitor peers continuously
716  * @param timeout how long is the lookup allowed to take at most
717  * @param peer_callback function to call with the results
718  * @param peer_callback_cls closure for @a peer_address_callback
719  */
720 struct GNUNET_TRANSPORT_PeerMonitoringContext *
721 GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
722                                 const struct GNUNET_PeerIdentity *peer,
723                                 int one_shot,
724                                 struct GNUNET_TIME_Relative timeout,
725                                 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
726                                 void *peer_callback_cls)
727 {
728   struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx;
729   struct GNUNET_CLIENT_Connection *client;
730
731   client = GNUNET_CLIENT_connect ("transport", cfg);
732   if (client == NULL)
733     return NULL;
734   if (GNUNET_YES != one_shot)
735     timeout = GNUNET_TIME_UNIT_FOREVER_REL;
736   pal_ctx = GNUNET_new (struct GNUNET_TRANSPORT_PeerMonitoringContext);
737   pal_ctx->cb = peer_callback;
738   pal_ctx->cb_cls = peer_callback_cls;
739   pal_ctx->cfg = cfg;
740   pal_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
741   if (NULL != peer)
742     pal_ctx->peer = *peer;
743   pal_ctx->one_shot = one_shot;
744   pal_ctx->client = client;
745   send_peer_mon_request (pal_ctx);
746
747   return pal_ctx;
748 }
749
750
751 /**
752  * Cancel request to monitor peers
753  *
754  * @param pic handle for the request to cancel
755  */
756 void
757 GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic)
758 {
759   if (NULL != pic->client)
760   {
761     GNUNET_CLIENT_disconnect (pic->client);
762     pic->client = NULL;
763   }
764   if (GNUNET_SCHEDULER_NO_TASK != pic->reconnect_task)
765   {
766     GNUNET_SCHEDULER_cancel (pic->reconnect_task);
767     pic->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
768   }
769   GNUNET_free (pic);
770 }
771
772
773 /**
774  * Return information about pending address validation operations for a specific
775  * or all peers
776  *
777  * @param cfg configuration to use
778  * @param peer a specific peer identity to obtain validation entries for,
779  *      NULL for all peers
780  * @param one_shot GNUNET_YES to return all entries and then end (with NULL+NULL),
781  *                 GNUNET_NO to monitor validation entries continuously
782  * @param timeout how long is the lookup allowed to take at most
783  * @param validation_callback function to call with the results
784  * @param validation_callback_cls closure for peer_address_callback
785  */
786 struct GNUNET_TRANSPORT_ValidationMonitoringContext *
787 GNUNET_TRANSPORT_monitor_validation_entries (const struct
788                                 GNUNET_CONFIGURATION_Handle *cfg,
789                                 const struct GNUNET_PeerIdentity *peer,
790                                 int one_shot,
791                                 struct GNUNET_TIME_Relative timeout,
792                                 GNUNET_TRANSPORT_ValidationIterateCallback validation_callback,
793                                 void *validation_callback_cls)
794 {
795   struct GNUNET_TRANSPORT_ValidationMonitoringContext *val_ctx;
796   struct GNUNET_CLIENT_Connection *client;
797
798   client = GNUNET_CLIENT_connect ("transport", cfg);
799   if (client == NULL)
800     return NULL;
801   if (GNUNET_YES != one_shot)
802     timeout = GNUNET_TIME_UNIT_FOREVER_REL;
803   val_ctx = GNUNET_new (struct GNUNET_TRANSPORT_ValidationMonitoringContext);
804   val_ctx->cb = validation_callback;
805   val_ctx->cb_cls = validation_callback_cls;
806   val_ctx->cfg = cfg;
807   val_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
808   if (NULL != peer)
809     val_ctx->peer = *peer;
810   val_ctx->one_shot = one_shot;
811   val_ctx->client = client;
812   send_val_mon_request (val_ctx);
813
814   return val_ctx;
815 }
816
817
818 /**
819  * Return information about all current pending validation operations
820  *
821  * @param vic handle for the request to cancel
822  */
823 void
824 GNUNET_TRANSPORT_monitor_validation_entries_cancel (struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic)
825 {
826   if (NULL != vic->client)
827   {
828     GNUNET_CLIENT_disconnect (vic->client);
829     vic->client = NULL;
830   }
831   if (GNUNET_SCHEDULER_NO_TASK != vic->reconnect_task)
832   {
833     GNUNET_SCHEDULER_cancel (vic->reconnect_task);
834     vic->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
835   }
836   GNUNET_free (vic);
837 }
838
839
840 /* end of transport_api_monitoring.c */