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