@@ -73,6 +73,7 @@ static void tls_listen_cb(int fd, uint16_t event, void *arg);
7373static void tls_client_cb (int fd , uint16_t event , void * arg );
7474static tls_client_t * tls_client_alloc (void );
7575static void tls_client_free (tls_client_t * client );
76+ static void tls_client_handle_data (tls_client_t * client , uint16_t event );
7677
7778/* External functions from wolfssl_io.c */
7879extern int wolfSSL_SetIO_wolfIP_CTX (WOLFSSL_CTX * ctx , struct wolfIP * s );
@@ -259,6 +260,38 @@ static void tls_client_free(tls_client_t *client)
259260 client -> state = TLS_CLIENT_STATE_FREE ;
260261}
261262
263+ static void tls_client_handle_data (tls_client_t * client , uint16_t event )
264+ {
265+ int ret ;
266+ int err ;
267+
268+ if (!(event & CB_EVENT_READABLE ) && wolfSSL_pending (client -> ssl ) == 0 ) {
269+ return ;
270+ }
271+
272+ /* Read encrypted data or any decrypted data already buffered by wolfSSL. */
273+ ret = wolfSSL_read (client -> ssl , server .rx_buf , sizeof (server .rx_buf ) - 1 );
274+ if (ret > 0 ) {
275+ ret = wolfSSL_write (client -> ssl , server .rx_buf , ret );
276+ if (ret <= 0 ) {
277+ err = wolfSSL_get_error (client -> ssl , ret );
278+ if (err != WOLFSSL_ERROR_WANT_WRITE ) {
279+ debug_print ("TLS: Write error\n" );
280+ tls_client_free (client );
281+ }
282+ }
283+ } else {
284+ err = wolfSSL_get_error (client -> ssl , ret );
285+ if (err == WOLFSSL_ERROR_ZERO_RETURN ) {
286+ debug_print ("TLS: Client closed connection\n" );
287+ tls_client_free (client );
288+ } else if (err != WOLFSSL_ERROR_WANT_READ ) {
289+ debug_print ("TLS: Read error\n" );
290+ tls_client_free (client );
291+ }
292+ }
293+ }
294+
262295static void tls_listen_cb (int fd , uint16_t event , void * arg )
263296{
264297 tls_client_t * client ;
@@ -337,6 +370,8 @@ static void tls_client_cb(int fd, uint16_t event, void *arg)
337370 if (ret == WOLFSSL_SUCCESS ) {
338371 debug_print ("TLS: Handshake complete\n" );
339372 client -> state = TLS_CLIENT_STATE_CONNECTED ;
373+ /* Process any app data that arrived in the same event batch. */
374+ tls_client_handle_data (client , event );
340375 } else {
341376 err = wolfSSL_get_error (client -> ssl , ret );
342377 if (err != WOLFSSL_ERROR_WANT_READ &&
@@ -349,34 +384,7 @@ static void tls_client_cb(int fd, uint16_t event, void *arg)
349384 break ;
350385
351386 case TLS_CLIENT_STATE_CONNECTED :
352- if (!(event & CB_EVENT_READABLE )) {
353- break ;
354- }
355-
356- /* Read encrypted data */
357- ret = wolfSSL_read (client -> ssl , server .rx_buf ,
358- sizeof (server .rx_buf ) - 1 );
359- if (ret > 0 ) {
360- /* Echo data back */
361- ret = wolfSSL_write (client -> ssl , server .rx_buf , ret );
362- if (ret <= 0 ) {
363- err = wolfSSL_get_error (client -> ssl , ret );
364- if (err != WOLFSSL_ERROR_WANT_WRITE ) {
365- debug_print ("TLS: Write error\n" );
366- tls_client_free (client );
367- }
368- }
369- } else {
370- err = wolfSSL_get_error (client -> ssl , ret );
371- if (err == WOLFSSL_ERROR_ZERO_RETURN ) {
372- /* Clean shutdown */
373- debug_print ("TLS: Client closed connection\n" );
374- tls_client_free (client );
375- } else if (err != WOLFSSL_ERROR_WANT_READ ) {
376- debug_print ("TLS: Read error\n" );
377- tls_client_free (client );
378- }
379- }
387+ tls_client_handle_data (client , event );
380388 break ;
381389
382390 default :
0 commit comments