paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / namestore / namestore_api_monitor.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013, 2016, 2018 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero 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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file namestore/namestore_api_monitor.c
20  * @brief API to monitor changes in the NAMESTORE
21  * @author Christian Grothoff
22  */
23
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_crypto_lib.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_dnsparser_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_signatures.h"
31 #include "gnunet_namestore_service.h"
32 #include "namestore.h"
33
34
35 /**
36  * Handle for a monitoring activity.
37  */
38 struct GNUNET_NAMESTORE_ZoneMonitor
39 {
40   /**
41    * Configuration (to reconnect).
42    */
43   const struct GNUNET_CONFIGURATION_Handle *cfg;
44
45   /**
46    * Handle to namestore service.
47    */
48   struct GNUNET_MQ_Handle *mq;
49
50   /**
51    * Function to call on errors.
52    */
53   GNUNET_SCHEDULER_TaskCallback error_cb;
54
55   /**
56    * Closure for @e error_cb.
57    */
58   void *error_cb_cls;
59
60   /**
61    * Function to call on events.
62    */
63   GNUNET_NAMESTORE_RecordMonitor monitor;
64
65   /**
66    * Closure for @e monitor.
67    */
68   void *monitor_cls;
69
70   /**
71    * Function called when we've synchronized.
72    */
73   GNUNET_SCHEDULER_TaskCallback sync_cb;
74
75   /**
76    * Closure for @e sync_cb.
77    */
78   void *sync_cb_cls;
79
80   /**
81    * Monitored zone.
82    */
83   struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
84
85   /**
86    * Do we first iterate over all existing records?
87    */
88   int iterate_first;
89
90 };
91
92
93 /**
94  * Reconnect to the namestore service.
95  *
96  * @param zm monitor to reconnect
97  */
98 static void
99 reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm);
100
101
102 /**
103  * Handle SYNC message from the namestore service.
104  *
105  * @param cls the monitor
106  * @param msg the sync message
107  */
108 static void
109 handle_sync (void *cls,
110              const struct GNUNET_MessageHeader *msg)
111 {
112   struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
113
114   (void) cls;
115   (void) msg;
116   if (NULL != zm->sync_cb)
117     zm->sync_cb (zm->sync_cb_cls);
118 }
119
120
121 /**
122  * We've received a notification about a change to our zone.
123  * Check that it is well-formed.
124  *
125  * @param cls the zone monitor handle
126  * @param lrm the message from the service.
127  */
128 static int
129 check_result (void *cls,
130               const struct RecordResultMessage *lrm)
131 {
132   size_t lrm_len;
133   size_t exp_lrm_len;
134   size_t name_len;
135   size_t rd_len;
136   unsigned rd_count;
137   const char *name_tmp;
138   const char *rd_ser_tmp;
139
140   (void) cls;
141   lrm_len = ntohs (lrm->gns_header.header.size);
142   rd_len = ntohs (lrm->rd_len);
143   rd_count = ntohs (lrm->rd_count);
144   name_len = ntohs (lrm->name_len);
145   if (name_len > MAX_NAME_LEN)
146   {
147     GNUNET_break (0);
148     return GNUNET_SYSERR;
149   }
150   exp_lrm_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
151   if (lrm_len != exp_lrm_len)
152   {
153     GNUNET_break (0);
154     return GNUNET_SYSERR;
155   }
156   if (0 == name_len)
157   {
158     GNUNET_break (0);
159     return GNUNET_SYSERR;
160   }
161   name_tmp = (const char *) &lrm[1];
162   if (name_tmp[name_len -1] != '\0')
163   {
164     GNUNET_break (0);
165     return GNUNET_SYSERR;
166   }
167   rd_ser_tmp = (const char *) &name_tmp[name_len];
168   {
169     struct GNUNET_GNSRECORD_Data rd[rd_count];
170
171     if (GNUNET_OK !=
172         GNUNET_GNSRECORD_records_deserialize (rd_len,
173                                               rd_ser_tmp,
174                                               rd_count,
175                                               rd))
176     {
177       GNUNET_break (0);
178       return GNUNET_SYSERR;
179     }
180   }
181   return GNUNET_OK;
182 }
183
184
185 /**
186  * We've received a notification about a change to our zone.
187  * Forward to monitor callback.
188  *
189  * @param cls the zone monitor handle
190  * @param lrm the message from the service.
191  */
192 static void
193 handle_result (void *cls,
194                const struct RecordResultMessage *lrm)
195 {
196   struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
197   size_t name_len;
198   size_t rd_len;
199   unsigned rd_count;
200   const char *name_tmp;
201   const char *rd_ser_tmp;
202
203   rd_len = ntohs (lrm->rd_len);
204   rd_count = ntohs (lrm->rd_count);
205   name_len = ntohs (lrm->name_len);
206   name_tmp = (const char *) &lrm[1];
207   rd_ser_tmp = (const char *) &name_tmp[name_len];
208   {
209     struct GNUNET_GNSRECORD_Data rd[rd_count];
210
211     GNUNET_assert (GNUNET_OK ==
212                    GNUNET_GNSRECORD_records_deserialize (rd_len,
213                                                          rd_ser_tmp,
214                                                          rd_count,
215                                                          rd));
216     zm->monitor (zm->monitor_cls,
217                  &lrm->private_key,
218                  name_tmp,
219                  rd_count,
220                  rd);
221   }
222 }
223
224
225 /**
226  * Generic error handler, called with the appropriate error code and
227  * the same closure specified at the creation of the message queue.
228  * Not every message queue implementation supports an error handler.
229  *
230  * @param cls closure with the `struct GNUNET_NAMESTORE_ZoneMonitor *`
231  * @param error error code
232  */
233 static void
234 mq_error_handler (void *cls,
235                   enum GNUNET_MQ_Error error)
236 {
237   struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
238
239   (void) error;
240   reconnect (zm);
241 }
242
243
244 /**
245  * Reconnect to the namestore service.
246  *
247  * @param zm monitor to reconnect
248  */
249 static void
250 reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
251 {
252   struct GNUNET_MQ_MessageHandler handlers[] = {
253     GNUNET_MQ_hd_fixed_size (sync,
254                              GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC,
255                              struct GNUNET_MessageHeader,
256                              zm),
257     GNUNET_MQ_hd_var_size (result,
258                            GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT,
259                            struct RecordResultMessage,
260                            zm),
261     GNUNET_MQ_handler_end ()
262   };
263   struct GNUNET_MQ_Envelope *env;
264   struct ZoneMonitorStartMessage *sm;
265
266   if (NULL != zm->mq)
267   {
268     GNUNET_MQ_destroy (zm->mq);
269     zm->error_cb (zm->error_cb_cls);
270   }
271   zm->mq = GNUNET_CLIENT_connect (zm->cfg,
272                                   "namestore",
273                                   handlers,
274                                   &mq_error_handler,
275                                   zm);
276   if (NULL == zm->mq)
277     return;
278   env = GNUNET_MQ_msg (sm,
279                        GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START);
280   sm->iterate_first = htonl (zm->iterate_first);
281   sm->zone = zm->zone;
282   GNUNET_MQ_send (zm->mq,
283                   env);
284 }
285
286
287
288 /**
289  * Begin monitoring a zone for changes.  If @a iterate_first is set,
290  * we Will first call the @a monitor function on all existing records
291  * in the selected zone(s).  In any case, we will call @a sync and
292  * afterwards call @a monitor whenever a record changes.
293  *
294  * @param cfg configuration to use to connect to namestore
295  * @param zone zone to monitor
296  * @param iterate_first #GNUNET_YES to first iterate over all existing records,
297  *                      #GNUNET_NO to only return changes that happen from now on
298  * @param error_cb function to call on error (i.e. disconnect); note that
299  *         unlike the other error callbacks in this API, a call to this
300  *         function does NOT destroy the monitor handle, it merely signals
301  *         that monitoring is down. You need to still explicitly call
302  *         #GNUNET_NAMESTORE_zone_monitor_stop().
303  * @param error_cb_cls closure for @a error_cb
304  * @param monitor function to call on zone changes
305  * @param monitor_cls closure for @a monitor
306  * @param sync_cb function called when we're in sync with the namestore
307  * @param cls closure for @a sync_cb
308  * @return handle to stop monitoring
309  */
310 struct GNUNET_NAMESTORE_ZoneMonitor *
311 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
312                                      const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
313                                      int iterate_first,
314                                      GNUNET_SCHEDULER_TaskCallback error_cb,
315                                      void *error_cb_cls,
316                                      GNUNET_NAMESTORE_RecordMonitor monitor,
317                                      void *monitor_cls,
318                                      GNUNET_SCHEDULER_TaskCallback sync_cb,
319                                      void *sync_cb_cls)
320 {
321   struct GNUNET_NAMESTORE_ZoneMonitor *zm;
322
323   zm = GNUNET_new (struct GNUNET_NAMESTORE_ZoneMonitor);
324   if (NULL != zone)
325     zm->zone = *zone;
326   zm->iterate_first = iterate_first;
327   zm->error_cb = error_cb;
328   zm->error_cb_cls = error_cb_cls;
329   zm->monitor = monitor;
330   zm->monitor_cls = monitor_cls;
331   zm->sync_cb = sync_cb;
332   zm->sync_cb_cls = sync_cb_cls;
333   zm->cfg = cfg;
334   reconnect (zm);
335   if (NULL == zm->mq)
336   {
337     GNUNET_free (zm);
338     return NULL;
339   }
340   return zm;
341 }
342
343
344 /**
345  * Calls the monitor processor specified in #GNUNET_NAMESTORE_zone_monitor_start
346  * for the next record(s).  This function is used to allow clients that merely
347  * monitor the NAMESTORE to still throttle namestore operations, so we can be
348  * sure that the monitors can keep up.
349  *
350  * Note that #GNUNET_NAMESTORE_records_store() only waits for this
351  * call if the previous limit set by the client was already reached.
352  * Thus, by using a @a limit greater than 1, monitors basically enable
353  * a queue of notifications to be processed asynchronously with some
354  * delay.  Note that even with a limit of 1 the
355  * #GNUNET_NAMESTORE_records_store() function will run asynchronously
356  * and the continuation may be invoked before the monitors completed
357  * (or even started) processing the notification.  Thus, monitors will
358  * only closely track the current state of the namestore, but not
359  * be involved in the transactions.
360  *
361  * @param zm the monitor
362  * @param limit number of records to return to the iterator in one shot
363  *        (before #GNUNET_NAMESTORE_zone_monitor_next is to be called again)
364  */
365 void
366 GNUNET_NAMESTORE_zone_monitor_next (struct GNUNET_NAMESTORE_ZoneMonitor *zm,
367                                     uint64_t limit)
368 {
369   struct GNUNET_MQ_Envelope *env;
370   struct ZoneMonitorNextMessage *nm;
371
372   env = GNUNET_MQ_msg (nm,
373                        GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT);
374   nm->limit = GNUNET_htonll (limit);
375   GNUNET_MQ_send (zm->mq,
376                   env);
377 }
378
379
380 /**
381  * Stop monitoring a zone for changes.
382  *
383  * @param zm handle to the monitor activity to stop
384  */
385 void
386 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
387 {
388   if (NULL != zm->mq)
389   {
390     GNUNET_MQ_destroy (zm->mq);
391     zm->mq = NULL;
392   }
393   GNUNET_free (zm);
394 }
395
396 /* end of namestore_api_monitor.c */