24083f1a15996c4f661480da981773143aeb742d
[oweals/gnunet.git] / src / include / gnunet_core_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_core_service.h
23  * @brief core service; this is the main API for encrypted P2P
24  *        communications
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_CORE_SERVICE_H
29 #define GNUNET_CORE_SERVICE_H
30
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #if 0                           /* keep Emacsens' auto-indent happy */
35 }
36 #endif
37 #endif
38
39 #include "gnunet_util_lib.h"
40 #include "gnunet_transport_service.h"
41
42 /**
43  * Version number of GNUnet-core API.
44  */
45 #define GNUNET_CORE_VERSION 0x00000000
46
47
48 /**
49  * Opaque handle to the service.
50  */
51 struct GNUNET_CORE_Handle;
52
53
54 /**
55  * Method called whenever a given peer connects.
56  *
57  * @param cls closure
58  * @param peer peer identity this notification is about
59  * @param atsi performance data for the connection
60  */
61 typedef void (*GNUNET_CORE_ConnectEventHandler) (void *cls,
62                                                  const struct
63                                                  GNUNET_PeerIdentity * peer,
64                                                  const struct
65                                                  GNUNET_TRANSPORT_ATS_Information
66                                                  * atsi);
67
68
69 /**
70  * Method called whenever a peer disconnects.
71  *
72  * @param cls closure
73  * @param peer peer identity this notification is about
74  */
75 typedef void (*GNUNET_CORE_DisconnectEventHandler) (void *cls,
76                                                     const struct
77                                                     GNUNET_PeerIdentity * peer);
78
79
80 /**
81  * Functions with this signature are called whenever a message is
82  * received or transmitted.
83  *
84  * @param cls closure (set from GNUNET_CORE_connect)
85  * @param peer the other peer involved (sender or receiver, NULL
86  *        for loopback messages where we are both sender and receiver)
87  * @param message the actual message
88  * @param atsi performance data for the connection
89  * @return GNUNET_OK to keep the connection open,
90  *         GNUNET_SYSERR to close it (signal serious error)
91  */
92 typedef int (*GNUNET_CORE_MessageCallback) (void *cls,
93                                             const struct GNUNET_PeerIdentity *
94                                             other,
95                                             const struct GNUNET_MessageHeader *
96                                             message,
97                                             const struct
98                                             GNUNET_TRANSPORT_ATS_Information *
99                                             atsi);
100
101
102 /**
103  * Message handler.  Each struct specifies how to handle on particular
104  * type of message received.
105  */
106 struct GNUNET_CORE_MessageHandler
107 {
108   /**
109    * Function to call for messages of "type".
110    */
111   GNUNET_CORE_MessageCallback callback;
112
113   /**
114    * Type of the message this handler covers.
115    */
116   uint16_t type;
117
118   /**
119    * Expected size of messages of this type.  Use 0 for variable-size.
120    * If non-zero, messages of the given type will be discarded if they
121    * do not have the right size.
122    */
123   uint16_t expected_size;
124
125 };
126
127
128 /**
129  * Function called after GNUNET_CORE_connect has succeeded (or failed
130  * for good).  Note that the private key of the peer is intentionally
131  * not exposed here; if you need it, your process should try to read
132  * the private key file directly (which should work if you are
133  * authorized...).
134  *
135  * @param cls closure
136  * @param server handle to the server, NULL if we failed
137  * @param my_identity ID of this peer, NULL if we failed
138  */
139 typedef void (*GNUNET_CORE_StartupCallback) (void *cls,
140                                              struct GNUNET_CORE_Handle * server,
141                                              const struct GNUNET_PeerIdentity *my_identity);
142
143
144 /**
145  * Connect to the core service.  Note that the connection may complete
146  * (or fail) asynchronously.  This function primarily causes the given
147  * callback notification functions to be invoked whenever the
148  * specified event happens.  The maximum number of queued
149  * notifications (queue length) is per client but the queue is shared
150  * across all types of notifications.  So a slow client that registers
151  * for 'outbound_notify' also risks missing 'inbound_notify' messages.
152  * Certain events (such as connect/disconnect notifications) are not
153  * subject to queue size limitations.
154  *
155  * @param cfg configuration to use
156  * @param queue_size size of the per-peer message queue
157  * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
158  * @param init callback to call on timeout or once we have successfully
159  *        connected to the core service; note that timeout is only meaningful if init is not NULL
160  * @param connects function to call on peer connect, can be NULL
161  * @param disconnects function to call on peer disconnect / timeout, can be NULL
162  * @param inbound_notify function to call for all inbound messages, can be NULL
163  *                note that the core is allowed to drop notifications about inbound
164  *                messages if the client does not process them fast enough (for this
165  *                notification type, a bounded queue is used)
166  * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
167  *                GNUNET_MessageHeader and hence we do not need to give it the full message;
168  *                can be used to improve efficiency, ignored if inbound_notify is NULL
169  *                note that the core is allowed to drop notifications about inbound
170  *                messages if the client does not process them fast enough (for this
171  *                notification type, a bounded queue is used)
172  * @param outbound_notify function to call for all outbound messages, can be NULL;
173  *                note that the core is allowed to drop notifications about outbound
174  *                messages if the client does not process them fast enough (for this
175  *                notification type, a bounded queue is used)
176  * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
177  *                GNUNET_MessageHeader and hence we do not need to give it the full message
178  *                can be used to improve efficiency, ignored if outbound_notify is NULL
179  *                note that the core is allowed to drop notifications about outbound
180  *                messages if the client does not process them fast enough (for this
181  *                notification type, a bounded queue is used)
182  * @param handlers callbacks for messages we care about, NULL-terminated
183  *                note that the core is allowed to drop notifications about inbound
184  *                messages if the client does not process them fast enough (for this
185  *                notification type, a bounded queue is used)
186  * @return handle to the core service (only useful for disconnect until 'init' is called),
187  *           NULL on error (in this case, init is never called)
188  */
189 struct GNUNET_CORE_Handle *
190 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
191                      unsigned int queue_size, void *cls,
192                      GNUNET_CORE_StartupCallback init,
193                      GNUNET_CORE_ConnectEventHandler connects,
194                      GNUNET_CORE_DisconnectEventHandler disconnects,
195                      GNUNET_CORE_MessageCallback inbound_notify,
196                      int inbound_hdr_only,
197                      GNUNET_CORE_MessageCallback outbound_notify,
198                      int outbound_hdr_only,
199                      const struct GNUNET_CORE_MessageHandler *handlers);
200
201
202 /**
203  * Disconnect from the core service.    This function can only
204  * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
205  * requests have been explicitly cancelled.
206  *
207  * @param handle connection to core to disconnect
208  */
209 void
210 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle);
211
212
213 /**
214  * Iterate over all connected peers.  Calls peer_cb with each
215  * connected peer, and then once with NULL to indicate that all peers
216  * have been handled.
217  *
218  * @param cfg configuration handle
219  * @param peer_cb function to call with the peer information
220  * @param cb_cls closure for peer_cb
221  * @return GNUNET_OK on success, GNUNET_SYSERR on errors
222  */
223 int
224 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
225                            GNUNET_CORE_ConnectEventHandler peer_cb,
226                            void *cb_cls);
227
228
229 /**
230  * Check if the given peer is currently connected and return information
231  * about the session if so.
232  *
233  * @param cfg configuration to use
234  * @param peer the specific peer to check for
235  * @param peer_cb function to call with the peer information
236  * @param cb_cls closure for peer_cb
237  *
238  * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
239  */
240 int
241 GNUNET_CORE_is_peer_connected (const struct GNUNET_CONFIGURATION_Handle *cfg,
242                                struct GNUNET_PeerIdentity *peer,
243                                GNUNET_CORE_ConnectEventHandler peer_cb,
244                                void *cb_cls);
245
246
247 /**
248  * Handle for a transmission request.
249  */
250 struct GNUNET_CORE_TransmitHandle;
251
252
253 /**
254  * Ask the core to call "notify" once it is ready to transmit the
255  * given number of bytes to the specified "target".   Must only be
256  * called after a connection to the respective peer has been
257  * established (and the client has been informed about this).
258  *
259  *
260  * @param handle connection to core service
261  * @param cork is corking allowed for this transmission?
262  * @param priority how important is the message?
263  * @param maxdelay how long can the message wait?
264  * @param target who should receive the message,
265  *        use NULL for this peer (loopback)
266  * @param notify_size how many bytes of buffer space does notify want?
267  * @param notify function to call when buffer space is available;
268  *        will be called with NULL on timeout or if the overall queue
269  *        for this peer is larger than queue_size and this is currently
270  *        the message with the lowest priority; will also be called
271  *        with 'NULL' buf if the peer disconnects; since the disconnect
272  *        signal will be emmitted even later, clients MUST cancel
273  *        all pending transmission requests DURING the disconnect
274  *        handler (unless they ensure that 'notify' never calls
275  *        'GNUNET_CORE_notify_transmit_ready').
276  * @param notify_cls closure for notify
277  * @return non-NULL if the notify callback was queued,
278  *         NULL if we can not even queue the request (insufficient
279  *         memory); if NULL is returned, "notify" will NOT be called.
280  */
281 struct GNUNET_CORE_TransmitHandle *
282 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle, int cork,
283                                    uint32_t priority,
284                                    struct GNUNET_TIME_Relative maxdelay,
285                                    const struct GNUNET_PeerIdentity *target,
286                                    size_t notify_size,
287                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
288                                    void *notify_cls);
289
290
291 /**
292  * Cancel the specified transmission-ready notification.
293  *
294  * @param th handle that was returned by "notify_transmit_ready".
295  */
296 void
297 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
298                                           *th);
299
300
301 #if 0                           /* keep Emacsens' auto-indent happy */
302 {
303 #endif
304 #ifdef __cplusplus
305 }
306 #endif
307
308 /* ifndef GNUNET_CORE_SERVICE_H */
309 #endif
310 /* end of gnunet_core_service.h */