GNUnet 0.21.1
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 "gnunet_common.h"
26#include "gnunet_protocols.h"
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "peerstore.h"
30#include "peerstore_common.h"
32
33#define LOG(kind, ...) GNUNET_log_from (kind, "peerstore-monitor-api", \
34 __VA_ARGS__)
39{
44
49
54
59
64
69
74
79
83 const char *key;
84
88 const char *sub_system;
89
93 uint32_t rid;
94
99
104
109
114
119};
120
121static void
122handle_sync (void *cls, const struct GNUNET_MessageHeader *msg)
123{
124 struct GNUNET_PEERSTORE_Monitor *mc = cls;
125
126 if (NULL != mc->sync_cb)
127 mc->sync_cb (mc->sync_cb_cls);
128}
129
130
138static int
139check_result (void *cls, const struct PeerstoreRecordMessage *msg)
140{
141 /* we defer validation to #handle_result */
142 return GNUNET_OK;
143}
144
145
152static void
153handle_result (void *cls, const struct PeerstoreRecordMessage *msg)
154{
155 struct GNUNET_PEERSTORE_Monitor *mc = cls;
157
158 LOG (GNUNET_ERROR_TYPE_DEBUG, "Monitor received RecordMessage\n");
160 if (NULL == record)
161 {
162 mc->callback (mc->callback_cls,
163 NULL,
164 _ ("Received a malformed response from service."));
165 }
166 else
167 {
168 mc->callback (mc->callback_cls, record, NULL);
170 }
171}
172
173
174static void reconnect (struct GNUNET_PEERSTORE_Monitor *mc);
175
176static void
178{
179 struct GNUNET_PEERSTORE_Monitor *mc = cls;
180
181 reconnect (mc);
182}
183
184
185static void
187{
192 mc),
195 struct PeerstoreRecordMessage, mc),
197 };
198 struct GNUNET_MQ_Envelope *env;
200 size_t key_len = 0;
201 size_t ss_size = 0;
202
203 if (NULL != mc->mq)
204 {
205 GNUNET_MQ_destroy (mc->mq);
206 mc->error_cb (mc->error_cb_cls);
207 }
208 mc->mq = GNUNET_CLIENT_connect (mc->cfg,
209 "peerstore",
210 handlers,
212 mc);
213 if (NULL == mc->mq)
214 return;
215 if (NULL != mc->key)
216 key_len = strlen (mc->key) + 1;
217 if (NULL != mc->sub_system)
218 ss_size = strlen (mc->sub_system) + 1;
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending MONITOR_START\n");
221 htons (key_len) + htons (ss_size),
223 sm->iterate_first = htons (mc->iterate_first);
224 if (NULL != mc->peer)
225 {
226 sm->peer = *mc->peer;
227 sm->peer_set = htons (GNUNET_YES);
228 }
229 if (NULL != mc->sub_system)
230 GNUNET_memcpy (&sm[1], mc->sub_system, ss_size);
231 sm->sub_system_size = htons (ss_size);
232 if (NULL != mc->key)
233 GNUNET_memcpy (((char*) &sm[1]) + ss_size, mc->key, key_len);
234 sm->key_size = htons (key_len);
235 GNUNET_MQ_send (mc->mq, env);
236}
237
238
241 int iterate_first,
242 const char *sub_system,
243 const struct GNUNET_PeerIdentity *peer,
244 const char *key,
246 void *error_cb_cls,
248 void *sync_cb_cls,
250 void *callback_cls)
251{
253
255 mc->callback = callback;
256 mc->callback_cls = callback_cls;
257 mc->sync_cb = sync_cb;
258 mc->sync_cb_cls = sync_cb_cls;
259 mc->error_cb = error_cb;
260 mc->error_cb_cls = error_cb_cls;
261 mc->key = key;
262 mc->peer = peer;
263 mc->iterate_first = iterate_first;
264 mc->sub_system = GNUNET_strdup (sub_system);
265 mc->cfg = cfg;
266 reconnect (mc);
267 if (NULL == mc->mq)
268 {
269 GNUNET_free (mc);
270 return NULL;
271 }
272 return mc;
273}
274
275
281void
283{
284 if (NULL != zm->mq)
285 {
287 zm->mq = NULL;
288 }
289 GNUNET_free (zm);
290}
291
292
293void
295 uint64_t limit)
296{
297 struct GNUNET_MQ_Envelope *env;
299
301 nm->limit = GNUNET_htonll (limit);
303}
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:479
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:109
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.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
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:1057
#define GNUNET_log(kind,...)
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:37
#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:304
#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.
Definition: gnunet_mq_lib.h:63
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:78
#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:683
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 void reconnect(struct GNUNET_PEERSTORE_Monitor *mc)
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:178
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.
Context for a monitor.
const char * sub_system
The sub system requested the watch.
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.
int iterate_first
Iterate first flag.
GNUNET_PEERSTORE_Processor callback
Callback with each record received.
struct GNUNET_PEERSTORE_Monitor * next
Kept in a DLL.
const char * key
The key we like to watch for values.
GNUNET_SCHEDULER_TaskCallback sync_cb
Sync CB.
GNUNET_SCHEDULER_TaskCallback error_cb
Function to call on errors.
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.
Single PEERSTORE record.
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