vici_message.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * Copyright (C) 2015 Tobias Brunner
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * Copyright (C) 2014 Martin Willi
  6. * Copyright (C) 2014 revosec AG
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. */
  18. #include "vici_message.h"
  19. #include "vici_builder.h"
  20. #include <bio/bio_reader.h>
  21. #include <bio/bio_writer.h>
  22. #include <errno.h>
  23. typedef struct private_vici_message_t private_vici_message_t;
  24. /**
  25. * Private data of an vici_message_t object.
  26. */
  27. struct private_vici_message_t {
  28. /**
  29. * Public vici_message_t interface.
  30. */
  31. vici_message_t public;
  32. /**
  33. * Message encoding
  34. */
  35. chunk_t encoding;
  36. /**
  37. * Free encoding during destruction?
  38. */
  39. bool cleanup;
  40. /**
  41. * Allocated strings we maintain for get_str()
  42. */
  43. linked_list_t *strings;
  44. };
  45. ENUM(vici_type_names, VICI_START, VICI_END,
  46. "start",
  47. "section-start",
  48. "section-end",
  49. "key-value",
  50. "list-start",
  51. "list-item",
  52. "list-end",
  53. "end"
  54. );
  55. /**
  56. * See header.
  57. */
  58. bool vici_stringify(chunk_t chunk, char *buf, size_t size)
  59. {
  60. if (!chunk_printable(chunk, NULL, 0))
  61. {
  62. return FALSE;
  63. }
  64. snprintf(buf, size, "%.*s", (int)chunk.len, chunk.ptr);
  65. return TRUE;
  66. }
  67. /**
  68. * See header.
  69. */
  70. bool vici_verify_type(vici_type_t type, u_int section, bool list)
  71. {
  72. if (list)
  73. {
  74. if (type != VICI_LIST_END && type != VICI_LIST_ITEM)
  75. {
  76. DBG1(DBG_ENC, "'%N' within list", vici_type_names, type);
  77. return FALSE;
  78. }
  79. }
  80. else
  81. {
  82. if (type == VICI_LIST_ITEM || type == VICI_LIST_END)
  83. {
  84. DBG1(DBG_ENC, "'%N' outside list", vici_type_names, type);
  85. return FALSE;
  86. }
  87. }
  88. if (type == VICI_SECTION_END && section == 0)
  89. {
  90. DBG1(DBG_ENC, "'%N' outside of section", vici_type_names, type);
  91. return FALSE;
  92. }
  93. if (type == VICI_END && section)
  94. {
  95. DBG1(DBG_ENC, "'%N' within section", vici_type_names, type);
  96. return FALSE;
  97. }
  98. return TRUE;
  99. }
  100. /**
  101. * Enumerator parsing message
  102. */
  103. typedef struct {
  104. /* implements enumerator */
  105. enumerator_t public;
  106. /** reader to parse from */
  107. bio_reader_t *reader;
  108. /** section nesting level */
  109. int section;
  110. /** currently parsing list? */
  111. bool list;
  112. /** string currently enumerating */
  113. char name[257];
  114. } parse_enumerator_t;
  115. METHOD(enumerator_t, parse_enumerate, bool,
  116. parse_enumerator_t *this, va_list args)
  117. {
  118. vici_type_t *out;
  119. chunk_t *value;
  120. char **name;
  121. uint8_t type;
  122. chunk_t data;
  123. VA_ARGS_VGET(args, out, name, value);
  124. if (!this->reader->remaining(this->reader) ||
  125. !this->reader->read_uint8(this->reader, &type))
  126. {
  127. *out = VICI_END;
  128. return TRUE;
  129. }
  130. if (!vici_verify_type(type, this->section, this->list))
  131. {
  132. return FALSE;
  133. }
  134. switch (type)
  135. {
  136. case VICI_SECTION_START:
  137. if (!this->reader->read_data8(this->reader, &data) ||
  138. !vici_stringify(data, this->name, sizeof(this->name)))
  139. {
  140. DBG1(DBG_ENC, "invalid '%N' encoding", vici_type_names, type);
  141. return FALSE;
  142. }
  143. *name = this->name;
  144. this->section++;
  145. break;
  146. case VICI_SECTION_END:
  147. this->section--;
  148. break;
  149. case VICI_KEY_VALUE:
  150. if (!this->reader->read_data8(this->reader, &data) ||
  151. !vici_stringify(data, this->name, sizeof(this->name)) ||
  152. !this->reader->read_data16(this->reader, value))
  153. {
  154. DBG1(DBG_ENC, "invalid '%N' encoding", vici_type_names, type);
  155. return FALSE;
  156. }
  157. *name = this->name;
  158. break;
  159. case VICI_LIST_START:
  160. if (!this->reader->read_data8(this->reader, &data) ||
  161. !vici_stringify(data, this->name, sizeof(this->name)))
  162. {
  163. DBG1(DBG_ENC, "invalid '%N' encoding", vici_type_names, type);
  164. return FALSE;
  165. }
  166. *name = this->name;
  167. this->list = TRUE;
  168. break;
  169. case VICI_LIST_ITEM:
  170. this->reader->read_data16(this->reader, value);
  171. break;
  172. case VICI_LIST_END:
  173. this->list = FALSE;
  174. break;
  175. case VICI_END:
  176. return TRUE;
  177. default:
  178. DBG1(DBG_ENC, "unknown encoding type: %u", type);
  179. return FALSE;
  180. }
  181. *out = type;
  182. return TRUE;
  183. }
  184. METHOD(enumerator_t, parse_destroy, void,
  185. parse_enumerator_t *this)
  186. {
  187. this->reader->destroy(this->reader);
  188. free(this);
  189. }
  190. METHOD(vici_message_t, create_enumerator, enumerator_t*,
  191. private_vici_message_t *this)
  192. {
  193. parse_enumerator_t *enumerator;
  194. INIT(enumerator,
  195. .public = {
  196. .enumerate = enumerator_enumerate_default,
  197. .venumerate = _parse_enumerate,
  198. .destroy = _parse_destroy,
  199. },
  200. .reader = bio_reader_create(this->encoding),
  201. );
  202. return &enumerator->public;
  203. }
  204. /**
  205. * Find a value for given vararg key
  206. */
  207. static bool find_value(private_vici_message_t *this, chunk_t *value,
  208. char *fmt, va_list args)
  209. {
  210. enumerator_t *enumerator;
  211. char buf[128], *name, *key, *dot, *next;
  212. int section = 0, keysection = 0;
  213. bool found = FALSE;
  214. chunk_t current;
  215. vici_type_t type;
  216. vsnprintf(buf, sizeof(buf), fmt, args);
  217. next = buf;
  218. enumerator = create_enumerator(this);
  219. /* descent into section */
  220. while (TRUE)
  221. {
  222. dot = strchr(next, '.');
  223. if (!dot)
  224. {
  225. key = next;
  226. break;
  227. }
  228. *dot = '\0';
  229. key = next;
  230. next = dot + 1;
  231. keysection++;
  232. while (enumerator->enumerate(enumerator, &type, &name, &current))
  233. {
  234. switch (type)
  235. {
  236. case VICI_SECTION_START:
  237. section++;
  238. if (section == keysection && streq(name, key))
  239. {
  240. break;
  241. }
  242. continue;
  243. case VICI_SECTION_END:
  244. section--;
  245. continue;
  246. case VICI_END:
  247. break;
  248. default:
  249. continue;
  250. }
  251. break;
  252. }
  253. }
  254. /* find key/value in current section */
  255. while (enumerator->enumerate(enumerator, &type, &name, &current))
  256. {
  257. switch (type)
  258. {
  259. case VICI_KEY_VALUE:
  260. if (section == keysection && streq(key, name))
  261. {
  262. *value = current;
  263. found = TRUE;
  264. break;
  265. }
  266. continue;
  267. case VICI_SECTION_START:
  268. section++;
  269. continue;
  270. case VICI_SECTION_END:
  271. section--;
  272. continue;
  273. case VICI_END:
  274. break;
  275. default:
  276. continue;
  277. }
  278. break;
  279. }
  280. enumerator->destroy(enumerator);
  281. return found;
  282. }
  283. METHOD(vici_message_t, vget_str, char*,
  284. private_vici_message_t *this, char *def, char *fmt, va_list args)
  285. {
  286. chunk_t value;
  287. bool found;
  288. char *str;
  289. found = find_value(this, &value, fmt, args);
  290. if (found)
  291. {
  292. if (chunk_printable(value, NULL, 0))
  293. {
  294. str = strndup(value.ptr, value.len);
  295. /* keep a reference to string, so caller doesn't have to care */
  296. this->strings->insert_last(this->strings, str);
  297. return str;
  298. }
  299. }
  300. return def;
  301. }
  302. METHOD(vici_message_t, get_str, char*,
  303. private_vici_message_t *this, char *def, char *fmt, ...)
  304. {
  305. va_list args;
  306. char *str;
  307. va_start(args, fmt);
  308. str = vget_str(this, def, fmt, args);
  309. va_end(args);
  310. return str;
  311. }
  312. METHOD(vici_message_t, vget_int, int,
  313. private_vici_message_t *this, int def, char *fmt, va_list args)
  314. {
  315. chunk_t value;
  316. bool found;
  317. char buf[32], *pos;
  318. int ret;
  319. found = find_value(this, &value, fmt, args);
  320. if (found)
  321. {
  322. if (value.len == 0)
  323. {
  324. return def;
  325. }
  326. if (chunk_printable(value, NULL, 0))
  327. {
  328. snprintf(buf, sizeof(buf), "%.*s", (int)value.len, value.ptr);
  329. errno = 0;
  330. ret = strtol(buf, &pos, 0);
  331. if (errno == 0 && pos == buf + strlen(buf))
  332. {
  333. return ret;
  334. }
  335. }
  336. }
  337. return def;
  338. }
  339. METHOD(vici_message_t, get_int, int,
  340. private_vici_message_t *this, int def, char *fmt, ...)
  341. {
  342. va_list args;
  343. int val;
  344. va_start(args, fmt);
  345. val = vget_int(this, def, fmt, args);
  346. va_end(args);
  347. return val;
  348. }
  349. METHOD(vici_message_t, vget_bool, bool,
  350. private_vici_message_t *this, bool def, char *fmt, va_list args)
  351. {
  352. chunk_t value;
  353. bool found;
  354. char buf[16];
  355. found = find_value(this, &value, fmt, args);
  356. if (found)
  357. {
  358. if (value.len == 0)
  359. {
  360. return def;
  361. }
  362. if (chunk_printable(value, NULL, 0))
  363. {
  364. snprintf(buf, sizeof(buf), "%.*s", (int)value.len, value.ptr);
  365. return settings_value_as_bool(buf, def);
  366. }
  367. }
  368. return def;
  369. }
  370. METHOD(vici_message_t, get_bool, bool,
  371. private_vici_message_t *this, bool def, char *fmt, ...)
  372. {
  373. va_list args;
  374. bool val;
  375. va_start(args, fmt);
  376. val = vget_bool(this, def, fmt, args);
  377. va_end(args);
  378. return val;
  379. }
  380. METHOD(vici_message_t, vget_value, chunk_t,
  381. private_vici_message_t *this, chunk_t def, char *fmt, va_list args)
  382. {
  383. chunk_t value;
  384. bool found;
  385. found = find_value(this, &value, fmt, args);
  386. if (found)
  387. {
  388. return value;
  389. }
  390. return def;
  391. }
  392. METHOD(vici_message_t, get_value, chunk_t,
  393. private_vici_message_t *this, chunk_t def, char *fmt, ...)
  394. {
  395. va_list args;
  396. chunk_t value;
  397. va_start(args, fmt);
  398. value = vget_value(this, def, fmt, args);
  399. va_end(args);
  400. return value;
  401. }
  402. METHOD(vici_message_t, get_encoding, chunk_t,
  403. private_vici_message_t *this)
  404. {
  405. return this->encoding;
  406. }
  407. /**
  408. * Private parse context data
  409. */
  410. struct vici_parse_context_t {
  411. /** current section nesting level */
  412. int level;
  413. /** parse enumerator */
  414. enumerator_t *e;
  415. };
  416. METHOD(vici_message_t, parse, bool,
  417. private_vici_message_t *this, vici_parse_context_t *ctx,
  418. vici_section_cb_t section, vici_value_cb_t kv, vici_value_cb_t li,
  419. void *user)
  420. {
  421. vici_parse_context_t root = {};
  422. char *name, *list = NULL;
  423. vici_type_t type;
  424. chunk_t value;
  425. int base;
  426. bool ok = TRUE;
  427. if (!ctx)
  428. {
  429. ctx = &root;
  430. root.e = create_enumerator(this);
  431. }
  432. base = ctx->level;
  433. while (ok)
  434. {
  435. ok = ctx->e->enumerate(ctx->e, &type, &name, &value);
  436. if (ok)
  437. {
  438. switch (type)
  439. {
  440. case VICI_START:
  441. /* should never occur */
  442. continue;
  443. case VICI_KEY_VALUE:
  444. if (ctx->level == base && kv)
  445. {
  446. name = strdup(name);
  447. this->strings->insert_last(this->strings, name);
  448. ok = kv(user, &this->public, name, value);
  449. }
  450. continue;
  451. case VICI_LIST_START:
  452. if (ctx->level == base)
  453. {
  454. list = strdup(name);
  455. this->strings->insert_last(this->strings, list);
  456. }
  457. continue;
  458. case VICI_LIST_ITEM:
  459. if (list && li)
  460. {
  461. name = strdup(name);
  462. this->strings->insert_last(this->strings, name);
  463. ok = li(user, &this->public, list, value);
  464. }
  465. continue;
  466. case VICI_LIST_END:
  467. if (ctx->level == base)
  468. {
  469. list = NULL;
  470. }
  471. continue;
  472. case VICI_SECTION_START:
  473. if (ctx->level++ == base && section)
  474. {
  475. name = strdup(name);
  476. this->strings->insert_last(this->strings, name);
  477. ok = section(user, &this->public, ctx, name);
  478. }
  479. continue;
  480. case VICI_SECTION_END:
  481. if (ctx->level-- == base)
  482. {
  483. break;
  484. }
  485. continue;
  486. case VICI_END:
  487. break;
  488. }
  489. }
  490. break;
  491. }
  492. if (ctx == &root)
  493. {
  494. root.e->destroy(root.e);
  495. }
  496. return ok;
  497. }
  498. METHOD(vici_message_t, dump, bool,
  499. private_vici_message_t *this, char *label, bool pretty, FILE *out)
  500. {
  501. enumerator_t *enumerator;
  502. int ident = 0, delta;
  503. vici_type_t type, last_type = VICI_START;
  504. char *name, *term, *sep, *separ, *assign;
  505. chunk_t value;
  506. /* pretty print uses indentation on multiple lines */
  507. if (pretty)
  508. {
  509. delta = 2;
  510. term = "\n";
  511. separ = "";
  512. assign = " = ";
  513. }
  514. else
  515. {
  516. delta = 0;
  517. term = "";
  518. separ = " ";
  519. assign = "=";
  520. }
  521. fprintf(out, "%s {%s", label, term);
  522. ident += delta;
  523. enumerator = create_enumerator(this);
  524. while (enumerator->enumerate(enumerator, &type, &name, &value))
  525. {
  526. switch (type)
  527. {
  528. case VICI_START:
  529. /* should never occur */
  530. break;
  531. case VICI_SECTION_START:
  532. sep = (last_type != VICI_SECTION_START &&
  533. last_type != VICI_START) ? separ : "";
  534. fprintf(out, "%*s%s%s {%s", ident, "", sep, name, term);
  535. ident += delta;
  536. break;
  537. case VICI_SECTION_END:
  538. ident -= delta;
  539. fprintf(out, "%*s}%s", ident, "", term);
  540. break;
  541. case VICI_KEY_VALUE:
  542. sep = (last_type != VICI_SECTION_START &&
  543. last_type != VICI_START) ? separ : "";
  544. if (chunk_printable(value, NULL, ' '))
  545. {
  546. fprintf(out, "%*s%s%s%s%.*s%s", ident, "", sep, name,
  547. assign, (int)value.len, value.ptr, term);
  548. }
  549. else
  550. {
  551. fprintf(out, "%*s%s%s%s0x%+#B%s", ident, "", sep, name,
  552. assign, &value, term);
  553. }
  554. break;
  555. case VICI_LIST_START:
  556. sep = (last_type != VICI_SECTION_START &&
  557. last_type != VICI_START) ? separ : "";
  558. fprintf(out, "%*s%s%s%s[%s", ident, "", sep, name, assign, term);
  559. ident += delta;
  560. break;
  561. case VICI_LIST_END:
  562. ident -= delta;
  563. fprintf(out, "%*s]%s", ident, "", term);
  564. break;
  565. case VICI_LIST_ITEM:
  566. sep = (last_type != VICI_LIST_START) ? separ : "";
  567. if (chunk_printable(value, NULL, ' '))
  568. {
  569. fprintf(out, "%*s%s%.*s%s", ident, "", sep,
  570. (int)value.len, value.ptr, term);
  571. }
  572. else
  573. {
  574. fprintf(out, "%*s%s0x%+#B%s", ident, "", sep,
  575. &value, term);
  576. }
  577. break;
  578. case VICI_END:
  579. fprintf(out, "}\n");
  580. enumerator->destroy(enumerator);
  581. return TRUE;
  582. }
  583. last_type = type;
  584. }
  585. enumerator->destroy(enumerator);
  586. return FALSE;
  587. }
  588. METHOD(vici_message_t, destroy, void,
  589. private_vici_message_t *this)
  590. {
  591. if (this->cleanup)
  592. {
  593. chunk_clear(&this->encoding);
  594. }
  595. this->strings->destroy_function(this->strings, free);
  596. free(this);
  597. }
  598. /**
  599. * See header
  600. */
  601. vici_message_t *vici_message_create_from_data(chunk_t data, bool cleanup)
  602. {
  603. private_vici_message_t *this;
  604. INIT(this,
  605. .public = {
  606. .create_enumerator = _create_enumerator,
  607. .get_str = _get_str,
  608. .vget_str = _vget_str,
  609. .get_int = _get_int,
  610. .vget_int = _vget_int,
  611. .get_bool = _get_bool,
  612. .vget_bool = _vget_bool,
  613. .get_value = _get_value,
  614. .vget_value = _vget_value,
  615. .get_encoding = _get_encoding,
  616. .parse = _parse,
  617. .dump = _dump,
  618. .destroy = _destroy,
  619. },
  620. .strings = linked_list_create(),
  621. .encoding = data,
  622. .cleanup = cleanup,
  623. );
  624. return &this->public;
  625. }
  626. /**
  627. * See header
  628. */
  629. vici_message_t *vici_message_create_from_enumerator(enumerator_t *enumerator)
  630. {
  631. vici_builder_t *builder;
  632. vici_type_t type;
  633. char *name;
  634. chunk_t value;
  635. builder = vici_builder_create();
  636. while (enumerator->enumerate(enumerator, &type, &name, &value))
  637. {
  638. switch (type)
  639. {
  640. case VICI_SECTION_START:
  641. case VICI_LIST_START:
  642. builder->add(builder, type, name);
  643. continue;
  644. case VICI_KEY_VALUE:
  645. builder->add(builder, type, name, value);
  646. continue;
  647. case VICI_LIST_ITEM:
  648. builder->add(builder, type, value);
  649. continue;
  650. case VICI_SECTION_END:
  651. case VICI_LIST_END:
  652. default:
  653. builder->add(builder, type);
  654. continue;
  655. case VICI_END:
  656. break;
  657. }
  658. break;
  659. }
  660. enumerator->destroy(enumerator);
  661. return builder->finalize(builder);
  662. }
  663. /**
  664. * See header
  665. */
  666. vici_message_t *vici_message_create_from_args(vici_type_t type, ...)
  667. {
  668. vici_builder_t *builder;
  669. va_list args;
  670. char *name;
  671. chunk_t value;
  672. builder = vici_builder_create();
  673. va_start(args, type);
  674. while (type != VICI_END)
  675. {
  676. switch (type)
  677. {
  678. case VICI_LIST_START:
  679. case VICI_SECTION_START:
  680. name = va_arg(args, char*);
  681. builder->add(builder, type, name);
  682. break;
  683. case VICI_KEY_VALUE:
  684. name = va_arg(args, char*);
  685. value = va_arg(args, chunk_t);
  686. builder->add(builder, type, name, value);
  687. break;
  688. case VICI_LIST_ITEM:
  689. value = va_arg(args, chunk_t);
  690. builder->add(builder, type, value);
  691. break;
  692. case VICI_SECTION_END:
  693. case VICI_LIST_END:
  694. default:
  695. builder->add(builder, type);
  696. break;
  697. }
  698. type = va_arg(args, vici_type_t);
  699. }
  700. va_end(args);
  701. return builder->finalize(builder);
  702. }