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,
64 const struct GNUNET_TRANSPORT_ATS_Information *atsi);
68 * Method called whenever a given peer has a status change.
71 * @param peer peer identity this notification is about
72 * @param timeout absolute time when this peer will time out
73 * unless we see some further activity from it
74 * @param bandwidth_in available amount of inbound bandwidth
75 * @param bandwidth_out available amount of outbound bandwidth
76 * @param atsi performance data for the connection
78 typedef void (*GNUNET_CORE_PeerStatusEventHandler) (void *cls,
80 GNUNET_PeerIdentity *peer,
81 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
82 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
83 struct GNUNET_TIME_Absolute timeout,
84 const struct GNUNET_TRANSPORT_ATS_Information *atsi);
88 * Method called whenever a peer disconnects.
91 * @param peer peer identity this notification is about
93 typedef void (*GNUNET_CORE_DisconnectEventHandler) (void *cls,
95 GNUNET_PeerIdentity *peer);
99 * Functions with this signature are called whenever a message is
100 * received or transmitted.
102 * @param cls closure (set from GNUNET_CORE_connect)
103 * @param peer the other peer involved (sender or receiver, NULL
104 * for loopback messages where we are both sender and receiver)
105 * @param message the actual message
106 * @param atsi performance data for the connection
107 * @return GNUNET_OK to keep the connection open,
108 * GNUNET_SYSERR to close it (signal serious error)
111 (*GNUNET_CORE_MessageCallback) (void *cls,
112 const struct GNUNET_PeerIdentity *other,
113 const struct GNUNET_MessageHeader *message,
114 const struct GNUNET_TRANSPORT_ATS_Information *atsi);
118 * Message handler. Each struct specifies how to handle on particular
119 * type of message received.
121 struct GNUNET_CORE_MessageHandler
124 * Function to call for messages of "type".
126 GNUNET_CORE_MessageCallback callback;
129 * Type of the message this handler covers.
134 * Expected size of messages of this type. Use 0 for variable-size.
135 * If non-zero, messages of the given type will be discarded if they
136 * do not have the right size.
138 uint16_t expected_size;
144 * Function called after GNUNET_CORE_connect has succeeded
145 * (or failed for good). Note that the private key of the
146 * peer is intentionally not exposed here; if you need it,
147 * your process should try to read the private key file
148 * directly (which should work if you are authorized...).
151 * @param server handle to the server, NULL if we failed
152 * @param my_identity ID of this peer, NULL if we failed
153 * @param publicKey public key of this peer, NULL if we failed
156 (*GNUNET_CORE_StartupCallback) (void *cls,
157 struct GNUNET_CORE_Handle * server,
158 const struct GNUNET_PeerIdentity *
161 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
166 * Connect to the core service. Note that the connection may complete
167 * (or fail) asynchronously. This function primarily causes the given
168 * callback notification functions to be invoked whenever the
169 * specified event happens. The maximum number of queued
170 * notifications (queue length) is per client but the queue is shared
171 * across all types of notifications. So a slow client that registers
172 * for 'outbound_notify' also risks missing 'inbound_notify' messages.
173 * Certain events (such as connect/disconnect notifications) are not
174 * subject to queue size limitations.
176 * @param cfg configuration to use
177 * @param queue_size size of the per-peer message queue
178 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
179 * @param init callback to call on timeout or once we have successfully
180 * connected to the core service; note that timeout is only meaningful if init is not NULL
181 * @param connects function to call on peer connect, can be NULL
182 * @param disconnects function to call on peer disconnect / timeout, can be NULL
183 * @param status_events function to call on peer status changes, can be NULL
184 * @param inbound_notify function to call for all inbound messages, can be NULL
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 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
189 * GNUNET_MessageHeader and hence we do not need to give it the full message;
190 * can be used to improve efficiency, ignored if inbound_notify is 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 outbound_notify function to call for all outbound messages, can be NULL;
195 * note that the core is allowed to drop notifications about outbound
196 * messages if the client does not process them fast enough (for this
197 * notification type, a bounded queue is used)
198 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
199 * GNUNET_MessageHeader and hence we do not need to give it the full message
200 * can be used to improve efficiency, ignored if outbound_notify is 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 handlers callbacks for messages we care about, NULL-terminated
205 * note that the core is allowed to drop notifications about inbound
206 * messages if the client does not process them fast enough (for this
207 * notification type, a bounded queue is used)
208 * @return handle to the core service (only useful for disconnect until 'init' is called),
209 * NULL on error (in this case, init is never called)
211 struct GNUNET_CORE_Handle *
212 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
213 unsigned int queue_size,
215 GNUNET_CORE_StartupCallback init,
216 GNUNET_CORE_ConnectEventHandler connects,
217 GNUNET_CORE_DisconnectEventHandler disconnects,
218 GNUNET_CORE_PeerStatusEventHandler status_events,
219 GNUNET_CORE_MessageCallback inbound_notify,
220 int inbound_hdr_only,
221 GNUNET_CORE_MessageCallback outbound_notify,
222 int outbound_hdr_only,
223 const struct GNUNET_CORE_MessageHandler *handlers);
227 * Disconnect from the core service. This function can only
228 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
229 * requests have been explicitly cancelled.
231 * @param handle connection to core to disconnect
233 void GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle);
237 * Handle for a request to the core to connect or disconnect
238 * from a particular peer. Can be used to cancel the request
239 * (before the 'cont'inuation is called).
241 struct GNUNET_CORE_PeerRequestHandle;
245 * Type of function called upon completion.
248 * @param success GNUNET_OK on success (which for request_connect
249 * ONLY means that we transmitted the connect request to CORE,
250 * it does not mean that we are actually now connected!);
251 * GNUNET_NO on timeout,
252 * GNUNET_SYSERR if core was shut down
254 typedef void (*GNUNET_CORE_ControlContinuation)(void *cls, int success);
258 * Request that the core should try to connect to a particular peer.
259 * Once the request has been transmitted to the core, the continuation
260 * function will be called. Note that this does NOT mean that a
261 * connection was successfully established -- it only means that the
262 * core will now try. Successful establishment of the connection
263 * will be signalled to the 'connects' callback argument of
264 * 'GNUNET_CORE_connect' only. If the core service does not respond
265 * to our connection attempt within the given time frame, 'cont' will
266 * be called with the TIMEOUT reason code.
268 * @param h core handle
269 * @param peer who should we connect to
270 * @param cont function to call once the request has been completed (or timed out)
271 * @param cont_cls closure for cont
272 * @return NULL on error (cont will not be called), otherwise handle for cancellation
274 struct GNUNET_CORE_PeerRequestHandle *
275 GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
276 const struct GNUNET_PeerIdentity * peer,
277 GNUNET_CORE_ControlContinuation cont,
282 * Cancel a pending request to connect to a particular peer. Must not
283 * be called after the 'cont' function was invoked.
285 * @param req request handle that was returned for the original request
288 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req);
292 * Function called with perference change information about the given peer.
295 * @param peer identifies the peer
296 * @param bandwidth_out available amount of outbound bandwidth
297 * @param amount set to the amount that was actually reserved or unreserved;
298 * either the full requested amount or zero (no partial reservations)
299 * @param res_delay if the reservation could not be satisfied (amount was 0), how
300 * long should the client wait until re-trying?
301 * @param preference current traffic preference for the given peer
304 (*GNUNET_CORE_PeerConfigurationInfoCallback) (void *cls,
306 GNUNET_PeerIdentity *peer,
307 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
309 struct GNUNET_TIME_Relative res_delay,
310 uint64_t preference);
315 * Context that can be used to cancel a peer information request.
317 struct GNUNET_CORE_InformationRequestContext;
321 * Obtain statistics and/or change preferences for the given peer.
322 * You can only have one such pending request per peer.
324 * @param h core handle
325 * @param peer identifies the peer
326 * @param timeout after how long should we give up (and call "info" with NULL
327 * for "peer" to signal an error)?
328 * @param bw_out set to the current bandwidth limit (sending) for this peer,
329 * caller should set "bpm_out" to "GNUNET_BANDWIDTH_VALUE_MAX" to avoid changing
330 * the current value; otherwise "bw_out" will be lowered to
331 * the specified value; passing a pointer to "0" can be used to force
332 * us to disconnect from the peer; "bw_out" might not increase
333 * as specified since the upper bound is generally
334 * determined by the other peer!
335 * @param amount reserve N bytes for receiving, negative
336 * amounts can be used to undo a (recent) reservation;
337 * @param preference increase incoming traffic share preference by this amount;
338 * in the absence of "amount" reservations, we use this
339 * preference value to assign proportional bandwidth shares
340 * to all connected peers
341 * @param info function to call with the resulting configuration information
342 * @param info_cls closure for info
343 * @return NULL on error
345 struct GNUNET_CORE_InformationRequestContext *
346 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
347 const struct GNUNET_PeerIdentity *peer,
348 struct GNUNET_TIME_Relative timeout,
349 struct GNUNET_BANDWIDTH_Value32NBO bw_out,
352 GNUNET_CORE_PeerConfigurationInfoCallback info,
357 * Cancel request for getting information about a peer.
358 * Note that an eventual change in preference, trust or bandwidth
359 * assignment MAY have already been committed at the time,
360 * so cancelling a request is NOT sure to undo the original
361 * request. The original request may or may not still commit.
362 * The only thing cancellation ensures is that the callback
363 * from the original request will no longer be called.
365 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
368 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc);
372 * Iterate over all connected peers.
374 * @param cfg configuration handle
375 * @param peer_cb function to call with the peer information
376 * @param cb_cls closure for peer_cb
377 * @return GNUNET_OK on success, GNUNET_SYSERR on errors
380 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
381 GNUNET_CORE_ConnectEventHandler peer_cb,
385 * Iterate over all currently connected peers.
386 * Calls peer_cb with each connected peer, and then
387 * once with NULL to indicate that all peers have
390 * @param cfg configuration to use
391 * @param peer the specific peer to check for
392 * @param peer_cb function to call with the peer information
393 * @param cb_cls closure for peer_cb
395 * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
398 GNUNET_CORE_is_peer_connected (const struct GNUNET_CONFIGURATION_Handle *cfg,
399 struct GNUNET_PeerIdentity *peer,
400 GNUNET_CORE_ConnectEventHandler peer_cb,
405 * Handle for a transmission request.
407 struct GNUNET_CORE_TransmitHandle;
411 * Ask the core to call "notify" once it is ready to transmit the
412 * given number of bytes to the specified "target". Must only be
413 * called after a connection to the respective peer has been
414 * established (and the client has been informed about this).
417 * @param handle connection to core service
418 * @param cork is corking allowed for this transmission?
419 * @param priority how important is the message?
420 * @param maxdelay how long can the message wait?
421 * @param target who should receive the message,
422 * use NULL for this peer (loopback)
423 * @param notify_size how many bytes of buffer space does notify want?
424 * @param notify function to call when buffer space is available;
425 * will be called with NULL on timeout or if the overall queue
426 * for this peer is larger than queue_size and this is currently
427 * the message with the lowest priority; will also be called
428 * with 'NULL' buf if the peer disconnects; since the disconnect
429 * signal will be emmitted even later, clients MUST cancel
430 * all pending transmission requests DURING the disconnect
431 * handler (unless they ensure that 'notify' never calls
432 * 'GNUNET_CORE_notify_transmit_ready').
433 * @param notify_cls closure for notify
434 * @return non-NULL if the notify callback was queued,
435 * NULL if we can not even queue the request (insufficient
436 * memory); if NULL is returned, "notify" will NOT be called.
438 struct GNUNET_CORE_TransmitHandle *
439 GNUNET_CORE_notify_transmit_ready (struct
453 GNUNET_CONNECTION_TransmitReadyNotify
460 * Cancel the specified transmission-ready notification.
462 * @param th handle that was returned by "notify_transmit_ready".
465 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
469 #if 0 /* keep Emacsens' auto-indent happy */
476 /* ifndef GNUNET_CORE_SERVICE_H */
478 /* end of gnunet_core_service.h */