SocksUdpClient.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2018 Jigsaw Operations LLC
  3. * Copyright (C) 2019 Ambroz Bizjak (modifications)
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. Neither the name of the author nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef BADVPN_SOCKS_UDP_CLIENT_SOCKSUDPCLIENT_H
  28. #define BADVPN_SOCKS_UDP_CLIENT_SOCKSUDPCLIENT_H
  29. #include <stddef.h>
  30. #include <stdint.h>
  31. #include <base/BPending.h>
  32. #include <base/DebugObject.h>
  33. #include <flow/BufferWriter.h>
  34. #include <flow/PacketBuffer.h>
  35. #include <flow/SinglePacketBuffer.h>
  36. #include <flow/PacketPassInterface.h>
  37. #include <flowextra/PacketPassInactivityMonitor.h>
  38. #include <socksclient/BSocksClient.h>
  39. #include <structure/BAVL.h>
  40. #include <system/BAddr.h>
  41. #include <system/BDatagram.h>
  42. #include <system/BReactor.h>
  43. #include <system/BTime.h>
  44. typedef void (*SocksUdpClient_handler_received) (
  45. void *user, BAddr local_addr, BAddr remote_addr, const uint8_t *data, int data_len);
  46. typedef struct {
  47. BAddr server_addr;
  48. const struct BSocksClient_auth_info *auth_info;
  49. size_t num_auth_info;
  50. int num_connections;
  51. int max_connections;
  52. int send_buf_size;
  53. int udp_mtu;
  54. int socks_mtu;
  55. btime_t keepalive_time;
  56. BReactor *reactor;
  57. void *user;
  58. SocksUdpClient_handler_received handler_received;
  59. BAVL connections_tree; // By local_addr
  60. DebugObject d_obj;
  61. } SocksUdpClient;
  62. struct SocksUdpClient_connection {
  63. SocksUdpClient *client;
  64. BAddr local_addr;
  65. BSocksClient socks;
  66. BufferWriter send_writer;
  67. PacketBuffer send_buffer;
  68. PacketPassInactivityMonitor send_monitor;
  69. PacketPassInterface send_if;
  70. BDatagram socket;
  71. PacketPassInterface recv_if;
  72. SinglePacketBuffer recv_buffer;
  73. // The first_* members represent the initial packet, which has to be stored so it can
  74. // wait for send_writer to become ready.
  75. uint8_t *first_data;
  76. int first_data_len;
  77. BAddr first_remote_addr;
  78. // If all packets sent so far have been sent to the same IP, port 53, with the
  79. // same DNS ID, then this is that ID. Otherwise, it is -1. This is used to
  80. // close ephemeral DNS query connections once a response is received.
  81. int dns_id;
  82. BPending first_job;
  83. BAVLNode connections_tree_node;
  84. };
  85. /**
  86. * Initializes the SOCKS5-UDP client object.
  87. *
  88. * This function only initialzies the object and does not perform network access.
  89. *
  90. * Currently, this function only supports connection to a SOCKS5 server that is routable from
  91. * localhost (i.e. running on the local machine). It may be possible to add support for
  92. * remote servers, but SOCKS5 does not support UDP if there is a NAT or firewall between the
  93. * client and the proxy.
  94. *
  95. * @param o the object
  96. * @param udp_mtu the maximum size of packets that will be sent through the tunnel
  97. * @param max_connections how many local ports to track before dropping packets
  98. * @param send_buf_size maximum number of buffered outgoing packets per connection
  99. * @param keepalive_time how long to track an idle local port before forgetting it
  100. * @param server_addr SOCKS5 server address. MUST BE ON LOCALHOST.
  101. * @param auth_info List of authentication info for BSocksClient. The pointer must remain
  102. * valid while this object exists, the data is not copied.
  103. * @param num_auth_info Number of the above.
  104. * @param reactor reactor we live in
  105. * @param user value passed to handler
  106. * @param handler_received handler for incoming UDP packets
  107. * @return 1 on success, 0 on failure
  108. */
  109. int SocksUdpClient_Init (SocksUdpClient *o, int udp_mtu, int max_connections,
  110. int send_buf_size, btime_t keepalive_time, BAddr server_addr,
  111. const struct BSocksClient_auth_info *auth_info, size_t num_auth_info,
  112. BReactor *reactor, void *user, SocksUdpClient_handler_received handler_received);
  113. /**
  114. * Frees the SOCKS5-UDP client object.
  115. *
  116. * @param o the object
  117. */
  118. void SocksUdpClient_Free (SocksUdpClient *o);
  119. /**
  120. * Submit a packet to be sent through the proxy.
  121. *
  122. * This will reuse an existing connection for packets from local_addr, or create one if
  123. * there is none. If the number of live connections exceeds max_connections, or if the
  124. * number of buffered packets from this port exceeds a limit, packets will be dropped
  125. * silently.
  126. *
  127. * As a resource optimization, if a connection has only been used to send one DNS query,
  128. * then the connection will be closed and freed once the reply is received.
  129. *
  130. * @param o the object
  131. * @param local_addr the UDP packet's source address, and the expected destination for
  132. * replies
  133. * @param remote_addr the destination of the packet after it exits the proxy
  134. * @param data the packet contents. Caller retains ownership.
  135. * @param data_len number of bytes in the data
  136. */
  137. void SocksUdpClient_SubmitPacket (SocksUdpClient *o,
  138. BAddr local_addr, BAddr remote_addr, const uint8_t *data, int data_len);
  139. #endif