Add maximum message size
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_dht.c
1 /*
2      This file is part of GNUnet.
3      (C) 2013 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
19 */
20
21
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24
25 #include "gnunet_dht_service.h"
26 #include "gnunet_statistics_service.h"
27
28 #include "cadet_path.h"
29 #include "gnunet-service-cadet_dht.h"
30 #include "gnunet-service-cadet_peer.h"
31 #include "gnunet-service-cadet_hello.h"
32
33 #define LOG(level, ...) GNUNET_log_from (level,"cadet-dht",__VA_ARGS__)
34
35
36 /******************************************************************************/
37 /********************************   STRUCTS  **********************************/
38 /******************************************************************************/
39
40 /**
41  * Handle for DHT searches.
42  */
43 struct GCD_search_handle
44 {
45   /** DHT_GET handle. */
46   struct GNUNET_DHT_GetHandle *dhtget;
47
48   /** Provided callback to call when a path is found. */
49   GCD_search_callback callback;
50
51   /** Provided closure. */
52   void *cls;
53
54   /** Peer ID searched for */
55   GNUNET_PEER_Id peer_id;
56 };
57
58
59 /******************************************************************************/
60 /*******************************   GLOBALS  ***********************************/
61 /******************************************************************************/
62
63 /**
64  * Global handle to the statistics service.
65  */
66 extern struct GNUNET_STATISTICS_Handle *stats;
67
68 /**
69  * Own ID (short value).
70  */
71 extern GNUNET_PEER_Id myid;
72
73 /**
74  * Own ID (full value).
75  */
76 extern struct GNUNET_PeerIdentity my_full_id;
77
78 /**
79  * Handle to use DHT.
80  */
81 static struct GNUNET_DHT_Handle *dht_handle;
82
83 /**
84  * How often to PUT own ID in the DHT.
85  */
86 static struct GNUNET_TIME_Relative id_announce_time;
87
88 /**
89  * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
90  */
91 static unsigned long long dht_replication_level;
92
93 /**
94  * Task to periodically announce itself in the network.
95  */
96 static GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
97
98 /**
99  * GET requests to stop on shutdown.
100  */
101 static struct GNUNET_CONTAINER_MultiHashMap32 *get_requests;
102
103 /******************************************************************************/
104 /********************************   STATIC  ***********************************/
105 /******************************************************************************/
106
107
108 /**
109  * Build a PeerPath from the paths returned from the DHT, reversing the paths
110  * to obtain a local peer -> destination path and interning the peer ids.
111  *
112  * @return Newly allocated and created path
113  *
114  * FIXME refactor and use build_path_from_peer_ids
115  */
116 static struct CadetPeerPath *
117 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
118                      unsigned int get_path_length,
119                      const struct GNUNET_PeerIdentity *put_path,
120                      unsigned int put_path_length)
121 {
122   size_t size = get_path_length + put_path_length + 1;
123   struct GNUNET_PeerIdentity peers[size];
124   const struct GNUNET_PeerIdentity *peer;
125   struct CadetPeerPath *p;
126   unsigned int own_pos;
127   int i;
128
129   peers[0] = my_full_id;
130   LOG (GNUNET_ERROR_TYPE_DEBUG, "   GET has %d hops.\n", get_path_length);
131   for (i = 0 ; i < get_path_length; i++)
132   {
133     peer = &get_path[get_path_length - i - 1];
134     LOG (GNUNET_ERROR_TYPE_DEBUG, "   From GET: %s\n", GNUNET_i2s (peer));
135     peers[i + 1] = *peer;
136   }
137   for (i = 0 ; i < put_path_length; i++)
138   {
139     peer = &put_path[put_path_length - i - 1];
140     LOG (GNUNET_ERROR_TYPE_DEBUG, "   From PUT: %s\n", GNUNET_i2s (peer));
141     peers[i + get_path_length + 1] = *peer;
142   }
143   p = path_build_from_peer_ids (peers, size, myid, &own_pos);
144   return p;
145 }
146
147
148 /**
149  * Function to process paths received for a new peer addition. The recorded
150  * paths form the initial tunnel, which can be optimized later.
151  * Called on each result obtained for the DHT search.
152  *
153  * @param cls closure
154  * @param exp when will this value expire
155  * @param key key of the result
156  * @param get_path path of the get request
157  * @param get_path_length lenght of get_path
158  * @param put_path path of the put request
159  * @param put_path_length length of the put_path
160  * @param type type of the result
161  * @param size number of bytes in data
162  * @param data pointer to the result data
163  */
164 static void
165 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
166                     const struct GNUNET_HashCode * key,
167                     const struct GNUNET_PeerIdentity *get_path,
168                     unsigned int get_path_length,
169                     const struct GNUNET_PeerIdentity *put_path,
170                     unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
171                     size_t size, const void *data)
172 {
173   struct GCD_search_handle *h = cls;
174   struct GNUNET_HELLO_Message *hello;
175   struct CadetPeerPath *p;
176   struct CadetPeer *peer;
177   char *s;
178
179   p = path_build_from_dht (get_path, get_path_length,
180                            put_path, put_path_length);
181   if (NULL == p)
182   {
183     GNUNET_break_op (0);
184     return;
185   }
186
187   s = path_2s (p);
188   LOG (GNUNET_ERROR_TYPE_INFO, "Got path from DHT: %s\n", s);
189   GNUNET_free_non_null (s);
190   peer = GCP_get_short (p->peers[p->length - 1]);
191   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got HELLO for %s\n", GCP_2s (peer));
192   h->callback (h->cls, p);
193   path_destroy (p);
194   hello = (struct GNUNET_HELLO_Message *) data;
195   GCP_set_hello (peer, hello);
196   GCP_try_connect (peer);
197 }
198
199
200 /**
201  * Periodically announce self id in the DHT
202  *
203  * @param cls closure
204  * @param tc task context
205  */
206 static void
207 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
208 {
209   struct GNUNET_HashCode phash;
210   const struct GNUNET_HELLO_Message *hello;
211   size_t size;
212   struct GNUNET_TIME_Absolute expiration;
213   struct GNUNET_TIME_Relative retry_time;
214
215   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
216   {
217     announce_id_task = GNUNET_SCHEDULER_NO_TASK;
218     return;
219   }
220   LOG (GNUNET_ERROR_TYPE_DEBUG, "Announce ID\n");
221
222   /* TODO
223    * - Set data expiration in function of X
224    * - Adapt X to churn
225    */
226   hello = GCH_get_mine ();
227   if (NULL == hello || (size = GNUNET_HELLO_size (hello)) == 0)
228   {
229     /* Peerinfo gave us no hello yet, try again in a second. */
230     announce_id_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
231                                                      &announce_id, cls);
232     LOG (GNUNET_ERROR_TYPE_DEBUG, "  no hello, waiting!\n");
233     return;
234   }
235   expiration = GNUNET_HELLO_get_last_expiration (hello);
236   retry_time = GNUNET_TIME_absolute_get_remaining (expiration);
237
238   LOG (GNUNET_ERROR_TYPE_DEBUG, "Hello %p size: %u\n", hello, size);
239   memset (&phash, 0, sizeof (phash));
240   memcpy (&phash, &my_full_id, sizeof (my_full_id));
241   GNUNET_DHT_put (dht_handle,   /* DHT handle */
242                   &phash,       /* Key to use */
243                   dht_replication_level,     /* Replication level */
244                   GNUNET_DHT_RO_RECORD_ROUTE
245                   | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,    /* DHT options */
246                   GNUNET_BLOCK_TYPE_DHT_HELLO,       /* Block type */
247                   size,  /* Size of the data */
248                   (const char *) hello, /* Data itself */
249                   expiration,  /* Data expiration */
250                   retry_time, /* Retry time */
251                   NULL,         /* Continuation */
252                   NULL);        /* Continuation closure */
253   announce_id_task =
254       GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
255 }
256
257 /**
258  * Iterator over hash map entries and stop GET requests before disconnecting
259  * from the DHT.
260  *
261  * @param cls Closure (unused)
262  * @param key Current peer ID.
263  * @param value Value in the hash map (GCD_search_handle).
264  *
265  * @return #GNUNET_YES, we should continue to iterate,
266  */
267 int
268 stop_get (void *cls,
269           uint32_t key,
270           void *value)
271 {
272   struct GCD_search_handle *h = value;
273
274   GCD_search_stop (h);
275   return GNUNET_YES;
276 }
277
278
279 /******************************************************************************/
280 /********************************    API    ***********************************/
281 /******************************************************************************/
282
283 /**
284  * Initialize the DHT subsystem.
285  *
286  * @param c Configuration.
287  */
288 void
289 GCD_init (const struct GNUNET_CONFIGURATION_Handle *c)
290 {
291   LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
292   if (GNUNET_OK !=
293       GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DHT_REPLICATION_LEVEL",
294                                              &dht_replication_level))
295   {
296     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
297                                "CADET", "DHT_REPLICATION_LEVEL", "USING DEFAULT");
298     dht_replication_level = 3;
299   }
300
301   if (GNUNET_OK !=
302       GNUNET_CONFIGURATION_get_value_time (c, "CADET", "ID_ANNOUNCE_TIME",
303                                            &id_announce_time))
304   {
305     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
306                                "CADET", "ID_ANNOUNCE_TIME", "MISSING");
307     GNUNET_SCHEDULER_shutdown ();
308     return;
309   }
310
311   dht_handle = GNUNET_DHT_connect (c, 64);
312   if (NULL == dht_handle)
313   {
314     GNUNET_break (0);
315   }
316
317   announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, NULL);
318   get_requests = GNUNET_CONTAINER_multihashmap32_create (32);
319 }
320
321
322 /**
323  * Shut down the DHT subsystem.
324  */
325 void
326 GCD_shutdown (void)
327 {
328   GNUNET_CONTAINER_multihashmap32_iterate (get_requests, &stop_get, NULL);
329   GNUNET_CONTAINER_multihashmap32_destroy (get_requests);
330   if (dht_handle != NULL)
331   {
332     GNUNET_DHT_disconnect (dht_handle);
333     dht_handle = NULL;
334   }
335   if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
336   {
337     GNUNET_SCHEDULER_cancel (announce_id_task);
338     announce_id_task = GNUNET_SCHEDULER_NO_TASK;
339   }
340 }
341
342 struct GCD_search_handle *
343 GCD_search (const struct GNUNET_PeerIdentity *peer_id,
344             GCD_search_callback callback, void *cls)
345 {
346   struct GNUNET_HashCode phash;
347   struct GCD_search_handle *h;
348
349   LOG (GNUNET_ERROR_TYPE_DEBUG,
350        "  Starting DHT GET for peer %s\n", GNUNET_i2s (peer_id));
351   memset (&phash, 0, sizeof (phash));
352   memcpy (&phash, peer_id, sizeof (*peer_id));
353   h = GNUNET_new (struct GCD_search_handle);
354   h->peer_id = GNUNET_PEER_intern (peer_id);
355   h->callback = callback;
356   h->cls = cls;
357   h->dhtget = GNUNET_DHT_get_start (dht_handle,    /* handle */
358                                     GNUNET_BLOCK_TYPE_DHT_HELLO, /* type */
359                                     &phash,     /* key to search */
360                                     dht_replication_level, /* replication level */
361                                     GNUNET_DHT_RO_RECORD_ROUTE |
362                                     GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
363                                     NULL,       /* xquery */
364                                     0,     /* xquery bits */
365                                     &dht_get_id_handler, h);
366   GNUNET_CONTAINER_multihashmap32_put (get_requests, h->peer_id, h,
367                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
368   return h;
369 }
370
371 void
372 GCD_search_stop (struct GCD_search_handle *h)
373 {
374   GNUNET_break (GNUNET_OK ==
375                 GNUNET_CONTAINER_multihashmap32_remove (get_requests,
376                                                         h->peer_id, h));
377   GNUNET_DHT_get_stop (h->dhtget);
378   GNUNET_free (h);
379 }