BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 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 | |||
28 | #ifndef BADVPN_SOCKS_UDP_CLIENT_SOCKSUDPCLIENT_H |
||
29 | #define BADVPN_SOCKS_UDP_CLIENT_SOCKSUDPCLIENT_H |
||
30 | |||
31 | #include <stddef.h> |
||
32 | #include <stdint.h> |
||
33 | |||
34 | #include <base/BPending.h> |
||
35 | #include <base/DebugObject.h> |
||
36 | #include <flow/BufferWriter.h> |
||
37 | #include <flow/PacketBuffer.h> |
||
38 | #include <flow/SinglePacketBuffer.h> |
||
39 | #include <flow/PacketPassInterface.h> |
||
40 | #include <flowextra/PacketPassInactivityMonitor.h> |
||
41 | #include <socksclient/BSocksClient.h> |
||
42 | #include <structure/BAVL.h> |
||
43 | #include <system/BAddr.h> |
||
44 | #include <system/BDatagram.h> |
||
45 | #include <system/BReactor.h> |
||
46 | #include <system/BTime.h> |
||
47 | |||
48 | typedef void (*SocksUdpClient_handler_received) ( |
||
49 | void *user, BAddr local_addr, BAddr remote_addr, const uint8_t *data, int data_len); |
||
50 | |||
51 | typedef struct { |
||
52 | BAddr server_addr; |
||
53 | const struct BSocksClient_auth_info *auth_info; |
||
54 | size_t num_auth_info; |
||
55 | int num_connections; |
||
56 | int max_connections; |
||
57 | int send_buf_size; |
||
58 | int udp_mtu; |
||
59 | int socks_mtu; |
||
60 | btime_t keepalive_time; |
||
61 | BReactor *reactor; |
||
62 | void *user; |
||
63 | SocksUdpClient_handler_received handler_received; |
||
64 | BAVL connections_tree; // By local_addr |
||
65 | DebugObject d_obj; |
||
66 | } SocksUdpClient; |
||
67 | |||
68 | struct SocksUdpClient_connection { |
||
69 | SocksUdpClient *client; |
||
70 | BAddr local_addr; |
||
71 | BSocksClient socks; |
||
72 | BufferWriter send_writer; |
||
73 | PacketBuffer send_buffer; |
||
74 | PacketPassInactivityMonitor send_monitor; |
||
75 | PacketPassInterface send_if; |
||
76 | BDatagram socket; |
||
77 | PacketPassInterface recv_if; |
||
78 | SinglePacketBuffer recv_buffer; |
||
79 | // The first_* members represent the initial packet, which has to be stored so it can |
||
80 | // wait for send_writer to become ready. |
||
81 | uint8_t *first_data; |
||
82 | int first_data_len; |
||
83 | BAddr first_remote_addr; |
||
84 | // If all packets sent so far have been sent to the same IP, port 53, with the |
||
85 | // same DNS ID, then this is that ID. Otherwise, it is -1. This is used to |
||
86 | // close ephemeral DNS query connections once a response is received. |
||
87 | int dns_id; |
||
88 | BPending first_job; |
||
89 | BAVLNode connections_tree_node; |
||
90 | }; |
||
91 | |||
92 | /** |
||
93 | * Initializes the SOCKS5-UDP client object. |
||
94 | * |
||
95 | * This function only initialzies the object and does not perform network access. |
||
96 | * |
||
97 | * @param o the object |
||
98 | * @param udp_mtu the maximum size of packets that will be sent through the tunnel |
||
99 | * @param max_connections how many local ports to track before dropping packets |
||
100 | * @param send_buf_size maximum number of buffered outgoing packets per connection |
||
101 | * @param keepalive_time how long to track an idle local port before forgetting it |
||
102 | * @param server_addr SOCKS5 server address |
||
103 | * @param auth_info List of authentication info for BSocksClient. The pointer must remain |
||
104 | * valid while this object exists, the data is not copied. |
||
105 | * @param num_auth_info Number of the above. |
||
106 | * @param reactor reactor we live in |
||
107 | * @param user value passed to handler |
||
108 | * @param handler_received handler for incoming UDP packets |
||
109 | * @return 1 on success, 0 on failure |
||
110 | */ |
||
111 | int SocksUdpClient_Init (SocksUdpClient *o, int udp_mtu, int max_connections, |
||
112 | int send_buf_size, btime_t keepalive_time, BAddr server_addr, |
||
113 | const struct BSocksClient_auth_info *auth_info, size_t num_auth_info, |
||
114 | BReactor *reactor, void *user, SocksUdpClient_handler_received handler_received); |
||
115 | |||
116 | /** |
||
117 | * Frees the SOCKS5-UDP client object. |
||
118 | * |
||
119 | * @param o the object |
||
120 | */ |
||
121 | void SocksUdpClient_Free (SocksUdpClient *o); |
||
122 | |||
123 | /** |
||
124 | * Submit a packet to be sent through the proxy. |
||
125 | * |
||
126 | * This will reuse an existing connection for packets from local_addr, or create one if |
||
127 | * there is none. If the number of live connections exceeds max_connections, or if the |
||
128 | * number of buffered packets from this port exceeds a limit, packets will be dropped |
||
129 | * silently. |
||
130 | * |
||
131 | * As a resource optimization, if a connection has only been used to send one DNS query, |
||
132 | * then the connection will be closed and freed once the reply is received. |
||
133 | * |
||
134 | * @param o the object |
||
135 | * @param local_addr the UDP packet's source address, and the expected destination for |
||
136 | * replies |
||
137 | * @param remote_addr the destination of the packet after it exits the proxy |
||
138 | * @param data the packet contents. Caller retains ownership. |
||
139 | * @param data_len number of bytes in the data |
||
140 | */ |
||
141 | void SocksUdpClient_SubmitPacket (SocksUdpClient *o, |
||
142 | BAddr local_addr, BAddr remote_addr, const uint8_t *data, int data_len); |
||
143 | |||
144 | #endif |