gnunet-service-xdht.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file dht/gnunet-service-xdht.c
  19. * @brief GNUnet DHT service
  20. * @author Christian Grothoff
  21. * @author Nathan Evans
  22. */
  23. #include "platform.h"
  24. #include "gnunet_block_lib.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_transport_service.h"
  27. #include "gnunet_hello_lib.h"
  28. #include "gnunet_dht_service.h"
  29. #include "gnunet_statistics_service.h"
  30. #include "gnunet-service-xdht.h"
  31. #include "gnunet-service-xdht_clients.h"
  32. #include "gnunet-service-xdht_datacache.h"
  33. #include "gnunet-service-xdht_hello.h"
  34. #include "gnunet-service-xdht_neighbours.h"
  35. #include "gnunet-service-xdht_nse.h"
  36. #include "gnunet-service-xdht_routing.h"
  37. /**
  38. * Handle for the statistics service.
  39. */
  40. struct GNUNET_STATISTICS_Handle *GDS_stats;
  41. /**
  42. * Our handle to the BLOCK library.
  43. */
  44. struct GNUNET_BLOCK_Context *GDS_block_context;
  45. /**
  46. * The configuration the DHT service is running with
  47. */
  48. const struct GNUNET_CONFIGURATION_Handle *GDS_cfg;
  49. /**
  50. * Our HELLO
  51. */
  52. struct GNUNET_MessageHeader *GDS_my_hello;
  53. /**
  54. * Handle to the transport service, for getting our hello
  55. */
  56. struct GNUNET_TRANSPORT_Handle *GDS_transport_handle;
  57. /**
  58. * Handle to get our current HELLO.
  59. */
  60. static struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
  61. /**
  62. * Hello address expiration
  63. */
  64. struct GNUNET_TIME_Relative hello_expiration;
  65. /**
  66. * Should we store our topology predecessor and successor IDs into statistics?
  67. */
  68. extern unsigned int track_topology;
  69. #if ENABLE_MALICIOUS
  70. /**
  71. * Should this peer act malicious?
  72. */
  73. unsigned int malicious;
  74. #endif
  75. /**
  76. * Receive the HELLO from transport service, free current and replace
  77. * if necessary.
  78. *
  79. * @param cls NULL
  80. * @param message HELLO message of peer
  81. */
  82. static void
  83. process_hello (void *cls, const struct GNUNET_MessageHeader *message)
  84. {
  85. GNUNET_assert (message != NULL);
  86. GNUNET_free_non_null (GDS_my_hello);
  87. GDS_my_hello = GNUNET_malloc (ntohs (message->size));
  88. memcpy (GDS_my_hello, message, ntohs (message->size));
  89. }
  90. /**
  91. * Task run during shutdown.
  92. *
  93. * @param cls unused
  94. * @param tc unused
  95. */
  96. static void
  97. shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  98. {
  99. if (NULL != ghh)
  100. {
  101. GNUNET_TRANSPORT_get_hello_cancel (ghh);
  102. ghh = NULL;
  103. }
  104. if (GDS_transport_handle != NULL)
  105. {
  106. GNUNET_TRANSPORT_disconnect (GDS_transport_handle);
  107. GDS_transport_handle = NULL;
  108. }
  109. GDS_NEIGHBOURS_done ();
  110. GDS_DATACACHE_done ();
  111. GDS_ROUTING_done ();
  112. GDS_HELLO_done ();
  113. GDS_NSE_done ();
  114. if (GDS_block_context != NULL)
  115. {
  116. GNUNET_BLOCK_context_destroy (GDS_block_context);
  117. GDS_block_context = NULL;
  118. }
  119. if (GDS_stats != NULL)
  120. {
  121. GNUNET_STATISTICS_destroy (GDS_stats, GNUNET_YES);
  122. GDS_stats = NULL;
  123. }
  124. GNUNET_free_non_null (GDS_my_hello);
  125. GDS_my_hello = NULL;
  126. }
  127. /**
  128. * Process dht requests.
  129. *
  130. * @param cls closure
  131. * @param server the initialized server
  132. * @param c configuration to use
  133. */
  134. static void
  135. run (void *cls, struct GNUNET_SERVER_Handle *server,
  136. const struct GNUNET_CONFIGURATION_Handle *c)
  137. {
  138. unsigned long long _track_topology;
  139. GDS_cfg = c;
  140. if (GNUNET_OK !=
  141. GNUNET_CONFIGURATION_get_value_time (c, "transport", "HELLO_EXPIRATION", &hello_expiration))
  142. {
  143. hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION;
  144. }
  145. GDS_block_context = GNUNET_BLOCK_context_create (GDS_cfg);
  146. GDS_stats = GNUNET_STATISTICS_create ("dht", GDS_cfg);
  147. GDS_ROUTING_init ();
  148. GDS_NSE_init ();
  149. GDS_DATACACHE_init ();
  150. GDS_HELLO_init ();
  151. GDS_CLIENTS_init (server);
  152. if (GNUNET_OK ==
  153. GNUNET_CONFIGURATION_get_value_number (c, "xdht", "track_toplogy",
  154. &_track_topology))
  155. {
  156. track_topology = (unsigned int) _track_topology;
  157. }
  158. if (GNUNET_OK != GDS_NEIGHBOURS_init ())
  159. {
  160. shutdown_task (NULL, NULL);
  161. return;
  162. }
  163. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
  164. NULL);
  165. GDS_transport_handle =
  166. GNUNET_TRANSPORT_connect (GDS_cfg, NULL, NULL, NULL, NULL, NULL);
  167. if (GDS_transport_handle == NULL)
  168. {
  169. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  170. _("Failed to connect to transport service!\n"));
  171. return;
  172. }
  173. ghh = GNUNET_TRANSPORT_get_hello (GDS_transport_handle, &process_hello, NULL);
  174. }
  175. /**
  176. * The main function for the dht service.
  177. *
  178. * @param argc number of arguments from the command line
  179. * @param argv command line arguments
  180. * @return 0 ok, 1 on error
  181. */
  182. int
  183. main (int argc, char *const *argv)
  184. {
  185. int ret;
  186. ret =
  187. (GNUNET_OK ==
  188. GNUNET_SERVICE_run (argc, argv, "dht", GNUNET_SERVICE_OPTION_NONE, &run,
  189. NULL)) ? 0 : 1;
  190. GDS_CLIENTS_done ();
  191. return ret;
  192. }
  193. /* end of gnunet-service-dht.c */