46d59560ac172b242ed35b54312d4e9a00e96702
[oweals/gnunet.git] / src / core / core_api_monitor_peers.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2014, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15
16 /**
17  * @file core/core_api_monitor_peers.c
18  * @brief implementation of the peer_iterate function
19  * @author Christian Grothoff
20  * @author Nathan Evans
21  */
22 #include "platform.h"
23 #include "gnunet_core_service.h"
24 #include "core.h"
25
26
27 /**
28  * Handle to a CORE monitoring operation.
29  */
30 struct GNUNET_CORE_MonitorHandle
31 {
32
33   /**
34    * Our configuration.
35    */
36   const struct GNUNET_CONFIGURATION_Handle *cfg;
37
38   /**
39    * Our connection to the service.
40    */
41   struct GNUNET_MQ_Handle *mq;
42
43   /**
44    * Function called with the peer.
45    */
46   GNUNET_CORE_MonitorCallback peer_cb;
47
48   /**
49    * Closure for @e peer_cb.
50    */
51   void *peer_cb_cls;
52
53 };
54
55
56 /**
57  * Protocol error, reconnect to CORE service and notify
58  * client.
59  *
60  * @param mh monitoring session to reconnect to CORE
61  */
62 static void
63 reconnect (struct GNUNET_CORE_MonitorHandle *mh);
64
65
66 /**
67  * Generic error handler, called with the appropriate error code and
68  * the same closure specified at the creation of the message queue.
69  * Not every message queue implementation supports an error handler.
70  *
71  * @param cls closure, a `struct GNUNET_CORE_MonitorHandle *`
72  * @param error error code
73  */
74 static void
75 handle_mq_error (void *cls,
76                  enum GNUNET_MQ_Error error)
77 {
78   struct GNUNET_CORE_MonitorHandle *mh = cls;
79
80   reconnect (mh);
81 }
82
83
84 /**
85  * Receive reply from CORE service with information about a peer.
86  *
87  * @param cls our `struct  GNUNET_CORE_MonitorHandle *`
88  * @param mon_message monitor message
89  */
90 static void
91 handle_receive_info (void *cls,
92                      const struct MonitorNotifyMessage *mon_message)
93 {
94   struct GNUNET_CORE_MonitorHandle *mh = cls;
95
96   mh->peer_cb (mh->peer_cb_cls,
97                &mon_message->peer,
98                (enum GNUNET_CORE_KxState) ntohl (mon_message->state),
99                GNUNET_TIME_absolute_ntoh (mon_message->timeout));
100 }
101
102
103 /**
104  * Protocol error, reconnect to CORE service and notify
105  * client.
106  *
107  * @param mh monitoring session to reconnect to CORE
108  */
109 static void
110 reconnect (struct GNUNET_CORE_MonitorHandle *mh)
111 {
112   struct GNUNET_MQ_MessageHandler handlers[] = {
113     GNUNET_MQ_hd_fixed_size (receive_info,
114                              GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY,
115                              struct MonitorNotifyMessage,
116                              mh),
117     GNUNET_MQ_handler_end ()
118   };
119   struct GNUNET_MQ_Envelope *env;
120   struct GNUNET_MessageHeader *msg;
121
122   if (NULL != mh->mq)
123     GNUNET_MQ_destroy (mh->mq);
124   /* FIXME: use backoff? */
125   mh->mq = GNUNET_CLIENT_connect (mh->cfg,
126                                   "core",
127                                   handlers,
128                                   &handle_mq_error,
129                                   mh);
130   if (NULL == mh->mq)
131     return;
132   /* notify callback about reconnect */
133   mh->peer_cb (mh->peer_cb_cls,
134                NULL,
135                GNUNET_CORE_KX_CORE_DISCONNECT,
136                GNUNET_TIME_UNIT_FOREVER_ABS);
137   env = GNUNET_MQ_msg (msg,
138                        GNUNET_MESSAGE_TYPE_CORE_MONITOR_PEERS);
139   GNUNET_MQ_send (mh->mq,
140                   env);
141 }
142
143
144 /**
145  * Monitor connectivity and KX status of all peers known to CORE.
146  * Calls @a peer_cb with the current status for each connected peer,
147  * and then once with NULL to indicate that all peers that are
148  * currently active have been handled.  After that, the iteration
149  * continues until it is cancelled.  Normal users of the CORE API are
150  * not expected to use this function.  It is different in that it
151  * truly lists all connections (including those where the KX is in
152  * progress), not just those relevant to the application.  This
153  * function is used by special applications for diagnostics.
154  *
155  * @param cfg configuration handle
156  * @param peer_cb function to call with the peer information
157  * @param peer_cb_cls closure for @a peer_cb
158  * @return NULL on error
159  */
160 struct GNUNET_CORE_MonitorHandle *
161 GNUNET_CORE_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
162                            GNUNET_CORE_MonitorCallback peer_cb,
163                            void *peer_cb_cls)
164 {
165   struct GNUNET_CORE_MonitorHandle *mh;
166
167   GNUNET_assert (NULL != peer_cb);
168   mh = GNUNET_new (struct GNUNET_CORE_MonitorHandle);
169   mh->cfg = cfg;
170   mh->peer_cb = peer_cb;
171   mh->peer_cb_cls = peer_cb_cls;
172   reconnect (mh);
173   if (NULL == mh->mq)
174   {
175     GNUNET_free (mh);
176     return NULL;
177   }
178   return mh;
179 }
180
181
182 /**
183  * Stop monitoring CORE activity.
184  *
185  * @param mh monitor to stop
186  */
187 void
188 GNUNET_CORE_monitor_stop (struct GNUNET_CORE_MonitorHandle *mh)
189 {
190   if (NULL != mh->mq)
191   {
192     GNUNET_MQ_destroy (mh->mq);
193     mh->mq = NULL;
194   }
195   GNUNET_free (mh);
196 }
197
198
199 /* end of core_api_monitor_peers.c */