Skip to content

Commit 408ce56

Browse files
committed
1. Echoserver to catch Ctrl-E to provide session statistics.
2. Add wolfSSH_GetStats() to return session statistics. 3. Echoserver server_worker threads get a context with their wolfSSH session, an ID number, and socket fd.
1 parent bcbf807 commit 408ce56

3 files changed

Lines changed: 76 additions & 13 deletions

File tree

examples/echoserver/echoserver.c

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ typedef int SOCKET_T;
9090
#endif
9191

9292
typedef 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+
291312
static 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

src/ssh.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,32 @@ int wolfSSH_CTX_UsePrivateKey_buffer(WOLFSSH_CTX* ctx,
557557
}
558558

559559

560+
void wolfSSH_GetStats(WOLFSSH* ssh, uint32_t* txCount, uint32_t* rxCount,
561+
uint32_t* seq, uint32_t* peerSeq)
562+
{
563+
uint32_t rTxCount = 0;
564+
uint32_t rRxCount = 0;
565+
uint32_t rSeq = 0;
566+
uint32_t rPeerSeq = 0;
567+
568+
if (ssh != NULL) {
569+
rTxCount = ssh->txCount;
570+
rRxCount = ssh->rxCount;
571+
rSeq = ssh->seq;
572+
rPeerSeq = ssh->peerSeq;
573+
}
574+
575+
if (txCount != NULL)
576+
*txCount = rTxCount;
577+
if (rxCount != NULL)
578+
*rxCount = rRxCount;
579+
if (seq != NULL)
580+
*seq = rSeq;
581+
if (peerSeq != NULL)
582+
*peerSeq = rPeerSeq;
583+
}
584+
585+
560586
int wolfSSH_KDF(uint8_t hashId, uint8_t keyId,
561587
uint8_t* key, uint32_t keySz,
562588
const uint8_t* k, uint32_t kSz,

wolfssh/ssh.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ WOLFSSH_API int wolfSSH_channel_send(WOLFSSH_CHANNEL*, uint8_t*, uint32_t);
136136
WOLFSSH_API int wolfSSH_worker(WOLFSSH*);
137137
WOLFSSH_API int wolfSSH_TriggerKeyExchange(WOLFSSH*);
138138

139+
WOLFSSH_API void wolfSSH_GetStats(WOLFSSH*,
140+
uint32_t*, uint32_t*, uint32_t*, uint32_t*);
141+
139142
WOLFSSH_API int wolfSSH_KDF(uint8_t, uint8_t, uint8_t*, uint32_t,
140143
const uint8_t*, uint32_t, const uint8_t*, uint32_t,
141144
const uint8_t*, uint32_t);

0 commit comments

Comments
 (0)