cadet.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001 - 2011 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 Bartlomiej Polot
  18. * @file cadet/cadet.h
  19. */
  20. #ifndef CADET_H_
  21. #define CADET_H_
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #if 0 /* keep Emacsens' auto-indent happy */
  25. }
  26. #endif
  27. #endif
  28. #include <stdint.h>
  29. #if ! defined(GNUNET_CULL_LOGGING)
  30. #define CADET_TIMING_START \
  31. struct GNUNET_TIME_Absolute __timestamp; \
  32. __timestamp = GNUNET_TIME_absolute_get ()
  33. #define CADET_TIMING_END \
  34. struct GNUNET_TIME_Relative __duration; \
  35. __duration = GNUNET_TIME_absolute_get_duration (__timestamp); \
  36. LOG (GNUNET_ERROR_TYPE_INFO, \
  37. " %s duration %s\n", \
  38. __FUNCTION__, \
  39. GNUNET_STRINGS_relative_time_to_string (__duration, GNUNET_YES));
  40. #else
  41. #define CADET_TIMING_START
  42. #define CADET_TIMING_END
  43. #endif
  44. #include "platform.h"
  45. #include "gnunet_util_lib.h"
  46. #include "gnunet_peer_lib.h"
  47. #include "gnunet_core_service.h"
  48. #include "gnunet_cadet_service.h"
  49. #include "gnunet_protocols.h"
  50. #include "gnunet_cadet_service.h"
  51. /******************************************************************************/
  52. /************************** CONSTANTS ******************************/
  53. /******************************************************************************/
  54. /**
  55. * Minimum value for channel IDs of local clients.
  56. */
  57. #define GNUNET_CADET_LOCAL_CHANNEL_ID_CLI 0x80000000U
  58. /**
  59. * FIXME.
  60. */
  61. #define HIGH_PID 0xFF000000
  62. /**
  63. * FIXME.
  64. */
  65. #define LOW_PID 0x00FFFFFF
  66. /**
  67. * Test if the two PIDs (of type `uint32_t`) are in the range where we
  68. * have to worry about overflows. This is the case when @a pid is
  69. * large and @a max is small, useful when comparing @a pid smaller
  70. * than @a max.
  71. */
  72. #define PID_OVERFLOW(pid, max) (((pid) > HIGH_PID) && ((max) < LOW_PID))
  73. /******************************************************************************/
  74. /************************** MESSAGES ******************************/
  75. /******************************************************************************/
  76. GNUNET_NETWORK_STRUCT_BEGIN
  77. /**
  78. * Number uniquely identifying a channel of a client.
  79. */
  80. struct GNUNET_CADET_ClientChannelNumber
  81. {
  82. /**
  83. * Values for channel numbering.
  84. * Local channel numbers given by the service (incoming) are
  85. * smaller than #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI.
  86. * Local channel numbers given by the client (created) are
  87. * larger than #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI.
  88. */
  89. uint32_t channel_of_client GNUNET_PACKED;
  90. };
  91. /**
  92. * Opaque handle to a channel.
  93. */
  94. struct GNUNET_CADET_Channel
  95. {
  96. /**
  97. * Other end of the channel.
  98. */
  99. struct GNUNET_PeerIdentity peer;
  100. /**
  101. * Handle to the cadet this channel belongs to
  102. */
  103. struct GNUNET_CADET_Handle *cadet;
  104. /**
  105. * Channel's port, if incoming.
  106. */
  107. struct GNUNET_CADET_Port *incoming_port;
  108. /**
  109. * Any data the caller wants to put in here, used for the
  110. * various callbacks (@e disconnects, @e window_changes, handlers).
  111. */
  112. void *ctx;
  113. /**
  114. * Message Queue for the channel (which we are implementing).
  115. */
  116. struct GNUNET_MQ_Handle *mq;
  117. /**
  118. * Task to allow mq to send more traffic.
  119. */
  120. struct GNUNET_SCHEDULER_Task *mq_cont;
  121. /**
  122. * Pending envelope with a message to be transmitted to the
  123. * service as soon as we are allowed to. Should only be
  124. * non-NULL if @e allow_send is 0.
  125. */
  126. struct GNUNET_MQ_Envelope *pending_env;
  127. /**
  128. * Window change handler.
  129. */
  130. GNUNET_CADET_WindowSizeEventHandler window_changes;
  131. /**
  132. * Disconnect handler.
  133. */
  134. GNUNET_CADET_DisconnectEventHandler disconnects;
  135. /**
  136. * Local ID of the channel, #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI bit is set if outbound.
  137. */
  138. struct GNUNET_CADET_ClientChannelNumber ccn;
  139. /**
  140. * How many messages are we allowed to send to the service right now?
  141. */
  142. unsigned int allow_send;
  143. };
  144. /**
  145. * Message for a client to create and destroy channels.
  146. */
  147. struct GNUNET_CADET_PortMessage
  148. {
  149. /**
  150. * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN
  151. * or #GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE
  152. *
  153. * Size: sizeof(struct GNUNET_CADET_ChannelMessage)
  154. */
  155. struct GNUNET_MessageHeader header;
  156. /**
  157. * Port to open/close.
  158. */
  159. struct GNUNET_HashCode port GNUNET_PACKED;
  160. };
  161. /**
  162. * Message for a client to create channels.
  163. */
  164. struct GNUNET_CADET_LocalChannelCreateMessage
  165. {
  166. /**
  167. * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE
  168. *
  169. * Size: sizeof(struct GNUNET_CADET_ChannelOpenMessageMessage)
  170. */
  171. struct GNUNET_MessageHeader header;
  172. /**
  173. * ID of a channel controlled by this client.
  174. */
  175. struct GNUNET_CADET_ClientChannelNumber ccn;
  176. /**
  177. * Channel's peer
  178. */
  179. struct GNUNET_PeerIdentity peer;
  180. /**
  181. * Port of the channel.
  182. */
  183. struct GNUNET_HashCode port;
  184. /**
  185. * Options.
  186. */
  187. uint32_t opt GNUNET_PACKED;
  188. };
  189. /**
  190. * Message for or to a client to destroy tunnel.
  191. */
  192. struct GNUNET_CADET_LocalChannelDestroyMessage
  193. {
  194. /**
  195. * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY
  196. */
  197. struct GNUNET_MessageHeader header;
  198. /**
  199. * ID of a channel controlled by this client.
  200. */
  201. struct GNUNET_CADET_ClientChannelNumber ccn;
  202. };
  203. /**
  204. * Message for cadet data traffic.
  205. */
  206. struct GNUNET_CADET_LocalData
  207. {
  208. /**
  209. * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA
  210. */
  211. struct GNUNET_MessageHeader header;
  212. /**
  213. * ID of the channel
  214. */
  215. struct GNUNET_CADET_ClientChannelNumber ccn;
  216. /**
  217. * Priority and preferences (an enum GNUNET_MQ_PriorityPreferences)
  218. * of the message in NBO.
  219. */
  220. uint32_t pp GNUNET_PACKED;
  221. /**
  222. * Payload follows
  223. */
  224. };
  225. /**
  226. * Message to allow the client send more data to the service
  227. * (always service -> client).
  228. */
  229. struct GNUNET_CADET_LocalAck
  230. {
  231. /**
  232. * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK
  233. */
  234. struct GNUNET_MessageHeader header;
  235. /**
  236. * ID of the channel allowed to send more data.
  237. */
  238. struct GNUNET_CADET_ClientChannelNumber ccn;
  239. };
  240. /**
  241. * Message to inform the client about channels in the service.
  242. *
  243. * TODO: split into two messages!
  244. */
  245. struct GNUNET_CADET_LocalInfo
  246. {
  247. /**
  248. * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL or
  249. * #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER
  250. */
  251. struct GNUNET_MessageHeader header;
  252. /**
  253. * ID of the channel allowed to send more data.
  254. */
  255. struct GNUNET_CADET_ClientChannelNumber ccn;
  256. /**
  257. * ID of the destination of the channel (can be local peer).
  258. */
  259. struct GNUNET_PeerIdentity peer;
  260. };
  261. /**
  262. * Message to drop another message of specific type. Used in test context
  263. */
  264. struct GNUNET_CADET_RequestDropCadetMessage
  265. {
  266. /**
  267. * Type: #GNUNET_MESSAGE_TYPE_CADET_DROP_CADET_MESSAGE
  268. */
  269. struct GNUNET_MessageHeader header;
  270. /**
  271. * Type of the message this handler covers, in host byte order.
  272. */
  273. uint16_t type;
  274. /**
  275. * ID of the channel we want to drop a message for.
  276. */
  277. struct GNUNET_CADET_ClientChannelNumber ccn;
  278. };
  279. /**
  280. * Message to inform the client about channels in the service.
  281. */
  282. struct GNUNET_CADET_RequestPathInfoMessage
  283. {
  284. /**
  285. * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PATH
  286. */
  287. struct GNUNET_MessageHeader header;
  288. /**
  289. * Always zero.
  290. */
  291. uint32_t resered GNUNET_PACKED;
  292. /**
  293. * ID of the destination of the channel (can be local peer).
  294. */
  295. struct GNUNET_PeerIdentity peer;
  296. };
  297. /**
  298. * Message to inform the client about channels in the service.
  299. */
  300. struct GNUNET_CADET_ChannelInfoMessage
  301. {
  302. /**
  303. * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL.
  304. */
  305. struct GNUNET_MessageHeader header;
  306. /**
  307. * Root of the channel
  308. */
  309. struct GNUNET_PeerIdentity root;
  310. /**
  311. * Destination of the channel
  312. */
  313. struct GNUNET_PeerIdentity dest;
  314. /* FIXME: expand! */
  315. };
  316. /**
  317. * Message to as the service about information on a channel.
  318. */
  319. struct GNUNET_CADET_RequestChannelInfoMessage
  320. {
  321. /**
  322. * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_CHANNEL.
  323. */
  324. struct GNUNET_MessageHeader header;
  325. /**
  326. * Target of the channel.
  327. */
  328. struct GNUNET_PeerIdentity target;
  329. };
  330. /**
  331. * Message to inform the client about one of the paths known to the service.
  332. */
  333. struct GNUNET_CADET_LocalInfoPath
  334. {
  335. /**
  336. * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH.
  337. */
  338. struct GNUNET_MessageHeader header;
  339. /**
  340. * Offset of the peer that was requested.
  341. */
  342. uint32_t off GNUNET_PACKED;
  343. };
  344. /**
  345. * Message to inform the client about one of the peers in the service.
  346. */
  347. struct GNUNET_CADET_LocalInfoPeers
  348. {
  349. /**
  350. * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS
  351. */
  352. struct GNUNET_MessageHeader header;
  353. /**
  354. * Number of paths.
  355. */
  356. uint16_t paths GNUNET_PACKED;
  357. /**
  358. * Do we have a tunnel toward this peer?
  359. */
  360. int16_t tunnel GNUNET_PACKED;
  361. /**
  362. * Shortest known path.
  363. */
  364. uint32_t best_path_length GNUNET_PACKED;
  365. /**
  366. * ID of the peer (can be local peer).
  367. */
  368. struct GNUNET_PeerIdentity destination;
  369. };
  370. /**
  371. * Message to inform the client about one of the tunnels in the service.
  372. *
  373. * TODO: split into two messages!
  374. */
  375. struct GNUNET_CADET_LocalInfoTunnel
  376. {
  377. /**
  378. * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL
  379. * or #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS
  380. */
  381. struct GNUNET_MessageHeader header;
  382. /**
  383. * Number of channels.
  384. */
  385. uint32_t channels GNUNET_PACKED;
  386. /**
  387. * ID of the destination of the tunnel (can be local peer).
  388. */
  389. struct GNUNET_PeerIdentity destination;
  390. /**
  391. * Number of connections.
  392. */
  393. uint32_t connections GNUNET_PACKED;
  394. /**
  395. * Encryption state.
  396. */
  397. uint16_t estate GNUNET_PACKED;
  398. /**
  399. * Connection state.
  400. */
  401. uint16_t cstate GNUNET_PACKED;
  402. /* If TUNNEL (no 'S'): struct GNUNET_CADET_ConnectionTunnelIdentifier connection_ids[connections] */
  403. /* If TUNNEL (no 'S'): uint32_t channel_ids[channels] */
  404. };
  405. GNUNET_NETWORK_STRUCT_END
  406. /**
  407. * @brief Translate a fwd variable into a string representation, for logging.
  408. *
  409. * @param fwd Is FWD? (#GNUNET_YES or #GNUNET_NO)
  410. *
  411. * @return String representing FWD or BCK.
  412. */
  413. char *
  414. GC_f2s (int fwd);
  415. /**
  416. * Check if one pid is bigger than other, accounting for overflow.
  417. *
  418. * @param bigger Argument that should be bigger.
  419. * @param smaller Argument that should be smaller.
  420. *
  421. * @return True if bigger (arg1) has a higher value than smaller (arg 2).
  422. */
  423. int
  424. GC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
  425. /**
  426. * Get the higher ACK value out of two values, taking in account overflow.
  427. *
  428. * @param a First ACK value.
  429. * @param b Second ACK value.
  430. *
  431. * @return Highest ACK value from the two.
  432. */
  433. uint32_t
  434. GC_max_pid (uint32_t a, uint32_t b);
  435. /**
  436. * Get the lower ACK value out of two values, taking in account overflow.
  437. *
  438. * @param a First ACK value.
  439. * @param b Second ACK value.
  440. *
  441. * @return Lowest ACK value from the two.
  442. */
  443. uint32_t
  444. GC_min_pid (uint32_t a, uint32_t b);
  445. /**
  446. * Allocate a string with a hexdump of any binary data.
  447. *
  448. * @param bin Arbitrary binary data.
  449. * @param len Length of @a bin in bytes.
  450. * @param output Where to write the output (if *output be NULL it's allocated).
  451. *
  452. * @return The size of the output.
  453. */
  454. size_t
  455. GC_bin2s (void *bin, unsigned int len, char **output);
  456. /**
  457. * Convert a message type into a string to help debug
  458. * Generated with:
  459. * FIND: "#define ([^ ]+)[ ]*([0-9]+)"
  460. * REPLACE: " case \2: return "\1"; break;"
  461. *
  462. * @param m Message type.
  463. *
  464. * @return Human readable string description.
  465. */
  466. const char *
  467. GC_m2s (uint16_t m);
  468. #if 0 /* keep Emacsens' auto-indent happy */
  469. {
  470. #endif
  471. #ifdef __cplusplus
  472. }
  473. #endif
  474. #endif