gnunet_cadet_service.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2017 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. * @author Christian Grothoff
  18. * @author Bart Polot
  19. *
  20. * @file
  21. * CADET service; establish channels to distant peers
  22. *
  23. * @defgroup cadet CADET service
  24. * Confidential Ad-hoc Decentralized End-to-End Transport
  25. *
  26. * @see [Documentation](https://gnunet.org/cadet-subsystem)
  27. * @see [Paper](https://gnunet.org/cadet)
  28. *
  29. * @{
  30. */
  31. #ifndef GNUNET_CADET_SERVICE_H
  32. #define GNUNET_CADET_SERVICE_H
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #if 0 /* keep Emacsens' auto-indent happy */
  36. }
  37. #endif
  38. #endif
  39. #include "gnunet_util_lib.h"
  40. #include "gnunet_transport_service.h"
  41. /**
  42. * Version number of GNUnet-cadet API.
  43. */
  44. #define GNUNET_CADET_VERSION 0x00000005
  45. /**
  46. * Opaque handle to the service.
  47. */
  48. struct GNUNET_CADET_Handle;
  49. /**
  50. * Opaque handle to a channel.
  51. */
  52. struct GNUNET_CADET_Channel;
  53. /**
  54. * Opaque handle to a port.
  55. */
  56. struct GNUNET_CADET_Port;
  57. /**
  58. * Hash uniquely identifying a connection below a tunnel.
  59. */
  60. struct GNUNET_CADET_ConnectionTunnelIdentifier
  61. {
  62. struct GNUNET_ShortHashCode connection_of_tunnel;
  63. };
  64. /**
  65. * Number identifying a CADET channel within a tunnel.
  66. */
  67. struct GNUNET_CADET_ChannelTunnelNumber
  68. {
  69. /**
  70. * Which number does this channel have that uniquely identfies
  71. * it within its tunnel, in network byte order.
  72. *
  73. * Given two peers, both may initiate channels over the same tunnel.
  74. * The @e cn must be greater or equal to 0x80000000 (high-bit set)
  75. * for tunnels initiated with the peer that has the larger peer
  76. * identity as compared using #GNUNET_memcmp().
  77. */
  78. uint32_t cn GNUNET_PACKED;
  79. };
  80. /**
  81. * Method called whenever a peer connects to a port in MQ-based CADET.
  82. *
  83. * @param cls Closure from #GNUNET_CADET_open_port.
  84. * @param channel New handle to the channel.
  85. * @param source Peer that started this channel.
  86. * @return Closure for the incoming @a channel. It's given to:
  87. * - The #GNUNET_CADET_DisconnectEventHandler (given to
  88. * #GNUNET_CADET_open_port) when the channel dies.
  89. * - Each the #GNUNET_MQ_MessageCallback handlers for each message
  90. * received on the @a channel.
  91. */
  92. typedef void *(*GNUNET_CADET_ConnectEventHandler) (
  93. void *cls,
  94. struct GNUNET_CADET_Channel *channel,
  95. const struct GNUNET_PeerIdentity *source);
  96. /**
  97. * Function called whenever an MQ-channel is destroyed, unless the destruction
  98. * was requested by #GNUNET_CADET_channel_destroy.
  99. * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
  100. *
  101. * It should clean up any associated state, including cancelling any pending
  102. * transmission on this channel.
  103. *
  104. * @param cls Channel closure.
  105. * @param channel Connection to the other end (henceforth invalid).
  106. */
  107. typedef void (*GNUNET_CADET_DisconnectEventHandler) (
  108. void *cls,
  109. const struct GNUNET_CADET_Channel *channel);
  110. /**
  111. * Function called whenever an MQ-channel's transmission window size changes.
  112. *
  113. * The first callback in an outgoing channel will be with a non-zero value
  114. * and will mean the channel is connected to the destination.
  115. *
  116. * For an incoming channel it will be called immediately after the
  117. * #GNUNET_CADET_ConnectEventHandler, also with a non-zero value.
  118. *
  119. * @param cls Channel closure.
  120. * @param channel Connection to the other end --- FIXME: drop?
  121. * @param window_size New window size. If the is more messages than buffer size
  122. * this value will be negative. -- FIXME: make unsigned, we never call negative?
  123. */
  124. typedef void (*GNUNET_CADET_WindowSizeEventHandler) (
  125. void *cls,
  126. const struct GNUNET_CADET_Channel *channel,
  127. int window_size);
  128. /**
  129. * Connect to the MQ-based cadet service.
  130. *
  131. * @param cfg Configuration to use.
  132. * @return Handle to the cadet service NULL on error.
  133. */
  134. struct GNUNET_CADET_Handle *
  135. GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
  136. /**
  137. * Disconnect from the cadet service. All channels will be destroyed. All channel
  138. * disconnect callbacks will be called on any still connected peers, notifying
  139. * about their disconnection. The registered inbound channel cleaner will be
  140. * called should any inbound channels still exist.
  141. *
  142. * @param handle connection to cadet to disconnect
  143. */
  144. void
  145. GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle);
  146. /**
  147. * Open a port to receive incomming MQ-based channels.
  148. *
  149. * @param h CADET handle.
  150. * @param port Hash identifying the port.
  151. * @param connects Function called when an incoming channel is connected.
  152. * @param connects_cls Closure for the @a connects handler.
  153. * @param window_changes Function called when the transmit window size changes.
  154. * Can be NULL.
  155. * @param disconnects Function called when a channel is disconnected.
  156. * @param handlers Callbacks for messages we care about, NULL-terminated.
  157. * @return Port handle, NULL if port is in use
  158. */
  159. struct GNUNET_CADET_Port *
  160. GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
  161. const struct GNUNET_HashCode *port,
  162. GNUNET_CADET_ConnectEventHandler connects,
  163. void *connects_cls,
  164. GNUNET_CADET_WindowSizeEventHandler window_changes,
  165. GNUNET_CADET_DisconnectEventHandler disconnects,
  166. const struct GNUNET_MQ_MessageHandler *handlers);
  167. /**
  168. * Close a port opened with @a GNUNET_CADET_open_port.
  169. * The @a new_channel callback will no longer be called.
  170. *
  171. * @param p Port handle.
  172. */
  173. void
  174. GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p);
  175. /**
  176. * Create a new channel towards a remote peer.
  177. *
  178. * If the destination peer closes the channel after accepting it,
  179. * @a disconnects will be called for this channel (unless
  180. * #GNUNET_CADET_channel_destroy() was called on this end first).
  181. *
  182. * @param h CADET handle.
  183. * @param channel_cls Closure for the channel. It's given to:
  184. * - The management handler @a window_changes.
  185. * - The disconnect handler @a disconnects
  186. * - Each message type callback in @a handlers
  187. * @param destination Peer identity the channel should go to.
  188. * @param port Identification of the destination port.
  189. * @param window_changes Function called when the transmit window size changes.
  190. * Can be NULL if this data is of no interest.
  191. * @param disconnects Function called when the channel is disconnected.
  192. * @param handlers Callbacks for messages we care about, NULL-terminated.
  193. * @return Handle to the channel.
  194. */
  195. struct GNUNET_CADET_Channel *
  196. GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
  197. void *channel_cls,
  198. const struct GNUNET_PeerIdentity *destination,
  199. const struct GNUNET_HashCode *port,
  200. GNUNET_CADET_WindowSizeEventHandler window_changes,
  201. GNUNET_CADET_DisconnectEventHandler disconnects,
  202. const struct GNUNET_MQ_MessageHandler *handlers);
  203. /**
  204. * Destroy an existing channel.
  205. *
  206. * The existing end callback for the channel will NOT be called.
  207. * Any pending outgoing messages will be sent but no incoming messages will be
  208. * accepted and no data callbacks will be called.
  209. *
  210. * @param channel Channel handle, becomes invalid after this call.
  211. */
  212. void
  213. GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel);
  214. /**
  215. * Obtain the message queue for a connected channel.
  216. *
  217. * @param channel The channel handle from which to get the MQ.
  218. * @return The message queue of the channel.
  219. */
  220. struct GNUNET_MQ_Handle *
  221. GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel);
  222. /**
  223. * Indicate readiness to receive the next message on a channel.
  224. *
  225. * Should only be called once per handler called.
  226. *
  227. * @param channel Channel that will be allowed to call another handler.
  228. */
  229. void
  230. GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel);
  231. /**
  232. * Transitional function to convert an unsigned int port to a hash value.
  233. * WARNING: local static value returned, NOT reentrant!
  234. * WARNING: do not use this function for new code!
  235. *
  236. * @param port Numerical port (unsigned int format).
  237. *
  238. * @return A GNUNET_HashCode usable for the new CADET API.
  239. */
  240. const struct GNUNET_HashCode *
  241. GC_u2h (uint32_t port);
  242. enum GNUNET_CADET_ChannelInfoOption
  243. {
  244. /**
  245. * Who is the peer at the other end of the channel.
  246. * Only for use in @c GNUNET_CADET_channel_get_info
  247. * struct GNUNET_PeerIdentity *peer
  248. */
  249. GNUNET_CADET_OPTION_PEER = 0x0
  250. };
  251. /**
  252. * Union to retrieve info about a channel.
  253. */
  254. union GNUNET_CADET_ChannelInfo
  255. {
  256. /**
  257. * #GNUNET_YES / #GNUNET_NO, for binary flags.
  258. */
  259. int yes_no;
  260. /**
  261. * Peer on the other side of the channel
  262. */
  263. const struct GNUNET_PeerIdentity peer;
  264. };
  265. /**
  266. * Get information about a channel.
  267. *
  268. * @param channel Channel handle.
  269. * @param ... dependant on option, currently not used
  270. * @return Union with an answer to the query.
  271. */
  272. const union GNUNET_CADET_ChannelInfo *
  273. GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
  274. enum GNUNET_CADET_ChannelInfoOption option,
  275. ...);
  276. /******************************************************************************/
  277. /******************** MONITORING /DEBUG API *************************/
  278. /******************************************************************************/
  279. /* The following calls are not useful for normal CADET operation, but for */
  280. /* debug and monitoring of the cadet state. They can be safely ignored. */
  281. /* The API can change at any point without notice. */
  282. /* Please contact the developer if you consider any of this calls useful for */
  283. /* normal cadet applications. */
  284. /******************************************************************************/
  285. /**
  286. * Internal details about a channel.
  287. */
  288. struct GNUNET_CADET_ChannelInternals
  289. {
  290. /**
  291. * Root of the channel
  292. */
  293. struct GNUNET_PeerIdentity root;
  294. /**
  295. * Destination of the channel
  296. */
  297. struct GNUNET_PeerIdentity dest;
  298. // to be expanded!
  299. };
  300. /**
  301. * Method called to retrieve information about a specific channel the cadet peer
  302. * is aware of, including all transit nodes.
  303. *
  304. * @param cls Closure.
  305. * @param info internal details, NULL for end of list
  306. */
  307. typedef void (*GNUNET_CADET_ChannelCB) (
  308. void *cls,
  309. const struct GNUNET_CADET_ChannelInternals *info);
  310. /**
  311. * Operation handle.
  312. */
  313. struct GNUNET_CADET_ChannelMonitor;
  314. /**
  315. * Request information about channels to @a peer from the local peer.
  316. *
  317. * @param cfg configuration to use
  318. * @param peer ID of the other end of the channel.
  319. * @param callback Function to call with the requested data.
  320. * @param callback_cls Closure for @c callback.
  321. */
  322. struct GNUNET_CADET_ChannelMonitor *
  323. GNUNET_CADET_get_channel (const struct GNUNET_CONFIGURATION_Handle *cfg,
  324. struct GNUNET_PeerIdentity *peer,
  325. GNUNET_CADET_ChannelCB callback,
  326. void *callback_cls);
  327. /**
  328. * Cancel a channel monitor request. The callback will not be called (anymore).
  329. *
  330. * @param h Cadet handle.
  331. * @return Closure that was given to #GNUNET_CADET_get_channel().
  332. */
  333. void *
  334. GNUNET_CADET_get_channel_cancel (struct GNUNET_CADET_ChannelMonitor *cm);
  335. /**
  336. * Information we return per peer.
  337. */
  338. struct GNUNET_CADET_PeerListEntry
  339. {
  340. /**
  341. * Which peer is the information about?
  342. */
  343. struct GNUNET_PeerIdentity peer;
  344. /**
  345. * Do we have a tunnel to this peer?
  346. */
  347. int have_tunnel;
  348. /**
  349. * Number of disjoint known paths to @e peer.
  350. */
  351. unsigned int n_paths;
  352. /**
  353. * Length of the shortest path (0 = unknown, 1 = ourselves, 2 = direct neighbour).
  354. */
  355. unsigned int best_path_length;
  356. };
  357. /**
  358. * Method called to retrieve information about all peers in CADET, called
  359. * once per peer.
  360. *
  361. * After last peer has been reported, an additional call with NULL is done.
  362. *
  363. * @param cls Closure.
  364. * @param ple information about a peer, or NULL on "EOF".
  365. */
  366. typedef void (*GNUNET_CADET_PeersCB) (
  367. void *cls,
  368. const struct GNUNET_CADET_PeerListEntry *ple);
  369. /**
  370. * Operation handle.
  371. */
  372. struct GNUNET_CADET_PeersLister;
  373. /**
  374. * Request information about peers known to the running cadet service.
  375. * The callback will be called for every peer known to the service.
  376. * Only one info request (of any kind) can be active at once.
  377. *
  378. * @param cfg configuration to use
  379. * @param callback Function to call with the requested data.
  380. * @param callback_cls Closure for @c callback.
  381. * @return NULL on error
  382. */
  383. struct GNUNET_CADET_PeersLister *
  384. GNUNET_CADET_list_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
  385. GNUNET_CADET_PeersCB callback,
  386. void *callback_cls);
  387. /**
  388. * Cancel a peer info request. The callback will not be called (anymore).
  389. *
  390. * @param pl operation handle
  391. * @return Closure that was given to #GNUNET_CADET_list_peers().
  392. */
  393. void *
  394. GNUNET_CADET_list_peers_cancel (struct GNUNET_CADET_PeersLister *pl);
  395. /**
  396. * Detailed information we return per peer.
  397. */
  398. struct GNUNET_CADET_PeerPathDetail
  399. {
  400. /**
  401. * Peer this is about.
  402. */
  403. struct GNUNET_PeerIdentity peer;
  404. /**
  405. * Offset of the target peer on the @e path.
  406. */
  407. unsigned int target_offset;
  408. /**
  409. * Number of entries on the @e path.
  410. */
  411. unsigned int path_length;
  412. /**
  413. * Array of PEER_IDs representing all paths to reach the peer. Each
  414. * path starts with the first hop (local peer not included). Each
  415. * path ends with the destination peer (given in @e peer).
  416. */
  417. const struct GNUNET_PeerIdentity *path;
  418. };
  419. /**
  420. * Method called to retrieve information about a specific path
  421. * known to the service.
  422. *
  423. * @param cls Closure.
  424. * @param ppd details about a path to the peer, NULL for end of information
  425. */
  426. typedef void (*GNUNET_CADET_PathCB) (
  427. void *cls,
  428. const struct GNUNET_CADET_PeerPathDetail *ppd);
  429. /**
  430. * Handle to cancel #GNUNET_CADET_get_path() operation.
  431. */
  432. struct GNUNET_CADET_GetPath;
  433. /**
  434. * Request information about a peer known to the running cadet peer.
  435. *
  436. * @param cfg configuration to use
  437. * @param id Peer whose paths we want to examine.
  438. * @param callback Function to call with the requested data.
  439. * @param callback_cls Closure for @c callback.
  440. * @return NULL on error
  441. */
  442. struct GNUNET_CADET_GetPath *
  443. GNUNET_CADET_get_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
  444. const struct GNUNET_PeerIdentity *id,
  445. GNUNET_CADET_PathCB callback,
  446. void *callback_cls);
  447. /**
  448. * Cancel @a gp operation.
  449. *
  450. * @param gp operation to cancel
  451. * @return closure from #GNUNET_CADET_get_path().
  452. */
  453. void *
  454. GNUNET_CADET_get_path_cancel (struct GNUNET_CADET_GetPath *gp);
  455. /**
  456. * Details about a tunnel managed by CADET.
  457. */
  458. struct GNUNET_CADET_TunnelDetails
  459. {
  460. /**
  461. * Target of the tunnel.
  462. */
  463. struct GNUNET_PeerIdentity peer;
  464. /**
  465. * How many channels use the tunnel.
  466. */
  467. uint32_t channels;
  468. /**
  469. * How many connections support the tunnel.
  470. */
  471. uint32_t connections;
  472. /**
  473. * What is our encryption state?
  474. */
  475. uint16_t estate;
  476. /**
  477. * What is our connectivity state?
  478. */
  479. uint16_t cstate;
  480. };
  481. /**
  482. * Method called to retrieve information about all tunnels in CADET, called
  483. * once per tunnel.
  484. *
  485. * After last tunnel has been reported, an additional call with NULL is done.
  486. *
  487. * @param cls Closure.
  488. * @param td tunnel details, NULL for end of list
  489. */
  490. typedef void (*GNUNET_CADET_TunnelsCB) (
  491. void *cls,
  492. const struct GNUNET_CADET_TunnelDetails *td);
  493. /**
  494. * Operation handle.
  495. */
  496. struct GNUNET_CADET_ListTunnels;
  497. /**
  498. * Request information about tunnels of the running cadet peer.
  499. * The callback will be called for every tunnel of the service.
  500. * Only one info request (of any kind) can be active at once.
  501. *
  502. * @param cfg configuration to use
  503. * @param callback Function to call with the requested data.
  504. * @param callback_cls Closure for @c callback.
  505. * @return NULL on error
  506. */
  507. struct GNUNET_CADET_ListTunnels *
  508. GNUNET_CADET_list_tunnels (const struct GNUNET_CONFIGURATION_Handle *cfg,
  509. GNUNET_CADET_TunnelsCB callback,
  510. void *callback_cls);
  511. /**
  512. * Cancel a monitor request. The monitor callback will not be called.
  513. *
  514. * @param lt operation handle
  515. * @return Closure given to #GNUNET_CADET_list_tunnels(), if any.
  516. */
  517. void *
  518. GNUNET_CADET_list_tunnels_cancel (struct GNUNET_CADET_ListTunnels *lt);
  519. #if 0 /* keep Emacsens' auto-indent happy */
  520. {
  521. #endif
  522. #ifdef __cplusplus
  523. }
  524. #endif
  525. /* ifndef GNUNET_CADET_SERVICE_H */
  526. #endif
  527. /** @} */ /* end of group */
  528. /* end of gnunet_cadet_service.h */