plugin_transport_template.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. This file is part of GNUnet
  3. (C) 2002-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 transport/plugin_transport_template.c
  19. * @brief template for a new transport service
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_protocols.h"
  25. #include "gnunet_statistics_service.h"
  26. #include "gnunet_transport_service.h"
  27. #include "gnunet_transport_plugin.h"
  28. #define LOG(kind,...) GNUNET_log_from (kind, "transport-template",__VA_ARGS__)
  29. /**
  30. * After how long do we expire an address that we
  31. * learned from another peer if it is not reconfirmed
  32. * by anyone?
  33. */
  34. #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (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 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 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 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 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 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 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_ATS_Network_Type
  246. template_plugin_get_network (void *cls,
  247. struct Session *session)
  248. {
  249. GNUNET_assert (NULL != session);
  250. return GNUNET_ATS_NET_UNSPECIFIED; /* Change to correct network type */
  251. }
  252. /**
  253. * Convert the transports address to a nice, human-readable
  254. * format.
  255. *
  256. * @param cls closure
  257. * @param type name of the transport that generated the address
  258. * @param addr one of the addresses of the host, NULL for the last address
  259. * the specific address format depends on the transport
  260. * @param addrlen length of the address
  261. * @param numeric should (IP) addresses be displayed in numeric form?
  262. * @param timeout after how long should we give up?
  263. * @param asc function to call on each string
  264. * @param asc_cls closure for @a asc
  265. */
  266. static void
  267. template_plugin_address_pretty_printer (void *cls, const char *type,
  268. const void *addr, size_t addrlen,
  269. int numeric,
  270. struct GNUNET_TIME_Relative timeout,
  271. GNUNET_TRANSPORT_AddressStringCallback
  272. asc, void *asc_cls)
  273. {
  274. asc (asc_cls, "converted address", GNUNET_OK); /* return address */
  275. asc (asc_cls, NULL, GNUNET_OK); /* done */
  276. }
  277. /**
  278. * Another peer has suggested an address for this
  279. * peer and transport plugin. Check that this could be a valid
  280. * address. If so, consider adding it to the list
  281. * of addresses.
  282. *
  283. * @param cls closure
  284. * @param addr pointer to the address
  285. * @param addrlen length of addr
  286. * @return #GNUNET_OK if this is a plausible address for this peer
  287. * and transport
  288. */
  289. static int
  290. template_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
  291. {
  292. /* struct Plugin *plugin = cls; */
  293. /* check if the address is belonging to the plugin*/
  294. return GNUNET_OK;
  295. }
  296. /**
  297. * Function called for a quick conversion of the binary address to
  298. * a numeric address. Note that the caller must not free the
  299. * address and that the next call to this function is allowed
  300. * to override the address again.
  301. *
  302. * @param cls closure
  303. * @param addr binary address
  304. * @param addrlen length of the address
  305. * @return string representing the same address
  306. */
  307. static const char *
  308. template_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
  309. {
  310. /*
  311. * Print address in format template.options.address
  312. */
  313. if (0 == addrlen)
  314. {
  315. return TRANSPORT_SESSION_INBOUND_STRING;
  316. }
  317. GNUNET_break (0);
  318. return NULL;
  319. }
  320. /**
  321. * Function called to convert a string address to
  322. * a binary address.
  323. *
  324. * @param cls closure ('struct Plugin*')
  325. * @param addr string address
  326. * @param addrlen length of the @a addr
  327. * @param buf location to store the buffer
  328. * @param added location to store the number of bytes in the buffer.
  329. * If the function returns #GNUNET_SYSERR, its contents are undefined.
  330. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  331. */
  332. static int
  333. template_plugin_string_to_address (void *cls,
  334. const char *addr,
  335. uint16_t addrlen,
  336. void **buf, size_t *added)
  337. {
  338. /*
  339. * Parse string in format template.options.address
  340. */
  341. GNUNET_break (0);
  342. return GNUNET_SYSERR;
  343. }
  344. /**
  345. * Create a new session to transmit data to the target
  346. * This session will used to send data to this peer and the plugin will
  347. * notify us by calling the env->session_end function
  348. *
  349. * @param cls closure
  350. * @param address pointer to the GNUNET_HELLO_Address
  351. * @return the session if the address is valid, NULL otherwise
  352. */
  353. static struct Session *
  354. template_plugin_get_session (void *cls,
  355. const struct GNUNET_HELLO_Address *address)
  356. {
  357. GNUNET_break (0);
  358. return NULL;
  359. }
  360. static void
  361. template_plugin_update_session_timeout (void *cls,
  362. const struct GNUNET_PeerIdentity *peer,
  363. struct Session *session)
  364. {
  365. }
  366. #if 0
  367. /**
  368. * Return information about the given session to the
  369. * monitor callback.
  370. *
  371. * @param cls the `struct Plugin` with the monitor callback (`sic`)
  372. * @param peer peer we send information about
  373. * @param value our `struct Session` to send information about
  374. * @return #GNUNET_OK (continue to iterate)
  375. */
  376. static int
  377. send_session_info_iter (void *cls,
  378. const struct GNUNET_PeerIdentity *peer,
  379. void *value)
  380. {
  381. struct Plugin *plugin = cls;
  382. struct Session *session = value;
  383. notify_session_monitor (plugin,
  384. session,
  385. GNUNET_TRANSPORT_SS_UP);
  386. return GNUNET_OK;
  387. }
  388. #endif
  389. /**
  390. * Begin monitoring sessions of a plugin. There can only
  391. * be one active monitor per plugin (i.e. if there are
  392. * multiple monitors, the transport service needs to
  393. * multiplex the generated events over all of them).
  394. *
  395. * @param cls closure of the plugin
  396. * @param sic callback to invoke, NULL to disable monitor;
  397. * plugin will being by iterating over all active
  398. * sessions immediately and then enter monitor mode
  399. * @param sic_cls closure for @a sic
  400. */
  401. static void
  402. template_plugin_setup_monitor (void *cls,
  403. GNUNET_TRANSPORT_SessionInfoCallback sic,
  404. void *sic_cls)
  405. {
  406. struct Plugin *plugin = cls;
  407. plugin->sic = sic;
  408. plugin->sic_cls = sic_cls;
  409. if (NULL != sic)
  410. {
  411. #if 0
  412. GNUNET_CONTAINER_multipeermap_iterate (NULL /* FIXME */,
  413. &send_session_info_iter,
  414. plugin);
  415. #endif
  416. /* signal end of first iteration */
  417. sic (sic_cls, NULL, NULL);
  418. }
  419. }
  420. /**
  421. * Entry point for the plugin.
  422. */
  423. void *
  424. libgnunet_plugin_transport_template_init (void *cls)
  425. {
  426. struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
  427. struct GNUNET_TRANSPORT_PluginFunctions *api;
  428. struct Plugin *plugin;
  429. if (NULL == env->receive)
  430. {
  431. /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
  432. initialze the plugin or the API */
  433. api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
  434. api->cls = NULL;
  435. api->address_to_string = &template_plugin_address_to_string;
  436. api->string_to_address = &template_plugin_string_to_address;
  437. api->address_pretty_printer = &template_plugin_address_pretty_printer;
  438. return api;
  439. }
  440. plugin = GNUNET_new (struct Plugin);
  441. plugin->env = env;
  442. api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
  443. api->cls = plugin;
  444. api->send = &template_plugin_send;
  445. api->disconnect_peer = &template_plugin_disconnect_peer;
  446. api->disconnect_session = &template_plugin_disconnect_session;
  447. api->query_keepalive_factor = &template_plugin_query_keepalive_factor;
  448. api->address_pretty_printer = &template_plugin_address_pretty_printer;
  449. api->check_address = &template_plugin_address_suggested;
  450. api->address_to_string = &template_plugin_address_to_string;
  451. api->string_to_address = &template_plugin_string_to_address;
  452. api->get_session = &template_plugin_get_session;
  453. api->get_network = &template_plugin_get_network;
  454. api->update_session_timeout = &template_plugin_update_session_timeout;
  455. api->setup_monitor = &template_plugin_setup_monitor;
  456. LOG (GNUNET_ERROR_TYPE_INFO, "Template plugin successfully loaded\n");
  457. return api;
  458. }
  459. /**
  460. * Exit point from the plugin.
  461. */
  462. void *
  463. libgnunet_plugin_transport_template_done (void *cls)
  464. {
  465. struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
  466. struct Plugin *plugin = api->cls;
  467. GNUNET_free (plugin);
  468. GNUNET_free (api);
  469. return NULL;
  470. }
  471. /* end of plugin_transport_template.c */