msg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /** @file msg.c
  2. * @brief Class represents a single message for custom protocol.
  3. *
  4. * Message consist of main header, a block of parameter headers and then
  5. * parameters themselfs. Class stores these data. Additionally class knows
  6. * the structure of message and can send and receive messages via socket. It
  7. * uses external faux_net_t object to do so. The receive function is necessary
  8. * because message has a variable length and message parsing is needed to get
  9. * actual length of message. The send function is usefull because class uses
  10. * struct iovec array to compose outgoing message so it's not necessary to
  11. * assemble message into the single long memory chunk.
  12. */
  13. #include <stdlib.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <assert.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <sys/socket.h>
  24. #include <sys/un.h>
  25. #include <arpa/inet.h>
  26. #include <faux/faux.h>
  27. #include <faux/str.h>
  28. #include <faux/list.h>
  29. #include <faux/net.h>
  30. #include <faux/async.h>
  31. #include <faux/msg.h>
  32. // Global variable to switch debug on/off (true/false)
  33. bool_t faux_msg_debug_flag = BOOL_FALSE;
  34. /** @brief Opaque faux_msg_s structure. */
  35. struct faux_msg_s {
  36. faux_hdr_t *hdr; // Message header
  37. faux_list_t *params; // List of parameters
  38. };
  39. static void faux_msg_set_len(faux_msg_t *msg, uint32_t len);
  40. static void faux_msg_set_param_num(faux_msg_t *msg, uint32_t param_num);
  41. /** @brief Allocate memory to store message.
  42. *
  43. * This static function is needed because new message object can be created
  44. * in a different ways. The first way is creating outgoing message manually and
  45. * the second way is receiving message from network. These ways need
  46. * different initialization but the same memory allocation.
  47. *
  48. * @return Allocated but not fully initialized faux_msg_t object
  49. * or NULL on error
  50. */
  51. static faux_msg_t *faux_msg_allocate(void)
  52. {
  53. faux_msg_t *msg = NULL;
  54. msg = faux_zmalloc(sizeof(*msg));
  55. assert(msg);
  56. if (!msg)
  57. return NULL;
  58. // Init message header
  59. msg->hdr = faux_zmalloc(sizeof(*msg->hdr));
  60. assert(msg->hdr);
  61. if (!msg->hdr) {
  62. faux_msg_free(msg);
  63. return NULL;
  64. }
  65. msg->params = faux_list_new(
  66. FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE, NULL, NULL, faux_free);
  67. return msg;
  68. }
  69. /** @brief Creates new faux_msg_t object. It's usually outgoing message.
  70. *
  71. * Function initializes main message header with default values. Usually
  72. * only outgoing messages need initialized header.
  73. *
  74. * @param [in] magic Protocol's magic number.
  75. * @param [in] major Protocol's version major number.
  76. * @param [in] minor Protocol's version minor number.
  77. * @return Allocated and initilized faux_msg_t object or NULL on error.
  78. */
  79. faux_msg_t *faux_msg_new(uint32_t magic, uint8_t major, uint8_t minor)
  80. {
  81. faux_msg_t *msg = NULL;
  82. msg = faux_msg_allocate();
  83. assert(msg);
  84. if (!msg)
  85. return NULL;
  86. // Init
  87. faux_hdr_set_magic(msg->hdr, magic);
  88. faux_hdr_set_major(msg->hdr, major);
  89. faux_hdr_set_minor(msg->hdr, minor);
  90. faux_msg_set_cmd(msg, 0);
  91. faux_msg_set_status(msg, 0);
  92. faux_msg_set_req_id(msg, 0l);
  93. faux_msg_set_param_num(msg, 0l);
  94. faux_msg_set_len(msg, sizeof(*msg->hdr));
  95. return msg;
  96. }
  97. /** @brief Frees allocated message.
  98. *
  99. * @param [in] msg Allocated faux_msg_t object.
  100. */
  101. void faux_msg_free(faux_msg_t *msg)
  102. {
  103. if (!msg)
  104. return;
  105. faux_list_free(msg->params);
  106. faux_free(msg->hdr);
  107. faux_free(msg);
  108. }
  109. /** @brief Sets command code to header.
  110. *
  111. * See the protocol and header description for possible values.
  112. *
  113. * @param [in] msg Allocated faux_msg_t object.
  114. * @param [in] cmd Command code (16 bit).
  115. */
  116. void faux_msg_set_cmd(faux_msg_t *msg, uint16_t cmd)
  117. {
  118. assert(msg);
  119. assert(msg->hdr);
  120. if (!msg || !msg->hdr)
  121. return;
  122. return faux_hdr_set_cmd(msg->hdr, cmd);
  123. }
  124. /** @brief Gets command code from header.
  125. *
  126. * See the protocol and header description for possible values.
  127. *
  128. * @param [in] msg Allocated faux_msg_t object.
  129. * @return Command code or 0 on error.
  130. */
  131. uint16_t faux_msg_get_cmd(const faux_msg_t *msg)
  132. {
  133. assert(msg);
  134. assert(msg->hdr);
  135. if (!msg || !msg->hdr)
  136. return 0;
  137. return faux_hdr_cmd(msg->hdr);
  138. }
  139. /** @brief Sets message status to header.
  140. *
  141. * See the protocol and header description for possible values.
  142. *
  143. * @param [in] msg Allocated faux_msg_t object.
  144. * @param [in] status Message status.
  145. */
  146. void faux_msg_set_status(faux_msg_t *msg, uint32_t status)
  147. {
  148. assert(msg);
  149. assert(msg->hdr);
  150. if (!msg || !msg->hdr)
  151. return;
  152. return faux_hdr_set_status(msg->hdr, status);
  153. }
  154. /** @brief Gets message status from header.
  155. *
  156. * See the protocol and header description for possible values.
  157. *
  158. * @param [in] msg Allocated faux_msg_t object.
  159. * @return Message status or 0 on error.
  160. */
  161. uint32_t faux_msg_get_status(const faux_msg_t *msg)
  162. {
  163. assert(msg);
  164. assert(msg->hdr);
  165. if (!msg || !msg->hdr)
  166. return 0;
  167. return faux_hdr_status(msg->hdr);
  168. }
  169. /** @brief Sets request ID to header.
  170. *
  171. * @param [in] msg Allocated faux_msg_t object.
  172. * @param [in] req_id Request ID.
  173. */
  174. void faux_msg_set_req_id(faux_msg_t *msg, uint32_t req_id)
  175. {
  176. assert(msg);
  177. assert(msg->hdr);
  178. if (!msg || !msg->hdr)
  179. return;
  180. return faux_hdr_set_req_id(msg->hdr, req_id);
  181. }
  182. /** @brief Gets request ID from header.
  183. *
  184. * @param [in] msg Allocated faux_msg_t object.
  185. * @return Request ID or 0 on error.
  186. */
  187. uint32_t faux_msg_get_req_id(const faux_msg_t *msg)
  188. {
  189. assert(msg);
  190. assert(msg->hdr);
  191. if (!msg || !msg->hdr)
  192. return 0;
  193. return faux_hdr_req_id(msg->hdr);
  194. }
  195. /** @brief Sets number of parameters to header.
  196. *
  197. * It's a static function because external user can add or remove parameters
  198. * but class calculates total number of parameters internally.
  199. *
  200. * @param [in] msg Allocated faux_msg_t object.
  201. * @param [in] param_num Number of parameters.
  202. */
  203. static void faux_msg_set_param_num(faux_msg_t *msg, uint32_t param_num)
  204. {
  205. assert(msg);
  206. assert(msg->hdr);
  207. if (!msg || !msg->hdr)
  208. return;
  209. return faux_hdr_set_param_num(msg->hdr, param_num);
  210. }
  211. /** @brief Gets number of parameters from header.
  212. *
  213. * @param [in] msg Allocated faux_msg_t object.
  214. * @return Number of parameters or 0 on error.
  215. */
  216. uint32_t faux_msg_get_param_num(const faux_msg_t *msg)
  217. {
  218. assert(msg);
  219. assert(msg->hdr);
  220. if (!msg || !msg->hdr)
  221. return -1;
  222. return faux_hdr_param_num(msg->hdr);
  223. }
  224. /** @brief Sets total length of message to header.
  225. *
  226. * It's a static function because external user can add or remove parameters
  227. * but class calculates total length of message internally.
  228. *
  229. * @param [in] msg Allocated faux_msg_t object.
  230. * @param [in] len Total length of message.
  231. */
  232. static void faux_msg_set_len(faux_msg_t *msg, uint32_t len)
  233. {
  234. assert(msg);
  235. assert(msg->hdr);
  236. if (!msg || !msg->hdr)
  237. return;
  238. return faux_hdr_set_len(msg->hdr, len);
  239. }
  240. /** @brief Gets total length of message from header.
  241. *
  242. * @param [in] msg Allocated faux_msg_t object.
  243. * @return Total length of message or 0 on error.
  244. */
  245. int faux_msg_get_len(const faux_msg_t *msg)
  246. {
  247. assert(msg);
  248. assert(msg->hdr);
  249. if (!msg || !msg->hdr)
  250. return 0;
  251. return faux_hdr_len(msg->hdr);
  252. }
  253. /** @brief Gets magic number from header.
  254. *
  255. * @param [in] msg Allocated faux_msg_t object.
  256. * @return Magic number or 0 on error.
  257. */
  258. uint32_t faux_msg_get_magic(const faux_msg_t *msg)
  259. {
  260. assert(msg);
  261. assert(msg->hdr);
  262. if (!msg || !msg->hdr)
  263. return 0;
  264. return faux_hdr_magic(msg->hdr);
  265. }
  266. /** @brief Gets major version from header.
  267. *
  268. * @param [in] msg Allocated faux_msg_t object.
  269. * @return Major version number or 0 on error.
  270. */
  271. int faux_msg_get_major(const faux_msg_t *msg)
  272. {
  273. assert(msg);
  274. assert(msg->hdr);
  275. if (!msg || !msg->hdr)
  276. return 0;
  277. return faux_hdr_major(msg->hdr);
  278. }
  279. /** @brief Gets minor version from header.
  280. *
  281. * @param [in] msg Allocated faux_msg_t object.
  282. * @return Minor version number or 0 on error.
  283. */
  284. int faux_msg_get_minor(const faux_msg_t *msg)
  285. {
  286. assert(msg);
  287. assert(msg->hdr);
  288. if (!msg || !msg->hdr)
  289. return 0;
  290. return faux_hdr_minor(msg->hdr);
  291. }
  292. /** @brief Internal function to add message parameter
  293. *
  294. * Internal function can update or don't update number of parameters and
  295. * whole length within message header. It can be used while
  296. * message receive to don't break already calculated header
  297. * values. So when user is constructing message the values must be updated.
  298. *
  299. * @param [in] msg Allocated faux_msg_t object.
  300. * @param [in] type Type of parameter.
  301. * @param [in] buf Parameter's data buffer.
  302. * @param [in] len Parameter's data length.
  303. * @param [in] upadte_len Flag that says to update or don't update number of
  304. * parameters and total message length within header. BOOL_TRUE - update,
  305. * BOOL_FALSE - don't update.
  306. * @return Length of parameter's data or < 0 on error.
  307. */
  308. static ssize_t faux_msg_add_param_internal(faux_msg_t *msg,
  309. uint16_t type, const void *buf, size_t len, bool_t update_len)
  310. {
  311. faux_phdr_t *phdr = NULL;
  312. char *param = NULL;
  313. assert(msg);
  314. assert(msg->hdr);
  315. if (!msg || !msg->hdr)
  316. return -1;
  317. // Allocate parameter header and data
  318. param = faux_zmalloc(sizeof(*phdr) + len);
  319. assert(param);
  320. if (!param)
  321. return -1;
  322. // Init param hdr
  323. phdr = (faux_phdr_t *)param;
  324. faux_phdr_set_type(phdr, type);
  325. faux_phdr_set_len(phdr, len);
  326. // Copy data
  327. memcpy(param + sizeof(*phdr), buf, len);
  328. if (update_len) {
  329. // Update number of parameters
  330. faux_msg_set_param_num(msg, faux_msg_get_param_num(msg) + 1);
  331. // Update whole message length
  332. faux_msg_set_len(msg,
  333. faux_msg_get_len(msg) + sizeof(*phdr) + len);
  334. }
  335. // Add to parameter list
  336. faux_list_add(msg->params, param);
  337. return len;
  338. }
  339. /** @brief Adds parameter to message.
  340. *
  341. * @param [in] msg Allocated faux_msg_t object.
  342. * @param [in] type Type of parameter.
  343. * @param [in] buf Parameter's data buffer.
  344. * @param [in] len Parameter's data length.
  345. * @return Length of parameter's data or < 0 on error.
  346. */
  347. ssize_t faux_msg_add_param(faux_msg_t *msg, uint16_t type,
  348. const void *buf, size_t len)
  349. {
  350. return faux_msg_add_param_internal(msg, type, buf, len, BOOL_TRUE);
  351. }
  352. /** @brief Initializes iterator to iterate through the message parameters.
  353. *
  354. * The iterator must be initialized before iteration.
  355. *
  356. * @param [in] msg Allocated faux_msg_t object.
  357. * @return Initialized iterator.
  358. */
  359. faux_list_node_t *faux_msg_init_param_iter(const faux_msg_t *msg)
  360. {
  361. assert(msg);
  362. assert(msg->params);
  363. if (!msg || !msg->params)
  364. return NULL;
  365. return faux_list_head(msg->params);
  366. }
  367. /** @brief Internal function to get parameter's data by node (faux_list_node_t).
  368. *
  369. * Note function returns the main data by output arguments.
  370. *
  371. * @param [in] node Node from the parameter's list.
  372. * @param [out] param_type Type of parameter.
  373. * @param [out] param_buf Parameter's data buffer.
  374. * @param [out] param_len Parameter's data length.
  375. * @return Pointer to parameter's header or NULL on error.
  376. */
  377. static faux_phdr_t *faux_msg_get_param_by_node(const faux_list_node_t *node,
  378. uint16_t *param_type, void **param_data, uint32_t *param_len)
  379. {
  380. char *param = NULL;
  381. faux_phdr_t *phdr = NULL;
  382. char *data = NULL;
  383. if (!node)
  384. return NULL;
  385. param = faux_list_data(node);
  386. phdr = (faux_phdr_t *)param;
  387. data = param + sizeof(*phdr);
  388. if (param_type)
  389. *param_type = faux_phdr_get_type(phdr);
  390. if (param_len)
  391. *param_len = faux_phdr_get_len(phdr);
  392. if (param_data)
  393. *param_data = data;
  394. return phdr;
  395. }
  396. /** @brief Iterate through the message parameters.
  397. *
  398. * First parameter (iterator/node) must be initialized first by
  399. * faux_msg_init_param_iter().
  400. *
  401. * @param [in] node Initialized iterator of parameter list.
  402. * @param [out] param_type Type of parameter.
  403. * @param [out] param_buf Parameter's data buffer.
  404. * @param [out] param_len Parameter's data length.
  405. * @return Pointer to parameter's header or NULL on error.
  406. */
  407. faux_phdr_t *faux_msg_get_param_each(faux_list_node_t **node,
  408. uint16_t *param_type, void **param_data, uint32_t *param_len)
  409. {
  410. faux_list_node_t *current_node = NULL;
  411. if (!node || !*node)
  412. return NULL;
  413. current_node = *node;
  414. *node = faux_list_next_node(current_node);
  415. return faux_msg_get_param_by_node(current_node,
  416. param_type, param_data, param_len);
  417. }
  418. /** @brief Gets message parameter by the index.
  419. *
  420. * @param [in] msg Allocated faux_msg_t object.
  421. * @param [in] index Parameter's index.
  422. * @param [out] param_type Type of parameter.
  423. * @param [out] param_buf Parameter's data buffer.
  424. * @param [out] param_len Parameter's data length.
  425. * @return Pointer to parameter's header or NULL on error.
  426. */
  427. faux_phdr_t *faux_msg_get_param_by_index(const faux_msg_t *msg, unsigned int index,
  428. uint16_t *param_type, void **param_data, uint32_t *param_len)
  429. {
  430. faux_list_node_t *iter = NULL;
  431. unsigned int i = 0;
  432. assert(msg);
  433. assert(msg->hdr);
  434. if (!msg || !msg->hdr)
  435. return NULL;
  436. if (index >= faux_msg_get_param_num(msg)) // Non-existent entry
  437. return NULL;
  438. iter = faux_msg_init_param_iter(msg);
  439. while ((i != index) && iter) {
  440. i++;
  441. iter = faux_list_next_node(iter);
  442. }
  443. return faux_msg_get_param_by_node(iter,
  444. param_type, param_data, param_len);
  445. }
  446. /** @brief Gets message parameter by parameter's type.
  447. *
  448. * Note message can contain many parameters with the same type. This function
  449. * will find only the first parameter with specified type. You can iterate
  450. * through all parameters to find all entries with type you need.
  451. *
  452. * @param [in] msg Allocated faux_msg_t object.
  453. * @param [in] param_type Type of parameter.
  454. * @param [out] param_buf Parameter's data buffer.
  455. * @param [out] param_len Parameter's data length.
  456. * @return Pointer to parameter's header or NULL on error.
  457. */
  458. faux_phdr_t *faux_msg_get_param_by_type(const faux_msg_t *msg,
  459. uint16_t param_type, void **param_data, uint32_t *param_len)
  460. {
  461. faux_list_node_t *iter = NULL;
  462. assert(msg);
  463. assert(msg->hdr);
  464. if (!msg || !msg->hdr)
  465. return NULL;
  466. for (iter = faux_msg_init_param_iter(msg);
  467. iter; iter = faux_list_next_node(iter)) {
  468. faux_phdr_t *phdr = NULL;
  469. phdr = (faux_phdr_t *)faux_list_data(iter);
  470. if (faux_phdr_get_type(phdr) == param_type)
  471. return faux_msg_get_param_by_node(iter,
  472. NULL, param_data, param_len);
  473. }
  474. // Not found
  475. return NULL;
  476. }
  477. /** @brief Create IOV of message.
  478. *
  479. * Function creates and fills iovec structure. This iovec contains references
  480. * to parts of message enough to construct message in network format.
  481. *
  482. * @param [in] msg Allocated faux_msg_t object.
  483. * @param [out] iov_out iovec structure.
  484. * @param [out] iov_num_out Number of iovec entries.
  485. * @return BOOL_TRUE - success, BOOL_FALSE - fail.
  486. */
  487. bool_t faux_msg_iov(const faux_msg_t *msg, struct iovec **iov_out, size_t *iov_num_out)
  488. {
  489. size_t vec_entries_num = 0;
  490. struct iovec *iov = NULL;
  491. unsigned int i = 0;
  492. faux_list_node_t *iter = NULL;
  493. assert(msg);
  494. if (!msg)
  495. return BOOL_FALSE;
  496. assert(msg->hdr);
  497. if (!msg->hdr)
  498. return BOOL_FALSE;
  499. assert(iov_out);
  500. if (!iov_out)
  501. return BOOL_FALSE;
  502. assert(iov_num_out);
  503. if (!iov_num_out)
  504. return BOOL_FALSE;
  505. // Calculate number if struct iovec entries.
  506. // n = (msg header) + ((param hdr) + (param data)) * (param_num)
  507. vec_entries_num = 1 + (2 * faux_msg_get_param_num(msg));
  508. iov = faux_zmalloc(vec_entries_num * sizeof(*iov));
  509. // Message header
  510. iov[i].iov_base = msg->hdr;
  511. iov[i].iov_len = sizeof(*msg->hdr);
  512. i++;
  513. // Parameter headers
  514. for (iter = faux_msg_init_param_iter(msg);
  515. iter; iter = faux_list_next_node(iter)) {
  516. faux_phdr_t *phdr = NULL;
  517. phdr = (faux_phdr_t *)faux_list_data(iter);
  518. iov[i].iov_base = phdr;
  519. iov[i].iov_len = sizeof(*phdr);
  520. i++;
  521. }
  522. // Parameter data
  523. for (iter = faux_msg_init_param_iter(msg);
  524. iter; iter = faux_list_next_node(iter)) {
  525. faux_phdr_t *phdr = NULL;
  526. void *data = NULL;
  527. phdr = (faux_phdr_t *)faux_list_data(iter);
  528. data = (char *)phdr + sizeof(*phdr);
  529. iov[i].iov_base = data;
  530. iov[i].iov_len = faux_phdr_get_len(phdr);
  531. i++;
  532. }
  533. *iov_out = iov;
  534. *iov_num_out = vec_entries_num;
  535. return BOOL_TRUE;
  536. }
  537. /** @brief Sends message to network.
  538. *
  539. * Function sends message to network using preinitialized faux_net_t object.
  540. * User can specify timeout, signal mask, etc while faux_net_t object creation.
  541. *
  542. * Function can return length less than whole message length in the following
  543. * cases:
  544. * - An error has occured like broken file descriptor.
  545. * - Interrupted by allowed signal (see signal mask).
  546. * - Timeout.
  547. *
  548. * @param [in] msg Allocated faux_msg_t object.
  549. * @param [in] faux_net Preinitialized faux_net_t object.
  550. * @return Length of sent data or < 0 on error.
  551. */
  552. ssize_t faux_msg_send(const faux_msg_t *msg, faux_net_t *faux_net)
  553. {
  554. size_t vec_entries_num = 0;
  555. struct iovec *iov = NULL;
  556. size_t ret = 0;
  557. assert(msg);
  558. if (!msg)
  559. return -1;
  560. assert(faux_net);
  561. if (!faux_net)
  562. return -1;
  563. if (!faux_msg_iov(msg, &iov, &vec_entries_num))
  564. return -1;
  565. ret = faux_net_sendv(faux_net, iov, vec_entries_num);
  566. faux_free(iov);
  567. #ifdef DEBUG
  568. // Debug
  569. if (msg && ret > 0 && faux_msg_debug_flag) {
  570. printf("(o) ");
  571. faux_msg_debug(msg);
  572. }
  573. #endif
  574. return ret;
  575. }
  576. /** @brief Sends message to network in async mode.
  577. *
  578. * Function sends message to network using preinitialized faux_async_t object.
  579. *
  580. * @param [in] msg Allocated faux_msg_t object.
  581. * @param [in] async Preinitialized faux_async_t object.
  582. * @return Length of sent data or < 0 on error.
  583. */
  584. ssize_t faux_msg_send_async(const faux_msg_t *msg, faux_async_t *async)
  585. {
  586. size_t vec_entries_num = 0;
  587. struct iovec *iov = NULL;
  588. size_t ret = 0;
  589. assert(msg);
  590. if (!msg)
  591. return -1;
  592. assert(async);
  593. if (!async)
  594. return -1;
  595. if (!faux_msg_iov(msg, &iov, &vec_entries_num))
  596. return -1;
  597. ret = faux_async_writev(async, iov, vec_entries_num);
  598. faux_free(iov);
  599. #ifdef DEBUG
  600. // Debug
  601. if (msg && ret > 0 && faux_msg_debug_flag) {
  602. printf("(o) ");
  603. faux_msg_debug(msg);
  604. }
  605. #endif
  606. return ret;
  607. }
  608. /** @brief Serializes message.
  609. *
  610. * @param [in] msg Allocated faux_msg_t object.
  611. * @param [out] buf Serialized message.
  612. * @param [out] len Length of serialized message.
  613. * @return BOOL_TRUE - success, BOOL_FALSE - fail.
  614. */
  615. bool_t faux_msg_serialize(const faux_msg_t *msg, char **buf, size_t *len)
  616. {
  617. size_t vec_entries_num = 0;
  618. struct iovec *iov = NULL;
  619. unsigned int i = 0;
  620. size_t total_len = 0;
  621. char *buffer = NULL;
  622. char *p = NULL;
  623. if (!faux_msg_iov(msg, &iov, &vec_entries_num))
  624. return BOOL_FALSE;
  625. for (i = 0; i < vec_entries_num; i++)
  626. total_len += iov[i].iov_len;
  627. buffer = faux_malloc(total_len);
  628. p = buffer;
  629. for (i = 0; i < vec_entries_num; i++) {
  630. memcpy(p, iov[i].iov_base, iov[i].iov_len);
  631. p += iov[i].iov_len;
  632. }
  633. *buf = buffer;
  634. *len = total_len;
  635. return BOOL_TRUE;
  636. }
  637. /** @brief Deserializes message header and body to faux_msg_t structure.
  638. *
  639. * The typical case is when message is received to two buffers. The first is
  640. * a header of constant size and the second is message body with size found out
  641. * from already received message header. Function gets header buffer and body
  642. * buffer and deserializes them into faux_msg_t format.
  643. *
  644. * @param [in] hdr Message header.
  645. * @param [in] body Message body.
  646. * @param [in] body_len Length of message body.
  647. * @return Deserialized faux_msg_t object or NULL on error.
  648. */
  649. faux_msg_t *faux_msg_deserialize_parts(const faux_hdr_t *hdr,
  650. const char *body, size_t body_len)
  651. {
  652. faux_msg_t *msg = NULL;
  653. faux_phdr_t *phdr = NULL;
  654. size_t phdr_whole_len = 0;
  655. size_t params_whole_len = 0;
  656. unsigned int i = 0;
  657. const char *data = NULL;
  658. uint32_t param_num = 0;
  659. msg = faux_msg_allocate();
  660. assert(msg);
  661. if (!msg)
  662. return NULL;
  663. // Replace message header by new one
  664. memcpy(msg->hdr, hdr, sizeof(*hdr));
  665. if (0 == body_len) // Message contains header only
  666. return msg;
  667. // Process message body i.e. parameters
  668. param_num = faux_msg_get_param_num(msg);
  669. if (0 == param_num) { // Something went wrong
  670. faux_msg_free(msg);
  671. return NULL;
  672. }
  673. phdr_whole_len = param_num * sizeof(*phdr);
  674. if (phdr_whole_len > body_len) { // Something went wrong
  675. faux_msg_free(msg);
  676. return NULL;
  677. }
  678. phdr = (faux_phdr_t *)body;
  679. // Find out whole parameters length
  680. for (i = 0; i < param_num; i++)
  681. params_whole_len += faux_phdr_get_len(phdr + i);
  682. if ((phdr_whole_len + params_whole_len) != body_len) { // Something went wrong
  683. faux_msg_free(msg);
  684. return NULL;
  685. }
  686. // Parameters
  687. data = body + phdr_whole_len;
  688. for (i = 0; i < param_num; i++) {
  689. size_t cur_data_len = faux_phdr_get_len(phdr + i);
  690. if (0 == cur_data_len)
  691. continue;
  692. faux_msg_add_param_internal(msg,
  693. faux_phdr_get_type(phdr + i),
  694. data, cur_data_len, BOOL_FALSE);
  695. data += cur_data_len;
  696. }
  697. return msg;
  698. }
  699. /* @brief Deserialized message stored in linear buffer.
  700. *
  701. * Message header and message body can be stored in linear buffer. Function
  702. * deserializes it from network format to faux_msg_t structure.
  703. *
  704. * @param [in] data Message in network format.
  705. * @param [in] len Message length.
  706. * @return Deserialized faux_msg_t object or NULL on error.
  707. */
  708. faux_msg_t *faux_msg_deserialize(const char *data, size_t len)
  709. {
  710. const faux_hdr_t *msg_hdr = (const faux_hdr_t *)data;
  711. const char *msg_body = data + sizeof(*msg_hdr);
  712. size_t msg_body_len = len - sizeof(*msg_hdr);
  713. assert(data);
  714. if (!data)
  715. return NULL;
  716. if (len < sizeof(*msg_hdr))
  717. return NULL;
  718. return faux_msg_deserialize_parts(msg_hdr, msg_body, msg_body_len);
  719. }
  720. /** @brief Receives full message and allocates faux_msg_t object for it.
  721. *
  722. * Function receives message from network using preinitialized faux_net_t object.
  723. * User can specify timeout, signal mask, etc while faux_net_t object creation.
  724. *
  725. * Function can return length less than whole message length in the following
  726. * cases:
  727. * - An error has occured like broken file descriptor.
  728. * - Interrupted by allowed signal (see signal mask).
  729. * - Timeout.
  730. *
  731. * @param [in] faux_net Preinitialized faux_net_t object.
  732. * @param [out] status Status while message receiving. Can be NULL.
  733. * @return Allocated faux_msg_t object. Object contains received message.
  734. */
  735. faux_msg_t *faux_msg_recv(faux_net_t *faux_net)
  736. {
  737. faux_msg_t *msg = NULL;
  738. size_t received = 0;
  739. faux_hdr_t hdr = {};
  740. char *body = NULL;
  741. size_t body_len = 0;
  742. // Receive message header
  743. received = faux_net_recv(faux_net, &hdr, sizeof(hdr));
  744. if (received != sizeof(hdr))
  745. return NULL;
  746. body_len = faux_hdr_len(&hdr) - sizeof(hdr);
  747. if (body_len > 0) {
  748. body = faux_malloc(body_len);
  749. received = faux_net_recv(faux_net, body, body_len);
  750. if (received != body_len) {
  751. faux_free(body);
  752. return NULL;
  753. }
  754. }
  755. msg = faux_msg_deserialize_parts(&hdr, body, body_len);
  756. faux_free(body);
  757. #ifdef DEBUG
  758. // Debug
  759. if (msg && faux_msg_debug_flag) {
  760. printf("(i) ");
  761. faux_msg_debug(msg);
  762. }
  763. #endif
  764. return msg;
  765. }
  766. /** @brief Prints message debug info.
  767. *
  768. * Function prints header values and parameters.
  769. *
  770. * @param [in] msg Allocated faux_msg_t object.
  771. */
  772. void faux_msg_debug(const faux_msg_t *msg)
  773. #ifdef DEBUG
  774. {
  775. faux_list_node_t *iter = 0;
  776. // Parameter vars
  777. void *param_data = NULL;
  778. uint16_t param_type = 0;
  779. uint32_t param_len = 0;
  780. assert(msg);
  781. if (!msg)
  782. return;
  783. // Header
  784. printf("%x(%u.%u): c%04x s%08x i%08x p%u l%u |%lub\n",
  785. faux_msg_get_magic(msg),
  786. faux_msg_get_major(msg),
  787. faux_msg_get_minor(msg),
  788. faux_msg_get_cmd(msg),
  789. faux_msg_get_status(msg),
  790. faux_msg_get_req_id(msg),
  791. faux_msg_get_param_num(msg),
  792. faux_msg_get_len(msg),
  793. sizeof(*msg->hdr)
  794. );
  795. // Parameters
  796. iter = faux_msg_init_param_iter(msg);
  797. while (faux_msg_get_param_each(&iter, &param_type, &param_data, &param_len)) {
  798. printf(" t%04x l%u |%lub\n",
  799. param_type,
  800. param_len,
  801. sizeof(faux_phdr_t) + param_len
  802. );
  803. }
  804. }
  805. #else
  806. {
  807. msg = msg; // Happy compiler
  808. }
  809. #endif