msg.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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. if (len > 0)
  328. memcpy(param + sizeof(*phdr), buf, len);
  329. if (update_len) {
  330. // Update number of parameters
  331. faux_msg_set_param_num(msg, faux_msg_get_param_num(msg) + 1);
  332. // Update whole message length
  333. faux_msg_set_len(msg,
  334. faux_msg_get_len(msg) + sizeof(*phdr) + len);
  335. }
  336. // Add to parameter list
  337. faux_list_add(msg->params, param);
  338. return len;
  339. }
  340. /** @brief Adds parameter to message.
  341. *
  342. * @param [in] msg Allocated faux_msg_t object.
  343. * @param [in] type Type of parameter.
  344. * @param [in] buf Parameter's data buffer.
  345. * @param [in] len Parameter's data length.
  346. * @return Length of parameter's data or < 0 on error.
  347. */
  348. ssize_t faux_msg_add_param(faux_msg_t *msg, uint16_t type,
  349. const void *buf, size_t len)
  350. {
  351. return faux_msg_add_param_internal(msg, type, buf, len, BOOL_TRUE);
  352. }
  353. /** @brief Initializes iterator to iterate through the message parameters.
  354. *
  355. * The iterator must be initialized before iteration.
  356. *
  357. * @param [in] msg Allocated faux_msg_t object.
  358. * @return Initialized iterator.
  359. */
  360. faux_list_node_t *faux_msg_init_param_iter(const faux_msg_t *msg)
  361. {
  362. assert(msg);
  363. assert(msg->params);
  364. if (!msg || !msg->params)
  365. return NULL;
  366. return faux_list_head(msg->params);
  367. }
  368. /** @brief Internal function to get parameter's data by node (faux_list_node_t).
  369. *
  370. * Note function returns the main data by output arguments.
  371. *
  372. * @param [in] node Node from the parameter's list.
  373. * @param [out] param_type Type of parameter.
  374. * @param [out] param_buf Parameter's data buffer.
  375. * @param [out] param_len Parameter's data length.
  376. * @return Pointer to parameter's header or NULL on error.
  377. */
  378. static faux_phdr_t *faux_msg_get_param_by_node(const faux_list_node_t *node,
  379. uint16_t *param_type, void **param_data, uint32_t *param_len)
  380. {
  381. char *param = NULL;
  382. faux_phdr_t *phdr = NULL;
  383. char *data = NULL;
  384. if (!node)
  385. return NULL;
  386. param = faux_list_data(node);
  387. phdr = (faux_phdr_t *)param;
  388. data = param + sizeof(*phdr);
  389. if (param_type)
  390. *param_type = faux_phdr_get_type(phdr);
  391. if (param_len)
  392. *param_len = faux_phdr_get_len(phdr);
  393. if (param_data)
  394. *param_data = data;
  395. return phdr;
  396. }
  397. /** @brief Iterate through the message parameters.
  398. *
  399. * First parameter (iterator/node) must be initialized first by
  400. * faux_msg_init_param_iter().
  401. *
  402. * @param [in] node Initialized iterator of parameter list.
  403. * @param [out] param_type Type of parameter.
  404. * @param [out] param_buf Parameter's data buffer.
  405. * @param [out] param_len Parameter's data length.
  406. * @return Pointer to parameter's header or NULL on error.
  407. */
  408. faux_phdr_t *faux_msg_get_param_each(faux_list_node_t **node,
  409. uint16_t *param_type, void **param_data, uint32_t *param_len)
  410. {
  411. faux_list_node_t *current_node = NULL;
  412. if (!node || !*node)
  413. return NULL;
  414. current_node = *node;
  415. *node = faux_list_next_node(current_node);
  416. return faux_msg_get_param_by_node(current_node,
  417. param_type, param_data, param_len);
  418. }
  419. /** @brief Gets message parameter by the index.
  420. *
  421. * @param [in] msg Allocated faux_msg_t object.
  422. * @param [in] index Parameter's index.
  423. * @param [out] param_type Type of parameter.
  424. * @param [out] param_buf Parameter's data buffer.
  425. * @param [out] param_len Parameter's data length.
  426. * @return Pointer to parameter's header or NULL on error.
  427. */
  428. faux_phdr_t *faux_msg_get_param_by_index(const faux_msg_t *msg, unsigned int index,
  429. uint16_t *param_type, void **param_data, uint32_t *param_len)
  430. {
  431. faux_list_node_t *iter = NULL;
  432. unsigned int i = 0;
  433. assert(msg);
  434. assert(msg->hdr);
  435. if (!msg || !msg->hdr)
  436. return NULL;
  437. if (index >= faux_msg_get_param_num(msg)) // Non-existent entry
  438. return NULL;
  439. iter = faux_msg_init_param_iter(msg);
  440. while ((i != index) && iter) {
  441. i++;
  442. iter = faux_list_next_node(iter);
  443. }
  444. return faux_msg_get_param_by_node(iter,
  445. param_type, param_data, param_len);
  446. }
  447. /** @brief Gets message parameter by parameter's type.
  448. *
  449. * Note message can contain many parameters with the same type. This function
  450. * will find only the first parameter with specified type. You can iterate
  451. * through all parameters to find all entries with type you need.
  452. *
  453. * @param [in] msg Allocated faux_msg_t object.
  454. * @param [in] param_type Type of parameter.
  455. * @param [out] param_buf Parameter's data buffer.
  456. * @param [out] param_len Parameter's data length.
  457. * @return Pointer to parameter's header or NULL on error.
  458. */
  459. faux_phdr_t *faux_msg_get_param_by_type(const faux_msg_t *msg,
  460. uint16_t param_type, void **param_data, uint32_t *param_len)
  461. {
  462. faux_list_node_t *iter = NULL;
  463. assert(msg);
  464. assert(msg->hdr);
  465. if (!msg || !msg->hdr)
  466. return NULL;
  467. for (iter = faux_msg_init_param_iter(msg);
  468. iter; iter = faux_list_next_node(iter)) {
  469. faux_phdr_t *phdr = NULL;
  470. phdr = (faux_phdr_t *)faux_list_data(iter);
  471. if (faux_phdr_get_type(phdr) == param_type)
  472. return faux_msg_get_param_by_node(iter,
  473. NULL, param_data, param_len);
  474. }
  475. // Not found
  476. return NULL;
  477. }
  478. /** @brief Gets message string parameter by parameter's type.
  479. *
  480. * It's the same as faux_msg_get_param_by_type() but it's supposed
  481. * the parameter contains text string without ending '\0'. Function
  482. * will create C-string dup of this parameter. The resulting line must be freed
  483. * later with faux_str_free().
  484. *
  485. * @sa faux_msg_get_param_by_type()
  486. * @param [in] msg Allocated faux_msg_t object.
  487. * @param [in] param_type Type of parameter.
  488. * @return Pointer to allocated C-string or NULL on error.
  489. */
  490. char *faux_msg_get_str_param_by_type(const faux_msg_t *msg,
  491. uint16_t param_type)
  492. {
  493. const char *raw = NULL;
  494. uint32_t raw_len = 0;
  495. char *line = NULL;
  496. if (!faux_msg_get_param_by_type(msg, param_type,
  497. (void **)&raw, &raw_len))
  498. return NULL;
  499. line = faux_str_dupn(raw, raw_len);
  500. return line;
  501. }
  502. /** @brief Create IOV of message.
  503. *
  504. * Function creates and fills iovec structure. This iovec contains references
  505. * to parts of message enough to construct message in network format.
  506. *
  507. * @param [in] msg Allocated faux_msg_t object.
  508. * @param [out] iov_out iovec structure.
  509. * @param [out] iov_num_out Number of iovec entries.
  510. * @return BOOL_TRUE - success, BOOL_FALSE - fail.
  511. */
  512. bool_t faux_msg_iov(const faux_msg_t *msg, struct iovec **iov_out, size_t *iov_num_out)
  513. {
  514. size_t vec_entries_num = 0;
  515. struct iovec *iov = NULL;
  516. unsigned int i = 0;
  517. faux_list_node_t *iter = NULL;
  518. assert(msg);
  519. if (!msg)
  520. return BOOL_FALSE;
  521. assert(msg->hdr);
  522. if (!msg->hdr)
  523. return BOOL_FALSE;
  524. assert(iov_out);
  525. if (!iov_out)
  526. return BOOL_FALSE;
  527. assert(iov_num_out);
  528. if (!iov_num_out)
  529. return BOOL_FALSE;
  530. // Calculate number if struct iovec entries.
  531. // n = (msg header) + ((param hdr) + (param data)) * (param_num)
  532. vec_entries_num = 1 + (2 * faux_msg_get_param_num(msg));
  533. iov = faux_zmalloc(vec_entries_num * sizeof(*iov));
  534. // Message header
  535. iov[i].iov_base = msg->hdr;
  536. iov[i].iov_len = sizeof(*msg->hdr);
  537. i++;
  538. // Parameter headers
  539. for (iter = faux_msg_init_param_iter(msg);
  540. iter; iter = faux_list_next_node(iter)) {
  541. faux_phdr_t *phdr = NULL;
  542. phdr = (faux_phdr_t *)faux_list_data(iter);
  543. iov[i].iov_base = phdr;
  544. iov[i].iov_len = sizeof(*phdr);
  545. i++;
  546. }
  547. // Parameter data
  548. for (iter = faux_msg_init_param_iter(msg);
  549. iter; iter = faux_list_next_node(iter)) {
  550. faux_phdr_t *phdr = NULL;
  551. void *data = NULL;
  552. phdr = (faux_phdr_t *)faux_list_data(iter);
  553. data = (char *)phdr + sizeof(*phdr);
  554. iov[i].iov_base = data;
  555. iov[i].iov_len = faux_phdr_get_len(phdr);
  556. i++;
  557. }
  558. *iov_out = iov;
  559. *iov_num_out = vec_entries_num;
  560. return BOOL_TRUE;
  561. }
  562. /** @brief Sends message to network.
  563. *
  564. * Function sends message to network using preinitialized faux_net_t object.
  565. * User can specify timeout, signal mask, etc while faux_net_t object creation.
  566. *
  567. * Function can return length less than whole message length in the following
  568. * cases:
  569. * - An error has occured like broken file descriptor.
  570. * - Interrupted by allowed signal (see signal mask).
  571. * - Timeout.
  572. *
  573. * @param [in] msg Allocated faux_msg_t object.
  574. * @param [in] faux_net Preinitialized faux_net_t object.
  575. * @return Length of sent data or < 0 on error.
  576. */
  577. ssize_t faux_msg_send(const faux_msg_t *msg, faux_net_t *faux_net)
  578. {
  579. size_t vec_entries_num = 0;
  580. struct iovec *iov = NULL;
  581. size_t ret = 0;
  582. assert(msg);
  583. if (!msg)
  584. return -1;
  585. assert(faux_net);
  586. if (!faux_net)
  587. return -1;
  588. if (!faux_msg_iov(msg, &iov, &vec_entries_num))
  589. return -1;
  590. ret = faux_net_sendv(faux_net, iov, vec_entries_num);
  591. faux_free(iov);
  592. #ifdef DEBUG
  593. // Debug
  594. if (msg && ret > 0 && faux_msg_debug_flag) {
  595. printf("(o) ");
  596. faux_msg_debug(msg);
  597. }
  598. #endif
  599. return ret;
  600. }
  601. /** @brief Sends message to network in async mode.
  602. *
  603. * Function sends message to network using preinitialized faux_async_t object.
  604. *
  605. * @param [in] msg Allocated faux_msg_t object.
  606. * @param [in] async Preinitialized faux_async_t object.
  607. * @return Length of sent data or < 0 on error.
  608. */
  609. ssize_t faux_msg_send_async(const faux_msg_t *msg, faux_async_t *async)
  610. {
  611. size_t vec_entries_num = 0;
  612. struct iovec *iov = NULL;
  613. size_t ret = 0;
  614. assert(msg);
  615. if (!msg)
  616. return -1;
  617. assert(async);
  618. if (!async)
  619. return -1;
  620. if (!faux_msg_iov(msg, &iov, &vec_entries_num))
  621. return -1;
  622. ret = faux_async_writev(async, iov, vec_entries_num);
  623. faux_free(iov);
  624. #ifdef DEBUG
  625. // Debug
  626. if (msg && ret > 0 && faux_msg_debug_flag) {
  627. printf("(o) ");
  628. faux_msg_debug(msg);
  629. }
  630. #endif
  631. return ret;
  632. }
  633. /** @brief Serializes message.
  634. *
  635. * @param [in] msg Allocated faux_msg_t object.
  636. * @param [out] buf Serialized message.
  637. * @param [out] len Length of serialized message.
  638. * @return BOOL_TRUE - success, BOOL_FALSE - fail.
  639. */
  640. bool_t faux_msg_serialize(const faux_msg_t *msg, char **buf, size_t *len)
  641. {
  642. size_t vec_entries_num = 0;
  643. struct iovec *iov = NULL;
  644. unsigned int i = 0;
  645. size_t total_len = 0;
  646. char *buffer = NULL;
  647. char *p = NULL;
  648. if (!faux_msg_iov(msg, &iov, &vec_entries_num))
  649. return BOOL_FALSE;
  650. for (i = 0; i < vec_entries_num; i++)
  651. total_len += iov[i].iov_len;
  652. buffer = faux_malloc(total_len);
  653. p = buffer;
  654. for (i = 0; i < vec_entries_num; i++) {
  655. memcpy(p, iov[i].iov_base, iov[i].iov_len);
  656. p += iov[i].iov_len;
  657. }
  658. *buf = buffer;
  659. *len = total_len;
  660. return BOOL_TRUE;
  661. }
  662. /** @brief Deserializes message header and body to faux_msg_t structure.
  663. *
  664. * The typical case is when message is received to two buffers. The first is
  665. * a header of constant size and the second is message body with size found out
  666. * from already received message header. Function gets header buffer and body
  667. * buffer and deserializes them into faux_msg_t format.
  668. *
  669. * @param [in] hdr Message header.
  670. * @param [in] body Message body.
  671. * @param [in] body_len Length of message body.
  672. * @return Deserialized faux_msg_t object or NULL on error.
  673. */
  674. faux_msg_t *faux_msg_deserialize_parts(const faux_hdr_t *hdr,
  675. const char *body, size_t body_len)
  676. {
  677. faux_msg_t *msg = NULL;
  678. faux_phdr_t *phdr = NULL;
  679. size_t phdr_whole_len = 0;
  680. size_t params_whole_len = 0;
  681. unsigned int i = 0;
  682. const char *data = NULL;
  683. uint32_t param_num = 0;
  684. msg = faux_msg_allocate();
  685. assert(msg);
  686. if (!msg)
  687. return NULL;
  688. // Replace message header by new one
  689. memcpy(msg->hdr, hdr, sizeof(*hdr));
  690. if (0 == body_len) // Message contains header only
  691. return msg;
  692. // Process message body i.e. parameters
  693. param_num = faux_msg_get_param_num(msg);
  694. if (0 == param_num) { // Something went wrong
  695. faux_msg_free(msg);
  696. return NULL;
  697. }
  698. phdr_whole_len = param_num * sizeof(*phdr);
  699. if (phdr_whole_len > body_len) { // Something went wrong
  700. faux_msg_free(msg);
  701. return NULL;
  702. }
  703. phdr = (faux_phdr_t *)body;
  704. // Find out whole parameters length
  705. for (i = 0; i < param_num; i++)
  706. params_whole_len += faux_phdr_get_len(phdr + i);
  707. if ((phdr_whole_len + params_whole_len) != body_len) { // Something went wrong
  708. faux_msg_free(msg);
  709. return NULL;
  710. }
  711. // Parameters
  712. data = body + phdr_whole_len;
  713. for (i = 0; i < param_num; i++) {
  714. size_t cur_data_len = faux_phdr_get_len(phdr + i);
  715. faux_msg_add_param_internal(msg,
  716. faux_phdr_get_type(phdr + i),
  717. data, cur_data_len, BOOL_FALSE);
  718. data += cur_data_len;
  719. }
  720. return msg;
  721. }
  722. /* @brief Deserialized message stored in linear buffer.
  723. *
  724. * Message header and message body can be stored in linear buffer. Function
  725. * deserializes it from network format to faux_msg_t structure.
  726. *
  727. * @param [in] data Message in network format.
  728. * @param [in] len Message length.
  729. * @return Deserialized faux_msg_t object or NULL on error.
  730. */
  731. faux_msg_t *faux_msg_deserialize(const char *data, size_t len)
  732. {
  733. const faux_hdr_t *msg_hdr = (const faux_hdr_t *)data;
  734. const char *msg_body = data + sizeof(*msg_hdr);
  735. size_t msg_body_len = len - sizeof(*msg_hdr);
  736. assert(data);
  737. if (!data)
  738. return NULL;
  739. if (len < sizeof(*msg_hdr))
  740. return NULL;
  741. return faux_msg_deserialize_parts(msg_hdr, msg_body, msg_body_len);
  742. }
  743. /** @brief Receives full message and allocates faux_msg_t object for it.
  744. *
  745. * Function receives message from network using preinitialized faux_net_t object.
  746. * User can specify timeout, signal mask, etc while faux_net_t object creation.
  747. *
  748. * Function can return length less than whole message length in the following
  749. * cases:
  750. * - An error has occured like broken file descriptor.
  751. * - Interrupted by allowed signal (see signal mask).
  752. * - Timeout.
  753. *
  754. * @param [in] faux_net Preinitialized faux_net_t object.
  755. * @param [out] status Status while message receiving. Can be NULL.
  756. * @return Allocated faux_msg_t object. Object contains received message.
  757. */
  758. faux_msg_t *faux_msg_recv(faux_net_t *faux_net)
  759. {
  760. faux_msg_t *msg = NULL;
  761. size_t received = 0;
  762. faux_hdr_t hdr = {};
  763. char *body = NULL;
  764. size_t body_len = 0;
  765. // Receive message header
  766. received = faux_net_recv(faux_net, &hdr, sizeof(hdr));
  767. if (received != sizeof(hdr))
  768. return NULL;
  769. body_len = faux_hdr_len(&hdr) - sizeof(hdr);
  770. if (body_len > 0) {
  771. body = faux_malloc(body_len);
  772. received = faux_net_recv(faux_net, body, body_len);
  773. if (received != body_len) {
  774. faux_free(body);
  775. return NULL;
  776. }
  777. }
  778. msg = faux_msg_deserialize_parts(&hdr, body, body_len);
  779. faux_free(body);
  780. #ifdef DEBUG
  781. // Debug
  782. if (msg && faux_msg_debug_flag) {
  783. printf("(i) ");
  784. faux_msg_debug(msg);
  785. }
  786. #endif
  787. return msg;
  788. }
  789. /** @brief Prints message debug info.
  790. *
  791. * Function prints header values and parameters.
  792. *
  793. * @param [in] msg Allocated faux_msg_t object.
  794. */
  795. void faux_msg_debug(const faux_msg_t *msg)
  796. #ifdef DEBUG
  797. {
  798. faux_list_node_t *iter = 0;
  799. // Parameter vars
  800. void *param_data = NULL;
  801. uint16_t param_type = 0;
  802. uint32_t param_len = 0;
  803. assert(msg);
  804. if (!msg)
  805. return;
  806. // Header
  807. printf("%x(%u.%u): c%04x s%08x i%08x p%u l%u |%lub\n",
  808. faux_msg_get_magic(msg),
  809. faux_msg_get_major(msg),
  810. faux_msg_get_minor(msg),
  811. faux_msg_get_cmd(msg),
  812. faux_msg_get_status(msg),
  813. faux_msg_get_req_id(msg),
  814. faux_msg_get_param_num(msg),
  815. faux_msg_get_len(msg),
  816. sizeof(*msg->hdr)
  817. );
  818. // Parameters
  819. iter = faux_msg_init_param_iter(msg);
  820. while (faux_msg_get_param_each(&iter, &param_type, &param_data, &param_len)) {
  821. printf(" t%04x l%u |%lub\n",
  822. param_type,
  823. param_len,
  824. sizeof(faux_phdr_t) + param_len
  825. );
  826. }
  827. }
  828. #else
  829. {
  830. msg = msg; // Happy compiler
  831. }
  832. #endif