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