gnunet_cadet_service.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 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., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @author Christian Grothoff
  19. * @author Bart Polot
  20. *
  21. * @file
  22. * CADET service; establish channels to distant peers
  23. *
  24. * @defgroup cadet CADET service
  25. * Confidential Ad-hoc Decentralized End-to-End Transport
  26. *
  27. * See also:
  28. * - [CADET documentation](https://gnunet.org/cadet-subsystem)
  29. * - [CADET paper](https://gnunet.org/cadet)
  30. *
  31. * @{
  32. */
  33. #ifndef GNUNET_CADET_SERVICE_H
  34. #define GNUNET_CADET_SERVICE_H
  35. #ifdef __cplusplus
  36. extern "C"
  37. {
  38. #if 0 /* keep Emacsens' auto-indent happy */
  39. }
  40. #endif
  41. #endif
  42. #include "gnunet_util_lib.h"
  43. #include "gnunet_transport_service.h"
  44. /**
  45. * Version number of GNUnet-cadet API.
  46. */
  47. #define GNUNET_CADET_VERSION 0x00000003
  48. /**
  49. * Opaque handle to the service.
  50. */
  51. struct GNUNET_CADET_Handle;
  52. /**
  53. * Opaque handle to a channel.
  54. */
  55. struct GNUNET_CADET_Channel;
  56. /**
  57. * Hash to be used in Cadet communication. Only 256 bits needed,
  58. * instead of the 512 from `struct GNUNET_HashCode`.
  59. */
  60. struct GNUNET_CADET_Hash
  61. {
  62. unsigned char bits[256 / 8];
  63. };
  64. /**
  65. * Channel options. Second line indicates filed in the
  66. * CadetChannelInfo union carrying the answer.
  67. */
  68. enum GNUNET_CADET_ChannelOption
  69. {
  70. /**
  71. * Default options: unreliable, default buffering, not out of order.
  72. */
  73. GNUNET_CADET_OPTION_DEFAULT = 0x0,
  74. /**
  75. * Disable buffering on intermediate nodes (for minimum latency).
  76. * Yes/No.
  77. */
  78. GNUNET_CADET_OPTION_NOBUFFER = 0x1,
  79. /**
  80. * Enable channel reliability, lost messages will be retransmitted.
  81. * Yes/No.
  82. */
  83. GNUNET_CADET_OPTION_RELIABLE = 0x2,
  84. /**
  85. * Enable out of order delivery of messages.
  86. * Yes/No.
  87. */
  88. GNUNET_CADET_OPTION_OOORDER = 0x4,
  89. /**
  90. * Who is the peer at the other end of the channel.
  91. * Only for use in @c GNUNET_CADET_channel_get_info
  92. * struct GNUNET_PeerIdentity *peer
  93. */
  94. GNUNET_CADET_OPTION_PEER = 0x8
  95. };
  96. /**
  97. * Functions with this signature are called whenever a message is
  98. * received.
  99. *
  100. * Each time the function must call #GNUNET_CADET_receive_done on the channel
  101. * in order to receive the next message. This doesn't need to be immediate:
  102. * can be delayed if some processing is done on the message.
  103. *
  104. * @param cls Closure (set from #GNUNET_CADET_connect).
  105. * @param channel Connection to the other end.
  106. * @param channel_ctx Place to store local state associated with the channel.
  107. * @param message The actual message.
  108. * @return #GNUNET_OK to keep the channel open,
  109. * #GNUNET_SYSERR to close it (signal serious error).
  110. */
  111. typedef int
  112. (*GNUNET_CADET_MessageCallback) (void *cls,
  113. struct GNUNET_CADET_Channel *channel,
  114. void **channel_ctx,
  115. const struct GNUNET_MessageHeader *message);
  116. /**
  117. * Message handler. Each struct specifies how to handle on particular
  118. * type of message received.
  119. */
  120. struct GNUNET_CADET_MessageHandler
  121. {
  122. /**
  123. * Function to call for messages of type @e type.
  124. */
  125. GNUNET_CADET_MessageCallback callback;
  126. /**
  127. * Type of the message this handler covers.
  128. */
  129. uint16_t type;
  130. /**
  131. * Expected size of messages of this type. Use 0 for variable-size.
  132. * If non-zero, messages of the given type will be discarded if they
  133. * do not have the right size.
  134. */
  135. uint16_t expected_size;
  136. };
  137. /**
  138. * Method called whenever another peer has added us to a channel
  139. * the other peer initiated.
  140. * Only called (once) upon reception of data with a message type which was
  141. * subscribed to in #GNUNET_CADET_connect.
  142. *
  143. * A call to #GNUNET_CADET_channel_destroy causes te channel to be ignored. In
  144. * this case the handler MUST return NULL.
  145. *
  146. * @param cls closure
  147. * @param channel new handle to the channel
  148. * @param initiator peer that started the channel
  149. * @param port Port this channel is for.
  150. * @param options CadetOption flag field, with all active option bits set to 1.
  151. *
  152. * @return initial channel context for the channel
  153. * (can be NULL -- that's not an error)
  154. */
  155. typedef void *
  156. (GNUNET_CADET_InboundChannelNotificationHandler) (void *cls,
  157. struct GNUNET_CADET_Channel *channel,
  158. const struct GNUNET_PeerIdentity *initiator,
  159. uint32_t port,
  160. enum GNUNET_CADET_ChannelOption options);
  161. /**
  162. * Function called whenever a channel is destroyed. Should clean up
  163. * any associated state, including cancelling any pending transmission on this
  164. * channel.
  165. *
  166. * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
  167. *
  168. * @param cls closure (set from #GNUNET_CADET_connect)
  169. * @param channel connection to the other end (henceforth invalid)
  170. * @param channel_ctx place where local state associated
  171. * with the channel is stored
  172. */
  173. typedef void
  174. (GNUNET_CADET_ChannelEndHandler) (void *cls,
  175. const struct GNUNET_CADET_Channel *channel,
  176. void *channel_ctx);
  177. /**
  178. * Connect to the cadet service.
  179. *
  180. * @param cfg Configuration to use.
  181. * @param cls Closure for the various callbacks that follow (including
  182. * handlers in the handlers array).
  183. * @param new_channel Function called when an *incoming* channel is created.
  184. * Can be NULL if no inbound channels are desired.
  185. * See @a ports.
  186. * @param cleaner Function called when a channel is destroyed.
  187. * It is called immediately if #GNUNET_CADET_channel_destroy
  188. * is called on the channel.
  189. * @param handlers Callbacks for messages we care about, NULL-terminated. Each
  190. * one must call #GNUNET_CADET_receive_done on the channel to
  191. * receive the next message. Messages of a type that is not
  192. * in the handlers array are ignored if received.
  193. * @param ports NULL or 0-terminated array of port numbers for incoming channels.
  194. * See @a new_channel.
  195. *
  196. * @return handle to the cadet service NULL on error
  197. * (in this case, init is never called)
  198. */
  199. struct GNUNET_CADET_Handle *
  200. GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
  201. void *cls,
  202. GNUNET_CADET_InboundChannelNotificationHandler new_channel,
  203. GNUNET_CADET_ChannelEndHandler cleaner,
  204. const struct GNUNET_CADET_MessageHandler *handlers,
  205. const uint32_t *ports);
  206. /**
  207. * Disconnect from the cadet service. All channels will be destroyed. All channel
  208. * disconnect callbacks will be called on any still connected peers, notifying
  209. * about their disconnection. The registered inbound channel cleaner will be
  210. * called should any inbound channels still exist.
  211. *
  212. * @param handle connection to cadet to disconnect
  213. */
  214. void
  215. GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle);
  216. /**
  217. * Create a new channel towards a remote peer.
  218. *
  219. * If the destination port is not open by any peer or the destination peer
  220. * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
  221. * for this channel.
  222. *
  223. * @param h cadet handle
  224. * @param channel_ctx client's channel context to associate with the channel
  225. * @param peer peer identity the channel should go to
  226. * @param port Port number.
  227. * @param options CadetOption flag field, with all desired option bits set to 1.
  228. *
  229. * @return handle to the channel
  230. */
  231. struct GNUNET_CADET_Channel *
  232. GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
  233. void *channel_ctx,
  234. const struct GNUNET_PeerIdentity *peer,
  235. uint32_t port,
  236. enum GNUNET_CADET_ChannelOption options);
  237. /**
  238. * Destroy an existing channel.
  239. *
  240. * The existing end callback for the channel will be called immediately.
  241. * Any pending outgoing messages will be sent but no incoming messages will be
  242. * accepted and no data callbacks will be called.
  243. *
  244. * @param channel Channel handle, becomes invalid after this call.
  245. */
  246. void
  247. GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel);
  248. /**
  249. * Struct to retrieve info about a channel.
  250. */
  251. union GNUNET_CADET_ChannelInfo
  252. {
  253. /**
  254. * #GNUNET_YES / #GNUNET_NO, for binary flags.
  255. */
  256. int yes_no;
  257. /**
  258. * Peer on the other side of the channel
  259. */
  260. const struct GNUNET_PeerIdentity peer;
  261. };
  262. /**
  263. * Get information about a channel.
  264. *
  265. * @param channel Channel handle.
  266. * @param option Query type GNUNET_CADET_OPTION_*
  267. * @param ... dependant on option, currently not used
  268. * @return Union with an answer to the query.
  269. */
  270. const union GNUNET_CADET_ChannelInfo *
  271. GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
  272. enum GNUNET_CADET_ChannelOption option, ...);
  273. /**
  274. * Handle for a transmission request.
  275. */
  276. struct GNUNET_CADET_TransmitHandle;
  277. /**
  278. * Ask the cadet to call @a notify once it is ready to transmit the
  279. * given number of bytes to the specified channel.
  280. * Only one call can be active at any time, to issue another request,
  281. * wait for the callback or cancel the current request.
  282. *
  283. * @param channel channel to use for transmission
  284. * @param cork is corking allowed for this transmission?
  285. * @param maxdelay how long can the message wait?
  286. * @param notify_size how many bytes of buffer space does notify want?
  287. * @param notify function to call when buffer space is available;
  288. * will be called with NULL on timeout or if the overall queue
  289. * for this peer is larger than queue_size and this is currently
  290. * the message with the lowest priority
  291. * @param notify_cls closure for @a notify
  292. * @return non-NULL if the notify callback was queued,
  293. * NULL if we can not even queue the request (insufficient
  294. * memory); if NULL is returned, @a notify will NOT be called.
  295. */
  296. struct GNUNET_CADET_TransmitHandle *
  297. GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
  298. int cork,
  299. struct GNUNET_TIME_Relative maxdelay,
  300. size_t notify_size,
  301. GNUNET_CONNECTION_TransmitReadyNotify notify,
  302. void *notify_cls);
  303. /**
  304. * Cancel the specified transmission-ready notification.
  305. *
  306. * @param th handle that was returned by "notify_transmit_ready".
  307. */
  308. void
  309. GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th);
  310. /**
  311. * Indicate readiness to receive the next message on a channel.
  312. *
  313. * Should only be called once per handler called.
  314. *
  315. * @param channel Channel that will be allowed to call another handler.
  316. */
  317. void
  318. GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel);
  319. /******************************************************************************/
  320. /******************** MONITORING /DEBUG API *************************/
  321. /******************************************************************************/
  322. /* The following calls are not useful for normal CADET operation, but for */
  323. /* debug and monitoring of the cadet state. They can be safely ignored. */
  324. /* The API can change at any point without notice. */
  325. /* Please contact the developer if you consider any of this calls useful for */
  326. /* normal cadet applications. */
  327. /******************************************************************************/
  328. /**
  329. * Method called to retrieve information about a specific channel the cadet peer
  330. * is aware of, including all transit nodes.
  331. *
  332. * @param cls Closure.
  333. * @param root Root of the channel.
  334. * @param dest Destination of the channel.
  335. * @param port Destination port of the channel.
  336. * @param root_channel_number Local number for root, if known.
  337. * @param dest_channel_number Local number for dest, if known.
  338. * @param public_channel_numbe Number for P2P, always known.
  339. */
  340. typedef void
  341. (*GNUNET_CADET_ChannelCB) (void *cls,
  342. const struct GNUNET_PeerIdentity *root,
  343. const struct GNUNET_PeerIdentity *dest,
  344. uint32_t port,
  345. uint32_t root_channel_number,
  346. uint32_t dest_channel_number,
  347. uint32_t public_channel_number);
  348. /**
  349. * Method called to retrieve information about all peers in CADET, called
  350. * once per peer.
  351. *
  352. * After last peer has been reported, an additional call with NULL is done.
  353. *
  354. * @param cls Closure.
  355. * @param peer Peer, or NULL on "EOF".
  356. * @param tunnel Do we have a tunnel towards this peer?
  357. * @param n_paths Number of known paths towards this peer.
  358. * @param best_path How long is the best path?
  359. * (0 = unknown, 1 = ourselves, 2 = neighbor)
  360. */
  361. typedef void
  362. (*GNUNET_CADET_PeersCB) (void *cls,
  363. const struct GNUNET_PeerIdentity *peer,
  364. int tunnel,
  365. unsigned int n_paths,
  366. unsigned int best_path);
  367. /**
  368. * Method called to retrieve information about a specific peer
  369. * known to the service.
  370. *
  371. * @param cls Closure.
  372. * @param peer Peer ID.
  373. * @param tunnel Do we have a tunnel towards this peer? #GNUNET_YES/#GNUNET_NO
  374. * @param neighbor Is this a direct neighbor? #GNUNET_YES/#GNUNET_NO
  375. * @param n_paths Number of paths known towards peer.
  376. * @param paths Array of PEER_IDs representing all paths to reach the peer.
  377. * Each path starts with the first hop (local peer not included).
  378. * Each path ends with the destination peer (given in @c peer).
  379. */
  380. typedef void
  381. (*GNUNET_CADET_PeerCB) (void *cls,
  382. const struct GNUNET_PeerIdentity *peer,
  383. int tunnel,
  384. int neighbor,
  385. unsigned int n_paths,
  386. struct GNUNET_PeerIdentity *paths);
  387. /**
  388. * Method called to retrieve information about all tunnels in CADET, called
  389. * once per tunnel.
  390. *
  391. * After last tunnel has been reported, an additional call with NULL is done.
  392. *
  393. * @param cls Closure.
  394. * @param peer Destination peer, or NULL on "EOF".
  395. * @param channels Number of channels.
  396. * @param connections Number of connections.
  397. * @param estate Encryption state.
  398. * @param cstate Connectivity state.
  399. */
  400. typedef void
  401. (*GNUNET_CADET_TunnelsCB) (void *cls,
  402. const struct GNUNET_PeerIdentity *peer,
  403. unsigned int channels,
  404. unsigned int connections,
  405. uint16_t estate,
  406. uint16_t cstate);
  407. /**
  408. * Method called to retrieve information about a specific tunnel the cadet peer
  409. * has established, o`r is trying to establish.
  410. *
  411. * @param cls Closure.
  412. * @param peer Peer towards whom the tunnel is directed.
  413. * @param n_channels Number of channels.
  414. * @param n_connections Number of connections.
  415. * @param channels Channels.
  416. * @param connections Connections.
  417. * @param estate Encryption state.
  418. * @param cstate Connectivity state.
  419. */
  420. typedef void
  421. (*GNUNET_CADET_TunnelCB) (void *cls,
  422. const struct GNUNET_PeerIdentity *peer,
  423. unsigned int n_channels,
  424. unsigned int n_connections,
  425. uint32_t *channels,
  426. struct GNUNET_CADET_Hash *connections,
  427. unsigned int estate,
  428. unsigned int cstate);
  429. /**
  430. * Request information about a specific channel of the running cadet peer.
  431. *
  432. * WARNING: unstable API, likely to change in the future!
  433. *
  434. * @param h Handle to the cadet peer.
  435. * @param peer ID of the other end of the channel.
  436. * @param channel_number Channel number.
  437. * @param callback Function to call with the requested data.
  438. * @param callback_cls Closure for @c callback.
  439. */
  440. void
  441. GNUNET_CADET_get_channel (struct GNUNET_CADET_Handle *h,
  442. struct GNUNET_PeerIdentity *peer,
  443. uint32_t channel_number,
  444. GNUNET_CADET_ChannelCB callback,
  445. void *callback_cls);
  446. /**
  447. * Request a debug dump on the service's STDERR.
  448. *
  449. * WARNING: unstable API, likely to change in the future!
  450. *
  451. * @param h cadet handle
  452. */
  453. void
  454. GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h);
  455. /**
  456. * Request information about peers known to the running cadet service.
  457. * The callback will be called for every peer known to the service.
  458. * Only one info request (of any kind) can be active at once.
  459. *
  460. *
  461. * WARNING: unstable API, likely to change in the future!
  462. *
  463. * @param h Handle to the cadet peer.
  464. * @param callback Function to call with the requested data.
  465. * @param callback_cls Closure for @c callback.
  466. *
  467. * @return #GNUNET_OK / #GNUNET_SYSERR
  468. */
  469. int
  470. GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
  471. GNUNET_CADET_PeersCB callback,
  472. void *callback_cls);
  473. /**
  474. * Cancel a peer info request. The callback will not be called (anymore).
  475. *
  476. * WARNING: unstable API, likely to change in the future!
  477. *
  478. * @param h Cadet handle.
  479. *
  480. * @return Closure that was given to #GNUNET_CADET_get_peers().
  481. */
  482. void *
  483. GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h);
  484. /**
  485. * Request information about a peer known to the running cadet peer.
  486. * The callback will be called for the tunnel once.
  487. * Only one info request (of any kind) can be active at once.
  488. *
  489. * WARNING: unstable API, likely to change in the future!
  490. *
  491. * @param h Handle to the cadet peer.
  492. * @param id Peer whose tunnel to examine.
  493. * @param callback Function to call with the requested data.
  494. * @param callback_cls Closure for @c callback.
  495. *
  496. * @return #GNUNET_OK / #GNUNET_SYSERR
  497. */
  498. int
  499. GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
  500. const struct GNUNET_PeerIdentity *id,
  501. GNUNET_CADET_PeerCB callback,
  502. void *callback_cls);
  503. /**
  504. * Request information about tunnels of the running cadet peer.
  505. * The callback will be called for every tunnel of the service.
  506. * Only one info request (of any kind) can be active at once.
  507. *
  508. * WARNING: unstable API, likely to change in the future!
  509. *
  510. * @param h Handle to the cadet peer.
  511. * @param callback Function to call with the requested data.
  512. * @param callback_cls Closure for @c callback.
  513. *
  514. * @return #GNUNET_OK / #GNUNET_SYSERR
  515. */
  516. int
  517. GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
  518. GNUNET_CADET_TunnelsCB callback,
  519. void *callback_cls);
  520. /**
  521. * Cancel a monitor request. The monitor callback will not be called.
  522. *
  523. * @param h Cadet handle.
  524. *
  525. * @return Closure given to #GNUNET_CADET_get_tunnels(), if any.
  526. */
  527. void *
  528. GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h);
  529. /**
  530. * Request information about a tunnel of the running cadet peer.
  531. * The callback will be called for the tunnel once.
  532. * Only one info request (of any kind) can be active at once.
  533. *
  534. * WARNING: unstable API, likely to change in the future!
  535. *
  536. * @param h Handle to the cadet peer.
  537. * @param id Peer whose tunnel to examine.
  538. * @param callback Function to call with the requested data.
  539. * @param callback_cls Closure for @c callback.
  540. *
  541. * @return #GNUNET_OK / #GNUNET_SYSERR
  542. */
  543. int
  544. GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
  545. const struct GNUNET_PeerIdentity *id,
  546. GNUNET_CADET_TunnelCB callback,
  547. void *callback_cls);
  548. /**
  549. * Create a message queue for a cadet channel.
  550. * The message queue can only be used to transmit messages,
  551. * not to receive them.
  552. *
  553. * @param channel the channel to create the message qeue for
  554. * @return a message queue to messages over the channel
  555. */
  556. struct GNUNET_MQ_Handle *
  557. GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel);
  558. #if 0 /* keep Emacsens' auto-indent happy */
  559. {
  560. #endif
  561. #ifdef __cplusplus
  562. }
  563. #endif
  564. /* ifndef GNUNET_CADET_SERVICE_H */
  565. #endif
  566. /** @} */ /* end of group */
  567. /* end of gnunet_cadet_service.h */