GNUnet 0.28.1-dev.2-13-g57ceb9dfb
 
Loading...
Searching...
No Matches
peerstore_api_monitor.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2013-2024, 2019 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 */
25#include "platform.h"
26#include "gnunet_util_lib.h"
27#include "gnunet_protocols.h"
28#include "peerstore.h"
29#include "peerstore_common.h"
31
32#define LOG(kind, ...) GNUNET_log_from (kind, "peerstore-monitor-api", \
33 __VA_ARGS__)
128
129static void
130handle_sync (void *cls, const struct GNUNET_MessageHeader *msg)
131{
132 struct GNUNET_PEERSTORE_Monitor *mc = cls;
133
134 if (NULL != mc->sync_cb)
135 mc->sync_cb (mc->sync_cb_cls);
136}
137
138
146static int
147check_result (void *cls, const struct PeerstoreRecordMessage *msg)
148{
149 /* we defer validation to #handle_result */
150 return GNUNET_OK;
151}
152
153
160static void
161handle_result (void *cls, const struct PeerstoreRecordMessage *msg)
162{
163 struct GNUNET_PEERSTORE_Monitor *mc = cls;
165
166 LOG (GNUNET_ERROR_TYPE_DEBUG, "Monitor received RecordMessage\n");
168 if (NULL == record)
169 {
170 mc->callback (mc->callback_cls,
171 NULL,
172 _ ("Received a malformed response from service."));
173 }
174 else
175 {
176 mc->callback (mc->callback_cls, record, NULL);
178 }
179}
180
181
182static void reconnect (struct GNUNET_PEERSTORE_Monitor *mc);
183
184static void
186{
187 struct GNUNET_PEERSTORE_Monitor *mc = cls;
188
189 reconnect (mc);
190}
191
192
193static void
195{
200 mc),
203 struct PeerstoreRecordMessage, mc),
205 };
206 struct GNUNET_MQ_Envelope *env;
208 size_t key_len = 0;
209 size_t ss_size = 0;
210
211 if (NULL != mc->mq)
212 {
213 GNUNET_MQ_destroy (mc->mq);
214 /* MUST clear @e mq before handing control to the callback: it is
215 dangling now, and an error callback that reacts by calling
216 #GNUNET_PEERSTORE_monitor_next() would send on freed memory. */
217 mc->mq = NULL;
218 if (NULL != mc->error_cb)
219 mc->error_cb (mc->error_cb_cls);
220 }
221 mc->mq = GNUNET_CLIENT_connect (mc->cfg,
222 "peerstore",
223 handlers,
225 mc);
226 if (NULL == mc->mq)
227 return;
228 if (NULL != mc->key)
229 key_len = strlen (mc->key) + 1;
230 if (NULL != mc->sub_system)
231 ss_size = strlen (mc->sub_system) + 1;
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending MONITOR_START\n");
234 htons (key_len) + htons (ss_size),
236 sm->iterate_first = htons (mc->iterate_first);
237 if (NULL != mc->peer)
238 {
239 sm->peer = *mc->peer;
240 sm->peer_set = htons (GNUNET_YES);
241 }
242 if (NULL != mc->sub_system)
243 GNUNET_memcpy (&sm[1], mc->sub_system, ss_size);
244 sm->sub_system_size = htons (ss_size);
245 if (NULL != mc->key)
246 GNUNET_memcpy (((char*) &sm[1]) + ss_size, mc->key, key_len);
247 sm->key_size = htons (key_len);
248 GNUNET_MQ_send (mc->mq, env);
249}
250
251
254 const struct GNUNET_CONFIGURATION_Handle *cfg,
255 int iterate_first,
256 const char *sub_system,
257 const struct GNUNET_PeerIdentity *peer,
258 const char *key,
260 void *error_cb_cls,
262 void *sync_cb_cls,
264 void *callback_cls)
265{
267
269 mc->callback = callback;
270 mc->callback_cls = callback_cls;
271 mc->sync_cb = sync_cb;
272 mc->sync_cb_cls = sync_cb_cls;
273 mc->error_cb = error_cb;
274 mc->error_cb_cls = error_cb_cls;
275 if (NULL != key)
276 mc->key = GNUNET_strdup (key);
277 if (NULL != peer)
278 {
279 mc->peer_buf = *peer;
280 mc->peer = &mc->peer_buf;
281 }
282 mc->iterate_first = iterate_first;
283 mc->sub_system = GNUNET_strdup (sub_system);
284 mc->cfg = cfg;
285 reconnect (mc);
286 if (NULL == mc->mq)
287 {
288 GNUNET_free (mc->sub_system);
289 GNUNET_free (mc->key);
290 GNUNET_free (mc);
291 return NULL;
292 }
293 return mc;
294}
295
296
302void
304{
305 if (NULL != zm->mq)
306 {
308 zm->mq = NULL;
309 }
310 GNUNET_free (zm->sub_system);
311 GNUNET_free (zm->key);
312 GNUNET_free (zm);
313}
314
315
316void
318 uint64_t limit)
319{
320 struct GNUNET_MQ_Envelope *env;
322
323 if (NULL == zm->mq)
324 {
325 /* Disconnected; #reconnect() re-sends MONITOR_START and the service
326 starts over, so there is nothing to ask for right now. */
327 return;
328 }
330 nm->limit = GNUNET_htonll (limit);
332}
struct GNUNET_MQ_MessageHandlers handlers[]
Definition 003.c:1
struct GNUNET_MessageHeader * msg
Definition 005.c:2
struct GNUNET_MQ_Envelope * env
Definition 005.c:1
static void error_cb(void *cls)
Function called if lookup fails.
Definition gnunet-abd.c:484
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition gnunet-arm.c:108
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
struct GNUNET_HashCode key
The key used in the DHT.
static void sync_cb(void *cls)
Function called once we are in sync in monitor mode.
static struct GNUNET_NAMESTORE_ZoneMonitor * zm
Monitor handle.
static struct GNUNET_TESTBED_Controller * mc
Handle to the master controller.
static int result
Global testing status.
API to the peerstore service.
Constants for network protocols.
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition client.c:1060
#define GNUNET_log(kind,...)
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_MQ_Error
Error codes for the queue.
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition mq.c:337
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition mq.c:732
void GNUNET_PEERSTORE_monitor_stop(struct GNUNET_PEERSTORE_Monitor *zm)
Stop monitoring.
struct GNUNET_PEERSTORE_Monitor * GNUNET_PEERSTORE_monitor_start(const struct GNUNET_CONFIGURATION_Handle *cfg, int iterate_first, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, GNUNET_SCHEDULER_TaskCallback error_cb, void *error_cb_cls, GNUNET_SCHEDULER_TaskCallback sync_cb, void *sync_cb_cls, GNUNET_PEERSTORE_Processor callback, void *callback_cls)
Request watching a given key The monitoring can be filtered to contain only records matching peer and...
void GNUNET_PEERSTORE_monitor_next(struct GNUNET_PEERSTORE_Monitor *zm, uint64_t limit)
Calls the monitor processor specified in GNUNET_PEERSTORE_monitor_start for the next record(s).
void(* GNUNET_PEERSTORE_Processor)(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Function called by PEERSTORE for each matching record.
#define GNUNET_MESSAGE_TYPE_PEERSTORE_MONITOR_SYNC
Monitor sync.
#define GNUNET_MESSAGE_TYPE_PEERSTORE_MONITOR_NEXT
Monitor next request.
#define GNUNET_MESSAGE_TYPE_PEERSTORE_MONITOR_START
Monitor request.
#define GNUNET_MESSAGE_TYPE_PEERSTORE_RECORD
Record result message.
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
IPC messages.
static int check_result(void *cls, const struct PeerstoreRecordMessage *msg)
When a response for iterate request is received, check the message is well-formed.
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error err)
static void handle_sync(void *cls, const struct GNUNET_MessageHeader *msg)
static void handle_result(void *cls, const struct PeerstoreRecordMessage *msg)
When a response to monitor is received.
#define LOG(kind,...)
struct GNUNET_PEERSTORE_Record * PEERSTORE_parse_record_message(const struct PeerstoreRecordMessage *srm)
Parses a message carrying a record.
void PEERSTORE_destroy_record(struct GNUNET_PEERSTORE_Record *record)
Free any memory allocated for this record.
Helper peerstore functions.
#define _(String)
GNU gettext support macro.
Definition platform.h:179
static void reconnect(void)
Adjust exponential back-off and reconnect to the service.
A 512-bit hashcode.
Handle to a message queue.
Definition mq.c:87
Message handler for a specific message type.
Header for all communications.
struct GNUNET_MQ_Handle * mq
Handle to namestore service.
void * error_cb_cls
Closure for error_cb.
struct GNUNET_MQ_Handle * mq
MQ.
void * callback_cls
Closure for callback.
const struct GNUNET_PeerIdentity * peer
The peer we are watching for values, pointing at peer_buf, or NULL if we watch all peers.
int iterate_first
Iterate first flag.
GNUNET_PEERSTORE_Processor callback
Callback with each record received.
struct GNUNET_PEERSTORE_Monitor * next
Kept in a DLL.
GNUNET_SCHEDULER_TaskCallback sync_cb
Sync CB.
GNUNET_SCHEDULER_TaskCallback error_cb
Function to call on errors.
char * sub_system
The sub system requested the watch.
char * key
The key we like to watch for values.
struct GNUNET_PeerIdentity peer_buf
Backing storage for peer.
const struct GNUNET_CONFIGURATION_Handle * cfg
CFG.
struct GNUNET_PEERSTORE_Monitor * prev
Kept in a DLL.
struct GNUNET_HashCode keyhash
Hash of the combined key.
The identity of the host (wraps the signing key of the peer).
Iteration next message.
Definition peerstore.h:185
uint64_t limit
Number of records to return.
Definition peerstore.h:194
Iteration start message.
Definition peerstore.h:140
uint16_t iterate_first
GNUNET_YES if iterate first, GNUNET_NO otherwise
Definition peerstore.h:176
struct GNUNET_PeerIdentity peer
Peer Identity.
Definition peerstore.h:149
uint16_t peer_set
GNUNET_YES if peer id value set, GNUNET_NO otherwise
Definition peerstore.h:165
uint16_t key_size
Size of the key string Allocated at position 1 after this struct.
Definition peerstore.h:160
uint16_t sub_system_size
Size of the sub_system string Allocated at position 0 after this struct.
Definition peerstore.h:171
Message carrying a PEERSTORE record message.
Definition peerstore.h:38