dbus_dict_helpers.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*
  2. * WPA Supplicant / dbus-based control interface
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include <dbus/dbus.h>
  16. #include "common.h"
  17. #include "wpabuf.h"
  18. #include "dbus_dict_helpers.h"
  19. /**
  20. * Start a dict in a dbus message. Should be paired with a call to
  21. * wpa_dbus_dict_close_write().
  22. *
  23. * @param iter A valid dbus message iterator
  24. * @param iter_dict (out) A dict iterator to pass to further dict functions
  25. * @return TRUE on success, FALSE on failure
  26. *
  27. */
  28. dbus_bool_t wpa_dbus_dict_open_write(DBusMessageIter *iter,
  29. DBusMessageIter *iter_dict)
  30. {
  31. dbus_bool_t result;
  32. if (!iter || !iter_dict)
  33. return FALSE;
  34. result = dbus_message_iter_open_container(
  35. iter,
  36. DBUS_TYPE_ARRAY,
  37. DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
  38. DBUS_TYPE_STRING_AS_STRING
  39. DBUS_TYPE_VARIANT_AS_STRING
  40. DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
  41. iter_dict);
  42. return result;
  43. }
  44. /**
  45. * End a dict element in a dbus message. Should be paired with
  46. * a call to wpa_dbus_dict_open_write().
  47. *
  48. * @param iter valid dbus message iterator, same as passed to
  49. * wpa_dbus_dict_open_write()
  50. * @param iter_dict a dbus dict iterator returned from
  51. * wpa_dbus_dict_open_write()
  52. * @return TRUE on success, FALSE on failure
  53. *
  54. */
  55. dbus_bool_t wpa_dbus_dict_close_write(DBusMessageIter *iter,
  56. DBusMessageIter *iter_dict)
  57. {
  58. if (!iter || !iter_dict)
  59. return FALSE;
  60. return dbus_message_iter_close_container(iter, iter_dict);
  61. }
  62. const char * wpa_dbus_type_as_string(const int type)
  63. {
  64. switch(type) {
  65. case DBUS_TYPE_BYTE:
  66. return DBUS_TYPE_BYTE_AS_STRING;
  67. case DBUS_TYPE_BOOLEAN:
  68. return DBUS_TYPE_BOOLEAN_AS_STRING;
  69. case DBUS_TYPE_INT16:
  70. return DBUS_TYPE_INT16_AS_STRING;
  71. case DBUS_TYPE_UINT16:
  72. return DBUS_TYPE_UINT16_AS_STRING;
  73. case DBUS_TYPE_INT32:
  74. return DBUS_TYPE_INT32_AS_STRING;
  75. case DBUS_TYPE_UINT32:
  76. return DBUS_TYPE_UINT32_AS_STRING;
  77. case DBUS_TYPE_INT64:
  78. return DBUS_TYPE_INT64_AS_STRING;
  79. case DBUS_TYPE_UINT64:
  80. return DBUS_TYPE_UINT64_AS_STRING;
  81. case DBUS_TYPE_DOUBLE:
  82. return DBUS_TYPE_DOUBLE_AS_STRING;
  83. case DBUS_TYPE_STRING:
  84. return DBUS_TYPE_STRING_AS_STRING;
  85. case DBUS_TYPE_OBJECT_PATH:
  86. return DBUS_TYPE_OBJECT_PATH_AS_STRING;
  87. case DBUS_TYPE_ARRAY:
  88. return DBUS_TYPE_ARRAY_AS_STRING;
  89. default:
  90. return NULL;
  91. }
  92. }
  93. static dbus_bool_t _wpa_dbus_add_dict_entry_start(
  94. DBusMessageIter *iter_dict, DBusMessageIter *iter_dict_entry,
  95. const char *key, const int value_type)
  96. {
  97. if (!dbus_message_iter_open_container(iter_dict,
  98. DBUS_TYPE_DICT_ENTRY, NULL,
  99. iter_dict_entry))
  100. return FALSE;
  101. if (!dbus_message_iter_append_basic(iter_dict_entry, DBUS_TYPE_STRING,
  102. &key))
  103. return FALSE;
  104. return TRUE;
  105. }
  106. static dbus_bool_t _wpa_dbus_add_dict_entry_end(
  107. DBusMessageIter *iter_dict, DBusMessageIter *iter_dict_entry,
  108. DBusMessageIter *iter_dict_val)
  109. {
  110. if (!dbus_message_iter_close_container(iter_dict_entry, iter_dict_val))
  111. return FALSE;
  112. if (!dbus_message_iter_close_container(iter_dict, iter_dict_entry))
  113. return FALSE;
  114. return TRUE;
  115. }
  116. static dbus_bool_t _wpa_dbus_add_dict_entry_basic(DBusMessageIter *iter_dict,
  117. const char *key,
  118. const int value_type,
  119. const void *value)
  120. {
  121. DBusMessageIter iter_dict_entry, iter_dict_val;
  122. const char *type_as_string = NULL;
  123. if (key == NULL)
  124. return FALSE;
  125. type_as_string = wpa_dbus_type_as_string(value_type);
  126. if (!type_as_string)
  127. return FALSE;
  128. if (!_wpa_dbus_add_dict_entry_start(iter_dict, &iter_dict_entry,
  129. key, value_type))
  130. return FALSE;
  131. if (!dbus_message_iter_open_container(&iter_dict_entry,
  132. DBUS_TYPE_VARIANT,
  133. type_as_string, &iter_dict_val))
  134. return FALSE;
  135. if (!dbus_message_iter_append_basic(&iter_dict_val, value_type, value))
  136. return FALSE;
  137. if (!_wpa_dbus_add_dict_entry_end(iter_dict, &iter_dict_entry,
  138. &iter_dict_val))
  139. return FALSE;
  140. return TRUE;
  141. }
  142. static dbus_bool_t _wpa_dbus_add_dict_entry_byte_array(
  143. DBusMessageIter *iter_dict, const char *key,
  144. const char *value, const dbus_uint32_t value_len)
  145. {
  146. DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
  147. dbus_uint32_t i;
  148. if (!_wpa_dbus_add_dict_entry_start(iter_dict, &iter_dict_entry,
  149. key, DBUS_TYPE_ARRAY))
  150. return FALSE;
  151. if (!dbus_message_iter_open_container(&iter_dict_entry,
  152. DBUS_TYPE_VARIANT,
  153. DBUS_TYPE_ARRAY_AS_STRING
  154. DBUS_TYPE_BYTE_AS_STRING,
  155. &iter_dict_val))
  156. return FALSE;
  157. if (!dbus_message_iter_open_container(&iter_dict_val, DBUS_TYPE_ARRAY,
  158. DBUS_TYPE_BYTE_AS_STRING,
  159. &iter_array))
  160. return FALSE;
  161. for (i = 0; i < value_len; i++) {
  162. if (!dbus_message_iter_append_basic(&iter_array,
  163. DBUS_TYPE_BYTE,
  164. &(value[i])))
  165. return FALSE;
  166. }
  167. if (!dbus_message_iter_close_container(&iter_dict_val, &iter_array))
  168. return FALSE;
  169. if (!_wpa_dbus_add_dict_entry_end(iter_dict, &iter_dict_entry,
  170. &iter_dict_val))
  171. return FALSE;
  172. return TRUE;
  173. }
  174. /**
  175. * Add a string entry to the dict.
  176. *
  177. * @param iter_dict A valid DBusMessageIter returned from
  178. * wpa_dbus_dict_open_write()
  179. * @param key The key of the dict item
  180. * @param value The string value
  181. * @return TRUE on success, FALSE on failure
  182. *
  183. */
  184. dbus_bool_t wpa_dbus_dict_append_string(DBusMessageIter *iter_dict,
  185. const char *key, const char *value)
  186. {
  187. if (!value)
  188. return FALSE;
  189. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_STRING,
  190. &value);
  191. }
  192. /**
  193. * Add a byte entry to the dict.
  194. *
  195. * @param iter_dict A valid DBusMessageIter returned from
  196. * wpa_dbus_dict_open_write()
  197. * @param key The key of the dict item
  198. * @param value The byte value
  199. * @return TRUE on success, FALSE on failure
  200. *
  201. */
  202. dbus_bool_t wpa_dbus_dict_append_byte(DBusMessageIter *iter_dict,
  203. const char *key, const char value)
  204. {
  205. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_BYTE,
  206. &value);
  207. }
  208. /**
  209. * Add a boolean entry to the dict.
  210. *
  211. * @param iter_dict A valid DBusMessageIter returned from
  212. * wpa_dbus_dict_open_write()
  213. * @param key The key of the dict item
  214. * @param value The boolean value
  215. * @return TRUE on success, FALSE on failure
  216. *
  217. */
  218. dbus_bool_t wpa_dbus_dict_append_bool(DBusMessageIter *iter_dict,
  219. const char *key, const dbus_bool_t value)
  220. {
  221. return _wpa_dbus_add_dict_entry_basic(iter_dict, key,
  222. DBUS_TYPE_BOOLEAN, &value);
  223. }
  224. /**
  225. * Add a 16-bit signed integer entry to the dict.
  226. *
  227. * @param iter_dict A valid DBusMessageIter returned from
  228. * wpa_dbus_dict_open_write()
  229. * @param key The key of the dict item
  230. * @param value The 16-bit signed integer value
  231. * @return TRUE on success, FALSE on failure
  232. *
  233. */
  234. dbus_bool_t wpa_dbus_dict_append_int16(DBusMessageIter *iter_dict,
  235. const char *key,
  236. const dbus_int16_t value)
  237. {
  238. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_INT16,
  239. &value);
  240. }
  241. /**
  242. * Add a 16-bit unsigned integer entry to the dict.
  243. *
  244. * @param iter_dict A valid DBusMessageIter returned from
  245. * wpa_dbus_dict_open_write()
  246. * @param key The key of the dict item
  247. * @param value The 16-bit unsigned integer value
  248. * @return TRUE on success, FALSE on failure
  249. *
  250. */
  251. dbus_bool_t wpa_dbus_dict_append_uint16(DBusMessageIter *iter_dict,
  252. const char *key,
  253. const dbus_uint16_t value)
  254. {
  255. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_UINT16,
  256. &value);
  257. }
  258. /**
  259. * Add a 32-bit signed integer to the dict.
  260. *
  261. * @param iter_dict A valid DBusMessageIter returned from
  262. * wpa_dbus_dict_open_write()
  263. * @param key The key of the dict item
  264. * @param value The 32-bit signed integer value
  265. * @return TRUE on success, FALSE on failure
  266. *
  267. */
  268. dbus_bool_t wpa_dbus_dict_append_int32(DBusMessageIter *iter_dict,
  269. const char *key,
  270. const dbus_int32_t value)
  271. {
  272. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_INT32,
  273. &value);
  274. }
  275. /**
  276. * Add a 32-bit unsigned integer entry to the dict.
  277. *
  278. * @param iter_dict A valid DBusMessageIter returned from
  279. * wpa_dbus_dict_open_write()
  280. * @param key The key of the dict item
  281. * @param value The 32-bit unsigned integer value
  282. * @return TRUE on success, FALSE on failure
  283. *
  284. */
  285. dbus_bool_t wpa_dbus_dict_append_uint32(DBusMessageIter *iter_dict,
  286. const char *key,
  287. const dbus_uint32_t value)
  288. {
  289. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_UINT32,
  290. &value);
  291. }
  292. /**
  293. * Add a 64-bit integer entry to the dict.
  294. *
  295. * @param iter_dict A valid DBusMessageIter returned from
  296. * wpa_dbus_dict_open_write()
  297. * @param key The key of the dict item
  298. * @param value The 64-bit integer value
  299. * @return TRUE on success, FALSE on failure
  300. *
  301. */
  302. dbus_bool_t wpa_dbus_dict_append_int64(DBusMessageIter *iter_dict,
  303. const char *key,
  304. const dbus_int64_t value)
  305. {
  306. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_INT64,
  307. &value);
  308. }
  309. /**
  310. * Add a 64-bit unsigned integer entry to the dict.
  311. *
  312. * @param iter_dict A valid DBusMessageIter returned from
  313. * wpa_dbus_dict_open_write()
  314. * @param key The key of the dict item
  315. * @param value The 64-bit unsigned integer value
  316. * @return TRUE on success, FALSE on failure
  317. *
  318. */
  319. dbus_bool_t wpa_dbus_dict_append_uint64(DBusMessageIter *iter_dict,
  320. const char *key,
  321. const dbus_uint64_t value)
  322. {
  323. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_UINT64,
  324. &value);
  325. }
  326. /**
  327. * Add a double-precision floating point entry to the dict.
  328. *
  329. * @param iter_dict A valid DBusMessageIter returned from
  330. * wpa_dbus_dict_open_write()
  331. * @param key The key of the dict item
  332. * @param value The double-precision floating point value
  333. * @return TRUE on success, FALSE on failure
  334. *
  335. */
  336. dbus_bool_t wpa_dbus_dict_append_double(DBusMessageIter *iter_dict,
  337. const char *key, const double value)
  338. {
  339. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_DOUBLE,
  340. &value);
  341. }
  342. /**
  343. * Add a DBus object path entry to the dict.
  344. *
  345. * @param iter_dict A valid DBusMessageIter returned from
  346. * wpa_dbus_dict_open_write()
  347. * @param key The key of the dict item
  348. * @param value The DBus object path value
  349. * @return TRUE on success, FALSE on failure
  350. *
  351. */
  352. dbus_bool_t wpa_dbus_dict_append_object_path(DBusMessageIter *iter_dict,
  353. const char *key,
  354. const char *value)
  355. {
  356. if (!value)
  357. return FALSE;
  358. return _wpa_dbus_add_dict_entry_basic(iter_dict, key,
  359. DBUS_TYPE_OBJECT_PATH, &value);
  360. }
  361. /**
  362. * Add a byte array entry to the dict.
  363. *
  364. * @param iter_dict A valid DBusMessageIter returned from
  365. * wpa_dbus_dict_open_write()
  366. * @param key The key of the dict item
  367. * @param value The byte array
  368. * @param value_len The length of the byte array, in bytes
  369. * @return TRUE on success, FALSE on failure
  370. *
  371. */
  372. dbus_bool_t wpa_dbus_dict_append_byte_array(DBusMessageIter *iter_dict,
  373. const char *key,
  374. const char *value,
  375. const dbus_uint32_t value_len)
  376. {
  377. if (!key)
  378. return FALSE;
  379. if (!value && (value_len != 0))
  380. return FALSE;
  381. return _wpa_dbus_add_dict_entry_byte_array(iter_dict, key, value,
  382. value_len);
  383. }
  384. /**
  385. * Begin an array entry in the dict
  386. *
  387. * @param iter_dict A valid DBusMessageIter returned from
  388. * wpa_dbus_dict_open_write()
  389. * @param key The key of the dict item
  390. * @param type The type of the contained data
  391. * @param iter_dict_entry A private DBusMessageIter provided by the caller to
  392. * be passed to wpa_dbus_dict_end_string_array()
  393. * @param iter_dict_val A private DBusMessageIter provided by the caller to
  394. * be passed to wpa_dbus_dict_end_string_array()
  395. * @param iter_array On return, the DBusMessageIter to be passed to
  396. * wpa_dbus_dict_string_array_add_element()
  397. * @return TRUE on success, FALSE on failure
  398. *
  399. */
  400. dbus_bool_t wpa_dbus_dict_begin_array(DBusMessageIter *iter_dict,
  401. const char *key, const char *type,
  402. DBusMessageIter *iter_dict_entry,
  403. DBusMessageIter *iter_dict_val,
  404. DBusMessageIter *iter_array)
  405. {
  406. char array_type[10];
  407. int err;
  408. err = os_snprintf(array_type, sizeof(array_type),
  409. DBUS_TYPE_ARRAY_AS_STRING "%s",
  410. type);
  411. if (err < 0 || err > (int) sizeof(array_type))
  412. return FALSE;
  413. if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array)
  414. return FALSE;
  415. if (!_wpa_dbus_add_dict_entry_start(iter_dict, iter_dict_entry,
  416. key, DBUS_TYPE_ARRAY))
  417. return FALSE;
  418. if (!dbus_message_iter_open_container(iter_dict_entry,
  419. DBUS_TYPE_VARIANT,
  420. array_type,
  421. iter_dict_val))
  422. return FALSE;
  423. if (!dbus_message_iter_open_container(iter_dict_val, DBUS_TYPE_ARRAY,
  424. type, iter_array))
  425. return FALSE;
  426. return TRUE;
  427. }
  428. dbus_bool_t wpa_dbus_dict_begin_string_array(DBusMessageIter *iter_dict,
  429. const char *key,
  430. DBusMessageIter *iter_dict_entry,
  431. DBusMessageIter *iter_dict_val,
  432. DBusMessageIter *iter_array)
  433. {
  434. return wpa_dbus_dict_begin_array(
  435. iter_dict, key,
  436. DBUS_TYPE_STRING_AS_STRING,
  437. iter_dict_entry, iter_dict_val, iter_array);
  438. }
  439. /**
  440. * Add a single string element to a string array dict entry
  441. *
  442. * @param iter_array A valid DBusMessageIter returned from
  443. * wpa_dbus_dict_begin_string_array()'s
  444. * iter_array parameter
  445. * @param elem The string element to be added to the dict entry's string array
  446. * @return TRUE on success, FALSE on failure
  447. *
  448. */
  449. dbus_bool_t wpa_dbus_dict_string_array_add_element(DBusMessageIter *iter_array,
  450. const char *elem)
  451. {
  452. if (!iter_array || !elem)
  453. return FALSE;
  454. return dbus_message_iter_append_basic(iter_array, DBUS_TYPE_STRING,
  455. &elem);
  456. }
  457. /**
  458. * Add a single byte array element to a string array dict entry
  459. *
  460. * @param iter_array A valid DBusMessageIter returned from
  461. * wpa_dbus_dict_begin_array()'s iter_array
  462. * parameter -- note that wpa_dbus_dict_begin_array()
  463. * must have been called with "ay" as the type
  464. * @param value The data to be added to the dict entry's array
  465. * @param value_len The length of the data
  466. * @return TRUE on success, FALSE on failure
  467. *
  468. */
  469. dbus_bool_t wpa_dbus_dict_bin_array_add_element(DBusMessageIter *iter_array,
  470. const u8 *value,
  471. size_t value_len)
  472. {
  473. DBusMessageIter iter_bytes;
  474. size_t i;
  475. if (!iter_array || !value)
  476. return FALSE;
  477. if (!dbus_message_iter_open_container(iter_array, DBUS_TYPE_ARRAY,
  478. DBUS_TYPE_BYTE_AS_STRING,
  479. &iter_bytes))
  480. return FALSE;
  481. for (i = 0; i < value_len; i++) {
  482. if (!dbus_message_iter_append_basic(&iter_bytes,
  483. DBUS_TYPE_BYTE,
  484. &(value[i])))
  485. return FALSE;
  486. }
  487. if (!dbus_message_iter_close_container(iter_array, &iter_bytes))
  488. return FALSE;
  489. return TRUE;
  490. }
  491. /**
  492. * End an array dict entry
  493. *
  494. * @param iter_dict A valid DBusMessageIter returned from
  495. * wpa_dbus_dict_open_write()
  496. * @param iter_dict_entry A private DBusMessageIter returned from
  497. * wpa_dbus_dict_begin_string_array() or
  498. * wpa_dbus_dict_begin_array()
  499. * @param iter_dict_val A private DBusMessageIter returned from
  500. * wpa_dbus_dict_begin_string_array() or
  501. * wpa_dbus_dict_begin_array()
  502. * @param iter_array A DBusMessageIter returned from
  503. * wpa_dbus_dict_begin_string_array() or
  504. * wpa_dbus_dict_begin_array()
  505. * @return TRUE on success, FALSE on failure
  506. *
  507. */
  508. dbus_bool_t wpa_dbus_dict_end_array(DBusMessageIter *iter_dict,
  509. DBusMessageIter *iter_dict_entry,
  510. DBusMessageIter *iter_dict_val,
  511. DBusMessageIter *iter_array)
  512. {
  513. if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array)
  514. return FALSE;
  515. if (!dbus_message_iter_close_container(iter_dict_val, iter_array))
  516. return FALSE;
  517. if (!_wpa_dbus_add_dict_entry_end(iter_dict, iter_dict_entry,
  518. iter_dict_val))
  519. return FALSE;
  520. return TRUE;
  521. }
  522. /**
  523. * Convenience function to add an entire string array to the dict.
  524. *
  525. * @param iter_dict A valid DBusMessageIter returned from
  526. * wpa_dbus_dict_open_write()
  527. * @param key The key of the dict item
  528. * @param items The array of strings
  529. * @param num_items The number of strings in the array
  530. * @return TRUE on success, FALSE on failure
  531. *
  532. */
  533. dbus_bool_t wpa_dbus_dict_append_string_array(DBusMessageIter *iter_dict,
  534. const char *key,
  535. const char **items,
  536. const dbus_uint32_t num_items)
  537. {
  538. DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
  539. dbus_uint32_t i;
  540. if (!key)
  541. return FALSE;
  542. if (!items && (num_items != 0))
  543. return FALSE;
  544. if (!wpa_dbus_dict_begin_string_array(iter_dict, key,
  545. &iter_dict_entry, &iter_dict_val,
  546. &iter_array))
  547. return FALSE;
  548. for (i = 0; i < num_items; i++) {
  549. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  550. items[i]))
  551. return FALSE;
  552. }
  553. if (!wpa_dbus_dict_end_string_array(iter_dict, &iter_dict_entry,
  554. &iter_dict_val, &iter_array))
  555. return FALSE;
  556. return TRUE;
  557. }
  558. /**
  559. * Convenience function to add an wpabuf binary array to the dict.
  560. *
  561. * @param iter_dict A valid DBusMessageIter returned from
  562. * wpa_dbus_dict_open_write()
  563. * @param key The key of the dict item
  564. * @param items The array of wpabuf structures
  565. * @param num_items The number of strings in the array
  566. * @return TRUE on success, FALSE on failure
  567. *
  568. */
  569. dbus_bool_t wpa_dbus_dict_append_wpabuf_array(DBusMessageIter *iter_dict,
  570. const char *key,
  571. const struct wpabuf **items,
  572. const dbus_uint32_t num_items)
  573. {
  574. DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
  575. dbus_uint32_t i;
  576. if (!key)
  577. return FALSE;
  578. if (!items && (num_items != 0))
  579. return FALSE;
  580. if (!wpa_dbus_dict_begin_array(iter_dict, key,
  581. DBUS_TYPE_ARRAY_AS_STRING
  582. DBUS_TYPE_BYTE_AS_STRING,
  583. &iter_dict_entry, &iter_dict_val,
  584. &iter_array))
  585. return FALSE;
  586. for (i = 0; i < num_items; i++) {
  587. if (!wpa_dbus_dict_bin_array_add_element(&iter_array,
  588. wpabuf_head(items[i]),
  589. wpabuf_len(items[i])))
  590. return FALSE;
  591. }
  592. if (!wpa_dbus_dict_end_array(iter_dict, &iter_dict_entry,
  593. &iter_dict_val, &iter_array))
  594. return FALSE;
  595. return TRUE;
  596. }
  597. /*****************************************************/
  598. /* Stuff for reading dicts */
  599. /*****************************************************/
  600. /**
  601. * Start reading from a dbus dict.
  602. *
  603. * @param iter A valid DBusMessageIter pointing to the start of the dict
  604. * @param iter_dict (out) A DBusMessageIter to be passed to
  605. * wpa_dbus_dict_read_next_entry()
  606. * @return TRUE on success, FALSE on failure
  607. *
  608. */
  609. dbus_bool_t wpa_dbus_dict_open_read(DBusMessageIter *iter,
  610. DBusMessageIter *iter_dict)
  611. {
  612. if (!iter || !iter_dict)
  613. return FALSE;
  614. if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
  615. dbus_message_iter_get_element_type(iter) != DBUS_TYPE_DICT_ENTRY)
  616. return FALSE;
  617. dbus_message_iter_recurse(iter, iter_dict);
  618. return TRUE;
  619. }
  620. #define BYTE_ARRAY_CHUNK_SIZE 34
  621. #define BYTE_ARRAY_ITEM_SIZE (sizeof(char))
  622. static dbus_bool_t _wpa_dbus_dict_entry_get_byte_array(
  623. DBusMessageIter *iter, struct wpa_dbus_dict_entry *entry)
  624. {
  625. dbus_uint32_t count = 0;
  626. dbus_bool_t success = FALSE;
  627. char *buffer, *nbuffer;;
  628. entry->bytearray_value = NULL;
  629. entry->array_type = DBUS_TYPE_BYTE;
  630. buffer = os_zalloc(BYTE_ARRAY_ITEM_SIZE * BYTE_ARRAY_CHUNK_SIZE);
  631. if (!buffer)
  632. return FALSE;
  633. entry->bytearray_value = buffer;
  634. entry->array_len = 0;
  635. while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_BYTE) {
  636. char byte;
  637. if ((count % BYTE_ARRAY_CHUNK_SIZE) == 0 && count != 0) {
  638. nbuffer = os_realloc(buffer, BYTE_ARRAY_ITEM_SIZE *
  639. (count + BYTE_ARRAY_CHUNK_SIZE));
  640. if (nbuffer == NULL) {
  641. os_free(buffer);
  642. wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_"
  643. "entry_get_byte_array out of "
  644. "memory trying to retrieve the "
  645. "string array");
  646. goto done;
  647. }
  648. buffer = nbuffer;
  649. }
  650. entry->bytearray_value = buffer;
  651. dbus_message_iter_get_basic(iter, &byte);
  652. entry->bytearray_value[count] = byte;
  653. entry->array_len = ++count;
  654. dbus_message_iter_next(iter);
  655. }
  656. /* Zero-length arrays are valid. */
  657. if (entry->array_len == 0) {
  658. os_free(entry->bytearray_value);
  659. entry->bytearray_value = NULL;
  660. }
  661. success = TRUE;
  662. done:
  663. return success;
  664. }
  665. #define STR_ARRAY_CHUNK_SIZE 8
  666. #define STR_ARRAY_ITEM_SIZE (sizeof(char *))
  667. static dbus_bool_t _wpa_dbus_dict_entry_get_string_array(
  668. DBusMessageIter *iter, int array_type,
  669. struct wpa_dbus_dict_entry *entry)
  670. {
  671. dbus_uint32_t count = 0;
  672. dbus_bool_t success = FALSE;
  673. char **buffer, **nbuffer;
  674. entry->strarray_value = NULL;
  675. entry->array_type = DBUS_TYPE_STRING;
  676. buffer = os_zalloc(STR_ARRAY_ITEM_SIZE * STR_ARRAY_CHUNK_SIZE);
  677. if (buffer == NULL)
  678. return FALSE;
  679. entry->strarray_value = buffer;
  680. entry->array_len = 0;
  681. while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
  682. const char *value;
  683. char *str;
  684. if ((count % STR_ARRAY_CHUNK_SIZE) == 0 && count != 0) {
  685. nbuffer = os_realloc(buffer, STR_ARRAY_ITEM_SIZE *
  686. (count + STR_ARRAY_CHUNK_SIZE));
  687. if (nbuffer == NULL) {
  688. os_free(buffer);
  689. wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_"
  690. "entry_get_string_array out of "
  691. "memory trying to retrieve the "
  692. "string array");
  693. goto done;
  694. }
  695. buffer = nbuffer;
  696. }
  697. entry->strarray_value = buffer;
  698. dbus_message_iter_get_basic(iter, &value);
  699. str = os_strdup(value);
  700. if (str == NULL) {
  701. wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_entry_get_"
  702. "string_array out of memory trying to "
  703. "duplicate the string array");
  704. goto done;
  705. }
  706. entry->strarray_value[count] = str;
  707. entry->array_len = ++count;
  708. dbus_message_iter_next(iter);
  709. }
  710. /* Zero-length arrays are valid. */
  711. if (entry->array_len == 0) {
  712. os_free(entry->strarray_value);
  713. entry->strarray_value = NULL;
  714. }
  715. success = TRUE;
  716. done:
  717. return success;
  718. }
  719. #define BIN_ARRAY_CHUNK_SIZE 10
  720. #define BIN_ARRAY_ITEM_SIZE (sizeof(struct wpabuf *))
  721. static dbus_bool_t _wpa_dbus_dict_entry_get_binarray(
  722. DBusMessageIter *iter, struct wpa_dbus_dict_entry *entry)
  723. {
  724. struct wpa_dbus_dict_entry tmpentry;
  725. size_t buflen = 0;
  726. int i;
  727. if (dbus_message_iter_get_element_type(iter) != DBUS_TYPE_BYTE)
  728. return FALSE;
  729. entry->array_type = WPAS_DBUS_TYPE_BINARRAY;
  730. entry->array_len = 0;
  731. entry->binarray_value = NULL;
  732. while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_ARRAY) {
  733. DBusMessageIter iter_array;
  734. if (entry->array_len == buflen) {
  735. struct wpabuf **newbuf;
  736. buflen += BIN_ARRAY_CHUNK_SIZE;
  737. newbuf = os_realloc(entry->binarray_value,
  738. buflen * BIN_ARRAY_ITEM_SIZE);
  739. if (!newbuf)
  740. goto cleanup;
  741. entry->binarray_value = newbuf;
  742. }
  743. dbus_message_iter_recurse(iter, &iter_array);
  744. if (_wpa_dbus_dict_entry_get_byte_array(&iter_array, &tmpentry)
  745. == FALSE)
  746. goto cleanup;
  747. entry->binarray_value[entry->array_len] =
  748. wpabuf_alloc_ext_data((u8 *) tmpentry.bytearray_value,
  749. tmpentry.array_len);
  750. if (entry->binarray_value[entry->array_len] == NULL) {
  751. wpa_dbus_dict_entry_clear(&tmpentry);
  752. goto cleanup;
  753. }
  754. entry->array_len++;
  755. dbus_message_iter_next(iter);
  756. }
  757. return TRUE;
  758. cleanup:
  759. for (i = 0; i < (int) entry->array_len; i++)
  760. wpabuf_free(entry->binarray_value[i]);
  761. os_free(entry->binarray_value);
  762. entry->array_len = 0;
  763. entry->binarray_value = NULL;
  764. return FALSE;
  765. }
  766. static dbus_bool_t _wpa_dbus_dict_entry_get_array(
  767. DBusMessageIter *iter_dict_val, struct wpa_dbus_dict_entry *entry)
  768. {
  769. int array_type = dbus_message_iter_get_element_type(iter_dict_val);
  770. dbus_bool_t success = FALSE;
  771. DBusMessageIter iter_array;
  772. if (!entry)
  773. return FALSE;
  774. dbus_message_iter_recurse(iter_dict_val, &iter_array);
  775. switch (array_type) {
  776. case DBUS_TYPE_BYTE:
  777. success = _wpa_dbus_dict_entry_get_byte_array(&iter_array,
  778. entry);
  779. break;
  780. case DBUS_TYPE_STRING:
  781. success = _wpa_dbus_dict_entry_get_string_array(&iter_array,
  782. array_type,
  783. entry);
  784. break;
  785. case DBUS_TYPE_ARRAY:
  786. success = _wpa_dbus_dict_entry_get_binarray(&iter_array, entry);
  787. default:
  788. break;
  789. }
  790. return success;
  791. }
  792. static dbus_bool_t _wpa_dbus_dict_fill_value_from_variant(
  793. struct wpa_dbus_dict_entry *entry, DBusMessageIter *iter)
  794. {
  795. const char *v;
  796. switch (entry->type) {
  797. case DBUS_TYPE_OBJECT_PATH:
  798. case DBUS_TYPE_STRING:
  799. dbus_message_iter_get_basic(iter, &v);
  800. entry->str_value = os_strdup(v);
  801. if (entry->str_value == NULL)
  802. return FALSE;
  803. break;
  804. case DBUS_TYPE_BOOLEAN:
  805. dbus_message_iter_get_basic(iter, &entry->bool_value);
  806. break;
  807. case DBUS_TYPE_BYTE:
  808. dbus_message_iter_get_basic(iter, &entry->byte_value);
  809. break;
  810. case DBUS_TYPE_INT16:
  811. dbus_message_iter_get_basic(iter, &entry->int16_value);
  812. break;
  813. case DBUS_TYPE_UINT16:
  814. dbus_message_iter_get_basic(iter, &entry->uint16_value);
  815. break;
  816. case DBUS_TYPE_INT32:
  817. dbus_message_iter_get_basic(iter, &entry->int32_value);
  818. break;
  819. case DBUS_TYPE_UINT32:
  820. dbus_message_iter_get_basic(iter, &entry->uint32_value);
  821. break;
  822. case DBUS_TYPE_INT64:
  823. dbus_message_iter_get_basic(iter, &entry->int64_value);
  824. break;
  825. case DBUS_TYPE_UINT64:
  826. dbus_message_iter_get_basic(iter, &entry->uint64_value);
  827. break;
  828. case DBUS_TYPE_DOUBLE:
  829. dbus_message_iter_get_basic(iter, &entry->double_value);
  830. break;
  831. case DBUS_TYPE_ARRAY:
  832. return _wpa_dbus_dict_entry_get_array(iter, entry);
  833. default:
  834. return FALSE;
  835. }
  836. return TRUE;
  837. }
  838. /**
  839. * Read the current key/value entry from the dict. Entries are dynamically
  840. * allocated when needed and must be freed after use with the
  841. * wpa_dbus_dict_entry_clear() function.
  842. *
  843. * The returned entry object will be filled with the type and value of the next
  844. * entry in the dict, or the type will be DBUS_TYPE_INVALID if an error
  845. * occurred.
  846. *
  847. * @param iter_dict A valid DBusMessageIter returned from
  848. * wpa_dbus_dict_open_read()
  849. * @param entry A valid dict entry object into which the dict key and value
  850. * will be placed
  851. * @return TRUE on success, FALSE on failure
  852. *
  853. */
  854. dbus_bool_t wpa_dbus_dict_get_entry(DBusMessageIter *iter_dict,
  855. struct wpa_dbus_dict_entry * entry)
  856. {
  857. DBusMessageIter iter_dict_entry, iter_dict_val;
  858. int type;
  859. const char *key;
  860. if (!iter_dict || !entry)
  861. goto error;
  862. if (dbus_message_iter_get_arg_type(iter_dict) != DBUS_TYPE_DICT_ENTRY)
  863. goto error;
  864. dbus_message_iter_recurse(iter_dict, &iter_dict_entry);
  865. dbus_message_iter_get_basic(&iter_dict_entry, &key);
  866. entry->key = key;
  867. if (!dbus_message_iter_next(&iter_dict_entry))
  868. goto error;
  869. type = dbus_message_iter_get_arg_type(&iter_dict_entry);
  870. if (type != DBUS_TYPE_VARIANT)
  871. goto error;
  872. dbus_message_iter_recurse(&iter_dict_entry, &iter_dict_val);
  873. entry->type = dbus_message_iter_get_arg_type(&iter_dict_val);
  874. if (!_wpa_dbus_dict_fill_value_from_variant(entry, &iter_dict_val))
  875. goto error;
  876. dbus_message_iter_next(iter_dict);
  877. return TRUE;
  878. error:
  879. if (entry) {
  880. wpa_dbus_dict_entry_clear(entry);
  881. entry->type = DBUS_TYPE_INVALID;
  882. entry->array_type = DBUS_TYPE_INVALID;
  883. }
  884. return FALSE;
  885. }
  886. /**
  887. * Return whether or not there are additional dictionary entries.
  888. *
  889. * @param iter_dict A valid DBusMessageIter returned from
  890. * wpa_dbus_dict_open_read()
  891. * @return TRUE if more dict entries exists, FALSE if no more dict entries
  892. * exist
  893. */
  894. dbus_bool_t wpa_dbus_dict_has_dict_entry(DBusMessageIter *iter_dict)
  895. {
  896. if (!iter_dict)
  897. return FALSE;
  898. return dbus_message_iter_get_arg_type(iter_dict) ==
  899. DBUS_TYPE_DICT_ENTRY;
  900. }
  901. /**
  902. * Free any memory used by the entry object.
  903. *
  904. * @param entry The entry object
  905. */
  906. void wpa_dbus_dict_entry_clear(struct wpa_dbus_dict_entry *entry)
  907. {
  908. unsigned int i;
  909. if (!entry)
  910. return;
  911. switch (entry->type) {
  912. case DBUS_TYPE_OBJECT_PATH:
  913. case DBUS_TYPE_STRING:
  914. os_free(entry->str_value);
  915. break;
  916. case DBUS_TYPE_ARRAY:
  917. switch (entry->array_type) {
  918. case DBUS_TYPE_BYTE:
  919. os_free(entry->bytearray_value);
  920. break;
  921. case DBUS_TYPE_STRING:
  922. for (i = 0; i < entry->array_len; i++)
  923. os_free(entry->strarray_value[i]);
  924. os_free(entry->strarray_value);
  925. break;
  926. }
  927. break;
  928. case WPAS_DBUS_TYPE_BINARRAY:
  929. for (i = 0; i < entry->array_len; i++)
  930. wpabuf_free(entry->binarray_value[i]);
  931. os_free(entry->binarray_value);
  932. break;
  933. }
  934. memset(entry, 0, sizeof(struct wpa_dbus_dict_entry));
  935. }