Asterisk - The Open Source Telephony Project  18.5.0
res_sorcery_memory.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012 - 2013, Digium, Inc.
5  *
6  * Joshua Colp <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*!
20  * \file
21  *
22  * \brief Sorcery In-Memory Object Wizard
23  *
24  * \author Joshua Colp <[email protected]>
25  */
26 
27 /*** MODULEINFO
28  <support_level>core</support_level>
29  ***/
30 
31 #include "asterisk.h"
32 
33 #include <regex.h>
34 
35 #include "asterisk/module.h"
36 #include "asterisk/sorcery.h"
37 #include "asterisk/astobj2.h"
38 
39 /*! \brief Number of buckets for sorcery objects */
40 #define OBJECT_BUCKETS 53
41 
42 static void *sorcery_memory_open(const char *data);
43 static int sorcery_memory_create(const struct ast_sorcery *sorcery, void *data, void *object);
44 static void *sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
45 static void *sorcery_memory_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
46 static void sorcery_memory_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
47  const struct ast_variable *fields);
48 static void sorcery_memory_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
49 static void sorcery_memory_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *prefix, const size_t prefix_len);
50 static int sorcery_memory_update(const struct ast_sorcery *sorcery, void *data, void *object);
51 static int sorcery_memory_delete(const struct ast_sorcery *sorcery, void *data, void *object);
52 static void sorcery_memory_close(void *data);
53 
55  .name = "memory",
56  .open = sorcery_memory_open,
57  .create = sorcery_memory_create,
58  .retrieve_id = sorcery_memory_retrieve_id,
59  .retrieve_fields = sorcery_memory_retrieve_fields,
60  .retrieve_multiple = sorcery_memory_retrieve_multiple,
61  .retrieve_regex = sorcery_memory_retrieve_regex,
62  .retrieve_prefix = sorcery_memory_retrieve_prefix,
63  .update = sorcery_memory_update,
64  .delete = sorcery_memory_delete,
65  .close = sorcery_memory_close,
66 };
67 
68 /*! \brief Structure used for fields comparison */
70  /*! \brief Pointer to the sorcery structure */
71  const struct ast_sorcery *sorcery;
72 
73  /*! \brief Pointer to the fields to check */
74  const struct ast_variable *fields;
75 
76  /*! \brief Regular expression for checking object id */
77  regex_t *regex;
78 
79  /*! \brief Prefix for matching object id */
80  const char *prefix;
81 
82  /*! \brief Prefix length in bytes for matching object id */
83  const size_t prefix_len;
84 
85  /*! \brief Optional container to put object into */
87 };
88 
89 /*! \brief Hashing function for sorcery objects */
90 static int sorcery_memory_hash(const void *obj, const int flags)
91 {
92  const char *id = obj;
93 
94  return ast_str_hash(flags & OBJ_KEY ? id : ast_sorcery_object_get_id(obj));
95 }
96 
97 /*! \brief Comparator function for sorcery objects */
98 static int sorcery_memory_cmp(void *obj, void *arg, int flags)
99 {
100  const char *id = arg;
101 
102  return !strcmp(ast_sorcery_object_get_id(obj), flags & OBJ_KEY ? id : ast_sorcery_object_get_id(arg)) ? CMP_MATCH | CMP_STOP : 0;
103 }
104 
105 static int sorcery_memory_create(const struct ast_sorcery *sorcery, void *data, void *object)
106 {
107  void *existing;
108 
109  ao2_lock(data);
110 
111  existing = ao2_find(data, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_NOLOCK);
112  if (existing) {
113  ao2_ref(existing, -1);
114  ao2_unlock(data);
115  return -1;
116  }
117 
118  ao2_link_flags(data, object, OBJ_NOLOCK);
119 
120  ao2_unlock(data);
121 
122  return 0;
123 }
124 
125 static int sorcery_memory_fields_cmp(void *obj, void *arg, int flags)
126 {
127  const struct sorcery_memory_fields_cmp_params *params = arg;
128  RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
129 
130  if (params->regex) {
131  /* If a regular expression has been provided see if it matches, otherwise move on */
132  if (!regexec(params->regex, ast_sorcery_object_get_id(obj), 0, NULL, 0)) {
133  ao2_link(params->container, obj);
134  }
135  return 0;
136  } else if (params->prefix) {
137  if (!strncmp(params->prefix, ast_sorcery_object_get_id(obj), params->prefix_len)) {
138  ao2_link(params->container, obj);
139  }
140  return 0;
141  } else if (params->fields &&
142  (!(objset = ast_sorcery_objectset_create(params->sorcery, obj)) ||
143  (!ast_variable_lists_match(objset, params->fields, 0)))) {
144  /* If we can't turn the object into an object set OR if differences exist between the fields
145  * passed in and what are present on the object they are not a match.
146  */
147  return 0;
148  }
149 
150  if (params->container) {
151  ao2_link(params->container, obj);
152 
153  /* As multiple objects are being returned keep going */
154  return 0;
155  } else {
156  /* Immediately stop and return, we only want a single object */
157  return CMP_MATCH | CMP_STOP;
158  }
159 }
160 
161 static void *sorcery_memory_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
162 {
163  struct sorcery_memory_fields_cmp_params params = {
164  .sorcery = sorcery,
165  .fields = fields,
166  .container = NULL,
167  };
168 
169  /* If no fields are present return nothing, we require *something* */
170  if (!fields) {
171  return NULL;
172  }
173 
174  return ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
175 }
176 
177 static void *sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
178 {
179  return ao2_find(data, id, OBJ_KEY);
180 }
181 
182 static void sorcery_memory_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
183 {
184  struct sorcery_memory_fields_cmp_params params = {
185  .sorcery = sorcery,
186  .fields = fields,
187  .container = objects,
188  };
189 
190  ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
191 }
192 
193 static void sorcery_memory_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
194 {
195  regex_t expression;
196  struct sorcery_memory_fields_cmp_params params = {
197  .sorcery = sorcery,
198  .container = objects,
199  .regex = &expression,
200  };
201 
202  if (ast_strlen_zero(regex)) {
203  regex = ".";
204  }
205 
206  if (regcomp(&expression, regex, REG_EXTENDED | REG_NOSUB)) {
207  return;
208  }
209 
210  ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
211  regfree(&expression);
212 }
213 
214 static void sorcery_memory_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *prefix, const size_t prefix_len)
215 {
216  struct sorcery_memory_fields_cmp_params params = {
217  .sorcery = sorcery,
218  .container = objects,
219  .prefix = prefix,
220  .prefix_len = prefix_len,
221  };
222 
223  ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
224 }
225 
226 static int sorcery_memory_update(const struct ast_sorcery *sorcery, void *data, void *object)
227 {
228  RAII_VAR(void *, existing, NULL, ao2_cleanup);
229 
230  ao2_lock(data);
231 
232  if (!(existing = ao2_find(data, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_UNLINK))) {
233  ao2_unlock(data);
234  return -1;
235  }
236 
237  ao2_link(data, object);
238 
239  ao2_unlock(data);
240 
241  return 0;
242 }
243 
244 static int sorcery_memory_delete(const struct ast_sorcery *sorcery, void *data, void *object)
245 {
246  RAII_VAR(void *, existing, ao2_find(data, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
247 
248  return existing ? 0 : -1;
249 }
250 
251 static void *sorcery_memory_open(const char *data)
252 {
255 }
256 
257 static void sorcery_memory_close(void *data)
258 {
259  ao2_ref(data, -1);
260 }
261 
262 static int load_module(void)
263 {
264  if (ast_sorcery_wizard_register(&memory_object_wizard)) {
266  }
267 
269 }
270 
271 static int unload_module(void)
272 {
273  ast_sorcery_wizard_unregister(&memory_object_wizard);
274  return 0;
275 }
276 
278  .support_level = AST_MODULE_SUPPORT_CORE,
279  .load = load_module,
280  .unload = unload_module,
281  .load_pri = AST_MODPRI_REALTIME_DRIVER,
282 );
static const char type[]
Definition: chan_ooh323.c:109
Asterisk main include file. File version handling, generic pbx functions.
static void * sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1263
#define OBJ_KEY
Definition: astobj2.h:1155
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
Structure for variables, used for configurations and for channel variables.
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
Full structure for sorcery.
Definition: sorcery.c:230
static int sorcery_memory_update(const struct ast_sorcery *sorcery, void *data, void *object)
#define ao2_link_flags(container, obj, flags)
Definition: astobj2.h:1572
#define ao2_unlock(a)
Definition: astobj2.h:730
#define NULL
Definition: resample.c:96
const char * name
Name of the wizard.
Definition: sorcery.h:278
static void sorcery_memory_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *prefix, const size_t prefix_len)
static int sorcery_memory_delete(const struct ast_sorcery *sorcery, void *data, void *object)
static void * sorcery_memory_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
#define ast_strlen_zero(foo)
Definition: strings.h:52
struct ao2_container * container
Optional container to put object into.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
static int sorcery_memory_create(const struct ast_sorcery *sorcery, void *data, void *object)
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_lock(a)
Definition: astobj2.h:718
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
struct ao2_global_obj objects
Objects retrieved from the configuration file.
regex_t * regex
Regular expression for checking object id.
static void sorcery_memory_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
#define ast_sorcery_wizard_register(interface)
See __ast_sorcery_wizard_register()
Definition: sorcery.h:383
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
const size_t prefix_len
Prefix length in bytes for matching object id.
#define OBJECT_BUCKETS
Number of buckets for sorcery objects.
int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
Unregister a sorcery wizard.
Definition: sorcery.c:474
#define ast_sorcery_objectset_create(sorcery, object)
Create an object set (KVP list) for an object.
Definition: sorcery.h:1136
static int regex(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_strings.c:948
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
static void sorcery_memory_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
static int sorcery_memory_fields_cmp(void *obj, void *arg, int flags)
static int unload_module(void)
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
const char * prefix
Prefix for matching object id.
Interface for a sorcery wizard.
Definition: sorcery.h:276
static int load_module(void)
static struct ast_sorcery * sorcery
int ast_variable_lists_match(const struct ast_variable *left, const struct ast_variable *right, int exact_match)
Tests 2 variable lists to see if they match.
Definition: main/config.c:772
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static void sorcery_memory_close(void *data)
static int sorcery_memory_hash(const void *obj, const int flags)
Hashing function for sorcery objects.
Generic container type.
const struct ast_variable * fields
Pointer to the fields to check.
static struct ast_sorcery_wizard memory_object_wizard
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
static int sorcery_memory_cmp(void *obj, void *arg, int flags)
Comparator function for sorcery objects.
const struct ast_sorcery * sorcery
Pointer to the sorcery structure.
Structure used for fields comparison.
static void * sorcery_memory_open(const char *data)
Sorcery Data Access Layer API.
static char prefix[MAX_PREFIX]
Definition: http.c:141
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1206
#define ao2_link(container, obj)
Definition: astobj2.h:1549