change hostname in config files when created, if running remotely... Otherwise the...
[oweals/gnunet.git] / src / include / gnunet_core_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file include/gnunet_core_service.h
23  * @brief core service; this is the main API for encrypted P2P
24  *        communications
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_CORE_SERVICE_H
29 #define GNUNET_CORE_SERVICE_H
30
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #if 0                           /* keep Emacsens' auto-indent happy */
35 }
36 #endif
37 #endif
38
39 #include "gnunet_util_lib.h"
40
41 /**
42  * Version number of GNUnet-core API.
43  */
44 #define GNUNET_CORE_VERSION 0x00000000
45
46
47 /**
48  * Opaque handle to the service.
49  */
50 struct GNUNET_CORE_Handle;
51
52
53 /**
54  * Method called whenever a given peer either connects.
55  *
56  * @param cls closure
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' 
60  */
61 typedef void (*GNUNET_CORE_ConnectEventHandler) (void *cls,
62                                                  const struct
63                                                  GNUNET_PeerIdentity * peer,
64                                                  struct GNUNET_TIME_Relative latency,
65                                                  uint32_t distance);
66
67
68
69 /**
70  * Method called whenever a given peer either disconnects.
71  *
72  * @param cls closure
73  * @param peer peer identity this notification is about
74  */
75 typedef void (*GNUNET_CORE_DisconnectEventHandler) (void *cls,
76                                                     const struct
77                                                     GNUNET_PeerIdentity * peer);
78
79
80 /**
81  * Functions with this signature are called whenever a message is
82  * received or transmitted.
83  *
84  * @param cls closure
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)
92  */
93 typedef int
94   (*GNUNET_CORE_MessageCallback) (void *cls,
95                                   const struct GNUNET_PeerIdentity * other,
96                                   const struct GNUNET_MessageHeader *
97                                   message,
98                                   struct GNUNET_TIME_Relative latency,
99                                   uint32_t distance);
100
101
102 /**
103  * Message handler.  Each struct specifies how to handle on particular
104  * type of message received.
105  */
106 struct GNUNET_CORE_MessageHandler
107 {
108   /**
109    * Function to call for messages of "type".
110    */
111   GNUNET_CORE_MessageCallback callback;
112
113   /**
114    * Type of the message this handler covers.
115    */
116   uint16_t type;
117
118   /**
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.
122    */
123   uint16_t expected_size;
124
125 };
126
127
128 /**
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...).
134  *
135  * @param cls closure
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
139  */
140 typedef void
141   (*GNUNET_CORE_StartupCallback) (void *cls,
142                                   struct GNUNET_CORE_Handle * server,
143                                   const struct GNUNET_PeerIdentity *
144                                   my_identity,
145                                   const struct
146                                   GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
147                                   publicKey);
148
149
150 /**
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.
160  *
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 pre_connects function to call on peer pre-connect (no session key yet), can be NULL
168  * @param connects function to call on peer connect, can be NULL
169  * @param disconnects function to call on peer disconnect / timeout, can be NULL
170  * @param inbound_notify function to call for all inbound messages, can be NULL
171  *                note that the core is allowed to drop notifications about inbound
172  *                messages if the client does not process them fast enough (for this
173  *                notification type, a bounded queue is used)
174  * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
175  *                GNUNET_MessageHeader and hence we do not need to give it the full message;
176  *                can be used to improve efficiency, ignored if inbound_notify is NULL
177  *                note that the core is allowed to drop notifications about inbound
178  *                messages if the client does not process them fast enough (for this
179  *                notification type, a bounded queue is used) 
180  * @param outbound_notify function to call for all outbound messages, can be NULL;
181  *                note that the core is allowed to drop notifications about outbound
182  *                messages if the client does not process them fast enough (for this
183  *                notification type, a bounded queue is used)
184  * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
185  *                GNUNET_MessageHeader and hence we do not need to give it the full message
186  *                can be used to improve efficiency, ignored if outbound_notify is NULL
187  *                note that the core is allowed to drop notifications about outbound
188  *                messages if the client does not process them fast enough (for this
189  *                notification type, a bounded queue is used)
190  * @param handlers callbacks for messages we care about, NULL-terminated
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  * @return handle to the core service (only useful for disconnect until 'init' is called),
195  *           NULL on error (in this case, init is never called)
196  */
197 struct GNUNET_CORE_Handle *
198 GNUNET_CORE_connect (struct GNUNET_SCHEDULER_Handle *sched,
199                      const struct GNUNET_CONFIGURATION_Handle *cfg,
200                      struct GNUNET_TIME_Relative timeout,
201                      void *cls,
202                      GNUNET_CORE_StartupCallback init,
203                      GNUNET_CORE_ConnectEventHandler pre_connects,
204                      GNUNET_CORE_ConnectEventHandler connects,
205                      GNUNET_CORE_DisconnectEventHandler disconnects,
206                      GNUNET_CORE_MessageCallback inbound_notify,
207                      int inbound_hdr_only,
208                      GNUNET_CORE_MessageCallback outbound_notify,
209                      int outbound_hdr_only,
210                      const struct GNUNET_CORE_MessageHandler *handlers);
211
212
213 /**
214  * Disconnect from the core service.
215  *
216  * @param handle connection to core to disconnect
217  */
218 void GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle);
219
220
221 /**
222  * Handle for a request to the core to connect or disconnect
223  * from a particular peer.  Can be used to cancel the request
224  * (before the 'cont'inuation is called).
225  */
226 struct GNUNET_CORE_PeerRequestHandle;
227
228
229 /**
230  * Request that the core should try to connect to a particular peer.
231  * Once the request has been transmitted to the core, the continuation
232  * function will be called.  Note that this does NOT mean that a
233  * connection was successfully established -- it only means that the
234  * core will now try.  Successful establishment of the connection
235  * will be signalled to the 'connects' callback argument of
236  * 'GNUNET_CORE_connect' only.  If the core service does not respond
237  * to our connection attempt within the given time frame, 'cont' will
238  * be called with the TIMEOUT reason code.
239  *
240  * @param sched scheduler to use
241  * @param cfg configuration to use
242  * @param timeout how long to try to talk to core
243  * @param peer who should we connect to
244  * @param cont function to call once the request has been completed (or timed out)
245  * @param cont_cls closure for cont
246  * @return NULL on error (cont will not be called), otherwise handle for cancellation
247  */
248 struct GNUNET_CORE_PeerRequestHandle *
249 GNUNET_CORE_peer_request_connect (struct GNUNET_SCHEDULER_Handle *sched,
250                                   const struct GNUNET_CONFIGURATION_Handle *cfg,
251                                   struct GNUNET_TIME_Relative timeout,
252                                   const struct GNUNET_PeerIdentity * peer,
253                                   GNUNET_SCHEDULER_Task cont,
254                                   void *cont_cls);
255
256
257 /**
258  * Cancel a pending request to connect to a particular peer.  Must not
259  * be called after the 'cont' function was invoked.
260  *
261  * @param req request handle that was returned for the original request
262  */
263 void
264 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req);
265
266
267 /**
268  * Function called with statistics about the given peer.
269  *
270  * @param cls closure
271  * @param peer identifies the peer
272  * @param bpm_in set to the current bandwidth limit (receiving) for this peer
273  * @param bpm_out set to the current bandwidth limit (sending) for this peer
274  * @param latency current latency estimate, "FOREVER" if we have been
275  *                disconnected
276  * @param amount set to the amount that was actually reserved or unreserved;
277  *               either the full requested amount or zero (no partial reservations)
278  * @param preference current traffic preference for the given peer
279  */
280 typedef void
281   (*GNUNET_CORE_PeerConfigurationInfoCallback) (void *cls,
282                                                 const struct
283                                                 GNUNET_PeerIdentity * peer,
284                                                 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
285                                                 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
286                                                 int amount,
287                                                 uint64_t preference);
288
289
290
291 /**
292  * Context that can be used to cancel a peer information request.
293  */
294 struct GNUNET_CORE_InformationRequestContext;
295
296
297 /**
298  * Obtain statistics and/or change preferences for the given peer.
299  *
300  * @param sched scheduler to use
301  * @param cfg configuration to use
302  * @param peer identifies the peer
303  * @param timeout after how long should we give up (and call "info" with NULL
304  *                for "peer" to signal an error)?
305  * @param bw_out set to the current bandwidth limit (sending) for this peer,
306  *                caller should set "bpm_out" to "-1" to avoid changing
307  *                the current value; otherwise "bw_out" will be lowered to
308  *                the specified value; passing a pointer to "0" can be used to force
309  *                us to disconnect from the peer; "bw_out" might not increase
310  *                as specified since the upper bound is generally
311  *                determined by the other peer!
312  * @param amount reserve N bytes for receiving, negative
313  *                amounts can be used to undo a (recent) reservation;
314  * @param preference increase incoming traffic share preference by this amount;
315  *                in the absence of "amount" reservations, we use this
316  *                preference value to assign proportional bandwidth shares
317  *                to all connected peers
318  * @param info function to call with the resulting configuration information
319  * @param info_cls closure for info
320  * @return NULL on error
321  */
322 struct GNUNET_CORE_InformationRequestContext *
323 GNUNET_CORE_peer_change_preference (struct GNUNET_SCHEDULER_Handle *sched,
324                                     const struct GNUNET_CONFIGURATION_Handle *cfg,
325                                     const struct GNUNET_PeerIdentity *peer,
326                                     struct GNUNET_TIME_Relative timeout,
327                                     struct GNUNET_BANDWIDTH_Value32NBO bw_out,
328                                     int32_t amount,
329                                     uint64_t preference,
330                                     GNUNET_CORE_PeerConfigurationInfoCallback info,
331                                     void *info_cls);
332
333
334 /**
335  * Cancel request for getting information about a peer.
336  *
337  * @param irc context returned by the original GNUNET_CORE_peer_get_info call
338  */
339 void
340 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc);
341
342
343 /**
344  * Handle for a transmission request.
345  */
346 struct GNUNET_CORE_TransmitHandle;
347
348
349 /**
350  * Ask the core to call "notify" once it is ready to transmit the
351  * given number of bytes to the specified "target".  If we are not yet
352  * connected to the specified peer, a call to this function will cause
353  * us to try to establish a connection.
354  *
355  * @param handle connection to core service
356  * @param priority how important is the message?
357  * @param maxdelay how long can the message wait?
358  * @param target who should receive the message,
359  *        use NULL for this peer (loopback)
360  * @param notify_size how many bytes of buffer space does notify want?
361  * @param notify function to call when buffer space is available
362  * @param notify_cls closure for notify
363  * @return non-NULL if the notify callback was queued,
364  *         NULL if we can not even queue the request (insufficient
365  *         memory); if NULL is returned, "notify" will NOT be called.
366  */
367 struct GNUNET_CORE_TransmitHandle *
368 GNUNET_CORE_notify_transmit_ready (struct
369                                    GNUNET_CORE_Handle
370                                    *handle,
371                                    uint32_t priority,
372                                    struct
373                                    GNUNET_TIME_Relative
374                                    maxdelay,
375                                    const
376                                    struct
377                                    GNUNET_PeerIdentity
378                                    *target,
379                                    size_t
380                                    notify_size,
381                                    GNUNET_CONNECTION_TransmitReadyNotify
382                                    notify,
383                                    void
384                                    *notify_cls);
385
386
387 /**
388  * Cancel the specified transmission-ready notification.
389  *
390  * @param h handle that was returned by "notify_transmit_ready".
391  */
392 void
393 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
394                                           *h);
395
396
397 #if 0                           /* keep Emacsens' auto-indent happy */
398 {
399 #endif
400 #ifdef __cplusplus
401 }
402 #endif
403
404 /* ifndef GNUNET_CORE_SERVICE_H */
405 #endif
406 /* end of gnunet_core_service.h */