Wed Jan 8 2020 09:50:12

Asterisk developer's documentation


func_dialgroup.c File Reference

Dial group dialplan function. More...

#include "asterisk.h"
#include <sys/stat.h>
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/astobj2.h"
#include "asterisk/astdb.h"

Go to the source code of this file.

Data Structures

struct  group
 
struct  group_entry
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int dialgroup_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int dialgroup_refreshdb (struct ast_channel *chan, const char *cdialgroup)
 
static int dialgroup_write (struct ast_channel *chan, const char *cmd, char *data, const char *cvalue)
 
static int entry_cmp_fn (void *obj1, void *name2, int flags)
 
static int entry_hash_fn (const void *obj, const int flags)
 
static int group_cmp_fn (void *obj1, void *name2, int flags)
 
static void group_destroy (void *vgroup)
 
static int group_hash_fn (const void *obj, const int flags)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Dialgroup dialplan function" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
 
static struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_custom_function dialgroup_function
 
static struct ao2_containergroup_container = NULL
 

Detailed Description

Function Documentation

static void __reg_module ( void  )
static

Definition at line 320 of file func_dialgroup.c.

static void __unreg_module ( void  )
static

Definition at line 320 of file func_dialgroup.c.

static int dialgroup_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 129 of file func_dialgroup.c.

References ao2_find, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ast_copy_string(), ast_log(), ast_strlen_zero(), group::entries, LOG_WARNING, and group_entry::name.

Referenced by dialgroup_refreshdb().

130 {
131  struct ao2_iterator i;
132  struct group *grhead = ao2_find(group_container, data, 0);
133  struct group_entry *entry;
134  size_t bufused = 0;
135  int trunc_warning = 0;
136  int res = 0;
137 
138  if (!grhead) {
139  if (!ast_strlen_zero(cmd)) {
140  ast_log(LOG_WARNING, "No such dialgroup '%s'\n", data);
141  }
142  return -1;
143  }
144 
145  buf[0] = '\0';
146 
147  i = ao2_iterator_init(grhead->entries, 0);
148  while ((entry = ao2_iterator_next(&i))) {
149  int tmp = strlen(entry->name);
150  /* Ensure that we copy only complete names, not partials */
151  if (len - bufused > tmp + 2) {
152  if (bufused != 0)
153  buf[bufused++] = '&';
154  ast_copy_string(buf + bufused, entry->name, len - bufused);
155  bufused += tmp;
156  } else if (trunc_warning++ == 0) {
157  if (!ast_strlen_zero(cmd)) {
158  ast_log(LOG_WARNING, "Dialgroup '%s' is too large. Truncating list.\n", data);
159  } else {
160  res = 1;
161  ao2_ref(entry, -1);
162  break;
163  }
164  }
165  ao2_ref(entry, -1);
166  }
168  ao2_ref(grhead, -1);
169 
170  return res;
171 }
#define ao2_iterator_next(arg1)
Definition: astobj2.h:1126
#define LOG_WARNING
Definition: logger.h:144
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
Create an iterator for a container.
Definition: astobj2.c:818
static struct ao2_container * group_container
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
char name[AST_CHANNEL_NAME]
#define ao2_ref(o, delta)
Definition: astobj2.h:472
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ao2_find(arg1, arg2, arg3)
Definition: astobj2.h:964
void ao2_iterator_destroy(struct ao2_iterator *i)
Destroy a container iterator.
Definition: astobj2.c:833
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1053
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
struct ao2_container * entries
static int dialgroup_refreshdb ( struct ast_channel chan,
const char *  cdialgroup 
)
static

Definition at line 173 of file func_dialgroup.c.

References ast_db_del(), ast_db_put(), ast_free, ast_realloc, ast_strdupa, ast_strlen_zero(), dialgroup_read(), and len().

Referenced by dialgroup_write().

174 {
175  int len = 500, res = 0;
176  char *buf = NULL;
177  char *new_buf;
178  char *dialgroup = ast_strdupa(cdialgroup);
179 
180  do {
181  len *= 2;
182  new_buf = ast_realloc(buf, len);
183  if (!new_buf) {
184  ast_free(buf);
185  return -1;
186  }
187  buf = new_buf;
188 
189  if ((res = dialgroup_read(chan, "", dialgroup, buf, len)) < 0) {
190  ast_free(buf);
191  return -1;
192  }
193  } while (res == 1);
194 
195  if (ast_strlen_zero(buf)) {
196  ast_db_del("dialgroup", cdialgroup);
197  } else {
198  ast_db_put("dialgroup", cdialgroup, buf);
199  }
200  ast_free(buf);
201  return 0;
202 }
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
static int dialgroup_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_free(a)
Definition: astmm.h:97
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: db.c:365
#define ast_realloc(a, b)
Definition: astmm.h:103
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: db.c:260
static int dialgroup_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  cvalue 
)
static

Definition at line 204 of file func_dialgroup.c.

