UdpGwClient.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @file UdpGwClient.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_UDPGW_CLIENT_UDPGWCLIENT_H
  30. #define BADVPN_UDPGW_CLIENT_UDPGWCLIENT_H
  31. #include <stdint.h>
  32. #include <protocol/udpgw_proto.h>
  33. #include <misc/debug.h>
  34. #include <misc/packed.h>
  35. #include <structure/BAVL.h>
  36. #include <structure/LinkedList1.h>
  37. #include <base/DebugObject.h>
  38. #include <system/BAddr.h>
  39. #include <base/BPending.h>
  40. #include <flow/PacketPassFairQueue.h>
  41. #include <flow/PacketStreamSender.h>
  42. #include <flow/PacketProtoFlow.h>
  43. #include <flow/PacketProtoDecoder.h>
  44. #include <flow/PacketPassConnector.h>
  45. #include <flowextra/PacketPassInactivityMonitor.h>
  46. typedef void (*UdpGwClient_handler_servererror) (void *user);
  47. typedef void (*UdpGwClient_handler_received) (void *user, BAddr local_addr, BAddr remote_addr, const uint8_t *data, int data_len);
  48. B_START_PACKED
  49. struct UdpGwClient__keepalive_packet {
  50. struct packetproto_header pp;
  51. struct udpgw_header udpgw;
  52. } B_PACKED;
  53. B_END_PACKED
  54. typedef struct {
  55. int udp_mtu;
  56. int max_connections;
  57. int send_buffer_size;
  58. btime_t keepalive_time;
  59. BReactor *reactor;
  60. void *user;
  61. UdpGwClient_handler_servererror handler_servererror;
  62. UdpGwClient_handler_received handler_received;
  63. int udpgw_mtu;
  64. int pp_mtu;
  65. BAVL connections_tree_by_conaddr;
  66. BAVL connections_tree_by_conid;
  67. LinkedList1 connections_list;
  68. int num_connections;
  69. int next_conid;
  70. PacketPassFairQueue send_queue;
  71. PacketPassInactivityMonitor send_monitor;
  72. PacketPassConnector send_connector;
  73. struct UdpGwClient__keepalive_packet keepalive_packet;
  74. PacketPassInterface *keepalive_if;
  75. PacketPassFairQueueFlow keepalive_qflow;
  76. int keepalive_sending;
  77. int have_server;
  78. PacketStreamSender send_sender;
  79. PacketProtoDecoder recv_decoder;
  80. PacketPassInterface recv_if;
  81. DebugObject d_obj;
  82. } UdpGwClient;
  83. struct UdpGwClient_conaddr {
  84. BAddr local_addr;
  85. BAddr remote_addr;
  86. };
  87. struct UdpGwClient_connection {
  88. UdpGwClient *client;
  89. struct UdpGwClient_conaddr conaddr;
  90. const uint8_t *first_data;
  91. int first_data_len;
  92. uint16_t conid;
  93. BPending first_job;
  94. BufferWriter *send_if;
  95. PacketProtoFlow send_ppflow;
  96. PacketPassFairQueueFlow send_qflow;
  97. BAVLNode connections_tree_by_conaddr_node;
  98. BAVLNode connections_tree_by_conid_node;
  99. LinkedList1Node connections_list_node;
  100. };
  101. int UdpGwClient_Init (UdpGwClient *o, int udp_mtu, int max_connections, int send_buffer_size, btime_t keepalive_time, BReactor *reactor, void *user,
  102. UdpGwClient_handler_servererror handler_servererror,
  103. UdpGwClient_handler_received handler_received) WARN_UNUSED;
  104. void UdpGwClient_Free (UdpGwClient *o);
  105. void UdpGwClient_SubmitPacket (UdpGwClient *o, BAddr local_addr, BAddr remote_addr, const uint8_t *data, int data_len);
  106. int UdpGwClient_ConnectServer (UdpGwClient *o, StreamPassInterface *send_if, StreamRecvInterface *recv_if) WARN_UNUSED;
  107. void UdpGwClient_DisconnectServer (UdpGwClient *o);
  108. #endif