UdpGwClient.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C) Ambroz Bizjak <ambrop7@gmail.com>
  3. * Contributions:
  4. * Transparent DNS: Copyright (C) Kerem Hadimli <kerem.hadimli@gmail.com>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the author nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef BADVPN_UDPGW_CLIENT_UDPGWCLIENT_H
  29. #define BADVPN_UDPGW_CLIENT_UDPGWCLIENT_H
  30. #include <stdint.h>
  31. #include <protocol/udpgw_proto.h>
  32. #include <misc/debug.h>
  33. #include <misc/packed.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. B_START_PACKED
  48. struct UdpGwClient__keepalive_packet {
  49. struct packetproto_header pp;
  50. struct udpgw_header udpgw;
  51. } B_PACKED;
  52. B_END_PACKED
  53. typedef struct {
  54. int udp_mtu;
  55. int max_connections;
  56. int send_buffer_size;
  57. btime_t keepalive_time;
  58. BReactor *reactor;
  59. void *user;
  60. UdpGwClient_handler_servererror handler_servererror;
  61. UdpGwClient_handler_received handler_received;
  62. int udpgw_mtu;
  63. int pp_mtu;
  64. BAVL connections_tree_by_conaddr;
  65. BAVL connections_tree_by_conid;
  66. LinkedList1 connections_list;
  67. int num_connections;
  68. int next_conid;
  69. PacketPassFairQueue send_queue;
  70. PacketPassInactivityMonitor send_monitor;
  71. PacketPassConnector send_connector;
  72. struct UdpGwClient__keepalive_packet keepalive_packet;
  73. PacketPassInterface *keepalive_if;
  74. PacketPassFairQueueFlow keepalive_qflow;
  75. int keepalive_sending;
  76. int have_server;
  77. PacketStreamSender send_sender;
  78. PacketProtoDecoder recv_decoder;
  79. PacketPassInterface recv_if;
  80. DebugObject d_obj;
  81. } UdpGwClient;
  82. struct UdpGwClient_conaddr {
  83. BAddr local_addr;
  84. BAddr remote_addr;
  85. };
  86. struct UdpGwClient_connection {
  87. UdpGwClient *client;
  88. struct UdpGwClient_conaddr conaddr;
  89. uint8_t first_flags;
  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, int is_dns, 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