553624b82ef83459d630b8c772ec8bf54aac8c60
[oweals/gnunet.git] / src / include / gnunet_peerinfo_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  * @file include/gnunet_peerinfo_service.h
22  * @brief Code to maintain the list of currently known hosts
23  *   (in memory structure of data/hosts) and their trust ratings
24  *   (in memory structure of data/trust)
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_PEERINFO_SERVICE_H
29 #define GNUNET_PEERINFO_SERVICE_H
30
31 #include "gnunet_common.h"
32 #include "gnunet_configuration_lib.h"
33 #include "gnunet_crypto_lib.h"
34 #include "gnunet_hello_lib.h"
35
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #if 0                           /* keep Emacsens' auto-indent happy */
40 }
41 #endif
42 #endif
43
44
45 /**
46  * Handle to the peerinfo service.
47  */
48 struct GNUNET_PEERINFO_Handle;
49
50
51 /**
52  * Connect to the peerinfo service.
53  *
54  * @param cfg configuration to use
55  * @param sched scheduler to use
56  * @return NULL on error (configuration related, actual connection
57  *         etablishment may happen asynchronously).
58  */
59 struct GNUNET_PEERINFO_Handle *
60 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
61                          struct GNUNET_SCHEDULER_Handle *sched);
62
63
64 /**
65  * Disconnect from the peerinfo service.  Note that all iterators must
66  * have completed or have been cancelled by the time this function is
67  * called (otherwise, calling this function is a serious error).
68  * Furthermore, if 'GNUNET_PEERINFO_add_peer' operations are still
69  * pending, they will be cancelled silently on disconnect.
70  *
71  * @param h handle to disconnect
72  */
73 void
74 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h);
75
76
77 /**
78  * Add a host to the persistent list.  This method operates in 
79  * semi-reliable mode: if the transmission is not completed by
80  * the time 'GNUNET_PEERINFO_disconnect' is called, it will be
81  * aborted.  Furthermore, if a second HELLO is added for the
82  * same peer before the first one was transmitted, PEERINFO may
83  * merge the two HELLOs prior to transmission to the service.
84  *
85  * @param h handle to the peerinfo service
86  * @param peer identity of the peer
87  * @param hello the verified (!) HELLO message
88  */
89 void
90 GNUNET_PEERINFO_add_peer_new (struct GNUNET_PEERINFO_Handle *h,
91                               const struct GNUNET_PeerIdentity *peer,
92                               const struct GNUNET_HELLO_Message *hello);
93
94
95
96 /**
97  * Add a host to the persistent list.
98  *
99  * @param cfg configuration to use
100  * @param sched scheduler to use
101  * @param peer identity of the peer
102  * @param hello the verified (!) HELLO message
103  */
104 void
105 GNUNET_PEERINFO_add_peer (const struct GNUNET_CONFIGURATION_Handle *cfg,
106                           struct GNUNET_SCHEDULER_Handle *sched,
107                           const struct GNUNET_PeerIdentity *peer,
108                           const struct GNUNET_HELLO_Message *hello);
109
110 /**
111  * Type of an iterator over the hosts.  Note that each
112  * host will be called with each available protocol.
113  *
114  * @param cls closure
115  * @param peer id of the peer, NULL for last call
116  * @param hello hello message for the peer (can be NULL)
117  * @param trust amount of trust we have in the peer
118  */
119 typedef void
120   (*GNUNET_PEERINFO_Processor) (void *cls,
121                                 const struct GNUNET_PeerIdentity * peer,
122                                 const struct GNUNET_HELLO_Message * hello,
123                                 uint32_t trust);
124
125
126 /**
127  * Handle for cancellation of iteration over peers.
128  */
129 struct GNUNET_PEERINFO_IteratorContext;
130
131
132 /**
133  * Call a method for each known matching host and change
134  * its trust value.  The method will be invoked once for
135  * each host and then finally once with a NULL pointer.  After
136  * that final invocation, the iterator context must no longer
137  * be used.
138  *
139  * Note that the last call can be triggered by timeout or by simply
140  * being done; however, the trust argument will be set to zero if we
141  * are done, 1 if we timed out and 2 for fatal error.
142  *
143  * @param cfg configuration to use
144  * @param sched scheduler to use
145  * @param peer restrict iteration to this peer only (can be NULL)
146  * @param trust_delta how much to change the trust in all matching peers
147  * @param timeout how long to wait until timing out
148  * @param callback the method to call for each peer
149  * @param callback_cls closure for callback
150  * @return NULL on error (in this case, 'callback' is never called!), 
151  *         otherwise an iterator context
152  */
153 struct GNUNET_PEERINFO_IteratorContext *
154 GNUNET_PEERINFO_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
155                          struct GNUNET_SCHEDULER_Handle *sched,
156                          const struct GNUNET_PeerIdentity *peer,
157                          int trust_delta,
158                          struct GNUNET_TIME_Relative timeout,
159                          GNUNET_PEERINFO_Processor callback,
160                          void *callback_cls);
161
162
163 struct GNUNET_PEERINFO_NewIteratorContext;
164
165
166 /**
167  * Call a method for each known matching host and change its trust
168  * value.  The callback method will be invoked once for each matching
169  * host and then finally once with a NULL pointer.  After that final
170  * invocation, the iterator context must no longer be used.
171  *
172  * Note that the last call can be triggered by timeout or by simply
173  * being done; however, the trust argument will be set to zero if we
174  * are done, 1 if we timed out and 2 for fatal error.
175  *
176  * Instead of calling this function with 'peer == NULL' and 'trust ==
177  * 0', it is often better to use 'GNUNET_PEERINFO_notify'.
178  * 
179  * @param h handle to the peerinfo service
180  * @param peer restrict iteration to this peer only (can be NULL)
181  * @param trust_delta how much to change the trust in all matching peers
182  * @param timeout how long to wait until timing out
183  * @param callback the method to call for each peer
184  * @param callback_cls closure for callback
185  * @return NULL on error (in this case, 'callback' is never called!), 
186  *         otherwise an iterator context
187  */
188 struct GNUNET_PEERINFO_NewIteratorContext *
189 GNUNET_PEERINFO_iterate_new (struct GNUNET_PEERINFO_Handle *h,
190                              const struct GNUNET_PeerIdentity *peer,
191                              int trust_delta,
192                              struct GNUNET_TIME_Relative timeout,
193                              GNUNET_PEERINFO_Processor callback,
194                              void *callback_cls);
195
196
197
198 /**
199  * Cancel an iteration over peer information.
200  *
201  * @param ic context of the iterator to cancel
202  */
203 void
204 GNUNET_PEERINFO_iterate_cancel_new (struct GNUNET_PEERINFO_NewIteratorContext *ic);
205
206
207 /**
208  * Cancel an iteration over peer information.
209  *
210  * @param ic context of the iterator to cancel
211  */
212 void
213 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic);
214
215
216 /**
217  * Handle for notifications about changes to the set of known peers.
218  */
219 struct GNUNET_PEERINFO_NotifyContext;
220
221
222 /**
223  * Call a method whenever our known information about peers
224  * changes.  Initially calls the given function for all known
225  * peers and then only signals changes.  Note that it is
226  * possible (i.e. on disconnects) that the callback is called
227  * twice with the same peer information.
228  *
229  * @param cfg configuration to use
230  * @param sched scheduler to use
231  * @param callback the method to call for each peer
232  * @param callback_cls closure for callback
233  * @return NULL on error
234  */
235 struct GNUNET_PEERINFO_NotifyContext *
236 GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg,
237                         struct GNUNET_SCHEDULER_Handle *sched,
238                         GNUNET_PEERINFO_Processor callback,
239                         void *callback_cls);
240
241
242 /**
243  * Stop notifying about changes.
244  *
245  * @param nc context to stop notifying
246  */
247 void
248 GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc);
249
250
251 #if 0                           /* keep Emacsens' auto-indent happy */
252 {
253 #endif
254 #ifdef __cplusplus
255 }
256 #endif
257
258
259 /* end of gnunet_peerinfo_service.h */
260 #endif