@@ -90,7 +90,9 @@ typedef int SOCKET_T;
9090#endif
9191
9292typedef struct {
93- SOCKET_T clientFd ;
93+ WOLFSSH * ssh ;
94+ SOCKET_T fd ;
95+ uint32_t id ;
9496} thread_ctx_t ;
9597
9698
@@ -288,12 +290,30 @@ static uint8_t find_char(const uint8_t* str, const uint8_t* buf, uint32_t bufSz)
288290}
289291
290292
293+ static int dump_stats (thread_ctx_t * ctx )
294+ {
295+ char stats [1024 ];
296+ uint32_t statsSz ;
297+ uint32_t txCount , rxCount , seq , peerSeq ;
298+
299+ wolfSSH_GetStats (ctx -> ssh , & txCount , & rxCount , & seq , & peerSeq );
300+
301+ sprintf (stats ,
302+ "Statistics for Thread #%u:\r\n"
303+ " txCount = %u\r\n rxCount = %u\r\n"
304+ " seq = %u\r\n peerSeq = %u\r\n" ,
305+ ctx -> id , txCount , rxCount , seq , peerSeq );
306+ statsSz = (uint32_t )strlen (stats );
307+
308+ fprintf (stderr , "%s" , stats );
309+ return wolfSSH_stream_send (ctx -> ssh , (uint8_t * )stats , statsSz );
310+ }
311+
291312static THREAD_RETURN CYASSL_THREAD server_worker (void * vArgs )
292313{
293- WOLFSSH * ssh = (WOLFSSH * )vArgs ;
294- SOCKET_T clientFd = wolfSSH_get_fd (ssh );
314+ thread_ctx_t * threadCtx = (thread_ctx_t * )vArgs ;
295315
296- if (wolfSSH_accept (ssh ) == WS_SUCCESS ) {
316+ if (wolfSSH_accept (threadCtx -> ssh ) == WS_SUCCESS ) {
297317 uint8_t * buf = NULL ;
298318 uint8_t * tmpBuf ;
299319 int bufSz , backlogSz = 0 , rxSz , txSz , stop = 0 , txSum ;
@@ -308,7 +328,7 @@ static THREAD_RETURN CYASSL_THREAD server_worker(void* vArgs)
308328 buf = tmpBuf ;
309329
310330 if (!stop ) {
311- rxSz = wolfSSH_stream_read (ssh ,
331+ rxSz = wolfSSH_stream_read (threadCtx -> ssh ,
312332 buf + backlogSz ,
313333 EXAMPLE_BUFFER_SZ );
314334 if (rxSz > 0 ) {
@@ -317,7 +337,7 @@ static THREAD_RETURN CYASSL_THREAD server_worker(void* vArgs)
317337 txSz = 0 ;
318338
319339 while (backlogSz != txSum && txSz >= 0 && !stop ) {
320- txSz = wolfSSH_stream_send (ssh ,
340+ txSz = wolfSSH_stream_send (threadCtx -> ssh ,
321341 buf + txSum ,
322342 backlogSz - txSum );
323343
@@ -331,7 +351,8 @@ static THREAD_RETURN CYASSL_THREAD server_worker(void* vArgs)
331351 stop = 1 ;
332352 break ;
333353 case 0x05 :
334- fprintf (stderr , "dump stats\n" );
354+ if (dump_stats (threadCtx ) <= 0 )
355+ stop = 1 ;
335356 default :
336357 txSum += txSz ;
337358 }
@@ -351,8 +372,9 @@ static THREAD_RETURN CYASSL_THREAD server_worker(void* vArgs)
351372
352373 free (buf );
353374 }
354- close (clientFd );
355- wolfSSH_free (ssh );
375+ close (threadCtx -> fd );
376+ wolfSSH_free (threadCtx -> ssh );
377+ free (threadCtx );
356378
357379 return 0 ;
358380}
@@ -647,6 +669,7 @@ int main(void)
647669 PwMapList pwMapList ;
648670 SOCKET_T listenFd = 0 ;
649671 uint32_t defaultHighwater = EXAMPLE_HIGHWATER_MARK ;
672+ uint32_t threadCount = 0 ;
650673
651674 #ifdef DEBUG_WOLFSSH
652675 wolfSSH_Debugging_ON ();
@@ -695,12 +718,22 @@ int main(void)
695718
696719 tcp_bind (& listenFd , SERVER_PORT_NUMBER , 0 );
697720
721+ if (listen (listenFd , 5 ) != 0 )
722+ err_sys ("tcp listen failed" );
723+
698724 for (;;) {
699725 SOCKET_T clientFd = 0 ;
700726 SOCKADDR_IN_T clientAddr ;
701727 SOCKLEN_T clientAddrSz = sizeof (clientAddr );
702728 THREAD_TYPE thread ;
703729 WOLFSSH * ssh ;
730+ thread_ctx_t * threadCtx ;
731+
732+ threadCtx = (thread_ctx_t * )malloc (sizeof (thread_ctx_t ));
733+ if (threadCtx == NULL ) {
734+ fprintf (stderr , "Couldn't allocate thread context data.\n" );
735+ exit (EXIT_FAILURE );
736+ }
704737
705738 ssh = wolfSSH_new (ctx );
706739 if (ssh == NULL ) {
@@ -714,17 +747,18 @@ int main(void)
714747 wolfSSH_SetHighwater (ssh , defaultHighwater );
715748 }
716749
717- if (listen (listenFd , 5 ) != 0 )
718- err_sys ("tcp listen failed" );
719-
720750 clientFd = accept (listenFd , (struct sockaddr * )& clientAddr ,
721751 & clientAddrSz );
722752 if (clientFd == -1 )
723753 err_sys ("tcp accept failed" );
724754
725755 wolfSSH_set_fd (ssh , clientFd );
726756
727- pthread_create (& thread , 0 , server_worker , ssh );
757+ threadCtx -> ssh = ssh ;
758+ threadCtx -> fd = clientFd ;
759+ threadCtx -> id = threadCount ++ ;
760+
761+ pthread_create (& thread , 0 , server_worker , threadCtx );
728762 pthread_detach (thread );
729763 }
730764
0 commit comments