msg.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /** @brief Parameter header
  45. */
  46. typedef struct faux_phdr_s {
  47. uint16_t param_type; // Parameter type
  48. uint8_t reserved[2];
  49. uint32_t param_len; // Length of parameter (not including header)
  50. } faux_phdr_t;
  51. /** @brief Message header
  52. */
  53. typedef struct faux_hdr_s {
  54. uint32_t magic; // Magic number
  55. uint8_t major; // Major protocol version number
  56. uint8_t minor; // Minor protocol version number
  57. uint16_t cmd; // Command
  58. uint32_t status; // Status
  59. uint32_t req_id; // Some abstract ID of request. Identifies request/answer
  60. uint32_t param_num; // Number of parameters
  61. uint32_t len; // Length of whole message (including header)
  62. faux_phdr_t phdr[]; // Parameter headers (unknown length)
  63. } crsp_hdr_t;
  64. // Debug variable. 1 for debug and 0 to switch debug off
  65. extern int crsp_debug;
  66. C_DECL_BEGIN
  67. // CRSP message functions
  68. crsp_msg_t *crsp_msg_new(void);
  69. void crsp_msg_free(crsp_msg_t *crsp_msg);
  70. void crsp_msg_set_cmd(crsp_msg_t *crsp_msg, crsp_cmd_e cmd);
  71. crsp_cmd_e crsp_msg_get_cmd(const crsp_msg_t *crsp_msg);
  72. void crsp_msg_set_status(crsp_msg_t *crsp_msg, crsp_status_e status);
  73. crsp_status_e crsp_msg_get_status(const crsp_msg_t *crsp_msg);
  74. void crsp_msg_set_req_id(crsp_msg_t *crsp_msg, uint32_t req_id);
  75. uint32_t crsp_msg_get_req_id(const crsp_msg_t *crsp_msg);
  76. uint32_t crsp_msg_get_param_num(const crsp_msg_t *crsp_msg);
  77. uint32_t crsp_msg_get_len(const crsp_msg_t *crsp_msg);
  78. ssize_t crsp_msg_add_param(crsp_msg_t *crsp_msg, crsp_param_e type,
  79. const void *buf, size_t len);
  80. faux_list_node_t *crsp_msg_init_param_iter(const crsp_msg_t *crsp_msg);
  81. crsp_phdr_t *crsp_msg_get_param_each(faux_list_node_t **node,
  82. crsp_param_e *param_type, void **param_data, uint32_t *param_len);
  83. crsp_phdr_t *crsp_msg_get_param_by_index(const crsp_msg_t *crsp_msg, unsigned int index,
  84. crsp_param_e *param_type, void **param_data, uint32_t *param_len);
  85. crsp_phdr_t *crsp_msg_get_param_by_type(const crsp_msg_t *crsp_msg,
  86. crsp_param_e param_type, void **param_data, uint32_t *param_len);
  87. ssize_t crsp_msg_send(crsp_msg_t *crsp_msg, faux_net_t *faux_net);
  88. crsp_msg_t *crsp_msg_recv(faux_net_t *faux_net, crsp_recv_e *status);
  89. char *crsp_msg_get_param_cdp_uri(const crsp_msg_t *crsp_msg);
  90. void crsp_msg_debug(crsp_msg_t *crsp_msg);
  91. C_DECL_END
  92. #endif // _faux_msg_h