gnunet-service-cadet_connection.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001-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. * @file cadet/gnunet-service-cadet_connection.h
  18. * @brief A connection is a live end-to-end messaging mechanism
  19. * where the peers are identified by a path and know how
  20. * to forward along the route using a connection identifier
  21. * for routing the data.
  22. * @author Bartlomiej Polot
  23. * @author Christian Grothoff
  24. */
  25. #ifndef GNUNET_SERVICE_CADET_CONNECTION_H
  26. #define GNUNET_SERVICE_CADET_CONNECTION_H
  27. #include "gnunet_util_lib.h"
  28. #include "gnunet-service-cadet.h"
  29. #include "gnunet-service-cadet_peer.h"
  30. #include "cadet_protocol.h"
  31. /**
  32. * Function called to notify tunnel about change in our readyness.
  33. *
  34. * @param cls closure
  35. * @param is_ready #GNUNET_YES if the connection is now ready for transmission,
  36. * #GNUNET_NO if the connection is no longer ready for transmission
  37. */
  38. typedef void
  39. (*GCC_ReadyCallback)(void *cls,
  40. int is_ready);
  41. /**
  42. * Destroy a connection, called when the CORE layer is already done
  43. * (i.e. has received a BROKEN message), but if we still have to
  44. * communicate the destruction of the connection to the tunnel (if one
  45. * exists).
  46. *
  47. * @param cc connection to destroy
  48. */
  49. void
  50. GCC_destroy_without_core(struct CadetConnection *cc);
  51. /**
  52. * Destroy a connection, called if the tunnel association with the
  53. * connection was already broken, but we still need to notify the CORE
  54. * layer about the breakage.
  55. *
  56. * @param cc connection to destroy
  57. */
  58. void
  59. GCC_destroy_without_tunnel(struct CadetConnection *cc);
  60. /**
  61. * Lookup a connection by its identifier.
  62. *
  63. * @param cid identifier to resolve
  64. * @return NULL if connection was not found
  65. */
  66. struct CadetConnection *
  67. GCC_lookup(const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
  68. /**
  69. * Create a connection to @a destination via @a path and
  70. * notify @a cb whenever we are ready for more data.
  71. *
  72. * @param destination where to go
  73. * @param path which path to take (may not be the full path)
  74. * @param off offset of @a destination on @a path
  75. * @param ct which tunnel uses this connection
  76. * @param ready_cb function to call when ready to transmit
  77. * @param ready_cb_cls closure for @a cb
  78. * @return handle to the connection
  79. */
  80. struct CadetConnection *
  81. GCC_create(struct CadetPeer *destination,
  82. struct CadetPeerPath *path,
  83. unsigned int off,
  84. struct CadetTConnection *ct,
  85. GCC_ReadyCallback ready_cb,
  86. void *ready_cb_cls);
  87. /**
  88. * Create a connection to @a destination via @a path and
  89. * notify @a cb whenever we are ready for more data. This
  90. * is an inbound tunnel, so we must use the existing @a cid
  91. *
  92. * @param destination where to go
  93. * @param path which path to take (may not be the full path)
  94. * @param ct which tunnel uses this connection
  95. * @param ready_cb function to call when ready to transmit
  96. * @param ready_cb_cls closure for @a cb
  97. * @return handle to the connection, NULL if we already have
  98. * a connection that takes precedence on @a path
  99. */
  100. struct CadetConnection *
  101. GCC_create_inbound(struct CadetPeer *destination,
  102. struct CadetPeerPath *path,
  103. struct CadetTConnection *ct,
  104. const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
  105. GCC_ReadyCallback ready_cb,
  106. void *ready_cb_cls);
  107. /**
  108. * Transmit message @a msg via connection @a cc. Must only be called
  109. * (once) after the connection has signalled that it is ready via the
  110. * `ready_cb`. Clients can also use #GCC_is_ready() to check if the
  111. * connection is right now ready for transmission.
  112. *
  113. * @param cc connection identification
  114. * @param env envelope with message to transmit;
  115. * the #GNUNET_MQ_notify_send() must not have yet been used
  116. * for the envelope. Also, the message better match the
  117. * connection identifier of this connection...
  118. */
  119. void
  120. GCC_transmit(struct CadetConnection *cc,
  121. struct GNUNET_MQ_Envelope *env);
  122. /**
  123. * A CREATE_ACK was received for this connection, process it.
  124. *
  125. * @param cc the connection that got the ACK.
  126. */
  127. void
  128. GCC_handle_connection_create_ack(struct CadetConnection *cc);
  129. /**
  130. * We got a #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE for a
  131. * connection that we already have. Either our ACK got lost
  132. * or something is fishy. Consider retransmitting the ACK.
  133. *
  134. * @param cc connection that got the duplicate CREATE
  135. */
  136. void
  137. GCC_handle_duplicate_create(struct CadetConnection *cc);
  138. /**
  139. * Handle KX message.
  140. *
  141. * @param cc connection that received encrypted message
  142. * @param msg the key exchange message
  143. */
  144. void
  145. GCC_handle_kx(struct CadetConnection *cc,
  146. const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg);
  147. /**
  148. * Handle KX_AUTH message.
  149. *
  150. * @param cc connection that received encrypted message
  151. * @param msg the key exchange message
  152. */
  153. void
  154. GCC_handle_kx_auth(struct CadetConnection *cc,
  155. const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg);
  156. /**
  157. * Performance metrics for a connection.
  158. */
  159. struct CadetConnectionMetrics {
  160. /**
  161. * Our current best estimate of the latency, based on a weighted
  162. * average of at least @a latency_datapoints values.
  163. */
  164. struct GNUNET_TIME_Relative aged_latency;
  165. /**
  166. * When was this connection first established? (by us sending or
  167. * receiving the CREATE_ACK for the first time)
  168. */
  169. struct GNUNET_TIME_Absolute age;
  170. /**
  171. * When was this connection last used? (by us sending or
  172. * receiving a PAYLOAD message on it)
  173. */
  174. struct GNUNET_TIME_Absolute last_use;
  175. /**
  176. * How many packets that ought to generate an ACK did we send via
  177. * this connection?
  178. */
  179. unsigned long long num_acked_transmissions;
  180. /**
  181. * Number of packets that were sent via this connection did actually
  182. * receive an ACK? (Note: ACKs may be transmitted and lost via
  183. * other connections, so this value should only be interpreted
  184. * relative to @e num_acked_transmissions and in relation to other
  185. * connections.)
  186. */
  187. unsigned long long num_successes;
  188. };
  189. /**
  190. * Obtain performance @a metrics from @a cc.
  191. *
  192. * @param cc connection to query
  193. * @return the metrics
  194. */
  195. const struct CadetConnectionMetrics *
  196. GCC_get_metrics(struct CadetConnection *cc);
  197. /**
  198. * Handle encrypted message.
  199. *
  200. * @param cc connection that received encrypted message
  201. * @param msg the encrypted message to decrypt
  202. */
  203. void
  204. GCC_handle_encrypted(struct CadetConnection *cc,
  205. const struct GNUNET_CADET_TunnelEncryptedMessage *msg);
  206. /**
  207. * We sent a message for which we expect to receive an ACK via
  208. * the connection identified by @a cti.
  209. *
  210. * @param cid connection identifier where we expect an ACK
  211. */
  212. void
  213. GCC_ack_expected(const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
  214. /**
  215. * We observed an ACK for a message that was originally sent via
  216. * the connection identified by @a cti.
  217. *
  218. * @param cid connection identifier where we got an ACK for a message
  219. * that was originally sent via this connection (the ACK
  220. * may have gotten back to us via a different connection).
  221. */
  222. void
  223. GCC_ack_observed(const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
  224. /**
  225. * We observed some the given @a latency on the connection
  226. * identified by @a cti. (The same connection was taken
  227. * in both directions.)
  228. *
  229. * @param cti connection identifier where we measured latency
  230. * @param latency the observed latency
  231. */
  232. void
  233. GCC_latency_observed(const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
  234. struct GNUNET_TIME_Relative latency);
  235. /**
  236. * Return the tunnel associated with this connection.
  237. *
  238. * @param cc connection to query
  239. * @return corresponding entry in the tunnel's connection list
  240. */
  241. struct CadetTConnection *
  242. GCC_get_ct(struct CadetConnection *cc);
  243. /**
  244. * Obtain the path used by this connection.
  245. *
  246. * @param cc connection
  247. * @param off[out] set to offset in this path where the connection @a cc ends
  248. * @return path to @a cc
  249. */
  250. struct CadetPeerPath *
  251. GCC_get_path(struct CadetConnection *cc,
  252. unsigned int *off);
  253. /**
  254. * Obtain unique ID for the connection.
  255. *
  256. * @param cc connection.
  257. * @return unique number of the connection
  258. */
  259. const struct GNUNET_CADET_ConnectionTunnelIdentifier *
  260. GCC_get_id(struct CadetConnection *cc);
  261. /**
  262. * Get a (static) string for a connection.
  263. *
  264. * @param cc Connection.
  265. */
  266. const char *
  267. GCC_2s(const struct CadetConnection *cc);
  268. /**
  269. * Log connection info.
  270. *
  271. * @param cc connection
  272. * @param level Debug level to use.
  273. */
  274. void
  275. GCC_debug(struct CadetConnection *cc,
  276. enum GNUNET_ErrorType level);
  277. #endif