GNUnet  0.10.x
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_dht_service.h"
31 #include "gnunet-service-cadet.h"
36 
43 #define STARTUP_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
44 
50 #define CHANGE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
51 
52 
53 #define LOG(level, ...) GNUNET_log_from (level,"cadet-dht",__VA_ARGS__)
54 
55 
60 {
65 
66 };
67 
68 
73 
78 
82 static unsigned long long dht_replication_level;
83 
88 
93 
94 
111 static void
113  const struct GNUNET_HashCode *key,
114  const struct GNUNET_PeerIdentity *get_path,
115  unsigned int get_path_length,
116  const struct GNUNET_PeerIdentity *put_path,
117  unsigned int put_path_length,
118  enum GNUNET_BLOCK_Type type,
119  size_t size,
120  const void *data)
121 {
122  const struct GNUNET_HELLO_Message *hello = data;
123  struct CadetPeer *peer;
124 
125  GCPP_try_path_from_dht (get_path,
126  get_path_length,
127  put_path,
128  put_path_length);
129  if ( (size >= sizeof (struct GNUNET_HELLO_Message)) &&
130  (ntohs (hello->header.size) == size) &&
131  (size == GNUNET_HELLO_size (hello)) )
132  {
133  peer = GCP_get (&put_path[0],
134  GNUNET_YES);
136  "Got HELLO for %s\n",
137  GCP_2s (peer));
138  GCP_set_hello (peer,
139  hello);
140  }
141 }
142 
143 
149 static void
150 announce_id (void *cls)
151 {
152  struct GNUNET_HashCode phash;
153  const struct GNUNET_HELLO_Message *hello;
154  size_t size;
155  struct GNUNET_TIME_Absolute expiration;
156  struct GNUNET_TIME_Relative next_put;
157 
158  hello = GCH_get_mine ();
159  size = (NULL != hello) ? GNUNET_HELLO_size (hello) : 0;
160  if (0 == size)
161  {
165  }
166  else
167  {
168  expiration = GNUNET_HELLO_get_last_expiration (hello);
170  }
171 
172  /* Call again in id_announce_time, unless HELLO expires first,
173  * but wait at least 1s. */
174  next_put
175  = GNUNET_TIME_absolute_get_remaining (expiration);
176  next_put
177  = GNUNET_TIME_relative_min (next_put,
179  next_put
180  = GNUNET_TIME_relative_max (next_put,
182  announce_id_task
183  = GNUNET_SCHEDULER_add_delayed (next_put,
184  &announce_id,
185  cls);
187  "# DHT announce",
188  1,
189  GNUNET_NO);
190  memset (&phash,
191  0,
192  sizeof (phash));
193  GNUNET_memcpy (&phash,
194  &my_full_id,
195  sizeof (my_full_id));
197  "Announcing my HELLO (%u bytes) in the DHT\n",
198  size);
199  GNUNET_DHT_put (dht_handle, /* DHT handle */
200  &phash, /* Key to use */
201  dht_replication_level, /* Replication level */
203  | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
204  GNUNET_BLOCK_TYPE_DHT_HELLO, /* Block type */
205  size, /* Size of the data */
206  (const char *) hello, /* Data itself */
207  expiration, /* Data expiration */
208  NULL, /* Continuation */
209  NULL); /* Continuation closure */
210 }
211 
212 
217 void
219 {
220  if (NULL == announce_id_task)
221  return; /* too early */
222  GNUNET_SCHEDULER_cancel (announce_id_task);
223  announce_id_task
225  &announce_id,
226  NULL);
227 }
228 
229 
235 void
237 {
238  if (GNUNET_OK !=
240  "CADET",
241  "DHT_REPLICATION_LEVEL",
243  {
245  "CADET",
246  "DHT_REPLICATION_LEVEL",
247  "USING DEFAULT");
249  }
250 
251  if (GNUNET_OK !=
253  "CADET",
254  "ID_ANNOUNCE_TIME",
256  {
258  "CADET",
259  "ID_ANNOUNCE_TIME",
260  "MISSING");
262  return;
263  }
264 
265  dht_handle = GNUNET_DHT_connect (c,
266  64);
267  GNUNET_break (NULL != dht_handle);
269  announce_id_task = GNUNET_SCHEDULER_add_delayed (STARTUP_DELAY,
270  &announce_id,
271  NULL);
272 }
273 
274 
278 void
280 {
281  if (NULL != dht_handle)
282  {
283  GNUNET_DHT_disconnect (dht_handle);
284  dht_handle = NULL;
285  }
286  if (NULL != announce_id_task)
287  {
288  GNUNET_SCHEDULER_cancel (announce_id_task);
289  announce_id_task = NULL;
290  }
291 }
292 
293 
300 struct GCD_search_handle *
302 {
303  struct GNUNET_HashCode phash;
304  struct GCD_search_handle *h;
305 
307  "# DHT search",
308  1,
309  GNUNET_NO);
310  memset (&phash,
311  0,
312  sizeof (phash));
313  GNUNET_memcpy (&phash,
314  peer_id,
315  sizeof (*peer_id));
316 
317  h = GNUNET_new (struct GCD_search_handle);
318  h->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
319  GNUNET_BLOCK_TYPE_DHT_HELLO, /* type */
320  &phash, /* key to search */
321  dht_replication_level, /* replication level */
324  NULL, /* xquery */
325  0, /* xquery bits */
327  h);
329  "Starting DHT GET for peer %s (%p)\n",
330  GNUNET_i2s (peer_id),
331  h);
332  return h;
333 }
334 
335 
341 void
343 {
345  "Stopping DHT GET %p\n",
346  h);
348  GNUNET_free (h);
349 }
350 
351 /* end of gnunet-service-cadet_dht.c */
static struct GNUNET_STATISTICS_Handle * stats
Handle for statistics.
static struct GNUNET_SCHEDULER_Task * announce_id_task
Task to periodically announce itself in the network.
We should keep track of the route that the message took in the P2P network.
Peer description.
A HELLO message is used to exchange information about transports with other peers.
int 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.
int 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.
Handle for DHT searches.
void GCD_search_stop(struct GCD_search_handle *h)
Stop DHT search started with GCD_search().
GNUNET_BLOCK_Type
Blocks in the datastore and the datacache must have a unique type.
static struct GNUNET_TIME_Relative id_announce_time
How often to PUT own ID in the DHT.
void GCP_set_hello(struct CadetPeer *cp, const struct GNUNET_HELLO_Message *hello)
We got a HELLO for a peer, remember it, and possibly trigger adequate actions (like trying to connect...
#define LOG(level,...)
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:286
struct GNUNET_PeerIdentity my_full_id
Local peer own ID.
void GCD_shutdown(void)
Shut down the DHT subsystem.
#define GNUNET_TIME_UNIT_SECONDS
One second.
static struct GNUNET_DHT_Handle * dht_handle
Handle to use DHT.
static char * peer_id
Option –peer.
Definition: gnunet-cadet.c:42
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
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:393
#define GNUNET_new(type)
Allocate a struct or union of the given type.
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.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
void GCPP_try_path_from_dht(const struct GNUNET_PeerIdentity *get_path, unsigned int get_path_length, const struct GNUNET_PeerIdentity *put_path, unsigned int put_path_length)
Create a peer path based on the result of a DHT lookup.
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_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:524
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
Connection to the DHT service.
Definition: dht_api.c:205
const char * GCP_2s(const struct CadetPeer *cp)
Get the static string for a peer ID.
void GCD_hello_update()
Function called by the HELLO subsystem whenever OUR hello changes.
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:1246
Information we track per peer.
#define GNUNET_memcpy(dst, src, n)
const struct GNUNET_HELLO_Message * GCH_get_mine(void)
Get own hello message.
cadet service; dealing with hello messages
static void announce_id(void *cls)
Periodically announce self id in the DHT.
Type of a block that contains a HELLO for a peer (for DHT and CADET find-peer operations).
A 512-bit hashcode.
void GNUNET_DHT_get_stop(struct GNUNET_DHT_GetHandle *get_handle)
Stop async DHT-get.
Definition: dht_api.c:1160
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
static void dht_get_id_handler(void *cls, struct GNUNET_TIME_Absolute exp, const struct GNUNET_HashCode *key, const struct GNUNET_PeerIdentity *get_path, unsigned int get_path_length, const struct GNUNET_PeerIdentity *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.
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:271
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
struct GNUNET_HashCode key
The key used in the DHT.
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:971
static unsigned int size
Size of the "table".
Definition: peer.c:67
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:895
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition: dht_api.c:923
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
Handle to a GET request.
Definition: dht_api.c:80
struct GNUNET_DHT_GetHandle * dhtget
DHT_GET handle.
void GCD_init(const struct GNUNET_CONFIGURATION_Handle *c)
Initialize the DHT subsystem.
static struct GNUNET_TIME_Relative announce_delay
Delay for the next ID announce.
The identity of the host (wraps the signing key of the peer).
struct GNUNET_MessageHeader header
Type will be GNUNET_MESSAGE_TYPE_HELLO.
configuration data
Definition: configuration.c:85
struct CadetPeer * GCP_get(const struct GNUNET_PeerIdentity *peer_id, int create)
Retrieve the CadetPeer stucture associated with the peer.
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:1072
struct GCD_search_handle * GCD_search(const struct GNUNET_PeerIdentity *peer_id)
Search DHT for paths to peeR_id.
static unsigned long long dht_replication_level
DHT replication level, see DHT API: GNUNET_DHT_get_start(), GNUNET_DHT_put().
Entry in list of pending tasks.
Definition: scheduler.c:134
uint16_t GNUNET_HELLO_size(const struct GNUNET_HELLO_Message *hello)
Return the size of the given HELLO message.
Definition: hello.c:649
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:331
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:80
#define CHANGE_DELAY
How long do we wait after we get an updated HELLO before publishing? Allows for the HELLO to be updat...
cadet service; dealing with DHT requests and results
#define STARTUP_DELAY
How long do we wait before first announcing our presence to the DHT.
uint32_t data
The data value.
struct GNUNET_TIME_Absolute GNUNET_HELLO_get_last_expiration(const struct GNUNET_HELLO_Message *msg)
When does the last address in the given HELLO expire?
Definition: hello.c:890
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
Each peer along the way should look at &#39;enc&#39; (otherwise only the k-peers closest to the key should lo...
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
Information we track per peer.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965