msg.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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 Gets message string parameter by parameter's type.
  478. *
  479. * It's the same as faux_msg_get_param_by_type() but it's supposed
  480. * the parameter contains text string without ending '\0'. Function
  481. * will create C-string dup of this parameter. The resulting line must be freed
  482. * later with faux_str_free().
  483. *
  484. * @sa faux_msg_get_param_by_type()
  485. * @param [in] msg Allocated faux_msg_t object.
  486. * @param [in] param_type Type of parameter.
  487. * @return Pointer to allocated C-string or NULL on error.
  488. */
  489. char *faux_msg_get_str_param_by_type(const faux_msg_t *msg,
  490. uint16_t param_type)
  491. {
  492. const char *raw = NULL;
  493. uint32_t raw_len = 0;
  494. char *line = NULL;
  495. if (!faux_msg_get_param_by_type(msg, param_type,
  496. (void **)&raw, &raw_len))
  497. return NULL;
  498. line = faux_str_dupn(raw, raw_len);
  499. return line;
  500. }
  501. /** @brief Create IOV of message.
  502. *
  503. * Function creates and fills iovec structure. This iovec contains references
  504. * to parts of message enough to construct message in network format.
  505. *
  506. * @param [in] msg Allocated faux_msg_t object.
  507. * @param [out] iov_out iovec structure.
  508. * @param [out] iov_num_out Number of iovec entries.
  509. * @return BOOL_TRUE - success, BOOL_FALSE - fail.
  510. */
  511. bool_t faux_msg_iov(const faux_msg_t *msg, struct iovec **iov_out, size_t *iov_num_out)
  512. {
  513. size_t vec_entries_num = 0;
  514. struct iovec *iov = NULL;
  515. unsigned int i = 0;
  516. faux_list_node_t *iter = NULL;
  517. assert(msg);
  518. if (!msg)
  519. return BOOL_FALSE;
  520. assert(msg->hdr);
  521. if (!msg->hdr)
  522. return BOOL_FALSE;
  523. assert(iov_out);
  524. if (!iov_out)
  525. return BOOL_FALSE;
  526. assert(iov_num_out);
  527. if (!iov_num_out)
  528. return BOOL_FALSE;
  529. // Calculate number if struct iovec entries.
  530. // n = (msg header) + ((param hdr) + (param data)) * (param_num)
  531. vec_entries_num = 1 + (2 * faux_msg_get_param_num(msg));
  532. iov = faux_zmalloc(vec_entries_num * sizeof(*iov));
  533. // Message header
  534. iov[i].iov_base = msg->hdr;
  535. iov[i].iov_len = sizeof(*msg->hdr);
  536. i++;
  537. // Parameter headers
  538. for (iter = faux_msg_init_param_iter(msg);
  539. iter; iter = faux_list_next_node(iter)) {
  540. faux_phdr_t *phdr = NULL;
  541. phdr = (faux_phdr_t *)faux_list_data(iter);
  542. iov[i].iov_base = phdr;
  543. iov[i].iov_len = sizeof(*phdr);
  544. i++;
  545. }
  546. // Parameter data
  547. for (iter = faux_msg_init_param_iter(msg);
  548. iter; iter = faux_list_next_node(iter)) {
  549. faux_phdr_t *phdr = NULL;
  550. void *data = NULL;
  551. phdr = (faux_phdr_t *)faux_list_data(iter);
  552. data = (char *)phdr + sizeof(*phdr);
  553. iov[i].iov_base = data;
  554. iov[i].iov_len = faux_phdr_get_len(phdr);
  555. i++;
  556. }
  557. *iov_out = iov;
  558. *iov_num_out = vec_entries_num;
  559. return BOOL_TRUE;
  560. }
  561. /** @brief Sends message to network.
  562. *
  563. * Function sends message to network using preinitialized faux_net_t object.
  564. * User can specify timeout, signal mask, etc while faux_net_t object creation.
  565. *
  566. * Function can return length less than whole message length in the following
  567. * cases:
  568. * - An error has occured like broken file descriptor.
  569. * - Interrupted by allowed signal (see signal mask).
  570. * - Timeout.
  571. *
  572. * @param [in] msg Allocated faux_msg_t object.
  573. * @param [in] faux_net Preinitialized faux_net_t object.
  574. * @return Length of sent data or < 0 on error.
  575. */
  576. ssize_t faux_msg_send(const faux_msg_t *msg, faux_net_t *faux_net)
  577. {
  578. size_t vec_entries_num = 0;
  579. struct iovec *iov = NULL;
  580. size_t ret = 0;
  581. assert(msg);
  582. if (!msg)
  583. return -1;
  584. assert(faux_net);
  585. if (!faux_net)
  586. return -1;
  587. if (!faux_msg_iov(msg, &iov, &vec_entries_num))
  588. return -1;
  589. ret = faux_net_sendv(faux_net, iov, vec_entries_num);
  590. faux_free(iov);
  591. #ifdef DEBUG
  592. // Debug
  593. if (msg && ret > 0 && faux_msg_debug_flag) {
  594. printf("(o) ");
  595. faux_msg_debug(msg);
  596. }
  597. #endif
  598. return ret;
  599. }
  600. /** @brief Sends message to network in async mode.
  601. *
  602. * Function sends message to network using preinitialized faux_async_t object.
  603. *
  604. * @param [in] msg Allocated faux_msg_t object.
  605. * @param [in] async Preinitialized faux_async_t object.
  606. * @return Length of sent data or < 0 on error.
  607. */
  608. ssize_t faux_msg_send_async(const faux_msg_t *msg, faux_async_t *async)
  609. {
  610. size_t vec_entries_num = 0;
  611. struct iovec *iov = NULL;
  612. size_t ret = 0;
  613. assert(msg);
  614. if (!msg)
  615. return -1;
  616. assert(async);
  617. if (!async)
  618. return -1;
  619. if (!faux_msg_iov(msg, &iov, &vec_entries_num))
  620. return -1;
  621. ret = faux_async_writev(async, iov, vec_entries_num);
  622. faux_free(iov);
  623. #ifdef DEBUG
  624. // Debug
  625. if (msg && ret > 0 && faux_msg_debug_flag) {
  626. printf("(o) ");
  627. faux_msg_debug(msg);
  628. }
  629. #endif
  630. return ret;
  631. }
  632. /** @brief Serializes message.
  633. *
  634. * @param [in] msg Allocated faux_msg_t object.
  635. * @param [out] buf Serialized message.
  636. * @param [out] len Length of serialized message.
  637. * @return BOOL_TRUE - success, BOOL_FALSE - fail.
  638. */
  639. bool_t faux_msg_serialize(const faux_msg_t *msg, char **buf, size_t *len)
  640. {
  641. size_t vec_entries_num = 0;
  642. struct iovec *iov = NULL;
  643. unsigned int i = 0;
  644. size_t total_len = 0;
  645. char *buffer = NULL;
  646. char *p = NULL;
  647. if (!faux_msg_iov(msg, &iov, &vec_entries_num))
  648. return BOOL_FALSE;
  649. for (i = 0; i < vec_entries_num; i++)
  650. total_len += iov[i].iov_len;
  651. buffer = faux_malloc(total_len);
  652. p = buffer;
  653. for (i = 0; i < vec_entries_num; i++) {
  654. memcpy(p, iov[i].iov_base, iov[i].iov_len);
  655. p += iov[i].iov_len;
  656. }
  657. *buf = buffer;
  658. *len = total_len;
  659. return BOOL_TRUE;
  660. }
  661. /** @brief Deserializes message header and body to faux_msg_t structure.
  662. *
  663. * The typical case is when message is received to two buffers. The first is
  664. * a header of constant size and the second is message body with size found out
  665. * from already received message header. Function gets header buffer and body
  666. * buffer and deserializes them into faux_msg_t format.
  667. *
  668. * @param [in] hdr Message header.
  669. * @param [in] body Message body.
  670. * @param [in] body_len Length of message body.
  671. * @return Deserialized faux_msg_t object or NULL on error.
  672. */
  673. faux_msg_t *faux_msg_deserialize_parts(const faux_hdr_t *hdr,
  674. const char *body, size_t body_len)
  675. {
  676. faux_msg_t *msg = NULL;
  677. faux_phdr_t *phdr = NULL;
  678. size_t phdr_whole_len = 0;
  679. size_t params_whole_len = 0;
  680. unsigned int i = 0;
  681. const char *data = NULL;
  682. uint32_t param_num = 0;
  683. msg = faux_msg_allocate();
  684. assert(msg);
  685. if (!msg)
  686. return NULL;
  687. // Replace message header by new one
  688. memcpy(msg->hdr, hdr, sizeof(*hdr));
  689. if (0 == body_len) // Message contains header only
  690. return msg;
  691. // Process message body i.e. parameters
  692. param_num = faux_msg_get_param_num(msg);
  693. if (0 == param_num) { // Something went wrong
  694. faux_msg_free(msg);
  695. return NULL;
  696. }
  697. phdr_whole_len = param_num * sizeof(*phdr);
  698. if (phdr_whole_len > body_len) { // Something went wrong
  699. faux_msg_free(msg);
  700. return NULL;
  701. }
  702. phdr = (faux_phdr_t *)body;
  703. // Find out whole parameters length
  704. for (i = 0; i < param_num; i++)
  705. params_whole_len += faux_phdr_get_len(phdr + i);
  706. if ((phdr_whole_len + params_whole_len) != body_len) { // Something went wrong
  707. faux_msg_free(msg);
  708. return NULL;
  709. }
  710. // Parameters
  711. data = body + phdr_whole_len;
  712. for (i = 0; i < param_num; i++) {
  713. size_t cur_data_len = faux_phdr_get_len(phdr + i);
  714. if (0 == cur_data_len)
  715. continue;
  716. faux_msg_add_param_internal(msg,
  717. faux_phdr_get_type(phdr + i),
  718. data, cur_data_len, BOOL_FALSE);
  719. data += cur_data_len;
  720. }
  721. return msg;
  722. }
  723. /* @brief Deserialized message stored in linear buffer.
  724. *
  725. * Message header and message body can be stored in linear buffer. Function
  726. * deserializes it from network format to faux_msg_t structure.
  727. *
  728. * @param [in] data Message in network format.
  729. * @param [in] len Message length.
  730. * @return Deserialized faux_msg_t object or NULL on error.
  731. */
  732. faux_msg_t *faux_msg_deserialize(const char *data, size_t len)
  733. {
  734. const faux_hdr_t *msg_hdr = (const faux_hdr_t *)data;
  735. const char *msg_body = data + sizeof(*msg_hdr);
  736. size_t msg_body_len = len - sizeof(*msg_hdr);
  737. assert(data);
  738. if (!data)
  739. return NULL;
  740. if (len < sizeof(*msg_hdr))
  741. return NULL;
  742. return faux_msg_deserialize_parts(msg_hdr, msg_body, msg_body_len);
  743. }
  744. /** @brief Receives full message and allocates faux_msg_t object for it.
  745. *
  746. * Function receives message from network using preinitialized faux_net_t object.
  747. * User can specify timeout, signal mask, etc while faux_net_t object creation.
  748. *
  749. * Function can return length less than whole message length in the following
  750. * cases:
  751. * - An error has occured like broken file descriptor.
  752. * - Interrupted by allowed signal (see signal mask).
  753. * - Timeout.
  754. *
  755. * @param [in] faux_net Preinitialized faux_net_t object.
  756. * @param [out] status Status while message receiving. Can be NULL.
  757. * @return Allocated faux_msg_t object. Object contains received message.
  758. */
  759. faux_msg_t *faux_msg_recv(faux_net_t *faux_net)
  760. {
  761. faux_msg_t *msg = NULL;
  762. size_t received = 0;
  763. faux_hdr_t hdr = {};
  764. char *body = NULL;
  765. size_t body_len = 0;
  766. // Receive message header
  767. received = faux_net_recv(faux_net, &hdr, sizeof(hdr));
  768. if (received != sizeof(hdr))
  769. return NULL;
  770. body_len = faux_hdr_len(&hdr) - sizeof(hdr);
  771. if (body_len > 0) {
  772. body = faux_malloc(body_len);
  773. received = faux_net_recv(faux_net, body, body_len);
  774. if (received != body_len) {
  775. faux_free(body);
  776. return NULL;
  777. }
  778. }
  779. msg = faux_msg_deserialize_parts(&hdr, body, body_len);
  780. faux_free(body);
  781. #ifdef DEBUG
  782. // Debug
  783. if (msg && faux_msg_debug_flag) {
  784. printf("(i) ");
  785. faux_msg_debug(msg);
  786. }
  787. #endif
  788. return msg;
  789. }
  790. /** @brief Prints message debug info.
  791. *
  792. * Function prints header values and parameters.
  793. *
  794. * @param [in] msg Allocated faux_msg_t object.
  795. */
  796. void faux_msg_debug(const faux_msg_t *msg)
  797. #ifdef DEBUG
  798. {
  799. faux_list_node_t *iter = 0;
  800. // Parameter vars
  801. void *param_data = NULL;
  802. uint16_t param_type = 0;
  803. uint32_t param_len = 0;
  804. assert(msg);
  805. if (!msg)
  806. return;
  807. // Header
  808. printf("%x(%u.%u): c%04x s%08x i%08x p%u l%u |%lub\n",
  809. faux_msg_get_magic(msg),
  810. faux_msg_get_major(msg),
  811. faux_msg_get_minor(msg),
  812. faux_msg_get_cmd(msg),
  813. faux_msg_get_status(msg),
  814. faux_msg_get_req_id(msg),
  815. faux_msg_get_param_num(msg),
  816. faux_msg_get_len(msg),
  817. sizeof(*msg->hdr)
  818. );
  819. // Parameters
  820. iter = faux_msg_init_param_iter(msg);
  821. while (faux_msg_get_param_each(&iter, &param_type, &param_data, &param_len)) {
  822. printf(" t%04x l%u |%lub\n",
  823. param_type,
  824. param_len,
  825. sizeof(faux_phdr_t) + param_len
  826. );
  827. }
  828. }
  829. #else
  830. {
  831. msg = msg; // Happy compiler
  832. }
  833. #endif