SocksUdpClient.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2018 Jigsaw Operations LLC
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * 1. Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * 2. Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * 3. Neither the name of the author nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef BADVPN_SOCKS_UDP_CLIENT_SOCKSUDPCLIENT_H
  27. #define BADVPN_SOCKS_UDP_CLIENT_SOCKSUDPCLIENT_H
  28. #include <stdint.h>
  29. #include <base/BPending.h>
  30. #include <base/DebugObject.h>
  31. #include <flow/BufferWriter.h>
  32. #include <flow/PacketBuffer.h>
  33. #include <flow/SinglePacketBuffer.h>
  34. #include <flowextra/PacketPassInactivityMonitor.h>
  35. #include <misc/debug.h>
  36. #include <misc/socks_proto.h>
  37. #include <socksclient/BSocksClient.h>
  38. #include <structure/BAVL.h>
  39. #include <system/BAddr.h>
  40. #include <system/BDatagram.h>
  41. #include <system/BReactor.h>
  42. #include <system/BTime.h>
  43. // This sets the number of packets to accept while waiting for SOCKS server to authenticate and
  44. // connect. A slow or far-away SOCKS server could require 300 ms to connect, and a chatty
  45. // client (e.g. STUN) could send a packet every 20 ms, so a limit of 16 seems reasonable.
  46. #define SOCKS_UDP_SEND_BUFFER_PACKETS 16
  47. typedef void (*SocksUdpClient_handler_received) (void *user, BAddr local_addr, BAddr remote_addr, const uint8_t *data, int data_len);
  48. typedef struct {
  49. BAddr server_addr;
  50. const struct BSocksClient_auth_info *auth_info;
  51. size_t num_auth_info;
  52. int num_connections;
  53. int max_connections;
  54. int udp_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 wait for
  74. // 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. * This function does not perform network access, so it will always succeed if the arguments
  88. * are valid.
  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 remote
  92. * servers, but SOCKS5 does not support UDP if there is a NAT or firewall between the client
  93. * 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 keepalive_time how long to track an idle local port before forgetting it
  99. * @param server_addr SOCKS5 server address. MUST BE ON LOCALHOST.
  100. * @param reactor reactor we live in
  101. * @param user value passed to handler
  102. * @param handler_received handler for incoming UDP packets
  103. */
  104. void SocksUdpClient_Init (SocksUdpClient *o, int udp_mtu, int max_connections, btime_t keepalive_time,
  105. BAddr server_addr, const struct BSocksClient_auth_info *auth_info, size_t num_auth_info,
  106. BReactor *reactor, void *user, SocksUdpClient_handler_received handler_received);
  107. void SocksUdpClient_Free (SocksUdpClient *o);
  108. /**
  109. * Submit a packet to be sent through the proxy.
  110. *
  111. * This will reuse an existing connection for packets from local_addr, or create one if
  112. * there is none. If the number of live connections exceeds max_connections, or if the number of
  113. * buffered packets from this port exceeds a limit, packets will be dropped silently.
  114. *
  115. * As a resource optimization, if a connection has only been used to send one DNS query, then
  116. * the connection will be closed and freed once the reply is received.
  117. *
  118. * @param o the object
  119. * @param local_addr the UDP packet's source address, and the expected destination for replies
  120. * @param remote_addr the destination of the packet after it exits the proxy
  121. * @param data the packet contents. Caller retains ownership.
  122. */
  123. void SocksUdpClient_SubmitPacket (SocksUdpClient *o, BAddr local_addr, BAddr remote_addr, const uint8_t *data, int data_len);
  124. #endif