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