gnunet-service-dht.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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-dht.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-dht.h"
  31. #include "gnunet-service-dht_clients.h"
  32. #include "gnunet-service-dht_datacache.h"
  33. #include "gnunet-service-dht_hello.h"
  34. #include "gnunet-service-dht_neighbours.h"
  35. #include "gnunet-service-dht_nse.h"
  36. #include "gnunet-service-dht_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. * Receive the HELLO from transport service, free current and replace
  67. * if necessary.
  68. *
  69. * @param cls NULL
  70. * @param message HELLO message of peer
  71. */
  72. static void
  73. process_hello (void *cls, const struct GNUNET_MessageHeader *message)
  74. {
  75. GNUNET_assert (message != NULL);
  76. GNUNET_free_non_null (GDS_my_hello);
  77. GDS_my_hello = GNUNET_malloc (ntohs (message->size));
  78. memcpy (GDS_my_hello, message, ntohs (message->size));
  79. }
  80. /**
  81. * Task run during shutdown.
  82. *
  83. * @param cls unused
  84. * @param tc unused
  85. */
  86. static void
  87. shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  88. {
  89. if (NULL != ghh)
  90. {
  91. GNUNET_TRANSPORT_get_hello_cancel (ghh);
  92. ghh = NULL;
  93. }
  94. if (GDS_transport_handle != NULL)
  95. {
  96. GNUNET_TRANSPORT_disconnect (GDS_transport_handle);
  97. GDS_transport_handle = NULL;
  98. }
  99. GDS_NEIGHBOURS_done ();
  100. GDS_DATACACHE_done ();
  101. GDS_ROUTING_done ();
  102. GDS_HELLO_done ();
  103. GDS_NSE_done ();
  104. if (GDS_block_context != NULL)
  105. {
  106. GNUNET_BLOCK_context_destroy (GDS_block_context);
  107. GDS_block_context = NULL;
  108. }
  109. if (GDS_stats != NULL)
  110. {
  111. GNUNET_STATISTICS_destroy (GDS_stats, GNUNET_YES);
  112. GDS_stats = NULL;
  113. }
  114. GNUNET_free_non_null (GDS_my_hello);
  115. GDS_my_hello = NULL;
  116. }
  117. /**
  118. * Process dht requests.
  119. *
  120. * @param cls closure
  121. * @param server the initialized server
  122. * @param c configuration to use
  123. */
  124. static void
  125. run (void *cls, struct GNUNET_SERVER_Handle *server,
  126. const struct GNUNET_CONFIGURATION_Handle *c)
  127. {
  128. GDS_cfg = c;
  129. if (GNUNET_OK !=
  130. GNUNET_CONFIGURATION_get_value_time (c, "transport", "HELLO_EXPIRATION", &hello_expiration))
  131. {
  132. hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION;
  133. }
  134. GDS_block_context = GNUNET_BLOCK_context_create (GDS_cfg);
  135. GDS_stats = GNUNET_STATISTICS_create ("dht", GDS_cfg);
  136. GDS_ROUTING_init ();
  137. GDS_NSE_init ();
  138. GDS_DATACACHE_init ();
  139. GDS_HELLO_init ();
  140. GDS_CLIENTS_init (server);
  141. if (GNUNET_OK != GDS_NEIGHBOURS_init ())
  142. {
  143. shutdown_task (NULL, NULL);
  144. return;
  145. }
  146. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
  147. NULL);
  148. GDS_transport_handle =
  149. GNUNET_TRANSPORT_connect (GDS_cfg, NULL, NULL, NULL, NULL, NULL);
  150. if (GDS_transport_handle == NULL)
  151. {
  152. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  153. _("Failed to connect to transport service!\n"));
  154. return;
  155. }
  156. ghh = GNUNET_TRANSPORT_get_hello (GDS_transport_handle, &process_hello, NULL);
  157. }
  158. /**
  159. * The main function for the dht service.
  160. *
  161. * @param argc number of arguments from the command line
  162. * @param argv command line arguments
  163. * @return 0 ok, 1 on error
  164. */
  165. int
  166. main (int argc, char *const *argv)
  167. {
  168. int ret;
  169. ret =
  170. (GNUNET_OK ==
  171. GNUNET_SERVICE_run (argc, argv, "dht", GNUNET_SERVICE_OPTION_NONE, &run,
  172. NULL)) ? 0 : 1;
  173. GDS_CLIENTS_done ();
  174. return ret;
  175. }
  176. /* end of gnunet-service-dht.c */