gnunet_transport_communication_service.h 12 KB

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