@@ -127,6 +127,7 @@ int posixTransportTls_SendRequest(void* context, uint16_t size,
127127 (void * )& ctx -> tcpCtx , & ctx -> connect_fd_p1 ) != WH_ERROR_OK ) {
128128 return WH_ERROR_NOTREADY ;
129129 }
130+ ctx -> connect_fd_p1 ++ ;
130131
131132 ctx -> ssl = wolfSSL_new (ctx -> ssl_ctx );
132133 if (!ctx -> ssl ) {
@@ -135,7 +136,7 @@ int posixTransportTls_SendRequest(void* context, uint16_t size,
135136 }
136137
137138 /* Set the current socket file descriptor */
138- rc = wolfSSL_set_fd (ctx -> ssl , ctx -> connect_fd_p1 );
139+ rc = wolfSSL_set_fd (ctx -> ssl , ctx -> connect_fd_p1 - 1 );
139140 if (rc != WOLFSSL_SUCCESS ) {
140141 wolfSSL_free (ctx -> ssl );
141142 ctx -> ssl = NULL ;
@@ -157,16 +158,16 @@ int posixTransportTls_SendRequest(void* context, uint16_t size,
157158 * send was in the TCP backlog waiting on the server. But
158159 * if the server closes down the listen port then RST gets
159160 * returned. Retry the TCP connect() */
160- wolfSSL_free (ctx -> ssl );
161- ctx -> ssl = NULL ;
161+ wolfSSL_free (ctx -> ssl );
162+ ctx -> ssl = NULL ;
162163
163- /* Close the failed socket fd and set state for retry */
164- if (ctx -> tcpCtx .connect_fd_p1 != 0 ) {
165- close (ctx -> tcpCtx .connect_fd_p1 - 1 );
164+ /* Close the failed socket fd and set state for retry */
165+ if (ctx -> tcpCtx .connect_fd_p1 != 0 ) {
166166 ctx -> tcpCtx .connect_fd_p1 = 0 ;
167167 }
168- ctx -> tcpCtx .state = PTT_STATE_UNCONNECTED ;
169- return WH_ERROR_NOTREADY ;
168+ ctx -> connect_fd_p1 = 0 ;
169+ ctx -> tcpCtx .state = PTT_STATE_UNCONNECTED ;
170+ return WH_ERROR_NOTREADY ;
170171
171172 }
172173
@@ -347,7 +348,9 @@ int posixTransportTls_RecvRequest(void* context, uint16_t* out_size, void* data)
347348 ctx -> client_addr = client_addr ;
348349
349350 /* Make accepted socket non-blocking */
350- fcntl (ctx -> accept_fd_p1 - 1 , F_SETFL , O_NONBLOCK );
351+ if (fcntl (ctx -> accept_fd_p1 - 1 , F_SETFL , O_NONBLOCK ) != 0 ) {
352+ return WH_ERROR_ABORTED ;
353+ }
351354
352355 /* Create SSL object for this connection */
353356 ctx -> ssl = wolfSSL_new (ctx -> ssl_ctx );
@@ -378,7 +381,8 @@ int posixTransportTls_RecvRequest(void* context, uint16_t* out_size, void* data)
378381 }
379382 }
380383
381- /* Read data from SSL connection */
384+ /* Read data from SSL connection (also handles continuing on with
385+ * handshake if not complete yet) */
382386 rc = wolfSSL_read (ctx -> ssl , data , PTTLS_PACKET_MAX_SIZE );
383387 err = wolfSSL_get_error (ctx -> ssl , rc );
384388 if (rc > 0 ) {
@@ -423,7 +427,7 @@ int posixTransportTls_SendResponse(void* context, uint16_t size,
423427 }
424428 else {
425429 int err = wolfSSL_get_error (ctx -> ssl , rc );
426- if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE ) {
430+ if (( NonBlockingError ( err )) ) {
427431 return WH_ERROR_NOTREADY ;
428432 }
429433 return WH_ERROR_ABORTED ;
0 commit comments