a772813365f113ab50852d9f013ac830e761c98e
[oweals/gnunet.git] / src / include / gnunet_core_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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 or
55  * disconnects (or list of connections was requested).
56  *
57  * @param cls closure
58  * @param peer peer identity this notification is about
59  */
60 typedef void (*GNUNET_CORE_ClientEventHandler) (void *cls,
61                                                 const struct
62                                                 GNUNET_PeerIdentity * peer);
63
64
65 /**
66  * Type of a send callback to fill up buffers.
67  *
68  * @param receiver the receiver of the message
69  * @param position is the reference to the
70  *        first unused position in the buffer where GNUnet is building
71  *        the message
72  * @param padding is the number of bytes left in that buffer.
73  * @return the number of bytes written to
74  *   that buffer (must be a positive number).
75  */
76 typedef unsigned int
77   (*GNUNET_CORE_BufferFillCallback) (void *cls,
78                                      const struct GNUNET_PeerIdentity *
79                                      receiver,
80                                      void *position, unsigned int padding);
81
82
83 /**
84  * Functions with this signature are called whenever a message is
85  * received or transmitted.
86  *
87  * @param cls closure
88  * @param peer the other peer involved (sender or receiver, NULL
89  *        for loopback messages where we are both sender and receiver)
90  * @param message the actual message
91  * @return GNUNET_OK to keep the connection open,
92  *         GNUNET_SYSERR to close it (signal serious error)
93  */
94 typedef int
95   (*GNUNET_CORE_MessageCallback) (void *cls,
96                                   const struct GNUNET_PeerIdentity * other,
97                                   const struct GNUNET_MessageHeader *
98                                   message);
99
100
101 /**
102  * Message handler.  Each struct specifies how to handle on particular
103  * type of message received.
104  */
105 struct GNUNET_CORE_MessageHandler
106 {
107   /**
108    * Function to call for messages of "type".
109    */
110   GNUNET_CORE_MessageCallback callback;
111
112   /**
113    * Type of the message this handler covers.
114    */
115   uint16_t type;
116
117   /**
118    * Expected size of messages of this type.  Use 0 for variable-size.
119    * If non-zero, messages of the given type will be discarded if they
120    * do not have the right size.
121    */
122   uint16_t expected_size;
123
124 };
125
126
127 /**
128  * Function called after GNUNET_CORE_connect has succeeded
129  * (or failed for good).  Note that the private key of the
130  * peer is intentionally not exposed here; if you need it,
131  * your process should try to read the private key file
132  * directly (which should work if you are authorized...).
133  *
134  * @param cls closure
135  * @param server handle to the server, NULL if we failed
136  * @param my_identity ID of this peer, NULL if we failed
137  * @param publicKey public key of this peer, NULL if we failed
138  */
139 typedef void
140   (*GNUNET_CORE_StartupCallback) (void *cls,
141                                   struct GNUNET_CORE_Handle * server,
142                                   const struct GNUNET_PeerIdentity *
143                                   my_identity,
144                                   const struct
145                                   GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
146                                   publicKey);
147
148
149 /**
150  * Connect to the core service.  Note that the connection may
151  * complete (or fail) asynchronously.
152  *
153  * @param sched scheduler to use
154  * @param cfg configuration to use
155  * @param timeout after how long should we give up trying to connect to the core service?
156  * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
157  * @param init callback to call on timeout or once we have successfully
158  *        connected to the core service
159  * @param connects function to call on peer connect, can be NULL
160  * @param disconnects function to call on peer disconnect / timeout, can be NULL
161  * @param bfc function to call to fill up spare bandwidth, can be NULL
162  * @param inbound_notify function to call for all inbound messages, can be NULL
163  * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
164  *                GNUNET_MessageHeader and hence we do not need to give it the full message;
165  *                can be used to improve efficiency, ignored if inbound_notify is NULLL
166  * @param outbound_notify function to call for all outbound messages, can be NULL
167  * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
168  *                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 NULLL
170  * @param handlers callbacks for messages we care about, NULL-terminated
171  */
172 void
173 GNUNET_CORE_connect (struct GNUNET_SCHEDULER_Handle *sched,
174                      const struct GNUNET_CONFIGURATION_Handle *cfg,
175                      struct GNUNET_TIME_Relative timeout,
176                      void *cls,
177                      GNUNET_CORE_StartupCallback init,
178                      GNUNET_CORE_ClientEventHandler connects,
179                      GNUNET_CORE_ClientEventHandler disconnects,
180                      GNUNET_CORE_BufferFillCallback bfc,
181                      GNUNET_CORE_MessageCallback inbound_notify,
182                      int inbound_hdr_only,
183                      GNUNET_CORE_MessageCallback outbound_notify,
184                      int outbound_hdr_only,
185                      const struct GNUNET_CORE_MessageHandler *handlers);
186
187
188 /**
189  * Disconnect from the core service.
190  *
191  * @param handle connection to core to disconnect
192  */
193 void GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle);
194
195
196 /**
197  * Function called with statistics about the given peer.
198  *
199  * @param peer identifies the peer
200  * @param latency current latency estimate, "FOREVER" if we have been
201  *                disconnected
202  * @param bpm_in set to the current bandwidth limit (receiving) for this peer
203  * @param bpm_out set to the current bandwidth limit (sending) for this peer
204  * @param amount set to the amount that was actually reserved or unreserved
205  * @param preference current traffic preference for the given peer
206  */
207 typedef void
208   (*GNUNET_CORE_PeerConfigurationInfoCallback) (void *cls,
209                                                 const struct
210                                                 GNUNET_PeerIdentity * peer,
211                                                 unsigned int bpm_in,
212                                                 unsigned int bpm_out,
213                                                 struct GNUNET_TIME_Relative
214                                                 latency, int amount,
215                                                 unsigned long long preference);
216
217
218 /**
219  * Obtain statistics and/or change preferences for the given peer.
220  *
221  * @param handle connection to core to use
222  * @param peer identifies the peer
223  * @param timeout after how long should we give up (and call "info" with NULL
224  *                for "peer" to signal an error)?
225  * @param bpm_out set to the current bandwidth limit (sending) for this peer,
226  *                caller should set "bpm_out" to "-1" to avoid changing
227  *                the current value; otherwise "bpm_out" will be lowered to
228  *                the specified value; passing a pointer to "0" can be used to force
229  *                us to disconnect from the peer; "bpm_out" might not increase
230  *                as specified since the upper bound is generally
231  *                determined by the other peer!
232  * @param amount reserve N bytes for receiving, negative
233  *                amounts can be used to undo a (recent) reservation;
234  * @param preference increase incoming traffic share preference by this amount;
235  *                in the absence of "amount" reservations, we use this
236  *                preference value to assign proportional bandwidth shares
237  *                to all connected peers
238  * @param info function to call with the resulting configuration information
239  * @param info_cls closure for info
240  */
241 void
242 GNUNET_CORE_peer_configure (struct GNUNET_CORE_Handle *handle,
243                             const struct GNUNET_PeerIdentity *peer,
244                             struct GNUNET_TIME_Relative timeout,
245                             unsigned int bpm_out,
246                             int amount,
247                             unsigned long long preference,
248                             GNUNET_CORE_PeerConfigurationInfoCallback info,
249                             void *info_cls);
250
251
252 /**
253  * Handle for a transmission request.
254  */
255 struct GNUNET_CORE_TransmitHandle;
256
257
258 /**
259  * Ask the core to call "notify" once it is ready to transmit the
260  * given number of bytes to the specified "target".  If we are not yet
261  * connected to the specified peer, a call to this function will cause
262  * us to try to establish a connection.
263  *
264  * @param handle connection to core service
265  * @param priority how important is the message?
266  * @param maxdelay how long can the message wait?
267  * @param target who should receive the message,
268  *        use NULL for this peer (loopback)
269  * @param notify_size how many bytes of buffer space does notify want?
270  * @param notify function to call when buffer space is available
271  * @param notify_cls closure for notify
272  * @return non-NULL if the notify callback was queued,
273  *         NULL if we can not even queue the request (insufficient
274  *         memory); if NULL is returned, "notify" will NOT be called.
275  */
276 struct GNUNET_CORE_TransmitHandle *GNUNET_CORE_notify_transmit_ready (struct
277                                                                       GNUNET_CORE_Handle
278                                                                       *handle,
279                                                                       unsigned
280                                                                       int
281                                                                       priority,
282                                                                       struct
283                                                                       GNUNET_TIME_Relative
284                                                                       maxdelay,
285                                                                       const
286                                                                       struct
287                                                                       GNUNET_PeerIdentity
288                                                                       *target,
289                                                                       size_t
290                                                                       notify_size,
291                                                                       GNUNET_NETWORK_TransmitReadyNotify
292                                                                       notify,
293                                                                       void
294                                                                       *notify_cls);
295
296
297 /**
298  * Cancel the specified transmission-ready notification.
299  *
300  * @param h handle that was returned by "notify_transmit_ready".
301  */
302 void
303 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
304                                           *h);
305
306
307 #if 0                           /* keep Emacsens' auto-indent happy */
308 {
309 #endif
310 #ifdef __cplusplus
311 }
312 #endif
313
314 /* ifndef GNUNET_CORE_SERVICE_H */
315 #endif
316 /* end of gnunet_core_service.h */