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 2, 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"
42 * Version number of GNUnet-core API.
44 #define GNUNET_CORE_VERSION 0x00000000
48 * Opaque handle to the service.
50 struct GNUNET_CORE_Handle;
54 * Method called whenever a given peer connects.
57 * @param peer peer identity this notification is about
58 * @param latency reported latency of the connection with 'other'
59 * @param distance reported distance (DV) to 'other'
61 typedef void (*GNUNET_CORE_ConnectEventHandler) (void *cls,
63 GNUNET_PeerIdentity * peer,
64 struct GNUNET_TIME_Relative latency,
70 * Method called whenever a given peer either disconnects.
73 * @param peer peer identity this notification is about
75 typedef void (*GNUNET_CORE_DisconnectEventHandler) (void *cls,
77 GNUNET_PeerIdentity * peer);
81 * Functions with this signature are called whenever a message is
82 * received or transmitted.
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 latency reported latency of the connection with 'other'
89 * @param distance reported distance (DV) to 'other'
90 * @return GNUNET_OK to keep the connection open,
91 * GNUNET_SYSERR to close it (signal serious error)
94 (*GNUNET_CORE_MessageCallback) (void *cls,
95 const struct GNUNET_PeerIdentity * other,
96 const struct GNUNET_MessageHeader *
98 struct GNUNET_TIME_Relative latency,
103 * Message handler. Each struct specifies how to handle on particular
104 * type of message received.
106 struct GNUNET_CORE_MessageHandler
109 * Function to call for messages of "type".
111 GNUNET_CORE_MessageCallback callback;
114 * Type of the message this handler covers.
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.
123 uint16_t expected_size;
129 * Function called after GNUNET_CORE_connect has succeeded
130 * (or failed for good). Note that the private key of the
131 * peer is intentionally not exposed here; if you need it,
132 * your process should try to read the private key file
133 * directly (which should work if you are authorized...).
136 * @param server handle to the server, NULL if we failed
137 * @param my_identity ID of this peer, NULL if we failed
138 * @param publicKey public key of this peer, NULL if we failed
141 (*GNUNET_CORE_StartupCallback) (void *cls,
142 struct GNUNET_CORE_Handle * server,
143 const struct GNUNET_PeerIdentity *
146 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
151 * Connect to the core service. Note that the connection may complete
152 * (or fail) asynchronously. This function primarily causes the given
153 * callback notification functions to be invoked whenever the
154 * specified event happens. The maximum number of queued
155 * notifications (queue length) is per client but the queue is shared
156 * across all types of notifications. So a slow client that registers
157 * for 'outbound_notify' also risks missing 'inbound_notify' messages.
158 * Certain events (such as connect/disconnect notifications) are not
159 * subject to queue size limitations.
161 * @param sched scheduler to use
162 * @param cfg configuration to use
163 * @param timeout after how long should we give up trying to connect to the core service?
164 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
165 * @param init callback to call on timeout or once we have successfully
166 * connected to the core service; note that timeout is only meaningful if init is not NULL
167 * @param connects function to call on peer connect, can be NULL
168 * @param disconnects function to call on peer disconnect / timeout, can be NULL
169 * @param inbound_notify function to call for all inbound messages, can be NULL
170 * note that the core is allowed to drop notifications about inbound
171 * messages if the client does not process them fast enough (for this
172 * notification type, a bounded queue is used)
173 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
174 * GNUNET_MessageHeader and hence we do not need to give it the full message;
175 * can be used to improve efficiency, ignored if inbound_notify is NULL
176 * note that the core is allowed to drop notifications about inbound
177 * messages if the client does not process them fast enough (for this
178 * notification type, a bounded queue is used)
179 * @param outbound_notify function to call for all outbound messages, can be NULL;
180 * note that the core is allowed to drop notifications about outbound
181 * messages if the client does not process them fast enough (for this
182 * notification type, a bounded queue is used)
183 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
184 * GNUNET_MessageHeader and hence we do not need to give it the full message
185 * can be used to improve efficiency, ignored if outbound_notify is NULL
186 * note that the core is allowed to drop notifications about outbound
187 * messages if the client does not process them fast enough (for this
188 * notification type, a bounded queue is used)
189 * @param handlers callbacks for messages we care about, NULL-terminated
190 * note that the core is allowed to drop notifications about inbound
191 * messages if the client does not process them fast enough (for this
192 * notification type, a bounded queue is used)
193 * @return handle to the core service (only useful for disconnect until 'init' is called),
194 * NULL on error (in this case, init is never called)
196 struct GNUNET_CORE_Handle *
197 GNUNET_CORE_connect (struct GNUNET_SCHEDULER_Handle *sched,
198 const struct GNUNET_CONFIGURATION_Handle *cfg,
199 struct GNUNET_TIME_Relative timeout,
201 GNUNET_CORE_StartupCallback init,
202 GNUNET_CORE_ConnectEventHandler connects,
203 GNUNET_CORE_DisconnectEventHandler disconnects,
204 GNUNET_CORE_MessageCallback inbound_notify,
205 int inbound_hdr_only,
206 GNUNET_CORE_MessageCallback outbound_notify,
207 int outbound_hdr_only,
208 const struct GNUNET_CORE_MessageHandler *handlers);
212 * Disconnect from the core service.
214 * @param handle connection to core to disconnect
216 void GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle);
220 * Handle for a request to the core to connect or disconnect
221 * from a particular peer. Can be used to cancel the request
222 * (before the 'cont'inuation is called).
224 struct GNUNET_CORE_PeerRequestHandle;
228 * Request that the core should try to connect to a particular peer.
229 * Once the request has been transmitted to the core, the continuation
230 * function will be called. Note that this does NOT mean that a
231 * connection was successfully established -- it only means that the
232 * core will now try. Successful establishment of the connection
233 * will be signalled to the 'connects' callback argument of
234 * 'GNUNET_CORE_connect' only. If the core service does not respond
235 * to our connection attempt within the given time frame, 'cont' will
236 * be called with the TIMEOUT reason code.
238 * @param sched scheduler to use
239 * @param cfg configuration to use
240 * @param timeout how long to try to talk to core
241 * @param peer who should we connect to
242 * @param cont function to call once the request has been completed (or timed out)
243 * @param cont_cls closure for cont
244 * @return NULL on error (cont will not be called), otherwise handle for cancellation
246 struct GNUNET_CORE_PeerRequestHandle *
247 GNUNET_CORE_peer_request_connect (struct GNUNET_SCHEDULER_Handle *sched,
248 const struct GNUNET_CONFIGURATION_Handle *cfg,
249 struct GNUNET_TIME_Relative timeout,
250 const struct GNUNET_PeerIdentity * peer,
251 GNUNET_SCHEDULER_Task cont,
256 * Cancel a pending request to connect to a particular peer. Must not
257 * be called after the 'cont' function was invoked.
259 * @param req request handle that was returned for the original request
262 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req);
266 * Function called with statistics about the given peer.
269 * @param peer identifies the peer
270 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
271 * @param bpm_out set to the current bandwidth limit (sending) for this peer
272 * @param latency current latency estimate, "FOREVER" if we have been
274 * @param amount set to the amount that was actually reserved or unreserved;
275 * either the full requested amount or zero (no partial reservations)
276 * @param preference current traffic preference for the given peer
279 (*GNUNET_CORE_PeerConfigurationInfoCallback) (void *cls,
281 GNUNET_PeerIdentity * peer,
282 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
283 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
285 uint64_t preference);
290 * Context that can be used to cancel a peer information request.
292 struct GNUNET_CORE_InformationRequestContext;
296 * Obtain statistics and/or change preferences for the given peer.
298 * @param sched scheduler to use
299 * @param cfg configuration to use
300 * @param peer identifies the peer
301 * @param timeout after how long should we give up (and call "info" with NULL
302 * for "peer" to signal an error)?
303 * @param bw_out set to the current bandwidth limit (sending) for this peer,
304 * caller should set "bpm_out" to "-1" to avoid changing
305 * the current value; otherwise "bw_out" will be lowered to
306 * the specified value; passing a pointer to "0" can be used to force
307 * us to disconnect from the peer; "bw_out" might not increase
308 * as specified since the upper bound is generally
309 * determined by the other peer!
310 * @param amount reserve N bytes for receiving, negative
311 * amounts can be used to undo a (recent) reservation;
312 * @param preference increase incoming traffic share preference by this amount;
313 * in the absence of "amount" reservations, we use this
314 * preference value to assign proportional bandwidth shares
315 * to all connected peers
316 * @param info function to call with the resulting configuration information
317 * @param info_cls closure for info
318 * @return NULL on error
320 struct GNUNET_CORE_InformationRequestContext *
321 GNUNET_CORE_peer_change_preference (struct GNUNET_SCHEDULER_Handle *sched,
322 const struct GNUNET_CONFIGURATION_Handle *cfg,
323 const struct GNUNET_PeerIdentity *peer,
324 struct GNUNET_TIME_Relative timeout,
325 struct GNUNET_BANDWIDTH_Value32NBO bw_out,
328 GNUNET_CORE_PeerConfigurationInfoCallback info,
333 * Cancel request for getting information about a peer.
335 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
338 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc);
342 * Handle for a transmission request.
344 struct GNUNET_CORE_TransmitHandle;
348 * Ask the core to call "notify" once it is ready to transmit the
349 * given number of bytes to the specified "target". If we are not yet
350 * connected to the specified peer, a call to this function will cause
351 * us to try to establish a connection.
353 * @param handle connection to core service
354 * @param priority how important is the message?
355 * @param maxdelay how long can the message wait?
356 * @param target who should receive the message,
357 * use NULL for this peer (loopback)
358 * @param notify_size how many bytes of buffer space does notify want?
359 * @param notify function to call when buffer space is available
360 * @param notify_cls closure for notify
361 * @return non-NULL if the notify callback was queued,
362 * NULL if we can not even queue the request (insufficient
363 * memory); if NULL is returned, "notify" will NOT be called.
365 struct GNUNET_CORE_TransmitHandle *
366 GNUNET_CORE_notify_transmit_ready (struct
379 GNUNET_CONNECTION_TransmitReadyNotify
386 * Cancel the specified transmission-ready notification.
388 * @param th handle that was returned by "notify_transmit_ready".
391 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
395 #if 0 /* keep Emacsens' auto-indent happy */
402 /* ifndef GNUNET_CORE_SERVICE_H */
404 /* end of gnunet_core_service.h */