2 This file is part of GNUnet.
3 (C) 2009-2013 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
26 * @defgroup core encrypted direct communication between peers
30 #ifndef GNUNET_CORE_SERVICE_H
31 #define GNUNET_CORE_SERVICE_H
36 #if 0 /* keep Emacsens' auto-indent happy */
41 #include "gnunet_util_lib.h"
42 #include "gnunet_transport_service.h"
45 * Version number of GNUnet-core API.
47 #define GNUNET_CORE_VERSION 0x00000001
51 * Opaque handle to the service.
53 struct GNUNET_CORE_Handle;
57 * Method called whenever a given peer connects.
60 * @param peer peer identity this notification is about
62 typedef void (*GNUNET_CORE_ConnectEventHandler) (void *cls,
63 const struct GNUNET_PeerIdentity *peer);
67 * Method called whenever a peer disconnects.
70 * @param peer peer identity this notification is about
72 typedef void (*GNUNET_CORE_DisconnectEventHandler) (void *cls,
73 const struct GNUNET_PeerIdentity *peer);
77 * Functions with this signature are called whenever a message is
78 * received or transmitted.
80 * @param cls closure (set from #GNUNET_CORE_connect)
81 * @param peer the other peer involved (sender or receiver, NULL
82 * for loopback messages where we are both sender and receiver)
83 * @param message the actual message
84 * @return #GNUNET_OK to keep the connection open,
85 * #GNUNET_SYSERR to close connection to the peer (signal serious error)
87 typedef int (*GNUNET_CORE_MessageCallback) (void *cls,
88 const struct GNUNET_PeerIdentity *
90 const struct GNUNET_MessageHeader *
95 * Message handler. Each struct specifies how to handle on particular
96 * type of message received.
98 struct GNUNET_CORE_MessageHandler
101 * Function to call for messages of "type".
103 GNUNET_CORE_MessageCallback callback;
106 * Type of the message this handler covers.
111 * Expected size of messages of this type. Use 0 for variable-size.
112 * If non-zero, messages of the given type will be discarded if they
113 * do not have the right size.
115 uint16_t expected_size;
121 * Function called after #GNUNET_CORE_connect has succeeded (or failed
122 * for good). Note that the private key of the peer is intentionally
123 * not exposed here; if you need it, your process should try to read
124 * the private key file directly (which should work if you are
125 * authorized...). Implementations of this function must not call
126 * #GNUNET_CORE_disconnect (other than by scheduling a new task to
130 * @param my_identity ID of this peer, NULL if we failed
132 typedef void (*GNUNET_CORE_StartupCallback) (void *cls,
133 const struct GNUNET_PeerIdentity *my_identity);
137 * Connect to the core service. Note that the connection may complete
138 * (or fail) asynchronously. This function primarily causes the given
139 * callback notification functions to be invoked whenever the
140 * specified event happens. The maximum number of queued
141 * notifications (queue length) is per client; the queue is shared
142 * across all types of notifications. So a slow client that registers
143 * for @a outbound_notify also risks missing @a inbound_notify messages.
144 * Certain events (such as connect/disconnect notifications) are not
145 * subject to queue size limitations.
147 * @param cfg configuration to use
148 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
149 * @param init callback to call once we have successfully
150 * connected to the core service
151 * @param connects function to call on peer connect, can be NULL
152 * @param disconnects function to call on peer disconnect / timeout, can be NULL
153 * @param inbound_notify function to call for all inbound messages, can be NULL
154 * note that the core is allowed to drop notifications about inbound
155 * messages if the client does not process them fast enough (for this
156 * notification type, a bounded queue is used)
157 * @param inbound_hdr_only set to #GNUNET_YES if @a inbound_notify will only read the
158 * `struct GNUNET_MessageHeader` and hence we do not need to give it the full message;
159 * can be used to improve efficiency, ignored if inbound_notify is NULL
160 * note that the core is allowed to drop notifications about inbound
161 * messages if the client does not process them fast enough (for this
162 * notification type, a bounded queue is used)
163 * @param outbound_notify function to call for all outbound messages, can be NULL;
164 * note that the core is allowed to drop notifications about outbound
165 * messages if the client does not process them fast enough (for this
166 * notification type, a bounded queue is used)
167 * @param outbound_hdr_only set to #GNUNET_YES if @a outbound_notify will only read the
168 * `struct GNUNET_MessageHeader` and hence we do not need to give it the full message
169 * can be used to improve efficiency, ignored if outbound_notify is NULL
170 * note that the core is allowed to drop notifications about outbound
171 * messages if the client does not process them fast enough (for this
172 * notification type, a bounded queue is used)
173 * @param handlers callbacks for messages we care about, NULL-terminated
174 * note that the core is allowed to drop notifications about inbound
175 * messages if the client does not process them fast enough (for this
176 * notification type, a bounded queue is used)
177 * @return handle to the core service (only useful for disconnect until @a init is called),
178 * NULL on error (in this case, init is never called)
180 struct GNUNET_CORE_Handle *
181 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
183 GNUNET_CORE_StartupCallback init,
184 GNUNET_CORE_ConnectEventHandler connects,
185 GNUNET_CORE_DisconnectEventHandler disconnects,
186 GNUNET_CORE_MessageCallback inbound_notify,
187 int inbound_hdr_only,
188 GNUNET_CORE_MessageCallback outbound_notify,
189 int outbound_hdr_only,
190 const struct GNUNET_CORE_MessageHandler *handlers);
194 * Disconnect from the core service. This function can only
195 * be called *after* all pending #GNUNET_CORE_notify_transmit_ready
196 * requests have been explicitly cancelled.
198 * @param handle connection to core to disconnect
201 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle);
205 * Handle for a transmission request.
207 struct GNUNET_CORE_TransmitHandle;
211 * Ask the core to call @a notify once it is ready to transmit the
212 * given number of bytes to the specified @a target. Must only be
213 * called after a connection to the respective peer has been
214 * established (and the client has been informed about this). You may
215 * have one request of this type pending for each connected peer at
216 * any time. If a peer disconnects, the application MUST call
217 * #GNUNET_CORE_notify_transmit_ready_cancel on the respective
218 * transmission request, if one such request is pending.
220 * @param handle connection to core service
221 * @param cork is corking allowed for this transmission?
222 * @param priority how important is the message?
223 * @param maxdelay how long can the message wait?
224 * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
225 * @param notify_size how many bytes of buffer space does notify want?
226 * @param notify function to call when buffer space is available;
227 * will be called with NULL on timeout; clients MUST cancel
228 * all pending transmission requests DURING the disconnect
230 * @param notify_cls closure for @a notify
231 * @return non-NULL if the notify callback was queued,
232 * NULL if we can not even queue the request (request already pending);
233 * if NULL is returned, "notify" will NOT be called.
235 struct GNUNET_CORE_TransmitHandle *
236 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle, int cork,
238 struct GNUNET_TIME_Relative maxdelay,
239 const struct GNUNET_PeerIdentity *target,
241 GNUNET_CONNECTION_TransmitReadyNotify notify,
246 * Cancel the specified transmission-ready notification.
248 * @param th handle that was returned by "notify_transmit_ready".
251 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
256 * Iterate over all connected peers. Calls @a peer_cb with each
257 * connected peer, and then once with NULL to indicate that all peers
258 * have been handled. Normal users of the CORE API are not expected
259 * to use this function. It is different in that it truly lists
260 * all connections, not just those relevant to the application. This
261 * function is used by special applications for diagnostics. This
262 * function is NOT part of the 'versioned', 'official' API.
264 * FIXME: we should probably make it possible to 'cancel' the
267 * @param cfg configuration handle
268 * @param peer_cb function to call with the peer information
269 * @param cb_cls closure for @a peer_cb
270 * @return #GNUNET_OK on success, #GNUNET_SYSERR on errors
273 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
274 GNUNET_CORE_ConnectEventHandler peer_cb,
279 * Check if the given peer is currently connected. This function is for special
280 * cirumstances (GNUNET_TESTBED uses it), normal users of the CORE API are
281 * expected to track which peers are connected based on the connect/disconnect
282 * callbacks from #GNUNET_CORE_connect. This function is NOT part of the
283 * 'versioned', 'official' API. This function returns
284 * synchronously after looking in the CORE API cache.
286 * @param h the core handle
287 * @param pid the identity of the peer to check if it has been connected to us
288 * @return #GNUNET_YES if the peer is connected to us; #GNUNET_NO if not
291 GNUNET_CORE_is_peer_connected_sync (const struct GNUNET_CORE_Handle *h,
292 const struct GNUNET_PeerIdentity *pid);
296 * Create a message queue for sending messages to a peer with CORE.
297 * Messages may only be queued with #GNUNET_MQ_send once the init callback has
298 * been called for the given handle.
299 * There must only be one queue per peer for each core handle.
300 * The message queue can only be used to transmit messages,
301 * not to receive them.
303 * @param h the core handle
304 * @param target the target peer for this queue, may not be NULL
305 * @return a message queue for sending messages over the core handle
308 struct GNUNET_MQ_Handle *
309 GNUNET_CORE_mq_create (struct GNUNET_CORE_Handle *h,
310 const struct GNUNET_PeerIdentity *target);
313 #if 0 /* keep Emacsens' auto-indent happy */
320 /** @} */ /* end of group core */
322 /* ifndef GNUNET_CORE_SERVICE_H */
324 /* end of gnunet_core_service.h */