GNUnet debian-0.24.3-24-gfea921bd2
gnunet-service-dht.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011, 2016 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 "platform.h"
28#include "gnunet_common.h"
29#include "gnunet_block_lib.h"
30#include "gnunet_util_lib.h"
32#include "gnunet_pils_service.h"
34#include "gnunet-service-dht.h"
38#include "plugin_dhtu_ip.h"
39#include "plugin_dhtu_gnunet.h"
40
45#define HELLO_FREQUENCY GNUNET_TIME_UNIT_HOURS
46
47
52{
53
58
63
68
73
78
82 char *name;
83};
84
85
90{
94 struct MyAddress *next;
95
99 struct MyAddress *prev;
100
105
109 char *url;
110
115};
116
120struct PilsRequest
121{
125 struct PilsRequest *prev;
126
130 struct PilsRequest *next;
131
136};
137
142
147
148
153
158
163
168
172static struct GDS_Underlay *u_head;
173
177static struct GDS_Underlay *u_tail;
178
182static struct MyAddress *a_head;
183
187static struct MyAddress *a_tail;
188
194
195
205static void
208 double logestimate,
209 double std_dev)
210{
211 struct GDS_Underlay *u = cls;
212 double sum = 0.0;
213
215 "# Network size estimates received",
216 1,
217 GNUNET_NO);
218 /* do not allow estimates < 0.5 */
219 u->network_size_estimate = pow (2.0,
220 GNUNET_MAX (0.5,
221 logestimate));
222 for (struct GDS_Underlay *p = u_head; NULL != p; p = p->next)
223 sum += p->network_size_estimate;
224 if (sum <= 2.0)
226 else
227 log_of_network_size_estimate = log2 (sum);
228}
229
230
236double
238{
240}
241
242
244
245
255static void
256u_address_add (void *cls,
257 const char *address,
259 void **ctx)
260{
261 struct GDS_Underlay *u = cls;
262 struct MyAddress *a;
263
265 "Underlay adds address %s for this peer\n",
266 address);
267 a = GNUNET_new (struct MyAddress);
268 a->source = source;
270 a->u = u;
272 a_tail,
273 a);
274 *ctx = a;
275
276}
277
278
284static void
286{
287 struct MyAddress *a = ctx;
288
290 "Underlay deletes address %s for this peer\n",
291 a->url);
293 a_tail,
294 a);
295 GNUNET_free (a->url);
296 GNUNET_free (a);
297}
298
299
300void
302 const char *address)
303{
304 for (struct GDS_Underlay *u = u_head;
305 NULL != u;
306 u = u->next)
307 u->dhtu->try_connect (u->dhtu->cls,
308 pid,
309 address);
310}
311
312
313void
315 struct GNUNET_DHTU_Target *target,
316 const void *msg,
317 size_t msg_size,
319 void *finished_cb_cls)
320{
321 u->dhtu->send (u->dhtu->cls,
322 target,
323 msg,
324 msg_size,
326 finished_cb_cls);
327}
328
329
330void
333{
334 u->dhtu->drop (ph);
335}
336
337
341{
342 return u->dhtu->hold (u->dhtu->cls,
343 target);
344}
345
346
352static void
353shutdown_task (void *cls)
354{
355 struct GDS_Underlay *u;
356 struct PilsRequest *pr;
357
358 while (NULL != (u = u_head))
359 {
361 u_tail,
362 u);
363 if (0 == strcmp (u->name, "gnunet"))
364 {
365 DHTU_gnunet_done (u->dhtu);
366 }
367#ifdef LINUX
368 else if (0 == strcmp (u->name, "ip"))
369 {
370 DHTU_ip_done (u->dhtu);
371 }
372#endif
373 else
374 {
375 GNUNET_assert (0);
376 }
377 GNUNET_free (u->name);
378 GNUNET_free (u);
379 }
383 if (NULL != GDS_block_context)
384 {
386 GDS_block_context = NULL;
387 }
389 if (NULL != GDS_stats)
390 {
392 GNUNET_YES);
393 GDS_stats = NULL;
394 }
395 if (NULL != GDS_my_hello)
396 {
398 GDS_my_hello = NULL;
399 }
400 while (NULL != (pr = pils_requests_head))
401 {
404 pr);
405 if (NULL != pr->op)
407 GNUNET_free (pr);
408 }
409 if (NULL != GDS_pils)
410 {
412 GDS_pils = NULL;
413 }
414}
415
416
424static void
425load_underlay (void *cls,
426 const char *section)
427{
428 struct GDS_Underlay *u;
429
430 (void) cls;
431 if (0 != strncasecmp (section,
432 "dhtu-",
433 strlen ("dhtu-")))
434 return;
435 if (GNUNET_YES !=
437 section,
438 "ENABLED"))
439 return;
440 section += strlen ("dhtu-");
441 u = GNUNET_new (struct GDS_Underlay);
442 u->env.cls = u;
443 u->env.cfg = GDS_cfg;
444 u->env.address_add_cb = &u_address_add;
445 u->env.address_del_cb = &u_address_del;
446 u->env.network_size_cb = &update_network_size_estimate;
447 u->env.connect_cb = &GDS_u_connect;
448 u->env.disconnect_cb = &GDS_u_disconnect;
449 u->env.receive_cb = &GDS_u_receive;
450
453 if (0 == strcmp (section, "gnunet"))
454 {
455 u->dhtu = DHTU_gnunet_init (&u->env);
456 }
457#ifdef LINUX
458 else if (0 == strcmp (section, "ip"))
459 {
460 u->dhtu = DHTU_ip_init (&u->env);
461 }
462#endif
463 if (NULL == u->dhtu)
464 {
465 GNUNET_free (u);
466 return;
467 }
468 u->name = GNUNET_strdup (section);
470 u_tail,
471 u);
472}
473
474
475static void
476pid_change_cb (void *cls,
477 const struct GNUNET_HELLO_Parser *parser,
478 const struct GNUNET_HashCode *hash)
479{
480 (void) cls;
481
484 sizeof(struct GNUNET_PeerIdentity),
486 if (NULL != GDS_my_hello)
489
490 if (NULL == GDS_my_hello)
491 {
492 GNUNET_break (0);
493 return;
494 }
496}
497
498
506static void
507run (void *cls,
508 const struct GNUNET_CONFIGURATION_Handle *c,
510{
511 GDS_cfg = c;
514 GNUNET_assert (NULL != GDS_pils);
515 /* Wait until we get the first peer id until we actually start other parts of
516 * the service */
517 char *keyfile;
518
519 if (GNUNET_OK !=
521 "PEER",
522 "PRIVATE_KEY",
523 &keyfile))
524 {
526 "PEER",
527 "PRIVATE_KEY");
529 return;
530 }
531 if (GNUNET_SYSERR ==
535 {
537 "Failed to setup peer's private key\n");
538 GNUNET_free (keyfile);
540 return;
541 }
542 GNUNET_free (keyfile);
543
544
548 sizeof(struct GNUNET_PeerIdentity),
552 GDS_cfg);
557 NULL);
558 if (GNUNET_OK !=
560 {
562 return;
563 }
566 NULL);
567 if (NULL == u_head)
568 {
570 "No DHT underlays configured!\n");
572 return;
573 }
574}
575
576
577/* Finally, define the main method */
579
580
581/* end of gnunet-service-dht.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static mp_limb_t u[(((256)+GMP_NUMB_BITS - 1)/GMP_NUMB_BITS)]
static void finished_cb(void *cls, enum GNUNET_GenericReturnValue rv)
static uint64_t timestamp(void)
Get current timestamp.
static char * address
GNS address for this phone.
static struct GNUNET_FS_Handle * ctx
static GstElement * source
Appsrc instance into which we write data for the pipeline.
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
static void pid_change_cb(void *cls, const struct GNUNET_HELLO_Parser *parser, const struct GNUNET_HashCode *hash)
static void load_underlay(void *cls, const char *section)
Function iterating over all configuration sections.
struct GNUNET_PeerIdentity GDS_my_identity
Identity of this peer.
double GDS_NSE_get(void)
Return the current NSE.
struct GNUNET_MessageHeader * GDS_my_hello
Our HELLO.
static struct MyAddress * a_head
Head of addresses of this peer.
static void u_address_add(void *cls, const char *address, struct GNUNET_DHTU_Source *source, void **ctx)
Function to call with new addresses of this peer.
static struct MyAddress * a_tail
Tail of addresses of this peer.
struct GNUNET_CRYPTO_EddsaPrivateKey GDS_my_private_key
Our private key.
static struct PilsRequest * pils_requests_head
PILS Operation DLL.
static struct GDS_Underlay * u_head
Handles for the DHT underlays.
static void shutdown_task(void *cls)
Task run during shutdown.
static void u_address_del(void *ctx)
Function to call with expired addresses of this peer.
struct GNUNET_DHTU_PreferenceHandle * GDS_u_hold(struct GDS_Underlay *u, struct GNUNET_DHTU_Target *target)
Create a hold on target at underlay u.
void GDS_u_send(struct GDS_Underlay *u, 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 struct GDS_Underlay * u_tail
Handles for the DHT underlays.
static struct PilsRequest * pils_requests_tail
PILS Operation DLL.
static void run(void *cls, const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SERVICE_Handle *service)
Process dht requests.
void GDS_u_drop(struct GDS_Underlay *u, struct GNUNET_DHTU_PreferenceHandle *ph)
Drop a hold ph from underlay u.
void GDS_u_try_connect(const struct GNUNET_PeerIdentity *pid, const char *address)
Ask all underlays to connect to peer pid at address.
struct GNUNET_HashCode GDS_my_identity_hash
Hash of the identity of this peer.
static double log_of_network_size_estimate
log of the current network size estimate, used as the point where we switch between random and determ...
static void update_network_size_estimate(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
Callback that is called when network size estimate is updated.
GNUnet DHT globals.
struct GNUNET_PILS_Handle * GDS_pils
Handle for the pils service.
struct GNUNET_STATISTICS_Handle * GDS_stats
Handle for the statistics service.
struct GNUNET_BLOCK_Context * GDS_block_context
Our handle to the BLOCK library.
struct GNUNET_SERVICE_Handle * GDS_service
Handle for the service.
const struct GNUNET_CONFIGURATION_Handle * GDS_cfg
Configuration we use.
GNUnet DHT service's client management code.
#define GDS_DHT_SERVICE_INIT(name, run)
Define "main" method using service macro.
void GDS_CLIENTS_init(void)
Initialize client subsystem.
void GDS_CLIENTS_stop(void)
Shutdown client subsystem.
void GDS_DATACACHE_init()
Initialize datacache subsystem.
void GDS_DATACACHE_done()
Shutdown datacache subsystem.
GNUnet DHT service's datacache integration.
void GDS_NEIGHBOURS_done()
Shutdown neighbours subsystem.
void GDS_u_disconnect(void *ctx)
Function to call when we disconnected from a peer and can henceforth cannot transmit to that peer any...
void GDS_NEIGHBOURS_broadcast(const struct GNUNET_MessageHeader *msg)
Send msg to all peers in our buckets.
void GDS_u_connect(void *cls, struct GNUNET_DHTU_Target *target, const struct GNUNET_PeerIdentity *pid, void **ctx)
Function to call when we connect to a peer and can henceforth transmit to that peer.
enum GNUNET_GenericReturnValue GDS_NEIGHBOURS_init()
Initialize neighbours subsystem.
void GDS_u_receive(void *cls, void **tctx, void **sctx, const void *message, size_t message_size)
Function to call when we receive a message.
GNUnet DHT routing code.
void GDS_ROUTING_done()
Shutdown routing subsystem.
void GDS_ROUTING_init()
Initialize routing subsystem.
GNUnet DHT tracking of requests for routing replies.
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-uri.c:38
Library for data block manipulation.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
Helper library for handling HELLO URIs.
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:367
void GNUNET_PILS_disconnect(struct GNUNET_PILS_Handle *handle)
Disconnect from the PILS service.
Definition: pils_api.c:390
void GNUNET_PILS_cancel(struct GNUNET_PILS_Operation *op)
Cancel request.
Definition: pils_api.c:495
API to create, modify and access statistics.
void GNUNET_BLOCK_context_destroy(struct GNUNET_BLOCK_Context *ctx)
Destroy the block context.
Definition: block.c:158
struct GNUNET_BLOCK_Context * GNUNET_BLOCK_context_create(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a block context.
Definition: block.c:140
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
void GNUNET_CONFIGURATION_iterate_sections(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_CONFIGURATION_SectionIterator iter, void *iter_cls)
Iterate over all sections in the configuration.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_key_from_file(const char *filename, int do_create, struct GNUNET_CRYPTO_EddsaPrivateKey *pkey)
Create a new private key by reading it from a file.
void GNUNET_CRYPTO_eddsa_key_get_public(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:201
#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_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
struct GNUNET_MessageHeader * GNUNET_HELLO_parser_to_dht_hello_msg(const struct GNUNET_HELLO_Parser *parser)
Generate DHT HELLO message from a parser.
Definition: hello-uri.c:1172
const struct GNUNET_PeerIdentity * GNUNET_HELLO_parser_get_id(const struct GNUNET_HELLO_Parser *parser)
Get the PeerIdentity for this builder.
Definition: hello-uri.c:353
#define GNUNET_log(kind,...)
#define GNUNET_MAX(a, b)
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_INFO
#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.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:567
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1339
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
void * DHTU_gnunet_done(struct GNUNET_DHTU_PluginFunctions *api)
Exit point from the plugin.
struct GNUNET_DHTU_PluginFunctions * DHTU_gnunet_init(struct GNUNET_DHTU_PluginEnvironment *env)
Entry point for the plugin.
struct GNUNET_DHTU_PluginFunctions * DHTU_ip_init(struct GNUNET_DHTU_PluginEnvironment *env)
Entry point for the plugin.
void * DHTU_ip_done(struct GNUNET_DHTU_PluginFunctions *api)
Exit point from the plugin.
Information we keep per underlay.
struct GNUNET_DHTU_PluginEnvironment env
Environment for this underlay.
struct GDS_Underlay * prev
Kept in a DLL.
double network_size_estimate
current network size estimate for this underlay.
char * name
Name of the underlay (i.e.
struct GDS_Underlay * next
Kept in a DLL.
struct GNUNET_DHTU_PluginFunctions * dhtu
Underlay API handle.
Private ECC key encoded for transmission.
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
Opaque handle expressing a preference of the DHT to keep a particular target connected.
struct GNUNET_DHTU_Target * target
Target a preference was expressed for.
Opaque handle that the underlay offers for our address to be used when sending messages to another pe...
Opaque handle that the underlay offers for the target peer when sending messages to another peer.
Context for parsing HELLOs.
Definition: hello-uri.c:232
A 512-bit hashcode.
Header for all communications.
The identity of the host (wraps the signing key of the peer).
struct GNUNET_CRYPTO_EddsaPublicKey public_key
Handle to a service.
Definition: service.c:116
Time for absolute times used by GNUnet, in microseconds.
An address of this peer.
char * url
Textual representation of the address.
struct GDS_Underlay * u
Underlay of this address.
struct MyAddress * prev
Kept in a DLL.
struct MyAddress * next
Kept in a DLL.
struct GNUNET_DHTU_Source * source
Underlay handle for the address.
struct GNUNET_PILS_Operation * op
The pils operation.
struct PilsRequest * next
DLL.
struct PilsRequest * prev
DLL.