plugin_transport_template.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2002-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/plugin_transport_template.c
  18. * @brief template for a new transport service
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_protocols.h"
  24. #include "gnunet_statistics_service.h"
  25. #include "gnunet_transport_service.h"
  26. #include "gnunet_transport_plugin.h"
  27. #define LOG(kind,...) GNUNET_log_from (kind, "transport-template",__VA_ARGS__)
  28. /**
  29. * After how long do we expire an address that we
  30. * learned from another peer if it is not reconfirmed
  31. * by anyone?
  32. */
  33. #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
  34. #define PLUGIN_NAME "template"
  35. /**
  36. * Encapsulation of all of the state of the plugin.
  37. */
  38. struct Plugin;
  39. /**
  40. * Session handle for connections.
  41. */
  42. struct GNUNET_ATS_Session
  43. {
  44. /**
  45. * To whom are we talking to (set to our identity
  46. * if we are still waiting for the welcome message)
  47. */
  48. struct GNUNET_PeerIdentity sender;
  49. /**
  50. * Stored in a linked list (or a peer map, or ...)
  51. */
  52. struct GNUNET_ATS_Session *next;
  53. /**
  54. * Pointer to the global plugin struct.
  55. */
  56. struct Plugin *plugin;
  57. /**
  58. * The client (used to identify this connection)
  59. */
  60. /* void *client; */
  61. /**
  62. * Continuation function to call once the transmission buffer
  63. * has again space available. NULL if there is no
  64. * continuation to call.
  65. */
  66. GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
  67. /**
  68. * Closure for @e transmit_cont.
  69. */
  70. void *transmit_cont_cls;
  71. /**
  72. * At what time did we reset @e last_received last?
  73. */
  74. struct GNUNET_TIME_Absolute last_quota_update;
  75. /**
  76. * How many bytes have we received since the @e last_quota_update
  77. * timestamp?
  78. */
  79. uint64_t last_received;
  80. /**
  81. * Number of bytes per ms that this peer is allowed
  82. * to send to us.
  83. */
  84. uint32_t quota;
  85. };
  86. GNUNET_NETWORK_STRUCT_BEGIN
  87. struct TemplateAddress
  88. {
  89. /**
  90. * Address options in NBO
  91. */
  92. uint32_t options GNUNET_PACKED;
  93. /* Add address here */
  94. };
  95. GNUNET_NETWORK_STRUCT_END
  96. /**
  97. * Encapsulation of all of the state of the plugin.
  98. */
  99. struct Plugin
  100. {
  101. /**
  102. * Our environment.
  103. */
  104. struct GNUNET_TRANSPORT_PluginEnvironment *env;
  105. /**
  106. * List of open sessions (or peer map, or...)
  107. */
  108. struct GNUNET_ATS_Session *sessions;
  109. /**
  110. * Function to call about session status changes.
  111. */
  112. GNUNET_TRANSPORT_SessionInfoCallback sic;
  113. /**
  114. * Closure for @e sic.
  115. */
  116. void *sic_cls;
  117. /**
  118. * Options in HBO to be used with addresses
  119. */
  120. };
  121. #if 0
  122. /**
  123. * If a session monitor is attached, notify it about the new
  124. * session state.
  125. *
  126. * @param plugin our plugin
  127. * @param session session that changed state
  128. * @param state new state of the session
  129. */
  130. static void
  131. notify_session_monitor (struct Plugin *plugin,
  132. struct GNUNET_ATS_Session *session,
  133. enum GNUNET_TRANSPORT_SessionState state)
  134. {
  135. struct GNUNET_TRANSPORT_SessionInfo info;
  136. if (NULL == plugin->sic)
  137. return;
  138. memset (&info, 0, sizeof (info));
  139. info.state = state;
  140. info.is_inbound = GNUNET_SYSERR; /* FIXME */
  141. // info.num_msg_pending =
  142. // info.num_bytes_pending =
  143. // info.receive_delay =
  144. // info.session_timeout = session->timeout;
  145. // info.address = session->address;
  146. plugin->sic (plugin->sic_cls,
  147. session,
  148. &info);
  149. }
  150. #endif
  151. /**
  152. * Function that can be used by the transport service to transmit
  153. * a message using the plugin. Note that in the case of a
  154. * peer disconnecting, the continuation MUST be called
  155. * prior to the disconnect notification itself. This function
  156. * will be called with this peer's HELLO message to initiate
  157. * a fresh connection to another peer.
  158. *
  159. * @param cls closure
  160. * @param session which session must be used
  161. * @param msgbuf the message to transmit
  162. * @param msgbuf_size number of bytes in @a msgbuf
  163. * @param priority how important is the message (most plugins will
  164. * ignore message priority and just FIFO)
  165. * @param to how long to wait at most for the transmission (does not
  166. * require plugins to discard the message after the timeout,
  167. * just advisory for the desired delay; most plugins will ignore
  168. * this as well)
  169. * @param cont continuation to call once the message has
  170. * been transmitted (or if the transport is ready
  171. * for the next transmission call; or if the
  172. * peer disconnected...); can be NULL
  173. * @param cont_cls closure for @a cont
  174. * @return number of bytes used (on the physical network, with overheads);
  175. * -1 on hard errors (i.e. address invalid); 0 is a legal value
  176. * and does NOT mean that the message was not transmitted (DV)
  177. */
  178. static ssize_t
  179. template_plugin_send (void *cls,
  180. struct GNUNET_ATS_Session *session,
  181. const char *msgbuf,
  182. size_t msgbuf_size,
  183. unsigned int priority,
  184. struct GNUNET_TIME_Relative to,
  185. GNUNET_TRANSPORT_TransmitContinuation cont,
  186. void *cont_cls)
  187. {
  188. /* struct Plugin *plugin = cls; */
  189. ssize_t bytes_sent = 0;
  190. return bytes_sent;
  191. }
  192. /**
  193. * Function that can be used to force the plugin to disconnect
  194. * from the given peer and cancel all previous transmissions
  195. * (and their continuationc).
  196. *
  197. * @param cls closure
  198. * @param target peer from which to disconnect
  199. */
  200. static void
  201. template_plugin_disconnect_peer (void *cls,
  202. const struct GNUNET_PeerIdentity *target)
  203. {
  204. // struct Plugin *plugin = cls;
  205. // FIXME
  206. }
  207. /**
  208. * Function that can be used to force the plugin to disconnect
  209. * from the given peer and cancel all previous transmissions
  210. * (and their continuationc).
  211. *
  212. * @param cls closure
  213. * @param session session from which to disconnect
  214. * @return #GNUNET_OK on success
  215. */
  216. static int
  217. template_plugin_disconnect_session (void *cls,
  218. struct GNUNET_ATS_Session *session)
  219. {
  220. // struct Plugin *plugin = cls;
  221. // FIXME
  222. return GNUNET_SYSERR;
  223. }
  224. /**
  225. * Function that is called to get the keepalive factor.
  226. * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
  227. * calculate the interval between keepalive packets.
  228. *
  229. * @param cls closure with the `struct Plugin`
  230. * @return keepalive factor
  231. */
  232. static unsigned int
  233. template_plugin_query_keepalive_factor (void *cls)
  234. {
  235. return 3;
  236. }
  237. /**
  238. * Function obtain the network type for a session
  239. *
  240. * @param cls closure ('struct Plugin*')
  241. * @param session the session
  242. * @return the network type in HBO or #GNUNET_SYSERR
  243. */
  244. static enum GNUNET_NetworkType
  245. template_plugin_get_network (void *cls,
  246. struct GNUNET_ATS_Session *session)
  247. {
  248. GNUNET_assert (NULL != session);
  249. return GNUNET_NT_UNSPECIFIED; /* Change to correct network type */
  250. }
  251. /**
  252. * Function obtain the network type for an address.
  253. *
  254. * @param cls closure (`struct Plugin *`)
  255. * @param address the address
  256. * @return the network type
  257. */
  258. static enum GNUNET_NetworkType
  259. template_plugin_get_network_for_address (void *cls,
  260. const struct GNUNET_HELLO_Address *address)
  261. {
  262. return GNUNET_NT_WAN; /* FOR NOW */
  263. }
  264. /**
  265. * Convert the transports address to a nice, human-readable
  266. * format.
  267. *
  268. * @param cls closure
  269. * @param type name of the transport that generated the address
  270. * @param addr one of the addresses of the host, NULL for the last address
  271. * the specific address format depends on the transport
  272. * @param addrlen length of the address
  273. * @param numeric should (IP) addresses be displayed in numeric form?
  274. * @param timeout after how long should we give up?
  275. * @param asc function to call on each string
  276. * @param asc_cls closure for @a asc
  277. */
  278. static void
  279. template_plugin_address_pretty_printer (void *cls, const char *type,
  280. const void *addr, size_t addrlen,
  281. int numeric,
  282. struct GNUNET_TIME_Relative timeout,
  283. GNUNET_TRANSPORT_AddressStringCallback
  284. asc, void *asc_cls)
  285. {
  286. asc (asc_cls, "converted address", GNUNET_OK); /* return address */
  287. asc (asc_cls, NULL, GNUNET_OK); /* done */
  288. }
  289. /**
  290. * Another peer has suggested an address for this
  291. * peer and transport plugin. Check that this could be a valid
  292. * address. If so, consider adding it to the list
  293. * of addresses.
  294. *
  295. * @param cls closure
  296. * @param addr pointer to the address
  297. * @param addrlen length of addr
  298. * @return #GNUNET_OK if this is a plausible address for this peer
  299. * and transport
  300. */
  301. static int
  302. template_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
  303. {
  304. /* struct Plugin *plugin = cls; */
  305. /* check if the address is belonging to the plugin*/
  306. return GNUNET_OK;
  307. }
  308. /**
  309. * Function called for a quick conversion of the binary address to
  310. * a numeric address. Note that the caller must not free the
  311. * address and that the next call to this function is allowed
  312. * to override the address again.
  313. *
  314. * @param cls closure
  315. * @param addr binary address
  316. * @param addrlen length of the address
  317. * @return string representing the same address
  318. */
  319. static const char *
  320. template_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
  321. {
  322. /*
  323. * Print address in format template.options.address
  324. */
  325. if (0 == addrlen)
  326. {
  327. return TRANSPORT_SESSION_INBOUND_STRING;
  328. }
  329. GNUNET_break (0);
  330. return NULL;
  331. }
  332. /**
  333. * Function called to convert a string address to
  334. * a binary address.
  335. *
  336. * @param cls closure ('struct Plugin*')
  337. * @param addr string address
  338. * @param addrlen length of the @a addr
  339. * @param buf location to store the buffer
  340. * @param added location to store the number of bytes in the buffer.
  341. * If the function returns #GNUNET_SYSERR, its contents are undefined.
  342. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  343. */
  344. static int
  345. template_plugin_string_to_address (void *cls,
  346. const char *addr,
  347. uint16_t addrlen,
  348. void **buf, size_t *added)
  349. {
  350. /*
  351. * Parse string in format template.options.address
  352. */
  353. GNUNET_break (0);
  354. return GNUNET_SYSERR;
  355. }
  356. /**
  357. * Create a new session to transmit data to the target
  358. * This session will used to send data to this peer and the plugin will
  359. * notify us by calling the env->session_end function
  360. *
  361. * @param cls closure
  362. * @param address pointer to the GNUNET_HELLO_Address
  363. * @return the session if the address is valid, NULL otherwise
  364. */
  365. static struct GNUNET_ATS_Session *
  366. template_plugin_get_session (void *cls,
  367. const struct GNUNET_HELLO_Address *address)
  368. {
  369. GNUNET_break (0);
  370. return NULL;
  371. }
  372. static void
  373. template_plugin_update_session_timeout (void *cls,
  374. const struct GNUNET_PeerIdentity *peer,
  375. struct GNUNET_ATS_Session *session)
  376. {
  377. }
  378. #if 0
  379. /**
  380. * Return information about the given session to the
  381. * monitor callback.
  382. *
  383. * @param cls the `struct Plugin` with the monitor callback (`sic`)
  384. * @param peer peer we send information about
  385. * @param value our `struct GNUNET_ATS_Session` to send information about
  386. * @return #GNUNET_OK (continue to iterate)
  387. */
  388. static int
  389. send_session_info_iter (void *cls,
  390. const struct GNUNET_PeerIdentity *peer,
  391. void *value)
  392. {
  393. struct Plugin *plugin = cls;
  394. struct GNUNET_ATS_Session *session = value;
  395. notify_session_monitor (plugin,
  396. session,
  397. GNUNET_TRANSPORT_SS_UP);
  398. return GNUNET_OK;
  399. }
  400. #endif
  401. /**
  402. * Begin monitoring sessions of a plugin. There can only
  403. * be one active monitor per plugin (i.e. if there are
  404. * multiple monitors, the transport service needs to
  405. * multiplex the generated events over all of them).
  406. *
  407. * @param cls closure of the plugin
  408. * @param sic callback to invoke, NULL to disable monitor;
  409. * plugin will being by iterating over all active
  410. * sessions immediately and then enter monitor mode
  411. * @param sic_cls closure for @a sic
  412. */
  413. static void
  414. template_plugin_setup_monitor (void *cls,
  415. GNUNET_TRANSPORT_SessionInfoCallback sic,
  416. void *sic_cls)
  417. {
  418. struct Plugin *plugin = cls;
  419. plugin->sic = sic;
  420. plugin->sic_cls = sic_cls;
  421. if (NULL != sic)
  422. {
  423. #if 0
  424. GNUNET_CONTAINER_multipeermap_iterate (NULL /* FIXME */,
  425. &send_session_info_iter,
  426. plugin);
  427. #endif
  428. /* signal end of first iteration */
  429. sic (sic_cls, NULL, NULL);
  430. }
  431. }
  432. /**
  433. * Entry point for the plugin.
  434. */
  435. void *
  436. libgnunet_plugin_transport_template_init (void *cls)
  437. {
  438. struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
  439. struct GNUNET_TRANSPORT_PluginFunctions *api;
  440. struct Plugin *plugin;
  441. if (NULL == env->receive)
  442. {
  443. /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
  444. initialze the plugin or the API */
  445. api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
  446. api->cls = NULL;
  447. api->address_to_string = &template_plugin_address_to_string;
  448. api->string_to_address = &template_plugin_string_to_address;
  449. api->address_pretty_printer = &template_plugin_address_pretty_printer;
  450. return api;
  451. }
  452. plugin = GNUNET_new (struct Plugin);
  453. plugin->env = env;
  454. api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
  455. api->cls = plugin;
  456. api->send = &template_plugin_send;
  457. api->disconnect_peer = &template_plugin_disconnect_peer;
  458. api->disconnect_session = &template_plugin_disconnect_session;
  459. api->query_keepalive_factor = &template_plugin_query_keepalive_factor;
  460. api->address_pretty_printer = &template_plugin_address_pretty_printer;
  461. api->check_address = &template_plugin_address_suggested;
  462. api->address_to_string = &template_plugin_address_to_string;
  463. api->string_to_address = &template_plugin_string_to_address;
  464. api->get_session = &template_plugin_get_session;
  465. api->get_network = &template_plugin_get_network;
  466. api->get_network_for_address = &template_plugin_get_network_for_address;
  467. api->update_session_timeout = &template_plugin_update_session_timeout;
  468. api->setup_monitor = &template_plugin_setup_monitor;
  469. LOG (GNUNET_ERROR_TYPE_INFO, "Template plugin successfully loaded\n");
  470. return api;
  471. }
  472. /**
  473. * Exit point from the plugin.
  474. */
  475. void *
  476. libgnunet_plugin_transport_template_done (void *cls)
  477. {
  478. struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
  479. struct Plugin *plugin = api->cls;
  480. GNUNET_free (plugin);
  481. GNUNET_free (api);
  482. return NULL;
  483. }
  484. /* end of plugin_transport_template.c */