gnunet-daemon-hostlist.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2007, 2008, 2009, 2014 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 hostlist/gnunet-daemon-hostlist.c
  19. * @brief code for bootstrapping via hostlist servers
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet-daemon-hostlist_client.h"
  24. #include "gnunet_core_service.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_protocols.h"
  27. #include "gnunet_statistics_service.h"
  28. #if HAVE_MHD
  29. #include "gnunet-daemon-hostlist_server.h"
  30. /**
  31. * Set if we are allowed to advertise our hostlist to others.
  32. */
  33. static int advertising;
  34. /**
  35. * Set if the user wants us to run a hostlist server.
  36. */
  37. static int provide_hostlist;
  38. /**
  39. * Handle to hostlist server's connect handler
  40. */
  41. static GNUNET_CORE_ConnectEventHandler server_ch;
  42. /**
  43. * Handle to hostlist server's disconnect handler
  44. */
  45. static GNUNET_CORE_DisconnectEventHandler server_dh;
  46. #endif
  47. /**
  48. * Set if we are allowed to learn about peers by accessing
  49. * hostlist servers.
  50. */
  51. static int bootstrapping;
  52. /**
  53. * Set if the user allows us to learn about new hostlists
  54. * from the network.
  55. */
  56. static int learning;
  57. /**
  58. * Statistics handle.
  59. */
  60. static struct GNUNET_STATISTICS_Handle *stats;
  61. /**
  62. * Handle to the core service (NULL until we've connected to it).
  63. */
  64. static struct GNUNET_CORE_Handle *core;
  65. /**
  66. * Handle to the hostlist client's advertisement handler
  67. */
  68. static GNUNET_CORE_MessageCallback client_adv_handler;
  69. /**
  70. * Handle to hostlist client's connect handler
  71. */
  72. static GNUNET_CORE_ConnectEventHandler client_ch;
  73. /**
  74. * Handle to hostlist client's disconnect handler
  75. */
  76. static GNUNET_CORE_DisconnectEventHandler client_dh;
  77. GNUNET_NETWORK_STRUCT_BEGIN
  78. /**
  79. * A HOSTLIST_ADV message is used to exchange information about
  80. * hostlist advertisements. This struct is always
  81. * followed by the actual url under which the hostlist can be obtained:
  82. *
  83. * 1) transport-name (0-terminated)
  84. * 2) address-length (uint32_t, network byte order; possibly
  85. * unaligned!)
  86. * 3) address expiration (GNUNET_TIME_AbsoluteNBO); possibly
  87. * unaligned!)
  88. * 4) address (address-length bytes; possibly unaligned!)
  89. */
  90. struct GNUNET_HOSTLIST_ADV_Message
  91. {
  92. /**
  93. * Type will be GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT.
  94. */
  95. struct GNUNET_MessageHeader header;
  96. /**
  97. * Always zero (for alignment).
  98. */
  99. uint32_t reserved GNUNET_PACKED;
  100. };
  101. GNUNET_NETWORK_STRUCT_END
  102. /**
  103. * Our own peer identity.
  104. */
  105. static struct GNUNET_PeerIdentity me;
  106. /**
  107. * Callback invoked once our connection to CORE service is up.
  108. *
  109. * @param cls NULL
  110. * @param my_identity our peer's identity
  111. */
  112. static void
  113. core_init (void *cls,
  114. const struct GNUNET_PeerIdentity *my_identity)
  115. {
  116. me = *my_identity;
  117. }
  118. /**
  119. * Core handler for p2p hostlist advertisements
  120. *
  121. * @param cls closure
  122. * @param peer identity of the sender
  123. * @param message advertisement message we got
  124. * @return #GNUNET_OK on success
  125. */
  126. static int
  127. advertisement_handler (void *cls,
  128. const struct GNUNET_PeerIdentity *peer,
  129. const struct GNUNET_MessageHeader *message)
  130. {
  131. GNUNET_assert (NULL != client_adv_handler);
  132. return (*client_adv_handler) (cls, peer, message);
  133. }
  134. /**
  135. * Method called whenever a given peer connects. Wrapper to call both
  136. * client's and server's functions
  137. *
  138. * @param cls closure
  139. * @param peer peer identity this notification is about
  140. */
  141. static void
  142. connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
  143. {
  144. if (0 == memcmp (&me, peer, sizeof (struct GNUNET_PeerIdentity)))
  145. return;
  146. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  147. "A new peer connected, notifying client and server\n");
  148. if (NULL != client_ch)
  149. (*client_ch) (cls, peer);
  150. #if HAVE_MHD
  151. if (NULL != server_ch)
  152. (*server_ch) (cls, peer);
  153. #endif
  154. }
  155. /**
  156. * Method called whenever a given peer disconnects. Wrapper to call
  157. * both client's and server's functions
  158. *
  159. * @param cls closure
  160. * @param peer peer identity this notification is about
  161. */
  162. static void
  163. disconnect_handler (void *cls,
  164. const struct GNUNET_PeerIdentity *peer)
  165. {
  166. if (0 == memcmp (&me, peer, sizeof (struct GNUNET_PeerIdentity)))
  167. return;
  168. /* call hostlist client disconnect handler */
  169. if (NULL != client_dh)
  170. (*client_dh) (cls, peer);
  171. #if HAVE_MHD
  172. /* call hostlist server disconnect handler */
  173. if (NULL != server_dh)
  174. (*server_dh) (cls, peer);
  175. #endif
  176. }
  177. /**
  178. * Last task run during shutdown. Disconnects us from
  179. * the other services.
  180. *
  181. * @param cls NULL
  182. * @param tc scheduler context
  183. */
  184. static void
  185. cleaning_task (void *cls,
  186. const struct GNUNET_SCHEDULER_TaskContext *tc)
  187. {
  188. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  189. "Hostlist daemon is shutting down\n");
  190. if (NULL != core)
  191. {
  192. GNUNET_CORE_disconnect (core);
  193. core = NULL;
  194. }
  195. if (bootstrapping)
  196. {
  197. GNUNET_HOSTLIST_client_stop ();
  198. }
  199. #if HAVE_MHD
  200. if (provide_hostlist)
  201. {
  202. GNUNET_HOSTLIST_server_stop ();
  203. }
  204. #endif
  205. if (NULL != stats)
  206. {
  207. GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
  208. stats = NULL;
  209. }
  210. }
  211. /**
  212. * Main function that will be run.
  213. *
  214. * @param cls closure
  215. * @param args remaining command-line arguments
  216. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  217. * @param cfg configuration
  218. */
  219. static void
  220. run (void *cls,
  221. char *const *args,
  222. const char *cfgfile,
  223. const struct GNUNET_CONFIGURATION_Handle *cfg)
  224. {
  225. static const struct GNUNET_CORE_MessageHandler learn_handlers[] = {
  226. {&advertisement_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
  227. {NULL, 0, 0}
  228. };
  229. static const struct GNUNET_CORE_MessageHandler no_learn_handlers[] = {
  230. {NULL, 0, 0}
  231. };
  232. if ((! bootstrapping) && (! learning)
  233. #if HAVE_MHD
  234. && (! provide_hostlist)
  235. #endif
  236. )
  237. {
  238. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  239. _("None of the functions for the hostlist daemon were enabled. I have no reason to run!\n"));
  240. return;
  241. }
  242. stats = GNUNET_STATISTICS_create ("hostlist", cfg);
  243. if (bootstrapping)
  244. GNUNET_HOSTLIST_client_start (cfg, stats, &client_ch, &client_dh,
  245. &client_adv_handler, learning);
  246. core =
  247. GNUNET_CORE_connect (cfg, NULL,
  248. &core_init,
  249. &connect_handler,
  250. &disconnect_handler, NULL,
  251. GNUNET_NO, NULL,
  252. GNUNET_NO,
  253. learning ? learn_handlers : no_learn_handlers);
  254. #if HAVE_MHD
  255. if (provide_hostlist)
  256. GNUNET_HOSTLIST_server_start (cfg, stats, core, &server_ch, &server_dh,
  257. advertising);
  258. #endif
  259. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
  260. &cleaning_task,
  261. NULL);
  262. if (NULL == core)
  263. {
  264. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  265. _("Failed to connect to `%s' service.\n"), "core");
  266. GNUNET_SCHEDULER_shutdown ();
  267. return;
  268. }
  269. }
  270. /**
  271. * The main function for the hostlist daemon.
  272. *
  273. * @param argc number of arguments from the command line
  274. * @param argv command line arguments
  275. * @return 0 ok, 1 on error
  276. */
  277. int
  278. main (int argc, char *const *argv)
  279. {
  280. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  281. #if HAVE_MHD
  282. {'a', "advertise", NULL,
  283. gettext_noop ("advertise our hostlist to other peers"),
  284. GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising},
  285. #endif
  286. {'b', "bootstrap", NULL,
  287. gettext_noop
  288. ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
  289. GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping},
  290. {'e', "enable-learning", NULL,
  291. gettext_noop ("enable learning about hostlist servers from other peers"),
  292. GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
  293. #if HAVE_MHD
  294. {'p', "provide-hostlist", NULL,
  295. gettext_noop ("provide a hostlist server"),
  296. GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
  297. #endif
  298. GNUNET_GETOPT_OPTION_END
  299. };
  300. int ret;
  301. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  302. return 2;
  303. GNUNET_log_setup ("hostlist", "WARNING", NULL);
  304. ret =
  305. (GNUNET_OK ==
  306. GNUNET_PROGRAM_run (argc, argv, "hostlist",
  307. _("GNUnet hostlist server and client"),
  308. options,
  309. &run, NULL)) ? 0 : 1;
  310. GNUNET_free ((void*) argv);
  311. return ret;
  312. }
  313. /* end of gnunet-daemon-hostlist.c */