GNUnet 0.22.2
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__)
38{
43
48
53
58
63
68
73
78
82 const char *key;
83
88
92 uint32_t rid;
93
98
103
108
113
118};
119
120static void
121handle_sync (void *cls, const struct GNUNET_MessageHeader *msg)
122{
123 struct GNUNET_PEERSTORE_Monitor *mc = cls;
124
125 if (NULL != mc->sync_cb)
126 mc->sync_cb (mc->sync_cb_cls);
127}
128
129
137static int
138check_result (void *cls, const struct PeerstoreRecordMessage *msg)
139{
140 /* we defer validation to #handle_result */
141 return GNUNET_OK;
142}
143
144
151static void
152handle_result (void *cls, const struct PeerstoreRecordMessage *msg)
153{
154 struct GNUNET_PEERSTORE_Monitor *mc = cls;
156
157 LOG (GNUNET_ERROR_TYPE_DEBUG, "Monitor received RecordMessage\n");
159 if (NULL == record)
160 {
161 mc->callback (mc->callback_cls,
162 NULL,
163 _ ("Received a malformed response from service."));
164 }
165 else
166 {
167 mc->callback (mc->callback_cls, record, NULL);
169 }
170}
171
172
173static void reconnect (struct GNUNET_PEERSTORE_Monitor *mc);
174
175static void
177{
178 struct GNUNET_PEERSTORE_Monitor *mc = cls;
179
180 reconnect (mc);
181}
182
183
184static void
186{
191 mc),
194 struct PeerstoreRecordMessage, mc),
196 };
197 struct GNUNET_MQ_Envelope *env;
199 size_t key_len = 0;
200 size_t ss_size = 0;
201
202 if (NULL != mc->mq)
203 {
204 GNUNET_MQ_destroy (mc->mq);
205 mc->error_cb (mc->error_cb_cls);
206 }
207 mc->mq = GNUNET_CLIENT_connect (mc->cfg,
208 "peerstore",
209 handlers,
211 mc);
212 if (NULL == mc->mq)
213 return;
214 if (NULL != mc->key)
215 key_len = strlen (mc->key) + 1;
216 if (NULL != mc->sub_system)
217 ss_size = strlen (mc->sub_system) + 1;
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending MONITOR_START\n");
220 htons (key_len) + htons (ss_size),
222 sm->iterate_first = htons (mc->iterate_first);
223 if (NULL != mc->peer)
224 {
225 sm->peer = *mc->peer;
226 sm->peer_set = htons (GNUNET_YES);
227 }
228 if (NULL != mc->sub_system)
229 GNUNET_memcpy (&sm[1], mc->sub_system, ss_size);
230 sm->sub_system_size = htons (ss_size);
231 if (NULL != mc->key)
232 GNUNET_memcpy (((char*) &sm[1]) + ss_size, mc->key, key_len);
233 sm->key_size = htons (key_len);
234 GNUNET_MQ_send (mc->mq, env);
235}
236
237
240 const struct GNUNET_CONFIGURATION_Handle *cfg,
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->sub_system);
290 GNUNET_free (zm);
291}
292
293
294void
296 uint64_t limit)
297{
298 struct GNUNET_MQ_Envelope *env;
300
302 nm->limit = GNUNET_htonll (limit);
304}
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:480
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.
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:305
#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:61
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:76
#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:700
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.
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.
char * sub_system
The sub system requested the watch.
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