GNUnet 0.27.0
 
Loading...
Searching...
No Matches
plugin_dhtu_gnunet.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet
3 Copyright (C) 2021, 2026 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 */
20
27#include "gnunet_common.h"
28#include "platform.h"
29#include "gnunet_dhtu_plugin.h"
30#include "gnunet_core_service.h"
31#include "gnunet_pils_service.h"
35#include "gnunet_nse_service.h"
36#include "plugin_dhtu_gnunet.h"
37
43{
44
48 void *app_ctx;
49
50};
51
52
101
102
124
125
129struct Plugin
130{
131
137
142
147
152
157
162
168
173
174};
175
176
177// #include "../peerinfo-tool/gnunet-peerinfo_plugins.c"
178
179
187static void
189 const struct GNUNET_PeerIdentity *pid,
190 const char *address)
191{
192 struct Plugin *plugin = cls;
193 enum GNUNET_NetworkType nt = 0;
194 char *addr;
195 const char *eou;
196 int pfx_len;
197
198 eou = strstr (address,
199 "://");
200 if (NULL == eou)
201 {
202 GNUNET_break (0);
203 return;
204 }
205 pfx_len = eou - address;
206 eou += 3;
207 GNUNET_asprintf (&addr,
208 "%.*s-%s",
209 pfx_len,
210 address,
211 eou);
213 pid,
214 nt,
215 addr);
216 GNUNET_free (addr);
217}
218
219
228static struct GNUNET_DHTU_PreferenceHandle *
229gnunet_hold (void *cls,
231{
232 struct Plugin *plugin = cls;
234
236 ph->target = target;
239 ph);
240 target->ph_count++;
241 if (NULL != target->ash)
243 target->ash
245 &target->pid,
248 return ph;
249}
250
251
258static void
260{
261 struct GNUNET_DHTU_Target *target = ph->target;
262 struct Plugin *plugin = target->plugin;
263
265 target->ph_tail,
266 ph);
267 target->ph_count--;
268 GNUNET_free (ph);
269 if (NULL != target->ash)
271 if (0 == target->ph_count)
272 target->ash = NULL;
273 else
274 target->ash
276 &target->pid,
279}
280
281
297static void
298gnunet_send (void *cls,
299 struct GNUNET_DHTU_Target *target,
300 const void *msg,
301 size_t msg_size,
303 void *finished_cb_cls)
304{
305 struct GNUNET_MQ_Envelope *env;
306 struct GNUNET_MessageHeader *cmsg;
307
308 env = GNUNET_MQ_msg_extra (cmsg,
309 msg_size,
313 finished_cb_cls);
314 memcpy (&cmsg[1],
315 msg,
316 msg_size);
317 GNUNET_MQ_send (target->mq,
318 env);
319}
320
321
331static void *
333 const struct GNUNET_PeerIdentity *peer,
334 struct GNUNET_MQ_Handle *mq,
335 enum GNUNET_CORE_PeerClass class)
336{
337 struct Plugin *plugin = cls;
338 struct GNUNET_DHTU_Target *target;
339
340 target = GNUNET_new (struct GNUNET_DHTU_Target);
341 target->plugin = plugin;
342 target->mq = mq;
343 target->pid = *peer;
344 plugin->env->connect_cb (plugin->env->cls,
345 target,
346 &target->pid,
347 &target->app_ctx);
348 return target;
349}
350
351
360static void
362 const struct GNUNET_PeerIdentity *peer,
363 void *peer_cls)
364{
365 struct Plugin *plugin = cls;
366 struct GNUNET_DHTU_Target *target = peer_cls;
367
368 plugin->env->disconnect_cb (target->app_ctx);
369 if (NULL != target->ash)
371 GNUNET_free (target);
372}
373
374
375static void
376add_addr (void *cls,
377 const struct GNUNET_PeerIdentity *pid,
378 const char *addr)
379{
380 struct Plugin *plugin = cls;
381
383 "peerinfo_cb addr %s\n",
384 addr);
385 plugin->env->address_add_cb (plugin->env->cls,
386 addr,
387 &plugin->src,
388 &plugin->src.app_ctx);
389}
390
391
404static void
405peerinfo_cb (void *cls,
406 const struct GNUNET_PEERSTORE_Record *record,
407 const char *emsg)
408{
409 const struct GNUNET_PeerIdentity *my_identity;
410 struct Plugin *plugin = cls;
411 struct GNUNET_HELLO_Parser *parser;
412 struct GNUNET_MessageHeader *hello;
413
414 hello = record->value;
415 if (NULL == hello)
416 return;
418 if (! my_identity)
419 return;
420 if (0 != GNUNET_memcmp (&record->peer, my_identity))
421 {
422 GNUNET_PEERSTORE_monitor_next (plugin->peerstore_notify, 1);
423 return;
424 }
425 parser = GNUNET_HELLO_parser_from_msg (hello, &record->peer);
427 add_addr,
428 plugin);
430 GNUNET_PEERSTORE_monitor_next (plugin->peerstore_notify, 1);
431}
432
433
434static void
435error_cb (void *cls)
436{
438 "Error in PEERSTORE monitoring\n");
439}
440
441
442static void
443sync_cb (void *cls)
444{
446 "Done with initial PEERSTORE iteration during monitoring\n");
447}
448
449
462static void
463core_init_cb (void *cls,
464 const struct GNUNET_PeerIdentity *identity)
465{
466 struct Plugin *plugin = cls;
467
468 if (NULL == identity)
469 return;
470
471 plugin->peerstore_notify = GNUNET_PEERSTORE_monitor_start (plugin->env->cfg,
473 "peerstore",
474 NULL,
476 &error_cb,
477 NULL,
478 &sync_cb,
479 NULL,
481 plugin);
482}
483
484
492static int
494 const struct GNUNET_MessageHeader *msg)
495{
496 (void) cls;
497 (void) msg;
498 return GNUNET_OK;
499}
500
501
509static void
511 const struct GNUNET_MessageHeader *msg)
512{
513 struct GNUNET_DHTU_Target *origin = cls;
514 struct Plugin *plugin = origin->plugin;
515
516 plugin->env->receive_cb (plugin->env->cls,
517 &origin->app_ctx,
518 &plugin->src.app_ctx,
519 &msg[1],
520 ntohs (msg->size) - sizeof (*msg));
521}
522
523
532static void
533nse_cb (void *cls,
535 double logestimate,
536 double std_dev)
537{
538 struct Plugin *plugin = cls;
539
540 plugin->env->network_size_cb (plugin->env->cls,
541 timestamp,
542 logestimate,
543 std_dev);
544}
545
546
553void *
555{
556 struct Plugin *plugin = api->cls;
557
558 if (NULL != plugin->nse)
560 plugin->env->network_size_cb (plugin->env->cls,
562 0.0,
563 0.0);
564 if (NULL != plugin->core)
566 if (NULL != plugin->transport)
568 if (NULL != plugin->peerstore_notify)
569 GNUNET_PEERSTORE_monitor_stop (plugin->peerstore_notify);
570 if (NULL != plugin->peerstore)
572 // GPI_plugins_unload ();
573 if (plugin->pils)
577 return NULL;
578}
579
580
589{
590 struct GNUNET_DHTU_PluginFunctions *api;
591 struct Plugin *plugin;
593 GNUNET_MQ_hd_var_size (core_message,
596 NULL),
598 };
599 const struct GNUNET_CORE_ServiceInfo service_info = {
601 .version = { 1, 0 },
602 .version_max = { 1, 0 },
603 .version_min = { 1, 0 },
604 };
605
606 plugin = GNUNET_new (struct Plugin);
608 env->cfg, NULL, NULL);
609 plugin->env = env;
611 api->cls = plugin;
613 api->hold = &gnunet_hold;
614 api->drop = &gnunet_drop;
615 api->send = &gnunet_send;
616 plugin->peerstore = GNUNET_PEERSTORE_connect (env->cfg);
617 plugin->transport = GNUNET_TRANSPORT_application_init (env->cfg);
618 plugin->core = GNUNET_CORE_connect (env->cfg,
619 plugin,
623 handlers,
624 &service_info);
625 plugin->nse = GNUNET_NSE_connect (env->cfg,
626 &nse_cb,
627 plugin);
628 if ( (NULL == plugin->transport) ||
629 (NULL == plugin->core) ||
630 (NULL == plugin->nse) )
631 {
632 GNUNET_break (0);
633 DHTU_gnunet_done (api);
634 return NULL;
635 }
636 // GPI_plugins_load (env->cfg);
637 return api;
638}
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 struct GNUNET_TESTING_PluginFunctions * plugin
Plugin to dynamically load a test case.
static void finished_cb(void *cls, enum GNUNET_GenericReturnValue rv)
static uint64_t timestamp(void)
Get current timestamp.
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
static char * address
GNS address for this phone.
static char origin[GNUNET_DNSPARSER_MAX_NAME_LENGTH]
Current origin.
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
static struct GNUNET_IDENTITY_Handle * identity
Which namespace do we publish to? NULL if we do not publish to a namespace.
static struct GNUNET_PeerIdentity my_identity
Identity of this peer.
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
Core service; the main API for encrypted P2P communications.
Helper library for handling HELLO URIs.
API to retrieve the current network size estimate.
API to the peerstore service.
struct GNUNET_PILS_Handle * GNUNET_PILS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_PILS_PidChangeCallback pid_change_cb, void *cls)
Connect to the PILS service.
Definition pils_api.c:465
void GNUNET_PILS_disconnect(struct GNUNET_PILS_Handle *handle)
Disconnect from the PILS service.
Definition pils_api.c:488
const struct GNUNET_PeerIdentity * GNUNET_PILS_get_identity(const struct GNUNET_PILS_Handle *handle)
Return the current peer identity of a given handle.
Definition pils_api.c:727
Bandwidth allocation API for applications to interact with.
struct GNUNET_TRANSPORT_ApplicationSuggestHandle * GNUNET_TRANSPORT_application_suggest(struct GNUNET_TRANSPORT_ApplicationHandle *ch, const struct GNUNET_PeerIdentity *peer, enum GNUNET_MQ_PriorityPreferences pk, struct GNUNET_BANDWIDTH_Value32NBO bw)
An application would like TRANSPORT to connect to a peer.
void GNUNET_TRANSPORT_application_done(struct GNUNET_TRANSPORT_ApplicationHandle *ch)
Shutdown TRANSPORT application client.
void GNUNET_TRANSPORT_application_validate(struct GNUNET_TRANSPORT_ApplicationHandle *ch, const struct GNUNET_PeerIdentity *peer, enum GNUNET_NetworkType nt, const char *addr)
An application (or a communicator) has received a HELLO (or other address data of another peer) and w...
void GNUNET_TRANSPORT_application_suggest_cancel(struct GNUNET_TRANSPORT_ApplicationSuggestHandle *sh)
We no longer care about being connected to a peer.
struct GNUNET_TRANSPORT_ApplicationHandle * GNUNET_TRANSPORT_application_init(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the TRANSPORT application client handle.
#define GNUNET_BANDWIDTH_ZERO
Convenience definition to use for 0-bandwidth.
struct GNUNET_CORE_Handle * GNUNET_CORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls, GNUNET_CORE_StartupCallback init, GNUNET_CORE_ConnectEventHandler connects, GNUNET_CORE_DisconnectEventHandler disconnects, const struct GNUNET_MQ_MessageHandler *handlers, const struct GNUNET_CORE_ServiceInfo *service_info)
Connect to the core service.
Definition core_api.c:698
GNUNET_CORE_PeerClass
The peer class gives a hint about the capabilities of a peer.
void GNUNET_CORE_disconnect(struct GNUNET_CORE_Handle *handle)
Disconnect from the core service.
Definition core_api.c:744
@ GNUNET_CORE_SERVICE_DHT
Identifier for dht (distributed hash table) service.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
void GNUNET_HELLO_parser_free(struct GNUNET_HELLO_Parser *parser)
Release resources of a builder.
Definition hello-uri.c:380
const struct GNUNET_PeerIdentity * GNUNET_HELLO_parser_iterate(const struct GNUNET_HELLO_Parser *parser, GNUNET_HELLO_UriCallback uc, void *uc_cls)
Iterate over URIs in a parser.
Definition hello-uri.c:1011
struct GNUNET_HELLO_Parser * GNUNET_HELLO_parser_from_msg(const struct GNUNET_MessageHeader *msg, const struct GNUNET_PeerIdentity *pid)
Parse msg.
Definition hello-uri.c:416
#define GNUNET_log(kind,...)
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
@ GNUNET_OK
@ GNUNET_YES
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
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.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
void GNUNET_MQ_notify_sent(struct GNUNET_MQ_Envelope *ev, GNUNET_SCHEDULER_TaskCallback cb, void *cb_cls)
Call a callback once the envelope has been sent, that is, sending it can not be canceled anymore.
Definition mq.c:655
@ GNUNET_MQ_PRIO_BEST_EFFORT
Best-effort traffic (e.g.
void GNUNET_NSE_disconnect(struct GNUNET_NSE_Handle *h)
Disconnect from network size estimation service.
Definition nse_api.c:192
struct GNUNET_NSE_Handle * GNUNET_NSE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_NSE_Callback func, void *func_cls)
Connect to the network size estimation service.
Definition nse_api.c:164
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
void GNUNET_PEERSTORE_monitor_stop(struct GNUNET_PEERSTORE_Monitor *zm)
Stop monitoring.
void GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
Disconnect from the PEERSTORE service.
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...
struct GNUNET_PEERSTORE_Handle * GNUNET_PEERSTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the PEERSTORE service.
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).
#define GNUNET_PEERSTORE_HELLO_KEY
Key used for storing HELLO in the peerstore.
#define GNUNET_MESSAGE_TYPE_DHT_CORE
Encapsulation of DHT messages in CORE service.
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
static void * core_connect_cb(void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_MQ_Handle *mq, enum GNUNET_CORE_PeerClass class)
Method called whenever a given peer connects.
void * DHTU_gnunet_done(struct GNUNET_DHTU_PluginFunctions *api)
Exit point from the plugin.
static void sync_cb(void *cls)
static void handle_core_message(void *cls, const struct GNUNET_MessageHeader *msg)
Handle message from CORE for the DHT.
struct GNUNET_DHTU_PluginFunctions * DHTU_gnunet_init(struct GNUNET_DHTU_PluginEnvironment *env)
Entry point for the plugin.
static int check_core_message(void *cls, const struct GNUNET_MessageHeader *msg)
Anything goes, always return GNUNET_OK.
static void gnunet_drop(struct GNUNET_DHTU_PreferenceHandle *ph)
Do no long request underlay to keep the connection alive.
static void add_addr(void *cls, const struct GNUNET_PeerIdentity *pid, const char *addr)
static void core_disconnect_cb(void *cls, const struct GNUNET_PeerIdentity *peer, void *peer_cls)
Method called whenever a peer disconnects.
static void core_init_cb(void *cls, const struct GNUNET_PeerIdentity *identity)
Function called after GNUNET_CORE_connect has succeeded (or failed for good).
static void gnunet_try_connect(void *cls, const struct GNUNET_PeerIdentity *pid, const char *address)
Request creation of a session with a peer at the given address.
static void error_cb(void *cls)
static void nse_cb(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
Callback to call when network size estimate is updated.
static struct GNUNET_DHTU_PreferenceHandle * gnunet_hold(void *cls, struct GNUNET_DHTU_Target *target)
Request underlay to keep the connection to target alive if possible.
static void gnunet_send(void *cls, struct GNUNET_DHTU_Target *target, const void *msg, size_t msg_size, GNUNET_SCHEDULER_TaskCallback finished_cb, void *finished_cb_cls)
Send message to some other participant over the network.
static void peerinfo_cb(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Find the hello for our identity and then pass it to the DHT as a URL.
static struct GNUNET_MQ_Handle * mq
Our connection to the resolver service, created on-demand, but then persists until error or shutdown.
void * cls
Closure for all of the callbacks.
Context for the core service connection.
Definition core_api.c:78
Gnunet service info - identifying compatibility with a range of version of a service communicating ov...
enum GNUNET_CORE_Service service
Identifier of the service on top of CORE.
The datastore service will pass a pointer to a struct of this type as the first and only argument to ...
struct returned by the initialization function of the plugin
void(* try_connect)(void *cls, const struct GNUNET_PeerIdentity *pid, const char *address)
Request creation of a session with a peer at the given address.
void * cls
Closure to pass to all plugin functions.
struct GNUNET_DHTU_PreferenceHandle *(* hold)(void *cls, struct GNUNET_DHTU_Target *target)
Request underlay to keep the connection to target alive if possible.
void(* drop)(struct GNUNET_DHTU_PreferenceHandle *ph)
Do no longer request underlay to keep the connection alive.
void(* send)(void *cls, struct GNUNET_DHTU_Target *target, const void *msg, size_t msg_size, GNUNET_SCHEDULER_TaskCallback finished_cb, void *finished_cb_cls)
Send message to some other participant over the network.
Opaque handle expressing a preference of the DHT to keep a particular target connected.
struct GNUNET_DHTU_PreferenceHandle * prev
Kept in a DLL.
struct GNUNET_DHTU_Target * target
Target a preference was expressed for.
struct GNUNET_DHTU_PreferenceHandle * next
Kept in a DLL.
Opaque handle that the underlay offers for our address to be used when sending messages to another pe...
void * app_ctx
Application context for this source.
Opaque handle that the underlay offers for the target peer when sending messages to another peer.
struct GNUNET_TRANSPORT_ApplicationSuggestHandle * ash
Transport suggest handle.
struct GNUNET_DHTU_PreferenceHandle * ph_head
Head of preferences expressed for this target.
unsigned int ph_count
Preference counter, length of the ph_head DLL.
struct Plugin * plugin
Our plugin with its environment.
struct GNUNET_MQ_Handle * mq
CORE MQ to send messages to this peer.
void * app_ctx
Application context for this target.
struct GNUNET_DHTU_PreferenceHandle * ph_tail
Tail of preferences expressed for this target.
struct sockaddr_storage addr
Target IP address.
struct GNUNET_PeerIdentity pid
Identity of this peer.
Context for parsing HELLOs.
Definition hello-uri.c:233
Handle to a message queue.
Definition mq.c:87
Message handler for a specific message type.
Header for all communications.
Handle for talking with the NSE service.
Definition nse_api.c:40
Handle to the PEERSTORE service.
A handle for the PILS service.
Definition pils_api.c:82
The identity of the host (wraps the signing key of the peer).
void * cls
Closure to pass to start_testcase.
Time for absolute times used by GNUnet, in microseconds.
Handle to the TRANSPORT subsystem for application management.
Handle for TRANSPORT address suggestion requests.
Handle for a plugin.
Definition block.c:38
struct GNUNET_DHTU_PluginEnvironment * env
Callbacks into the DHT.
struct GNUNET_PEERSTORE_Monitor * peerstore_notify
Our peerstore notification context.
struct GNUNET_PEERSTORE_Handle * peerstore
Handle to the PEERSTORE service.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition block.c:47
struct GNUNET_PILS_Handle * pils
Handle to the PILS service.
struct GNUNET_DHTU_Source src
Our "source" address.
struct GNUNET_NSE_Handle * nse
Handle to the NSE service.
struct GNUNET_TRANSPORT_ApplicationHandle * transport
Handle to Transport service.
struct GNUNET_CORE_Handle * core
Handle to the CORE service.