plugin_transport_template.c 15 KB

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