2 This file is part of GNUnet.
3 Copyright (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file namestore/namestore_api_monitor.c
23 * @brief API to monitor changes in the NAMESTORE
24 * @author Christian Grothoff
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"
39 * Handle for a monitoring activity.
41 struct GNUNET_NAMESTORE_ZoneMonitor
44 * Configuration (to reconnect).
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 * Handle to namestore service.
51 struct GNUNET_CLIENT_Connection *h;
54 * Function to call on events.
56 GNUNET_NAMESTORE_RecordMonitor monitor;
59 * Function called when we've synchronized.
61 GNUNET_NAMESTORE_RecordsSynchronizedCallback sync_cb;
64 * Closure for 'monitor' and 'sync_cb'.
69 * Transmission handle to client.
71 struct GNUNET_CLIENT_TransmitHandle *th;
76 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
79 * Do we first iterate over all existing records?
87 * Send our request to start monitoring to the service.
89 * @param cls the monitor handle
90 * @param size number of bytes available in buf
91 * @param buf where to copy the message to the service
92 * @return number of bytes copied to buf
95 transmit_monitor_message (void *cls,
101 * Reconnect to the namestore service.
103 * @param zm monitor to reconnect
106 reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
109 GNUNET_CLIENT_disconnect (zm->h);
110 zm->monitor (zm->cls,
113 GNUNET_assert (NULL != (zm->h = GNUNET_CLIENT_connect ("namestore", zm->cfg)));
114 zm->th = GNUNET_CLIENT_notify_transmit_ready (zm->h,
115 sizeof (struct ZoneMonitorStartMessage),
116 GNUNET_TIME_UNIT_FOREVER_REL,
118 &transmit_monitor_message,
124 * We've received a notification about a change to our zone.
125 * Forward to monitor callback.
127 * @param cls the zone monitor handle
128 * @param msg the message from the service.
131 handle_updates (void *cls,
132 const struct GNUNET_MessageHeader *msg)
134 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
135 const struct RecordResultMessage *lrm;
141 const char *name_tmp;
142 const char *rd_ser_tmp;
149 if ( (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader)) &&
150 (GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC == ntohs (msg->type) ) )
152 GNUNET_CLIENT_receive (zm->h,
155 GNUNET_TIME_UNIT_FOREVER_REL);
156 if (NULL != zm->sync_cb)
157 zm->sync_cb (zm->cls);
160 if ( (ntohs (msg->size) < sizeof (struct RecordResultMessage)) ||
161 (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT != ntohs (msg->type) ) )
167 lrm = (const struct RecordResultMessage *) msg;
168 lrm_len = ntohs (lrm->gns_header.header.size);
169 rd_len = ntohs (lrm->rd_len);
170 rd_count = ntohs (lrm->rd_count);
171 name_len = ntohs (lrm->name_len);
172 exp_lrm_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
173 if (lrm_len != exp_lrm_len)
185 name_tmp = (const char *) &lrm[1];
186 if ((name_tmp[name_len -1] != '\0') || (name_len > MAX_NAME_LEN))
192 rd_ser_tmp = (const char *) &name_tmp[name_len];
194 struct GNUNET_GNSRECORD_Data rd[rd_count];
196 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize (rd_len, rd_ser_tmp, rd_count, rd))
202 GNUNET_CLIENT_receive (zm->h,
205 GNUNET_TIME_UNIT_FOREVER_REL);
206 zm->monitor (zm->cls,
215 * Send our request to start monitoring to the service.
217 * @param cls the monitor handle
218 * @param size number of bytes available in buf
219 * @param buf where to copy the message to the service
220 * @return number of bytes copied to buf
223 transmit_monitor_message (void *cls,
227 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
228 struct ZoneMonitorStartMessage sm;
231 if (size < sizeof (struct ZoneMonitorStartMessage))
236 sm.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START);
237 sm.header.size = htons (sizeof (struct ZoneMonitorStartMessage));
238 sm.iterate_first = htonl (zm->iterate_first);
240 memcpy (buf, &sm, sizeof (sm));
241 GNUNET_CLIENT_receive (zm->h,
244 GNUNET_TIME_UNIT_FOREVER_REL);
250 * Begin monitoring a zone for changes. If @a iterate_first is set,
251 * we Will first call the @a monitor function on all existing records
252 * in the selected zone(s). In any case, we will call @a sync and
253 * afterwards call @a monitor whenever a record changes.
255 * @param cfg configuration to use to connect to namestore
256 * @param zone zone to monitor
257 * @param iterate_first #GNUNET_YES to first iterate over all existing records,
258 * #GNUNET_NO to only return changes that happen from now on
259 * @param monitor function to call on zone changes
260 * @param sync_cb function called when we're in sync with the namestore
261 * @param cls closure for @a monitor and @a sync_cb
262 * @return handle to stop monitoring
264 struct GNUNET_NAMESTORE_ZoneMonitor *
265 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
266 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
268 GNUNET_NAMESTORE_RecordMonitor monitor,
269 GNUNET_NAMESTORE_RecordsSynchronizedCallback sync_cb,
272 struct GNUNET_NAMESTORE_ZoneMonitor *zm;
273 struct GNUNET_CLIENT_Connection *client;
275 if (NULL == (client = GNUNET_CLIENT_connect ("namestore", cfg)))
277 zm = GNUNET_new (struct GNUNET_NAMESTORE_ZoneMonitor);
282 zm->iterate_first = iterate_first;
283 zm->monitor = monitor;
284 zm->sync_cb = sync_cb;
286 zm->th = GNUNET_CLIENT_notify_transmit_ready (zm->h,
287 sizeof (struct ZoneMonitorStartMessage),
288 GNUNET_TIME_UNIT_FOREVER_REL,
290 &transmit_monitor_message,
297 * Stop monitoring a zone for changes.
299 * @param zm handle to the monitor activity to stop
302 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
306 GNUNET_CLIENT_notify_transmit_ready_cancel (zm->th);
309 GNUNET_CLIENT_disconnect (zm->h);
313 /* end of namestore_api_monitor.c */