hdr.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /** @file hdr.c
  2. */
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <sys/socket.h>
  14. #include <sys/un.h>
  15. #include <arpa/inet.h>
  16. #include <faux/faux.h>
  17. #include <faux/str.h>
  18. #include <faux/list.h>
  19. #include <faux/net.h>
  20. #include <faux/msg.h>
  21. /** @brief Sets command code to header.
  22. *
  23. * See the protocol and header description for possible values.
  24. *
  25. * @param [in] hdr Allocated faux_hdr_t object.
  26. * @param [in] cmd Command code (16 bit).
  27. */
  28. void faux_hdr_set_cmd(faux_hdr_t *hdr, uint16_t cmd)
  29. {
  30. assert(hdr);
  31. if (!hdr)
  32. return;
  33. hdr->cmd = htons(cmd);
  34. }
  35. /** @brief Gets command code from header.
  36. *
  37. * See the protocol and header description for possible values.
  38. *
  39. * @param [in] hdr Allocated faux_hdr_t object.
  40. * @return Command code or 0 on error.
  41. */
  42. uint16_t faux_hdr_cmd(const faux_hdr_t *hdr)
  43. {
  44. assert(hdr);
  45. if (!hdr)
  46. return 0;
  47. return ntohs(hdr->cmd);
  48. }
  49. /** @brief Sets message status to header.
  50. *
  51. * See the protocol and header description for possible values.
  52. *
  53. * @param [in] hdr Allocated faux_hdr_t object.
  54. * @param [in] status Message status.
  55. */
  56. void faux_hdr_set_status(faux_hdr_t *hdr, uint32_t status)
  57. {
  58. assert(hdr);
  59. if (!hdr)
  60. return;
  61. hdr->status = htonl(status);
  62. }
  63. /** @brief Gets message status from header.
  64. *
  65. * See the protocol and header description for possible values.
  66. *
  67. * @param [in] hdr Allocated faux_hdr_t object.
  68. * @return Message status or 0 on error.
  69. */
  70. uint32_t faux_hdr_status(const faux_hdr_t *hdr)
  71. {
  72. assert(hdr);
  73. if (!hdr)
  74. return 0;
  75. return ntohl(hdr->status);
  76. }
  77. /** @brief Sets request ID to header.
  78. *
  79. * @param [in] hdr Allocated faux_hdr_t object.
  80. * @param [in] req_id Request ID.
  81. */
  82. void faux_hdr_set_req_id(faux_hdr_t *hdr, uint32_t req_id)
  83. {
  84. assert(hdr);
  85. if (!hdr)
  86. return;
  87. hdr->req_id = htonl(req_id);
  88. }
  89. /** @brief Gets request ID from header.
  90. *
  91. * @param [in] hdr Allocated faux_hdr_t object.
  92. * @return Request ID or 0 on error.
  93. */
  94. uint32_t faux_hdr_req_id(const faux_hdr_t *hdr)
  95. {
  96. assert(hdr);
  97. if (!hdr)
  98. return 0;
  99. return ntohl(hdr->req_id);
  100. }
  101. /** @brief Sets number of parameters to header.
  102. *
  103. * It's a static function because external user can add or remove parameters
  104. * but class calculates total number of parameters internally.
  105. *
  106. * @param [in] hdr Allocated faux_hdr_t object.
  107. * @param [in] param_num Number of parameters.
  108. */
  109. void faux_hdr_set_param_num(faux_hdr_t *hdr, uint32_t param_num)
  110. {
  111. assert(hdr);
  112. if (!hdr)
  113. return;
  114. hdr->param_num = htonl(param_num);
  115. }
  116. /** @brief Gets number of parameters from header.
  117. *
  118. * @param [in] hdr Allocated faux_hdr_t object.
  119. * @return Number of parameters or 0 on error.
  120. */
  121. uint32_t faux_hdr_param_num(const faux_hdr_t *hdr)
  122. {
  123. assert(hdr);
  124. if (!hdr)
  125. return -1;
  126. return ntohl(hdr->param_num);
  127. }
  128. /** @brief Sets total length of message to header.
  129. *
  130. * It's a static function because external user can add or remove parameters
  131. * but class calculates total length of message internally.
  132. *
  133. * @param [in] hdr Allocated faux_hdr_t object.
  134. * @param [in] len Total length of message.
  135. */
  136. void faux_hdr_set_len(faux_hdr_t *hdr, uint32_t len)
  137. {
  138. assert(hdr);
  139. if (!hdr)
  140. return;
  141. hdr->len = htonl(len);
  142. }
  143. /** @brief Gets total length of message from header.
  144. *
  145. * @param [in] hdr Allocated faux_hdr_t object.
  146. * @return Total length of message or 0 on error.
  147. */
  148. int faux_hdr_len(const faux_hdr_t *hdr)
  149. {
  150. assert(hdr);
  151. if (!hdr)
  152. return 0;
  153. return ntohl(hdr->len);
  154. }
  155. /** @brief Sets magic number to header.
  156. *
  157. * @param [in] hdr Allocated faux_hdr_t object.
  158. * @param [in] magic Magic number.
  159. */
  160. void faux_hdr_set_magic(faux_hdr_t *hdr, uint32_t magic)
  161. {
  162. assert(hdr);
  163. if (!hdr)
  164. return;
  165. hdr->magic = htonl(magic);
  166. }
  167. /** @brief Gets magic number from header.
  168. *
  169. * @param [in] hdr Allocated faux_hdr_t object.
  170. * @return Magic number or 0 on error.
  171. */
  172. uint32_t faux_hdr_magic(const faux_hdr_t *hdr)
  173. {
  174. assert(hdr);
  175. if (!hdr)
  176. return 0;
  177. return ntohl(hdr->magic);
  178. }
  179. /** @brief Sets major version to header.
  180. *
  181. * @param [in] hdr Allocated faux_hdr_t object.
  182. * @param [in] major Major protocol version.
  183. * @return Major version number or 0 on error.
  184. */
  185. void faux_hdr_set_major(faux_hdr_t *hdr, uint8_t major)
  186. {
  187. assert(hdr);
  188. if (!hdr)
  189. return;
  190. hdr->major = major;
  191. }
  192. /** @brief Gets major version from header.
  193. *
  194. * @param [in] hdr Allocated faux_hdr_t object.
  195. * @return Major version number or 0 on error.
  196. */
  197. uint8_t faux_hdr_major(const faux_hdr_t *hdr)
  198. {
  199. assert(hdr);
  200. if (!hdr)
  201. return 0;
  202. return hdr->major;
  203. }
  204. /** @brief Sets minor version to header.
  205. *
  206. * @param [in] hdr Allocated faux_hdr_t object.
  207. * @param [in] minor Minor protocol version.
  208. * @return Major version number or 0 on error.
  209. */
  210. void faux_hdr_set_minor(faux_hdr_t *hdr, uint8_t minor)
  211. {
  212. assert(hdr);
  213. if (!hdr)
  214. return;
  215. hdr->minor = minor;
  216. }
  217. /** @brief Gets minor version from header.
  218. *
  219. * @param [in] hdr Allocated faux_hdr_t object.
  220. * @return Minor version number or 0 on error.
  221. */
  222. uint8_t faux_hdr_minor(const faux_hdr_t *hdr)
  223. {
  224. assert(hdr);
  225. if (!hdr)
  226. return 0;
  227. return hdr->minor;
  228. }