GNUnet debian-0.24.3-24-gfea921bd2
gnunet-service-cadet_dht.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2013, 2017 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 */
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_pils_service.h"
30#include "gnunet_dht_service.h"
37
44#define STARTUP_DELAY GNUNET_TIME_relative_multiply ( \
45 GNUNET_TIME_UNIT_MILLISECONDS, 500)
46
52#define CHANGE_DELAY GNUNET_TIME_relative_multiply ( \
53 GNUNET_TIME_UNIT_MILLISECONDS, 100)
54
55
56#define LOG(level, ...) GNUNET_log_from (level, "cadet-dht", __VA_ARGS__)
57
58
60{
65};
66
67
72
77
81static unsigned long long dht_replication_level;
82
87
92
93
111static void
113 const struct GNUNET_HashCode *key,
114 const struct GNUNET_PeerIdentity *trunc_peer,
115 const struct GNUNET_DHT_PathElement *get_path,
116 unsigned int get_path_length,
117 const struct GNUNET_DHT_PathElement *put_path,
118 unsigned int put_path_length,
120 size_t size,
121 const void *data)
122{
123 const struct GNUNET_MessageHeader *hello = data;
124 struct CadetPeer *peer;
125
126 (void) trunc_peer;
127 GCPP_try_path_from_dht (get_path,
128 get_path_length,
129 put_path,
130 put_path_length);
131
132
133 peer = GCP_get (&put_path[0].pred,
134 GNUNET_YES);
136 "Got HELLO for %s\n",
137 GCP_2s (peer));
138 GCP_set_hello (peer,
139 hello);
140}
141
142
143static void
144announce_id (void*cls)
145{
146 struct GNUNET_HashCode phash;
147 const struct GNUNET_MessageHeader *hello;
148 size_t size;
149 size_t block_size;
150 void *block;
152 struct GNUNET_TIME_Relative next_put;
153
154 hello = GCH_get_mine ();
155 size = (NULL != hello) ? ntohs (hello->size) : 0;
156 if (0 == size)
157 {
161 }
162 else
163 {
166 }
167
168 /* Call again in id_announce_time, unless HELLO expires first,
169 * but wait at least 1s. */
170 next_put
172 next_put
173 = GNUNET_TIME_relative_min (next_put,
175 next_put
176 = GNUNET_TIME_relative_max (next_put,
181 NULL);
183 "# DHT announce",
184 1,
185 GNUNET_NO);
186 memset (&phash,
187 0,
188 sizeof(phash));
189 GNUNET_memcpy (&phash,
190 &my_full_id,
191 sizeof(my_full_id));
192 if ((0 == size) ||
194 &my_full_id,
195 &block,
196 &block_size,
197 &expiration)))
198 return;
199
201 "Announcing my HELLO (%lu bytes) in the DHT\n",
202 (unsigned long) block_size);
203 GNUNET_DHT_put (dht_handle, /* DHT handle */
204 &phash, /* Key to use */
205 dht_replication_level, /* Replication level */
207 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
208 GNUNET_BLOCK_TYPE_DHT_HELLO, /* Block type */
209 block_size, /* Size of the data */
210 (const char *) block, /* Data itself */
211 expiration, /* Data expiration */
212 NULL, /* Continuation */
213 NULL); /* Continuation closure */
214 GNUNET_free (block);
215}
216
217
218void
220{
221 if (NULL == announce_id_task)
222 return; /* too early */
227 NULL);
228}
229
230
231void
233{
234 if (GNUNET_OK !=
236 "CADET",
237 "DHT_REPLICATION_LEVEL",
239 {
241 "CADET",
242 "DHT_REPLICATION_LEVEL",
243 "USING DEFAULT");
245 }
246
247 if (GNUNET_OK !=
249 "CADET",
250 "ID_ANNOUNCE_TIME",
252 {
254 "CADET",
255 "ID_ANNOUNCE_TIME",
256 "MISSING");
258 return;
259 }
260
262 64);
263 GNUNET_break (NULL != dht_handle);
267 NULL);
268}
269
270
271void
273{
274 if (NULL != dht_handle)
275 {
277 dht_handle = NULL;
278 }
279 if (NULL != announce_id_task)
280 {
282 announce_id_task = NULL;
283 }
284}
285
286
287struct GCD_search_handle *
289{
290 struct GNUNET_HashCode phash;
291 struct GCD_search_handle *h;
292
294 "# DHT search",
295 1,
296 GNUNET_NO);
297 memset (&phash,
298 0,
299 sizeof(phash));
300 GNUNET_memcpy (&phash,
301 peer_id,
302 sizeof(*peer_id));
303
304 h = GNUNET_new (struct GCD_search_handle);
305 h->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
307 &phash, /* key to search */
308 dht_replication_level, /* replication level */
311 NULL, /* xquery */
312 0, /* xquery bits */
314 h);
316 "Starting DHT GET for peer %s (%p)\n",
318 h);
319 return h;
320}
321
322
323void
325{
327 "Stopping DHT GET %p\n",
328 h);
329 GNUNET_DHT_get_stop (h->dhtget);
330 GNUNET_free (h);
331}
332
333
334/* end of gnunet-service-cadet_dht.c */
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:98
static char * peer_id
Option –peer.
Definition: gnunet-cadet.c:42
static char * data
The data to insert into the dht.
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_TIME_Relative expiration
User supplied expiration value.
static uint32_t type
Type string converted to DNS type value.
struct GNUNET_PeerIdentity my_full_id
Local peer own ID.
Information we track per peer.
static struct GNUNET_SCHEDULER_Task * announce_id_task
Task to periodically announce itself in the network.
static struct GNUNET_TIME_Relative id_announce_time
How often to PUT own ID in the DHT.
#define CHANGE_DELAY
How long do we wait after we get an updated HELLO before publishing? Allows for the HELLO to be updat...
static unsigned long long dht_replication_level
DHT replication level, see DHT API: GNUNET_DHT_get_start(), GNUNET_DHT_put().
static struct GNUNET_TIME_Relative announce_delay
Delay for the next ID announce.
void GCD_search_stop(struct GCD_search_handle *h)
Stop DHT search started with GCD_search().
static void announce_id(void *cls)
static struct GNUNET_DHT_Handle * dht_handle
Handle to use DHT.
void GCD_hello_update()
Function called by the HELLO subsystem whenever OUR hello changes.
void GCD_init(const struct GNUNET_CONFIGURATION_Handle *c)
Initialize the DHT subsystem.
struct GCD_search_handle * GCD_search(const struct GNUNET_PeerIdentity *peer_id)
Search DHT for paths to peeR_id.
#define STARTUP_DELAY
How long do we wait before first announcing our presence to the DHT.
#define LOG(level,...)
static void dht_get_id_handler(void *cls, struct GNUNET_TIME_Absolute exp, const struct GNUNET_HashCode *key, const struct GNUNET_PeerIdentity *trunc_peer, const struct GNUNET_DHT_PathElement *get_path, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *put_path, unsigned int put_path_length, enum GNUNET_BLOCK_Type type, size_t size, const void *data)
Function to process paths received for a new peer addition.
void GCD_shutdown(void)
Shut down the DHT subsystem.
cadet service; dealing with DHT requests and results
struct GNUNET_MessageHeader * GCH_get_mine()
Get own hello message.
cadet service; dealing with hello messages
void GCPP_try_path_from_dht(const struct GNUNET_DHT_PathElement *get_path, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *put_path, unsigned int put_path_length)
Create a peer path based on the result of a DHT lookup.
struct CadetPeer * GCP_get(const struct GNUNET_PeerIdentity *peer_id, int create)
Retrieve the CadetPeer structure associated with the peer.
void GCP_set_hello(struct CadetPeer *cp, const struct GNUNET_MessageHeader *hello)
We got a HELLO for a cp, remember it, and possibly trigger adequate actions (like trying to connect).
const char * GCP_2s(const struct CadetPeer *cp)
Get the static string for a peer ID.
Information we track per peer.
static struct GNUNET_STATISTICS_Handle * stats
Handle to the statistics service.
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
@ GNUNET_BLOCK_TYPE_DHT_HELLO
Type of a block that contains a DHT-NG HELLO for a peer.
API to the DHT service.
API to create, modify and access statistics.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_time(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, struct GNUNET_TIME_Relative *time)
Get a configuration value that should be a relative time.
struct GNUNET_DHT_PutHandle * GNUNET_DHT_put(struct GNUNET_DHT_Handle *handle, const struct GNUNET_HashCode *key, uint32_t desired_replication_level, enum GNUNET_DHT_RouteOption options, enum GNUNET_BLOCK_Type type, size_t size, const void *data, struct GNUNET_TIME_Absolute exp, GNUNET_SCHEDULER_TaskCallback cont, void *cont_cls)
Perform a PUT operation storing data in the DHT.
Definition: dht_api.c:1081
struct GNUNET_DHT_Handle * GNUNET_DHT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int ht_len)
Initialize the connection with the DHT service.
Definition: dht_api.c:1030
void GNUNET_DHT_get_stop(struct GNUNET_DHT_GetHandle *get_handle)
Stop async DHT-get.
Definition: dht_api.c:1233
struct GNUNET_DHT_GetHandle * GNUNET_DHT_get_start(struct GNUNET_DHT_Handle *handle, enum GNUNET_BLOCK_Type type, const struct GNUNET_HashCode *key, uint32_t desired_replication_level, enum GNUNET_DHT_RouteOption options, const void *xquery, size_t xquery_size, GNUNET_DHT_GetIterator iter, void *iter_cls)
Perform an asynchronous GET operation on the DHT identified.
Definition: dht_api.c:1160
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition: dht_api.c:1053
@ GNUNET_DHT_RO_RECORD_ROUTE
We should keep track of the route that the message took in the P2P network.
@ GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE
Each peer along the way should process the request (otherwise only peers locally closest to the key w...
enum GNUNET_GenericReturnValue GNUNET_HELLO_dht_msg_to_block(const struct GNUNET_MessageHeader *hello, const struct GNUNET_PeerIdentity *pid, void **block, size_t *block_size, struct GNUNET_TIME_Absolute *block_expiration)
Convert a DHT hello message to a HELLO block.
Definition: hello-uri.c:997
struct GNUNET_TIME_Absolute GNUNET_HELLO_get_expiration_time_from_msg(const struct GNUNET_MessageHeader *msg)
Get the expiration time for this HELLO.
Definition: hello-uri.c:597
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#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_invalid(enum GNUNET_ErrorType kind, const char *section, const char *option, const char *required)
Log error message about invalid configuration option value.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
#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
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:980
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1277
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_min(struct GNUNET_TIME_Relative t1, struct GNUNET_TIME_Relative t2)
Return the minimum of two relative time values.
Definition: time.c:344
struct GNUNET_TIME_Relative GNUNET_TIME_relative_max(struct GNUNET_TIME_Relative t1, struct GNUNET_TIME_Relative t2)
Return the maximum of two relative time values.
Definition: time.c:352
#define GNUNET_TIME_UNIT_SECONDS
One second.
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:406
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Add a given relative duration to the given start time.
Definition: time.c:452
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
static unsigned int size
Size of the "table".
Definition: peer.c:68
Struct containing all information regarding a given peer.
struct GNUNET_MessageHeader * hello
Hello message of the peer.
struct GNUNET_DHT_GetHandle * dhtget
DHT_GET handle.
Handle to a GET request.
Definition: dht_api.c:79
Connection to the DHT service.
Definition: dht_api.c:235
A (signed) path tracking a block's flow through the DHT is represented by an array of path elements,...
A 512-bit hashcode.
Header for all communications.
The identity of the host (wraps the signing key of the peer).
Entry in list of pending tasks.
Definition: scheduler.c:136
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.