gnunet-service-cadet_dht.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013, 2017 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file cadet/gnunet-service-cadet_dht.c
  18. * @brief Information we track per peer.
  19. * @author Bartlomiej Polot
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_dht_service.h"
  25. #include "gnunet_statistics_service.h"
  26. #include "gnunet-service-cadet.h"
  27. #include "gnunet-service-cadet_dht.h"
  28. #include "gnunet-service-cadet_hello.h"
  29. #include "gnunet-service-cadet_peer.h"
  30. #include "gnunet-service-cadet_paths.h"
  31. /**
  32. * How long do we wait before first announcing our presence to the DHT.
  33. * Used to wait for our HELLO to be available. Note that we also get
  34. * notifications when our HELLO is ready, so this is just the maximum
  35. * we wait for the first notification.
  36. */
  37. #define STARTUP_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500)
  38. /**
  39. * How long do we wait after we get an updated HELLO before publishing?
  40. * Allows for the HELLO to be updated again quickly, for example in
  41. * case multiple addresses changed and we got a partial update.
  42. */
  43. #define CHANGE_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100)
  44. #define LOG(level, ...) GNUNET_log_from(level, "cadet-dht", __VA_ARGS__)
  45. /**
  46. * Handle for DHT searches.
  47. */
  48. struct GCD_search_handle {
  49. /**
  50. * DHT_GET handle.
  51. */
  52. struct GNUNET_DHT_GetHandle *dhtget;
  53. };
  54. /**
  55. * Handle to use DHT.
  56. */
  57. static struct GNUNET_DHT_Handle *dht_handle;
  58. /**
  59. * How often to PUT own ID in the DHT.
  60. */
  61. static struct GNUNET_TIME_Relative id_announce_time;
  62. /**
  63. * DHT replication level, see DHT API: #GNUNET_DHT_get_start(), #GNUNET_DHT_put().
  64. */
  65. static unsigned long long dht_replication_level;
  66. /**
  67. * Task to periodically announce itself in the network.
  68. */
  69. static struct GNUNET_SCHEDULER_Task *announce_id_task;
  70. /**
  71. * Delay for the next ID announce.
  72. */
  73. static struct GNUNET_TIME_Relative announce_delay;
  74. /**
  75. * Function to process paths received for a new peer addition. The recorded
  76. * paths form the initial tunnel, which can be optimized later.
  77. * Called on each result obtained for the DHT search.
  78. *
  79. * @param cls closure
  80. * @param exp when will this value expire
  81. * @param key key of the result
  82. * @param get_path path of the get request
  83. * @param get_path_length lenght of @a get_path
  84. * @param put_path path of the put request
  85. * @param put_path_length length of the @a put_path
  86. * @param type type of the result
  87. * @param size number of bytes in data
  88. * @param data pointer to the result data
  89. */
  90. static void
  91. dht_get_id_handler(void *cls, struct GNUNET_TIME_Absolute exp,
  92. const struct GNUNET_HashCode *key,
  93. const struct GNUNET_PeerIdentity *get_path,
  94. unsigned int get_path_length,
  95. const struct GNUNET_PeerIdentity *put_path,
  96. unsigned int put_path_length,
  97. enum GNUNET_BLOCK_Type type,
  98. size_t size,
  99. const void *data)
  100. {
  101. const struct GNUNET_HELLO_Message *hello = data;
  102. struct CadetPeer *peer;
  103. GCPP_try_path_from_dht(get_path,
  104. get_path_length,
  105. put_path,
  106. put_path_length);
  107. if ((size >= sizeof(struct GNUNET_HELLO_Message)) &&
  108. (ntohs(hello->header.size) == size) &&
  109. (size == GNUNET_HELLO_size(hello)))
  110. {
  111. peer = GCP_get(&put_path[0],
  112. GNUNET_YES);
  113. LOG(GNUNET_ERROR_TYPE_DEBUG,
  114. "Got HELLO for %s\n",
  115. GCP_2s(peer));
  116. GCP_set_hello(peer,
  117. hello);
  118. }
  119. }
  120. /**
  121. * Periodically announce self id in the DHT
  122. *
  123. * @param cls closure
  124. */
  125. static void
  126. announce_id(void *cls)
  127. {
  128. struct GNUNET_HashCode phash;
  129. const struct GNUNET_HELLO_Message *hello;
  130. size_t size;
  131. struct GNUNET_TIME_Absolute expiration;
  132. struct GNUNET_TIME_Relative next_put;
  133. hello = GCH_get_mine();
  134. size = (NULL != hello) ? GNUNET_HELLO_size(hello) : 0;
  135. if (0 == size)
  136. {
  137. expiration = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(),
  138. announce_delay);
  139. announce_delay = GNUNET_TIME_STD_BACKOFF(announce_delay);
  140. }
  141. else
  142. {
  143. expiration = GNUNET_HELLO_get_last_expiration(hello);
  144. announce_delay = GNUNET_TIME_UNIT_SECONDS;
  145. }
  146. /* Call again in id_announce_time, unless HELLO expires first,
  147. * but wait at least 1s. */
  148. next_put
  149. = GNUNET_TIME_absolute_get_remaining(expiration);
  150. next_put
  151. = GNUNET_TIME_relative_min(next_put,
  152. id_announce_time);
  153. next_put
  154. = GNUNET_TIME_relative_max(next_put,
  155. GNUNET_TIME_UNIT_SECONDS);
  156. announce_id_task
  157. = GNUNET_SCHEDULER_add_delayed(next_put,
  158. &announce_id,
  159. cls);
  160. GNUNET_STATISTICS_update(stats,
  161. "# DHT announce",
  162. 1,
  163. GNUNET_NO);
  164. memset(&phash,
  165. 0,
  166. sizeof(phash));
  167. GNUNET_memcpy(&phash,
  168. &my_full_id,
  169. sizeof(my_full_id));
  170. LOG(GNUNET_ERROR_TYPE_DEBUG,
  171. "Announcing my HELLO (%u bytes) in the DHT\n",
  172. size);
  173. GNUNET_DHT_put(dht_handle, /* DHT handle */
  174. &phash, /* Key to use */
  175. dht_replication_level, /* Replication level */
  176. GNUNET_DHT_RO_RECORD_ROUTE
  177. | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
  178. GNUNET_BLOCK_TYPE_DHT_HELLO, /* Block type */
  179. size, /* Size of the data */
  180. (const char *)hello, /* Data itself */
  181. expiration, /* Data expiration */
  182. NULL, /* Continuation */
  183. NULL); /* Continuation closure */
  184. }
  185. /**
  186. * Function called by the HELLO subsystem whenever OUR hello
  187. * changes. Re-triggers the DHT PUT immediately.
  188. */
  189. void
  190. GCD_hello_update()
  191. {
  192. if (NULL == announce_id_task)
  193. return; /* too early */
  194. GNUNET_SCHEDULER_cancel(announce_id_task);
  195. announce_id_task
  196. = GNUNET_SCHEDULER_add_delayed(CHANGE_DELAY,
  197. &announce_id,
  198. NULL);
  199. }
  200. /**
  201. * Initialize the DHT subsystem.
  202. *
  203. * @param c Configuration.
  204. */
  205. void
  206. GCD_init(const struct GNUNET_CONFIGURATION_Handle *c)
  207. {
  208. if (GNUNET_OK !=
  209. GNUNET_CONFIGURATION_get_value_number(c,
  210. "CADET",
  211. "DHT_REPLICATION_LEVEL",
  212. &dht_replication_level))
  213. {
  214. GNUNET_log_config_invalid(GNUNET_ERROR_TYPE_WARNING,
  215. "CADET",
  216. "DHT_REPLICATION_LEVEL",
  217. "USING DEFAULT");
  218. dht_replication_level = 3;
  219. }
  220. if (GNUNET_OK !=
  221. GNUNET_CONFIGURATION_get_value_time(c,
  222. "CADET",
  223. "ID_ANNOUNCE_TIME",
  224. &id_announce_time))
  225. {
  226. GNUNET_log_config_invalid(GNUNET_ERROR_TYPE_ERROR,
  227. "CADET",
  228. "ID_ANNOUNCE_TIME",
  229. "MISSING");
  230. GNUNET_SCHEDULER_shutdown();
  231. return;
  232. }
  233. dht_handle = GNUNET_DHT_connect(c,
  234. 64);
  235. GNUNET_break(NULL != dht_handle);
  236. announce_delay = GNUNET_TIME_UNIT_SECONDS;
  237. announce_id_task = GNUNET_SCHEDULER_add_delayed(STARTUP_DELAY,
  238. &announce_id,
  239. NULL);
  240. }
  241. /**
  242. * Shut down the DHT subsystem.
  243. */
  244. void
  245. GCD_shutdown(void)
  246. {
  247. if (NULL != dht_handle)
  248. {
  249. GNUNET_DHT_disconnect(dht_handle);
  250. dht_handle = NULL;
  251. }
  252. if (NULL != announce_id_task)
  253. {
  254. GNUNET_SCHEDULER_cancel(announce_id_task);
  255. announce_id_task = NULL;
  256. }
  257. }
  258. /**
  259. * Search DHT for paths to @a peeR_id
  260. *
  261. * @param peer_id peer to search for
  262. * @return handle to abort search
  263. */
  264. struct GCD_search_handle *
  265. GCD_search(const struct GNUNET_PeerIdentity *peer_id)
  266. {
  267. struct GNUNET_HashCode phash;
  268. struct GCD_search_handle *h;
  269. GNUNET_STATISTICS_update(stats,
  270. "# DHT search",
  271. 1,
  272. GNUNET_NO);
  273. memset(&phash,
  274. 0,
  275. sizeof(phash));
  276. GNUNET_memcpy(&phash,
  277. peer_id,
  278. sizeof(*peer_id));
  279. h = GNUNET_new(struct GCD_search_handle);
  280. h->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
  281. GNUNET_BLOCK_TYPE_DHT_HELLO, /* type */
  282. &phash, /* key to search */
  283. dht_replication_level, /* replication level */
  284. GNUNET_DHT_RO_RECORD_ROUTE |
  285. GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
  286. NULL, /* xquery */
  287. 0, /* xquery bits */
  288. &dht_get_id_handler,
  289. h);
  290. LOG(GNUNET_ERROR_TYPE_DEBUG,
  291. "Starting DHT GET for peer %s (%p)\n",
  292. GNUNET_i2s(peer_id),
  293. h);
  294. return h;
  295. }
  296. /**
  297. * Stop DHT search started with #GCD_search().
  298. *
  299. * @param h handle to search to stop
  300. */
  301. void
  302. GCD_search_stop(struct GCD_search_handle *h)
  303. {
  304. LOG(GNUNET_ERROR_TYPE_DEBUG,
  305. "Stopping DHT GET %p\n",
  306. h);
  307. GNUNET_DHT_get_stop(h->dhtget);
  308. GNUNET_free(h);
  309. }
  310. /* end of gnunet-service-cadet_dht.c */