UdpGwClient.h 4.3 KB

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