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