multicast: removed replay cancellation as responses are limited
[oweals/gnunet.git] / src / include / gnunet_env_lib.h
1 /*
2  * This file is part of GNUnet.
3  * Copyright (C) 2013 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 3, 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., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * @file include/gnunet_env_lib.h
23  * @brief Library providing operations for the @e environment of
24  *        PSYC and Social messages, and for (de)serializing variable values.
25  * @author Gabor X Toth
26  */
27
28
29 #ifndef GNUNET_ENV_LIB_H
30 #define GNUNET_ENV_LIB_H
31
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #if 0                           /* keep Emacsens' auto-indent happy */
36 }
37 #endif
38 #endif
39
40
41 /**
42  * Possible operations on PSYC state (persistent) and transient variables (per message).
43  */
44 enum GNUNET_ENV_Operator
45 {
46   /**
47    * Set value of a transient variable.
48    */
49   GNUNET_ENV_OP_SET = ':',
50
51   /**
52    * Assign value for a persistent state variable.
53    *
54    * If an assigned value is NULL, the variable is deleted.
55    */
56   GNUNET_ENV_OP_ASSIGN = '=',
57
58   /**
59    * Augment state variable.
60    *
61    * Used for appending strings, adding numbers, and adding new items to a list or dictionary.
62    */
63   GNUNET_ENV_OP_AUGMENT = '+',
64
65   /**
66    * Diminish state variable.
67    *
68    * Used for subtracting numbers, and removing items from a list or dictionary.
69    */
70   GNUNET_ENV_OP_DIMINISH = '-',
71
72   /**
73    * Update state variable.
74    *
75    * Used for modifying a single item of a list or dictionary.
76    */
77   GNUNET_ENV_OP_UPDATE = '@',
78 };
79
80
81 /**
82  * PSYC variable types.
83  */
84 enum GNUNET_ENV_Type
85 {
86   GNUNET_ENV_TYPE_DATA = 0,
87   GNUNET_ENV_TYPE_NUMBER,
88   GNUNET_ENV_TYPE_LIST,
89   GNUNET_ENV_TYPE_DICT
90 };
91
92
93 /**
94  * PSYC state modifier.
95  */
96 struct GNUNET_ENV_Modifier
97 {
98   /**
99    * State operation.
100    */
101   enum GNUNET_ENV_Operator oper;
102
103   /**
104    * Variable name.
105    */
106   const char *name;
107
108   /**
109    * Size of @a value.
110    */
111   size_t value_size;
112
113   /**
114    * Value of variable.
115    */
116   const void *value;
117
118   /**
119    * Next modifier.
120    */
121   struct GNUNET_ENV_Modifier *next;
122
123   /**
124    * Previous modifier.
125    */
126   struct GNUNET_ENV_Modifier *prev;
127 };
128
129
130 /**
131  * Environment for a message.
132  *
133  * Contains modifiers.
134  */
135 struct GNUNET_ENV_Environment;
136
137
138 /**
139  * Create an environment.
140  *
141  * @return A newly allocated environment.
142  */
143 struct GNUNET_ENV_Environment *
144 GNUNET_ENV_environment_create ();
145
146
147 /**
148  * Add a modifier to the environment.
149  *
150  * @param env The environment.
151  * @param oper Operation to perform.
152  * @param name Name of the variable.
153  * @param value Value of the variable.
154  * @param value_size Size of @a value.
155  */
156 void
157 GNUNET_ENV_environment_add (struct GNUNET_ENV_Environment *env,
158                             enum GNUNET_ENV_Operator oper, const char *name,
159                             const void *value, size_t value_size);
160
161
162 /**
163  * Get the first modifier of the environment.
164  */
165 struct GNUNET_ENV_Modifier *
166 GNUNET_ENV_environment_head (const struct GNUNET_ENV_Environment *env);
167
168
169
170 /**
171  * Get the last modifier of the environment.
172  */
173 struct GNUNET_ENV_Modifier *
174 GNUNET_ENV_environment_tail (const struct GNUNET_ENV_Environment *env);
175
176
177 /**
178  * Remove a modifier from the environment.
179  */
180 void
181 GNUNET_ENV_environment_remove (struct GNUNET_ENV_Environment *env,
182                                struct GNUNET_ENV_Modifier *mod);
183
184
185 /**
186  * Remove a modifier at the beginning of the environment.
187  */
188 int
189 GNUNET_ENV_environment_shift (struct GNUNET_ENV_Environment *env,
190                               enum GNUNET_ENV_Operator *oper, const char **name,
191                               const void **value, size_t *value_size);
192
193
194 /**
195  * Iterator for modifiers in the environment.
196  *
197  * @param cls Closure.
198  * @param mod Modifier.
199  *
200  * @return #GNUNET_YES to continue iterating,
201  *         #GNUNET_NO to stop.
202  */
203 typedef int
204 (*GNUNET_ENV_Iterator) (void *cls, enum GNUNET_ENV_Operator oper,
205                         const char *name, const char *value,
206                         uint32_t value_size);
207
208
209 /**
210  * Iterate through all modifiers in the environment.
211  *
212  * @param env The environment.
213  * @param it Iterator.
214  * @param it_cls Closure for iterator.
215  */
216 void
217 GNUNET_ENV_environment_iterate (const struct GNUNET_ENV_Environment *env,
218                                 GNUNET_ENV_Iterator it, void *it_cls);
219
220
221 /**
222  * Get the number of modifiers in the environment.
223  *
224  * @param env The environment.
225  *
226  * @return Number of modifiers.
227  */
228 size_t
229 GNUNET_ENV_environment_get_count (const struct GNUNET_ENV_Environment *env);
230
231
232 /**
233  * Destroy an environment.
234  *
235  * @param env The environment to destroy.
236  */
237 void
238 GNUNET_ENV_environment_destroy (struct GNUNET_ENV_Environment *env);
239
240
241 /**
242  * Get the type of variable.
243  *
244  * @param name Name of the variable.
245  *
246  * @return Variable type.
247  */
248 enum GNUNET_ENV_Type
249 GNUNET_ENV_var_get_type (char *name);
250
251
252 /**
253  * Perform an operation on a variable.
254  *
255  * @param name Name of variable.
256  * @param current_value Current value of variable.
257  * @param current_value_size Size of @a current_value.
258  * @param oper Operator.
259  * @param args Arguments for the operation.
260  * @param args_size Size of @a args.
261  * @param return_value Return value.
262  * @param return_value_size Size of @a return_value.
263  *
264  * @return #GNUNET_OK on success, else #GNUNET_SYSERR
265  */
266 int
267 GNUNET_ENV_operation (char *name, void *current_value, size_t current_value_size,
268                       enum GNUNET_ENV_Operator oper, void *args, size_t args_size,
269                       void **return_value, size_t *return_value_size);
270
271
272 /**
273  * Get the variable's value as an integer.
274  *
275  * @param size Size of value.
276  * @param value Raw value of variable.
277  * @param[out] number Value converted to a 64-bit integer.
278  *
279  * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
280  */
281 int
282 GNUNET_ENV_value_to_number (size_t size, const void *value, int64_t *number);
283
284
285 /**
286  * Get the variable's value as a dictionary.
287  *
288  * @param size Size of value.
289  * @param value Raw value of variable.
290  * @param[out] dict A newly created hashmap holding the elements of the dictionary.
291  *
292  * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
293  */
294 int
295 GNUNET_ENV_value_to_dict (size_t size, const void *value, struct GNUNET_CONTAINER_MultiHashMap **dict);
296
297
298 /**
299  * Create a PSYC variable value from an integer.
300  *
301  * @param number The number to convert.
302  * @param[out] value_size Size of returned value.
303  *
304  * @return A newly allocated value or NULL on error.
305  */
306 void *
307 GNUNET_ENV_value_from_number (int64_t number, size_t *value_size);
308
309
310 /**
311  * Create a PSYC variable value from a dictionary.
312  *
313  * @param dict The dict to convert.
314  * @param[out] value_size Size of returned value.
315  *
316  * @return A newly allocated value or NULL on error.
317  */
318 void *
319 GNUNET_ENV_value_from_dict (struct GNUNET_CONTAINER_MultiHashMap *dict, size_t *value_size);
320
321
322 #if 0                           /* keep Emacsens' auto-indent happy */
323 {
324 #endif
325 #ifdef __cplusplus
326 }
327 #endif
328
329 /* ifndef GNUNET_ENV_LIB_H */
330 #endif
331 /* end of gnunet_env_lib.h */