data.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief Data retrieval API.
  19. *
  20. * \author Brett Bryant <brettbryant@gmail.com>
  21. * \author Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
  22. */
  23. /*** MODULEINFO
  24. <support_level>core</support_level>
  25. ***/
  26. #include "asterisk.h"
  27. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  28. #include "asterisk/_private.h"
  29. #include <regex.h>
  30. #include "asterisk/module.h"
  31. #include "asterisk/utils.h"
  32. #include "asterisk/lock.h"
  33. #include "asterisk/data.h"
  34. #include "asterisk/astobj2.h"
  35. #include "asterisk/xml.h"
  36. #include "asterisk/cli.h"
  37. #include "asterisk/term.h"
  38. #include "asterisk/manager.h"
  39. #include "asterisk/test.h"
  40. #include "asterisk/frame.h"
  41. #include "asterisk/codec.h"
  42. /*** DOCUMENTATION
  43. <manager name="DataGet" language="en_US">
  44. <synopsis>
  45. Retrieve the data api tree.
  46. </synopsis>
  47. <syntax>
  48. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  49. <parameter name="Path" required="true" />
  50. <parameter name="Search" />
  51. <parameter name="Filter" />
  52. </syntax>
  53. <description>
  54. <para>Retrieve the data api tree.</para>
  55. </description>
  56. </manager>
  57. ***/
  58. #define NUM_DATA_NODE_BUCKETS 59
  59. #define NUM_DATA_RESULT_BUCKETS 59
  60. #define NUM_DATA_SEARCH_BUCKETS 59
  61. #define NUM_DATA_FILTER_BUCKETS 59
  62. /*! \brief The last compatible version. */
  63. static const uint32_t latest_handler_compatible_version = 0;
  64. /*! \brief The last compatible version. */
  65. static const uint32_t latest_query_compatible_version = 0;
  66. /*! \brief Current handler structure version. */
  67. static const uint32_t current_handler_version = AST_DATA_HANDLER_VERSION;
  68. /*! \brief Current query structure version. */
  69. static const uint32_t current_query_version = AST_DATA_QUERY_VERSION;
  70. /*! \brief The data tree to be returned by the callbacks and
  71. managed by functions local to this file. */
  72. struct ast_data {
  73. enum ast_data_type type;
  74. /*! \brief The node content. */
  75. union {
  76. int32_t sint;
  77. uint32_t uint;
  78. double dbl;
  79. unsigned int boolean;
  80. char *str;
  81. char character;
  82. struct in_addr ipaddr;
  83. void *ptr;
  84. } payload;
  85. /*! \brief The filter node that depends on the current node,
  86. * this is used only when creating the result tree. */
  87. const struct data_filter *filter;
  88. /*! \brief The list of nodes inside this node. */
  89. struct ao2_container *children;
  90. /*! \brief The name of the node. */
  91. char name[0];
  92. };
  93. /*! \brief Type of comparisons allow in the search string. */
  94. enum data_search_comparison {
  95. DATA_CMP_UNKNOWN,
  96. DATA_CMP_EQ, /* = */
  97. DATA_CMP_NEQ, /* != */
  98. DATA_CMP_GT, /* > */
  99. DATA_CMP_GE, /* >= */
  100. DATA_CMP_LT, /* < */
  101. DATA_CMP_LE /* <= */
  102. };
  103. /*! \brief The list of nodes with their search requirement. */
  104. struct ast_data_search {
  105. /*! \brief The value of the comparison. */
  106. char *value;
  107. /*! \brief The type of comparison. */
  108. enum data_search_comparison cmp_type;
  109. /*! \brief reference another node. */
  110. struct ao2_container *children;
  111. /*! \brief The name of the node we are trying to compare. */
  112. char name[0];
  113. };
  114. struct data_filter;
  115. /*! \brief The filter node. */
  116. struct data_filter {
  117. /*! \brief node childrens. */
  118. struct ao2_container *children;
  119. /*! \brief glob list */
  120. AST_LIST_HEAD_NOLOCK(glob_list_t, data_filter) glob_list;
  121. /*! \brief glob list entry */
  122. AST_LIST_ENTRY(data_filter) list;
  123. /*! \brief node name. */
  124. char name[0];
  125. };
  126. /*! \brief A data container node pointing to the registered handler. */
  127. struct data_provider {
  128. /*! \brief node content handler. */
  129. const struct ast_data_handler *handler;
  130. /*! \brief Module providing this handler. */
  131. struct ast_module *module;
  132. /*! \brief children nodes. */
  133. struct ao2_container *children;
  134. /*! \brief Who registered this node. */
  135. const char *registrar;
  136. /*! \brief Node name. */
  137. char name[0];
  138. };
  139. /*! \brief This structure is used by the iterator. */
  140. struct ast_data_iterator {
  141. /*! \brief The internal iterator. */
  142. struct ao2_iterator internal_iterator;
  143. /*! \brief The last returned node. */
  144. struct ast_data *last;
  145. /*! \brief The iterator pattern. */
  146. const char *pattern;
  147. /*! \brief The compiled patter. */
  148. regex_t regex_pattern;
  149. /*! \brief is a regular expression. */
  150. unsigned int is_pattern:1;
  151. };
  152. struct {
  153. /*! \brief The asterisk data main content structure. */
  154. struct ao2_container *container;
  155. /*! \brief asterisk data locking mechanism. */
  156. ast_rwlock_t lock;
  157. } root_data;
  158. static void __data_result_print_cli(int fd, const struct ast_data *root, uint32_t depth);
  159. /*!
  160. * \internal
  161. * \brief Common string hash function.
  162. * \see ast_data_init
  163. */
  164. static int data_provider_hash(const void *obj, const int flags)
  165. {
  166. const struct data_provider *node = obj;
  167. return ast_str_case_hash(node->name);
  168. }
  169. /*!
  170. * \internal
  171. * \brief Compare two data_provider's.
  172. * \see ast_data_init
  173. */
  174. static int data_provider_cmp(void *obj1, void *obj2, int flags)
  175. {
  176. struct data_provider *node1 = obj1, *node2 = obj2;
  177. return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
  178. }
  179. /*!
  180. * \internal
  181. * \brief Common string hash function for data nodes
  182. */
  183. static int data_result_hash(const void *obj, const int flags)
  184. {
  185. const struct ast_data *node = obj;
  186. return ast_str_hash(node->name);
  187. }
  188. /*!
  189. * \internal
  190. * \brief Common string comparison function
  191. */
  192. static int data_result_cmp(void *obj, void *arg, int flags)
  193. {
  194. struct ast_data *node1 = obj, *node2 = arg;
  195. return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
  196. }
  197. /*!
  198. * \internal
  199. * \brief Lock the data registered handlers structure for writing.
  200. * \see data_unlock
  201. */
  202. #define data_write_lock() ast_rwlock_wrlock(&root_data.lock)
  203. /*!
  204. * \internal
  205. * \brief Lock the data registered handlers structure for reading.
  206. * \see data_unlock
  207. */
  208. #define data_read_lock() ast_rwlock_rdlock(&root_data.lock)
  209. /*!
  210. * \internal
  211. * \brief Unlock the data registered handlers structure.
  212. */
  213. #define data_unlock() ast_rwlock_unlock(&root_data.lock)
  214. /*!
  215. * \internal
  216. * \brief Check if a version is compatible with the current core.
  217. * \param[in] structure_version The current structure version.
  218. * \param[in] latest_compatible The latest compatible version.
  219. * \param[in] current The current Data API version.
  220. * \retval 1 If the module is compatible.
  221. * \retval 0 If the module is NOT compatible.
  222. */
  223. static int data_structure_compatible(int structure_version, uint32_t latest_compatible,
  224. uint32_t current)
  225. {
  226. if (structure_version >= latest_compatible && structure_version <= current) {
  227. return 1;
  228. }
  229. ast_log(LOG_ERROR, "A module is not compatible with the"
  230. "current data api version\n");
  231. return 0;
  232. }
  233. /*!
  234. * \internal
  235. * \brief Get the next node name in a path (/node1/node2)
  236. * Avoid null nodes like //node1//node2/node3.
  237. * \param[in] path The path where we are going to search for the next node name.
  238. * \retval The next node name we found inside the given path.
  239. * \retval NULL if there are no more node names.
  240. */
  241. static char *next_node_name(char **path)
  242. {
  243. char *res;
  244. do {
  245. res = strsep(path, "/");
  246. } while (res && ast_strlen_zero(res));
  247. return res;
  248. }
  249. /*!
  250. * \internal
  251. * \brief Release the memory allocated by a call to ao2_alloc.
  252. */
  253. static void data_provider_destructor(void *obj)
  254. {
  255. struct data_provider *provider = obj;
  256. ao2_ref(provider->children, -1);
  257. }
  258. /*!
  259. * \internal
  260. * \brief Create a new data node.
  261. * \param[in] name The name of the node we are going to create.
  262. * \param[in] handler The handler registered for this node.
  263. * \param[in] registrar The name of the registrar.
  264. * \retval NULL on error.
  265. * \retval The allocated data node structure.
  266. */
  267. static struct data_provider *data_provider_new(const char *name,
  268. const struct ast_data_handler *handler, const char *registrar)
  269. {
  270. struct data_provider *node;
  271. size_t namelen;
  272. namelen = strlen(name) + 1;
  273. node = ao2_alloc(sizeof(*node) + namelen, data_provider_destructor);
  274. if (!node) {
  275. return NULL;
  276. }
  277. node->handler = handler;
  278. node->registrar = registrar;
  279. strcpy(node->name, name);
  280. /* initialize the childrens container. */
  281. node->children = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  282. NUM_DATA_NODE_BUCKETS, data_provider_hash, NULL, data_provider_cmp);
  283. if (!node->children) {
  284. ao2_ref(node, -1);
  285. return NULL;
  286. }
  287. return node;
  288. }
  289. /*!
  290. * \internal
  291. * \brief Add a child node named 'name' to the 'parent' node.
  292. * \param[in] parent Where to add the child node.
  293. * \param[in] name The name of the child node.
  294. * \param[in] handler The handler structure.
  295. * \param[in] registrar Who registered this node.
  296. * \retval NULL on error.
  297. * \retval A newly allocated child in parent.
  298. */
  299. static struct data_provider *data_provider_add_child(struct ao2_container *parent,
  300. const char *name, const struct ast_data_handler *handler, const char *registrar)
  301. {
  302. struct data_provider *child;
  303. child = data_provider_new(name, handler, registrar);
  304. if (!child) {
  305. return NULL;
  306. }
  307. ao2_link(parent, child);
  308. return child;
  309. }
  310. /*!
  311. * \internal
  312. * \brief Find a child node, based on his name.
  313. * \param[in] parent Where to find the node.
  314. * \param[in] name The node name to find.
  315. * \param[in] registrar Also check if the node was being used by this registrar.
  316. * \retval NULL if a node wasn't found.
  317. * \retval The node found.
  318. * \note Remember to decrement the ref count of the returned node after using it.
  319. */
  320. static struct data_provider *data_provider_find(struct ao2_container *parent,
  321. const char *name, const char *registrar)
  322. {
  323. struct data_provider *find_node, *found;
  324. /* XXX avoid allocating a new data node for searching... */
  325. find_node = data_provider_new(name, NULL, NULL);
  326. if (!find_node) {
  327. return NULL;
  328. }
  329. found = ao2_find(parent, find_node, OBJ_POINTER);
  330. /* free the created node used for searching. */
  331. ao2_ref(find_node, -1);
  332. if (found && found->registrar && registrar) {
  333. if (strcmp(found->registrar, registrar)) {
  334. /* if the name doesn't match, do not return this node. */
  335. ast_debug(1, "Registrar doesn't match, node was registered"
  336. " by '%s' and we are searching for '%s'\n",
  337. found->registrar, registrar);
  338. ao2_ref(found, -1);
  339. return NULL;
  340. }
  341. }
  342. return found;
  343. }
  344. /*!
  345. * \internal
  346. * \brief Release a group of nodes.
  347. * \param[in] parent The parent node.
  348. * \param[in] path The path of nodes to release.
  349. * \param[in] registrar Who registered this node.
  350. * \retval <0 on error.
  351. * \retval 0 on success.
  352. * \see data_provider_create
  353. */
  354. static int data_provider_release(struct ao2_container *parent, const char *path,
  355. const char *registrar)
  356. {
  357. char *node_name, *rpath;
  358. struct data_provider *child;
  359. int ret = 0;
  360. rpath = ast_strdupa(path);
  361. node_name = next_node_name(&rpath);
  362. if (!node_name) {
  363. return -1;
  364. }
  365. child = data_provider_find(parent, node_name, registrar);
  366. if (!child) {
  367. return -1;
  368. }
  369. /* if this is not a terminal node. */
  370. if (!child->handler && rpath) {
  371. ret = data_provider_release(child->children, rpath, registrar);
  372. }
  373. /* if this node is empty, unlink it. */
  374. if (!ret && !ao2_container_count(child->children)) {
  375. ao2_unlink(parent, child);
  376. }
  377. ao2_ref(child, -1);
  378. return ret;
  379. }
  380. /*!
  381. * \internal
  382. * \brief Release every node registered by 'registrar'.
  383. * \param[in] parent The parent node.
  384. * \param[in] registrar
  385. * \see __ast_data_unregister
  386. */
  387. static void data_provider_release_all(struct ao2_container *parent,
  388. const char *registrar)
  389. {
  390. struct ao2_iterator i;
  391. struct data_provider *node;
  392. i = ao2_iterator_init(parent, 0);
  393. while ((node = ao2_iterator_next(&i))) {
  394. if (!node->handler) {
  395. /* this is a non-terminal node, go inside it. */
  396. data_provider_release_all(node->children, registrar);
  397. if (!ao2_container_count(node->children)) {
  398. /* if this node was left empty, unlink it. */
  399. ao2_unlink(parent, node);
  400. }
  401. } else {
  402. if (!strcmp(node->registrar, registrar)) {
  403. /* if the registrars match, release it! */
  404. ao2_unlink(parent, node);
  405. }
  406. }
  407. ao2_ref(node, -1);
  408. }
  409. ao2_iterator_destroy(&i);
  410. }
  411. /*!
  412. * \internal
  413. * \brief Create the middle nodes for the specified path (asterisk/testnode1/childnode)
  414. * \param[in] parent Where to add the middle nodes structure.
  415. * \param[in] path The path of nodes to add.
  416. * \param[in] registrar Who is trying to create this node provider.
  417. * \retval NULL on error.
  418. * \retval The created node.
  419. * \see data_provider_release
  420. */
  421. static struct data_provider *data_provider_create(struct ao2_container *parent,
  422. const char *path, const char *registrar)
  423. {
  424. char *rpath, *node_name;
  425. struct data_provider *child, *ret = NULL;
  426. rpath = ast_strdupa(path);
  427. node_name = next_node_name(&rpath);
  428. if (!node_name) {
  429. /* no more nodes to create. */
  430. return NULL;
  431. }
  432. child = data_provider_find(parent, node_name, NULL);
  433. if (!child) {
  434. /* nodes without handler are non-terminal nodes. */
  435. child = data_provider_add_child(parent, node_name, NULL, registrar);
  436. }
  437. if (rpath) {
  438. ret = data_provider_create(child->children, rpath, registrar);
  439. if (ret) {
  440. ao2_ref(child, -1);
  441. }
  442. }
  443. return ret ? ret : child;
  444. }
  445. int __ast_data_register(const char *path, const struct ast_data_handler *handler,
  446. const char *registrar, struct ast_module *mod)
  447. {
  448. struct data_provider *node;
  449. if (!path) {
  450. return -1;
  451. }
  452. /* check if the handler structure is compatible. */
  453. if (!data_structure_compatible(handler->version,
  454. latest_handler_compatible_version,
  455. current_handler_version)) {
  456. return -1;
  457. }
  458. /* create the node structure for the registered handler. */
  459. data_write_lock();
  460. node = data_provider_create(root_data.container, path, registrar);
  461. if (!node) {
  462. ast_log(LOG_ERROR, "Unable to create the specified path (%s) "
  463. "for '%s'.\n", path, registrar);
  464. data_unlock();
  465. return -1;
  466. }
  467. if (ao2_container_count(node->children) || node->handler) {
  468. ast_log(LOG_ERROR, "The node '%s' was already registered. "
  469. "We were unable to register '%s' for registrar '%s'.\n",
  470. node->name, path, registrar);
  471. ao2_ref(node, -1);
  472. data_unlock();
  473. return -1;
  474. }
  475. /* add handler to that node. */
  476. node->handler = handler;
  477. node->module = mod;
  478. ao2_ref(node, -1);
  479. data_unlock();
  480. return 0;
  481. }
  482. int __ast_data_register_multiple(const struct ast_data_entry *data_entries,
  483. size_t entries, const char *registrar, struct ast_module *mod)
  484. {
  485. int i, res;
  486. for (i = 0; i < entries; i++) {
  487. res = __ast_data_register(data_entries[i].path, data_entries[i].handler,
  488. registrar, mod);
  489. if (res) {
  490. /* unregister all the already registered nodes, and make
  491. * this an atomic action. */
  492. while ((--i) >= 0) {
  493. __ast_data_unregister(data_entries[i].path, registrar);
  494. }
  495. return -1;
  496. }
  497. }
  498. return 0;
  499. }
  500. int __ast_data_unregister(const char *path, const char *registrar)
  501. {
  502. int ret = 0;
  503. data_write_lock();
  504. if (path) {
  505. ret = data_provider_release(root_data.container, path, registrar);
  506. } else {
  507. data_provider_release_all(root_data.container, registrar);
  508. }
  509. data_unlock();
  510. if (path && ret) {
  511. ast_log(LOG_ERROR, "Unable to unregister '%s' for '%s'\n",
  512. path, registrar);
  513. }
  514. return ret;
  515. }
  516. /*!
  517. * \internal
  518. * \brief Is a char used to specify a comparison?
  519. * \param[in] a Character to evaluate.
  520. * \retval 1 It is a char used to specify a comparison.
  521. * \retval 0 It is NOT a char used to specify a comparison.
  522. */
  523. static int data_search_comparison_char(char a)
  524. {
  525. switch (a) {
  526. case '!':
  527. case '=':
  528. case '<':
  529. case '>':
  530. return 1;
  531. }
  532. return 0;
  533. }
  534. /*!
  535. * \internal
  536. * \brief Get the type of comparison.
  537. */
  538. static enum data_search_comparison data_search_comparison_type(const char *comparison)
  539. {
  540. if (!strcmp(comparison, "=")) {
  541. return DATA_CMP_EQ;
  542. } else if (!strcmp(comparison, "!=")) {
  543. return DATA_CMP_NEQ;
  544. } else if (!strcmp(comparison, "<")) {
  545. return DATA_CMP_LT;
  546. } else if (!strcmp(comparison, ">")) {
  547. return DATA_CMP_GT;
  548. } else if (!strcmp(comparison, "<=")) {
  549. return DATA_CMP_LE;
  550. } else if (!strcmp(comparison, ">=")) {
  551. return DATA_CMP_GE;
  552. }
  553. return DATA_CMP_UNKNOWN;
  554. }
  555. /*!
  556. * \internal
  557. * \brief Common string hash function for data nodes
  558. */
  559. static int data_search_hash(const void *obj, const int flags)
  560. {
  561. const struct ast_data_search *node = obj;
  562. return ast_str_hash(node->name);
  563. }
  564. /*!
  565. * \internal
  566. * \brief Common string comparison function
  567. */
  568. static int data_search_cmp(void *obj, void *arg, int flags)
  569. {
  570. struct ast_data_search *node1 = obj, *node2 = arg;
  571. return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
  572. }
  573. /*!
  574. * \internal
  575. * \brief Destroy the ao2 search node.
  576. */
  577. static void data_search_destructor(void *obj)
  578. {
  579. struct ast_data_search *node = obj;
  580. if (node->value) {
  581. ast_free(node->value);
  582. }
  583. ao2_ref(node->children, -1);
  584. }
  585. /*!
  586. * \internal
  587. * \brief Allocate a search node.
  588. * \retval NULL on error.
  589. * \retval non-NULL The allocated search node structure.
  590. */
  591. static struct ast_data_search *data_search_alloc(const char *name)
  592. {
  593. struct ast_data_search *res;
  594. size_t name_len = strlen(name) + 1;
  595. res = ao2_alloc(sizeof(*res) + name_len, data_search_destructor);
  596. if (!res) {
  597. return NULL;
  598. }
  599. res->children = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  600. NUM_DATA_SEARCH_BUCKETS, data_search_hash, NULL, data_search_cmp);
  601. if (!res->children) {
  602. ao2_ref(res, -1);
  603. return NULL;
  604. }
  605. strcpy(res->name, name);
  606. return res;
  607. }
  608. /*!
  609. * \internal
  610. * \brief Find a child node, based on his name.
  611. * \param[in] parent Where to find the node.
  612. * \param[in] name The node name to find.
  613. * \retval NULL if a node wasn't found.
  614. * \retval The node found.
  615. * \note Remember to decrement the ref count of the returned node after using it.
  616. */
  617. static struct ast_data_search *data_search_find(struct ao2_container *parent,
  618. const char *name)
  619. {
  620. struct ast_data_search *find_node, *found;
  621. find_node = data_search_alloc(name);
  622. if (!find_node) {
  623. return NULL;
  624. }
  625. found = ao2_find(parent, find_node, OBJ_POINTER);
  626. /* free the created node used for searching. */
  627. ao2_ref(find_node, -1);
  628. return found;
  629. }
  630. /*!
  631. * \internal
  632. * \brief Add a child node named 'name' to the 'parent' node.
  633. * \param[in] parent Where to add the child node.
  634. * \param[in] name The name of the child node.
  635. * \retval NULL on error.
  636. * \retval A newly allocated child in parent.
  637. */
  638. static struct ast_data_search *data_search_add_child(struct ao2_container *parent,
  639. const char *name)
  640. {
  641. struct ast_data_search *child;
  642. child = data_search_alloc(name);
  643. if (!child) {
  644. return NULL;
  645. }
  646. ao2_link(parent, child);
  647. return child;
  648. }
  649. /*!
  650. * \internal
  651. * \brief Create the middle nodes for the specified path (asterisk/testnode1/childnode)
  652. * \param[in] parent Where to add the middle nodes structure.
  653. * \param[in] path The path of nodes to add.
  654. * \retval NULL on error.
  655. * \retval The created node.
  656. */
  657. static struct ast_data_search *data_search_create(struct ao2_container *parent,
  658. const char *path)
  659. {
  660. char *rpath, *node_name;
  661. struct ast_data_search *child = NULL;
  662. struct ao2_container *current = parent;
  663. rpath = ast_strdupa(path);
  664. node_name = next_node_name(&rpath);
  665. while (node_name) {
  666. child = data_search_find(current, node_name);
  667. if (!child) {
  668. child = data_search_add_child(current, node_name);
  669. }
  670. ao2_ref(child, -1);
  671. current = child->children;
  672. node_name = next_node_name(&rpath);
  673. }
  674. return child;
  675. }
  676. /*!
  677. * \internal
  678. * \brief Allocate a tree with the search string parsed.
  679. * \param[in] search_string The search string.
  680. * \retval NULL on error.
  681. * \retval non-NULL A dynamically allocated search tree.
  682. */
  683. static struct ast_data_search *data_search_generate(const char *search_string)
  684. {
  685. struct ast_str *name, *value, *comparison;
  686. char *elements, *search_string_dup, *saveptr;
  687. int i;
  688. struct ast_data_search *root, *child;
  689. enum data_search_comparison cmp_type;
  690. size_t search_string_len;
  691. if (!search_string) {
  692. ast_log(LOG_ERROR, "You must pass a valid search string.\n");
  693. return NULL;
  694. }
  695. search_string_len = strlen(search_string);
  696. name = ast_str_create(search_string_len);
  697. if (!name) {
  698. return NULL;
  699. }
  700. value = ast_str_create(search_string_len);
  701. if (!value) {
  702. ast_free(name);
  703. return NULL;
  704. }
  705. comparison = ast_str_create(search_string_len);
  706. if (!comparison) {
  707. ast_free(name);
  708. ast_free(value);
  709. return NULL;
  710. }
  711. search_string_dup = ast_strdupa(search_string);
  712. /* Create the root node (just used as a container) */
  713. root = data_search_alloc("/");
  714. if (!root) {
  715. ast_free(name);
  716. ast_free(value);
  717. ast_free(comparison);
  718. return NULL;
  719. }
  720. for (elements = strtok_r(search_string_dup, ",", &saveptr); elements;
  721. elements = strtok_r(NULL, ",", &saveptr)) {
  722. /* Parse the name */
  723. ast_str_reset(name);
  724. for (i = 0; !data_search_comparison_char(elements[i]) &&
  725. elements[i]; i++) {
  726. ast_str_append(&name, 0, "%c", elements[i]);
  727. }
  728. /* check if the syntax is ok. */
  729. if (!data_search_comparison_char(elements[i])) {
  730. /* if this is the end of the string, then this is
  731. * an error! */
  732. ast_log(LOG_ERROR, "Invalid search string!\n");
  733. continue;
  734. }
  735. /* parse the comparison string. */
  736. ast_str_reset(comparison);
  737. for (; data_search_comparison_char(elements[i]) && elements[i]; i++) {
  738. ast_str_append(&comparison, 0, "%c", elements[i]);
  739. }
  740. /* parse the value string. */
  741. ast_str_reset(value);
  742. for (; elements[i]; i++) {
  743. ast_str_append(&value, 0, "%c", elements[i]);
  744. }
  745. cmp_type = data_search_comparison_type(ast_str_buffer(comparison));
  746. if (cmp_type == DATA_CMP_UNKNOWN) {
  747. ast_log(LOG_ERROR, "Invalid comparison '%s'\n",
  748. ast_str_buffer(comparison));
  749. continue;
  750. }
  751. /* add this node to the tree. */
  752. child = data_search_create(root->children, ast_str_buffer(name));
  753. if (child) {
  754. child->cmp_type = cmp_type;
  755. child->value = ast_strdup(ast_str_buffer(value));
  756. }
  757. }
  758. ast_free(name);
  759. ast_free(value);
  760. ast_free(comparison);
  761. return root;
  762. }
  763. /*!
  764. * \internal
  765. * \brief Release the allocated memory for the search tree.
  766. * \param[in] search The search tree root node.
  767. */
  768. static void data_search_release(struct ast_data_search *search)
  769. {
  770. ao2_ref(search, -1);
  771. }
  772. /*!
  773. * \internal
  774. * \brief Based on the kind of comparison and the result in cmpval, return
  775. * if it matches.
  776. * \param[in] cmpval A result returned by a strcmp() for example.
  777. * \param[in] comparison_type The kind of comparison (<,>,=,!=,...)
  778. * \retval 1 If the comparison doesn't match.
  779. * \retval 0 If the comparison matches.
  780. */
  781. static inline int data_search_comparison_result(int cmpval,
  782. enum data_search_comparison comparison_type)
  783. {
  784. switch (comparison_type) {
  785. case DATA_CMP_GE:
  786. if (cmpval >= 0) {
  787. return 0;
  788. }
  789. break;
  790. case DATA_CMP_LE:
  791. if (cmpval <= 0) {
  792. return 0;
  793. }
  794. break;
  795. case DATA_CMP_EQ:
  796. if (cmpval == 0) {
  797. return 0;
  798. }
  799. break;
  800. case DATA_CMP_NEQ:
  801. if (cmpval != 0) {
  802. return 0;
  803. }
  804. break;
  805. case DATA_CMP_LT:
  806. if (cmpval < 0) {
  807. return 0;
  808. }
  809. break;
  810. case DATA_CMP_GT:
  811. if (cmpval > 0) {
  812. return 0;
  813. }
  814. break;
  815. case DATA_CMP_UNKNOWN:
  816. break;
  817. }
  818. return 1;
  819. }
  820. /*!
  821. * \internal
  822. * \brief Get an internal node, from the search tree.
  823. * \param[in] node A node container.
  824. * \param[in] path The path to the needed internal node.
  825. * \retval NULL if the internal node is not found.
  826. * \retval non-NULL the internal node with path 'path'.
  827. */
  828. static struct ast_data_search *data_search_get_node(const struct ast_data_search *node,
  829. const char *path)
  830. {
  831. char *savepath, *node_name;
  832. struct ast_data_search *child, *current = (struct ast_data_search *) node;
  833. if (!node) {
  834. return NULL;
  835. }
  836. savepath = ast_strdupa(path);
  837. node_name = next_node_name(&savepath);
  838. while (node_name) {
  839. child = data_search_find(current->children, node_name);
  840. if (current != node) {
  841. ao2_ref(current, -1);
  842. }
  843. if (!child) {
  844. return NULL;
  845. };
  846. current = child;
  847. node_name = next_node_name(&savepath);
  848. }
  849. return current;
  850. }
  851. /*!
  852. * \internal
  853. * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
  854. * current string value.
  855. * .search = "somename=somestring"
  856. * name = "somename"
  857. * value is the current value of something and will be evaluated against "somestring".
  858. * \param[in] root The root node pointer of the search tree.
  859. * \param[in] name The name of the specific.
  860. * \param[in] value The value to compare.
  861. * \returns The strcmp return value.
  862. */
  863. static int data_search_cmp_string(const struct ast_data_search *root, const char *name,
  864. char *value)
  865. {
  866. struct ast_data_search *child;
  867. enum data_search_comparison cmp_type;
  868. int ret;
  869. child = data_search_get_node(root, name);
  870. if (!child) {
  871. return 0;
  872. }
  873. ret = strcmp(value, child->value);
  874. cmp_type = child->cmp_type;
  875. ao2_ref(child, -1);
  876. return data_search_comparison_result(ret, cmp_type);
  877. }
  878. /*!
  879. * \internal
  880. * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
  881. * current pointer address value.
  882. * .search = "something=0x32323232"
  883. * name = "something"
  884. * value is the current value of something and will be evaluated against "0x32323232".
  885. * \param[in] root The root node pointer of the search tree.
  886. * \param[in] name The name of the specific.
  887. * \param[in] ptr The pointer address to compare.
  888. * \returns The (value - current_value) result.
  889. */
  890. static int data_search_cmp_ptr(const struct ast_data_search *root, const char *name,
  891. void *ptr)
  892. {
  893. struct ast_data_search *child;
  894. enum data_search_comparison cmp_type;
  895. void *node_ptr;
  896. child = data_search_get_node(root, name);
  897. if (!child) {
  898. return 0;
  899. }
  900. cmp_type = child->cmp_type;
  901. if (sscanf(child->value, "%p", &node_ptr) <= 0) {
  902. ao2_ref(child, -1);
  903. return 1;
  904. }
  905. ao2_ref(child, -1);
  906. return data_search_comparison_result((node_ptr - ptr), cmp_type);
  907. }
  908. /*!
  909. * \internal
  910. * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
  911. * current ipv4 address value.
  912. * .search = "something=192.168.2.2"
  913. * name = "something"
  914. * value is the current value of something and will be evaluated against "192.168.2.2".
  915. * \param[in] root The root node pointer of the search tree.
  916. * \param[in] name The name of the specific.
  917. * \param[in] addr The ipv4 address value to compare.
  918. * \returns The (value - current_value) result.
  919. */
  920. static int data_search_cmp_ipaddr(const struct ast_data_search *root, const char *name,
  921. struct in_addr addr)
  922. {
  923. struct ast_data_search *child;
  924. enum data_search_comparison cmp_type;
  925. struct in_addr node_addr;
  926. child = data_search_get_node(root, name);
  927. if (!child) {
  928. return 0;
  929. }
  930. cmp_type = child->cmp_type;
  931. inet_aton(child->value, &node_addr);
  932. ao2_ref(child, -1);
  933. return data_search_comparison_result((node_addr.s_addr - addr.s_addr), cmp_type);
  934. }
  935. /*!
  936. * \internal
  937. * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
  938. * current boolean value.
  939. * .search = "something=true"
  940. * name = "something"
  941. * value is the current value of something and will be evaluated against "true".
  942. * \param[in] root The root node pointer of the search tree.
  943. * \param[in] name The name of the specific.
  944. * \param[in] value The boolean value to compare.
  945. * \returns The (value - current_value) result.
  946. */
  947. static int data_search_cmp_bool(const struct ast_data_search *root, const char *name,
  948. unsigned int value)
  949. {
  950. struct ast_data_search *child;
  951. unsigned int node_value;
  952. enum data_search_comparison cmp_type;
  953. child = data_search_get_node(root, name);
  954. if (!child) {
  955. return 0;
  956. }
  957. node_value = abs(ast_true(child->value));
  958. cmp_type = child->cmp_type;
  959. ao2_ref(child, -1);
  960. return data_search_comparison_result(value - node_value, cmp_type);
  961. }
  962. /*!
  963. * \internal
  964. * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
  965. * current double value.
  966. * .search = "something=222"
  967. * name = "something"
  968. * value is the current value of something and will be evaluated against "222".
  969. * \param[in] root The root node pointer of the search tree.
  970. * \param[in] name The name of the specific.
  971. * \param[in] value The double value to compare.
  972. * \returns The (value - current_value) result.
  973. */
  974. static int data_search_cmp_dbl(const struct ast_data_search *root, const char *name,
  975. double value)
  976. {
  977. struct ast_data_search *child;
  978. double node_value;
  979. enum data_search_comparison cmp_type;
  980. child = data_search_get_node(root, name);
  981. if (!child) {
  982. return 0;
  983. }
  984. node_value = strtod(child->value, NULL);
  985. cmp_type = child->cmp_type;
  986. ao2_ref(child, -1);
  987. return data_search_comparison_result(value - node_value, cmp_type);
  988. }
  989. /*!
  990. * \internal
  991. * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
  992. * current unsigned integer value.
  993. * .search = "something=10"
  994. * name = "something"
  995. * value is the current value of something and will be evaluated against "10".
  996. * \param[in] root The root node pointer of the search tree.
  997. * \param[in] name The name of the specific.
  998. * \param[in] value The unsigned value to compare.
  999. * \returns The strcmp return value.
  1000. */
  1001. static int data_search_cmp_uint(const struct ast_data_search *root, const char *name,
  1002. unsigned int value)
  1003. {
  1004. struct ast_data_search *child;
  1005. unsigned int node_value;
  1006. enum data_search_comparison cmp_type;
  1007. child = data_search_get_node(root, name);
  1008. if (!child) {
  1009. return 0;
  1010. }
  1011. node_value = atoi(child->value);
  1012. cmp_type = child->cmp_type;
  1013. ao2_ref(child, -1);
  1014. return data_search_comparison_result(value - node_value, cmp_type);
  1015. }
  1016. /*!
  1017. * \internal
  1018. * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
  1019. * current signed integer value.
  1020. * .search = "something=10"
  1021. * name = "something"
  1022. * value is the current value of something and will be evaluated against "10".
  1023. * \param[in] root The root node pointer of the search tree.
  1024. * \param[in] name The name of the specific.
  1025. * \param[in] value The value to compare.
  1026. * \returns The strcmp return value.
  1027. */
  1028. static int data_search_cmp_int(const struct ast_data_search *root, const char *name,
  1029. int value)
  1030. {
  1031. struct ast_data_search *child;
  1032. int node_value;
  1033. enum data_search_comparison cmp_type;
  1034. child = data_search_get_node(root, name);
  1035. if (!child) {
  1036. return 0;
  1037. }
  1038. node_value = atoi(child->value);
  1039. cmp_type = child->cmp_type;
  1040. ao2_ref(child, -1);
  1041. return data_search_comparison_result(value - node_value, cmp_type);
  1042. }
  1043. /*!
  1044. * \internal
  1045. * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
  1046. * current character value.
  1047. * .search = "something=c"
  1048. * name = "something"
  1049. * value is the current value of something and will be evaluated against "c".
  1050. * \param[in] root The root node pointer of the search tree.
  1051. * \param[in] name The name of the specific.
  1052. * \param[in] value The boolean value to compare.
  1053. * \returns The (value - current_value) result.
  1054. */
  1055. static int data_search_cmp_char(const struct ast_data_search *root, const char *name,
  1056. char value)
  1057. {
  1058. struct ast_data_search *child;
  1059. char node_value;
  1060. enum data_search_comparison cmp_type;
  1061. child = data_search_get_node(root, name);
  1062. if (!child) {
  1063. return 0;
  1064. }
  1065. node_value = *(child->value);
  1066. cmp_type = child->cmp_type;
  1067. ao2_ref(child, -1);
  1068. return data_search_comparison_result(value - node_value, cmp_type);
  1069. }
  1070. /*!
  1071. * \internal
  1072. * \brief Get the member pointer, from a mapping structure, based on its name.
  1073. * \XXX We will need to improve performance here!!.
  1074. * \retval <0 if the member was not found.
  1075. * \retval >=0 The member position in the mapping structure.
  1076. */
  1077. static inline int data_search_mapping_find(const struct ast_data_mapping_structure *map,
  1078. size_t mapping_len,
  1079. const char *member_name)
  1080. {
  1081. int i;
  1082. for (i = 0; i < mapping_len; i++) {
  1083. if (!strcmp(map[i].name, member_name)) {
  1084. return i;
  1085. }
  1086. }
  1087. return -1;
  1088. }
  1089. int __ast_data_search_cmp_structure(const struct ast_data_search *search,
  1090. const struct ast_data_mapping_structure *mapping, size_t mapping_len,
  1091. void *structure, const char *structure_name)
  1092. {
  1093. struct ao2_iterator i;
  1094. struct ast_data_search *node, *struct_children;
  1095. int member, notmatch = 0;
  1096. if (!search) {
  1097. return 0;
  1098. }
  1099. struct_children = data_search_get_node(search, structure_name);
  1100. if (!struct_children) {
  1101. return 0;
  1102. }
  1103. i = ao2_iterator_init(struct_children->children, 0);
  1104. while ((node = ao2_iterator_next(&i))) {
  1105. member = data_search_mapping_find(mapping, mapping_len, node->name);
  1106. if (member < 0) {
  1107. /* the structure member name doesn't match! */
  1108. ao2_ref(node, -1);
  1109. ao2_ref(struct_children, -1);
  1110. ao2_iterator_destroy(&i);
  1111. return 0;
  1112. }
  1113. notmatch = 0;
  1114. switch (mapping[member].type) {
  1115. case AST_DATA_PASSWORD:
  1116. notmatch = data_search_cmp_string(struct_children,
  1117. node->name,
  1118. mapping[member].get.AST_DATA_PASSWORD(structure));
  1119. break;
  1120. case AST_DATA_TIMESTAMP:
  1121. notmatch = data_search_cmp_uint(struct_children,
  1122. node->name,
  1123. mapping[member].get.AST_DATA_TIMESTAMP(structure));
  1124. break;
  1125. case AST_DATA_SECONDS:
  1126. notmatch = data_search_cmp_uint(struct_children,
  1127. node->name,
  1128. mapping[member].get.AST_DATA_SECONDS(structure));
  1129. break;
  1130. case AST_DATA_MILLISECONDS:
  1131. notmatch = data_search_cmp_uint(struct_children,
  1132. node->name,
  1133. mapping[member].get.AST_DATA_MILLISECONDS(structure));
  1134. break;
  1135. case AST_DATA_STRING:
  1136. notmatch = data_search_cmp_string(struct_children,
  1137. node->name,
  1138. mapping[member].get.AST_DATA_STRING(structure));
  1139. break;
  1140. case AST_DATA_CHARACTER:
  1141. notmatch = data_search_cmp_char(struct_children,
  1142. node->name,
  1143. mapping[member].get.AST_DATA_CHARACTER(structure));
  1144. break;
  1145. case AST_DATA_INTEGER:
  1146. notmatch = data_search_cmp_int(struct_children,
  1147. node->name,
  1148. mapping[member].get.AST_DATA_INTEGER(structure));
  1149. break;
  1150. case AST_DATA_BOOLEAN:
  1151. notmatch = data_search_cmp_bool(struct_children,
  1152. node->name,
  1153. mapping[member].get.AST_DATA_BOOLEAN(structure));
  1154. break;
  1155. case AST_DATA_UNSIGNED_INTEGER:
  1156. notmatch = data_search_cmp_uint(struct_children,
  1157. node->name,
  1158. mapping[member].get.AST_DATA_UNSIGNED_INTEGER(structure));
  1159. break;
  1160. case AST_DATA_DOUBLE:
  1161. notmatch = data_search_cmp_dbl(struct_children,
  1162. node->name,
  1163. mapping[member].get.AST_DATA_DOUBLE(structure));
  1164. break;
  1165. case AST_DATA_IPADDR:
  1166. notmatch = data_search_cmp_ipaddr(struct_children,
  1167. node->name,
  1168. mapping[member].get.AST_DATA_IPADDR(structure));
  1169. break;
  1170. case AST_DATA_POINTER:
  1171. notmatch = data_search_cmp_ptr(struct_children,
  1172. node->name,
  1173. mapping[member].get.AST_DATA_POINTER(structure));
  1174. break;
  1175. case AST_DATA_CONTAINER:
  1176. break;
  1177. }
  1178. ao2_ref(node, -1);
  1179. }
  1180. ao2_iterator_destroy(&i);
  1181. ao2_ref(struct_children, -1);
  1182. return notmatch;
  1183. }
  1184. /*!
  1185. * \internal
  1186. * \brief Release the memory allocated by a call to ao2_alloc.
  1187. */
  1188. static void data_result_destructor(void *obj)
  1189. {
  1190. struct ast_data *root = obj;
  1191. switch (root->type) {
  1192. case AST_DATA_PASSWORD:
  1193. case AST_DATA_STRING:
  1194. ast_free(root->payload.str);
  1195. ao2_ref(root->children, -1);
  1196. break;
  1197. case AST_DATA_POINTER:
  1198. case AST_DATA_CHARACTER:
  1199. case AST_DATA_CONTAINER:
  1200. case AST_DATA_INTEGER:
  1201. case AST_DATA_TIMESTAMP:
  1202. case AST_DATA_SECONDS:
  1203. case AST_DATA_MILLISECONDS:
  1204. case AST_DATA_UNSIGNED_INTEGER:
  1205. case AST_DATA_DOUBLE:
  1206. case AST_DATA_BOOLEAN:
  1207. case AST_DATA_IPADDR:
  1208. ao2_ref(root->children, -1);
  1209. break;
  1210. }
  1211. }
  1212. static struct ast_data *data_result_create(const char *name)
  1213. {
  1214. struct ast_data *res;
  1215. size_t namelen;
  1216. namelen = ast_strlen_zero(name) ? 1 : strlen(name) + 1;
  1217. res = ao2_alloc(sizeof(*res) + namelen, data_result_destructor);
  1218. if (!res) {
  1219. return NULL;
  1220. }
  1221. strcpy(res->name, namelen ? name : "");
  1222. /* initialize the children container */
  1223. res->children = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  1224. NUM_DATA_RESULT_BUCKETS, data_result_hash, NULL, data_result_cmp);
  1225. if (!res->children) {
  1226. ao2_ref(res, -1);
  1227. return NULL;
  1228. }
  1229. /* set this node as a container. */
  1230. res->type = AST_DATA_CONTAINER;
  1231. return res;
  1232. }
  1233. /*!
  1234. * \internal
  1235. * \brief Find a child node, based on its name.
  1236. * \param[in] root The starting point.
  1237. * \param[in] name The child name.
  1238. * \retval NULL if the node wasn't found.
  1239. * \retval non-NULL the node we were looking for.
  1240. */
  1241. static struct ast_data *data_result_find_child(struct ast_data *root, const char *name)
  1242. {
  1243. struct ast_data *found, *find_node;
  1244. find_node = data_result_create(name);
  1245. if (!find_node) {
  1246. return NULL;
  1247. }
  1248. found = ao2_find(root->children, find_node, OBJ_POINTER);
  1249. /* release the temporary created node used for searching. */
  1250. ao2_ref(find_node, -1);
  1251. return found;
  1252. }
  1253. int ast_data_search_match(const struct ast_data_search *search, struct ast_data *data)
  1254. {
  1255. struct ao2_iterator i, ii;
  1256. struct ast_data_search *s, *s_child;
  1257. struct ast_data *d_child;
  1258. int notmatch = 1;
  1259. if (!search) {
  1260. return 1;
  1261. }
  1262. s_child = data_search_find(search->children, data->name);
  1263. if (!s_child) {
  1264. /* nothing to compare */
  1265. ao2_ref(s_child, -1);
  1266. return 1;
  1267. }
  1268. i = ao2_iterator_init(s_child->children, 0);
  1269. while ((s = ao2_iterator_next(&i))) {
  1270. if (!ao2_container_count(s->children)) {
  1271. /* compare this search node with every data node */
  1272. d_child = data_result_find_child(data, s->name);
  1273. if (!d_child) {
  1274. ao2_ref(s, -1);
  1275. notmatch = 1;
  1276. continue;
  1277. }
  1278. switch (d_child->type) {
  1279. case AST_DATA_PASSWORD:
  1280. case AST_DATA_STRING:
  1281. notmatch = data_search_cmp_string(s_child, d_child->name,
  1282. d_child->payload.str);
  1283. break;
  1284. case AST_DATA_CHARACTER:
  1285. notmatch = data_search_cmp_char(s_child, d_child->name,
  1286. d_child->payload.character);
  1287. break;
  1288. case AST_DATA_INTEGER:
  1289. notmatch = data_search_cmp_int(s_child, d_child->name,
  1290. d_child->payload.sint);
  1291. break;
  1292. case AST_DATA_BOOLEAN:
  1293. notmatch = data_search_cmp_bool(s_child, d_child->name,
  1294. d_child->payload.boolean);
  1295. break;
  1296. case AST_DATA_UNSIGNED_INTEGER:
  1297. notmatch = data_search_cmp_uint(s_child, d_child->name,
  1298. d_child->payload.uint);
  1299. break;
  1300. case AST_DATA_TIMESTAMP:
  1301. case AST_DATA_SECONDS:
  1302. case AST_DATA_MILLISECONDS:
  1303. case AST_DATA_DOUBLE:
  1304. notmatch = data_search_cmp_uint(s_child, d_child->name,
  1305. d_child->payload.dbl);
  1306. break;
  1307. case AST_DATA_IPADDR:
  1308. notmatch = data_search_cmp_ipaddr(s_child, d_child->name,
  1309. d_child->payload.ipaddr);
  1310. break;
  1311. case AST_DATA_POINTER:
  1312. notmatch = data_search_cmp_ptr(s_child, d_child->name,
  1313. d_child->payload.ptr);
  1314. break;
  1315. case AST_DATA_CONTAINER:
  1316. break;
  1317. }
  1318. ao2_ref(d_child, -1);
  1319. } else {
  1320. ii = ao2_iterator_init(data->children, 0);
  1321. while ((d_child = ao2_iterator_next(&ii))) {
  1322. if (strcmp(d_child->name, s->name)) {
  1323. ao2_ref(d_child, -1);
  1324. continue;
  1325. }
  1326. if (!(notmatch = !ast_data_search_match(s_child, d_child))) {
  1327. /* do not continue if we have a match. */
  1328. ao2_ref(d_child, -1);
  1329. break;
  1330. }
  1331. ao2_ref(d_child, -1);
  1332. }
  1333. ao2_iterator_destroy(&ii);
  1334. }
  1335. ao2_ref(s, -1);
  1336. if (notmatch) {
  1337. /* do not continue if we don't have a match. */
  1338. break;
  1339. }
  1340. }
  1341. ao2_iterator_destroy(&i);
  1342. ao2_ref(s_child, -1);
  1343. return !notmatch;
  1344. }
  1345. /*!
  1346. * \internal
  1347. * \brief Get an internal node, from the result set.
  1348. * \param[in] node A node container.
  1349. * \param[in] path The path to the needed internal node.
  1350. * \retval NULL if the internal node is not found.
  1351. * \retval non-NULL the internal node with path 'path'.
  1352. */
  1353. static struct ast_data *data_result_get_node(struct ast_data *node,
  1354. const char *path)
  1355. {
  1356. char *savepath, *node_name;
  1357. struct ast_data *child, *current = node;
  1358. savepath = ast_strdupa(path);
  1359. node_name = next_node_name(&savepath);
  1360. while (node_name) {
  1361. child = data_result_find_child(current, node_name);
  1362. if (current != node) {
  1363. ao2_ref(current, -1);
  1364. }
  1365. if (!child) {
  1366. return NULL;
  1367. }
  1368. current = child;
  1369. node_name = next_node_name(&savepath);
  1370. }
  1371. /* do not increment the refcount of the returned object. */
  1372. if (current != node) {
  1373. ao2_ref(current, -1);
  1374. }
  1375. return current;
  1376. }
  1377. /*!
  1378. * \internal
  1379. * \brief Add a child to the specified root node.
  1380. * \param[in] root The root node pointer.
  1381. * \param[in] child The child to add to the root node.
  1382. */
  1383. static void data_result_add_child(struct ast_data *root, struct ast_data *child)
  1384. {
  1385. ao2_link(root->children, child);
  1386. }
  1387. /*!
  1388. * \internal
  1389. * \brief Common string hash function for data nodes
  1390. */
  1391. static int data_filter_hash(const void *obj, const int flags)
  1392. {
  1393. const struct data_filter *node = obj;
  1394. return ast_str_hash(node->name);
  1395. }
  1396. /*!
  1397. * \internal
  1398. * \brief Common string comparison function
  1399. */
  1400. static int data_filter_cmp(void *obj, void *arg, int flags)
  1401. {
  1402. struct data_filter *node1 = obj, *node2 = arg;
  1403. return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
  1404. }
  1405. /*!
  1406. * \internal
  1407. * \brief Destroy a data filter tree.
  1408. * \param[in] obj Data filter list to be destroyed.
  1409. */
  1410. static void data_filter_destructor(void *obj)
  1411. {
  1412. struct data_filter *filter = obj, *globres;
  1413. while ((globres = AST_LIST_REMOVE_HEAD(&(filter->glob_list), list))) {
  1414. ao2_ref(globres, -1);
  1415. }
  1416. ao2_ref(filter->children, -1);
  1417. }
  1418. /*!
  1419. * \internal
  1420. * \brief Allocate a filter node.
  1421. * \retval NULL on error.
  1422. * \retval non-NULL The allocated search node structure.
  1423. */
  1424. static struct data_filter *data_filter_alloc(const char *name)
  1425. {
  1426. char *globname, *token;
  1427. struct data_filter *res, *globfilter;
  1428. size_t name_len = strlen(name) + 1;
  1429. res = ao2_alloc(sizeof(*res) + name_len, data_filter_destructor);
  1430. if (!res) {
  1431. return NULL;
  1432. }
  1433. res->children = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  1434. NUM_DATA_FILTER_BUCKETS, data_filter_hash, NULL, data_filter_cmp);
  1435. if (!res->children) {
  1436. ao2_ref(res, -1);
  1437. return NULL;
  1438. }
  1439. strcpy(res->name, name);
  1440. if (strchr(res->name, '*')) {
  1441. globname = ast_strdupa(res->name);
  1442. while ((token = strsep(&globname, "*"))) {
  1443. globfilter = data_filter_alloc(token);
  1444. AST_LIST_INSERT_TAIL(&(res->glob_list), globfilter, list);
  1445. }
  1446. }
  1447. return res;
  1448. }
  1449. /*!
  1450. * \internal
  1451. * \brief Release a filter tree.
  1452. * \param[in] filter The filter tree root node.
  1453. */
  1454. static void data_filter_release(struct data_filter *filter)
  1455. {
  1456. ao2_ref(filter, -1);
  1457. }
  1458. /*!
  1459. * \internal
  1460. * \brief Find a child node, based on his name.
  1461. * \param[in] parent Where to find the node.
  1462. * \param[in] name The node name to find.
  1463. * \retval NULL if a node wasn't found.
  1464. * \retval The node found.
  1465. * \note Remember to decrement the ref count of the returned node after using it.
  1466. */
  1467. static struct data_filter *data_filter_find(struct ao2_container *parent,
  1468. const char *name)
  1469. {
  1470. int i, olend, orend, globfound;
  1471. size_t name_len = strlen(name), glob_len;
  1472. struct ao2_iterator iter;
  1473. struct data_filter *find_node, *found, *globres;
  1474. find_node = data_filter_alloc(name);
  1475. if (!find_node) {
  1476. return NULL;
  1477. }
  1478. found = ao2_find(parent, find_node, OBJ_POINTER);
  1479. /* free the created node used for searching. */
  1480. ao2_ref(find_node, -1);
  1481. if (found) {
  1482. return found;
  1483. }
  1484. iter = ao2_iterator_init(parent, 0);
  1485. while ((found = ao2_iterator_next(&iter))) {
  1486. if (!AST_LIST_EMPTY(&(found->glob_list))) {
  1487. i = 0;
  1488. globfound = 1;
  1489. olend = ast_strlen_zero(AST_LIST_FIRST(&(found->glob_list))->name);
  1490. orend = ast_strlen_zero(AST_LIST_LAST(&(found->glob_list))->name);
  1491. AST_LIST_TRAVERSE(&(found->glob_list), globres, list) {
  1492. if (!*globres->name) {
  1493. continue;
  1494. }
  1495. glob_len = strlen(globres->name);
  1496. if (!i && !olend) {
  1497. if (strncasecmp(name, globres->name, glob_len)) {
  1498. globfound = 0;
  1499. break;
  1500. }
  1501. i += glob_len;
  1502. continue;
  1503. }
  1504. for (globfound = 0; name_len - i >= glob_len; ++i) {
  1505. if (!strncasecmp(name + i, globres->name, glob_len)) {
  1506. globfound = 1;
  1507. i += glob_len;
  1508. break;
  1509. }
  1510. }
  1511. if (!globfound) {
  1512. break;
  1513. }
  1514. }
  1515. if (globfound && (i == name_len || orend)) {
  1516. ao2_iterator_destroy(&iter);
  1517. return found;
  1518. }
  1519. }
  1520. ao2_ref(found, -1);
  1521. }
  1522. ao2_iterator_destroy(&iter);
  1523. return NULL;
  1524. }
  1525. /*!
  1526. * \internal
  1527. * \brief Add a child to the specified node.
  1528. * \param[in] root The root node where to add the child.
  1529. * \param[in] name The name of the node to add.
  1530. * \note Remember to decrement the ref count after using the returned node.
  1531. */
  1532. static struct data_filter *data_filter_add_child(struct ao2_container *root,
  1533. char *name)
  1534. {
  1535. struct data_filter *node;
  1536. node = data_filter_find(root, name);
  1537. if (node) {
  1538. return node;
  1539. }
  1540. node = data_filter_alloc(name);
  1541. if (!node) {
  1542. return NULL;
  1543. }
  1544. ao2_link(root, node);
  1545. return node;
  1546. }
  1547. /*!
  1548. * \internal
  1549. * \brief Add a node to a filter list from a path
  1550. * \param[in] Filter list to add the path onto.
  1551. * \param[in] The path to add into the filter list.
  1552. * \retval NULL on error.
  1553. * \retval non-NULL A tree with the wanted nodes.
  1554. */
  1555. static int data_filter_add_nodes(struct ao2_container *root, char *path)
  1556. {
  1557. struct data_filter *node;
  1558. char *savepath, *saveptr, *token, *node_name;
  1559. int ret = 0;
  1560. if (!path) {
  1561. return 0;
  1562. }
  1563. savepath = ast_strdupa(path);
  1564. node_name = next_node_name(&savepath);
  1565. if (!node_name) {
  1566. return 0;
  1567. }
  1568. for (token = strtok_r(node_name, "|", &saveptr);
  1569. token; token = strtok_r(NULL, "|", &saveptr)) {
  1570. node = data_filter_add_child(root, token);
  1571. if (!node) {
  1572. continue;
  1573. }
  1574. data_filter_add_nodes(node->children, savepath);
  1575. ret = 1;
  1576. ao2_ref(node, -1);
  1577. }
  1578. return ret;
  1579. }
  1580. /*!
  1581. * \internal
  1582. * \brief Generate a filter list based on a filter string provided by the API user.
  1583. * \param[in] A filter string to create a filter from.
  1584. */
  1585. static struct data_filter *data_filter_generate(const char *constfilter)
  1586. {
  1587. struct data_filter *filter = NULL;
  1588. char *strfilter, *token, *saveptr;
  1589. int node_added = 0;
  1590. if (!constfilter) {
  1591. return NULL;
  1592. }
  1593. strfilter = ast_strdupa(constfilter);
  1594. filter = data_filter_alloc("/");
  1595. if (!filter) {
  1596. return NULL;
  1597. }
  1598. for (token = strtok_r(strfilter, ",", &saveptr); token;
  1599. token = strtok_r(NULL, ",", &saveptr)) {
  1600. node_added = data_filter_add_nodes(filter->children, token);
  1601. }
  1602. if (!node_added) {
  1603. ao2_ref(filter, -1);
  1604. return NULL;
  1605. }
  1606. return filter;
  1607. }
  1608. /*!
  1609. * \internal
  1610. * \brief Generate all the tree from a specified provider.
  1611. * \param[in] query The query executed.
  1612. * \param[in] root_provider The provider specified in the path of the query.
  1613. * \param[in] parent_node_name The root node name.
  1614. * \retval NULL on error.
  1615. * \retval non-NULL The generated result tree.
  1616. */
  1617. static struct ast_data *data_result_generate_node(const struct ast_data_query *query,
  1618. const struct data_provider *root_provider,
  1619. const char *parent_node_name,
  1620. const struct ast_data_search *search,
  1621. const struct data_filter *filter)
  1622. {
  1623. struct ast_data *generated, *node;
  1624. struct ao2_iterator i;
  1625. struct data_provider *provider;
  1626. struct ast_data_search *search_child = NULL;
  1627. struct data_filter *filter_child;
  1628. node = data_result_create(parent_node_name);
  1629. if (!node) {
  1630. ast_log(LOG_ERROR, "Unable to allocate '%s' node\n", parent_node_name);
  1631. return NULL;
  1632. }
  1633. if (root_provider->module) {
  1634. ast_module_ref(root_provider->module);
  1635. }
  1636. /* if this is a terminal node, just run the callback function. */
  1637. if (root_provider->handler && root_provider->handler->get) {
  1638. node->filter = filter;
  1639. root_provider->handler->get(search, node);
  1640. if (root_provider->module) {
  1641. ast_module_unref(root_provider->module);
  1642. }
  1643. return node;
  1644. }
  1645. if (root_provider->module) {
  1646. ast_module_unref(root_provider->module);
  1647. }
  1648. /* if this is not a terminal node, generate every child node. */
  1649. i = ao2_iterator_init(root_provider->children, 0);
  1650. while ((provider = ao2_iterator_next(&i))) {
  1651. filter_child = NULL;
  1652. generated = NULL;
  1653. /* get the internal search node. */
  1654. if (search) {
  1655. search_child = data_search_find(search->children, provider->name);
  1656. }
  1657. /* get the internal filter node. */
  1658. if (filter) {
  1659. filter_child = data_filter_find(filter->children, provider->name);
  1660. }
  1661. if (!filter || filter_child) {
  1662. /* only generate the internal node, if we have something to
  1663. * generate based on the filtering string. */
  1664. generated = data_result_generate_node(query, provider,
  1665. provider->name,
  1666. search_child, filter_child);
  1667. }
  1668. /* decrement the refcount of the internal search node. */
  1669. if (search_child) {
  1670. ao2_ref(search_child, -1);
  1671. }
  1672. /* decrement the refcount of the internal filter node. */
  1673. if (filter_child) {
  1674. ao2_ref(filter_child, -1);
  1675. }
  1676. if (generated) {
  1677. data_result_add_child(node, generated);
  1678. ao2_ref(generated, -1);
  1679. }
  1680. ao2_ref(provider, -1);
  1681. }
  1682. ao2_iterator_destroy(&i);
  1683. return node;
  1684. }
  1685. /*!
  1686. * \internal
  1687. * \brief Generate a result tree based on a query.
  1688. * \param[in] query The complete query structure.
  1689. * \param[in] search_path The path to retrieve.
  1690. * \retval NULL on error.
  1691. * \retval non-NULL The generated data result.
  1692. */
  1693. static struct ast_data *data_result_generate(const struct ast_data_query *query,
  1694. const char *search_path)
  1695. {
  1696. char *node_name, *tmp_path;
  1697. struct data_provider *provider_child, *tmp_provider_child;
  1698. struct ast_data *result, *result_filtered;
  1699. struct ast_data_search *search = NULL, *search_child = NULL;
  1700. struct data_filter *filter = NULL, *filter_child = NULL;
  1701. if (!search_path) {
  1702. /* generate all the trees?. */
  1703. return NULL;
  1704. }
  1705. tmp_path = ast_strdupa(search_path);
  1706. /* start searching the root node name */
  1707. node_name = next_node_name(&tmp_path);
  1708. if (!node_name) {
  1709. return NULL;
  1710. }
  1711. provider_child = data_provider_find(root_data.container, node_name, NULL);
  1712. /* continue with the rest of the path. */
  1713. while (provider_child) {
  1714. node_name = next_node_name(&tmp_path);
  1715. if (!node_name) {
  1716. break;
  1717. }
  1718. tmp_provider_child = data_provider_find(provider_child->children,
  1719. node_name, NULL);
  1720. /* release the reference from this child */
  1721. ao2_ref(provider_child, -1);
  1722. provider_child = tmp_provider_child;
  1723. }
  1724. if (!provider_child) {
  1725. ast_log(LOG_ERROR, "Invalid path '%s', '%s' not found.\n",
  1726. tmp_path, node_name);
  1727. return NULL;
  1728. }
  1729. /* generate the search tree. */
  1730. if (query->search) {
  1731. search = data_search_generate(query->search);
  1732. if (search) {
  1733. search_child = data_search_find(search->children,
  1734. provider_child->name);
  1735. }
  1736. }
  1737. /* generate the filter tree. */
  1738. if (query->filter) {
  1739. filter = data_filter_generate(query->filter);
  1740. if (filter) {
  1741. filter_child = data_filter_find(filter->children,
  1742. provider_child->name);
  1743. }
  1744. }
  1745. result = data_result_generate_node(query, provider_child, provider_child->name,
  1746. search_child, filter_child);
  1747. /* release the requested provider. */
  1748. ao2_ref(provider_child, -1);
  1749. /* release the generated search tree. */
  1750. if (search_child) {
  1751. ao2_ref(search_child, -1);
  1752. }
  1753. if (filter_child) {
  1754. ao2_ref(filter_child, -1);
  1755. }
  1756. if (search) {
  1757. data_search_release(search);
  1758. }
  1759. result_filtered = result;
  1760. /* release the generated filter tree. */
  1761. if (filter) {
  1762. data_filter_release(filter);
  1763. }
  1764. return result_filtered;
  1765. }
  1766. struct ast_data *ast_data_get(const struct ast_data_query *query)
  1767. {
  1768. struct ast_data *res;
  1769. /* check compatibility */
  1770. if (!data_structure_compatible(query->version, latest_query_compatible_version,
  1771. current_query_version)) {
  1772. return NULL;
  1773. }
  1774. data_read_lock();
  1775. res = data_result_generate(query, query->path);
  1776. data_unlock();
  1777. if (!res) {
  1778. ast_log(LOG_ERROR, "Unable to get data from %s\n", query->path);
  1779. return NULL;
  1780. }
  1781. return res;
  1782. }
  1783. #ifdef HAVE_LIBXML2
  1784. /*!
  1785. * \internal
  1786. * \brief Helper function to move an ast_data tree to xml.
  1787. * \param[in] parent_data The initial ast_data node to be passed to xml.
  1788. * \param[out] parent_xml The root node to insert the xml.
  1789. */
  1790. static void data_get_xml_add_child(struct ast_data *parent_data,
  1791. struct ast_xml_node *parent_xml)
  1792. {
  1793. struct ao2_iterator i;
  1794. struct ast_data *node;
  1795. struct ast_xml_node *child_xml;
  1796. char node_content[256];
  1797. i = ao2_iterator_init(parent_data->children, 0);
  1798. while ((node = ao2_iterator_next(&i))) {
  1799. child_xml = ast_xml_new_node(node->name);
  1800. if (!child_xml) {
  1801. ao2_ref(node, -1);
  1802. continue;
  1803. }
  1804. switch (node->type) {
  1805. case AST_DATA_CONTAINER:
  1806. data_get_xml_add_child(node, child_xml);
  1807. break;
  1808. case AST_DATA_PASSWORD:
  1809. ast_xml_set_text(child_xml, node->payload.str);
  1810. break;
  1811. case AST_DATA_TIMESTAMP:
  1812. snprintf(node_content, sizeof(node_content), "%u",
  1813. node->payload.uint);
  1814. ast_xml_set_text(child_xml, node_content);
  1815. break;
  1816. case AST_DATA_SECONDS:
  1817. snprintf(node_content, sizeof(node_content), "%u",
  1818. node->payload.uint);
  1819. ast_xml_set_text(child_xml, node_content);
  1820. break;
  1821. case AST_DATA_MILLISECONDS:
  1822. snprintf(node_content, sizeof(node_content), "%u",
  1823. node->payload.uint);
  1824. ast_xml_set_text(child_xml, node_content);
  1825. break;
  1826. case AST_DATA_STRING:
  1827. ast_xml_set_text(child_xml, node->payload.str);
  1828. break;
  1829. case AST_DATA_CHARACTER:
  1830. snprintf(node_content, sizeof(node_content), "%c",
  1831. node->payload.character);
  1832. ast_xml_set_text(child_xml, node_content);
  1833. break;
  1834. case AST_DATA_INTEGER:
  1835. snprintf(node_content, sizeof(node_content), "%d",
  1836. node->payload.sint);
  1837. ast_xml_set_text(child_xml, node_content);
  1838. break;
  1839. case AST_DATA_UNSIGNED_INTEGER:
  1840. snprintf(node_content, sizeof(node_content), "%u",
  1841. node->payload.uint);
  1842. ast_xml_set_text(child_xml, node_content);
  1843. break;
  1844. case AST_DATA_DOUBLE:
  1845. snprintf(node_content, sizeof(node_content), "%f",
  1846. node->payload.dbl);
  1847. ast_xml_set_text(child_xml, node_content);
  1848. break;
  1849. case AST_DATA_BOOLEAN:
  1850. if (node->payload.boolean) {
  1851. ast_xml_set_text(child_xml, "true");
  1852. } else {
  1853. ast_xml_set_text(child_xml, "false");
  1854. }
  1855. break;
  1856. case AST_DATA_POINTER:
  1857. snprintf(node_content, sizeof(node_content), "%p",
  1858. node->payload.ptr);
  1859. ast_xml_set_text(child_xml, node_content);
  1860. break;
  1861. case AST_DATA_IPADDR:
  1862. snprintf(node_content, sizeof(node_content), "%s",
  1863. ast_inet_ntoa(node->payload.ipaddr));
  1864. ast_xml_set_text(child_xml, node_content);
  1865. break;
  1866. }
  1867. ast_xml_add_child(parent_xml, child_xml);
  1868. ao2_ref(node, -1);
  1869. }
  1870. ao2_iterator_destroy(&i);
  1871. }
  1872. struct ast_xml_doc *ast_data_get_xml(const struct ast_data_query *query)
  1873. {
  1874. struct ast_xml_doc *doc;
  1875. struct ast_xml_node *root;
  1876. struct ast_data *res;
  1877. res = ast_data_get(query);
  1878. if (!res) {
  1879. return NULL;
  1880. }
  1881. doc = ast_xml_new();
  1882. if (!doc) {
  1883. ast_data_free(res);
  1884. return NULL;
  1885. }
  1886. root = ast_xml_new_node(res->name);
  1887. if (!root) {
  1888. ast_xml_close(doc);
  1889. }
  1890. ast_xml_set_root(doc, root);
  1891. data_get_xml_add_child(res, root);
  1892. ast_data_free(res);
  1893. return doc;
  1894. }
  1895. #endif
  1896. enum ast_data_type ast_data_retrieve_type(struct ast_data *node, const char *path)
  1897. {
  1898. struct ast_data *internal;
  1899. internal = data_result_get_node(node, path);
  1900. if (!internal) {
  1901. return -1;
  1902. }
  1903. return internal->type;
  1904. }
  1905. char *ast_data_retrieve_name(struct ast_data *node)
  1906. {
  1907. return node->name;
  1908. }
  1909. /*!
  1910. * \internal
  1911. * \brief Insert a child node inside a passed parent node.
  1912. * \param root Where we are going to insert the child node.
  1913. * \param name The name of the child node to add.
  1914. * \param type The type of content inside the child node.
  1915. * \param ptr The actual content of the child node.
  1916. * \retval NULL on error.
  1917. * \retval non-NULL The added child node pointer.
  1918. */
  1919. static struct ast_data *__ast_data_add(struct ast_data *root, const char *name,
  1920. enum ast_data_type type, void *ptr)
  1921. {
  1922. struct ast_data *node;
  1923. struct data_filter *filter, *filter_child = NULL;
  1924. if (!root || !root->children) {
  1925. /* invalid data result node. */
  1926. return NULL;
  1927. }
  1928. /* check if we need to add this node, based on the filter. */
  1929. if (root->filter) {
  1930. filter = data_filter_find(root->filter->children, name);
  1931. if (!filter) {
  1932. return NULL;
  1933. }
  1934. ao2_ref(filter, -1);
  1935. }
  1936. node = data_result_create(name);
  1937. if (!node) {
  1938. return NULL;
  1939. }
  1940. node->type = type;
  1941. switch (type) {
  1942. case AST_DATA_BOOLEAN:
  1943. node->payload.boolean = *(unsigned int *) ptr;
  1944. break;
  1945. case AST_DATA_INTEGER:
  1946. node->payload.sint = *(int *) ptr;
  1947. break;
  1948. case AST_DATA_TIMESTAMP:
  1949. case AST_DATA_SECONDS:
  1950. case AST_DATA_MILLISECONDS:
  1951. case AST_DATA_UNSIGNED_INTEGER:
  1952. node->payload.uint = *(unsigned int *) ptr;
  1953. break;
  1954. case AST_DATA_DOUBLE:
  1955. node->payload.dbl = *(double *) ptr;
  1956. break;
  1957. case AST_DATA_PASSWORD:
  1958. case AST_DATA_STRING:
  1959. node->payload.str = (char *) ptr;
  1960. break;
  1961. case AST_DATA_CHARACTER:
  1962. node->payload.character = *(char *) ptr;
  1963. break;
  1964. case AST_DATA_POINTER:
  1965. node->payload.ptr = ptr;
  1966. break;
  1967. case AST_DATA_IPADDR:
  1968. node->payload.ipaddr = *(struct in_addr *) ptr;
  1969. break;
  1970. case AST_DATA_CONTAINER:
  1971. if (root->filter) {
  1972. filter_child = data_filter_find(root->filter->children, name);
  1973. if (filter_child) {
  1974. /* do not increment the refcount because it is not neccesary. */
  1975. ao2_ref(filter_child, -1);
  1976. }
  1977. }
  1978. node->filter = filter_child;
  1979. break;
  1980. default:
  1981. break;
  1982. }
  1983. data_result_add_child(root, node);
  1984. ao2_ref(node, -1);
  1985. return node;
  1986. }
  1987. struct ast_data *ast_data_add_node(struct ast_data *root, const char *name)
  1988. {
  1989. return __ast_data_add(root, name, AST_DATA_CONTAINER, NULL);
  1990. }
  1991. struct ast_data *ast_data_add_int(struct ast_data *root, const char *name, int value)
  1992. {
  1993. return __ast_data_add(root, name, AST_DATA_INTEGER, &value);
  1994. }
  1995. struct ast_data *ast_data_add_char(struct ast_data *root, const char *name, char value)
  1996. {
  1997. return __ast_data_add(root, name, AST_DATA_CHARACTER, &value);
  1998. }
  1999. struct ast_data *ast_data_add_uint(struct ast_data *root, const char *name,
  2000. unsigned int value)
  2001. {
  2002. return __ast_data_add(root, name, AST_DATA_UNSIGNED_INTEGER, &value);
  2003. }
  2004. struct ast_data *ast_data_add_dbl(struct ast_data *root, const char *childname,
  2005. double dbl)
  2006. {
  2007. return __ast_data_add(root, childname, AST_DATA_DOUBLE, &dbl);
  2008. }
  2009. struct ast_data *ast_data_add_bool(struct ast_data *root, const char *childname,
  2010. unsigned int boolean)
  2011. {
  2012. return __ast_data_add(root, childname, AST_DATA_BOOLEAN, &boolean);
  2013. }
  2014. struct ast_data *ast_data_add_ipaddr(struct ast_data *root, const char *childname,
  2015. struct in_addr addr)
  2016. {
  2017. return __ast_data_add(root, childname, AST_DATA_IPADDR, &addr);
  2018. }
  2019. struct ast_data *ast_data_add_ptr(struct ast_data *root, const char *childname,
  2020. void *ptr)
  2021. {
  2022. return __ast_data_add(root, childname, AST_DATA_POINTER, ptr);
  2023. }
  2024. struct ast_data *ast_data_add_timestamp(struct ast_data *root, const char *childname,
  2025. unsigned int timestamp)
  2026. {
  2027. return __ast_data_add(root, childname, AST_DATA_TIMESTAMP, &timestamp);
  2028. }
  2029. struct ast_data *ast_data_add_seconds(struct ast_data *root, const char *childname,
  2030. unsigned int seconds)
  2031. {
  2032. return __ast_data_add(root, childname, AST_DATA_SECONDS, &seconds);
  2033. }
  2034. struct ast_data *ast_data_add_milliseconds(struct ast_data *root, const char *childname,
  2035. unsigned int milliseconds)
  2036. {
  2037. return __ast_data_add(root, childname, AST_DATA_MILLISECONDS, &milliseconds);
  2038. }
  2039. struct ast_data *ast_data_add_password(struct ast_data *root, const char *childname,
  2040. const char *value)
  2041. {
  2042. char *name;
  2043. size_t namelen = 1 + (ast_strlen_zero(value) ? 0 : strlen(value));
  2044. struct ast_data *res;
  2045. if (!(name = ast_malloc(namelen))) {
  2046. return NULL;
  2047. }
  2048. strcpy(name, (ast_strlen_zero(value) ? "" : value));
  2049. res = __ast_data_add(root, childname, AST_DATA_PASSWORD, name);
  2050. if (!res) {
  2051. ast_free(name);
  2052. }
  2053. return res;
  2054. }
  2055. struct ast_data *ast_data_add_str(struct ast_data *root, const char *childname,
  2056. const char *value)
  2057. {
  2058. char *name;
  2059. size_t namelen = 1 + (ast_strlen_zero(value) ? 0 : strlen(value));
  2060. struct ast_data *res;
  2061. if (!(name = ast_malloc(namelen))) {
  2062. return NULL;
  2063. }
  2064. strcpy(name, (ast_strlen_zero(value) ? "" : value));
  2065. res = __ast_data_add(root, childname, AST_DATA_STRING, name);
  2066. if (!res) {
  2067. ast_free(name);
  2068. }
  2069. return res;
  2070. }
  2071. int __ast_data_add_structure(struct ast_data *root,
  2072. const struct ast_data_mapping_structure *mapping, size_t mapping_len,
  2073. void *structure)
  2074. {
  2075. int i;
  2076. for (i = 0; i < mapping_len; i++) {
  2077. switch (mapping[i].type) {
  2078. case AST_DATA_INTEGER:
  2079. ast_data_add_int(root, mapping[i].name,
  2080. mapping[i].get.AST_DATA_INTEGER(structure));
  2081. break;
  2082. case AST_DATA_UNSIGNED_INTEGER:
  2083. ast_data_add_uint(root, mapping[i].name,
  2084. mapping[i].get.AST_DATA_UNSIGNED_INTEGER(structure));
  2085. break;
  2086. case AST_DATA_DOUBLE:
  2087. ast_data_add_dbl(root, mapping[i].name,
  2088. mapping[i].get.AST_DATA_DOUBLE(structure));
  2089. break;
  2090. case AST_DATA_BOOLEAN:
  2091. ast_data_add_bool(root, mapping[i].name,
  2092. mapping[i].get.AST_DATA_BOOLEAN(structure));
  2093. break;
  2094. case AST_DATA_PASSWORD:
  2095. ast_data_add_password(root, mapping[i].name,
  2096. mapping[i].get.AST_DATA_PASSWORD(structure));
  2097. break;
  2098. case AST_DATA_TIMESTAMP:
  2099. ast_data_add_timestamp(root, mapping[i].name,
  2100. mapping[i].get.AST_DATA_TIMESTAMP(structure));
  2101. break;
  2102. case AST_DATA_SECONDS:
  2103. ast_data_add_seconds(root, mapping[i].name,
  2104. mapping[i].get.AST_DATA_SECONDS(structure));
  2105. break;
  2106. case AST_DATA_MILLISECONDS:
  2107. ast_data_add_milliseconds(root, mapping[i].name,
  2108. mapping[i].get.AST_DATA_MILLISECONDS(structure));
  2109. break;
  2110. case AST_DATA_STRING:
  2111. ast_data_add_str(root, mapping[i].name,
  2112. mapping[i].get.AST_DATA_STRING(structure));
  2113. break;
  2114. case AST_DATA_CHARACTER:
  2115. ast_data_add_char(root, mapping[i].name,
  2116. mapping[i].get.AST_DATA_CHARACTER(structure));
  2117. break;
  2118. case AST_DATA_CONTAINER:
  2119. break;
  2120. case AST_DATA_IPADDR:
  2121. ast_data_add_ipaddr(root, mapping[i].name,
  2122. mapping[i].get.AST_DATA_IPADDR(structure));
  2123. break;
  2124. case AST_DATA_POINTER:
  2125. ast_data_add_ptr(root, mapping[i].name,
  2126. mapping[i].get.AST_DATA_POINTER(structure));
  2127. break;
  2128. }
  2129. }
  2130. return 0;
  2131. }
  2132. void ast_data_remove_node(struct ast_data *root, struct ast_data *child)
  2133. {
  2134. ao2_unlink(root->children, child);
  2135. }
  2136. void ast_data_free(struct ast_data *root)
  2137. {
  2138. /* destroy it, this will destroy all the internal nodes. */
  2139. ao2_ref(root, -1);
  2140. }
  2141. struct ast_data_iterator *ast_data_iterator_init(struct ast_data *tree,
  2142. const char *elements)
  2143. {
  2144. struct ast_data_iterator *iterator;
  2145. struct ao2_iterator i;
  2146. struct ast_data *internal = tree;
  2147. char *path, *ptr = NULL;
  2148. if (!elements) {
  2149. return NULL;
  2150. }
  2151. /* tree is the node we want to use to iterate? or we are going
  2152. * to iterate thow an internal node? */
  2153. path = ast_strdupa(elements);
  2154. ptr = strrchr(path, '/');
  2155. if (ptr) {
  2156. *ptr = '\0';
  2157. internal = data_result_get_node(tree, path);
  2158. if (!internal) {
  2159. return NULL;
  2160. }
  2161. }
  2162. iterator = ast_calloc(1, sizeof(*iterator));
  2163. if (!iterator) {
  2164. return NULL;
  2165. }
  2166. i = ao2_iterator_init(internal->children, 0);
  2167. iterator->pattern = (ptr ? strrchr(elements, '/') + 1 : elements);
  2168. /* is the last node a regular expression?, compile it! */
  2169. if (!regcomp(&(iterator->regex_pattern), iterator->pattern,
  2170. REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
  2171. iterator->is_pattern = 1;
  2172. }
  2173. iterator->internal_iterator = i;
  2174. return iterator;
  2175. }
  2176. void ast_data_iterator_end(struct ast_data_iterator *iterator)
  2177. {
  2178. /* decrement the reference counter. */
  2179. if (iterator->last) {
  2180. ao2_ref(iterator->last, -1);
  2181. }
  2182. /* release the generated pattern. */
  2183. if (iterator->is_pattern) {
  2184. regfree(&(iterator->regex_pattern));
  2185. }
  2186. ao2_iterator_destroy(&(iterator->internal_iterator));
  2187. ast_free(iterator);
  2188. iterator = NULL;
  2189. }
  2190. struct ast_data *ast_data_iterator_next(struct ast_data_iterator *iterator)
  2191. {
  2192. struct ast_data *res;
  2193. if (iterator->last) {
  2194. /* release the last retrieved node reference. */
  2195. ao2_ref(iterator->last, -1);
  2196. }
  2197. while ((res = ao2_iterator_next(&iterator->internal_iterator))) {
  2198. /* if there is no node name pattern specified, return
  2199. * the next node. */
  2200. if (!iterator->pattern) {
  2201. break;
  2202. }
  2203. /* if the pattern is a regular expression, check if this node
  2204. * matches. */
  2205. if (iterator->is_pattern && !regexec(&(iterator->regex_pattern),
  2206. res->name, 0, NULL, 0)) {
  2207. break;
  2208. }
  2209. /* if there is a pattern specified, check if this node matches
  2210. * the wanted node names. */
  2211. if (!iterator->is_pattern && (iterator->pattern &&
  2212. !strcasecmp(res->name, iterator->pattern))) {
  2213. break;
  2214. }
  2215. ao2_ref(res, -1);
  2216. }
  2217. iterator->last = res;
  2218. return res;
  2219. }
  2220. int ast_data_retrieve(struct ast_data *tree, const char *path,
  2221. struct ast_data_retrieve *content)
  2222. {
  2223. struct ast_data *node;
  2224. if (!content) {
  2225. return -1;
  2226. }
  2227. node = data_result_get_node(tree, path);
  2228. if (!node) {
  2229. ast_log(LOG_ERROR, "Invalid internal node %s\n", path);
  2230. return -1;
  2231. }
  2232. content->type = node->type;
  2233. switch (node->type) {
  2234. case AST_DATA_STRING:
  2235. content->value.AST_DATA_STRING = node->payload.str;
  2236. break;
  2237. case AST_DATA_PASSWORD:
  2238. content->value.AST_DATA_PASSWORD = node->payload.str;
  2239. break;
  2240. case AST_DATA_TIMESTAMP:
  2241. content->value.AST_DATA_TIMESTAMP = node->payload.uint;
  2242. break;
  2243. case AST_DATA_SECONDS:
  2244. content->value.AST_DATA_SECONDS = node->payload.uint;
  2245. break;
  2246. case AST_DATA_MILLISECONDS:
  2247. content->value.AST_DATA_MILLISECONDS = node->payload.uint;
  2248. break;
  2249. case AST_DATA_CHARACTER:
  2250. content->value.AST_DATA_CHARACTER = node->payload.character;
  2251. break;
  2252. case AST_DATA_INTEGER:
  2253. content->value.AST_DATA_INTEGER = node->payload.sint;
  2254. break;
  2255. case AST_DATA_UNSIGNED_INTEGER:
  2256. content->value.AST_DATA_UNSIGNED_INTEGER = node->payload.uint;
  2257. break;
  2258. case AST_DATA_BOOLEAN:
  2259. content->value.AST_DATA_BOOLEAN = node->payload.boolean;
  2260. break;
  2261. case AST_DATA_IPADDR:
  2262. content->value.AST_DATA_IPADDR = node->payload.ipaddr;
  2263. break;
  2264. case AST_DATA_DOUBLE:
  2265. content->value.AST_DATA_DOUBLE = node->payload.dbl;
  2266. break;
  2267. case AST_DATA_CONTAINER:
  2268. break;
  2269. case AST_DATA_POINTER:
  2270. content->value.AST_DATA_POINTER = node->payload.ptr;
  2271. break;
  2272. }
  2273. return 0;
  2274. }
  2275. /*!
  2276. * \internal
  2277. * \brief One color for each node type.
  2278. */
  2279. static const struct {
  2280. enum ast_data_type type;
  2281. int color;
  2282. } data_result_color[] = {
  2283. { AST_DATA_STRING, COLOR_BLUE },
  2284. { AST_DATA_PASSWORD, COLOR_BRBLUE },
  2285. { AST_DATA_TIMESTAMP, COLOR_CYAN },
  2286. { AST_DATA_SECONDS, COLOR_MAGENTA },
  2287. { AST_DATA_MILLISECONDS, COLOR_BRMAGENTA },
  2288. { AST_DATA_CHARACTER, COLOR_GRAY },
  2289. { AST_DATA_INTEGER, COLOR_RED },
  2290. { AST_DATA_UNSIGNED_INTEGER, COLOR_RED },
  2291. { AST_DATA_DOUBLE, COLOR_RED },
  2292. { AST_DATA_BOOLEAN, COLOR_BRRED },
  2293. { AST_DATA_CONTAINER, COLOR_GREEN },
  2294. { AST_DATA_IPADDR, COLOR_BROWN },
  2295. { AST_DATA_POINTER, COLOR_YELLOW },
  2296. };
  2297. /*!
  2298. * \internal
  2299. * \brief Get the color configured for a specific node type.
  2300. * \param[in] type The node type.
  2301. * \returns The color specified for the passed type.
  2302. */
  2303. static int data_result_get_color(enum ast_data_type type)
  2304. {
  2305. int i;
  2306. for (i = 0; i < ARRAY_LEN(data_result_color); i++) {
  2307. if (data_result_color[i].type == type) {
  2308. return data_result_color[i].color;
  2309. }
  2310. }
  2311. return COLOR_BLUE;
  2312. }
  2313. /*!
  2314. * \internal
  2315. * \brief Print a node to the CLI.
  2316. * \param[in] fd The CLI file descriptor.
  2317. * \param[in] node The node to print.
  2318. * \param[in] depth The actual node depth in the tree.
  2319. */
  2320. static void data_result_print_cli_node(int fd, const struct ast_data *node, uint32_t depth)
  2321. {
  2322. int i;
  2323. struct ast_str *tabs, *output;
  2324. tabs = ast_str_create(depth * 10 + 1);
  2325. if (!tabs) {
  2326. return;
  2327. }
  2328. ast_str_reset(tabs);
  2329. for (i = 0; i < depth; i++) {
  2330. ast_str_append(&tabs, 0, " ");
  2331. }
  2332. output = ast_str_create(20);
  2333. if (!output) {
  2334. ast_free(tabs);
  2335. return;
  2336. }
  2337. ast_str_reset(output);
  2338. ast_term_color_code(&output, data_result_get_color(node->type), 0);
  2339. switch (node->type) {
  2340. case AST_DATA_POINTER:
  2341. ast_str_append(&output, 0, "%s%s: %p\n", ast_str_buffer(tabs),
  2342. node->name, node->payload.ptr);
  2343. break;
  2344. case AST_DATA_PASSWORD:
  2345. ast_str_append(&output, 0, "%s%s: \"%s\"\n",
  2346. ast_str_buffer(tabs),
  2347. node->name,
  2348. node->payload.str);
  2349. break;
  2350. case AST_DATA_STRING:
  2351. ast_str_append(&output, 0, "%s%s: \"%s\"\n",
  2352. ast_str_buffer(tabs),
  2353. node->name,
  2354. node->payload.str);
  2355. break;
  2356. case AST_DATA_CHARACTER:
  2357. ast_str_append(&output, 0, "%s%s: \'%c\'\n",
  2358. ast_str_buffer(tabs),
  2359. node->name,
  2360. node->payload.character);
  2361. break;
  2362. case AST_DATA_CONTAINER:
  2363. ast_str_append(&output, 0, "%s%s\n", ast_str_buffer(tabs),
  2364. node->name);
  2365. break;
  2366. case AST_DATA_TIMESTAMP:
  2367. ast_str_append(&output, 0, "%s%s: %u\n", ast_str_buffer(tabs),
  2368. node->name,
  2369. node->payload.uint);
  2370. break;
  2371. case AST_DATA_SECONDS:
  2372. ast_str_append(&output, 0, "%s%s: %u\n", ast_str_buffer(tabs),
  2373. node->name,
  2374. node->payload.uint);
  2375. break;
  2376. case AST_DATA_MILLISECONDS:
  2377. ast_str_append(&output, 0, "%s%s: %u\n", ast_str_buffer(tabs),
  2378. node->name,
  2379. node->payload.uint);
  2380. break;
  2381. case AST_DATA_INTEGER:
  2382. ast_str_append(&output, 0, "%s%s: %d\n", ast_str_buffer(tabs),
  2383. node->name,
  2384. node->payload.sint);
  2385. break;
  2386. case AST_DATA_UNSIGNED_INTEGER:
  2387. ast_str_append(&output, 0, "%s%s: %u\n", ast_str_buffer(tabs),
  2388. node->name,
  2389. node->payload.uint);
  2390. break;
  2391. case AST_DATA_DOUBLE:
  2392. ast_str_append(&output, 0, "%s%s: %lf\n", ast_str_buffer(tabs),
  2393. node->name,
  2394. node->payload.dbl);
  2395. break;
  2396. case AST_DATA_BOOLEAN:
  2397. ast_str_append(&output, 0, "%s%s: %s\n", ast_str_buffer(tabs),
  2398. node->name,
  2399. ((node->payload.boolean) ? "True" : "False"));
  2400. break;
  2401. case AST_DATA_IPADDR:
  2402. ast_str_append(&output, 0, "%s%s: %s\n", ast_str_buffer(tabs),
  2403. node->name,
  2404. ast_inet_ntoa(node->payload.ipaddr));
  2405. break;
  2406. }
  2407. ast_free(tabs);
  2408. ast_term_color_code(&output, 0, 0);
  2409. ast_cli(fd, "%s", ast_str_buffer(output));
  2410. ast_free(output);
  2411. if (node->type == AST_DATA_CONTAINER) {
  2412. __data_result_print_cli(fd, node, depth + 1);
  2413. }
  2414. }
  2415. /*!
  2416. * \internal
  2417. * \brief Print out an ast_data tree to the CLI.
  2418. * \param[in] fd The CLI file descriptor.
  2419. * \param[in] root The root node of the tree.
  2420. * \param[in] depth Actual depth.
  2421. */
  2422. static void __data_result_print_cli(int fd, const struct ast_data *root, uint32_t depth)
  2423. {
  2424. struct ao2_iterator iter;
  2425. struct ast_data *node;
  2426. if (root->type == AST_DATA_CONTAINER) {
  2427. iter = ao2_iterator_init(root->children, 0);
  2428. while ((node = ao2_iterator_next(&iter))) {
  2429. data_result_print_cli_node(fd, node, depth + 1);
  2430. ao2_ref(node, -1);
  2431. }
  2432. ao2_iterator_destroy(&iter);
  2433. } else {
  2434. data_result_print_cli_node(fd, root, depth);
  2435. }
  2436. }
  2437. /*!
  2438. * \internal
  2439. * \brief
  2440. * \param[in] fd The CLI file descriptor.
  2441. * \param[in] root The root node of the tree.
  2442. */
  2443. static void data_result_print_cli(int fd, const struct ast_data *root)
  2444. {
  2445. ast_cli(fd, COLORIZE_FMT "\n", COLORIZE(data_result_get_color(root->type), 0, root->name));
  2446. __data_result_print_cli(fd, root, 0);
  2447. ast_cli(fd, "\n");
  2448. }
  2449. /*!
  2450. * \internal
  2451. * \brief Handle the CLI command "data get".
  2452. */
  2453. static char *handle_cli_data_get(struct ast_cli_entry *e, int cmd,
  2454. struct ast_cli_args *a)
  2455. {
  2456. struct ast_data_query query = {
  2457. .version = AST_DATA_QUERY_VERSION
  2458. };
  2459. struct ast_data *tree;
  2460. switch (cmd) {
  2461. case CLI_INIT:
  2462. e->command = "data get";
  2463. e->usage = ""
  2464. "Usage: data get <path> [<search> [<filter>]]\n"
  2465. " Get the tree based on a path.\n";
  2466. return NULL;
  2467. case CLI_GENERATE:
  2468. return NULL;
  2469. }
  2470. if (a->argc < e->args + 1) {
  2471. return CLI_SHOWUSAGE;
  2472. }
  2473. query.path = (char *) a->argv[e->args];
  2474. if (a->argc > e->args + 1) {
  2475. query.search = (char *) a->argv[e->args + 1];
  2476. }
  2477. if (a->argc > e->args + 2) {
  2478. query.filter = (char *) a->argv[e->args + 2];
  2479. }
  2480. tree = ast_data_get(&query);
  2481. if (!tree) {
  2482. return CLI_FAILURE;
  2483. }
  2484. data_result_print_cli(a->fd, tree);
  2485. ast_data_free(tree);
  2486. return CLI_SUCCESS;
  2487. }
  2488. /*!
  2489. * \internal
  2490. * \brief Print the list of data providers.
  2491. * \param[in] fd The CLI file descriptor.
  2492. * \param[in] name The last node visited name.
  2493. * \param[in] container The childrens of the last node.
  2494. * \param[in] path The path to the current node.
  2495. */
  2496. static void data_provider_print_cli(int fd, const char *name,
  2497. struct ao2_container *container, struct ast_str *path)
  2498. {
  2499. struct ao2_iterator i;
  2500. struct ast_str *current_path;
  2501. struct data_provider *provider;
  2502. current_path = ast_str_create(60);
  2503. if (!current_path) {
  2504. return;
  2505. }
  2506. ast_str_reset(current_path);
  2507. if (path) {
  2508. ast_str_set(&current_path, 0, "%s/%s", ast_str_buffer(path), name);
  2509. } else {
  2510. ast_str_set(&current_path, 0, "%s", name);
  2511. }
  2512. i = ao2_iterator_init(container, 0);
  2513. while ((provider = ao2_iterator_next(&i))) {
  2514. if (provider->handler) {
  2515. /* terminal node, print it. */
  2516. ast_cli(fd, "%s/%s (", ast_str_buffer(current_path),
  2517. provider->name);
  2518. if (provider->handler->get) {
  2519. ast_cli(fd, "get");
  2520. }
  2521. ast_cli(fd, ") [%s]\n", provider->registrar);
  2522. }
  2523. data_provider_print_cli(fd, provider->name, provider->children,
  2524. current_path);
  2525. ao2_ref(provider, -1);
  2526. }
  2527. ao2_iterator_destroy(&i);
  2528. ast_free(current_path);
  2529. }
  2530. /*!
  2531. * \internal
  2532. * \brief Handle CLI command "data show providers"
  2533. */
  2534. static char *handle_cli_data_show_providers(struct ast_cli_entry *e, int cmd,
  2535. struct ast_cli_args *a)
  2536. {
  2537. switch (cmd) {
  2538. case CLI_INIT:
  2539. e->command = "data show providers";
  2540. e->usage = ""
  2541. "Usage: data show providers\n"
  2542. " Show the list of registered providers\n";
  2543. return NULL;
  2544. case CLI_GENERATE:
  2545. return NULL;
  2546. }
  2547. data_read_lock();
  2548. data_provider_print_cli(a->fd, "", root_data.container, NULL);
  2549. data_unlock();
  2550. return CLI_SUCCESS;
  2551. }
  2552. /*!
  2553. * \internal
  2554. * \brief Data API CLI commands.
  2555. */
  2556. static struct ast_cli_entry cli_data[] = {
  2557. AST_CLI_DEFINE(handle_cli_data_get, "Data API get"),
  2558. AST_CLI_DEFINE(handle_cli_data_show_providers, "Show data providers")
  2559. };
  2560. /*!
  2561. * \internal
  2562. * \brief Output a tree to the AMI.
  2563. * \param[in] s AMI session.
  2564. * \param[in] name The root node name.
  2565. * \param[in] container The root container.
  2566. * \param[in] path The current path.
  2567. */
  2568. static void data_result_manager_output(struct mansession *s, const char *name,
  2569. struct ao2_container *container, struct ast_str *path, int id)
  2570. {
  2571. struct ao2_iterator i;
  2572. struct ast_str *current_path;
  2573. struct ast_data *node;
  2574. int current_id = id;
  2575. current_path = ast_str_create(60);
  2576. if (!current_path) {
  2577. return;
  2578. }
  2579. ast_str_reset(current_path);
  2580. if (path) {
  2581. ast_str_set(&current_path, 0, "%s.%s", ast_str_buffer(path), name);
  2582. } else {
  2583. ast_str_set(&current_path, 0, "%s", name);
  2584. }
  2585. i = ao2_iterator_init(container, 0);
  2586. while ((node = ao2_iterator_next(&i))) {
  2587. /* terminal node, print it. */
  2588. if (node->type != AST_DATA_CONTAINER) {
  2589. astman_append(s, "%d-%s.%s", id, ast_str_buffer(current_path),
  2590. node->name);
  2591. }
  2592. switch (node->type) {
  2593. case AST_DATA_CONTAINER:
  2594. data_result_manager_output(s, node->name, node->children, current_path, ++current_id);
  2595. break;
  2596. case AST_DATA_INTEGER:
  2597. astman_append(s, ": %d\r\n", node->payload.sint);
  2598. break;
  2599. case AST_DATA_TIMESTAMP:
  2600. case AST_DATA_SECONDS:
  2601. case AST_DATA_MILLISECONDS:
  2602. case AST_DATA_UNSIGNED_INTEGER:
  2603. astman_append(s, ": %u\r\n", node->payload.uint);
  2604. break;
  2605. case AST_DATA_PASSWORD:
  2606. astman_append(s, ": %s\r\n", node->payload.str);
  2607. break;
  2608. case AST_DATA_STRING:
  2609. astman_append(s, ": %s\r\n", node->payload.str);
  2610. break;
  2611. case AST_DATA_CHARACTER:
  2612. astman_append(s, ": %c\r\n", node->payload.character);
  2613. break;
  2614. case AST_DATA_IPADDR:
  2615. astman_append(s, ": %s\r\n", ast_inet_ntoa(node->payload.ipaddr));
  2616. break;
  2617. case AST_DATA_POINTER:
  2618. break;
  2619. case AST_DATA_DOUBLE:
  2620. astman_append(s, ": %f\r\n", node->payload.dbl);
  2621. break;
  2622. case AST_DATA_BOOLEAN:
  2623. astman_append(s, ": %s\r\n",
  2624. (node->payload.boolean ? "True" : "False"));
  2625. break;
  2626. }
  2627. ao2_ref(node, -1);
  2628. }
  2629. ao2_iterator_destroy(&i);
  2630. ast_free(current_path);
  2631. }
  2632. /*!
  2633. * \internal
  2634. * \brief Implements the manager action: "DataGet".
  2635. */
  2636. static int manager_data_get(struct mansession *s, const struct message *m)
  2637. {
  2638. const char *path = astman_get_header(m, "Path");
  2639. const char *search = astman_get_header(m, "Search");
  2640. const char *filter = astman_get_header(m, "Filter");
  2641. const char *id = astman_get_header(m, "ActionID");
  2642. struct ast_data *res;
  2643. struct ast_data_query query = {
  2644. .version = AST_DATA_QUERY_VERSION,
  2645. .path = (char *) path,
  2646. .search = (char *) search,
  2647. .filter = (char *) filter,
  2648. };
  2649. if (ast_strlen_zero(path)) {
  2650. astman_send_error(s, m, "'Path' parameter not specified");
  2651. return 0;
  2652. }
  2653. res = ast_data_get(&query);
  2654. if (!res) {
  2655. astman_send_error(s, m, "No data returned");
  2656. return 0;
  2657. }
  2658. astman_append(s, "Event: DataGet Tree\r\n");
  2659. if (!ast_strlen_zero(id)) {
  2660. astman_append(s, "ActionID: %s\r\n", id);
  2661. }
  2662. data_result_manager_output(s, res->name, res->children, NULL, 0);
  2663. astman_append(s, "\r\n");
  2664. ast_data_free(res);
  2665. return RESULT_SUCCESS;
  2666. }
  2667. static int data_add_codec(struct ast_data *codecs, struct ast_format *format) {
  2668. struct ast_data *codec;
  2669. struct ast_codec *tmp;
  2670. tmp = ast_codec_get_by_id(ast_format_get_codec_id(format));
  2671. if (!tmp) {
  2672. return -1;
  2673. }
  2674. codec = ast_data_add_node(codecs, "codec");
  2675. if (!codec) {
  2676. ao2_ref(tmp, -1);
  2677. return -1;
  2678. }
  2679. ast_data_add_str(codec, "name", tmp->name);
  2680. ast_data_add_int(codec, "samplespersecond", tmp->sample_rate);
  2681. ast_data_add_str(codec, "description", tmp->description);
  2682. ast_data_add_int(codec, "frame_length", tmp->minimum_bytes);
  2683. ao2_ref(tmp, -1);
  2684. return 0;
  2685. }
  2686. int ast_data_add_codec(struct ast_data *root, const char *node_name, struct ast_format *format)
  2687. {
  2688. struct ast_data *codecs;
  2689. codecs = ast_data_add_node(root, node_name);
  2690. if (!codecs) {
  2691. return -1;
  2692. }
  2693. return data_add_codec(codecs, format);
  2694. }
  2695. int ast_data_add_codecs(struct ast_data *root, const char *node_name, struct ast_format_cap *cap)
  2696. {
  2697. struct ast_data *codecs;
  2698. size_t i;
  2699. size_t count;
  2700. codecs = ast_data_add_node(root, node_name);
  2701. if (!codecs) {
  2702. return -1;
  2703. }
  2704. if (!cap) {
  2705. return 0;
  2706. }
  2707. count = ast_format_cap_count(cap);
  2708. for (i = 0; i < count; ++i) {
  2709. struct ast_format *fmt;
  2710. fmt = ast_format_cap_get_format(cap, i);
  2711. if (!fmt) {
  2712. return -1;
  2713. }
  2714. if (data_add_codec(codecs, fmt)) {
  2715. ao2_ref(fmt, -1);
  2716. return -1;
  2717. }
  2718. ao2_ref(fmt, -1);
  2719. }
  2720. return 0;
  2721. }
  2722. #ifdef TEST_FRAMEWORK
  2723. /*!
  2724. * \internal
  2725. * \brief Structure used to test how to add a complete structure,
  2726. * and how to compare it.
  2727. */
  2728. struct test_structure {
  2729. int a_int;
  2730. unsigned int b_bool:1;
  2731. char *c_str;
  2732. unsigned int a_uint;
  2733. };
  2734. /*!
  2735. * \internal
  2736. * \brief test_structure mapping.
  2737. */
  2738. #define DATA_EXPORT_TEST_STRUCTURE(MEMBER) \
  2739. MEMBER(test_structure, a_int, AST_DATA_INTEGER) \
  2740. MEMBER(test_structure, b_bool, AST_DATA_BOOLEAN) \
  2741. MEMBER(test_structure, c_str, AST_DATA_STRING) \
  2742. MEMBER(test_structure, a_uint, AST_DATA_UNSIGNED_INTEGER)
  2743. AST_DATA_STRUCTURE(test_structure, DATA_EXPORT_TEST_STRUCTURE);
  2744. /*!
  2745. * \internal
  2746. * \brief Callback implementation.
  2747. */
  2748. static int test_data_full_provider(const struct ast_data_search *search,
  2749. struct ast_data *root)
  2750. {
  2751. struct ast_data *test_structure;
  2752. struct test_structure local_test_structure = {
  2753. .a_int = 10,
  2754. .b_bool = 1,
  2755. .c_str = "test string",
  2756. .a_uint = 20
  2757. };
  2758. test_structure = ast_data_add_node(root, "test_structure");
  2759. if (!test_structure) {
  2760. ast_debug(1, "Internal data api error\n");
  2761. return 0;
  2762. }
  2763. /* add the complete structure. */
  2764. ast_data_add_structure(test_structure, test_structure, &local_test_structure);
  2765. if (!ast_data_search_match(search, test_structure)) {
  2766. ast_data_remove_node(root, test_structure);
  2767. }
  2768. return 0;
  2769. }
  2770. /*!
  2771. * \internal
  2772. * \brief Handler definition for the full provider.
  2773. */
  2774. static const struct ast_data_handler full_provider = {
  2775. .version = AST_DATA_HANDLER_VERSION,
  2776. .get = test_data_full_provider
  2777. };
  2778. /*!
  2779. * \internal
  2780. * \brief Structure used to define multiple providers at once.
  2781. */
  2782. static const struct ast_data_entry test_providers[] = {
  2783. AST_DATA_ENTRY("test/node1/node11/node111", &full_provider)
  2784. };
  2785. AST_TEST_DEFINE(test_data_get)
  2786. {
  2787. struct ast_data *res, *node;
  2788. struct ast_data_iterator *i;
  2789. struct ast_data_query query = {
  2790. .version = AST_DATA_QUERY_VERSION,
  2791. .path = "test/node1/node11/node111",
  2792. .search = "node111/test_structure/a_int=10",
  2793. .filter = "node111/test_structure/a*int"
  2794. };
  2795. switch (cmd) {
  2796. case TEST_INIT:
  2797. info->name = "data_test";
  2798. info->category = "/main/data/";
  2799. info->summary = "Data API unit test";
  2800. info->description =
  2801. "Tests whether data API get implementation works as expected.";
  2802. return AST_TEST_NOT_RUN;
  2803. case TEST_EXECUTE:
  2804. break;
  2805. }
  2806. ast_data_register_multiple_core(test_providers, ARRAY_LEN(test_providers));
  2807. res = ast_data_get(&query);
  2808. if (!res) {
  2809. ast_test_status_update(test, "Unable to get tree.");
  2810. ast_data_unregister("test/node1/node11/node111");
  2811. return AST_TEST_FAIL;
  2812. }
  2813. /* initiate the iterator and check for errors. */
  2814. i = ast_data_iterator_init(res, "test_structure/");
  2815. if (!i) {
  2816. ast_test_status_update(test, "Unable to initiate the iterator.");
  2817. ast_data_free(res);
  2818. ast_data_unregister("test/node1/node11/node111");
  2819. return AST_TEST_FAIL;
  2820. }
  2821. /* walk the returned nodes. */
  2822. while ((node = ast_data_iterator_next(i))) {
  2823. if (!strcmp(ast_data_retrieve_name(node), "a_int")) {
  2824. if (ast_data_retrieve_int(node, "/") != 10) {
  2825. ast_data_iterator_end(i);
  2826. ast_data_free(res);
  2827. ast_data_unregister("test/node1/node11/node111");
  2828. return AST_TEST_FAIL;
  2829. }
  2830. } else if (!strcmp(ast_data_retrieve_name(node), "a_uint")) {
  2831. if (ast_data_retrieve_uint(node, "/") != 20) {
  2832. ast_data_iterator_end(i);
  2833. ast_data_free(res);
  2834. ast_data_unregister("test/node1/node11/node111");
  2835. return AST_TEST_FAIL;
  2836. }
  2837. }
  2838. }
  2839. /* finish the iterator. */
  2840. ast_data_iterator_end(i);
  2841. ast_data_free(res);
  2842. ast_data_unregister("test/node1/node11/node111");
  2843. return AST_TEST_PASS;
  2844. }
  2845. #endif
  2846. /*!
  2847. * \internal
  2848. * \brief Clean up resources on Asterisk shutdown
  2849. */
  2850. static void data_shutdown(void)
  2851. {
  2852. ast_manager_unregister("DataGet");
  2853. ast_cli_unregister_multiple(cli_data, ARRAY_LEN(cli_data));
  2854. ao2_t_ref(root_data.container, -1, "Unref root_data.container in data_shutdown");
  2855. root_data.container = NULL;
  2856. ast_rwlock_destroy(&root_data.lock);
  2857. AST_TEST_UNREGISTER(test_data_get);
  2858. }
  2859. int ast_data_init(void)
  2860. {
  2861. int res = 0;
  2862. ast_rwlock_init(&root_data.lock);
  2863. root_data.container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  2864. NUM_DATA_NODE_BUCKETS, data_provider_hash, NULL, data_provider_cmp);
  2865. if (!root_data.container) {
  2866. return -1;
  2867. }
  2868. res |= ast_cli_register_multiple(cli_data, ARRAY_LEN(cli_data));
  2869. res |= ast_manager_register_xml_core("DataGet", 0, manager_data_get);
  2870. AST_TEST_REGISTER(test_data_get);
  2871. ast_register_cleanup(data_shutdown);
  2872. return res;
  2873. }