revert
[oweals/gnunet.git] / src / include / gnunet_peerstore_service.h
1 /*
2       This file is part of GNUnet
3       Copyright (C)
4
5       GNUnet is free software: you can redistribute it and/or modify it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your 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       Affero General Public License for more details.
14      
15       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * @author Omar Tarabai
21  *
22  * @file
23  * API to the peerstore service
24  *
25  * @defgroup peerstore  Peer Store service
26  *
27  * @see [Documentation](https://gnunet.org/gnunets-peerstore-subsystem)
28  *
29  * @{
30  */
31 #ifndef GNUNET_PEERSTORE_SERVICE_H
32 #define GNUNET_PEERSTORE_SERVICE_H
33
34 #include "gnunet_util_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  * Options for storing values in PEERSTORE
46  */
47 enum GNUNET_PEERSTORE_StoreOption
48 {
49
50   /**
51    * Possibly store multiple values under given key.
52    */
53   GNUNET_PEERSTORE_STOREOPTION_MULTIPLE = 0,
54
55   /**
56    * Delete any previous values for the given key before
57    * storing the given value.
58    */
59   GNUNET_PEERSTORE_STOREOPTION_REPLACE = 1
60
61 };
62
63 /**
64  * Handle to the peerstore service.
65  */
66 struct GNUNET_PEERSTORE_Handle;
67
68 /**
69  * Context for a store request
70  */
71 struct GNUNET_PEERSTORE_StoreContext;
72
73 /**
74  * Single PEERSTORE record
75  */
76 struct GNUNET_PEERSTORE_Record
77 {
78
79   /**
80    * Responsible sub system string
81    */
82   char *sub_system;
83
84   /**
85    * Peer Identity
86    */
87   struct GNUNET_PeerIdentity peer;
88
89   /**
90    * Record key string
91    */
92   char *key;
93
94   /**
95    * Record value BLOB
96    */
97   void *value;
98
99   /**
100    * Size of @e value BLOB
101    */
102   size_t value_size;
103
104   /**
105    * Expiry time of entry
106    */
107   struct GNUNET_TIME_Absolute expiry;
108
109   /**
110    * Client from which this record originated.
111    * NOTE: This is internal to the service.
112    */
113   struct GNUNET_SERVICE_Client *client;
114 };
115
116
117 /**
118  * Continuation called with a status result.
119  *
120  * @param cls closure
121  * @param success #GNUNET_OK or #GNUNET_SYSERR
122  */
123 typedef void
124 (*GNUNET_PEERSTORE_Continuation)(void *cls,
125                                  int success);
126
127
128 /**
129  * Function called by PEERSTORE for each matching record.
130  *
131  * @param cls closure
132  * @param record peerstore record information
133  * @param emsg error message, or NULL if no errors
134  */
135 typedef void
136 (*GNUNET_PEERSTORE_Processor) (void *cls,
137                                const struct GNUNET_PEERSTORE_Record *record,
138                                const char *emsg);
139
140
141 /**
142  * Connect to the PEERSTORE service.
143  *
144  * @return NULL on error
145  */
146 struct GNUNET_PEERSTORE_Handle *
147 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
148
149
150 /**
151  * Disconnect from the PEERSTORE service. Any pending ITERATE and WATCH requests
152  * will be canceled.
153  * Any pending STORE requests will depend on @e snyc_first flag.
154  *
155  * @param h handle to disconnect
156  * @param sync_first send any pending STORE requests before disconnecting
157  */
158 void
159 GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h,
160                              int sync_first);
161
162
163 /**
164  * Store a new entry in the PEERSTORE.
165  * Note that stored entries can be lost in some cases
166  * such as power failure.
167  *
168  * @param h Handle to the PEERSTORE service
169  * @param sub_system name of the sub system
170  * @param peer Peer Identity
171  * @param key entry key
172  * @param value entry value BLOB
173  * @param size size of @e value
174  * @param expiry absolute time after which the entry is (possibly) deleted
175  * @param options options specific to the storage operation
176  * @param cont Continuation function after the store request is sent
177  * @param cont_cls Closure for @a cont
178  */
179 struct GNUNET_PEERSTORE_StoreContext *
180 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
181                         const char *sub_system,
182                         const struct GNUNET_PeerIdentity *peer,
183                         const char *key,
184                         const void *value,
185                         size_t size,
186                         struct GNUNET_TIME_Absolute expiry,
187                         enum GNUNET_PEERSTORE_StoreOption options,
188                         GNUNET_PEERSTORE_Continuation cont,
189                         void *cont_cls);
190
191
192 /**
193  * Cancel a store request
194  *
195  * @param sc Store request context
196  */
197 void
198 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc);
199
200
201 /**
202  * Iterate over records matching supplied key information
203  *
204  * @param h handle to the PEERSTORE service
205  * @param sub_system name of sub system
206  * @param peer Peer identity (can be NULL)
207  * @param key entry key string (can be NULL)
208  * @param timeout time after which the iterate request is canceled
209  * @param callback function called with each matching record, all NULL's on end
210  * @param callback_cls closure for @a callback
211  */
212 struct GNUNET_PEERSTORE_IterateContext *
213 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
214                           const char *sub_system,
215                           const struct GNUNET_PeerIdentity *peer,
216                           const char *key,
217                           struct GNUNET_TIME_Relative timeout,
218                           GNUNET_PEERSTORE_Processor callback,
219                           void *callback_cls);
220
221
222 /**
223  * Cancel an iterate request
224  * Please do not call after the iterate request is done
225  *
226  * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
227  */
228 void
229 GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic);
230
231
232 /**
233  * Request watching a given key
234  * User will be notified with any new values added to key.
235  *
236  * @param h handle to the PEERSTORE service
237  * @param sub_system name of sub system
238  * @param peer Peer identity
239  * @param key entry key string
240  * @param callback function called with each new value
241  * @param callback_cls closure for @a callback
242  * @return Handle to watch request
243  */
244 struct GNUNET_PEERSTORE_WatchContext *
245 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
246                         const char *sub_system,
247                         const struct GNUNET_PeerIdentity *peer,
248                         const char *key,
249                         GNUNET_PEERSTORE_Processor callback,
250                         void *callback_cls);
251
252
253 /**
254  * Cancel a watch request
255  *
256  * @param wc handle to the watch request
257  */
258 void
259 GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc);
260
261
262 #if 0                           /* keep Emacsens' auto-indent happy */
263 {
264 #endif
265 #ifdef __cplusplus
266 }
267 #endif
268
269 #endif
270
271 /** @} */  /* end of group */