Asterisk - The Open Source Telephony Project GIT-master-7e7a603
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 <jcolp@digium.com>
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 <jcolp@digium.com>
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
42static void *sorcery_memory_open(const char *data);
43static int sorcery_memory_create(const struct ast_sorcery *sorcery, void *data, void *object);
44static void *sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
45static void *sorcery_memory_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
46static 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);
48static void sorcery_memory_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
49static 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);
50static int sorcery_memory_update(const struct ast_sorcery *sorcery, void *data, void *object);
51static int sorcery_memory_delete(const struct ast_sorcery *sorcery, void *data, void *object);
52static 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 */
90static 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 */
98static 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
105static 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
125static int sorcery_memory_fields_cmp(void *obj, void *arg, int flags)
126{
127 const struct sorcery_memory_fields_cmp_params *params = arg;
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
161static 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
177static 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
182static 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
193static 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
214static 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
226static 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
244static 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
251static void *sorcery_memory_open(const char *data)
252{
255}
256
257static void sorcery_memory_close(void *data)
258{
259 ao2_ref(data, -1);
260}
261
262static int load_module(void)
263{
266 }
267
269}
270
271static int unload_module(void)
272{
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,
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
@ CMP_MATCH
Definition: astobj2.h:1027
@ CMP_STOP
Definition: astobj2.h:1028
#define OBJ_KEY
Definition: astobj2.h:1151
@ AO2_ALLOC_OPT_LOCK_MUTEX
Definition: astobj2.h:363
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
Definition: astobj2.h:1693
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_link_flags(container, obj, flags)
Add an object to a container.
Definition: astobj2.h:1554
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1736
#define ao2_unlock(a)
Definition: astobj2.h:729
#define ao2_lock(a)
Definition: astobj2.h:717
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
@ OBJ_NOLOCK
Assume that the ao2_container is already locked.
Definition: astobj2.h:1063
@ OBJ_UNLINK
Definition: astobj2.h:1039
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1303
static const char type[]
Definition: chan_ooh323.c:109
static int regex(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
static char prefix[MAX_PREFIX]
Definition: http.c:144
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:861
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1262
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:317
@ AST_MODFLAG_GLOBAL_SYMBOLS
Definition: module.h:316
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ AST_MODPRI_REALTIME_DRIVER
Definition: module.h:323
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static struct ast_sorcery * sorcery
#define OBJECT_BUCKETS
Number of buckets for sorcery objects.
static int sorcery_memory_hash(const void *obj, const int flags)
Hashing function for sorcery objects.
static struct ast_sorcery_wizard memory_object_wizard
static void * sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
static void sorcery_memory_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
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)
static int sorcery_memory_fields_cmp(void *obj, void *arg, int flags)
static void * sorcery_memory_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
static int sorcery_memory_create(const struct ast_sorcery *sorcery, void *data, void *object)
static void * sorcery_memory_open(const char *data)
static int sorcery_memory_cmp(void *obj, void *arg, int flags)
Comparator function for sorcery objects.
static void sorcery_memory_close(void *data)
static int load_module(void)
static int unload_module(void)
static int sorcery_memory_update(const struct ast_sorcery *sorcery, void *data, void *object)
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)
#define NULL
Definition: resample.c:96
Sorcery Data Access Layer API.
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2317
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:1137
#define ast_sorcery_wizard_register(interface)
See __ast_sorcery_wizard_register()
Definition: sorcery.h:383
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1259
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Generic container type.
Interface for a sorcery wizard.
Definition: sorcery.h:276
const char * name
Name of the wizard.
Definition: sorcery.h:278
Full structure for sorcery.
Definition: sorcery.c:230
Structure for variables, used for configurations and for channel variables.
Structure used for fields comparison.
const char * prefix
Prefix for matching object id.
struct ao2_container * container
Optional container to put object into.
const size_t prefix_len
Prefix length in bytes for matching object id.
const struct ast_sorcery * sorcery
Pointer to the sorcery structure.
const struct ast_variable * fields
Pointer to the fields to check.
regex_t * regex
Regular expression for checking object id.
#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:941