2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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_EccPrivateKey zone;
82 * Send our request to start monitoring to the service.
84 * @param cls the monitor handle
85 * @param size number of bytes available in buf
86 * @param buf where to copy the message to the service
87 * @return number of bytes copied to buf
90 transmit_monitor_message (void *cls,
96 * Reconnect to the namestore service.
98 * @param zm monitor to reconnect
101 reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
104 GNUNET_CLIENT_disconnect (zm->h);
105 zm->monitor (zm->cls,
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,
113 &transmit_monitor_message,
119 * We've received a notification about a change to our zone.
120 * Forward to monitor callback.
122 * @param cls the zone monitor handle
123 * @param msg the message from the service.
126 handle_updates (void *cls,
127 const struct GNUNET_MessageHeader *msg)
129 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
130 const struct RecordResultMessage *lrm;
136 const char *name_tmp;
137 const char *rd_ser_tmp;
144 if ( (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader)) &&
145 (GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC == ntohs (msg->type) ) )
147 GNUNET_CLIENT_receive (zm->h,
150 GNUNET_TIME_UNIT_FOREVER_REL);
151 if (NULL != zm->sync_cb)
152 zm->sync_cb (zm->cls);
155 if ( (ntohs (msg->size) < sizeof (struct RecordResultMessage)) ||
156 (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT != ntohs (msg->type) ) )
162 lrm = (const struct RecordResultMessage *) msg;
163 lrm_len = ntohs (lrm->gns_header.header.size);
164 rd_len = ntohs (lrm->rd_len);
165 rd_count = ntohs (lrm->rd_count);
166 name_len = ntohs (lrm->name_len);
167 exp_lrm_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
168 if (lrm_len != exp_lrm_len)
180 name_tmp = (const char *) &lrm[1];
181 if ((name_tmp[name_len -1] != '\0') || (name_len > MAX_NAME_LEN))
187 rd_ser_tmp = (const char *) &name_tmp[name_len];
189 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
191 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize (rd_len, rd_ser_tmp, rd_count, rd))
197 GNUNET_CLIENT_receive (zm->h,
200 GNUNET_TIME_UNIT_FOREVER_REL);
201 zm->monitor (zm->cls,
210 * Send our request to start monitoring to the service.
212 * @param cls the monitor handle
213 * @param size number of bytes available in buf
214 * @param buf where to copy the message to the service
215 * @return number of bytes copied to buf
218 transmit_monitor_message (void *cls,
222 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
223 struct ZoneMonitorStartMessage sm;
226 if (size < sizeof (struct ZoneMonitorStartMessage))
231 sm.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START);
232 sm.gns_header.header.size = htons (sizeof (struct ZoneMonitorStartMessage));
233 sm.gns_header.r_id = htonl (0);
235 memcpy (buf, &sm, sizeof (sm));
236 GNUNET_CLIENT_receive (zm->h,
239 GNUNET_TIME_UNIT_FOREVER_REL);
245 * Begin monitoring a zone for changes. Will first call the 'monitor' function
246 * on all existing records in the selected zone(s) and then call it whenever
249 * @param cfg configuration to use to connect to namestore
250 * @param zone zone to monitor
251 * @param monitor function to call on zone changes
252 * @param sync_cb function called when we're in sync with the namestore
253 * @param cls closure for 'monitor' and 'sync_cb'
254 * @return handle to stop monitoring
256 struct GNUNET_NAMESTORE_ZoneMonitor *
257 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
258 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
259 GNUNET_NAMESTORE_RecordMonitor monitor,
260 GNUNET_NAMESTORE_RecordsSynchronizedCallback sync_cb,
263 struct GNUNET_NAMESTORE_ZoneMonitor *zm;
264 struct GNUNET_CLIENT_Connection *client;
268 if (NULL == (client = GNUNET_CLIENT_connect ("namestore", cfg)))
272 zm = GNUNET_new (struct GNUNET_NAMESTORE_ZoneMonitor);
276 zm->monitor = monitor;
277 zm->sync_cb = sync_cb;
279 zm->th = GNUNET_CLIENT_notify_transmit_ready (zm->h,
280 sizeof (struct ZoneMonitorStartMessage),
281 GNUNET_TIME_UNIT_FOREVER_REL,
283 &transmit_monitor_message,
290 * Stop monitoring a zone for changes.
292 * @param zm handle to the monitor activity to stop
295 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm)
299 GNUNET_CLIENT_notify_transmit_ready_cancel (zm->th);
302 GNUNET_CLIENT_disconnect (zm->h);
306 /* end of namestore_api_monitor.c */