2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 #include "gnunet_util_lib.h"
25 #include "gnunet_dht_service.h"
27 #include "gnunet-service-mesh_dht.h"
28 #include "gnunet-service-mesh_peer.h"
30 #define MESH_DEBUG_DHT GNUNET_NO
33 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
35 #define DEBUG_DHT(...)
38 /******************************************************************************/
39 /******************************** STRUCTS **********************************/
40 /******************************************************************************/
45 /******************************************************************************/
46 /******************************* GLOBALS ***********************************/
47 /******************************************************************************/
52 static struct GNUNET_DHT_Handle *dht_handle;
55 * How often to PUT own ID in the DHT.
57 static struct GNUNET_TIME_Relative id_announce_time;
60 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
62 static unsigned long long dht_replication_level;
65 * Task to periodically announce itself in the network.
67 static GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
70 * Own ID (full value).
72 static struct GNUNET_PeerIdentity *id;
77 static struct GNUNET_CRYPTO_EccPrivateKey *private_key;
80 /******************************************************************************/
81 /******************************** STATIC ***********************************/
82 /******************************************************************************/
86 * Periodically announce self id in the DHT
89 * @param tc task context
92 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
95 struct GNUNET_HashCode phash;
97 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
99 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
103 * - Set data expiration in function of X
106 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
108 block.id = my_full_id;
109 GNUNET_CRYPTO_hash (&my_full_id, sizeof (struct GNUNET_PeerIdentity), &phash);
110 GNUNET_DHT_put (dht_handle, /* DHT handle */
111 &phash, /* Key to use */
112 dht_replication_level, /* Replication level */
113 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
114 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
115 sizeof (block), /* Size of the data */
116 (const char *) &block, /* Data itself */
117 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
118 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
119 NULL, /* Continuation */
120 NULL); /* Continuation closure */
122 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
126 /******************************************************************************/
127 /******************************** API ***********************************/
128 /******************************************************************************/
131 * Initialize the DHT subsystem.
133 * @param c Configuration.
136 GMD_init (const struct GNUNET_CONFIGURATION_Handle *c)
139 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
140 &dht_replication_level))
142 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
143 "MESH", "DHT_REPLICATION_LEVEL", "USING DEFAULT");
144 dht_replication_level = 3;
148 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
151 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
152 "MESH", "ID_ANNOUNCE_TIME", "MISSING");
153 GNUNET_SCHEDULER_shutdown ();
157 dht_handle = GNUNET_DHT_connect (c, 64);
158 if (NULL == dht_handle)
163 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
168 * Shut down the DHT subsystem.
173 if (dht_handle != NULL)
175 GNUNET_DHT_disconnect (dht_handle);
178 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
180 GNUNET_SCHEDULER_cancel (announce_id_task);
181 announce_id_task = GNUNET_SCHEDULER_NO_TASK;