@@ -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
@@ -269,27 +271,49 @@ static INLINE void tcp_bind(SOCKET_T* sockFd, uint16_t port, int useAnyAddr)
269271}
270272
271273
272- static int find_char (uint8_t ch , const uint8_t * buf , uint32_t bufSz )
274+ static uint8_t find_char (const uint8_t * str , const uint8_t * buf , uint32_t bufSz )
273275{
276+ const uint8_t * cur ;
277+
274278 while (bufSz ) {
275- if ( ch == * buf )
276- return 1 ;
277- else {
278- buf ++ ;
279- bufSz -- ;
279+ cur = str ;
280+ while ( * cur != '\0' ) {
281+ if ( * cur == * buf )
282+ return * cur ;
283+ cur ++ ;
280284 }
285+ buf ++ ;
286+ bufSz -- ;
281287 }
282288
283289 return 0 ;
284290}
285291
286292
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+
287312static THREAD_RETURN CYASSL_THREAD server_worker (void * vArgs )
288313{
289- WOLFSSH * ssh = (WOLFSSH * )vArgs ;
290- SOCKET_T clientFd = wolfSSH_get_fd (ssh );
314+ thread_ctx_t * threadCtx = (thread_ctx_t * )vArgs ;
291315
292- if (wolfSSH_accept (ssh ) == WS_SUCCESS ) {
316+ if (wolfSSH_accept (threadCtx -> ssh ) == WS_SUCCESS ) {
293317 uint8_t * buf = NULL ;
294318 uint8_t * tmpBuf ;
295319 int bufSz , backlogSz = 0 , rxSz , txSz , stop = 0 , txSum ;
@@ -304,7 +328,7 @@ static THREAD_RETURN CYASSL_THREAD server_worker(void* vArgs)
304328 buf = tmpBuf ;
305329
306330 if (!stop ) {
307- rxSz = wolfSSH_stream_read (ssh ,
331+ rxSz = wolfSSH_stream_read (threadCtx -> ssh ,
308332 buf + backlogSz ,
309333 EXAMPLE_BUFFER_SZ );
310334 if (rxSz > 0 ) {
@@ -313,15 +337,25 @@ static THREAD_RETURN CYASSL_THREAD server_worker(void* vArgs)
313337 txSz = 0 ;
314338
315339 while (backlogSz != txSum && txSz >= 0 && !stop ) {
316- txSz = wolfSSH_stream_send (ssh ,
340+ txSz = wolfSSH_stream_send (threadCtx -> ssh ,
317341 buf + txSum ,
318342 backlogSz - txSum );
319343
320344 if (txSz > 0 ) {
321- if (find_char (0x03 , buf + txSum , txSz ))
322- stop = 1 ;
323- else
324- txSum += txSz ;
345+ uint8_t c ;
346+ const uint8_t matches [] = { 0x03 , 0x04 , 0x05 , 0x00 };
347+
348+ c = find_char (matches , buf + txSum , txSz );
349+ switch (c ) {
350+ case 0x03 :
351+ stop = 1 ;
352+ break ;
353+ case 0x05 :
354+ if (dump_stats (threadCtx ) <= 0 )
355+ stop = 1 ;
356+ default :
357+ txSum += txSz ;
358+ }
325359 }
326360 else if (txSz != WS_REKEYING )
327361 stop = 1 ;
@@ -338,8 +372,9 @@ static THREAD_RETURN CYASSL_THREAD server_worker(void* vArgs)
338372
339373 free (buf );
340374 }
341- close (clientFd );
342- wolfSSH_free (ssh );
375+ close (threadCtx -> fd );
376+ wolfSSH_free (threadCtx -> ssh );
377+ free (threadCtx );
343378
344379 return 0 ;
345380}
@@ -634,6 +669,7 @@ int main(void)
634669 PwMapList pwMapList ;
635670 SOCKET_T listenFd = 0 ;
636671 uint32_t defaultHighwater = EXAMPLE_HIGHWATER_MARK ;
672+ uint32_t threadCount = 0 ;
637673
638674 #ifdef DEBUG_WOLFSSH
639675 wolfSSH_Debugging_ON ();
@@ -682,12 +718,22 @@ int main(void)
682718
683719 tcp_bind (& listenFd , SERVER_PORT_NUMBER , 0 );
684720
721+ if (listen (listenFd , 5 ) != 0 )
722+ err_sys ("tcp listen failed" );
723+
685724 for (;;) {
686725 SOCKET_T clientFd = 0 ;
687726 SOCKADDR_IN_T clientAddr ;
688727 SOCKLEN_T clientAddrSz = sizeof (clientAddr );
689728 THREAD_TYPE thread ;
690729 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+ }
691737
692738 ssh = wolfSSH_new (ctx );
693739 if (ssh == NULL ) {
@@ -701,17 +747,18 @@ int main(void)
701747 wolfSSH_SetHighwater (ssh , defaultHighwater );
702748 }
703749
704- if (listen (listenFd , 5 ) != 0 )
705- err_sys ("tcp listen failed" );
706-
707750 clientFd = accept (listenFd , (struct sockaddr * )& clientAddr ,
708751 & clientAddrSz );
709752 if (clientFd == -1 )
710753 err_sys ("tcp accept failed" );
711754
712755 wolfSSH_set_fd (ssh , clientFd );
713756
714- 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 );
715762 pthread_detach (thread );
716763 }
717764
0 commit comments