msg.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /** @file proto.h
  2. *
  3. * @brief Library to implement simple network protocol
  4. *
  5. * CRSP is a protocol to send requests to CRSP server. CRSP server stores
  6. * information about CDPs, periodically renews CRLs.
  7. *
  8. * The CRSP protocol supposes request/answer method. Each request must have an
  9. * answer. The CRSP server is synchronous. It means that server will answer
  10. * immediately or nearly immediately. It can plans a long-time operations but
  11. * it sends answer to client immediately. For example client asks for status
  12. * of specified CDP but server has no such CDP in its database. So server will
  13. * answer with status something like "has no such CDP yet" immediately but it
  14. * schedules the long-time task to get CDP information. Later client can send
  15. * the same request but now server already has a status for specified CDP and
  16. * answers with correspondent status.
  17. *
  18. * Each CRSP protocol message (request or answer) has the same structure:
  19. *
  20. * [ CRSP header ]
  21. * [ CRSP first parameter's header ]
  22. * [ ... ]
  23. * [ CRSP n-th parameter's header ]
  24. * [ First parameter's data ]
  25. * [ ... ]
  26. * [ N-th parameter's data ]
  27. *
  28. * The length of CRSP message can vary. CRSP header has constant length and
  29. * contains total length of whole message and number of parameters. It also
  30. * contains magic number, CRSP version number, CRSP command, status and
  31. * request ID (to identify request/answer). There are parameter header for each
  32. * parameter. Parameter's header contains parameter type and length of
  33. * parameter. After parameter headers array the parameter's data follows in
  34. * a sequence.
  35. */
  36. #ifndef _faux_msg_h
  37. #define _faux_msg_h
  38. #include <stdint.h>
  39. #include <signal.h>
  40. #include <faux/faux.h>
  41. #include <faux/list.h>
  42. #include <faux/net.h>
  43. typedef struct faux_msg_s faux_msg_t;
  44. // Debug variable. BOOL_TRUE for debug and BOOL_FALSE to switch debug off
  45. extern bool_t faux_msg_debug;
  46. /** @brief Parameter header
  47. */
  48. typedef struct faux_phdr_s {
  49. uint16_t param_type; // Parameter type
  50. uint8_t reserved[2];
  51. uint32_t param_len; // Length of parameter (not including header)
  52. } faux_phdr_t;
  53. /** @brief Message header
  54. */
  55. typedef struct faux_hdr_s {
  56. uint32_t magic; // Magic number
  57. uint8_t major; // Major protocol version number
  58. uint8_t minor; // Minor protocol version number
  59. uint16_t cmd; // Command
  60. uint32_t status; // Status
  61. uint32_t req_id; // Some abstract ID of request. Identifies request/answer
  62. uint32_t param_num; // Number of parameters
  63. uint32_t len; // Length of whole message (including header)
  64. faux_phdr_t phdr[]; // Parameter headers (unknown length)
  65. } crsp_hdr_t;
  66. // Debug variable. 1 for debug and 0 to switch debug off
  67. extern int crsp_debug;
  68. C_DECL_BEGIN
  69. // Parameter functions
  70. void faux_phdr_set_type(faux_phdr_t *phdr, uint16_t param_type);
  71. uint16_t faux_phdr_get_type(const faux_phdr_t *phdr);
  72. void faux_phdr_set_len(faux_phdr_t *phdr, uint32_t param_len);
  73. uint32_t faux_phdr_get_len(const faux_phdr_t *phdr);
  74. // Message functions
  75. crsp_msg_t *crsp_msg_new(void);
  76. void crsp_msg_free(crsp_msg_t *crsp_msg);
  77. void crsp_msg_set_cmd(crsp_msg_t *crsp_msg, crsp_cmd_e cmd);
  78. crsp_cmd_e crsp_msg_get_cmd(const crsp_msg_t *crsp_msg);
  79. void crsp_msg_set_status(crsp_msg_t *crsp_msg, crsp_status_e status);
  80. crsp_status_e crsp_msg_get_status(const crsp_msg_t *crsp_msg);
  81. void crsp_msg_set_req_id(crsp_msg_t *crsp_msg, uint32_t req_id);
  82. uint32_t crsp_msg_get_req_id(const crsp_msg_t *crsp_msg);
  83. uint32_t crsp_msg_get_param_num(const crsp_msg_t *crsp_msg);
  84. uint32_t crsp_msg_get_len(const crsp_msg_t *crsp_msg);
  85. ssize_t crsp_msg_add_param(crsp_msg_t *crsp_msg, crsp_param_e type,
  86. const void *buf, size_t len);
  87. faux_list_node_t *crsp_msg_init_param_iter(const crsp_msg_t *crsp_msg);
  88. crsp_phdr_t *crsp_msg_get_param_each(faux_list_node_t **node,
  89. crsp_param_e *param_type, void **param_data, uint32_t *param_len);
  90. crsp_phdr_t *crsp_msg_get_param_by_index(const crsp_msg_t *crsp_msg, unsigned int index,
  91. crsp_param_e *param_type, void **param_data, uint32_t *param_len);
  92. crsp_phdr_t *crsp_msg_get_param_by_type(const crsp_msg_t *crsp_msg,
  93. crsp_param_e param_type, void **param_data, uint32_t *param_len);
  94. ssize_t crsp_msg_send(crsp_msg_t *crsp_msg, faux_net_t *faux_net);
  95. crsp_msg_t *crsp_msg_recv(faux_net_t *faux_net, crsp_recv_e *status);
  96. char *crsp_msg_get_param_cdp_uri(const crsp_msg_t *crsp_msg);
  97. void crsp_msg_debug(crsp_msg_t *crsp_msg);
  98. C_DECL_END
  99. #endif // _faux_msg_h