gnunet-service-transport_plugins.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-2014 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 transport/gnunet-service-transport_plugins.c
  18. * @brief plugin management
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet-service-transport.h"
  23. #include "gnunet-service-transport_hello.h"
  24. #include "gnunet-service-transport_ats.h"
  25. #include "gnunet-service-transport_plugins.h"
  26. /**
  27. * Entry in doubly-linked list of all of our plugins.
  28. */
  29. struct TransportPlugin
  30. {
  31. /**
  32. * This is a doubly-linked list.
  33. */
  34. struct TransportPlugin *next;
  35. /**
  36. * This is a doubly-linked list.
  37. */
  38. struct TransportPlugin *prev;
  39. /**
  40. * API of the transport as returned by the plugin's
  41. * initialization function.
  42. */
  43. struct GNUNET_TRANSPORT_PluginFunctions *api;
  44. /**
  45. * Short name for the plugin (e.g. "tcp").
  46. */
  47. char *short_name;
  48. /**
  49. * Name of the library (e.g. "gnunet_plugin_transport_tcp").
  50. */
  51. char *lib_name;
  52. /**
  53. * Environment this transport service is using
  54. * for this plugin.
  55. */
  56. struct GNUNET_TRANSPORT_PluginEnvironment env;
  57. };
  58. /**
  59. * Head of DLL of all loaded plugins.
  60. */
  61. static struct TransportPlugin *plugins_head;
  62. /**
  63. * Head of DLL of all loaded plugins.
  64. */
  65. static struct TransportPlugin *plugins_tail;
  66. /**
  67. * Function that will be called to update metrics for an address
  68. *
  69. * @param cls closure
  70. * @param address address to update metrics for
  71. * @param session the session
  72. * @param distance new distance
  73. */
  74. static void
  75. plugin_env_update_distance (void *cls,
  76. const struct GNUNET_HELLO_Address *address,
  77. uint32_t distance)
  78. {
  79. GST_ats_update_distance (address,
  80. distance);
  81. }
  82. /**
  83. * Function that will be called to figure if an address is an loopback,
  84. * LAN, WAN etc. address
  85. *
  86. * @param cls closure
  87. * @param addr binary address
  88. * @param addrlen length of the @a addr
  89. * @return type of the network @a addr belongs to
  90. */
  91. static enum GNUNET_NetworkType
  92. plugin_env_address_to_type (void *cls,
  93. const struct sockaddr *addr,
  94. size_t addrlen)
  95. {
  96. if (NULL == GST_is)
  97. {
  98. GNUNET_break (0);
  99. return GNUNET_NT_UNSPECIFIED;
  100. }
  101. return GNUNET_NT_scanner_get_type (GST_is,
  102. addr,
  103. addrlen);
  104. }
  105. /**
  106. * Load and initialize all plugins. The respective functions will be
  107. * invoked by the plugins when the respective events happen. The
  108. * closure will be set to a 'const char*' containing the name of the
  109. * plugin that caused the call.
  110. *
  111. * @param recv_cb function to call when data is received
  112. * @param address_cb function to call when our public addresses changed
  113. * @param session_start_cb function to call when a session was created
  114. * @param session_end_cb function to call when a session was terminated
  115. * @param address_type_cb function to call when a address type is requested
  116. */
  117. void
  118. GST_plugins_load (GNUNET_TRANSPORT_PluginReceiveCallback recv_cb,
  119. GNUNET_TRANSPORT_AddressNotification address_cb,
  120. GNUNET_TRANSPORT_SessionStart session_start_cb,
  121. GNUNET_TRANSPORT_SessionEnd session_end_cb)
  122. {
  123. struct TransportPlugin *plug;
  124. struct TransportPlugin *next;
  125. unsigned long long tneigh;
  126. char *libname;
  127. char *plugs;
  128. char *pos;
  129. int fail;
  130. if (GNUNET_OK !=
  131. GNUNET_CONFIGURATION_get_value_number (GST_cfg,
  132. "TRANSPORT",
  133. "NEIGHBOUR_LIMIT",
  134. &tneigh))
  135. {
  136. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  137. _ ("Transport service is lacking NEIGHBOUR_LIMIT option.\n"));
  138. return;
  139. }
  140. if (GNUNET_OK !=
  141. GNUNET_CONFIGURATION_get_value_string (GST_cfg,
  142. "TRANSPORT",
  143. "PLUGINS",
  144. &plugs))
  145. return;
  146. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  147. _ ("Starting transport plugins `%s'\n"),
  148. plugs);
  149. for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
  150. {
  151. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  152. _ ("Loading `%s' transport plugin\n"),
  153. pos);
  154. GNUNET_asprintf (&libname,
  155. "libgnunet_plugin_transport_%s",
  156. pos);
  157. plug = GNUNET_new (struct TransportPlugin);
  158. plug->short_name = GNUNET_strdup (pos);
  159. plug->lib_name = libname;
  160. plug->env.cfg = GST_cfg;
  161. plug->env.my_identity = &GST_my_identity;
  162. plug->env.get_our_hello = &GST_hello_get;
  163. plug->env.cls = plug->short_name;
  164. plug->env.receive = recv_cb;
  165. plug->env.notify_address = address_cb;
  166. plug->env.session_start = session_start_cb;
  167. plug->env.session_end = session_end_cb;
  168. plug->env.get_address_type = &plugin_env_address_to_type;
  169. plug->env.update_address_distance = &plugin_env_update_distance;
  170. plug->env.max_connections = tneigh;
  171. plug->env.stats = GST_stats;
  172. GNUNET_CONTAINER_DLL_insert (plugins_head,
  173. plugins_tail,
  174. plug);
  175. }
  176. GNUNET_free (plugs);
  177. next = plugins_head;
  178. while (NULL != next)
  179. {
  180. plug = next;
  181. next = plug->next;
  182. plug->api = GNUNET_PLUGIN_load (plug->lib_name,
  183. &plug->env);
  184. if (NULL == plug->api)
  185. {
  186. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  187. _ ("Failed to load transport plugin for `%s'\n"),
  188. plug->lib_name);
  189. GNUNET_CONTAINER_DLL_remove (plugins_head,
  190. plugins_tail,
  191. plug);
  192. GNUNET_free (plug->short_name);
  193. GNUNET_free (plug->lib_name);
  194. GNUNET_free (plug);
  195. continue;
  196. }
  197. fail = GNUNET_NO;
  198. if (NULL == plug->api->address_pretty_printer)
  199. {
  200. fail = GNUNET_YES;
  201. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  202. _ ("Missing function `%s' in transport plugin for `%s'\n"),
  203. "address_pretty_printer",
  204. plug->lib_name);
  205. }
  206. if (NULL == plug->api->address_to_string)
  207. {
  208. fail = GNUNET_YES;
  209. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  210. _ ("Missing function `%s' in transport plugin for `%s'\n"),
  211. "address_to_string",
  212. plug->lib_name);
  213. }
  214. if (NULL == plug->api->string_to_address)
  215. {
  216. fail = GNUNET_YES;
  217. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  218. _ ("Missing function `%s' in transport plugin for `%s'\n"),
  219. "string_to_address",
  220. plug->lib_name);
  221. }
  222. if (NULL == plug->api->check_address)
  223. {
  224. fail = GNUNET_YES;
  225. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  226. _ ("Missing function `%s' in transport plugin for `%s'\n"),
  227. "check_address",
  228. plug->lib_name);
  229. }
  230. if (NULL == plug->api->get_session)
  231. {
  232. fail = GNUNET_YES;
  233. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  234. _ ("Missing function `%s' in transport plugin for `%s'\n"),
  235. "get_session",
  236. plug->lib_name);
  237. }
  238. if (NULL == plug->api->get_network)
  239. {
  240. fail = GNUNET_YES;
  241. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  242. _ ("Missing function `%s' in transport plugin for `%s'\n"),
  243. "get_network",
  244. plug->lib_name);
  245. }
  246. if (NULL == plug->api->send)
  247. {
  248. fail = GNUNET_YES;
  249. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  250. _ ("Missing function `%s' in transport plugin for `%s'\n"),
  251. "send",
  252. plug->lib_name);
  253. }
  254. if (NULL == plug->api->disconnect_peer)
  255. {
  256. fail = GNUNET_YES;
  257. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  258. _ ("Missing function `%s' in transport plugin for `%s'\n"),
  259. "disconnect_peer",
  260. plug->lib_name);
  261. }
  262. if (NULL == plug->api->disconnect_session)
  263. {
  264. fail = GNUNET_YES;
  265. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  266. _ ("Missing function `%s' in transport plugin for `%s'\n"),
  267. "disconnect_session",
  268. plug->lib_name);
  269. }
  270. if (NULL == plug->api->query_keepalive_factor)
  271. {
  272. fail = GNUNET_YES;
  273. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  274. _ ("Missing function `%s' in transport plugin for `%s'\n"),
  275. "query_keepalive_factor",
  276. plug->lib_name);
  277. }
  278. if (NULL == plug->api->update_session_timeout)
  279. {
  280. fail = GNUNET_YES;
  281. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  282. _ ("Missing function `%s' in transport plugin for `%s'\n"),
  283. "update_session_timeout",
  284. plug->lib_name);
  285. }
  286. if (GNUNET_YES == fail)
  287. {
  288. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  289. _ ("Did not load plugin `%s' due to missing functions\n"),
  290. plug->lib_name);
  291. GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
  292. GNUNET_CONTAINER_DLL_remove (plugins_head,
  293. plugins_tail,
  294. plug);
  295. GNUNET_free (plug->short_name);
  296. GNUNET_free (plug->lib_name);
  297. GNUNET_free (plug);
  298. }
  299. }
  300. }
  301. /**
  302. * Unload all plugins
  303. */
  304. void
  305. GST_plugins_unload ()
  306. {
  307. struct TransportPlugin *plug;
  308. while (NULL != (plug = plugins_head))
  309. {
  310. GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
  311. GNUNET_free (plug->lib_name);
  312. GNUNET_free (plug->short_name);
  313. GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug);
  314. GNUNET_free (plug);
  315. }
  316. }
  317. /**
  318. * Obtain the plugin API based on a plugin name.
  319. *
  320. * @param name name of the plugin
  321. * @return the plugin's API, NULL if the plugin is not loaded
  322. */
  323. struct GNUNET_TRANSPORT_PluginFunctions *
  324. GST_plugins_find (const char *name)
  325. {
  326. struct TransportPlugin *pos;
  327. for (pos = plugins_head; NULL != pos; pos = pos->next)
  328. if (0 == strcmp (name, pos->short_name))
  329. break;
  330. if (NULL == pos)
  331. return NULL;
  332. return pos->api;
  333. }
  334. /**
  335. * Obtain the plugin API based on a the stripped plugin name after the underscore.
  336. *
  337. * Example: GST_plugins_printer_find (http_client) will return all plugins
  338. * starting with the prefix "http":
  339. * http_client or server if loaded
  340. *
  341. * @param name name of the plugin
  342. * @return the plugin's API, NULL if the plugin is not loaded
  343. */
  344. struct GNUNET_TRANSPORT_PluginFunctions *
  345. GST_plugins_printer_find (const char *name)
  346. {
  347. struct TransportPlugin *pos;
  348. char *stripped = GNUNET_strdup (name);
  349. char *sep = strchr (stripped, '_');
  350. if (NULL != sep)
  351. sep[0] = '\0';
  352. for (pos = plugins_head; NULL != pos; pos = pos->next)
  353. if (pos->short_name == strstr (pos->short_name, stripped))
  354. break;
  355. GNUNET_free (stripped);
  356. if (NULL == pos)
  357. return NULL;
  358. return pos->api;
  359. }
  360. /**
  361. * Convert a given address to a human-readable format. Note that the
  362. * return value will be overwritten on the next call to this function.
  363. *
  364. * @param address the address to convert
  365. * @return statically allocated (!) human-readable address
  366. */
  367. const char *
  368. GST_plugins_a2s (const struct GNUNET_HELLO_Address *address)
  369. {
  370. struct GNUNET_TRANSPORT_PluginFunctions *api;
  371. static char unable_to_show[1024];
  372. static const char *s;
  373. if (NULL == address)
  374. return "<NULL>";
  375. if (0 == address->address_length)
  376. return TRANSPORT_SESSION_INBOUND_STRING; /* Addresse with length 0 are inbound, address->address itself may be NULL */
  377. api = GST_plugins_printer_find (address->transport_name);
  378. if (NULL == api)
  379. {
  380. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  381. "Failed to find transport plugin `%s'\n",
  382. address->transport_name);
  383. return "<plugin unknown>";
  384. }
  385. if (0 == address->address_length)
  386. {
  387. GNUNET_snprintf (unable_to_show,
  388. sizeof(unable_to_show),
  389. "<unable to stringify %u-byte long address of %s transport>",
  390. (unsigned int) address->address_length,
  391. address->transport_name);
  392. return unable_to_show;
  393. }
  394. return(NULL != (s = api->address_to_string (NULL,
  395. address->address,
  396. address->address_length))
  397. ? s
  398. : "<invalid>");
  399. }
  400. /**
  401. * Register callback with all plugins to monitor their status.
  402. *
  403. * @param cb callback to register, NULL to unsubscribe
  404. * @param cb_cls closure for @a cb
  405. */
  406. void
  407. GST_plugins_monitor_subscribe (GNUNET_TRANSPORT_SessionInfoCallback cb,
  408. void *cb_cls)
  409. {
  410. struct TransportPlugin *pos;
  411. for (pos = plugins_head; NULL != pos; pos = pos->next)
  412. if (NULL == pos->api->setup_monitor)
  413. GNUNET_break (0);
  414. else
  415. pos->api->setup_monitor (pos->api->cls,
  416. cb,
  417. cb_cls);
  418. }
  419. /* end of file gnunet-service-transport_plugins.c */