References ao2_alloc, ao2_container_alloc, ao2_find, ao2_link, ao2_ref, ao2_unlink, args, AST_APP_ARG, ast_copy_string(), AST_DECLARE_APP_ARGS, ast_log(), AST_NONSTANDARD_APP_ARGS, AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero(), dialgroup_refreshdb(), group::entries, entry_cmp_fn(), entry_hash_fn(), group_destroy(), LOG_ERROR, LOG_WARNING, group_entry::name, group::name, OBJ_UNLINK, and value.

Referenced by load_module().

205 {
206  struct group *grhead;
207  struct group_entry *entry;
208  int j, needrefresh = 1;
211  AST_APP_ARG(op);
212  );
213  AST_DECLARE_APP_ARGS(inter,
214  AST_APP_ARG(faces)[100];
215  );
216  char *value = ast_strdupa(cvalue);
217 
219  AST_NONSTANDARD_APP_ARGS(inter, value, '&');
220 
221  if (!(grhead = ao2_find(group_container, args.group, 0))) {
222  /* Create group */
223  grhead = ao2_alloc(sizeof(*grhead), group_destroy);
224  if (!grhead)
225  return -1;
227  if (!grhead->entries) {
228  ao2_ref(grhead, -1);
229  return -1;
230  }
231  ast_copy_string(grhead->name, args.group, sizeof(grhead->name));
232  ao2_link(group_container, grhead);
233  }
234 
235  if (ast_strlen_zero(args.op)) {
236  /* Wholesale replacement of the group */
237  args.op = "add";
238 
239  /* Remove all existing */
240  ao2_ref(grhead->entries, -1);
241  if (!(grhead->entries = ao2_container_alloc(37, entry_hash_fn, entry_cmp_fn))) {
242  ao2_unlink(group_container, grhead);
243  ao2_ref(grhead, -1);
244  return -1;
245  }
246  }
247 
248  if (strcasecmp(args.op, "add") == 0) {
249  for (j = 0; j < inter.argc; j++) {
250  /* Eliminate duplicates */
251  if ((entry = ao2_find(grhead->entries, inter.faces[j], 0))) {
252  ao2_ref(entry, -1);
253  continue;
254  }
255  if ((entry = ao2_alloc(sizeof(*entry), NULL))) {
256  ast_copy_string(entry->name, inter.faces[j], sizeof(entry->name));
257  ao2_link(grhead->entries, entry);
258  ao2_ref(entry, -1);
259  } else {
260  ast_log(LOG_WARNING, "Unable to add '%s' to dialgroup '%s'\n", inter.faces[j], grhead->name);
261  }
262  }
263  } else if (strncasecmp(args.op, "del", 3) == 0) {
264  for (j = 0; j < inter.argc; j++) {
265  if ((entry = ao2_find(grhead->entries, inter.faces[j], OBJ_UNLINK))) {
266  ao2_ref(entry, -1);
267  } else {
268  ast_log(LOG_WARNING, "Interface '%s' not found in dialgroup '%s'\n", inter.faces[j], grhead->name);
269  }
270  }
271  } else {
272  ast_log(LOG_ERROR, "Unrecognized operation: %s\n", args.op);
273  needrefresh = 0;
274  }
275  ao2_ref(grhead, -1);
276 
277  if (needrefresh) {
278  dialgroup_refreshdb(chan, args.group);
279  }
280 
281  return 0;
282 }
#define ao2_link(arg1, arg2)
Definition: astobj2.h:785
#define LOG_WARNING
Definition: logger.h:144
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
int value
Definition: syslog.c:39
static struct ao2_container * group_container
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
char name[AST_CHANNEL_NAME]
#define ao2_ref(o, delta)
Definition: astobj2.h:472
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
#define LOG_ERROR
Definition: logger.h:155
static struct @350 args
char name[AST_MAX_EXTENSION]
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:430
static int entry_hash_fn(const void *obj, const int flags)
#define ao2_find(arg1, arg2, arg3)
Definition: astobj2.h:964
static int entry_cmp_fn(void *obj1, void *name2, int flags)
static int dialgroup_refreshdb(struct ast_channel *chan, const char *cdialgroup)
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
#define ao2_container_alloc(arg1, arg2, arg3)
Definition: astobj2.h:734
static void group_destroy(void *vgroup)
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the &#39;nonstandard&#39; argument separation process for an application.
Definition: app.h:619
struct ao2_container * entries
#define ao2_unlink(arg1, arg2)
Definition: astobj2.h:817
static int entry_cmp_fn ( void *  obj1,
void *  name2,
int  flags 
)
static

Definition at line 119 of file func_dialgroup.c.

References CMP_MATCH, CMP_STOP, name, group_entry::name, and OBJ_POINTER.

Referenced by dialgroup_write().

