example server attempts to accept an SSH connection. Note, the underlying code will lock up.

pull/1/head
John Safranek 2014-07-14 11:09:50 -07:00
parent 1945b2dddd
commit dd897d2f6c
1 changed files with 21 additions and 13 deletions

View File

@ -237,16 +237,18 @@ static WINLINE void tcp_bind(SOCKET_T* sockFd, uint16_t port, int useAnyAddr)
static THREAD_RETURN CYASSL_THREAD server_worker(void* vArgs) static THREAD_RETURN CYASSL_THREAD server_worker(void* vArgs)
{ {
thread_ctx_t* threadCtx = (thread_ctx_t*)vArgs; WOLFSSH* ssh = (WOLFSSH*)vArgs;
SOCKET_T clientFd = threadCtx->clientFd; SOCKET_T clientFd = wolfSSH_get_fd(ssh);
const char* msgA = "Who's there?!\n"; const char* msgA = "Who's there?!\n";
const char* msgB = "Go away!\n"; const char* msgB = "Go away!\n";
send(clientFd, msgA, strlen(msgA), 0); if (wolfSSH_accept(ssh) == WS_SUCCESS) {
sleep(1); send(clientFd, msgA, strlen(msgA), 0);
send(clientFd, msgB, strlen(msgB), 0); sleep(1);
send(clientFd, msgB, strlen(msgB), 0);
}
close(clientFd); close(clientFd);
free(threadCtx); wolfSSH_free(ssh);
return 0; return 0;
} }
@ -254,6 +256,7 @@ static THREAD_RETURN CYASSL_THREAD server_worker(void* vArgs)
int main(void) int main(void)
{ {
WOLFSSH_CTX* ctx = NULL;
SOCKET_T listenFd = 0; SOCKET_T listenFd = 0;
#ifdef DEBUG_WOLFSSH #ifdef DEBUG_WOLFSSH
@ -265,6 +268,12 @@ int main(void)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
ctx = wolfSSH_CTX_new(NULL);
if (ctx == NULL) {
fprintf(stderr, "Couldn't allocate SSH CTX data.\n");
exit(EXIT_FAILURE);
}
tcp_bind(&listenFd, 22222, 0); tcp_bind(&listenFd, 22222, 0);
for (;;) { for (;;) {
@ -272,11 +281,11 @@ int main(void)
SOCKADDR_IN_T clientAddr; SOCKADDR_IN_T clientAddr;
SOCKLEN_T clientAddrSz = sizeof(clientAddr); SOCKLEN_T clientAddrSz = sizeof(clientAddr);
THREAD_TYPE thread; THREAD_TYPE thread;
thread_ctx_t* threadCtx = WOLFSSH* ssh;
(thread_ctx_t*)calloc(1, sizeof(thread_ctx_t));
if (threadCtx == NULL) { ssh = wolfSSH_new(ctx);
fprintf(stderr, "Couldn't allocate thread data.\n"); if (ssh == NULL) {
fprintf(stderr, "Couldn't allocate SSH data.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -285,13 +294,12 @@ int main(void)
clientFd = accept(listenFd, (struct sockaddr*)&clientAddr, clientFd = accept(listenFd, (struct sockaddr*)&clientAddr,
&clientAddrSz); &clientAddrSz);
if (clientFd == -1) if (clientFd == -1)
err_sys("tcp accept failed"); err_sys("tcp accept failed");
threadCtx->clientFd = clientFd; wolfSSH_set_fd(ssh, clientFd);
pthread_create(&thread, 0, server_worker, threadCtx); pthread_create(&thread, 0, server_worker, ssh);
pthread_detach(thread); pthread_detach(thread);
} }