2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file include/gnunet_core_service.h
23 * @brief core service; this is the main API for encrypted P2P
25 * @author Christian Grothoff
28 #ifndef GNUNET_CORE_SERVICE_H
29 #define GNUNET_CORE_SERVICE_H
34 #if 0 /* keep Emacsens' auto-indent happy */
39 #include "gnunet_util_lib.h"
40 #include "gnunet_transport_service.h"
43 * Version number of GNUnet-core API.
45 #define GNUNET_CORE_VERSION 0x00000000
49 * Opaque handle to the service.
51 struct GNUNET_CORE_Handle;
55 * Method called whenever a given peer connects.
58 * @param peer peer identity this notification is about
59 * @param atsi performance data for the connection
60 * @param atsi_count number of records in 'atsi'
62 typedef void (*GNUNET_CORE_ConnectEventHandler) (void *cls,
64 GNUNET_PeerIdentity * peer,
66 GNUNET_ATS_Information * atsi,
67 unsigned int atsi_count);
71 * Method called whenever a peer disconnects.
74 * @param peer peer identity this notification is about
76 typedef void (*GNUNET_CORE_DisconnectEventHandler) (void *cls,
78 GNUNET_PeerIdentity * peer);
82 * Functions with this signature are called whenever a message is
83 * received or transmitted.
85 * @param cls closure (set from GNUNET_CORE_connect)
86 * @param peer the other peer involved (sender or receiver, NULL
87 * for loopback messages where we are both sender and receiver)
88 * @param message the actual message
89 * @param atsi performance data for the connection
90 * @param atsi_count number of records in 'atsi'
91 * @return GNUNET_OK to keep the connection open,
92 * GNUNET_SYSERR to close it (signal serious error)
94 typedef int (*GNUNET_CORE_MessageCallback) (void *cls,
95 const struct GNUNET_PeerIdentity *
97 const struct GNUNET_MessageHeader *
99 const struct GNUNET_ATS_Information
100 * atsi, unsigned int atsi_count);
104 * Message handler. Each struct specifies how to handle on particular
105 * type of message received.
107 struct GNUNET_CORE_MessageHandler
110 * Function to call for messages of "type".
112 GNUNET_CORE_MessageCallback callback;
115 * Type of the message this handler covers.
120 * Expected size of messages of this type. Use 0 for variable-size.
121 * If non-zero, messages of the given type will be discarded if they
122 * do not have the right size.
124 uint16_t expected_size;
130 * Function called after GNUNET_CORE_connect has succeeded (or failed
131 * for good). Note that the private key of the peer is intentionally
132 * not exposed here; if you need it, your process should try to read
133 * the private key file directly (which should work if you are
137 * @param server handle to the server, NULL if we failed
138 * @param my_identity ID of this peer, NULL if we failed
140 typedef void (*GNUNET_CORE_StartupCallback) (void *cls,
141 struct GNUNET_CORE_Handle * server,
142 const struct GNUNET_PeerIdentity *
147 * Connect to the core service. Note that the connection may complete
148 * (or fail) asynchronously. This function primarily causes the given
149 * callback notification functions to be invoked whenever the
150 * specified event happens. The maximum number of queued
151 * notifications (queue length) is per client but the queue is shared
152 * across all types of notifications. So a slow client that registers
153 * for 'outbound_notify' also risks missing 'inbound_notify' messages.
154 * Certain events (such as connect/disconnect notifications) are not
155 * subject to queue size limitations.
157 * @param cfg configuration to use
158 * @param queue_size size of the per-peer message queue
159 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
160 * @param init callback to call once we have successfully
161 * connected to the core service
162 * @param connects function to call on peer connect, can be NULL
163 * @param disconnects function to call on peer disconnect / timeout, can be NULL
164 * @param inbound_notify function to call for all inbound messages, can be NULL
165 * note that the core is allowed to drop notifications about inbound
166 * messages if the client does not process them fast enough (for this
167 * notification type, a bounded queue is used)
168 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
169 * GNUNET_MessageHeader and hence we do not need to give it the full message;
170 * can be used to improve efficiency, ignored if inbound_notify is NULL
171 * note that the core is allowed to drop notifications about inbound
172 * messages if the client does not process them fast enough (for this
173 * notification type, a bounded queue is used)
174 * @param outbound_notify function to call for all outbound messages, can be NULL;
175 * note that the core is allowed to drop notifications about outbound
176 * messages if the client does not process them fast enough (for this
177 * notification type, a bounded queue is used)
178 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
179 * GNUNET_MessageHeader and hence we do not need to give it the full message
180 * can be used to improve efficiency, ignored if outbound_notify is NULL
181 * note that the core is allowed to drop notifications about outbound
182 * messages if the client does not process them fast enough (for this
183 * notification type, a bounded queue is used)
184 * @param handlers callbacks for messages we care about, NULL-terminated
185 * note that the core is allowed to drop notifications about inbound
186 * messages if the client does not process them fast enough (for this
187 * notification type, a bounded queue is used)
188 * @return handle to the core service (only useful for disconnect until 'init' is called),
189 * NULL on error (in this case, init is never called)
191 struct GNUNET_CORE_Handle *
192 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
193 unsigned int queue_size, void *cls,
194 GNUNET_CORE_StartupCallback init,
195 GNUNET_CORE_ConnectEventHandler connects,
196 GNUNET_CORE_DisconnectEventHandler disconnects,
197 GNUNET_CORE_MessageCallback inbound_notify,
198 int inbound_hdr_only,
199 GNUNET_CORE_MessageCallback outbound_notify,
200 int outbound_hdr_only,
201 const struct GNUNET_CORE_MessageHandler *handlers);
205 * Disconnect from the core service. This function can only
206 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
207 * requests have been explicitly cancelled.
209 * @param handle connection to core to disconnect
212 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle);
216 * Handle for a transmission request.
218 struct GNUNET_CORE_TransmitHandle;
222 * Ask the core to call "notify" once it is ready to transmit the
223 * given number of bytes to the specified "target". Must only be
224 * called after a connection to the respective peer has been
225 * established (and the client has been informed about this).
228 * @param handle connection to core service
229 * @param cork is corking allowed for this transmission?
230 * @param priority how important is the message?
231 * @param maxdelay how long can the message wait?
232 * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
233 * @param notify_size how many bytes of buffer space does notify want?
234 * @param notify function to call when buffer space is available;
235 * will be called with NULL on timeout or if the overall queue
236 * for this peer is larger than queue_size and this is currently
237 * the message with the lowest priority; will also be called
238 * with 'NULL' buf if the peer disconnects; since the disconnect
239 * signal will be emmitted even later, clients MUST cancel
240 * all pending transmission requests DURING the disconnect
241 * handler (unless they ensure that 'notify' never calls
242 * 'GNUNET_CORE_notify_transmit_ready').
243 * @param notify_cls closure for notify
244 * @return non-NULL if the notify callback was queued,
245 * NULL if we can not even queue the request (insufficient
246 * memory); if NULL is returned, "notify" will NOT be called.
248 struct GNUNET_CORE_TransmitHandle *
249 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle, int cork,
251 struct GNUNET_TIME_Relative maxdelay,
252 const struct GNUNET_PeerIdentity *target,
254 GNUNET_CONNECTION_TransmitReadyNotify notify,
259 * Cancel the specified transmission-ready notification.
261 * @param th handle that was returned by "notify_transmit_ready".
264 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
272 * Iterate over all connected peers. Calls peer_cb with each
273 * connected peer, and then once with NULL to indicate that all peers
274 * have been handled. Normal users of the CORE API are not expected
275 * to use this function. It is different in that it truly lists
276 * all connections, not just those relevant to the application. This
277 * function is used by special applications for diagnostics. This
278 * function is NOT part of the 'versioned', 'official' API.
280 * FIXME: we should probably make it possible to 'cancel' the
283 * @param cfg configuration handle
284 * @param peer_cb function to call with the peer information
285 * @param cb_cls closure for peer_cb
286 * @return GNUNET_OK on success, GNUNET_SYSERR on errors
289 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
290 GNUNET_CORE_ConnectEventHandler peer_cb,
295 * Handle to cancel 'is_peer_connected' test.
297 struct GNUNET_CORE_ConnectTestHandle;
301 * Check if the given peer is currently connected and return information
302 * about the session if so. This function is for special cirumstances
303 * (GNUNET_TESTING uses it), normal users of the CORE API are
304 * expected to track which peers are connected based on the
305 * connect/disconnect callbacks from GNUNET_CORE_connect. This
306 * function is NOT part of the 'versioned', 'official' API.
308 * @param cfg configuration to use
309 * @param peer the specific peer to check for
310 * @param peer_cb function to call with the peer information
311 * @param cb_cls closure for peer_cb
312 * @return handle to cancel the operation
314 struct GNUNET_CORE_ConnectTestHandle *
315 GNUNET_CORE_is_peer_connected (const struct GNUNET_CONFIGURATION_Handle *cfg,
316 const struct GNUNET_PeerIdentity *peer,
317 GNUNET_CORE_ConnectEventHandler peer_cb,
322 * Abort 'is_connected' test operation.
324 * @param cth handle for operation to cancel
327 GNUNET_CORE_is_peer_connected_cancel (struct GNUNET_CORE_ConnectTestHandle *cth);
330 #if 0 /* keep Emacsens' auto-indent happy */
337 /* ifndef GNUNET_CORE_SERVICE_H */
339 /* end of gnunet_core_service.h */