ossl-guide-quic-client-non-block.7ossl 24 KB

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