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