data.c 81 KB

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