Newer
Older
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2009, Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Data retrieval API.
*
* \author Brett Bryant <brettbryant@gmail.com>
* \author Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/_private.h"
#include <regex.h>
#include "asterisk/module.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/data.h"
#include "asterisk/astobj2.h"
#include "asterisk/xml.h"
#include "asterisk/cli.h"
#include "asterisk/term.h"
#include "asterisk/manager.h"
#include "asterisk/test.h"
#include "asterisk/frame.h"
#include "asterisk/codec.h"
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*** DOCUMENTATION
<manager name="DataGet" language="en_US">
<synopsis>
Retrieve the data api tree.
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
<parameter name="Path" required="true" />
<parameter name="Search" />
<parameter name="Filter" />
</syntax>
<description>
<para>Retrieve the data api tree.</para>
</description>
</manager>
***/
#define NUM_DATA_NODE_BUCKETS 59
#define NUM_DATA_RESULT_BUCKETS 59
#define NUM_DATA_SEARCH_BUCKETS 59
#define NUM_DATA_FILTER_BUCKETS 59
/*! \brief The last compatible version. */
static const uint32_t latest_handler_compatible_version = 0;
/*! \brief The last compatible version. */
static const uint32_t latest_query_compatible_version = 0;
/*! \brief Current handler structure version. */
static const uint32_t current_handler_version = AST_DATA_HANDLER_VERSION;
/*! \brief Current query structure version. */
static const uint32_t current_query_version = AST_DATA_QUERY_VERSION;
/*! \brief The data tree to be returned by the callbacks and
managed by functions local to this file. */
struct ast_data {
enum ast_data_type type;
/*! \brief The node content. */
union {
int32_t sint;
uint32_t uint;
double dbl;
unsigned int boolean;
char character;
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
struct in_addr ipaddr;
void *ptr;
} payload;
/*! \brief The filter node that depends on the current node,
* this is used only when creating the result tree. */
const struct data_filter *filter;
/*! \brief The list of nodes inside this node. */
struct ao2_container *children;
/*! \brief The name of the node. */
char name[0];
};
/*! \brief Type of comparisons allow in the search string. */
enum data_search_comparison {
DATA_CMP_UNKNOWN,
DATA_CMP_EQ, /* = */
DATA_CMP_NEQ, /* != */
DATA_CMP_GT, /* > */
DATA_CMP_GE, /* >= */
DATA_CMP_LT, /* < */
DATA_CMP_LE /* <= */
};
/*! \brief The list of nodes with their search requirement. */
struct ast_data_search {
/*! \brief The value of the comparison. */
char *value;
/*! \brief The type of comparison. */
enum data_search_comparison cmp_type;
/*! \brief reference another node. */
struct ao2_container *children;
/*! \brief The name of the node we are trying to compare. */
char name[0];
};
struct data_filter;
/*! \brief The filter node. */
struct data_filter {
/*! \brief node childrens. */
struct ao2_container *children;
/*! \brief glob list */
AST_LIST_HEAD_NOLOCK(glob_list_t, data_filter) glob_list;
/*! \brief glob list entry */
AST_LIST_ENTRY(data_filter) list;
/*! \brief node name. */
char name[0];
};
/*! \brief A data container node pointing to the registered handler. */
struct data_provider {
/*! \brief node content handler. */
const struct ast_data_handler *handler;
/*! \brief Module providing this handler. */
struct ast_module *module;
/*! \brief children nodes. */
struct ao2_container *children;
/*! \brief Who registered this node. */
const char *registrar;
/*! \brief Node name. */
char name[0];
};
/*! \brief This structure is used by the iterator. */
struct ast_data_iterator {
/*! \brief The internal iterator. */
struct ao2_iterator internal_iterator;
/*! \brief The last returned node. */
struct ast_data *last;
/*! \brief The iterator pattern. */
const char *pattern;
/*! \brief The compiled patter. */
regex_t regex_pattern;
/*! \brief is a regular expression. */
unsigned int is_pattern:1;
};
struct {
/*! \brief The asterisk data main content structure. */
struct ao2_container *container;
/*! \brief asterisk data locking mechanism. */
ast_rwlock_t lock;
} root_data;
static void __data_result_print_cli(int fd, const struct ast_data *root, uint32_t depth);
/*!
* \internal
* \brief Common string hash function.
* \see ast_data_init
*/
static int data_provider_hash(const void *obj, const int flags)
{
const struct data_provider *node = obj;
return ast_str_case_hash(node->name);
}
/*!
* \internal
* \brief Compare two data_provider's.
* \see ast_data_init
*/
static int data_provider_cmp(void *obj1, void *obj2, int flags)
{
struct data_provider *node1 = obj1, *node2 = obj2;
return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
}
/*!
* \internal
* \brief Common string hash function for data nodes
*/
static int data_result_hash(const void *obj, const int flags)
{
const struct ast_data *node = obj;
return ast_str_hash(node->name);
}
/*!
* \internal
* \brief Common string comparison function
*/
static int data_result_cmp(void *obj, void *arg, int flags)
{
struct ast_data *node1 = obj, *node2 = arg;
return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
}
/*!
* \internal
* \brief Lock the data registered handlers structure for writing.
* \see data_unlock
*/
#define data_write_lock() ast_rwlock_wrlock(&root_data.lock)
/*!
* \internal
* \brief Lock the data registered handlers structure for reading.
* \see data_unlock
*/
#define data_read_lock() ast_rwlock_rdlock(&root_data.lock)
/*!
* \internal
* \brief Unlock the data registered handlers structure.
*/
#define data_unlock() ast_rwlock_unlock(&root_data.lock)
/*!
* \internal
* \brief Check if a version is compatible with the current core.
* \param[in] structure_version The current structure version.
* \param[in] latest_compatible The latest compatible version.
* \param[in] current The current Data API version.
* \retval 1 If the module is compatible.
* \retval 0 If the module is NOT compatible.
*/
static int data_structure_compatible(int structure_version, uint32_t latest_compatible,
uint32_t current)
{
if (structure_version >= latest_compatible && structure_version <= current) {
return 1;
}
ast_log(LOG_ERROR, "A module is not compatible with the"
"current data api version\n");
return 0;
}
/*!
* \internal
* \brief Get the next node name in a path (/node1/node2)
* Avoid null nodes like //node1//node2/node3.
* \param[in] path The path where we are going to search for the next node name.
* \retval The next node name we found inside the given path.
* \retval NULL if there are no more node names.
*/
static char *next_node_name(char **path)
{
char *res;
do {
res = strsep(path, "/");
} while (res && ast_strlen_zero(res));
return res;
}
/*!
* \internal
* \brief Release the memory allocated by a call to ao2_alloc.
*/
static void data_provider_destructor(void *obj)
{
struct data_provider *provider = obj;
ao2_ref(provider->children, -1);
}
/*!
* \internal
* \brief Create a new data node.
* \param[in] name The name of the node we are going to create.
* \param[in] handler The handler registered for this node.
* \param[in] registrar The name of the registrar.
* \retval NULL on error.
* \retval The allocated data node structure.
*/
static struct data_provider *data_provider_new(const char *name,
const struct ast_data_handler *handler, const char *registrar)
{
struct data_provider *node;
size_t namelen;
namelen = strlen(name) + 1;
node = ao2_alloc(sizeof(*node) + namelen, data_provider_destructor);
if (!node) {
return NULL;
}
node->handler = handler;
node->registrar = registrar;
strcpy(node->name, name);
/* initialize the childrens container. */
if (!(node->children = ao2_container_alloc(NUM_DATA_NODE_BUCKETS,
data_provider_hash, data_provider_cmp))) {
ao2_ref(node, -1);
return NULL;
}
return node;
}
/*!
* \internal
* \brief Add a child node named 'name' to the 'parent' node.
* \param[in] parent Where to add the child node.
* \param[in] name The name of the child node.
* \param[in] handler The handler structure.
* \param[in] registrar Who registered this node.
* \retval NULL on error.
* \retval A newly allocated child in parent.
*/
static struct data_provider *data_provider_add_child(struct ao2_container *parent,
const char *name, const struct ast_data_handler *handler, const char *registrar)
{
struct data_provider *child;
child = data_provider_new(name, handler, registrar);
if (!child) {
return NULL;
}
ao2_link(parent, child);
return child;
}
/*!
* \internal
* \brief Find a child node, based on his name.
* \param[in] parent Where to find the node.
* \param[in] name The node name to find.
* \param[in] registrar Also check if the node was being used by this registrar.
* \retval NULL if a node wasn't found.
* \retval The node found.
* \note Remember to decrement the ref count of the returned node after using it.
*/
static struct data_provider *data_provider_find(struct ao2_container *parent,
const char *name, const char *registrar)
{
struct data_provider *find_node, *found;
/* XXX avoid allocating a new data node for searching... */
find_node = data_provider_new(name, NULL, NULL);
if (!find_node) {
return NULL;
}
found = ao2_find(parent, find_node, OBJ_POINTER);
/* free the created node used for searching. */
ao2_ref(find_node, -1);
if (found && found->registrar && registrar) {
if (strcmp(found->registrar, registrar)) {
/* if the name doesn't match, do not return this node. */
ast_debug(1, "Registrar doesn't match, node was registered"
" by '%s' and we are searching for '%s'\n",
found->registrar, registrar);
ao2_ref(found, -1);
return NULL;
}
}
return found;
}
/*!
* \internal
* \brief Release a group of nodes.
* \param[in] parent The parent node.
* \param[in] path The path of nodes to release.
* \param[in] registrar Who registered this node.
* \retval <0 on error.
* \retval 0 on success.
* \see data_provider_create
*/
static int data_provider_release(struct ao2_container *parent, const char *path,
const char *registrar)
{
char *node_name, *rpath;
struct data_provider *child;
int ret = 0;
rpath = ast_strdupa(path);
node_name = next_node_name(&rpath);
if (!node_name) {
return -1;
}
child = data_provider_find(parent, node_name, registrar);
if (!child) {
return -1;
}
/* if this is not a terminal node. */
if (!child->handler && rpath) {
ret = data_provider_release(child->children, rpath, registrar);
}
/* if this node is empty, unlink it. */
if (!ret && !ao2_container_count(child->children)) {
ao2_unlink(parent, child);
}
ao2_ref(child, -1);
return ret;
}
/*!
* \internal
* \brief Release every node registered by 'registrar'.
* \param[in] parent The parent node.
* \param[in] registrar
* \see __ast_data_unregister
*/
static void data_provider_release_all(struct ao2_container *parent,
const char *registrar)
{
struct ao2_iterator i;
struct data_provider *node;
i = ao2_iterator_init(parent, 0);
while ((node = ao2_iterator_next(&i))) {
if (!node->handler) {
/* this is a non-terminal node, go inside it. */
data_provider_release_all(node->children, registrar);
if (!ao2_container_count(node->children)) {
/* if this node was left empty, unlink it. */
ao2_unlink(parent, node);
}
} else {
if (!strcmp(node->registrar, registrar)) {
/* if the registrars match, release it! */
ao2_unlink(parent, node);
}
}
ao2_ref(node, -1);
}
ao2_iterator_destroy(&i);
}
/*!
* \internal
* \brief Create the middle nodes for the specified path (asterisk/testnode1/childnode)
* \param[in] parent Where to add the middle nodes structure.
* \param[in] path The path of nodes to add.
* \param[in] registrar Who is trying to create this node provider.
* \retval NULL on error.
* \retval The created node.
* \see data_provider_release
*/
static struct data_provider *data_provider_create(struct ao2_container *parent,
const char *path, const char *registrar)
{
char *rpath, *node_name;
struct data_provider *child, *ret = NULL;
rpath = ast_strdupa(path);
node_name = next_node_name(&rpath);
if (!node_name) {
/* no more nodes to create. */
return NULL;
}
child = data_provider_find(parent, node_name, NULL);
if (!child) {
/* nodes without handler are non-terminal nodes. */
child = data_provider_add_child(parent, node_name, NULL, registrar);
}
if (rpath) {
ret = data_provider_create(child->children, rpath, registrar);
if (ret) {
ao2_ref(child, -1);
}
}
return ret ? ret : child;
}
int __ast_data_register(const char *path, const struct ast_data_handler *handler,
const char *registrar, struct ast_module *mod)
{
struct data_provider *node;
if (!path) {
return -1;
}
/* check if the handler structure is compatible. */
if (!data_structure_compatible(handler->version,
latest_handler_compatible_version,
current_handler_version)) {
return -1;
}
/* create the node structure for the registered handler. */
data_write_lock();
node = data_provider_create(root_data.container, path, registrar);
if (!node) {
ast_log(LOG_ERROR, "Unable to create the specified path (%s) "
"for '%s'.\n", path, registrar);
data_unlock();
return -1;
}
if (ao2_container_count(node->children) || node->handler) {
ast_log(LOG_ERROR, "The node '%s' was already registered. "
"We were unable to register '%s' for registrar '%s'.\n",
node->name, path, registrar);
ao2_ref(node, -1);
data_unlock();
return -1;
}
/* add handler to that node. */
node->handler = handler;
node->module = mod;
ao2_ref(node, -1);
data_unlock();
return 0;
}
int __ast_data_register_multiple(const struct ast_data_entry *data_entries,
size_t entries, const char *registrar, struct ast_module *mod)
{
int i, res;
for (i = 0; i < entries; i++) {
res = __ast_data_register(data_entries[i].path, data_entries[i].handler,
registrar, mod);
if (res) {
/* unregister all the already registered nodes, and make
* this an atomic action. */
while ((--i) >= 0) {
__ast_data_unregister(data_entries[i].path, registrar);
}
return -1;
}
}
return 0;
}
int __ast_data_unregister(const char *path, const char *registrar)
{
int ret = 0;
data_write_lock();
if (path) {
ret = data_provider_release(root_data.container, path, registrar);
} else {
data_provider_release_all(root_data.container, registrar);
}
data_unlock();
if (path && ret) {
ast_log(LOG_ERROR, "Unable to unregister '%s' for '%s'\n",
path, registrar);
}
return ret;
}
/*!
* \internal
* \brief Is a char used to specify a comparison?
* \param[in] a Character to evaluate.
* \retval 1 It is a char used to specify a comparison.
* \retval 0 It is NOT a char used to specify a comparison.
*/
static int data_search_comparison_char(char a)
{
switch (a) {
case '!':
case '=':
case '<':
case '>':
return 1;
}
return 0;
}
/*!
* \internal
* \brief Get the type of comparison.
*/
static enum data_search_comparison data_search_comparison_type(const char *comparison)
{
if (!strcmp(comparison, "=")) {
return DATA_CMP_EQ;
} else if (!strcmp(comparison, "!=")) {
return DATA_CMP_NEQ;
} else if (!strcmp(comparison, "<")) {
return DATA_CMP_LT;
} else if (!strcmp(comparison, ">")) {
return DATA_CMP_GT;
} else if (!strcmp(comparison, "<=")) {
return DATA_CMP_LE;
} else if (!strcmp(comparison, ">=")) {
return DATA_CMP_GE;
}
return DATA_CMP_UNKNOWN;
}
/*!
* \internal
* \brief Common string hash function for data nodes
*/
static int data_search_hash(const void *obj, const int flags)
{
const struct ast_data_search *node = obj;
return ast_str_hash(node->name);
}
/*!
* \internal
* \brief Common string comparison function
*/
static int data_search_cmp(void *obj, void *arg, int flags)
{
struct ast_data_search *node1 = obj, *node2 = arg;
return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
}
/*!
* \internal
* \brief Destroy the ao2 search node.
*/
static void data_search_destructor(void *obj)
{
struct ast_data_search *node = obj;
if (node->value) {
ast_free(node->value);
}
ao2_ref(node->children, -1);
}
/*!
* \internal
* \brief Allocate a search node.
* \retval NULL on error.
* \retval non-NULL The allocated search node structure.
*/
static struct ast_data_search *data_search_alloc(const char *name)
{
struct ast_data_search *res;
size_t name_len = strlen(name) + 1;
res = ao2_alloc(sizeof(*res) + name_len, data_search_destructor);
if (!res) {
return NULL;
}
res->children = ao2_container_alloc(NUM_DATA_SEARCH_BUCKETS, data_search_hash,
data_search_cmp);
if (!res->children) {
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
ao2_ref(res, -1);
return NULL;
}
strcpy(res->name, name);
return res;
}
/*!
* \internal
* \brief Find a child node, based on his name.
* \param[in] parent Where to find the node.
* \param[in] name The node name to find.
* \retval NULL if a node wasn't found.
* \retval The node found.
* \note Remember to decrement the ref count of the returned node after using it.
*/
static struct ast_data_search *data_search_find(struct ao2_container *parent,
const char *name)
{
struct ast_data_search *find_node, *found;
find_node = data_search_alloc(name);
if (!find_node) {
return NULL;
}
found = ao2_find(parent, find_node, OBJ_POINTER);
/* free the created node used for searching. */
ao2_ref(find_node, -1);
return found;
}
/*!
* \internal
* \brief Add a child node named 'name' to the 'parent' node.
* \param[in] parent Where to add the child node.
* \param[in] name The name of the child node.
* \retval NULL on error.
* \retval A newly allocated child in parent.
*/
static struct ast_data_search *data_search_add_child(struct ao2_container *parent,
const char *name)
{
struct ast_data_search *child;
child = data_search_alloc(name);
if (!child) {
return NULL;
}
ao2_link(parent, child);
return child;
}
/*!
* \internal
* \brief Create the middle nodes for the specified path (asterisk/testnode1/childnode)
* \param[in] parent Where to add the middle nodes structure.
* \param[in] path The path of nodes to add.
* \retval NULL on error.
* \retval The created node.
*/
static struct ast_data_search *data_search_create(struct ao2_container *parent,
const char *path)
{
char *rpath, *node_name;
struct ast_data_search *child = NULL;
struct ao2_container *current = parent;
rpath = ast_strdupa(path);
node_name = next_node_name(&rpath);
while (node_name) {
child = data_search_find(current, node_name);
if (!child) {
child = data_search_add_child(current, node_name);
}
ao2_ref(child, -1);
current = child->children;
node_name = next_node_name(&rpath);
}
return child;
}
/*!
* \internal
* \brief Allocate a tree with the search string parsed.
* \param[in] search_string The search string.
* \retval NULL on error.
* \retval non-NULL A dynamically allocated search tree.
*/
static struct ast_data_search *data_search_generate(const char *search_string)
{
struct ast_str *name, *value, *comparison;
char *elements, *search_string_dup, *saveptr;
int i;
struct ast_data_search *root, *child;
enum data_search_comparison cmp_type;
size_t search_string_len;
if (!search_string) {
ast_log(LOG_ERROR, "You must pass a valid search string.\n");
return NULL;
}
search_string_len = strlen(search_string);
name = ast_str_create(search_string_len);
if (!name) {
return NULL;
}
value = ast_str_create(search_string_len);
if (!value) {
ast_free(name);
return NULL;
}
comparison = ast_str_create(search_string_len);
if (!comparison) {
ast_free(name);
ast_free(value);
return NULL;
}
search_string_dup = ast_strdupa(search_string);
/* Create the root node (just used as a container) */
root = data_search_alloc("/");
if (!root) {
ast_free(name);
ast_free(value);
ast_free(comparison);
return NULL;
}
for (elements = strtok_r(search_string_dup, ",", &saveptr); elements;
elements = strtok_r(NULL, ",", &saveptr)) {
/* Parse the name */
ast_str_reset(name);
for (i = 0; !data_search_comparison_char(elements[i]) &&
elements[i]; i++) {
ast_str_append(&name, 0, "%c", elements[i]);
}
/* check if the syntax is ok. */
if (!data_search_comparison_char(elements[i])) {
/* if this is the end of the string, then this is
* an error! */
ast_log(LOG_ERROR, "Invalid search string!\n");
continue;
}
/* parse the comparison string. */
ast_str_reset(comparison);
for (; data_search_comparison_char(elements[i]) && elements[i]; i++) {
ast_str_append(&comparison, 0, "%c", elements[i]);
}
/* parse the value string. */
ast_str_reset(value);
for (; elements[i]; i++) {
ast_str_append(&value, 0, "%c", elements[i]);
}
cmp_type = data_search_comparison_type(ast_str_buffer(comparison));
if (cmp_type == DATA_CMP_UNKNOWN) {
ast_log(LOG_ERROR, "Invalid comparison '%s'\n",
ast_str_buffer(comparison));
continue;
}
/* add this node to the tree. */
child = data_search_create(root->children, ast_str_buffer(name));
if (child) {
child->cmp_type = cmp_type;
child->value = ast_strdup(ast_str_buffer(value));
}
}
ast_free(name);
ast_free(value);
ast_free(comparison);
return root;
}
/*!
* \internal
* \brief Release the allocated memory for the search tree.
* \param[in] search The search tree root node.
*/
static void data_search_release(struct ast_data_search *search)
{
ao2_ref(search, -1);
}
/*!
* \internal
* \brief Based on the kind of comparison and the result in cmpval, return
* if it matches.
* \param[in] cmpval A result returned by a strcmp() for example.
* \param[in] comparison_type The kind of comparison (<,>,=,!=,...)
* \retval 1 If the comparison doesn't match.
* \retval 0 If the comparison matches.
*/
static inline int data_search_comparison_result(int cmpval,
enum data_search_comparison comparison_type)
{
switch (comparison_type) {
case DATA_CMP_GE:
if (cmpval >= 0) {
return 0;
}
break;
case DATA_CMP_LE:
if (cmpval <= 0) {
return 0;
}
break;
case DATA_CMP_EQ:
if (cmpval == 0) {
return 0;
}
break;
case DATA_CMP_NEQ:
if (cmpval != 0) {
return 0;
}
break;
case DATA_CMP_LT:
if (cmpval < 0) {
return 0;
}
break;
case DATA_CMP_GT:
if (cmpval > 0) {
return 0;
}
break;
case DATA_CMP_UNKNOWN:
break;
}
return 1;
}
/*!
* \internal
* \brief Get an internal node, from the search tree.
* \param[in] node A node container.
* \param[in] path The path to the needed internal node.
* \retval NULL if the internal node is not found.
* \retval non-NULL the internal node with path 'path'.
*/
static struct ast_data_search *data_search_get_node(const struct ast_data_search *node,
const char *path)
{
char *savepath, *node_name;
struct ast_data_search *child, *current = (struct ast_data_search *) node;
if (!node) {
return NULL;
}
savepath = ast_strdupa(path);
node_name = next_node_name(&savepath);
while (node_name) {
child = data_search_find(current->children, node_name);
if (current != node) {
ao2_ref(current, -1);
}
if (!child) {
return NULL;
};
current = child;
node_name = next_node_name(&savepath);
}
return current;
}
/*!
* \internal
* \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
* current string value.
* .search = "somename=somestring"
* name = "somename"
* value is the current value of something and will be evaluated against "somestring".
* \param[in] root The root node pointer of the search tree.
* \param[in] name The name of the specific.
* \param[in] value The value to compare.