NCDRequestClient.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @file NCDRequestClient.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_NCDREQUESTCLIENT_H
  30. #define BADVPN_NCDREQUESTCLIENT_H
  31. #include <stdint.h>
  32. #include <misc/debug.h>
  33. #include <misc/debugerror.h>
  34. #include <misc/debugcounter.h>
  35. #include <structure/BAVL.h>
  36. #include <base/DebugObject.h>
  37. #include <system/BConnection.h>
  38. #include <flow/PacketProtoDecoder.h>
  39. #include <flow/PacketStreamSender.h>
  40. #include <flow/PacketPassFifoQueue.h>
  41. #include <ncd/NCDValueGenerator.h>
  42. #include <ncd/NCDValueParser.h>
  43. struct NCDRequestClient_req;
  44. typedef void (*NCDRequestClient_handler_error) (void *user);
  45. typedef void (*NCDRequestClient_handler_connected) (void *user);
  46. typedef void (*NCDRequestClientRequest_handler_sent) (void *user);
  47. typedef void (*NCDRequestClientRequest_handler_reply) (void *user, NCDValue reply_data);
  48. typedef void (*NCDRequestClientRequest_handler_finished) (void *user, int is_error);
  49. typedef struct {
  50. BReactor *reactor;
  51. void *user;
  52. NCDRequestClient_handler_error handler_error;
  53. NCDRequestClient_handler_connected handler_connected;
  54. BConnector connector;
  55. BConnection con;
  56. PacketPassFifoQueue send_queue;
  57. PacketStreamSender send_sender;
  58. PacketProtoDecoder recv_decoder;
  59. PacketPassInterface recv_if;
  60. BAVL reqs_tree;
  61. uint32_t next_request_id;
  62. int state;
  63. DebugCounter d_reqests_ctr;
  64. DebugError d_err;
  65. DebugObject d_obj;
  66. } NCDRequestClient;
  67. typedef struct {
  68. NCDRequestClient *client;
  69. void *user;
  70. NCDRequestClientRequest_handler_sent handler_sent;
  71. NCDRequestClientRequest_handler_reply handler_reply;
  72. NCDRequestClientRequest_handler_finished handler_finished;
  73. struct NCDRequestClient_req *req;
  74. DebugError d_err;
  75. DebugObject d_obj;
  76. } NCDRequestClientRequest;
  77. struct NCDRequestClient_req {
  78. NCDRequestClientRequest *creq;
  79. NCDRequestClient *client;
  80. BAVLNode reqs_tree_node;
  81. uint32_t request_id;
  82. uint8_t *request_data;
  83. int request_len;
  84. PacketPassInterface *send_qflow_iface;
  85. PacketPassFifoQueueFlow send_qflow;
  86. int state;
  87. };
  88. #define NCDREQUESTCLIENT_ADDR_TYPE_UNIX 1
  89. #define NCDREQUESTCLIENT_ADDR_TYPE_TCP 2
  90. struct NCDRequestClient_addr {
  91. int type;
  92. union {
  93. const char *unix_socket_path;
  94. BAddr tcp_baddr;
  95. } u;
  96. };
  97. #define NCDREQUESTCLIENT_UNIX_ADDR(socket_path) \
  98. (struct NCDRequestClient_addr){.type = NCDREQUESTCLIENT_ADDR_TYPE_UNIX, .u.unix_socket_path = (socket_path)}
  99. #define NCDREQUESTCLIENT_TCP_ADDR(baddr) \
  100. (struct NCDRequestClient_addr){.type = NCDREQUESTCLIENT_ADDR_TYPE_TCP, .u.tcp_baddr = (baddr)}
  101. int NCDRequestClient_Init (NCDRequestClient *o, struct NCDRequestClient_addr addr, BReactor *reactor, void *user,
  102. NCDRequestClient_handler_error handler_error,
  103. NCDRequestClient_handler_connected handler_connected) WARN_UNUSED;
  104. void NCDRequestClient_Free (NCDRequestClient *o);
  105. int NCDRequestClientRequest_Init (NCDRequestClientRequest *o, NCDRequestClient *client, NCDValue *payload_value, void *user,
  106. NCDRequestClientRequest_handler_sent handler_sent,
  107. NCDRequestClientRequest_handler_reply handler_reply,
  108. NCDRequestClientRequest_handler_finished handler_finished) WARN_UNUSED;
  109. void NCDRequestClientRequest_Free (NCDRequestClientRequest *o);
  110. void NCDRequestClientRequest_Abort (NCDRequestClientRequest *o);
  111. #endif