e2caea8a575428e732d09f24abe2f94dd76c2bb2
[oweals/gnunet.git] / src / namestore / namestore_api_monitor.c
1 /*
2      This file is part of GNUnet.
3      (C) 2013 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 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_CLIENT_Connection *h;
52
53   /**
54    * Function to call on events.
55    */
56   GNUNET_NAMESTORE_RecordMonitor monitor;
57
58   /**
59    * Closure for 'monitor'.
60    */
61   void *monitor_cls;
62
63   /**
64    * Transmission handle to client.
65    */
66   struct GNUNET_CLIENT_TransmitHandle *th;
67
68   /**
69    * Monitored zone.
70    */
71   struct GNUNET_CRYPTO_ShortHashCode zone;
72
73   /**
74    * GNUNET_YES if we monitor all zones, GNUNET_NO if we only monitor 'zone'.
75    */
76   int all_zones;
77 };
78
79
80 /**
81  * Send our request to start monitoring to the service.
82  *
83  * @param cls the monitor handle
84  * @param size number of bytes available in buf
85  * @param buf where to copy the message to the service
86  * @return number of bytes copied to buf
87  */
88 static size_t
89 transmit_monitor_message (void *cls,
90                           size_t size,
91                           void *buf);
92
93
94 /**
95  * Reconnect to the namestore service.
96  *
97  * @param zm monitor to reconnect
98  */
99 static void
100 reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
101 {
102   if (NULL != zm->h)
103     GNUNET_CLIENT_disconnect (zm->h);
104   zm->monitor (zm->monitor_cls,
105                NULL,
106                GNUNET_TIME_UNIT_ZERO_ABS,
107                NULL, 0, NULL, NULL);
108   GNUNET_assert (NULL != (zm->h = GNUNET_CLIENT_connect ("namestore", zm->cfg)));
109   zm->th = GNUNET_CLIENT_notify_transmit_ready (zm->h,
110                                                 sizeof (struct ZoneMonitorStartMessage),
111                                                 GNUNET_TIME_UNIT_FOREVER_REL,
112                                                 GNUNET_YES,
113                                                 &transmit_monitor_message,
114                                                 zm);
115 }
116
117
118 /**
119  * We've received a notification about a change to our zone.
120  * Forward to monitor callback.
121  *
122  * @param cls the zone monitor handle
123  * @param msg the message from the service.
124  */
125 static void
126 handle_updates (void *cls,
127                 const struct GNUNET_MessageHeader *msg)
128 {
129   struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
130
131   if (NULL == msg)
132   {
133     reconnect (zm);
134     return;
135   }
136   // FIXME: parse, validate
137
138   GNUNET_CLIENT_receive (zm->h,
139                          &handle_updates,
140                          zm,
141                          GNUNET_TIME_UNIT_FOREVER_REL);
142   // FIXME: call 'monitor'.
143   // zm->monitor (zm->monitor_cls, ...);
144 }
145
146
147 /**
148  * Send our request to start monitoring to the service.
149  *
150  * @param cls the monitor handle
151  * @param size number of bytes available in buf
152  * @param buf where to copy the message to the service
153  * @return number of bytes copied to buf
154  */
155 static size_t
156 transmit_monitor_message (void *cls,
157                           size_t size,
158                           void *buf)
159 {
160   struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
161   struct ZoneMonitorStartMessage sm;
162
163   if (size < sizeof (struct ZoneMonitorStartMessage))
164   {    
165     reconnect (zm);
166     return 0;
167   }
168  
169   sm.zone = zm->zone;
170   sm.all_zones = htonl (zm->all_zones);
171   memcpy (buf, &sm, sizeof (sm));
172   GNUNET_CLIENT_receive (zm->h,
173                          &handle_updates,
174                          zm,
175                          GNUNET_TIME_UNIT_FOREVER_REL);
176   return sizeof (sm);
177 }
178
179
180 /**
181  * Begin monitoring a zone for changes.  Will first call the 'monitor' function
182  * on all existing records in the selected zone(s) and then call it whenever
183  * a record changes.
184  *
185  * @param cfg configuration to use to connect to namestore
186  * @param zone zone to monitor, NULL for all zones
187  * @param monitor function to call on zone changes
188  * @param monitor_cls closure for 'monitor'
189  * @return handle to stop monitoring
190  */
191 struct GNUNET_NAMESTORE_ZoneMonitor *
192 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
193                                      const struct GNUNET_CRYPTO_ShortHashCode *zone,
194                                      GNUNET_NAMESTORE_RecordMonitor monitor,
195                                      void *monitor_cls)
196 {
197   struct GNUNET_NAMESTORE_ZoneMonitor *zm;
198   struct GNUNET_CLIENT_Connection *client;
199
200   if (NULL == (client = GNUNET_CLIENT_connect ("namestore", cfg)))
201     return NULL; 
202   zm = GNUNET_new (struct GNUNET_NAMESTORE_ZoneMonitor);
203   zm->cfg = cfg;
204   zm->h = client;
205   if (NULL == zone)
206     zm->all_zones = GNUNET_YES;
207   else
208     zm->zone = *zone;
209   zm->monitor = monitor;
210   zm->monitor_cls = monitor_cls;
211   zm->th = GNUNET_CLIENT_notify_transmit_ready (zm->h,
212                                                 sizeof (struct ZoneMonitorStartMessage),
213                                                 GNUNET_TIME_UNIT_FOREVER_REL,
214                                                 GNUNET_YES,
215                                                 &transmit_monitor_message,
216                                                 zm);
217   return zm;
218 }
219
220
221 /**
222  * Stop monitoring a zone for changes.
223  *
224  * @param zm handle to the monitor activity to stop
225  */
226 void
227 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
228 {
229   if (NULL != zm->th)
230   {
231     GNUNET_CLIENT_notify_transmit_ready_cancel (zm->th);
232     zm->th = NULL;
233   }
234   GNUNET_CLIENT_disconnect (zm->h);
235   GNUNET_free (zm);
236 }
237
238 /* end of namestore_api_monitor.c */