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