BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file BSocksClient.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 | * @section DESCRIPTION |
||
30 | * |
||
31 | * SOCKS5 client. TCP only, no authentication. |
||
32 | */ |
||
33 | |||
34 | #ifndef BADVPN_SOCKS_BSOCKSCLIENT_H |
||
35 | #define BADVPN_SOCKS_BSOCKSCLIENT_H |
||
36 | |||
37 | #include <stddef.h> |
||
38 | #include <stdint.h> |
||
39 | #include <stdbool.h> |
||
40 | |||
41 | #include <misc/debug.h> |
||
42 | #include <misc/debugerror.h> |
||
43 | #include <misc/socks_proto.h> |
||
44 | #include <misc/packed.h> |
||
45 | #include <base/DebugObject.h> |
||
46 | #include <base/BPending.h> |
||
47 | #include <system/BConnection.h> |
||
48 | #include <flow/PacketStreamSender.h> |
||
49 | |||
50 | #define BSOCKSCLIENT_EVENT_ERROR 1 |
||
51 | #define BSOCKSCLIENT_EVENT_UP 2 |
||
52 | #define BSOCKSCLIENT_EVENT_ERROR_CLOSED 3 |
||
53 | #define BSOCKSCLIENT_EVENT_CONNECTED 4 |
||
54 | |||
55 | /** |
||
56 | * Handler for events generated by the SOCKS client. |
||
57 | * |
||
58 | * The event is one of the following: |
||
59 | * - BSOCKSCLIENT_EVENT_ERROR: An error has occured. The object must be freed from the |
||
60 | * job closure of the handler and no further I/O must be attempted. |
||
61 | * - BSOCKSCLIENT_EVENT_ERROR_CLOSED: The server has closed the connection. This event |
||
62 | * can only be reported after BSOCKSCLIENT_EVENT_UP. The object must be freed from |
||
63 | * the job closure of the handler and no further I/O must be attempted. |
||
64 | * - BSOCKSCLIENT_EVENT_UP: The CONNECT or UDP ASSOCIATE operation was successful. In |
||
65 | * the case of CONNECT, application I/O may now begin. |
||
66 | * - BSOCKSCLIENT_EVENT_CONNECTED: The TCP connection to the server has been established |
||
67 | * and the SOCKS protocol is about to begin. The local address of the TCP connection is |
||
68 | * now available via @ref BSocksClient_GetLocalAddr. The job closure of this callback |
||
69 | * is the last chance to call @ref BSocksClient_SetDestAddr. |
||
70 | * |
||
71 | * @param user as in {@link BSocksClient_Init} |
||
72 | * @param event See above. |
||
73 | */ |
||
74 | typedef void (*BSocksClient_handler) (void *user, int event); |
||
75 | |||
76 | struct BSocksClient_auth_info { |
||
77 | int auth_type; |
||
78 | union { |
||
79 | struct { |
||
80 | const char *username; |
||
81 | size_t username_len; |
||
82 | const char *password; |
||
83 | size_t password_len; |
||
84 | } password; |
||
85 | }; |
||
86 | }; |
||
87 | |||
88 | typedef struct { |
||
89 | const struct BSocksClient_auth_info *auth_info; |
||
90 | size_t num_auth_info; |
||
91 | BAddr dest_addr; |
||
92 | bool udp; |
||
93 | BAddr bind_addr; |
||
94 | BSocksClient_handler handler; |
||
95 | void *user; |
||
96 | BReactor *reactor; |
||
97 | int state; |
||
98 | char *buffer; |
||
99 | BConnector connector; |
||
100 | BConnection con; |
||
101 | BPending continue_job; |
||
102 | union { |
||
103 | struct { |
||
104 | PacketPassInterface *send_if; |
||
105 | PacketStreamSender send_sender; |
||
106 | StreamRecvInterface *recv_if; |
||
107 | uint8_t *recv_dest; |
||
108 | int recv_len; |
||
109 | int recv_total; |
||
110 | } control; |
||
111 | }; |
||
112 | DebugError d_err; |
||
113 | DebugObject d_obj; |
||
114 | } BSocksClient; |
||
115 | |||
116 | struct BSocksClient_auth_info BSocksClient_auth_none (void); |
||
117 | struct BSocksClient_auth_info BSocksClient_auth_password (const char *username, size_t username_len, const char *password, size_t password_len); |
||
118 | |||
119 | /** |
||
120 | * Initializes the object. |
||
121 | * |
||
122 | * This object connects to a SOCKS5 server and performs a CONNECT or UDP ASSOCIATE |
||
123 | * operation. In any case, the object reports the BSOCKSCLIENT_EVENT_UP event via the |
||
124 | * handler when the operation was completed successfully. In the case of CONNECT, the |
||
125 | * user may then use the send and receive interfaces to exchange data through the |
||
126 | * connection (@ref BSocksClient_GetSendInterface and @ref BSocksClient_GetRecvInterface). |
||
127 | * |
||
128 | * @param o the object |
||
129 | * @param server_addr SOCKS5 server address |
||
130 | * @param auth_info List of supported authentication methods and associated parameters. |
||
131 | * Initialize these using functions such as BSocksClient_auth_none() and |
||
132 | * BSocksClient_auth_password(). The pointer must remain valid while this object |
||
133 | * exists, the data is not copied. |
||
134 | * @param num_auth_info Number of the above. There should be at least one, otherwise it |
||
135 | * certainly won't work. |
||
136 | * @param dest_addr Address to send as DST.ADDR in the CONNECT or UDP ASSOCIATE request. |
||
137 | * It is also possible to specify it later from the BSOCKSCLIENT_EVENT_CONNECTED |
||
138 | * event callback using @ref BSocksClient_SetDestAddr; this is necessary for UDP |
||
139 | * if the local TCP connection address must be known to bind the UDP socket. |
||
140 | * @param udp false to perform a CONNECT, true to perform a UDP ASSOCIATE |
||
141 | * @param handler handler for up and error events |
||
142 | * @param user value passed to handler |
||
143 | * @param reactor reactor we live in |
||
144 | * @return 1 on success, 0 on failure |
||
145 | */ |
||
146 | int BSocksClient_Init (BSocksClient *o, BAddr server_addr, |
||
147 | const struct BSocksClient_auth_info *auth_info, size_t num_auth_info, BAddr dest_addr, |
||
148 | bool udp, BSocksClient_handler handler, void *user, BReactor *reactor) WARN_UNUSED; |
||
149 | |||
150 | /** |
||
151 | * Frees the object. |
||
152 | * |
||
153 | * @param o the object |
||
154 | */ |
||
155 | void BSocksClient_Free (BSocksClient *o); |
||
156 | |||
157 | /** |
||
158 | * Get the local address of the TCP socket for the SOCKS server connection. |
||
159 | * |
||
160 | * This may only be called after the BSOCKSCLIENT_EVENT_CONNECTED event was reported. |
||
161 | * |
||
162 | * @param o the object |
||
163 | * @param local_addr On success the local address is returned here. |
||
164 | * @return 1 on success, 0 on failure |
||
165 | */ |
||
166 | int BSocksClient_GetLocalAddr (BSocksClient *o, BAddr *local_addr); |
||
167 | |||
168 | /** |
||
169 | * Set the DST.ADDR to send, overriding that specified in @ref BSocksClient_Init. |
||
170 | * |
||
171 | * The last chance to call this function is in the job closure of the |
||
172 | * BSOCKSCLIENT_EVENT_CONNECTED event, this must not be called after that. |
||
173 | * |
||
174 | * @param o the object |
||
175 | * @param dest_addr DST.ADDR to set. |
||
176 | */ |
||
177 | void BSocksClient_SetDestAddr (BSocksClient *o, BAddr dest_addr); |
||
178 | |||
179 | /** |
||
180 | * Return the BND.ADDR that the SOCKS server reported. |
||
181 | * |
||
182 | * This may only be called after the BSOCKSCLIENT_EVENT_UP event was reported. |
||
183 | * This address is needed for UDP ASSOCIATE because it is the address that the |
||
184 | * client should send UDP packets to. |
||
185 | * |
||
186 | * @param o the object |
||
187 | * @return The BND.ADDR, of type BADDR_TYPE_IPV4 or BADDR_TYPE_IPV6. |
||
188 | */ |
||
189 | BAddr BSocksClient_GetBindAddr (BSocksClient *o); |
||
190 | |||
191 | /** |
||
192 | * Returns the send interface. |
||
193 | * The object must be in up state. Additionally this must not be called if the |
||
194 | * object was initialized in UDP ASSOCIATE mode. |
||
195 | * |
||
196 | * @param o the object |
||
197 | * @return send interface |
||
198 | */ |
||
199 | StreamPassInterface * BSocksClient_GetSendInterface (BSocksClient *o); |
||
200 | |||
201 | /** |
||
202 | * Returns the receive interface. |
||
203 | * The object must be in up state. Additionally this must not be called if the |
||
204 | * object was initialized in UDP ASSOCIATE mode. |
||
205 | * |
||
206 | * @param o the object |
||
207 | * @return receive interface |
||
208 | */ |
||
209 | StreamRecvInterface * BSocksClient_GetRecvInterface (BSocksClient *o); |
||
210 | |||
211 | #endif |