120 {
121  struct group_entry *e1 = obj1, *e2 = name2;
122  char *name = name2;
123  if (flags & OBJ_POINTER)
124  return strcmp(e1->name, e2->name) ? 0 : CMP_MATCH | CMP_STOP;
125  else
126  return strcmp(e1->name, name) ? 0 : CMP_MATCH | CMP_STOP;
127 }
char name[AST_CHANNEL_NAME]
static const char name[]
static int entry_hash_fn ( const void *  obj,
const int  flags 
)
static

Definition at line 113 of file func_dialgroup.c.

References ast_str_hash(), and group_entry::name.

Referenced by dialgroup_write().

114 {
115  const struct group_entry *e = obj;
116  return ast_str_hash(e->name);
117 }
char name[AST_CHANNEL_NAME]
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:949
static int group_cmp_fn ( void *  obj1,
void *  name2,
int  flags 
)
static

Definition at line 103 of file func_dialgroup.c.

References CMP_MATCH, CMP_STOP, name, group::name, and OBJ_POINTER.

Referenced by load_module().

104 {
105  struct group *g1 = obj1, *g2 = name2;
106  char *name = name2;
107  if (flags & OBJ_POINTER)
108  return strcmp(g1->name, g2->name) ? 0 : CMP_MATCH | CMP_STOP;
109  else
110  return strcmp(g1->name, name) ? 0 : CMP_MATCH | CMP_STOP;
111 }
char name[AST_MAX_EXTENSION]
static const char name[]
static void group_destroy ( void *  vgroup)
static

Definition at line 91 of file func_dialgroup.c.

References ao2_ref, and group::entries.

Referenced by dialgroup_write().

92 {
93  struct group *group = vgroup;
94  ao2_ref(group->entries, -1);
95 }
#define ao2_ref(o, delta)
Definition: astobj2.h:472
struct ao2_container * entries
static int group_hash_fn ( const void *  obj,
const int  flags 
)
static

Definition at line 97 of file func_dialgroup.c.

References ast_str_hash(), and group::name.

Referenced by load_module().

98 {
99  const struct group *g = obj;
100  return ast_str_hash(g->name);
101 }
char name[AST_MAX_EXTENSION]
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:949
static int load_module ( void  )
static

Definition at line 297 of file func_dialgroup.c.

References ao2_container_alloc, ast_copy_string(), ast_custom_function_register, ast_db_freetree(), ast_db_gettree(), AST_MAX_EXTENSION, AST_MODULE_LOAD_DECLINE, ast_db_entry::data, dialgroup_write(), group_cmp_fn(), group_hash_fn(), ast_db_entry::key, and ast_db_entry::next.

298 {
299  struct ast_db_entry *dbtree, *tmp;
300  char groupname[AST_MAX_EXTENSION], *ptr;
301 
303  /* Refresh groups from astdb */
304  if ((dbtree = ast_db_gettree("dialgroup", NULL))) {
305  for (tmp = dbtree; tmp; tmp = tmp->next) {
306  ast_copy_string(groupname, tmp->key, sizeof(groupname));
307  if ((ptr = strrchr(groupname, '/'))) {
308  ptr++;
309  dialgroup_write(NULL, "", ptr, tmp->data);
310  }
311  }
312  ast_db_freetree(dbtree);
313  }
315  } else {
317  }
318 }
static struct ast_custom_function dialgroup_function
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
Definition: db.c:656
static struct ao2_container * group_container
struct ast_db_entry * next
Definition: astdb.h:31
static int group_hash_fn(const void *obj, const int flags)
#define AST_MAX_EXTENSION
Definition: channel.h:135
struct ast_db_entry * ast_db_gettree(const char *family, const char *keytree)
Get a list of values within the astdb tree If family is specified, only those keys will be returned...
Definition: db.c:631
Definition: astdb.h:30
char data[0]
Definition: astdb.h:33
static int group_cmp_fn(void *obj1, void *name2, int flags)
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
#define ao2_container_alloc(arg1, arg2, arg3)
Definition: astobj2.h:734
static int dialgroup_write(struct ast_channel *chan, const char *cmd, char *data, const char *cvalue)
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
char * key
Definition: astdb.h:32
static int unload_module ( void  )
static

Definition at line 290 of file func_dialgroup.c.

References ao2_ref, and ast_custom_function_unregister().

291 {
294  return res;
295 }
static struct ast_custom_function dialgroup_function
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
static struct ao2_container * group_container
#define ao2_ref(o, delta)
Definition: astobj2.h:472

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Dialgroup dialplan function" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static

Definition at line 320 of file func_dialgroup.c.

Definition at line 320 of file func_dialgroup.c.

struct ast_custom_function dialgroup_function
static
Initial value:
= {
.name = "DIALGROUP",
.read = dialgroup_read,
.write = dialgroup_write,
}
static int dialgroup_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int dialgroup_write(struct ast_channel *chan, const char *cmd, char *data, const char *cvalue)

Definition at line 284 of file func_dialgroup.c.

struct ao2_container* group_container = NULL
static

Definition at line 80 of file func_dialgroup.c.