BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file StreamPeerIO.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 for communicating with a peer over TCP. |
||
32 | */ |
||
33 | |||
34 | #ifndef BADVPN_CLIENT_STREAMPEERIO_H |
||
35 | #define BADVPN_CLIENT_STREAMPEERIO_H |
||
36 | |||
37 | #include <stdint.h> |
||
38 | |||
39 | #include <nss/cert.h> |
||
40 | #include <nss/keyhi.h> |
||
41 | |||
42 | #include <misc/debug.h> |
||
43 | #include <base/DebugObject.h> |
||
44 | #include <base/BLog.h> |
||
45 | #include <system/BReactor.h> |
||
46 | #include <system/BConnection.h> |
||
47 | #include <structure/LinkedList1.h> |
||
48 | #include <flow/PacketProtoDecoder.h> |
||
49 | #include <flow/PacketStreamSender.h> |
||
50 | #include <flow/SinglePacketBuffer.h> |
||
51 | #include <flow/PacketProtoEncoder.h> |
||
52 | #include <flow/PacketCopier.h> |
||
53 | #include <flow/PacketPassConnector.h> |
||
54 | #include <flow/StreamRecvConnector.h> |
||
55 | #include <flow/SingleStreamSender.h> |
||
56 | #include <client/PasswordListener.h> |
||
57 | |||
58 | /** |
||
59 | * Callback function invoked when an error occurs with the peer connection. |
||
60 | * The object has entered default state. |
||
61 | * May be called from within a sending Send call. |
||
62 | * |
||
63 | * @param user value given to {@link StreamPeerIO_Init}. |
||
64 | */ |
||
65 | typedef void (*StreamPeerIO_handler_error) (void *user); |
||
66 | |||
67 | /** |
||
68 | * Object used for communicating with a peer over TCP. |
||
69 | * The object has a logical state which can be one of the following: |
||
70 | * - default state |
||
71 | * - listening state |
||
72 | * - connecting state |
||
73 | */ |
||
74 | typedef struct { |
||
75 | // common arguments |
||
76 | BReactor *reactor; |
||
77 | BThreadWorkDispatcher *twd; |
||
78 | int ssl; |
||
79 | int ssl_flags; |
||
80 | uint8_t *ssl_peer_cert; |
||
81 | int ssl_peer_cert_len; |
||
82 | int payload_mtu; |
||
83 | int sock_sndbuf; |
||
84 | BLog_logfunc logfunc; |
||
85 | StreamPeerIO_handler_error handler_error; |
||
86 | void *user; |
||
87 | |||
88 | // persistent I/O modules |
||
89 | |||
90 | // base sending objects |
||
91 | PacketCopier output_user_copier; |
||
92 | PacketProtoEncoder output_user_ppe; |
||
93 | SinglePacketBuffer output_user_spb; |
||
94 | PacketPassConnector output_connector; |
||
95 | |||
96 | // receiving objects |
||
97 | StreamRecvConnector input_connector; |
||
98 | PacketProtoDecoder input_decoder; |
||
99 | |||
100 | // connection side |
||
101 | int mode; |
||
102 | |||
103 | union { |
||
104 | // listening data |
||
105 | struct { |
||
106 | int state; |
||
107 | PasswordListener *listener; |
||
108 | PasswordListener_pwentry pwentry; |
||
109 | sslsocket *sock; |
||
110 | } listen; |
||
111 | // connecting data |
||
112 | struct { |
||
113 | int state; |
||
114 | CERTCertificate *ssl_cert; |
||
115 | SECKEYPrivateKey *ssl_key; |
||
116 | BConnector connector; |
||
117 | sslsocket sock; |
||
118 | BSSLConnection sslcon; |
||
119 | uint64_t password; |
||
120 | SingleStreamSender pwsender; |
||
121 | } connect; |
||
122 | }; |
||
123 | |||
124 | // socket data |
||
125 | sslsocket *sock; |
||
126 | BSSLConnection sslcon; |
||
127 | |||
128 | // sending objects |
||
129 | PacketStreamSender output_pss; |
||
130 | |||
131 | DebugObject d_obj; |
||
132 | } StreamPeerIO; |
||
133 | |||
134 | /** |
||
135 | * Initializes the object. |
||
136 | * The object is initialized in default state. |
||
137 | * {@link BLog_Init} must have been done. |
||
138 | * {@link BNetwork_GlobalInit} must have been done. |
||
139 | * {@link BSSLConnection_GlobalInit} must have been done if using SSL. |
||
140 | * |
||
141 | * @param pio the object |
||
142 | * @param reactor reactor we live in |
||
143 | * @param twd thread work dispatcher. May be NULL if ssl_flags does not request performing SSL |
||
144 | * operations in threads. |
||
145 | * @param ssl if nonzero, SSL will be used for peer connection |
||
146 | * @param ssl_flags flags passed down to {@link BSSLConnection_MakeBackend}. May be used to |
||
147 | * request performing SSL operations in threads. |
||
148 | * @param ssl_peer_cert if using SSL, the certificate we expect the peer to have |
||
149 | * @param ssl_peer_cert_len if using SSL, the length of the certificate |
||
150 | * @param payload_mtu maximum packet size as seen from the user. Must be >=0. |
||
151 | * @param sock_sndbuf socket SO_SNDBUF option. Specify <=0 to not set it. |
||
152 | * @param user_recv_if interface to use for submitting received packets. Its MTU |
||
153 | * must be >=payload_mtu. |
||
154 | * @param logfunc function which prepends the log prefix using {@link BLog_Append} |
||
155 | * @param handler_error handler function invoked when a connection error occurs |
||
156 | * @param user value to pass to handler functions |
||
157 | * @return 1 on success, 0 on failure |
||
158 | */ |
||
159 | int StreamPeerIO_Init ( |
||
160 | StreamPeerIO *pio, |
||
161 | BReactor *reactor, |
||
162 | BThreadWorkDispatcher *twd, |
||
163 | int ssl, |
||
164 | int ssl_flags, |
||
165 | uint8_t *ssl_peer_cert, |
||
166 | int ssl_peer_cert_len, |
||
167 | int payload_mtu, |
||
168 | int sock_sndbuf, |
||
169 | PacketPassInterface *user_recv_if, |
||
170 | BLog_logfunc logfunc, |
||
171 | StreamPeerIO_handler_error handler_error, |
||
172 | void *user |
||
173 | ) WARN_UNUSED; |
||
174 | |||
175 | /** |
||
176 | * Frees the object. |
||
177 | * |
||
178 | * @param pio the object |
||
179 | */ |
||
180 | void StreamPeerIO_Free (StreamPeerIO *pio); |
||
181 | |||
182 | /** |
||
183 | * Returns the interface for sending packets to the peer. |
||
184 | * The OTP warning handler may be called from within Send calls |
||
185 | * to the interface. |
||
186 | * |
||
187 | * @param pio the object |
||
188 | * @return interface for sending packets to the peer |
||
189 | */ |
||
190 | PacketPassInterface * StreamPeerIO_GetSendInput (StreamPeerIO *pio); |
||
191 | |||
192 | /** |
||
193 | * Starts an attempt to connect to the peer. |
||
194 | * On success, the object enters connecting state. |
||
195 | * On failure, the object enters default state. |
||
196 | * |
||
197 | * @param pio the object |
||
198 | * @param addr address to connect to |
||
199 | * @param password identification code to send to the peer |
||
200 | * @param ssl_cert if using SSL, the client certificate to use. This object does not |
||
201 | * take ownership of the certificate; it must remain valid until |
||
202 | * the object is reset. |
||
203 | * @param ssl_key if using SSL, the private key to use. This object does not take |
||
204 | * ownership of the key; it must remain valid until the object is reset. |
||
205 | * @return 1 on success, 0 on failure |
||
206 | */ |
||
207 | int StreamPeerIO_Connect (StreamPeerIO *pio, BAddr addr, uint64_t password, CERTCertificate *ssl_cert, SECKEYPrivateKey *ssl_key) WARN_UNUSED; |
||
208 | |||
209 | /** |
||
210 | * Starts an attempt to accept a connection from the peer. |
||
211 | * The object enters listening state. |
||
212 | * |
||
213 | * @param pio the object |
||
214 | * @param listener {@link PasswordListener} object to use for accepting a connection. |
||
215 | * The listener must have SSL enabled if and only if this object has |
||
216 | * SSL enabled. The listener must be available until the object is |
||
217 | * reset or {@link StreamPeerIO_handler_up} is called. |
||
218 | * @param password will return the identification code the peer should send when connecting |
||
219 | */ |
||
220 | void StreamPeerIO_Listen (StreamPeerIO *pio, PasswordListener *listener, uint64_t *password); |
||
221 | |||
222 | #endif |