dbus_dict_helpers.c 28 KB

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