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