BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file ServerConnection.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 | * Object used to communicate with a VPN chat server. |
||
32 | */ |
||
33 | |||
34 | #ifndef BADVPN_SERVERCONNECTION_SERVERCONNECTION_H |
||
35 | #define BADVPN_SERVERCONNECTION_SERVERCONNECTION_H |
||
36 | |||
37 | #include <stdint.h> |
||
38 | |||
39 | #include <prinit.h> |
||
40 | #include <prio.h> |
||
41 | #include <prerror.h> |
||
42 | #include <prtypes.h> |
||
43 | #include <nss/nss.h> |
||
44 | #include <nss/ssl.h> |
||
45 | #include <nss/pk11func.h> |
||
46 | #include <nss/cert.h> |
||
47 | #include <nss/keyhi.h> |
||
48 | |||
49 | #include <misc/debug.h> |
||
50 | #include <misc/debugerror.h> |
||
51 | #include <protocol/scproto.h> |
||
52 | #include <protocol/msgproto.h> |
||
53 | #include <base/DebugObject.h> |
||
54 | #include <system/BConnection.h> |
||
55 | #include <flow/PacketProtoEncoder.h> |
||
56 | #include <flow/PacketStreamSender.h> |
||
57 | #include <flow/PacketProtoDecoder.h> |
||
58 | #include <flow/PacketPassPriorityQueue.h> |
||
59 | #include <flow/PacketProtoFlow.h> |
||
60 | #include <flowextra/KeepaliveIO.h> |
||
61 | #include <nspr_support/BSSLConnection.h> |
||
62 | #include <server_connection/SCKeepaliveSource.h> |
||
63 | |||
64 | /** |
||
65 | * Handler function invoked when an error occurs. |
||
66 | * The object must be freed from withing this function. |
||
67 | * |
||
68 | * @param user value passed to {@link ServerConnection_Init} |
||
69 | */ |
||
70 | typedef void (*ServerConnection_handler_error) (void *user); |
||
71 | |||
72 | /** |
||
73 | * Handler function invoked when the server becomes ready, i.e. |
||
74 | * the hello packet has been received. |
||
75 | * The object was in not ready state before. |
||
76 | * The object enters ready state before the handler is invoked. |
||
77 | * |
||
78 | * @param user value passed to {@link ServerConnection_Init} |
||
79 | * @param my_id our ID as reported by the server |
||
80 | * @param ext_ip the clientAddr field in the server's hello packet |
||
81 | */ |
||
82 | typedef void (*ServerConnection_handler_ready) (void *user, peerid_t my_id, uint32_t ext_ip); |
||
83 | |||
84 | /** |
||
85 | * Handler function invoked when a newclient packet is received. |
||
86 | * The object was in ready state. |
||
87 | * |
||
88 | * @param user value passed to {@link ServerConnection_Init} |
||
89 | * @param peer_id ID of the peer |
||
90 | * @param flags flags field from the newclient message |
||
91 | * @param cert peer's certificate (if any) |
||
92 | * @param cert_len certificate length. Will be >=0. |
||
93 | */ |
||
94 | typedef void (*ServerConnection_handler_newclient) (void *user, peerid_t peer_id, int flags, const uint8_t *cert, int cert_len); |
||
95 | |||
96 | /** |
||
97 | * Handler function invoked when an enclient packet is received. |
||
98 | * The object was in ready state. |
||
99 | * |
||
100 | * @param user value passed to {@link ServerConnection_Init} |
||
101 | * @param peer_id ID of the peer |
||
102 | */ |
||
103 | typedef void (*ServerConnection_handler_endclient) (void *user, peerid_t peer_id); |
||
104 | |||
105 | /** |
||
106 | * Handler function invoked when an inmsg packet is received. |
||
107 | * The object was in ready state. |
||
108 | * |
||
109 | * @param user value passed to {@link ServerConnection_Init} |
||
110 | * @param peer_id ID of the peer from which the message came |
||
111 | * @param data message payload |
||
112 | * @param data_len message length. Will be >=0. |
||
113 | */ |
||
114 | typedef void (*ServerConnection_handler_message) (void *user, peerid_t peer_id, uint8_t *data, int data_len); |
||
115 | |||
116 | /** |
||
117 | * Object used to communicate with a VPN chat server. |
||
118 | */ |
||
119 | typedef struct { |
||
120 | // global resources |
||
121 | BReactor *reactor; |
||
122 | BThreadWorkDispatcher *twd; |
||
123 | |||
124 | // keepalive interval |
||
125 | int keepalive_interval; |
||
126 | |||
127 | // send buffer size |
||
128 | int buffer_size; |
||
129 | |||
130 | // whether we use SSL |
||
131 | int have_ssl; |
||
132 | |||
133 | // ssl flags |
||
134 | int ssl_flags; |
||
135 | |||
136 | // client certificate if using SSL |
||
137 | CERTCertificate *client_cert; |
||
138 | |||
139 | // client private key if using SSL |
||
140 | SECKEYPrivateKey *client_key; |
||
141 | |||
142 | // server name if using SSL |
||
143 | char *server_name; |
||
144 | |||
145 | // handlers |
||
146 | void *user; |
||
147 | ServerConnection_handler_error handler_error; |
||
148 | ServerConnection_handler_ready handler_ready; |
||
149 | ServerConnection_handler_newclient handler_newclient; |
||
150 | ServerConnection_handler_endclient handler_endclient; |
||
151 | ServerConnection_handler_message handler_message; |
||
152 | |||
153 | // socket |
||
154 | BConnector connector; |
||
155 | BConnection con; |
||
156 | |||
157 | // job to report new client after sending acceptpeer |
||
158 | BPending newclient_job; |
||
159 | uint8_t *newclient_data; |
||
160 | int newclient_data_len; |
||
161 | |||
162 | // state |
||
163 | int state; |
||
164 | int buffers_released; |
||
165 | |||
166 | // whether an error is being reported |
||
167 | int error; |
||
168 | |||
169 | // defined when state > SERVERCONNECTION_STATE_CONNECTING |
||
170 | |||
171 | // SSL file descriptor, defined only if using SSL |
||
172 | PRFileDesc bottom_prfd; |
||
173 | PRFileDesc *ssl_prfd; |
||
174 | BSSLConnection sslcon; |
||
175 | |||
176 | // input |
||
177 | PacketProtoDecoder input_decoder; |
||
178 | PacketPassInterface input_interface; |
||
179 | |||
180 | // keepalive output branch |
||
181 | SCKeepaliveSource output_ka_zero; |
||
182 | PacketProtoEncoder output_ka_encoder; |
||
183 | |||
184 | // output common |
||
185 | PacketPassPriorityQueue output_queue; |
||
186 | KeepaliveIO output_keepaliveio; |
||
187 | PacketStreamSender output_sender; |
||
188 | |||
189 | // output local flow |
||
190 | int output_local_packet_len; |
||
191 | uint8_t *output_local_packet; |
||
192 | BufferWriter *output_local_if; |
||
193 | PacketProtoFlow output_local_oflow; |
||
194 | PacketPassPriorityQueueFlow output_local_qflow; |
||
195 | |||
196 | // output user flow |
||
197 | PacketPassPriorityQueueFlow output_user_qflow; |
||
198 | |||
199 | // job to start client I/O |
||
200 | BPending start_job; |
||
201 | |||
202 | DebugError d_err; |
||
203 | DebugObject d_obj; |
||
204 | } ServerConnection; |
||
205 | |||
206 | /** |
||
207 | * Initializes the object. |
||
208 | * The object is initialized in not ready state. |
||
209 | * {@link BLog_Init} must have been done. |
||
210 | * {@link BNetwork_GlobalInit} must have been done. |
||
211 | * {@link BSSLConnection_GlobalInit} must have been done if using SSL. |
||
212 | * |
||
213 | * @param o the object |
||
214 | * @param reactor {@link BReactor} we live in |
||
215 | * @param twd thread work dispatcher. May be NULL if ssl_flags does not request performing SSL |
||
216 | * operations in threads. |
||
217 | * @param addr address to connect to |
||
218 | * @param keepalive_interval keep-alive sending interval. Must be >0. |
||
219 | * @param buffer_size minimum size of send buffer in number of packets. Must be >0. |
||
220 | * @param have_ssl whether to use SSL for connecting to the server. Must be 1 or 0. |
||
221 | * @param ssl_flags flags passed down to {@link BSSLConnection_MakeBackend}. May be used to |
||
222 | * request performing SSL operations in threads. |
||
223 | * @param client_cert if using SSL, client certificate to use. Must remain valid as |
||
224 | * long as this object is alive. |
||
225 | * @param client_key if using SSL, prvate ket to use. Must remain valid as |
||
226 | * long as this object is alive. |
||
227 | * @param server_name if using SSL, the name of the server. The string is copied. |
||
228 | * @param user value passed to callback functions |
||
229 | * @param handler_error error handler. The object must be freed from within the error |
||
230 | * handler before doing anything else with this object. |
||
231 | * @param handler_ready handler when the server becomes ready, i.e. the hello message has |
||
232 | * been received. |
||
233 | * @param handler_newclient handler when a newclient message has been received |
||
234 | * @param handler_endclient handler when an endclient message has been received |
||
235 | * @param handler_message handler when a peer message has been reveived |
||
236 | * @return 1 on success, 0 on failure |
||
237 | */ |
||
238 | int ServerConnection_Init ( |
||
239 | ServerConnection *o, |
||
240 | BReactor *reactor, |
||
241 | BThreadWorkDispatcher *twd, |
||
242 | BAddr addr, |
||
243 | int keepalive_interval, |
||
244 | int buffer_size, |
||
245 | int have_ssl, |
||
246 | int ssl_flags, |
||
247 | CERTCertificate *client_cert, |
||
248 | SECKEYPrivateKey *client_key, |
||
249 | const char *server_name, |
||
250 | void *user, |
||
251 | ServerConnection_handler_error handler_error, |
||
252 | ServerConnection_handler_ready handler_ready, |
||
253 | ServerConnection_handler_newclient handler_newclient, |
||
254 | ServerConnection_handler_endclient handler_endclient, |
||
255 | ServerConnection_handler_message handler_message |
||
256 | ) WARN_UNUSED; |
||
257 | |||
258 | /** |
||
259 | * Frees the object. |
||
260 | * {@link ServerConnection_ReleaseBuffers} must have been called if the |
||
261 | * send interface obtained from {@link ServerConnection_GetSendInterface} |
||
262 | * was used. |
||
263 | * |
||
264 | * @param o the object |
||
265 | */ |
||
266 | void ServerConnection_Free (ServerConnection *o); |
||
267 | |||
268 | /** |
||
269 | * Stops using any buffers passed to the send interface obtained from |
||
270 | * {@link ServerConnection_GetSendInterface}. If the send interface |
||
271 | * has been used, this must be called at appropriate time before this |
||
272 | * object is freed. |
||
273 | */ |
||
274 | void ServerConnection_ReleaseBuffers (ServerConnection *o); |
||
275 | |||
276 | /** |
||
277 | * Returns an interface for sending data to the server (just one). |
||
278 | * This goes directly into the link (i.e. TCP, possibly via SSL), so packets |
||
279 | * need to be manually encoded according to PacketProto. |
||
280 | * The interface must not be used after an error was reported. |
||
281 | * The object must be in ready state. |
||
282 | * Must not be called from the error handler. |
||
283 | * |
||
284 | * @param o the object |
||
285 | * @return the interface |
||
286 | */ |
||
287 | PacketPassInterface * ServerConnection_GetSendInterface (ServerConnection *o); |
||
288 | |||
289 | #endif |