social: use full pubkey as place/nym id; multicast/psyc/store: identify members by...
[oweals/gnunet.git] / src / include / gnunet_env_lib.h
1 /*
2      This file is part of GNUnet.
3      (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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 /** 
30  * Possible operations on PSYC state (persistent) and transient variables (per message).
31  */
32 enum GNUNET_ENV_Operator
33 {
34   /** 
35    * Set value of a transient variable.
36    */
37   GNUNET_ENV_OP_SET = ':',
38
39   /** 
40    * Assign value for a persistent state variable.
41    *
42    * If an assigned value is NULL, the variable is deleted.
43    * If the variable name that comes with thi operator is empty,
44    * it means a full state reset, the channel state is zeroed.
45    */
46   GNUNET_ENV_OP_ASSIGN = '=',
47
48   /** 
49    * Augment state variable.
50    *
51    * Used for appending strings, adding numbers, and adding new items to a list or dictionary.
52    */
53   GNUNET_ENV_OP_AUGMENT = '+',
54
55   /** 
56    * Diminish state variable.
57    *
58    * Used for subtracting numbers, and removing items from a list or dictionary.
59    */
60   GNUNET_ENV_OP_DIMINISH = '-',
61
62   /** 
63    * Update state variable.
64    *
65    * Used for modifying a single item of a list or dictionary.
66    */
67   GNUNET_ENV_OP_UPDATE = '@',
68 };
69
70
71 /** 
72  * PSYC variable types.
73  */
74 enum GNUNET_ENV_Type
75 {
76   GNUNET_ENV_TYPE_DATA = 0,
77   GNUNET_ENV_TYPE_NUMBER,
78   GNUNET_ENV_TYPE_LIST,
79   GNUNET_ENV_TYPE_DICT
80 };
81
82
83 /** 
84  * PSYC state modifier.
85  */
86 struct GNUNET_ENV_Modifier {
87   /** 
88    * State operation.
89    */
90   GNUNET_ENV_Operator oper;
91
92   /** 
93    * Variable name.
94    */
95   const char *name;
96
97   /** 
98    * Size of @a value.
99    */
100   size_t value_size;
101
102   /** 
103    * Value of variable.
104    */
105   const void *value;
106 };
107
108
109 /** 
110  * Environment for a message.
111  *
112  * Contains modifiers.
113  */
114 struct GNUNET_ENV_Environment;
115
116
117 /** 
118  * Create an environment.
119  * 
120  * @return A newly allocated environment.
121  */
122 struct GNUNET_ENV_Environment *
123 GNUNET_ENV_environment_create ();
124
125
126 /** 
127  * Add an operation on a variable to the environment.
128  *
129  * @param env The environment.
130  * @param oper Operation to perform.
131  * @param name Name of the variable.
132  * @param value_size Size of @a value.
133  * @param value Value of the variable.
134  * 
135  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error.
136  */
137 int
138 GNUNET_ENV_environment_operation (struct GNUNET_ENV_Environment *env,
139                                   enum GNUNET_ENV_Operator oper,
140                                   const char *name,
141                                   size_t value_size, const void *value);
142
143
144 /** 
145  * Get all modifiers in the environment.
146  *
147  * FIXME: use an iterator instead, as we'll likely use a SList to store the
148  *        modifiers in the environment.
149  *
150  * @param env The environment.
151  * @param[out] modifier_count Set to the number of returned modifiers.
152  * 
153  * @return Array of modifiers.
154  */
155 const struct GNUNET_ENV_Modifier *
156 GNUNET_ENV_environment_get_modifiers (const struct GNUNET_ENV_Environment *env,
157                                       size_t *modifier_count);
158
159
160 /** 
161  * Add list of modifiers to the environment.
162  *
163  * @param env The environment.
164  * @param modifier_count Number of @a modifiers.
165  * @param modifiers Array of modifiers to add.
166  * 
167  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error.
168  */
169 int
170 GNUNET_ENV_environment_set_modifiers (const struct GNUNET_ENV_Environment *env,
171                                       size_t modifier_count,
172                                       const struct GNUNET_ENV_Modifier *modifiers);
173
174
175 /** 
176  * Destroy an environment.
177  *
178  * @param env The environment to destroy.
179  */
180 void
181 GNUNET_ENV_environment_destroy (struct GNUNET_ENV_Environment *env);
182
183
184 /** 
185  * Get the type of variable.
186  *
187  * @param name Name of the variable.
188  * 
189  * @return Variable type.
190  */
191 enum GNUNET_ENV_Type
192 GNUNET_ENV_var_get_type (char *name);
193
194
195 /** 
196  * Get the variable's value as an integer.
197  *
198  * @param size Size of value.
199  * @param value Raw value of variable.
200  * @param[out] number Value converted to a 64-bit integer.
201  *
202  * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
203  */
204 int
205 GNUNET_ENV_value_to_number (size_t size, const void *value, int64_t *number);
206
207
208 /** 
209  * Get the variable's value as a list.
210  *
211  * @param size Size of value.
212  * @param value Raw value of variable.
213  * @param[out] list A newly created list holding the elements.
214  *
215  * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
216  */
217 int
218 GNUNET_ENV_value_to_list (size_t size, const void *value, GNUNET_CONTAINER_SList **list);
219
220
221 /** 
222  * Get the variable's value as a dictionary.
223  *
224  * @param size Size of value.
225  * @param value Raw value of variable.
226  * @param[out] dict A newly created hashmap holding the elements of the dictionary.
227  *
228  * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
229  */
230 int
231 GNUNET_ENV_value_to_dict (size_t size, const void *value, GNUNET_CONTAINER_MultiHashMap **dict);
232
233
234 /** 
235  * Create a PSYC variable value from an integer.
236  *
237  * @param number The number to convert.
238  * @param[out] value_size Size of returned value.
239  * 
240  * @return A newly allocated value or NULL on error.
241  */
242 void *
243 GNUNET_ENV_value_from_number (int64_t number, size_t *value_size);
244
245
246 /** 
247  * Create a PSYC variable value from a list.
248  *
249  * @param list The list to convert.
250  * @param[out] value_size Size of returned value.
251  * 
252  * @return A newly allocated value or NULL on error.
253  */
254 void *
255 GNUNET_ENV_value_from_list (GNUNET_CONTAINER_SList *list, size_t *value_size);
256
257
258 /** 
259  * Create a PSYC variable value from a dictionary.
260  *
261  * @param dict The dict to convert.
262  * @param[out] value_size Size of returned value.
263  * 
264  * @return A newly allocated value or NULL on error.
265  */
266 void *
267 GNUNET_ENV_value_from_dict (GNUNET_CONTAINER_MultiHashMap *dict, size_t *value_size);