msg.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /** @file crsp_msg.c
  2. * @brief Class represents a single message of CRSP protocol.
  3. *
  4. * CRSP 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/msg.h>
  31. // Global variable to switch debug on/off (true/false)
  32. bool_t faux_msg_debug = BOOL_FALSE;
  33. /** @brief Opaque faux_msg_s structure. */
  34. struct faux_msg_s {
  35. faux_hdr_t *hdr; // Message header
  36. faux_list_t *params; // List of parameters
  37. };
  38. /** @brief Allocate memory to store message.
  39. *
  40. * This static function is needed because new message object can be created
  41. * in a different ways. The first way is creating outgoing message manually and
  42. * the second way is receiving CRSP message from network. These ways need
  43. * different initialization but the same memory allocation.
  44. *
  45. * @return Allocated but not fully initialized faux_msg_t object
  46. * or NULL on error
  47. */
  48. static faux_msg_t *faux_msg_allocate(void)
  49. {
  50. faux_msg_t *msg = NULL;
  51. msg = faux_zmalloc(sizeof(*msg));
  52. assert(msg);
  53. if (!msg)
  54. return NULL;
  55. // Init message header
  56. msg->hdr = faux_zmalloc(sizeof(*msg->hdr));
  57. assert(msg->hdr);
  58. if (!msg->hdr) {
  59. faux_msg_free(msg);
  60. return NULL;
  61. }
  62. msg->params = faux_list_new(
  63. FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE, NULL, NULL, faux_free);
  64. return msg;
  65. }
  66. /** @brief Creates new faux_msg_t object. It's usually outgoing message.
  67. *
  68. * Function initializes main message header with default values. Usually
  69. * only outgoing messages need initialized header.
  70. *
  71. * @param [in] magic Protocol's magic number.
  72. * @param [in] major Protocol's version major number.
  73. * @param [in] minor Protocol's version minor number.
  74. * @return Allocated and initilized faux_msg_t object or NULL on error.
  75. */
  76. faux_msg_t *faux_msg_new(uint32_t magic, uint8_t major, uint8_t minor)
  77. {
  78. faux_msg_t *msg = NULL;
  79. msg = faux_msg_allocate();
  80. assert(msg);
  81. if (!msg)
  82. return NULL;
  83. // Init
  84. msg->hdr->magic = htonl(magic);
  85. msg->hdr->major = major;
  86. msg->hdr->minor = minor;
  87. faux_msg_set_cmd(msg, 0);
  88. faux_msg_set_status(msg, 0);
  89. faux_msg_set_req_id(msg, 0l);
  90. faux_msg_set_param_num(msg, 0l);
  91. faux_msg_set_len(msg, sizeof(*msg->hdr));
  92. return msg;
  93. }
  94. /** @brief Frees allocated message.
  95. *
  96. * @param [in] msg Allocated faux_msg_t object.
  97. */
  98. void faux_msg_free(faux_msg_t *msg)
  99. {
  100. if (!msg)
  101. return;
  102. faux_list_free(msg->params);
  103. faux_free(msg->hdr);
  104. faux_free(msg);
  105. }
  106. /** @brief Sets command code to header.
  107. *
  108. * See the protocol and header description for possible values.
  109. *
  110. * @param [in] msg Allocated faux_msg_t object.
  111. * @param [in] cmd Command code (16 bit).
  112. */
  113. void faux_msg_set_cmd(faux_msg_t *msg, uint16_t cmd)
  114. {
  115. assert(msg);
  116. assert(msg->hdr);
  117. if (!msg || !msg->hdr)
  118. return;
  119. msg->hdr->cmd = htons(cmd);
  120. }
  121. /** @brief Gets command code from header.
  122. *
  123. * See the protocol and header description for possible values.
  124. *
  125. * @param [in] msg Allocated faux_msg_t object.
  126. * @return Command code or 0 on error.
  127. */
  128. uint16_t faux_msg_get_cmd(const faux_msg_t *msg)
  129. {
  130. assert(msg);
  131. assert(msg->hdr);
  132. if (!msg || !msg->hdr)
  133. return 0;
  134. return ntohs(msg->hdr->cmd);
  135. }
  136. /** @brief Sets message status to header.
  137. *
  138. * See the protocol and header description for possible values.
  139. *
  140. * @param [in] msg Allocated faux_msg_t object.
  141. * @param [in] status Message status.
  142. */
  143. void faux_msg_set_status(faux_msg_t *msg, uint32_t status)
  144. {
  145. assert(msg);
  146. assert(msg->hdr);
  147. if (!msg || !msg->hdr)
  148. return;
  149. msg->hdr->status = htonl(status);
  150. }
  151. /** @brief Gets message status from header.
  152. *
  153. * See the protocol and header description for possible values.
  154. *
  155. * @param [in] msg Allocated faux_msg_t object.
  156. * @return Message status or 0 on error.
  157. */
  158. uint32_t faux_msg_get_status(const faux_msg_t *msg)
  159. {
  160. assert(msg);
  161. assert(msg->hdr);
  162. if (!msg || !msg->hdr)
  163. return 0;
  164. return ntohl(msg->hdr->status);
  165. }
  166. /** @brief Sets request ID to header.
  167. *
  168. * @param [in] msg Allocated faux_msg_t object.
  169. * @param [in] req_id Request ID.
  170. */
  171. void faux_msg_set_req_id(faux_msg_t *msg, uint32_t req_id)
  172. {
  173. assert(msg);
  174. assert(msg->hdr);
  175. if (!msg || !msg->hdr)
  176. return;
  177. msg->hdr->req_id = htonl(req_id);
  178. }
  179. /** @brief Gets request ID from header.
  180. *
  181. * @param [in] msg Allocated faux_msg_t object.
  182. * @return Request ID or 0 on error.
  183. */
  184. uint32_t faux_msg_get_req_id(const faux_msg_t *msg)
  185. {
  186. assert(msg);
  187. assert(msg->hdr);
  188. if (!msg || !msg->hdr)
  189. return 0;
  190. return ntohl(msg->hdr->req_id);
  191. }
  192. /** @brief Sets number of parameters to header.
  193. *
  194. * It's a static function because external user can add or remove parameters
  195. * but class calculates total number of parameters internally.
  196. *
  197. * @param [in] msg Allocated faux_msg_t object.
  198. * @param [in] param_num Number of parameters.
  199. */
  200. static void faux_msg_set_param_num(faux_msg_t *msg, uint32_t param_num)
  201. {
  202. assert(msg);
  203. assert(msg->hdr);
  204. if (!msg || !msg->hdr)
  205. return;
  206. msg->hdr->param_num = htonl(param_num);
  207. }
  208. /** @brief Gets number of parameters from header.
  209. *
  210. * @param [in] msg Allocated faux_msg_t object.
  211. * @return Number of parameters or 0 on error.
  212. */
  213. uint32_t faux_msg_get_param_num(const faux_msg_t *msg)
  214. {
  215. assert(msg);
  216. assert(msg->hdr);
  217. if (!msg || !msg->hdr)
  218. return -1;
  219. return ntohl(msg->hdr->param_num);
  220. }
  221. /** @brief Sets total length of message to header.
  222. *
  223. * It's a static function because external user can add or remove parameters
  224. * but class calculates total length of message internally.
  225. *
  226. * @param [in] msg Allocated faux_msg_t object.
  227. * @param [in] len Total length of message.
  228. */
  229. static void faux_msg_set_len(faux_msg_t *msg, uint32_t len)
  230. {
  231. assert(msg);
  232. assert(msg->hdr);
  233. if (!msg || !msg->hdr)
  234. return;
  235. msg->hdr->len = htonl(len);
  236. }
  237. /** @brief Gets total length of message from header.
  238. *
  239. * @param [in] msg Allocated faux_msg_t object.
  240. * @return Total length of message or 0 on error.
  241. */
  242. int faux_msg_get_len(const faux_msg_t *msg)
  243. {
  244. assert(msg);
  245. assert(msg->hdr);
  246. if (!msg || !msg->hdr)
  247. return 0;
  248. return ntohl(msg->hdr->len);
  249. }
  250. /** @brief Gets magic number from header.
  251. *
  252. * @param [in] msg Allocated faux_msg_t object.
  253. * @return Magic number or 0 on error.
  254. */
  255. uint32_t faux_msg_get_magic(const faux_msg_t *msg)
  256. {
  257. assert(msg);
  258. assert(msg->hdr);
  259. if (!msg || !msg->hdr)
  260. return 0;
  261. return ntohl(msg->hdr->magic);
  262. }
  263. /** @brief Gets major version from header.
  264. *
  265. * @param [in] msg Allocated faux_msg_t object.
  266. * @return Major version number or 0 on error.
  267. */
  268. int faux_msg_get_major(const faux_msg_t *msg)
  269. {
  270. assert(msg);
  271. assert(msg->hdr);
  272. if (!msg || !msg->hdr)
  273. return 0;
  274. return msg->hdr->major;
  275. }
  276. /** @brief Gets minor version from header.
  277. *
  278. * @param [in] msg Allocated faux_msg_t object.
  279. * @return Minor version number or 0 on error.
  280. */
  281. int faux_msg_get_minor(const faux_msg_t *msg)
  282. {
  283. assert(msg);
  284. assert(msg->hdr);
  285. if (!msg || !msg->hdr)
  286. return 0;
  287. return msg->hdr->minor;
  288. }
  289. /** @brief Internal function to add message parameter
  290. *
  291. * Internal function can update or don't update number of parameters and
  292. * whole length within message header. It can be used while
  293. * message receive to don't break already calculated header
  294. * values. So when user is constructing message the values must be updated.
  295. *
  296. * @param [in] msg Allocated faux_msg_t object.
  297. * @param [in] type Type of parameter.
  298. * @param [in] buf Parameter's data buffer.
  299. * @param [in] len Parameter's data length.
  300. * @param [in] upadte_len Flag that says to update or don't update number of
  301. * parameters and total message length within header. BOOL_TRUE - update,
  302. * BOOL_FALSE - don't update.
  303. * @return Length of parameter's data or < 0 on error.
  304. */
  305. static ssize_t faux_msg_add_param_internal(faux_msg_t *msg,
  306. uint16_t type, const void *buf, size_t len, bool_t update_len)
  307. {
  308. faux_phdr_t *phdr = NULL;
  309. char *param = NULL;
  310. assert(msg);
  311. assert(msg->hdr);
  312. if (!msg || !msg->hdr)
  313. return -1;
  314. // Allocate parameter header and data
  315. param = faux_zmalloc(sizeof(*phdr) + len);
  316. assert(param);
  317. if (!param)
  318. return -1;
  319. // Init param hdr
  320. phdr = (faux_phdr_t *)param;
  321. faux_phdr_set_type(phdr, type);
  322. faux_phdr_set_len(phdr, len);
  323. // Copy data
  324. memcpy(param + sizeof(*phdr), buf, len);
  325. if (update_len) {
  326. uint32_t cur_param_num = 0;
  327. uint32_t cur_len = 0;
  328. // Update number of parameters
  329. faux_msg_get_param_num(msg, &cur_param_num);
  330. faux_msg_set_param_num(msg, cur_param_num + 1);
  331. // Update whole message length
  332. faux_msg_get_len(msg, &cur_len);
  333. crsp_msg_set_len(crsp_msg, cur_len + 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. See the crsp_param_e enumeration.
  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 Sends message to network.
  478. *
  479. * Function sends message to network using preinitialized faux_net_t object.
  480. * User can specify timeout, signal mask, etc while faux_net_t object creation.
  481. *
  482. * Function can return length less than whole message length in the following
  483. * cases:
  484. * - An error has occured like broken file descriptor.
  485. * - Interrupted by allowed signal (see signal mask).
  486. * - Timeout.
  487. *
  488. * @param [in] msg Allocated faux_msg_t object.
  489. * @param [in] faux_net Preinitialized faux_net_t object.
  490. * @return Length of sent data or < 0 on error.
  491. */
  492. ssize_t faux_msg_send(crsp_msg_t *msg, faux_net_t *faux_net)
  493. {
  494. unsigned int vec_entries_num = 0;
  495. struct iovec *iov = NULL;
  496. unsigned int i = 0;
  497. faux_list_node_t *iter = NULL;
  498. size_t ret = 0;
  499. uint32_t param_num = 0;
  500. assert(msg);
  501. assert(msg->hdr);
  502. if (!msg || !msg->hdr)
  503. return -1;
  504. // Calculate number if struct iovec entries.
  505. // n = (msg header) + ((param hdr) + (param data)) * (param_num)
  506. faux_msg_get_param_num(msg, &param_num);
  507. vec_entries_num = 1 + (2 * param_num);
  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. crsp_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. ret = faux_net_sendv(faux_net, iov, vec_entries_num);
  534. faux_free(iov);
  535. #ifdef DEBUG
  536. // Debug
  537. if (msg && ret > 0 && faux_msg_debug) {
  538. printf("(o) ");
  539. faux_msg_debug(msg);
  540. }
  541. #endif
  542. return ret;
  543. }
  544. /** @brief Receives full message and allocates faux_msg_t object for it.
  545. *
  546. * Function receives message from network using preinitialized faux_net_t object.
  547. * User can specify timeout, signal mask, etc while faux_net_t object creation.
  548. *
  549. * Function can return length less than whole message length in the following
  550. * cases:
  551. * - An error has occured like broken file descriptor.
  552. * - Interrupted by allowed signal (see signal mask).
  553. * - Timeout.
  554. *
  555. * It can be an logical errors while message receiving like wrong protocol
  556. * version. So function has additional parameter named 'status'. It will
  557. * be CRSP_STATUS_OK in a case when all is ok but function can return NULL and
  558. * set appropriate status to this parameter. It can be
  559. * CRSP_STATUS_WRONG_VERSION for example. The function will return NULL
  560. * and CRSP_STATUS_OK on some system errors like illegal parameters or
  561. * insufficient of memory.
  562. *
  563. * @param [in] faux_net Preinitialized faux_net_t object.
  564. * @param [out] status Status while message receiving. Can be NULL.
  565. * @return Allocated faux_msg_t object. Object contains received message.
  566. */
  567. faux_msg_t *faux_msg_recv(faux_net_t *faux_net, crsp_recv_e *status)
  568. {
  569. faux_msg_t *msg = NULL;
  570. size_t received = 0;
  571. faux_phdr_t *phdr = NULL;
  572. unsigned int param_num = 0;
  573. size_t phdr_whole_len = 0;
  574. size_t max_data_len = 0;
  575. size_t cur_data_len = 0;
  576. unsigned int i = 0;
  577. char *data = NULL;
  578. if (status)
  579. *status = CRSP_RECV_OK;
  580. msg = faux_msg_allocate();
  581. assert(msg);
  582. if (!msg)
  583. return NULL;
  584. // Receive message header
  585. received = faux_net_recv(faux_net, msg->hdr, sizeof(*msg->hdr));
  586. if (received != sizeof(*msg->hdr)) {
  587. faux_msg_free(msg);
  588. return NULL;
  589. }
  590. // Receive parameter headers
  591. faux_msg_get_param_num(msg, &param_num);
  592. if (param_num != 0) {
  593. phdr_whole_len = param_num * sizeof(*phdr);
  594. phdr = faux_zmalloc(phdr_whole_len);
  595. received = faux_net_recv(faux_net, phdr, phdr_whole_len);
  596. if (received != phdr_whole_len) {
  597. faux_free(phdr);
  598. crsp_msg_free(crsp_msg);
  599. if (status)
  600. *status = CRSP_RECV_BROKEN_PARAM;
  601. return NULL;
  602. }
  603. // Find out maximum data length
  604. for (i = 0; i < param_num; i++) {
  605. cur_data_len = ntohl(phdr[i].param_len);
  606. if (cur_data_len > max_data_len)
  607. max_data_len = cur_data_len;
  608. }
  609. // Receive parameter data
  610. data = faux_zmalloc(max_data_len);
  611. for (i = 0; i < param_num; i++) {
  612. cur_data_len = ntohl(phdr[i].param_len);
  613. if (0 == cur_data_len)
  614. continue;
  615. received = faux_net_recv(faux_net,
  616. data, cur_data_len);
  617. if (received != cur_data_len) {
  618. faux_free(data);
  619. faux_free(phdr);
  620. crsp_msg_free(crsp_msg);
  621. if (status)
  622. *status = CRSP_RECV_BROKEN_PARAM;
  623. return NULL;
  624. }
  625. crsp_msg_add_param_internal(crsp_msg, phdr[i].param_type,
  626. data, cur_data_len, BOOL_FALSE);
  627. }
  628. faux_free(data);
  629. faux_free(phdr);
  630. }
  631. #ifdef DEBUG
  632. // Debug
  633. if (msg && faux_msg_debug) {
  634. printf("(i) ");
  635. faux_msg_debug(msg);
  636. }
  637. #endif
  638. return msg;
  639. }
  640. /** @brief Prints message debug info.
  641. *
  642. * Function prints header values and parameters.
  643. *
  644. * @param [in] msg Allocated faux_msg_t object.
  645. */
  646. void faux_msg_debug(faux_msg_t *msg)
  647. #ifdef DEBUG
  648. {
  649. faux_list_node_t *iter = 0;
  650. // Header vars
  651. uint32_t magic = 0;
  652. uint8_t major = 0;
  653. uint8_t minor = 0;
  654. uint16_t cmd = 0;
  655. uint32_t status = 0;
  656. uint32_t req_id = 0;
  657. uint32_t param_num = 0;
  658. uint32_t len = 0;
  659. // Parameter vars
  660. void *param_data = NULL;
  661. uint16_t param_type = 0;
  662. uint32_t param_len = 0;
  663. assert(msg);
  664. if (!msg)
  665. return;
  666. // Header
  667. faux_msg_get_magic(msg, &magic);
  668. faux_msg_get_version(msg, &major, &minor);
  669. faux_msg_get_cmd(msg, &cmd);
  670. faux_msg_get_status(msg, &status);
  671. faux_msg_get_req_id(msg, &req_id);
  672. faux_msg_get_param_num(msg, &param_num);
  673. faux_msg_get_len(msg, &len);
  674. printf("%lx(%u.%u): c%04x s%08x i%08x p%u l%u |%lub\n",
  675. magic,
  676. major,
  677. minor,
  678. cmd,
  679. status,
  680. req_id,
  681. param_num,
  682. len
  683. sizeof(*msg->hdr)
  684. );
  685. // Parameters
  686. iter = faux_msg_init_param_iter(msg);
  687. while (faux_msg_get_param_each(&iter, &param_type, &param_data, &param_len)) {
  688. printf(" t%04x l%u |%lub\n",
  689. param_type,
  690. param_len,
  691. sizeof(faux_phdr_t) + param_len
  692. );
  693. }
  694. }
  695. #else
  696. {
  697. msg = msg; // Happy compiler
  698. }
  699. #endif