3333#include <errno.h>
3434
3535/* socket */
36+ #ifndef NET_USER_HEADER
3637#include <fcntl.h>
3738#include <netdb.h>
3839#include <sys/socket.h>
3940#include <sys/types.h>
4041#include <sys/select.h>
4142#include <unistd.h>
43+ #endif
4244
4345/* wolfSSL */
4446#ifdef WOLFSSL_USER_SETTINGS
5557/* ------------------------------------------------------------------ */
5658/* POSIX transport helpers (replace with your BSP/port layer). */
5759/* ------------------------------------------------------------------ */
60+ #ifndef NET_USER_HEADER
5861static int posix_set_nonblocking (int fd )
5962{
6063 int flags = fcntl (fd , F_GETFL , 0 );
@@ -144,54 +147,11 @@ static int posix_net_connect(const char* host, int port)
144147 }
145148 return fd ;
146149}
150+ #endif
147151
148152/* ------------------------------------------------------------------ */
149153/* WOLFSSL_USER_IO callbacks. */
150154/* ------------------------------------------------------------------ */
151- static int posix_send_cb (WOLFSSL * ssl , char * buf , int sz , void * ctx )
152- {
153- (void )ssl ;
154- int fd = (int )(intptr_t )ctx ;
155- int ret = (int )send (fd , buf , (size_t )sz , 0 );
156- if (ret >= 0 ) {
157- return ret ;
158- }
159- if (errno == EAGAIN || errno == EWOULDBLOCK ) {
160- return WOLFSSL_CBIO_ERR_WANT_WRITE ;
161- }
162- return WOLFSSL_CBIO_ERR_GENERAL ;
163- }
164-
165- static int posix_recv_cb (WOLFSSL * ssl , char * buf , int sz , void * ctx )
166- {
167- (void )ssl ;
168- int fd = (int )(intptr_t )ctx ;
169- int ret = (int )recv (fd , buf , (size_t )sz , 0 );
170- if (ret >= 0 ) {
171- return ret ;
172- }
173- if (errno == EAGAIN || errno == EWOULDBLOCK ) {
174- return WOLFSSL_CBIO_ERR_WANT_READ ;
175- }
176- return WOLFSSL_CBIO_ERR_GENERAL ;
177- }
178-
179- int posix_getdevrandom (unsigned char * out , unsigned int sz );
180- int posix_getdevrandom (unsigned char * out , unsigned int sz )
181- {
182- ssize_t ret ;
183- int fd = open ("/dev/urandom" , O_RDONLY );
184- if (fd < 0 ) {
185- return -1 ;
186- }
187- ret = read (fd , out , sz );
188- close (fd );
189- if (ret != (ssize_t )sz ) {
190- return -1 ;
191- }
192- return 0 ;
193- }
194-
195155static void usage (const char * prog )
196156{
197157 printf ("usage: %s [--ecc|--x25519] [host] [port]\n" , prog );
@@ -260,7 +220,14 @@ int client_async_test(int argc, char** argv)
260220 return 0 ;
261221 }
262222
263- net = posix_net_connect (host , port );
223+ {
224+ const char * ready = getenv (WOLFSSL_ASYNC_READYFILE_ENV );
225+ if (ready != NULL ) {
226+ (void )async_readyfile_wait (ready ,
227+ WOLFSSL_ASYNC_READYFILE_TIMEOUT_MS );
228+ }
229+ }
230+ net = NET_CONNECT (host , port );
264231 if (net < 0 ) {
265232 return -1 ;
266233 }
@@ -288,10 +255,11 @@ int client_async_test(int argc, char** argv)
288255 /* Bare-metal style: disable verification unless you load CA/peer certs. */
289256 wolfSSL_CTX_set_verify (ctx , WOLFSSL_VERIFY_NONE , NULL );
290257
291- wolfSSL_SetIORecv (ctx , posix_recv_cb );
292- wolfSSL_SetIOSend (ctx , posix_send_cb );
258+ wolfSSL_SetIORecv (ctx , NET_IO_RECV_CB );
259+ wolfSSL_SetIOSend (ctx , NET_IO_SEND_CB );
293260
294- wolfSSL_CTX_UseSNI (ctx , WOLFSSL_SNI_HOST_NAME , host , strlen (host ));
261+ wolfSSL_CTX_UseSNI (ctx , WOLFSSL_SNI_HOST_NAME , host ,
262+ (word16 )XSTRLEN (host ));
295263
296264 ssl = wolfSSL_new (ctx );
297265 if (ssl == NULL ) {
@@ -300,7 +268,8 @@ int client_async_test(int argc, char** argv)
300268
301269 wolfSSL_SetIOReadCtx (ssl , (void * )(intptr_t )net );
302270 wolfSSL_SetIOWriteCtx (ssl , (void * )(intptr_t )net );
303- (void )wolfSSL_UseSNI (ssl , WOLFSSL_SNI_HOST_NAME , host , (word16 )XSTRLEN (host ));
271+ (void )wolfSSL_UseSNI (ssl , WOLFSSL_SNI_HOST_NAME , host ,
272+ (word16 )XSTRLEN (host ));
304273
305274 for (;;) {
306275 ret = wolfSSL_UseKeyShare (ssl , group );
@@ -328,10 +297,10 @@ int client_async_test(int argc, char** argv)
328297 break ;
329298 }
330299 err = wolfSSL_get_error (ssl , 0 );
331- if (err == WC_PENDING_E ||
300+ if (err == WC_NO_ERR_TRACE ( WC_PENDING_E ) ||
332301 err == WOLFSSL_ERROR_WANT_READ ||
333302 err == WOLFSSL_ERROR_WANT_WRITE ) {
334- if (err == WC_PENDING_E ) {
303+ if (err == WC_NO_ERR_TRACE ( WC_PENDING_E ) ) {
335304#ifdef WOLFSSL_DEBUG_NONBLOCK
336305 pending_count ++ ;
337306#endif
@@ -368,10 +337,10 @@ int client_async_test(int argc, char** argv)
368337 break ;
369338 }
370339 err = wolfSSL_get_error (ssl , 0 );
371- if (err == WC_PENDING_E ||
340+ if (err == WC_NO_ERR_TRACE ( WC_PENDING_E ) ||
372341 err == WOLFSSL_ERROR_WANT_READ ||
373342 err == WOLFSSL_ERROR_WANT_WRITE ) {
374- if (err == WC_PENDING_E ) {
343+ if (err == WC_NO_ERR_TRACE ( WC_PENDING_E ) ) {
375344#ifdef WOLFSSL_DEBUG_NONBLOCK
376345 pending_count ++ ;
377346#endif
@@ -400,10 +369,10 @@ int client_async_test(int argc, char** argv)
400369 break ;
401370 }
402371 err = wolfSSL_get_error (ssl , 0 );
403- if (err == WC_PENDING_E ||
372+ if (err == WC_NO_ERR_TRACE ( WC_PENDING_E ) ||
404373 err == WOLFSSL_ERROR_WANT_READ ||
405374 err == WOLFSSL_ERROR_WANT_WRITE ) {
406- if (err == WC_PENDING_E ) {
375+ if (err == WC_NO_ERR_TRACE ( WC_PENDING_E ) ) {
407376#ifdef WOLFSSL_DEBUG_NONBLOCK
408377 pending_count ++ ;
409378#endif
@@ -444,7 +413,7 @@ int client_async_test(int argc, char** argv)
444413#endif
445414 wolfSSL_Cleanup ();
446415 if (net >= 0 ) {
447- close (net );
416+ NET_CLOSE (net );
448417 }
449418
450419 return ret ;
0 commit comments