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