gnunet_transport_communication_service.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2019 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. *
  19. * @file
  20. * API of the transport service towards the communicator processes.
  21. *
  22. * @defgroup transport TRANSPORT service
  23. * Low-level communication with other peers
  24. *
  25. * @see [Documentation](https://gnunet.org/transport-service)
  26. *
  27. * @{
  28. */
  29. #ifndef GNUNET_TRANSPORT_COMMUNICATION_SERVICE_H
  30. #define GNUNET_TRANSPORT_COMMUNICATION_SERVICE_H
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #if 0 /* keep Emacsens' auto-indent happy */
  34. }
  35. #endif
  36. #endif
  37. #include "gnunet_util_lib.h"
  38. #include "gnunet_nt_lib.h"
  39. /**
  40. * Version number of the transport communication API.
  41. */
  42. #define GNUNET_TRANSPORT_COMMUNICATION_VERSION 0x00000000
  43. /**
  44. * Function called by the transport service to initialize a
  45. * message queue given address information about another peer.
  46. * If and when the communication channel is established, the
  47. * communicator must call #GNUNET_TRANSPORT_communicator_mq_add()
  48. * to notify the service that the channel is now up. It is
  49. * the responsibility of the communicator to manage sane
  50. * retries and timeouts for any @a peer/@a address combination
  51. * provided by the transport service. Timeouts and retries
  52. * do not need to be signalled to the transport service.
  53. *
  54. * @param cls closure
  55. * @param peer identity of the other peer
  56. * @param address where to send the message, human-readable
  57. * communicator-specific format, 0-terminated, UTF-8
  58. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the provided address is
  59. * invalid
  60. */
  61. typedef int (*GNUNET_TRANSPORT_CommunicatorMqInit) (
  62. void *cls,
  63. const struct GNUNET_PeerIdentity *peer,
  64. const char *address);
  65. /**
  66. * Opaque handle to the transport service for communicators.
  67. */
  68. struct GNUNET_TRANSPORT_CommunicatorHandle;
  69. /**
  70. * What characteristics does this communicator have?
  71. *
  72. * FIXME: may want to distinguish bi-directional as well,
  73. * should we define a bit for that? Needed in DV logic (handle_dv_learn)!
  74. */
  75. enum GNUNET_TRANSPORT_CommunicatorCharacteristics
  76. {
  77. /**
  78. * Characteristics are unknown (i.e. DV).
  79. */
  80. GNUNET_TRANSPORT_CC_UNKNOWN = 0,
  81. /**
  82. * Transmission is reliabile (with ACKs), i.e. TCP/HTTP/HTTPS.
  83. */
  84. GNUNET_TRANSPORT_CC_RELIABLE = 1,
  85. /**
  86. * Transmission is unreliable (i.e. UDP)
  87. */
  88. GNUNET_TRANSPORT_CC_UNRELIABLE = 2
  89. };
  90. /**
  91. * Function called when the transport service has received a
  92. * backchannel message for this communicator (!) via a different
  93. * return path.
  94. *
  95. * Typically used to receive messages of type
  96. * #GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_FC_LIMITS or
  97. * #GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_KX_CONFIRMATION
  98. * as well as communicator-specific messages to assist with
  99. * NAT traversal.
  100. *
  101. * @param cls closure
  102. * @param sender which peer sent the notification
  103. * @param msg payload
  104. */
  105. typedef void (*GNUNET_TRANSPORT_CommunicatorNotify) (
  106. void *cls,
  107. const struct GNUNET_PeerIdentity *sender,
  108. const struct GNUNET_MessageHeader *msg);
  109. /**
  110. * Connect to the transport service.
  111. *
  112. * @param cfg configuration to use
  113. * @param config_section section of the configuration to use for options
  114. * @param addr_prefix address prefix for addresses supported by this
  115. * communicator, could be NULL for incoming-only communicators
  116. * @param cc what characteristics does the communicator have?
  117. * @param mq_init function to call to initialize a message queue given
  118. * the address of another peer, can be NULL if the
  119. * communicator only supports receiving messages
  120. * @param mq_init_cls closure for @a mq_init
  121. * @param notify_cb function to pass backchannel messages to communicator
  122. * @param notify_cb_cls closure for @a notify_cb
  123. * @return NULL on error
  124. */
  125. struct GNUNET_TRANSPORT_CommunicatorHandle *
  126. GNUNET_TRANSPORT_communicator_connect (
  127. const struct GNUNET_CONFIGURATION_Handle *cfg,
  128. const char *config_section_name,
  129. const char *addr_prefix,
  130. enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc,
  131. GNUNET_TRANSPORT_CommunicatorMqInit mq_init,
  132. void *mq_init_cls,
  133. GNUNET_TRANSPORT_CommunicatorNotify notify_cb,
  134. void *notify_cb_cls);
  135. /**
  136. * Disconnect from the transport service.
  137. *
  138. * @param ch handle returned from connect
  139. */
  140. void
  141. GNUNET_TRANSPORT_communicator_disconnect (
  142. struct GNUNET_TRANSPORT_CommunicatorHandle *ch);
  143. /* ************************* Receiving *************************** */
  144. /**
  145. * Function called to notify communicator that we have received
  146. * and processed the message. Used for flow control (if supported
  147. * by the communicator).
  148. *
  149. * @param cls closure
  150. * @param success #GNUNET_SYSERR on failure (try to disconnect/reset connection)
  151. * #GNUNET_OK on success
  152. */
  153. typedef void (*GNUNET_TRANSPORT_MessageCompletedCallback) (void *cls,
  154. int success);
  155. /**
  156. * Notify transport service that the communicator has received
  157. * a message.
  158. *
  159. * @param handle connection to transport service
  160. * @param sender presumed sender of the message (details to be checked
  161. * by higher layers)
  162. * @param msg the message
  163. * @param expected_addr_validity how long does the communicator believe it
  164. * will continue to be able to receive messages from the same address
  165. * on which it received this message?
  166. * @param cb function to call once handling the message is done, NULL if
  167. * flow control is not supported by this communicator
  168. * @param cb_cls closure for @a cb
  169. * @return #GNUNET_OK if all is well, #GNUNET_NO if the message was
  170. * immediately dropped due to memory limitations (communicator
  171. * should try to apply back pressure),
  172. * #GNUNET_SYSERR if the message could not be delivered because
  173. * the tranport service is not yet up
  174. */
  175. int
  176. GNUNET_TRANSPORT_communicator_receive (
  177. struct GNUNET_TRANSPORT_CommunicatorHandle *handle,
  178. const struct GNUNET_PeerIdentity *sender,
  179. const struct GNUNET_MessageHeader *msg,
  180. struct GNUNET_TIME_Relative expected_addr_validity,
  181. GNUNET_TRANSPORT_MessageCompletedCallback cb,
  182. void *cb_cls);
  183. /* ************************* Discovery *************************** */
  184. /**
  185. * Handle returned to identify the internal data structure the transport
  186. * API has created to manage a message queue to a particular peer.
  187. */
  188. struct GNUNET_TRANSPORT_QueueHandle;
  189. /**
  190. * Possible states of a connection.
  191. */
  192. enum GNUNET_TRANSPORT_ConnectionStatus
  193. {
  194. /**
  195. * Connection is down.
  196. */
  197. GNUNET_TRANSPORT_CS_DOWN = -1,
  198. /**
  199. * this is an outbound connection (transport initiated)
  200. */
  201. GNUNET_TRANSPORT_CS_OUTBOUND = 0,
  202. /**
  203. * this is an inbound connection (communicator initiated)
  204. */
  205. GNUNET_TRANSPORT_CS_INBOUND = 1
  206. };
  207. /**
  208. * Notify transport service that a MQ became available due to an
  209. * "inbound" connection or because the communicator discovered the
  210. * presence of another peer.
  211. *
  212. * @param ch connection to transport service
  213. * @param peer peer with which we can now communicate
  214. * @param address address in human-readable format, 0-terminated, UTF-8
  215. * @param mtu maximum message size supported by queue, 0 if
  216. * sending is not supported, SIZE_MAX for no MTU
  217. * @param nt which network type does the @a address belong to?
  218. * @param cs what is the connection status of the queue?
  219. * @param mq message queue of the @a peer
  220. * @return API handle identifying the new MQ
  221. */
  222. struct GNUNET_TRANSPORT_QueueHandle *
  223. GNUNET_TRANSPORT_communicator_mq_add (
  224. struct GNUNET_TRANSPORT_CommunicatorHandle *ch,
  225. const struct GNUNET_PeerIdentity *peer,
  226. const char *address,
  227. uint32_t mtu,
  228. enum GNUNET_NetworkType nt,
  229. enum GNUNET_TRANSPORT_ConnectionStatus cs,
  230. struct GNUNET_MQ_Handle *mq);
  231. /**
  232. * Notify transport service that an MQ became unavailable due to a
  233. * disconnect or timeout.
  234. *
  235. * @param qh handle for the queue that must be invalidated
  236. */
  237. void
  238. GNUNET_TRANSPORT_communicator_mq_del (struct GNUNET_TRANSPORT_QueueHandle *qh);
  239. /**
  240. * Internal representation of an address a communicator is
  241. * currently providing for the transport service.
  242. */
  243. struct GNUNET_TRANSPORT_AddressIdentifier;
  244. /**
  245. * Notify transport service about an address that this communicator
  246. * provides for this peer.
  247. *
  248. * @param ch connection to transport service
  249. * @param address our address in human-readable format, 0-terminated, UTF-8
  250. * @param nt which network type does the address belong to?
  251. * @param expiration when does the communicator forsee this address expiring?
  252. */
  253. struct GNUNET_TRANSPORT_AddressIdentifier *
  254. GNUNET_TRANSPORT_communicator_address_add (
  255. struct GNUNET_TRANSPORT_CommunicatorHandle *ch,
  256. const char *address,
  257. enum GNUNET_NetworkType nt,
  258. struct GNUNET_TIME_Relative expiration);
  259. /**
  260. * Notify transport service about an address that this communicator
  261. * no longer provides for this peer.
  262. *
  263. * @param ai address that is no longer provided
  264. */
  265. void
  266. GNUNET_TRANSPORT_communicator_address_remove (
  267. struct GNUNET_TRANSPORT_AddressIdentifier *ai);
  268. /**
  269. * The communicator asks the transport service to route a message via
  270. * a different path to another communicator service at another peer.
  271. * This must only be done for special control traffic (as there is no
  272. * flow control for this API), such as acknowledgements, and generally
  273. * only be done if the communicator is uni-directional (i.e. cannot
  274. * send the message back itself).
  275. *
  276. * While backchannel messages are signed and encrypted, communicators
  277. * must protect against replay attacks when using this backchannel
  278. * communication!
  279. *
  280. * @param ch handle of this communicator
  281. * @param pid peer to send the message to
  282. * @param comm name of the communicator to send the message to
  283. * @param header header of the message to transmit and pass via the
  284. * notify-API to @a pid's communicator @a comm
  285. */
  286. void
  287. GNUNET_TRANSPORT_communicator_notify (
  288. struct GNUNET_TRANSPORT_CommunicatorHandle *ch,
  289. const struct GNUNET_PeerIdentity *pid,
  290. const char *comm,
  291. const struct GNUNET_MessageHeader *header);
  292. #if 0 /* keep Emacsens' auto-indent happy */
  293. {
  294. #endif
  295. #ifdef __cplusplus
  296. }
  297. #endif
  298. /* ifndef GNUNET_TRANSPORT_COMMUNICATOR_SERVICE_H */
  299. #endif
  300. /** @} */ /* end of group */
  301. /* end of gnunet_transport_communicator_service.h */