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 timeout how long to try to talk to core
270 * @param peer who should we connect to
271 * @param cont function to call once the request has been completed (or timed out)
272 * @param cont_cls closure for cont
273 * @return NULL on error (cont will not be called), otherwise handle for cancellation
275 struct GNUNET_CORE_PeerRequestHandle *
276 GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
277 struct GNUNET_TIME_Relative timeout,
278 const struct GNUNET_PeerIdentity * peer,
279 GNUNET_CORE_ControlContinuation cont,
284 * Cancel a pending request to connect to a particular peer. Must not
285 * be called after the 'cont' function was invoked.
287 * @param req request handle that was returned for the original request
290 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req);
294 * Function called with perference change information about the given peer.
297 * @param peer identifies the peer
298 * @param bandwidth_out available amount of outbound bandwidth
299 * @param amount set to the amount that was actually reserved or unreserved;
300 * either the full requested amount or zero (no partial reservations)
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 uint64_t preference);
314 * Context that can be used to cancel a peer information request.
316 struct GNUNET_CORE_InformationRequestContext;
320 * Obtain statistics and/or change preferences for the given peer.
321 * You can only have one such pending request per peer.
323 * @param h core handle
324 * @param peer identifies the peer
325 * @param timeout after how long should we give up (and call "info" with NULL
326 * for "peer" to signal an error)?
327 * @param bw_out set to the current bandwidth limit (sending) for this peer,
328 * caller should set "bpm_out" to "GNUNET_BANDWIDTH_VALUE_MAX" to avoid changing
329 * the current value; otherwise "bw_out" will be lowered to
330 * the specified value; passing a pointer to "0" can be used to force
331 * us to disconnect from the peer; "bw_out" might not increase
332 * as specified since the upper bound is generally
333 * determined by the other peer!
334 * @param amount reserve N bytes for receiving, negative
335 * amounts can be used to undo a (recent) reservation;
336 * @param preference increase incoming traffic share preference by this amount;
337 * in the absence of "amount" reservations, we use this
338 * preference value to assign proportional bandwidth shares
339 * to all connected peers
340 * @param info function to call with the resulting configuration information
341 * @param info_cls closure for info
342 * @return NULL on error
344 struct GNUNET_CORE_InformationRequestContext *
345 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
346 const struct GNUNET_PeerIdentity *peer,
347 struct GNUNET_TIME_Relative timeout,
348 struct GNUNET_BANDWIDTH_Value32NBO bw_out,
351 GNUNET_CORE_PeerConfigurationInfoCallback info,
356 * Cancel request for getting information about a peer.
357 * Note that an eventual change in preference, trust or bandwidth
358 * assignment MAY have already been committed at the time,
359 * so cancelling a request is NOT sure to undo the original
360 * request. The original request may or may not still commit.
361 * The only thing cancellation ensures is that the callback
362 * from the original request will no longer be called.
364 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
367 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc);
371 * Iterate over all connected peers.
373 * @param cfg configuration handle
374 * @param peer_cb function to call with the peer information
375 * @param cb_cls closure for peer_cb
376 * @return GNUNET_OK on success, GNUNET_SYSERR on errors
379 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
380 GNUNET_CORE_ConnectEventHandler peer_cb,
384 * Iterate over all currently connected peers.
385 * Calls peer_cb with each connected peer, and then
386 * once with NULL to indicate that all peers have
389 * @param cfg configuration to use
390 * @param peer the specific peer to check for
391 * @param peer_cb function to call with the peer information
392 * @param cb_cls closure for peer_cb
394 * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
397 GNUNET_CORE_is_peer_connected (const struct GNUNET_CONFIGURATION_Handle *cfg,
398 struct GNUNET_PeerIdentity *peer,
399 GNUNET_CORE_ConnectEventHandler peer_cb,
404 * Handle for a transmission request.
406 struct GNUNET_CORE_TransmitHandle;
410 * Ask the core to call "notify" once it is ready to transmit the
411 * given number of bytes to the specified "target". Must only be
412 * called after a connection to the respective peer has been
413 * established (and the client has been informed about this).
416 * @param handle connection to core service
417 * @param cork is corking allowed for this transmission?
418 * @param priority how important is the message?
419 * @param maxdelay how long can the message wait?
420 * @param target who should receive the message,
421 * use NULL for this peer (loopback)
422 * @param notify_size how many bytes of buffer space does notify want?
423 * @param notify function to call when buffer space is available;
424 * will be called with NULL on timeout or if the overall queue
425 * for this peer is larger than queue_size and this is currently
426 * the message with the lowest priority; will also be called
427 * with 'NULL' buf if the peer disconnects; since the disconnect
428 * signal will be emmitted even later, clients MUST cancel
429 * all pending transmission requests DURING the disconnect
430 * handler (unless they ensure that 'notify' never calls
431 * 'GNUNET_CORE_notify_transmit_ready').
432 * @param notify_cls closure for notify
433 * @return non-NULL if the notify callback was queued,
434 * NULL if we can not even queue the request (insufficient
435 * memory); if NULL is returned, "notify" will NOT be called.
437 struct GNUNET_CORE_TransmitHandle *
438 GNUNET_CORE_notify_transmit_ready (struct
452 GNUNET_CONNECTION_TransmitReadyNotify
459 * Cancel the specified transmission-ready notification.
461 * @param th handle that was returned by "notify_transmit_ready".
464 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
468 #if 0 /* keep Emacsens' auto-indent happy */
475 /* ifndef GNUNET_CORE_SERVICE_H */
477 /* end of gnunet_core_service.h */