- delay tunnel destruction 1 min to avoid rekeying
[oweals/gnunet.git] / src / env / env.c
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 env/env.c
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 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_env_lib.h"
31
32 /**
33  * Environment for a message.
34  *
35  * Contains modifiers.
36  */
37 struct GNUNET_ENV_Environment
38 {
39   struct GNUNET_ENV_Modifier *mod_head;
40   struct GNUNET_ENV_Modifier *mod_tail;
41   size_t mod_count;
42 };
43
44
45 /**
46  * Create an environment.
47  *
48  * @return A newly allocated environment.
49  */
50 struct GNUNET_ENV_Environment *
51 GNUNET_ENV_environment_create ()
52 {
53   return GNUNET_new (struct GNUNET_ENV_Environment);
54 }
55
56
57 /**
58  * Add a modifier to the environment.
59  *
60  * @param env The environment.
61  * @param oper Operation to perform.
62  * @param name Name of the variable.
63  * @param value Value of the variable.
64  * @param value_size Size of @a value.
65  */
66 void
67 GNUNET_ENV_environment_add (struct GNUNET_ENV_Environment *env,
68                             enum GNUNET_ENV_Operator oper, const char *name,
69                             const void *value, size_t value_size)
70 {
71   struct GNUNET_ENV_Modifier *mod = GNUNET_new (struct GNUNET_ENV_Modifier);
72   mod->oper = oper;
73   mod->name = name;
74   mod->value = value;
75   mod->value_size = value_size;
76   GNUNET_CONTAINER_DLL_insert_tail (env->mod_head, env->mod_tail, mod);
77   env->mod_count++;
78 }
79
80
81 /** 
82  * Get the modifier at the beginning of an environment.
83  *
84  * @param env 
85  * @param oper 
86  * @param name 
87  * @param value 
88  * @param value_size 
89  * 
90  * @return 
91  */
92 int
93 GNUNET_ENV_environment_head (struct GNUNET_ENV_Environment *env,
94                               enum GNUNET_ENV_Operator *oper, const char **name,
95                               const void **value, size_t *value_size)
96 {
97   if (NULL == env->mod_head)
98     return GNUNET_NO;
99
100   struct GNUNET_ENV_Modifier *mod = env->mod_head;
101   *oper = mod->oper;
102   *name = mod->name;
103   *value = mod->value;
104   *value_size = mod->value_size;
105   return GNUNET_YES;
106 }
107
108
109 /** 
110  * Get the modifier at the beginning of an environment and remove it.
111  *
112  * @param env 
113  * @param oper 
114  * @param name 
115  * @param value 
116  * @param value_size 
117  * 
118  * @return 
119  */
120 int
121 GNUNET_ENV_environment_shift (struct GNUNET_ENV_Environment *env,
122                               enum GNUNET_ENV_Operator *oper, const char **name,
123                               const void **value, size_t *value_size)
124 {
125   if (NULL == env->mod_head)
126     return GNUNET_NO;
127
128   struct GNUNET_ENV_Modifier *mod = env->mod_head;
129   *oper = mod->oper;
130   *name = mod->name;
131   *value = mod->value;
132   *value_size = mod->value_size;
133
134   GNUNET_CONTAINER_DLL_remove (env->mod_head, env->mod_tail, mod);
135   GNUNET_free (mod);
136   env->mod_count--;
137
138   return GNUNET_YES;
139 }
140
141
142 /**
143  * Iterate through all modifiers in the environment.
144  *
145  * @param env The environment.
146  * @param it Iterator.
147  * @param it_cls Closure for iterator.
148  */
149 void
150 GNUNET_ENV_environment_iterate (const struct GNUNET_ENV_Environment *env,
151                                 GNUNET_ENV_Iterator it, void *it_cls)
152 {
153   struct GNUNET_ENV_Modifier *mod;
154   for (mod = env->mod_head; NULL != mod; mod = mod->next)
155     it (it_cls, mod->oper, mod->name, mod->value, mod->value_size);
156 }
157
158
159 /**
160  * Get the number of modifiers in the environment.
161  *
162  * @param env The environment.
163  *
164  * @return Number of modifiers.
165  */
166 size_t
167 GNUNET_ENV_environment_get_count (const struct GNUNET_ENV_Environment *env)
168 {
169   return env->mod_count;
170 }
171
172
173 /**
174  * Destroy an environment.
175  *
176  * @param env The environment to destroy.
177  */
178 void
179 GNUNET_ENV_environment_destroy (struct GNUNET_ENV_Environment *env)
180 {
181   struct GNUNET_ENV_Modifier *mod, *prev = NULL;
182   for (mod = env->mod_head; NULL != mod; mod = mod->next)
183   {
184     if (NULL != prev)
185       GNUNET_free (prev);
186     prev = mod;
187   }
188   if (NULL != prev)
189     GNUNET_free (prev);
190
191   GNUNET_free (env);
192 }