]> git.ipfire.org Git - thirdparty/openssl.git/blob - doc/man7/ossl-guide-quic-client-non-block.pod
Expand the explanation of how to go and do useful work in non-blocking
[thirdparty/openssl.git] / doc / man7 / ossl-guide-quic-client-non-block.pod
1 =pod
2
3 =begin comment
4
5 NB: Changes to the source code samples in this file should also be reflected in
6 demos/guide/quic-client-non-block.c
7
8 =end comment
9
10 =head1 NAME
11
12 ossl-guide-quic-client-non-block
13 - OpenSSL Guide: Writing a simple nonblocking QUIC client
14
15 =head1 SIMPLE NONBLOCKING QUIC CLIENT EXAMPLE
16
17 This page will build on the example developed on the
18 L<ossl-guide-quic-client-block(7)> page which demonstrates how to write a simple
19 blocking QUIC client. On this page we will amend that demo code so that it
20 supports nonblocking functionality.
21
22 The complete source code for this example nonblocking QUIC client is available
23 in the B<demos/guide> directory of the OpenSSL source distribution in the file
24 B<quic-client-non-block.c>. It is also available online at
25 L<https://github.com/openssl/openssl/blob/master/demos/guide/quic-client-non-block.c>.
26
27 As we saw in the previous example an OpenSSL QUIC application always uses a
28 nonblocking socket. However, despite this, the B<SSL> object still has blocking
29 behaviour. When the B<SSL> object has blocking behaviour then this means that
30 it waits (blocks) until data is available to read if you attempt to read from
31 it when there is no data yet. Similarly it waits when writing if the B<SSL>
32 object is currently unable to write at the moment. This can simplify the
33 development of code because you do not have to worry about what to do in these
34 cases. The execution of the code will simply stop until it is able to continue.
35 However in many cases you do not want this behaviour. Rather than stopping and
36 waiting your application may need to go and do other tasks whilst the B<SSL>
37 object is unable to read/write, for example updating a GUI or performing
38 operations on some other connection or stream.
39
40 We will see later in this tutorial how to change the B<SSL> object so that it
41 has nonblocking behaviour. With a nonblocking B<SSL> object, functions such as
42 L<SSL_read_ex(3)> or L<SSL_write_ex(3)> will return immediately with a non-fatal
43 error if they are currently unable to read or write respectively.
44
45 Since this page is building on the example developed on the
46 L<ossl-guide-quic-client-block(7)> page we assume that you are familar with it
47 and we only explain how this example differs.
48
49 =head2 Performing work while waiting for the socket
50
51 In a nonblocking application you will need work to perform in the event that
52 we want to read or write to the B<SSL> object but we are currently unable to.
53 In fact this is the whole point of using a nonblocking B<SSL> object, i.e. to
54 give the application the opportunity to do something else. Whatever it is that
55 the application has to do, it must also be prepared to come back and retry the
56 operation that it previously attempted periodically to see if it can now
57 complete. Ideally it would only do this in the event that something has changed
58 such that it might succeed on the retry attempt, but this does not have to be
59 the case. It can retry at any time.
60
61 Note that it is important that you retry exactly the same operation that you
62 tried last time. You cannot start something new. For example if you were
63 attempting to write the text "Hello World" and the operation failed because the
64 B<SSL> object is currently unable to write, then you cannot then attempt to
65 write some other text when you retry the operation.
66
67 In this demo application we will create a helper function which simulates doing
68 other work. In fact, for the sake of simplicity, it will do nothing except wait
69 for the state of the underlying socket to change or until a timeout expires
70 after which the state of the B<SSL> object might have changed. We will call our
71 function C<wait_for_activity()>.
72
73 static void wait_for_activity(SSL *ssl)
74 {
75 fd_set wfds, rfds;
76 int width, sock, isinfinite;
77 struct timeval tv;
78 struct timeval *tvp = NULL;
79
80 /* Get hold of the underlying file descriptor for the socket */
81 sock = SSL_get_fd(ssl);
82
83 FD_ZERO(&wfds);
84 FD_ZERO(&rfds);
85
86 /*
87 * Find out if we would like to write to the socket, or read from it (or
88 * both)
89 */
90 if (SSL_net_write_desired(ssl))
91 FD_SET(sock, &wfds);
92 if (SSL_net_read_desired(ssl))
93 FD_SET(sock, &rfds);
94 width = sock + 1;
95
96 /*
97 * Find out when OpenSSL would next like to be called, regardless of
98 * whether the state of the underlying socket has changed or not.
99 */
100 if (SSL_get_event_timeout(ssl, &tv, &isinfinite) && !isinfinite)
101 tvp = &tv;
102
103 /*
104 * Wait until the socket is writeable or readable. We use select here
105 * for the sake of simplicity and portability, but you could equally use
106 * poll/epoll or similar functions
107 *
108 * NOTE: For the purposes of this demonstration code this effectively
109 * makes this demo block until it has something more useful to do. In a
110 * real application you probably want to go and do other work here (e.g.
111 * update a GUI, or service other connections).
112 *
113 * Let's say for example that you want to update the progress counter on
114 * a GUI every 100ms. One way to do that would be to use the timeout in
115 * the last parameter to "select" below. If the tvp value is greater
116 * than 100ms then use 100ms instead. Then, when select returns, you
117 * check if it did so because of activity on the file descriptors or
118 * because of the timeout. If the 100ms GUI timeout has expired but the
119 * tvp timeout has not then go and update the GUI and then restart the
120 * "select" (with updated timeouts).
121 */
122
123 select(width, &rfds, &wfds, NULL, tvp);
124 }
125
126 If you are familiar with how to write nonblocking applications in OpenSSL for
127 TLS (see L<ossl-guide-tls-client-non-block(7)>) then you should note that there
128 is an important difference here between the way a QUIC application and a TLS
129 application works. With a TLS application if we try to read or write something
130 to the B<SSL> object and we get a "retry" response (B<SSL_ERROR_WANT_READ> or
131 B<SSL_ERROR_WANT_WRITE>) then we can assume that is because OpenSSL attempted to
132 read or write to the underlying socket and the socket signalled the "retry".
133 With QUIC that is not the case. OpenSSL may signal retry as a result of an
134 L<SSL_read_ex(3)> or L<SSL_write_ex(3)> (or similar) call which indicates the
135 state of the stream. This is entirely independent of whether the underlying
136 socket needs to retry or not.
137
138 To determine whether OpenSSL currently wants to read or write to the underlying
139 socket for a QUIC application we must call the L<SSL_net_read_desired(3)> and
140 L<SSL_net_write_desired(3)> functions.
141
142 It is also important with QUIC that we periodically call an I/O function (or
143 otherwise call the L<SSL_handle_events(3)> function) to ensure that the QUIC
144 connection remains healthy. This is particularly important with a nonblocking
145 application because you are likely to leave the B<SSL> object idle for a while
146 while the application goes off to do other work. The L<SSL_get_event_timeout(3)>
147 function can be used to determine what the deadline is for the next time we need
148 to call an I/O function (or call L<SSL_handle_events(3)>).
149
150 An alternative to using L<SSL_get_event_timeout(3)> to find the next deadline
151 that OpenSSL must be called again by is to use "thread assisted" mode. In
152 "thread assisted" mode OpenSSL spawns an additional thread which will
153 periodically call L<SSL_handle_events(3)> automatically, meaning that the
154 application can leave the connection idle safe in the knowledge that the
155 connection will still be maintained in a healthy state. See
156 L</Creating the SSL_CTX and SSL objects> below for further details about this.
157
158 In this example we are using the C<select> function to check the
159 readability/writeability of the socket because it is very simple to use and is
160 available on most Operating Systems. However you could use any other similar
161 function to do the same thing. C<select> waits for the state of the underlying
162 socket(s) to become readable/writeable or until the timeout has expired before
163 returning.
164
165 =head2 Handling errors from OpenSSL I/O functions
166
167 A QUIC application that has been configured for nonblocking behaviour will need
168 to be prepared to handle errors returned from OpenSSL I/O functions such as
169 L<SSL_read_ex(3)> or L<SSL_write_ex(3)>. Errors may be fatal for the stream (for
170 example because the stream has been reset or because the underlying connection
171 has failed), or non-fatal (for example because we are trying to read from the
172 stream but no data has not yet arrived from the peer for that stream).
173
174 L<SSL_read_ex(3)> and L<SSL_write_ex(3)> will return 0 to indicate an error and
175 L<SSL_read(3)> and L<SSL_write(3)> will return 0 or a negative value to indicate
176 an error. L<SSL_shutdown(3)> will return a negative value to incidate an error.
177
178 In the event of an error an application should call L<SSL_get_error(3)> to find
179 out what type of error has occurred. If the error is non-fatal and can be
180 retried then L<SSL_get_error(3)> will return B<SSL_ERROR_WANT_READ> or
181 B<SSL_ERROR_WANT_WRITE> depending on whether OpenSSL wanted to read to or write
182 from the stream but was unable to. Note that a call to L<SSL_read_ex(3)> or
183 L<SSL_read(3)> can still generate B<SSL_ERROR_WANT_WRITE>. Similarly calls to
184 L<SSL_write_ex(3)> or L<SSL_write(3)> might generate B<SSL_ERROR_WANT_READ>.
185
186 Another type of non-fatal error that may occur is B<SSL_ERROR_ZERO_RETURN>. This
187 indicates an EOF (End-Of-File) which can occur if you attempt to read data from
188 an B<SSL> object but the peer has indicated that it will not send any more data
189 on the stream. In this case you may still want to write data to the stream but
190 you will not receive any more data.
191
192 Fatal errors that may occur are B<SSL_ERROR_SYSCALL> and B<SSL_ERROR_SSL>. These
193 indicate that the stream is no longer usable. For example, this could be because
194 the stream has been reset by the peer, or because the underlying connection has
195 failed. You can consult the OpenSSL error stack for further details (for example
196 by calling L<ERR_print_errors(3)> to print out details of errors that have
197 occurred). You can also consult the return value of
198 L<SSL_get_stream_read_state(3)> to determine whether the error is local to the
199 stream, or whether the underlying connection has also failed. A return value
200 of B<SSL_STREAM_STATE_RESET_REMOTE> tells you that the stream has been reset by
201 the peer and B<SSL_STREAM_STATE_CONN_CLOSED> tells you that the underlying
202 connection has closed.
203
204 In our demo application we will write a function to handle these errors from
205 OpenSSL I/O functions:
206
207 static int handle_io_failure(SSL *ssl, int res)
208 {
209 switch (SSL_get_error(ssl, res)) {
210 case SSL_ERROR_WANT_READ:
211 case SSL_ERROR_WANT_WRITE:
212 /* Temporary failure. Wait until we can read/write and try again */
213 wait_for_activity(ssl);
214 return 1;
215
216 case SSL_ERROR_ZERO_RETURN:
217 /* EOF */
218 return 0;
219
220 case SSL_ERROR_SYSCALL:
221 return -1;
222
223 case SSL_ERROR_SSL:
224 /*
225 * Some stream fatal error occurred. This could be because of a
226 * stream reset - or some failure occurred on the underlying
227 * connection.
228 */
229 switch (SSL_get_stream_read_state(ssl)) {
230 case SSL_STREAM_STATE_RESET_REMOTE:
231 printf("Stream reset occurred\n");
232 /*
233 * The stream has been reset but the connection is still
234 * healthy.
235 */
236 break;
237
238 case SSL_STREAM_STATE_CONN_CLOSED:
239 printf("Connection closed\n");
240 /* Connection is already closed. */
241 break;
242
243 default:
244 printf("Unknown stream failure\n");
245 break;
246 }
247 /*
248 * If the failure is due to a verification error we can get more
249 * information about it from SSL_get_verify_result().
250 */
251 if (SSL_get_verify_result(ssl) != X509_V_OK)
252 printf("Verify error: %s\n",
253 X509_verify_cert_error_string(SSL_get_verify_result(ssl)));
254 return -1;
255
256 default:
257 return -1;
258 }
259 }
260
261 This function takes as arguments the B<SSL> object that represents the
262 connection, as well as the return code from the I/O function that failed. In
263 the event of a non-fatal failure, it waits until a retry of the I/O operation
264 might succeed (by using the C<wait_for_activity()> function that we developed
265 in the previous section). It returns 1 in the event of a non-fatal error
266 (except EOF), 0 in the event of EOF, or -1 if a fatal error occurred.
267
268 =head2 Creating the SSL_CTX and SSL objects
269
270 In order to connect to a server we must create B<SSL_CTX> and B<SSL> objects for
271 this. Most of the steps to do this are the same as for a blocking client and are
272 explained on the L<ossl-guide-quic-client-block(7)> page. We won't repeat that
273 information here.
274
275 One key difference is that we must put the B<SSL> object into nonblocking mode
276 (the default is blocking mode). To do that we use the
277 L<SSL_set_blocking_mode(3)> function:
278
279 /*
280 * The underlying socket is always nonblocking with QUIC, but the default
281 * behaviour of the SSL object is still to block. We set it for nonblocking
282 * mode in this demo.
283 */
284 if (!SSL_set_blocking_mode(ssl, 0)) {
285 printf("Failed to turn off blocking mode\n");
286 goto end;
287 }
288
289 Although the demo application that we are developing here does not use it, it is
290 possible to use "thread assisted mode" when developing QUIC applications.
291 Normally, when writing an OpenSSL QUIC application, it is important that
292 L<SSL_handle_events(3)> (or alternatively any I/O function) is called on the
293 connection B<SSL> object periodically to maintain the connection in a healthy
294 state. See L</Performing work while waiting for the socket> for more discussion
295 on this. This is particularly important to keep in mind when writing a
296 nonblocking QUIC application because it is common to leave the B<SSL> connection
297 object idle for some time when using nonblocking mode. By using "thread assisted
298 mode" a separate thread is created by OpenSSL to do this automatically which
299 means that the application developer does not need to handle this aspect. To do
300 this we must use L<OSSL_QUIC_client_thread_method(3)> when we construct the
301 B<SSL_CTX> as shown below:
302
303 ctx = SSL_CTX_new(OSSL_QUIC_client_thread_method());
304 if (ctx == NULL) {
305 printf("Failed to create the SSL_CTX\n");
306 goto end;
307 }
308
309 =head2 Performing the handshake
310
311 As in the demo for a blocking QUIC client we use the L<SSL_connect(3)> function
312 to perform the handshake with the server. Since we are using a nonblocking
313 B<SSL> object it is very likely that calls to this function will fail with a
314 non-fatal error while we are waiting for the server to respond to our handshake
315 messages. In such a case we must retry the same L<SSL_connect(3)> call at a
316 later time. In this demo we do this in a loop:
317
318 /* Do the handshake with the server */
319 while ((ret = SSL_connect(ssl)) != 1) {
320 if (handle_io_failure(ssl, ret) == 1)
321 continue; /* Retry */
322 printf("Failed to connect to server\n");
323 goto end; /* Cannot retry: error */
324 }
325
326 We continually call L<SSL_connect(3)> until it gives us a success response.
327 Otherwise we use the C<handle_io_failure()> function that we created earlier to
328 work out what we should do next. Note that we do not expect an EOF to occur at
329 this stage, so such a response is treated in the same way as a fatal error.
330
331 =head2 Sending and receiving data
332
333 As with the blocking QUIC client demo we use the L<SSL_write_ex(3)> function to
334 send data to the server. As with L<SSL_connect(3)> above, because we are using
335 a nonblocking B<SSL> object, this call could fail with a non-fatal error. In
336 that case we should retry exactly the same L<SSL_write_ex(3)> call again. Note
337 that the parameters must be I<exactly> the same, i.e. the same pointer to the
338 buffer to write with the same length. You must not attempt to send different
339 data on a retry. An optional mode does exist
340 (B<SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER>) which will configure OpenSSL to allow
341 the buffer being written to change from one retry to the next. However, in this
342 case, you must still retry exactly the same data - even though the buffer that
343 contains that data may change location. See L<SSL_CTX_set_mode(3)> for further
344 details.
345
346 /* Write an HTTP GET request to the peer */
347 while (!SSL_write_ex(ssl, request, strlen(request), &written)) {
348 if (handle_io_failure(ssl, 0) == 1)
349 continue; /* Retry */
350 printf("Failed to write HTTP request\n");
351 goto end; /* Cannot retry: error */
352 }
353
354 On a write we do not expect to see an EOF response so we treat that case in the
355 same way as a fatal error.
356
357 Reading a response back from the server is similar:
358
359 do {
360 /*
361 * Get up to sizeof(buf) bytes of the response. We keep reading until
362 * the server closes the connection.
363 */
364 while (!eof && !SSL_read_ex(ssl, buf, sizeof(buf), &readbytes)) {
365 switch (handle_io_failure(ssl, 0)) {
366 case 1:
367 continue; /* Retry */
368 case 0:
369 eof = 1;
370 continue;
371 case -1:
372 default:
373 printf("Failed reading remaining data\n");
374 goto end; /* Cannot retry: error */
375 }
376 }
377 /*
378 * OpenSSL does not guarantee that the returned data is a string or
379 * that it is NUL terminated so we use fwrite() to write the exact
380 * number of bytes that we read. The data could be non-printable or
381 * have NUL characters in the middle of it. For this simple example
382 * we're going to print it to stdout anyway.
383 */
384 if (!eof)
385 fwrite(buf, 1, readbytes, stdout);
386 } while (!eof);
387 /* In case the response didn't finish with a newline we add one now */
388 printf("\n");
389
390 The main difference this time is that it is valid for us to receive an EOF
391 response when trying to read data from the server. This will occur when the
392 server closes down the connection after sending all the data in its response.
393
394 In this demo we just print out all the data we've received back in the response
395 from the server. We continue going around the loop until we either encounter a
396 fatal error, or we receive an EOF (indicating a graceful finish).
397
398 =head2 Shutting down the connection
399
400 As in the QUIC blocking example we must shutdown the connection when we are
401 finished with it.
402
403 Even though we have received EOF on the stream that we were reading from above,
404 this tell us nothing about the state of the underlying connection. Our demo
405 application will initiate the connection shutdown process via
406 L<SSL_shutdown(3)>.
407
408 Since our application is initiating the shutdown then we might expect to see
409 L<SSL_shutdown(3)> give a return value of 0, and then we should continue to call
410 it until we recieve a return value of 1 (meaning we have successfully completed
411 the shutdown). Since we are using a nonblocking B<SSL> object we might expect to
412 have to retry this operation several times. If L<SSL_shutdown(3)> returns a
413 negative result then we must call L<SSL_get_error(3)> to work out what to do
414 next. We use our handle_io_failure() function that we developed earlier for
415 this:
416
417 /*
418 * Repeatedly call SSL_shutdown() until the connection is fully
419 * closed.
420 */
421 while ((ret = SSL_shutdown(ssl)) != 1) {
422 if (ret < 0 && handle_io_failure(ssl, ret) == 1)
423 continue; /* Retry */
424 }
425
426 =head2 Final clean up
427
428 As with the blocking QUIC client example, once our connection is finished with
429 we must free it. The steps to do this for this example are the same as for the
430 blocking example, so we won't repeat it here.
431
432 =head1 FURTHER READING
433
434 See L<ossl-guide-quic-client-block(7)> to read a tutorial on how to write a
435 blocking QUIC client. See L<ossl-guide-quic-multi-stream(7)> to see how to write
436 a multi-stream QUIC client.
437
438 =head1 SEE ALSO
439
440 L<ossl-guide-introduction(7)>, L<ossl-guide-libraries-introduction(7)>,
441 L<ossl-guide-libssl-introduction(7)>, L<ossl-guide-quic-introduction(7)>,
442 L<ossl-guide-quic-client-block(7)>, L<ossl-guide-quic-multi-stream(7)>
443
444 =head1 COPYRIGHT
445
446 Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
447
448 Licensed under the Apache License 2.0 (the "License"). You may not use
449 this file except in compliance with the License. You can obtain a copy
450 in the file LICENSE in the source distribution or at
451 L<https://www.openssl.org/source/license.html>.
452
453 =cut