Newer
Older
* \returns The strcmp return value.
*/
static int data_search_cmp_string(const struct ast_data_search *root, const char *name,
char *value)
{
struct ast_data_search *child;
enum data_search_comparison cmp_type;
int ret;
child = data_search_get_node(root, name);
if (!child) {
return 0;
}
ret = strcmp(value, child->value);
cmp_type = child->cmp_type;
ao2_ref(child, -1);
return data_search_comparison_result(ret, cmp_type);
}
/*!
* \internal
* \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
* current pointer address value.
* .search = "something=0x32323232"
* name = "something"
* value is the current value of something and will be evaluated against "0x32323232".
* \param[in] root The root node pointer of the search tree.
* \param[in] name The name of the specific.
* \param[in] ptr The pointer address to compare.
* \returns The (value - current_value) result.
*/
static int data_search_cmp_ptr(const struct ast_data_search *root, const char *name,
void *ptr)
{
struct ast_data_search *child;
enum data_search_comparison cmp_type;
void *node_ptr;
child = data_search_get_node(root, name);
if (!child) {
return 0;
}
cmp_type = child->cmp_type;
if (sscanf(child->value, "%p", &node_ptr) <= 0) {
return 1;
}
ao2_ref(child, -1);
return data_search_comparison_result((node_ptr - ptr), cmp_type);
}
/*!
* \internal
* \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
* current ipv4 address value.
* .search = "something=192.168.2.2"
* name = "something"
* value is the current value of something and will be evaluated against "192.168.2.2".
* \param[in] root The root node pointer of the search tree.
* \param[in] name The name of the specific.
* \param[in] addr The ipv4 address value to compare.
* \returns The (value - current_value) result.
*/
static int data_search_cmp_ipaddr(const struct ast_data_search *root, const char *name,
struct in_addr addr)
{
struct ast_data_search *child;
enum data_search_comparison cmp_type;
struct in_addr node_addr;
child = data_search_get_node(root, name);
if (!child) {
return 0;
}
cmp_type = child->cmp_type;
inet_aton(child->value, &node_addr);
ao2_ref(child, -1);
return data_search_comparison_result((node_addr.s_addr - addr.s_addr), cmp_type);
}
/*!
* \internal
* \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
* current boolean value.
* .search = "something=true"
* name = "something"
* value is the current value of something and will be evaluated against "true".
* \param[in] root The root node pointer of the search tree.
* \param[in] name The name of the specific.
* \param[in] value The boolean value to compare.
* \returns The (value - current_value) result.
*/
static int data_search_cmp_bool(const struct ast_data_search *root, const char *name,
unsigned int value)
{
struct ast_data_search *child;
unsigned int node_value;
enum data_search_comparison cmp_type;
child = data_search_get_node(root, name);
if (!child) {
return 0;
}
node_value = abs(ast_true(child->value));
cmp_type = child->cmp_type;
ao2_ref(child, -1);
return data_search_comparison_result(value - node_value, cmp_type);
}
/*!
* \internal
* \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
* current double value.
* .search = "something=222"
* name = "something"
* value is the current value of something and will be evaluated against "222".
* \param[in] root The root node pointer of the search tree.
* \param[in] name The name of the specific.
* \param[in] value The double value to compare.
* \returns The (value - current_value) result.
*/
static int data_search_cmp_dbl(const struct ast_data_search *root, const char *name,
double value)
{
struct ast_data_search *child;
double node_value;
enum data_search_comparison cmp_type;
child = data_search_get_node(root, name);
if (!child) {
return 0;
}
node_value = strtod(child->value, NULL);
cmp_type = child->cmp_type;
ao2_ref(child, -1);
return data_search_comparison_result(value - node_value, cmp_type);
}
/*!
* \internal
* \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
* current unsigned integer value.
* .search = "something=10"
* name = "something"
* value is the current value of something and will be evaluated against "10".
* \param[in] root The root node pointer of the search tree.
* \param[in] name The name of the specific.
* \param[in] value The unsigned value to compare.
* \returns The strcmp return value.
*/
static int data_search_cmp_uint(const struct ast_data_search *root, const char *name,
unsigned int value)
{
struct ast_data_search *child;
unsigned int node_value;
enum data_search_comparison cmp_type;
child = data_search_get_node(root, name);
if (!child) {
return 0;
}
node_value = atoi(child->value);
cmp_type = child->cmp_type;
ao2_ref(child, -1);
return data_search_comparison_result(value - node_value, cmp_type);
}
/*!
* \internal
* \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
* current signed integer value.
* .search = "something=10"
* name = "something"
* value is the current value of something and will be evaluated against "10".
* \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.
* \returns The strcmp return value.
*/
static int data_search_cmp_int(const struct ast_data_search *root, const char *name,
int value)
{
struct ast_data_search *child;
int node_value;
enum data_search_comparison cmp_type;
child = data_search_get_node(root, name);
if (!child) {
return 0;
}
node_value = atoi(child->value);
cmp_type = child->cmp_type;
ao2_ref(child, -1);
return data_search_comparison_result(value - node_value, cmp_type);
}
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
/*!
* \internal
* \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
* current character value.
* .search = "something=c"
* name = "something"
* value is the current value of something and will be evaluated against "c".
* \param[in] root The root node pointer of the search tree.
* \param[in] name The name of the specific.
* \param[in] value The boolean value to compare.
* \returns The (value - current_value) result.
*/
static int data_search_cmp_char(const struct ast_data_search *root, const char *name,
char value)
{
struct ast_data_search *child;
char node_value;
enum data_search_comparison cmp_type;
child = data_search_get_node(root, name);
if (!child) {
return 0;
}
node_value = *(child->value);
cmp_type = child->cmp_type;
ao2_ref(child, -1);
return data_search_comparison_result(value - node_value, cmp_type);
}
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
/*!
* \internal
* \brief Get the member pointer, from a mapping structure, based on its name.
* \XXX We will need to improve performance here!!.
* \retval <0 if the member was not found.
* \retval >=0 The member position in the mapping structure.
*/
static inline int data_search_mapping_find(const struct ast_data_mapping_structure *map,
size_t mapping_len,
const char *member_name)
{
int i;
for (i = 0; i < mapping_len; i++) {
if (!strcmp(map[i].name, member_name)) {
return i;
}
}
return -1;
}
int __ast_data_search_cmp_structure(const struct ast_data_search *search,
const struct ast_data_mapping_structure *mapping, size_t mapping_len,
void *structure, const char *structure_name)
{
struct ao2_iterator i;
struct ast_data_search *node, *struct_children;
int member, notmatch = 0;
if (!search) {
return 0;
}
struct_children = data_search_get_node(search, structure_name);
if (!struct_children) {
return 0;
}
i = ao2_iterator_init(struct_children->children, 0);
while ((node = ao2_iterator_next(&i))) {
member = data_search_mapping_find(mapping, mapping_len, node->name);
if (member < 0) {
/* the structure member name doesn't match! */
ao2_ref(node, -1);
ao2_ref(struct_children, -1);
ao2_iterator_destroy(&i);
return 0;
}
notmatch = 0;
switch (mapping[member].type) {
case AST_DATA_PASSWORD:
notmatch = data_search_cmp_string(struct_children,
node->name,
mapping[member].get.AST_DATA_PASSWORD(structure));
break;
case AST_DATA_TIMESTAMP:
notmatch = data_search_cmp_uint(struct_children,
node->name,
mapping[member].get.AST_DATA_TIMESTAMP(structure));
break;
case AST_DATA_SECONDS:
notmatch = data_search_cmp_uint(struct_children,
node->name,
mapping[member].get.AST_DATA_SECONDS(structure));
break;
case AST_DATA_MILLISECONDS:
notmatch = data_search_cmp_uint(struct_children,
node->name,
mapping[member].get.AST_DATA_MILLISECONDS(structure));
break;
notmatch = data_search_cmp_string(struct_children,
node->name,
mapping[member].get.AST_DATA_STRING(structure));
break;
case AST_DATA_CHARACTER:
notmatch = data_search_cmp_char(struct_children,
node->name,
mapping[member].get.AST_DATA_CHARACTER(structure));
break;
notmatch = data_search_cmp_int(struct_children,
node->name,
mapping[member].get.AST_DATA_INTEGER(structure));
break;
case AST_DATA_BOOLEAN:
notmatch = data_search_cmp_bool(struct_children,
node->name,
mapping[member].get.AST_DATA_BOOLEAN(structure));
break;
case AST_DATA_UNSIGNED_INTEGER:
notmatch = data_search_cmp_uint(struct_children,
node->name,
mapping[member].get.AST_DATA_UNSIGNED_INTEGER(structure));
break;
case AST_DATA_DOUBLE:
notmatch = data_search_cmp_dbl(struct_children,
node->name,
mapping[member].get.AST_DATA_DOUBLE(structure));
break;
case AST_DATA_IPADDR:
notmatch = data_search_cmp_ipaddr(struct_children,
node->name,
mapping[member].get.AST_DATA_IPADDR(structure));
break;
case AST_DATA_POINTER:
notmatch = data_search_cmp_ptr(struct_children,
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
node->name,
mapping[member].get.AST_DATA_POINTER(structure));
break;
case AST_DATA_CONTAINER:
break;
}
ao2_ref(node, -1);
}
ao2_iterator_destroy(&i);
ao2_ref(struct_children, -1);
return notmatch;
}
/*!
* \internal
* \brief Release the memory allocated by a call to ao2_alloc.
*/
static void data_result_destructor(void *obj)
{
struct ast_data *root = obj;
switch (root->type) {
case AST_DATA_PASSWORD:
ast_free(root->payload.str);
ao2_ref(root->children, -1);
break;
case AST_DATA_POINTER:
case AST_DATA_CHARACTER:
case AST_DATA_CONTAINER:
case AST_DATA_INTEGER:
case AST_DATA_TIMESTAMP:
case AST_DATA_SECONDS:
case AST_DATA_MILLISECONDS:
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
case AST_DATA_UNSIGNED_INTEGER:
case AST_DATA_DOUBLE:
case AST_DATA_BOOLEAN:
case AST_DATA_IPADDR:
ao2_ref(root->children, -1);
break;
}
}
static struct ast_data *data_result_create(const char *name)
{
struct ast_data *res;
size_t namelen;
namelen = ast_strlen_zero(name) ? 1 : strlen(name) + 1;
res = ao2_alloc(sizeof(*res) + namelen, data_result_destructor);
if (!res) {
return NULL;
}
strcpy(res->name, namelen ? name : "");
/* initialize the children container */
res->children = ao2_container_alloc(NUM_DATA_RESULT_BUCKETS, data_result_hash,
data_result_cmp);
if (!res->children) {
ao2_ref(res, -1);
return NULL;
}
/* set this node as a container. */
res->type = AST_DATA_CONTAINER;
return res;
}
/*!
* \internal
* \brief Find a child node, based on its name.
* \param[in] root The starting point.
* \param[in] name The child name.
* \retval NULL if the node wasn't found.
* \retval non-NULL the node we were looking for.
*/
static struct ast_data *data_result_find_child(struct ast_data *root, const char *name)
{
struct ast_data *found, *find_node;
find_node = data_result_create(name);
if (!find_node) {
return NULL;
}
found = ao2_find(root->children, find_node, OBJ_POINTER);
/* release the temporary created node used for searching. */
ao2_ref(find_node, -1);
return found;
}
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
int ast_data_search_match(const struct ast_data_search *search, struct ast_data *data)
{
struct ao2_iterator i, ii;
struct ast_data_search *s, *s_child;
struct ast_data *d_child;
int notmatch = 1;
if (!search) {
return 1;
}
s_child = data_search_find(search->children, data->name);
if (!s_child) {
/* nothing to compare */
ao2_ref(s_child, -1);
return 1;
}
i = ao2_iterator_init(s_child->children, 0);
while ((s = ao2_iterator_next(&i))) {
if (!ao2_container_count(s->children)) {
/* compare this search node with every data node */
d_child = data_result_find_child(data, s->name);
if (!d_child) {
ao2_ref(s, -1);
notmatch = 1;
continue;
}
switch (d_child->type) {
case AST_DATA_PASSWORD:
case AST_DATA_STRING:
notmatch = data_search_cmp_string(s_child, d_child->name,
d_child->payload.str);
break;
case AST_DATA_CHARACTER:
notmatch = data_search_cmp_char(s_child, d_child->name,
d_child->payload.character);
break;
case AST_DATA_INTEGER:
notmatch = data_search_cmp_int(s_child, d_child->name,
d_child->payload.sint);
break;
case AST_DATA_BOOLEAN:
notmatch = data_search_cmp_bool(s_child, d_child->name,
d_child->payload.boolean);
break;
case AST_DATA_UNSIGNED_INTEGER:
notmatch = data_search_cmp_uint(s_child, d_child->name,
d_child->payload.uint);
break;
case AST_DATA_TIMESTAMP:
case AST_DATA_SECONDS:
case AST_DATA_MILLISECONDS:
case AST_DATA_DOUBLE:
notmatch = data_search_cmp_uint(s_child, d_child->name,
d_child->payload.dbl);
break;
case AST_DATA_IPADDR:
notmatch = data_search_cmp_ipaddr(s_child, d_child->name,
d_child->payload.ipaddr);
break;
case AST_DATA_POINTER:
notmatch = data_search_cmp_ptr(s_child, d_child->name,
d_child->payload.ptr);
break;
case AST_DATA_CONTAINER:
break;
}
ao2_ref(d_child, -1);
} else {
ii = ao2_iterator_init(data->children, 0);
while ((d_child = ao2_iterator_next(&ii))) {
if (strcmp(d_child->name, s->name)) {
ao2_ref(d_child, -1);
continue;
}
if (!(notmatch = !ast_data_search_match(s_child, d_child))) {
/* do not continue if we have a match. */
ao2_ref(d_child, -1);
break;
}
ao2_ref(d_child, -1);
}
ao2_iterator_destroy(&ii);
}
ao2_ref(s, -1);
if (notmatch) {
/* do not continue if we don't have a match. */
break;
}
}
ao2_iterator_destroy(&i);
ao2_ref(s_child, -1);
return !notmatch;
}
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
/*!
* \internal
* \brief Get an internal node, from the result set.
* \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 *data_result_get_node(struct ast_data *node,
const char *path)
{
char *savepath, *node_name;
struct ast_data *child, *current = node;
savepath = ast_strdupa(path);
node_name = next_node_name(&savepath);
while (node_name) {
child = data_result_find_child(current, node_name);
if (current != node) {
ao2_ref(current, -1);
}
if (!child) {
return NULL;
}
current = child;
node_name = next_node_name(&savepath);
}
/* do not increment the refcount of the returned object. */
if (current != node) {
ao2_ref(current, -1);
}
return current;
}
/*!
* \internal
* \brief Add a child to the specified root node.
* \param[in] root The root node pointer.
* \param[in] child The child to add to the root node.
*/
static void data_result_add_child(struct ast_data *root, struct ast_data *child)
{
ao2_link(root->children, child);
}
/*!
* \internal
* \brief Common string hash function for data nodes
*/
static int data_filter_hash(const void *obj, const int flags)
{
const struct data_filter *node = obj;
return ast_str_hash(node->name);
}
/*!
* \internal
* \brief Common string comparison function
*/
static int data_filter_cmp(void *obj, void *arg, int flags)
{
struct data_filter *node1 = obj, *node2 = arg;
return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
}
/*!
* \internal
* \brief Destroy a data filter tree.
* \param[in] obj Data filter list to be destroyed.
*/
static void data_filter_destructor(void *obj)
{
struct data_filter *filter = obj, *globres;
while ((globres = AST_LIST_REMOVE_HEAD(&(filter->glob_list), list))) {
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
ao2_ref(globres, -1);
}
ao2_ref(filter->children, -1);
}
/*!
* \internal
* \brief Allocate a filter node.
* \retval NULL on error.
* \retval non-NULL The allocated search node structure.
*/
static struct data_filter *data_filter_alloc(const char *name)
{
char *globname, *token;
struct data_filter *res, *globfilter;
size_t name_len = strlen(name) + 1;
res = ao2_alloc(sizeof(*res) + name_len, data_filter_destructor);
if (!res) {
return NULL;
}
res->children = ao2_container_alloc(NUM_DATA_FILTER_BUCKETS, data_filter_hash,
data_filter_cmp);
if (!res->children) {
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
ao2_ref(res, -1);
return NULL;
}
strcpy(res->name, name);
if (strchr(res->name, '*')) {
globname = ast_strdupa(res->name);
while ((token = strsep(&globname, "*"))) {
globfilter = data_filter_alloc(token);
AST_LIST_INSERT_TAIL(&(res->glob_list), globfilter, list);
}
}
return res;
}
/*!
* \internal
* \brief Release a filter tree.
* \param[in] filter The filter tree root node.
*/
static void data_filter_release(struct data_filter *filter)
{
ao2_ref(filter, -1);
}
/*!
* \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 data_filter *data_filter_find(struct ao2_container *parent,
const char *name)
{
int i, olend, orend, globfound;
size_t name_len = strlen(name), glob_len;
struct ao2_iterator iter;
struct data_filter *find_node, *found, *globres;
find_node = data_filter_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);
if (found) {
return found;
}
iter = ao2_iterator_init(parent, 0);
while ((found = ao2_iterator_next(&iter))) {
if (!AST_LIST_EMPTY(&(found->glob_list))) {
i = 0;
globfound = 1;
olend = ast_strlen_zero(AST_LIST_FIRST(&(found->glob_list))->name);
orend = ast_strlen_zero(AST_LIST_LAST(&(found->glob_list))->name);
AST_LIST_TRAVERSE(&(found->glob_list), globres, list) {
if (!*globres->name) {
continue;
}
glob_len = strlen(globres->name);
if (!i && !olend) {
if (strncasecmp(name, globres->name, glob_len)) {
globfound = 0;
break;
}
i += glob_len;
continue;
}
for (globfound = 0; name_len - i >= glob_len; ++i) {
if (!strncasecmp(name + i, globres->name, glob_len)) {
globfound = 1;
i += glob_len;
break;
}
}
if (!globfound) {
break;
}
}
if (globfound && (i == name_len || orend)) {
ao2_iterator_destroy(&iter);
return found;
}
}
ao2_ref(found, -1);
}
ao2_iterator_destroy(&iter);
return NULL;
}
/*!
* \internal
* \brief Add a child to the specified node.
* \param[in] root The root node where to add the child.
* \param[in] name The name of the node to add.
* \note Remember to decrement the ref count after using the returned node.
*/
static struct data_filter *data_filter_add_child(struct ao2_container *root,
char *name)
{
struct data_filter *node;
node = data_filter_find(root, name);
if (node) {
return node;
}
node = data_filter_alloc(name);
if (!node) {
return NULL;
}
ao2_link(root, node);
return node;
}
/*!
* \internal
* \brief Add a node to a filter list from a path
* \param[in] Filter list to add the path onto.
* \param[in] The path to add into the filter list.
* \retval NULL on error.
* \retval non-NULL A tree with the wanted nodes.
*/
static int data_filter_add_nodes(struct ao2_container *root, char *path)
{
struct data_filter *node;
char *savepath, *saveptr, *token, *node_name;
int ret = 0;
if (!path) {
return 0;
}
savepath = ast_strdupa(path);
node_name = next_node_name(&savepath);
if (!node_name) {
return 0;
}
for (token = strtok_r(node_name, "|", &saveptr);
token; token = strtok_r(NULL, "|", &saveptr)) {
node = data_filter_add_child(root, token);
if (!node) {
continue;
}
data_filter_add_nodes(node->children, savepath);
ret = 1;
ao2_ref(node, -1);
}
return ret;
}
/*!
* \internal
* \brief Generate a filter list based on a filter string provided by the API user.
* \param[in] A filter string to create a filter from.
*/
static struct data_filter *data_filter_generate(const char *constfilter)
{
struct data_filter *filter = NULL;
char *strfilter, *token, *saveptr;
int node_added = 0;
if (!constfilter) {
return NULL;
}
strfilter = ast_strdupa(constfilter);
filter = data_filter_alloc("/");
if (!filter) {
return NULL;
}
for (token = strtok_r(strfilter, ",", &saveptr); token;
token = strtok_r(NULL, ",", &saveptr)) {
node_added = data_filter_add_nodes(filter->children, token);
}
if (!node_added) {
ao2_ref(filter, -1);
return NULL;
}
return filter;
}
/*!
* \internal
* \brief Generate all the tree from a specified provider.
* \param[in] query The query executed.
* \param[in] root_provider The provider specified in the path of the query.
* \param[in] parent_node_name The root node name.
* \retval NULL on error.
* \retval non-NULL The generated result tree.
*/
static struct ast_data *data_result_generate_node(const struct ast_data_query *query,
const struct data_provider *root_provider,
const char *parent_node_name,
const struct ast_data_search *search,
const struct data_filter *filter)
{
struct ast_data *generated, *node;
struct ao2_iterator i;
struct data_provider *provider;
struct ast_data_search *search_child = NULL;
struct data_filter *filter_child;
node = data_result_create(parent_node_name);
if (!node) {
ast_log(LOG_ERROR, "Unable to allocate '%s' node\n", parent_node_name);
return NULL;
}
if (root_provider->module) {
ast_module_ref(root_provider->module);
}
/* if this is a terminal node, just run the callback function. */
if (root_provider->handler && root_provider->handler->get) {
node->filter = filter;
root_provider->handler->get(search, node);
if (root_provider->module) {
ast_module_unref(root_provider->module);
}
return node;
}
if (root_provider->module) {
ast_module_unref(root_provider->module);
}
/* if this is not a terminal node, generate every child node. */
i = ao2_iterator_init(root_provider->children, 0);
while ((provider = ao2_iterator_next(&i))) {
filter_child = NULL;
generated = NULL;
/* get the internal search node. */
if (search) {
search_child = data_search_find(search->children, provider->name);
}
/* get the internal filter node. */
if (filter) {
filter_child = data_filter_find(filter->children, provider->name);
}
if (!filter || filter_child) {
/* only generate the internal node, if we have something to
* generate based on the filtering string. */
generated = data_result_generate_node(query, provider,
provider->name,
search_child, filter_child);
}
/* decrement the refcount of the internal search node. */
if (search_child) {
ao2_ref(search_child, -1);
}
/* decrement the refcount of the internal filter node. */
if (filter_child) {
ao2_ref(filter_child, -1);
}
if (generated) {
data_result_add_child(node, generated);
ao2_ref(generated, -1);
}
ao2_ref(provider, -1);
}
ao2_iterator_destroy(&i);
return node;
}
/*!
* \internal
* \brief Generate a result tree based on a query.
* \param[in] query The complete query structure.
* \param[in] search_path The path to retrieve.
* \retval NULL on error.
* \retval non-NULL The generated data result.
*/
static struct ast_data *data_result_generate(const struct ast_data_query *query,
const char *search_path)
{
char *node_name, *tmp_path;
struct data_provider *provider_child, *tmp_provider_child;
struct ast_data *result, *result_filtered;
struct ast_data_search *search = NULL, *search_child = NULL;
struct data_filter *filter = NULL, *filter_child = NULL;
if (!search_path) {
/* generate all the trees?. */
return NULL;
}
tmp_path = ast_strdupa(search_path);
/* start searching the root node name */
node_name = next_node_name(&tmp_path);
if (!node_name) {
return NULL;
}
provider_child = data_provider_find(root_data.container, node_name, NULL);
/* continue with the rest of the path. */
while (provider_child) {
node_name = next_node_name(&tmp_path);
if (!node_name) {