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
61 typedef void (*GNUNET_CORE_ConnectEventHandler) (void *cls,
63 GNUNET_PeerIdentity * peer,
65 GNUNET_TRANSPORT_ATS_Information
70 * Method called whenever a given peer has a status change.
73 * @param peer peer identity this notification is about
74 * @param timeout absolute time when this peer will time out
75 * unless we see some further activity from it
76 * @param bandwidth_in available amount of inbound bandwidth
77 * @param bandwidth_out available amount of outbound bandwidth
78 * @param atsi performance data for the connection
80 typedef void (*GNUNET_CORE_PeerStatusEventHandler) (void *cls,
82 GNUNET_PeerIdentity * peer,
84 GNUNET_BANDWIDTH_Value32NBO
87 GNUNET_BANDWIDTH_Value32NBO
89 struct GNUNET_TIME_Absolute
92 GNUNET_TRANSPORT_ATS_Information
97 * Method called whenever a peer disconnects.
100 * @param peer peer identity this notification is about
102 typedef void (*GNUNET_CORE_DisconnectEventHandler) (void *cls,
104 GNUNET_PeerIdentity * peer);
108 * Functions with this signature are called whenever a message is
109 * received or transmitted.
111 * @param cls closure (set from GNUNET_CORE_connect)
112 * @param peer the other peer involved (sender or receiver, NULL
113 * for loopback messages where we are both sender and receiver)
114 * @param message the actual message
115 * @param atsi performance data for the connection
116 * @return GNUNET_OK to keep the connection open,
117 * GNUNET_SYSERR to close it (signal serious error)
119 typedef int (*GNUNET_CORE_MessageCallback) (void *cls,
120 const struct GNUNET_PeerIdentity *
122 const struct GNUNET_MessageHeader *
125 GNUNET_TRANSPORT_ATS_Information *
130 * Message handler. Each struct specifies how to handle on particular
131 * type of message received.
133 struct GNUNET_CORE_MessageHandler
136 * Function to call for messages of "type".
138 GNUNET_CORE_MessageCallback callback;
141 * Type of the message this handler covers.
146 * Expected size of messages of this type. Use 0 for variable-size.
147 * If non-zero, messages of the given type will be discarded if they
148 * do not have the right size.
150 uint16_t expected_size;
156 * Function called after GNUNET_CORE_connect has succeeded (or failed
157 * for good). Note that the private key of the peer is intentionally
158 * not exposed here; if you need it, your process should try to read
159 * the private key file directly (which should work if you are
163 * @param server handle to the server, NULL if we failed
164 * @param my_identity ID of this peer, NULL if we failed
166 typedef void (*GNUNET_CORE_StartupCallback) (void *cls,
167 struct GNUNET_CORE_Handle * server,
168 const struct GNUNET_PeerIdentity *my_identity);
172 * Connect to the core service. Note that the connection may complete
173 * (or fail) asynchronously. This function primarily causes the given
174 * callback notification functions to be invoked whenever the
175 * specified event happens. The maximum number of queued
176 * notifications (queue length) is per client but the queue is shared
177 * across all types of notifications. So a slow client that registers
178 * for 'outbound_notify' also risks missing 'inbound_notify' messages.
179 * Certain events (such as connect/disconnect notifications) are not
180 * subject to queue size limitations.
182 * @param cfg configuration to use
183 * @param queue_size size of the per-peer message queue
184 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
185 * @param init callback to call on timeout or once we have successfully
186 * connected to the core service; note that timeout is only meaningful if init is not NULL
187 * @param connects function to call on peer connect, can be NULL
188 * @param disconnects function to call on peer disconnect / timeout, can be NULL
189 * @param status_events function to call on peer status changes, can be NULL
190 * @param inbound_notify function to call for all inbound messages, can be NULL
191 * note that the core is allowed to drop notifications about inbound
192 * messages if the client does not process them fast enough (for this
193 * notification type, a bounded queue is used)
194 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
195 * GNUNET_MessageHeader and hence we do not need to give it the full message;
196 * can be used to improve efficiency, ignored if inbound_notify is NULL
197 * note that the core is allowed to drop notifications about inbound
198 * messages if the client does not process them fast enough (for this
199 * notification type, a bounded queue is used)
200 * @param outbound_notify function to call for all outbound messages, can be NULL;
201 * note that the core is allowed to drop notifications about outbound
202 * messages if the client does not process them fast enough (for this
203 * notification type, a bounded queue is used)
204 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
205 * GNUNET_MessageHeader and hence we do not need to give it the full message
206 * can be used to improve efficiency, ignored if outbound_notify is NULL
207 * note that the core is allowed to drop notifications about outbound
208 * messages if the client does not process them fast enough (for this
209 * notification type, a bounded queue is used)
210 * @param handlers callbacks for messages we care about, NULL-terminated
211 * note that the core is allowed to drop notifications about inbound
212 * messages if the client does not process them fast enough (for this
213 * notification type, a bounded queue is used)
214 * @return handle to the core service (only useful for disconnect until 'init' is called),
215 * NULL on error (in this case, init is never called)
217 struct GNUNET_CORE_Handle *
218 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
219 unsigned int queue_size, void *cls,
220 GNUNET_CORE_StartupCallback init,
221 GNUNET_CORE_ConnectEventHandler connects,
222 GNUNET_CORE_DisconnectEventHandler disconnects,
223 GNUNET_CORE_PeerStatusEventHandler status_events,
224 GNUNET_CORE_MessageCallback inbound_notify,
225 int inbound_hdr_only,
226 GNUNET_CORE_MessageCallback outbound_notify,
227 int outbound_hdr_only,
228 const struct GNUNET_CORE_MessageHandler *handlers);
232 * Disconnect from the core service. This function can only
233 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
234 * requests have been explicitly cancelled.
236 * @param handle connection to core to disconnect
239 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle);
243 * Function called with perference change information about the given peer.
246 * @param peer identifies the peer
247 * @param amount set to the amount that was actually reserved or unreserved;
248 * either the full requested amount or zero (no partial reservations)
249 * @param res_delay if the reservation could not be satisfied (amount was 0), how
250 * long should the client wait until re-trying?
252 typedef void (*GNUNET_CORE_PeerConfigurationInfoCallback) (void *cls,
254 GNUNET_PeerIdentity *
264 * Context that can be used to cancel a peer information request.
266 struct GNUNET_CORE_InformationRequestContext;
270 * Obtain statistics and/or change preferences for the given peer.
271 * You can only have one such pending request per peer.
273 * @param h core handle
274 * @param peer identifies the peer
275 * @param amount reserve N bytes for receiving, negative
276 * amounts can be used to undo a (recent) reservation;
277 * @param preference increase incoming traffic share preference by this amount;
278 * in the absence of "amount" reservations, we use this
279 * preference value to assign proportional bandwidth shares
280 * to all connected peers
281 * @param info function to call with the resulting configuration information
282 * @param info_cls closure for info
283 * @return NULL on error
285 struct GNUNET_CORE_InformationRequestContext *
286 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
287 const struct GNUNET_PeerIdentity *peer,
288 int32_t amount, uint64_t preference,
289 GNUNET_CORE_PeerConfigurationInfoCallback
290 info, void *info_cls);
294 * Cancel request for getting information about a peer.
295 * Note that an eventual change in preference, trust or bandwidth
296 * assignment MAY have already been committed at the time,
297 * so cancelling a request is NOT sure to undo the original
298 * request. The original request may or may not still commit.
299 * The only thing cancellation ensures is that the callback
300 * from the original request will no longer be called.
302 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
305 GNUNET_CORE_peer_change_preference_cancel (struct
306 GNUNET_CORE_InformationRequestContext
311 * Iterate over all connected peers.
313 * @param cfg configuration handle
314 * @param peer_cb function to call with the peer information
315 * @param cb_cls closure for peer_cb
316 * @return GNUNET_OK on success, GNUNET_SYSERR on errors
319 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
320 GNUNET_CORE_ConnectEventHandler peer_cb,
324 * Iterate over all currently connected peers.
325 * Calls peer_cb with each connected peer, and then
326 * once with NULL to indicate that all peers have
329 * @param cfg configuration to use
330 * @param peer the specific peer to check for
331 * @param peer_cb function to call with the peer information
332 * @param cb_cls closure for peer_cb
334 * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
337 GNUNET_CORE_is_peer_connected (const struct GNUNET_CONFIGURATION_Handle *cfg,
338 struct GNUNET_PeerIdentity *peer,
339 GNUNET_CORE_ConnectEventHandler peer_cb,
344 * Handle for a transmission request.
346 struct GNUNET_CORE_TransmitHandle;
350 * Ask the core to call "notify" once it is ready to transmit the
351 * given number of bytes to the specified "target". Must only be
352 * called after a connection to the respective peer has been
353 * established (and the client has been informed about this).
356 * @param handle connection to core service
357 * @param cork is corking allowed for this transmission?
358 * @param priority how important is the message?
359 * @param maxdelay how long can the message wait?
360 * @param target who should receive the message,
361 * use NULL for this peer (loopback)
362 * @param notify_size how many bytes of buffer space does notify want?
363 * @param notify function to call when buffer space is available;
364 * will be called with NULL on timeout or if the overall queue
365 * for this peer is larger than queue_size and this is currently
366 * the message with the lowest priority; will also be called
367 * with 'NULL' buf if the peer disconnects; since the disconnect
368 * signal will be emmitted even later, clients MUST cancel
369 * all pending transmission requests DURING the disconnect
370 * handler (unless they ensure that 'notify' never calls
371 * 'GNUNET_CORE_notify_transmit_ready').
372 * @param notify_cls closure for notify
373 * @return non-NULL if the notify callback was queued,
374 * NULL if we can not even queue the request (insufficient
375 * memory); if NULL is returned, "notify" will NOT be called.
377 struct GNUNET_CORE_TransmitHandle *
378 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle, int cork,
380 struct GNUNET_TIME_Relative maxdelay,
381 const struct GNUNET_PeerIdentity *target,
383 GNUNET_CONNECTION_TransmitReadyNotify notify,
388 * Cancel the specified transmission-ready notification.
390 * @param th handle that was returned by "notify_transmit_ready".
393 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
397 #if 0 /* keep Emacsens' auto-indent happy */
404 /* ifndef GNUNET_CORE_SERVICE_H */
406 /* end of gnunet_core_service.h */