From 95461c350f5db0dade43db544067d1285d4cf366 Mon Sep 17 00:00:00 2001 From: Conner Date: Fri, 26 May 2017 15:20:09 -0600 Subject: [PATCH 1/6] Removed Respond method so that code reads more linearly --- psk/server-psk.c | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/psk/server-psk.c b/psk/server-psk.c index 2d46d0d1..dffd670e 100644 --- a/psk/server-psk.c +++ b/psk/server-psk.c @@ -36,31 +36,6 @@ #define LISTENQ 1024 #define SERV_PORT 11111 -/* - * Handles response to client. - */ -int Respond(WOLFSSL* ssl) -{ - int n; /* length of string read */ - char buf[MAXLINE]; /* string read from client */ - char response[] = "I hear ya for shizzle"; - memset(buf, 0, MAXLINE); - n = wolfSSL_read(ssl, buf, MAXLINE); - if (n > 0) { - printf("%s\n", buf); - if (wolfSSL_write(ssl, response, strlen(response)) > strlen(response)) { - printf("Fatal error : respond: write error\n"); - return 1; - } - } - if (n < 0) { - printf("Fatal error :respond: read error\n"); - return 1; - } - - return 0; -} - /* * Identify which psk key to use. */ @@ -84,10 +59,13 @@ static unsigned int my_psk_server_cb(WOLFSSL* ssl, const char* identity, int main() { + int n; /* length of string read */ int listenfd, connfd; int opt; struct sockaddr_in cliAddr, servAddr; char buff[MAXLINE]; + char buf[MAXLINE]; /* string read from client */ + char response[] = "I hear ya for shizzle"; socklen_t cliLen; WOLFSSL_CTX* ctx; @@ -160,8 +138,25 @@ int main() printf("Fatal error : wolfSSL_new error\n"); return 1; } + + /* sets the file descriptor of the socket for the ssl session */ wolfSSL_set_fd(ssl, connfd); - if (Respond(ssl) != 0) { + + /* making sure buffered to store data sent from client is emprty */ + memset(buf, 0, MAXLINE); + + /* reads and displays data sent by client if no errors occur */ + n = wolfSSL_read(ssl, buf, MAXLINE); + if (n > 0) { + printf("%s\n", buf); + /* server response */ + if (wolfSSL_write(ssl, response, strlen(response)) > strlen(response)) { + printf("Fatal error : respond: write error\n"); + return 1; + } + } + if (n < 0) { + printf("Fatal error :respond: read error\n"); return 1; } From 6c518455ddf7718d73de5d32ea7d32f6faf4fc7c Mon Sep 17 00:00:00 2001 From: Conner Date: Wed, 31 May 2017 09:50:35 -0600 Subject: [PATCH 2/6] removed additional methods so that the code reads linearly in server-psk and client-psk --- psk/client-psk.c | 46 +++++++++++++++------------------------------- psk/server-psk.c | 2 +- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/psk/client-psk.c b/psk/client-psk.c index cbaf6f71..2bf108e7 100755 --- a/psk/client-psk.c +++ b/psk/client-psk.c @@ -58,36 +58,11 @@ static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint, return 4; } -/* - * this function will send the inputted string to the server and then - * recieve the string from the server outputing it to the termial - */ -int SendReceive(WOLFSSL* ssl) -{ - char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ - char recvline[MAXLINE]; /* string received from the server */ - - /* write string to the server */ - if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) { - printf("Write Error to Server\n"); - return 1; - } - - /* flags if the Server stopped before the client could end */ - if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) { - printf("Client: Server Terminated Prematurely!\n"); - return 1; - } - - /* show message from the server */ - printf("Server Message: %s\n", recvline); - - return 0; -} - int main(int argc, char **argv) { int ret, sockfd; + char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ + char recvline[MAXLINE]; /* string received from the server */ WOLFSSL* ssl; WOLFSSL_CTX* ctx; struct sockaddr_in servaddr;; @@ -148,11 +123,20 @@ int main(int argc, char **argv) return 1; } - /* takes inputting string and outputs it to the server */ - ret = SendReceive(ssl); - if (ret != 0) { + /* write string to the server */ + if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) { + printf("Write Error to Server\n"); return 1; - } + } + + /* check if server ended before client could read a response */ + if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) { + printf("Client: Server Terminated Prematurely!\n"); + return 1; + } + + /* show message from the server */ + printf("Server Message: %s\n", recvline); /* cleanup */ wolfSSL_free(ssl); diff --git a/psk/server-psk.c b/psk/server-psk.c index dffd670e..06b7e74f 100644 --- a/psk/server-psk.c +++ b/psk/server-psk.c @@ -62,10 +62,10 @@ int main() int n; /* length of string read */ int listenfd, connfd; int opt; - struct sockaddr_in cliAddr, servAddr; char buff[MAXLINE]; char buf[MAXLINE]; /* string read from client */ char response[] = "I hear ya for shizzle"; + struct sockaddr_in cliAddr, servAddr; socklen_t cliLen; WOLFSSL_CTX* ctx; From ae1ea7d33d1a7f3501dbb4d616a69bb796e23af2 Mon Sep 17 00:00:00 2001 From: Conner Date: Wed, 31 May 2017 09:51:18 -0600 Subject: [PATCH 3/6] removed additional methods so that the code reads linearly in server-tcp and client-tcp --- psk/client-tcp.c | 38 ++++++++++++++------------------------ psk/server-tcp.c | 48 +++++++++++++++++++++--------------------------- 2 files changed, 35 insertions(+), 51 deletions(-) diff --git a/psk/client-tcp.c b/psk/client-tcp.c index 6d679de4..d01454d0 100644 --- a/psk/client-tcp.c +++ b/psk/client-tcp.c @@ -37,31 +37,12 @@ * this function will send the inputted string to the server and then * recieve the string from the server outputing it to the termial */ -int SendReceive(int sockfd) -{ - char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ - char recvline[MAXLINE]; /* string received from the server */ - - /* write string to the server */ - if (write(sockfd, sendline, strlen(sendline)) != strlen(sendline)) { - printf("Write Error to Server\n"); - return 1; - } - - /* flags if the server stopped before the client could end */ - if (read(sockfd, recvline, MAXLINE) == 0) { - printf("Client: Server Terminated Prematurely!\n"); - return 1; - } - - printf("Server Message: %s\n", recvline); - - return 0; -} int main(int argc, char **argv) { int sockfd, ret; + char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ + char recvline[MAXLINE]; /* string received from the server */ struct sockaddr_in servaddr; /* must include an ip address or this will flag */ @@ -96,11 +77,20 @@ int main(int argc, char **argv) } /* takes inputting string and outputs it to the server */ - ret = SendReceive(sockfd); - if (ret != 0){ - printf("Send Recieve Error"); + /* write string to the server */ + if (write(sockfd, sendline, strlen(sendline)) != strlen(sendline)) { + printf("Write Error to Server\n"); return 1; } + + /* flags if the server stopped before the client could end */ + if (read(sockfd, recvline, MAXLINE) == 0) { + printf("Client: Server Terminated Prematurely!\n"); + return 1; + } + + printf("Server Message: %s\n", recvline); + /* close socket and connection */ close(sockfd); diff --git a/psk/server-tcp.c b/psk/server-tcp.c index 3a05b9e5..9a2c3315 100644 --- a/psk/server-tcp.c +++ b/psk/server-tcp.c @@ -42,34 +42,17 @@ void err_sys(const char *err, ...) printf("Fatal error : %s\n", err); } -/* - * Handles response to client. - */ -void respond(int sockfd) -{ - int n; /* length of string read */ - char buf[MAXLINE]; /* string read from client */ - char response[22] = "I hear ya for shizzle"; - memset(buf, 0, MAXLINE); - n = read(sockfd, buf, MAXLINE); - if (n > 0) { - printf("%s\n", buf); - if (write(sockfd, response, 22) > 22) { - err_sys("write error"); - } - } - if (n < 0) { - err_sys("respond: read error"); - } -} - int main() { - int listenfd, connfd; - int opt; + int listenfd, connfd; + int opt; + int n; /* length of string read */ + char buff[MAXLINE]; + char buf[MAXLINE]; /* string read from client */ + char response[22] = "I hear ya for shizzle"; struct sockaddr_in cliAddr, servAddr; - char buff[MAXLINE]; - socklen_t cliLen; + + socklen_t cliLen; /* find a socket , 0 for using TCP option */ listenfd = socket(AF_INET, SOCK_STREAM, 0); @@ -109,8 +92,19 @@ int main() printf("Connection from %s, port %d\n", inet_ntop(AF_INET, &cliAddr.sin_addr, buff, sizeof(buff)), ntohs(cliAddr.sin_port)); - - respond(connfd); + + /* empty response buffer to avoid unexpected output */ + memset(buf, 0, MAXLINE); + n = read(connfd, buf, MAXLINE); + if (n > 0) { + printf("%s\n", buf); + if (write(connfd, response, 22) > 22) { + err_sys("write error"); + } + } + if (n < 0) { + err_sys("respond: read error"); + } /* closes the connections after responding */ if (close(connfd) == -1) { err_sys("close error"); From bdf4b25e00ed5a85f1bc85cc3563b392c89a122b Mon Sep 17 00:00:00 2001 From: Conner Date: Wed, 31 May 2017 09:52:10 -0600 Subject: [PATCH 4/6] removed additional methods so that the code reads linearly in server-psk-nonblocking and client-psk-nonblocking --- psk/client-psk-nonblocking.c | 187 ++++++++++------------- psk/server-psk-nonblocking.c | 286 +++++++++++++++++++---------------- 2 files changed, 234 insertions(+), 239 deletions(-) diff --git a/psk/client-psk-nonblocking.c b/psk/client-psk-nonblocking.c index 11dfdf13..00c80911 100644 --- a/psk/client-psk-nonblocking.c +++ b/psk/client-psk-nonblocking.c @@ -45,80 +45,6 @@ enum { TEST_ERROR_READY }; - -static inline int tcp_select(int socketfd, int to_sec) -{ - fd_set recvfds, errfds; - int nfds = socketfd + 1; - struct timeval timeout = { (to_sec > 0) ? to_sec : 0, 0}; - int result; - - FD_ZERO(&recvfds); - FD_SET(socketfd, &recvfds); - FD_ZERO(&errfds); - FD_SET(socketfd, &errfds); - - result = select(nfds, &recvfds, NULL, &errfds, &timeout); - - if (result == 0) { - return TEST_TIMEOUT; - } - else if (result > 0) { - if (FD_ISSET(socketfd, &recvfds)) { - return TEST_RECV_READY; - } - else if(FD_ISSET(socketfd, &errfds)) { - return TEST_ERROR_READY; - } - } - - return TEST_SELECT_FAIL; -} - -/* - * sets up and uses nonblocking protocols using wolfssl - */ -static int NonBlockingSSL_Connect(WOLFSSL* ssl) -{ - int ret, error, sockfd, select_ret, currTimeout; - - ret = wolfSSL_connect(ssl); - error = wolfSSL_get_error(ssl, 0); - sockfd = (int)wolfSSL_get_fd(ssl); - - while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || - error == SSL_ERROR_WANT_WRITE)) { - currTimeout = 1; - - if (error == SSL_ERROR_WANT_READ) { - printf("... client would read block\n"); - } - else { - printf("... client would write block\n"); - } - - select_ret = tcp_select(sockfd, currTimeout); - - if ((select_ret == TEST_RECV_READY) || - (select_ret == TEST_ERROR_READY)) { - ret = wolfSSL_connect(ssl); - error = wolfSSL_get_error(ssl, 0); - } - else if (select_ret == TEST_TIMEOUT) { - error = SSL_ERROR_WANT_READ; - } - else { - error = SSL_FATAL_ERROR; - } - } - if (ret != SSL_SUCCESS){ - printf("SSL_connect failed"); - return 1; - } - - return 0; -} - /* *psk client set up. */ @@ -143,39 +69,17 @@ static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint, return 4; } -/* - * this function will send the inputted string to the server and then - * recieve the string from the server outputing it to the termial - */ -int SendReceive(WOLFSSL* ssl) -{ - char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ - char recvline[MAXLINE]; /* string received from the server */ - - /* write string to the server */ - if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) { - printf("Write Error to Server\n"); - return 1; - } - - /* flags if the Server stopped before the client could end */ - if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) { - printf("Client: Server Terminated Prematurely!\n"); - return 1; - } - - /* show message from the server */ - printf("Server Message: %s\n", recvline); - - return 0; -} - int main(int argc, char **argv) { - int sockfd, ret; + int sockfd, ret, error, select_ret, currTimeout; + int nfds; + int result; + char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ + char recvline[MAXLINE]; /* string received from the server */ + fd_set recvfds, errfds; WOLFSSL_CTX* ctx; WOLFSSL* ssl; - struct sockaddr_in servaddr;; + struct sockaddr_in servaddr; /* must include an ip address of this will flag */ if (argc != 2) { @@ -245,25 +149,88 @@ int main(int argc, char **argv) /* invokes the fcntl callable service to set file status flags. * Do not block an open, a read, or a write on the file * (do not wait for terminal input. If an error occurs, - * stop program*/ + * stop program */ flags = fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); if (flags < 0) { printf("fcntl set failed\n"); return 1; } - + /* setting up and running nonblocking socket */ - ret = NonBlockingSSL_Connect(ssl); - if (ret != 0) { - return 1; + ret = wolfSSL_connect(ssl); + error = wolfSSL_get_error(ssl, 0); + + while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || + error == SSL_ERROR_WANT_WRITE)) { + currTimeout = 1; + + if (error == SSL_ERROR_WANT_READ) { + printf("... client would read block\n"); + } + else { + printf("... client would write block\n"); + } + + struct timeval timeout = { (currTimeout > 0) ? currTimeout : 0, 0}; + sockfd = (int)wolfSSL_get_fd(ssl); + + FD_ZERO(&recvfds); + FD_SET(sockfd, &recvfds); + FD_ZERO(&errfds); + FD_SET(sockfd, &errfds); + + nfds = sockfd + 1; + + result = select(nfds, &recvfds, NULL, &errfds, &timeout); + + if (result == 0) { + select_ret = TEST_TIMEOUT; + } + else if (result > 0) { + if (FD_ISSET(sockfd, &recvfds)) { + select_ret = TEST_RECV_READY; + } + else if(FD_ISSET(sockfd, &errfds)) { + select_ret = TEST_ERROR_READY; + } + } + else { + select_ret = TEST_SELECT_FAIL; + } + + if ((select_ret == TEST_RECV_READY) || + (select_ret == TEST_ERROR_READY)) { + ret = wolfSSL_connect(ssl); + error = wolfSSL_get_error(ssl, 0); + } + else if (select_ret == TEST_TIMEOUT) { + error = SSL_ERROR_WANT_READ; + } + else { + error = SSL_FATAL_ERROR; + } + } + if (ret != SSL_SUCCESS){ + printf("SSL_connect failed"); + return 1; } /* takes inputting string and outputs it to the server */ - ret = SendReceive(ssl); - if (ret != 0) { + /* write string to the server */ + if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) { + printf("Write Error to Server\n"); + return 1; + } + + /* flags if the Server stopped before the client could end */ + if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) { + printf("Client: Server Terminated Prematurely!\n"); return 1; } + /* show message from the server */ + printf("Server Message: %s\n", recvline); + /* cleanup */ wolfSSL_free(ssl); diff --git a/psk/server-psk-nonblocking.c b/psk/server-psk-nonblocking.c index aa123e63..8116f8e3 100644 --- a/psk/server-psk-nonblocking.c +++ b/psk/server-psk-nonblocking.c @@ -47,127 +47,6 @@ enum{ TEST_ERROR_READY }; - -/* - * Pulled in from wolfssl/test.h - * Select the tcp, used when nonblocking. Checks the status of the connection. - */ -int tcp_select(int sockfd, int to_sec) -{ - fd_set recvfds, errfds; - int nfds = sockfd + 1; - struct timeval timeout = {to_sec, 0}; - int result; - - /* reset socket values */ - FD_ZERO(&recvfds); - FD_SET(sockfd, &recvfds); - FD_ZERO(&errfds); - FD_SET(sockfd, &errfds); - - result = select(nfds, &recvfds, NULL, &errfds, &timeout); - - /* logic for which enumerated value is returned */ - if (result == 0) { - return TEST_TIMEOUT; - } - else if (result > 0) { - if (FD_ISSET(sockfd, &recvfds)) { - return TEST_RECV_READY; - } - else if (FD_ISSET(sockfd, &errfds)) { - return TEST_ERROR_READY; - } - } - - return TEST_SELECT_FAIL; -} - - -/* - * Pulled in from examples/server/server.c - * Function to handle nonblocking. Loops until tcp_select notifies that it's - * ready for action. - */ -int NonBlockingSSL(WOLFSSL* ssl) -{ - int ret; - int error; - int select_ret; - int sockfd = wolfSSL_get_fd(ssl); - ret = wolfSSL_accept(ssl); - error = wolfSSL_get_error(ssl, 0); - while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || - error == SSL_ERROR_WANT_WRITE)) { - int currTimeout = 1; - - /* print out for user notification */ - if (error == SSL_ERROR_WANT_READ) { - printf("... server would read block\n"); - } - else { - printf("... server would write block\n"); - } - - select_ret = tcp_select(sockfd, currTimeout); - - /* if tcp_select signals ready try to accept otherwise continue loop*/ - if ((select_ret == TEST_RECV_READY) || - (select_ret == TEST_ERROR_READY)) { - ret = wolfSSL_accept(ssl); - error = wolfSSL_get_error(ssl, 0); - } - else if (select_ret == TEST_TIMEOUT) { - error = SSL_ERROR_WANT_READ; - } - else { - error = SSL_FATAL_ERROR; - } - } - /* faliure to accept */ - if (ret != SSL_SUCCESS) { - printf("Fatal error : SSL_accept failed\n"); - ret = SSL_FATAL_ERROR; - } - - return ret; -} - - -/* - * Handles response to client. - */ -int Respond(WOLFSSL* ssl) -{ - int n; /* length of string read */ - char buf[MAXLINE]; /* string read from client */ - char response[] = "I hear ya for shizzle"; - - memset(buf, 0, MAXLINE); - do { - if (NonBlockingSSL(ssl) != SSL_SUCCESS) { - return 1; - } - - n = wolfSSL_read(ssl, buf, MAXLINE); - if (n > 0) { - printf("%s\n", buf); - } - } - while(n < 0); - - if (NonBlockingSSL(ssl) != SSL_SUCCESS) { - return 1; - } - - if (wolfSSL_write(ssl, response, strlen(response)) != strlen(response)) { - printf("Fatal error : respond: write error\n"); - return 1; - } - - return 0; -} - /* * Used for finding psk value. */ @@ -192,12 +71,24 @@ static inline unsigned int my_psk_server_cb(WOLFSSL* ssl, const char* identity, int main() { - int listenfd, connfd; - int opt; - struct sockaddr_in cliAddr, servAddr; - char buff[MAXLINE]; + int n; /* length of string read */ + int ret; + int error; + int result; + int select_ret; + int sockfd; + int nfds; + int currTimeout = 1; + int listenfd, connfd; + int opt; + char buff[MAXLINE]; /* buffer for tcp connection */ + char buf[MAXLINE]; /* string read from client */ + char response[] = "I hear ya for shizzle"; + fd_set recvfds, errfds; socklen_t cliLen; WOLFSSL_CTX* ctx; + struct sockaddr_in cliAddr, servAddr; + struct timeval timeout = {currTimeout, 0}; wolfSSL_Init(); @@ -268,17 +159,153 @@ int main() return 1; } wolfSSL_set_fd(ssl, connfd); - + sockfd = wolfSSL_get_fd(ssl); + /* set wolfSSL and socket to non blocking and respond */ wolfSSL_set_using_nonblock(ssl, 1); if (fcntl(connfd, F_SETFL, O_NONBLOCK) < 0) { printf("Fatal error : fcntl set failed\n"); return 1; } - if (Respond(ssl) != 0) { - printf("Fatal error : respond error\n"); - return 1; + + ret = wolfSSL_accept(ssl); + error = wolfSSL_get_error(ssl, 0); + + /* clearing buffer for client reponse to prevent unexpected output*/ + memset(buf, 0, MAXLINE); + do { + + while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || + error == SSL_ERROR_WANT_WRITE)) { + + /* print out for user notification */ + if (error == SSL_ERROR_WANT_READ) { + printf("... server would read block\n"); } + else { + printf("... server would write block\n"); + } +/* -------------------------------------------------------------------------- */ +/* TCP */ +/* -------------------------------------------------------------------------- */ + FD_ZERO(&recvfds); + FD_SET(sockfd, &recvfds); + FD_ZERO(&errfds); + FD_SET(sockfd, &errfds); + + nfds = sockfd + 1; + result = select(nfds, &recvfds, NULL, &errfds, &timeout); + + if (result == 0) { + select_ret = TEST_TIMEOUT; + } + else if (result > 0) { + if (FD_ISSET(sockfd, &recvfds)) { + select_ret = TEST_RECV_READY; + } + else if (FD_ISSET(sockfd, &errfds)) { + select_ret = TEST_ERROR_READY; + } + } + else { + select_ret = TEST_SELECT_FAIL; + } + + /* if tcp_select signals ready try to accept otherwise continue loop*/ + if ((select_ret == TEST_RECV_READY) || + (select_ret == TEST_ERROR_READY)) { + ret = wolfSSL_accept(ssl); + error = wolfSSL_get_error(ssl, 0); + } + else if (select_ret == TEST_TIMEOUT) { + error = SSL_ERROR_WANT_READ; + } + else { + error = SSL_FATAL_ERROR; + } + } + /* faliure to accept */ + if (ret != SSL_SUCCESS) { + printf("Fatal error : SSL_accept failed\n"); + ret = SSL_FATAL_ERROR; + } + + if (ret != SSL_SUCCESS) { + return 1; + } + + n = wolfSSL_read(ssl, buf, MAXLINE); + if (n > 0) { + printf("%s\n", buf); + } + } + while(n < 0); + + while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || + error == SSL_ERROR_WANT_WRITE)) { + + /* print out for user notification */ + if (error == SSL_ERROR_WANT_READ) { + printf("... server would read block\n"); + } + else { + printf("... server would write block\n"); + } +/* -------------------------------------------------------------------------- */ +/* TCP */ +/* -------------------------------------------------------------------------- */ + FD_ZERO(&recvfds); + FD_SET(sockfd, &recvfds); + FD_ZERO(&errfds); + FD_SET(sockfd, &errfds); + + nfds = sockfd + 1; + result = select(nfds, &recvfds, NULL, &errfds, &timeout); + + if (result == 0) { + select_ret = TEST_TIMEOUT; + } + else if (result > 0) { + if (FD_ISSET(sockfd, &recvfds)) { + select_ret = TEST_RECV_READY; + } + else if (FD_ISSET(sockfd, &errfds)) { + select_ret = TEST_ERROR_READY; + } + } + else { + select_ret = TEST_SELECT_FAIL; + } + + /* if tcp_select signals ready try to accept otherwise continue loop*/ + if ((select_ret == TEST_RECV_READY) || + (select_ret == TEST_ERROR_READY)) { + ret = wolfSSL_accept(ssl); + error = wolfSSL_get_error(ssl, 0); + } + else if (select_ret == TEST_TIMEOUT) { + error = SSL_ERROR_WANT_READ; + } + else { + error = SSL_FATAL_ERROR; + } + } + + /* faliure to accept */ + if (ret != SSL_SUCCESS) { + printf("Fatal error : SSL_accept failed\n"); + ret = SSL_FATAL_ERROR; + } + + if (ret != SSL_SUCCESS) { + return 1; + } + + if (wolfSSL_write(ssl, response, strlen(response)) != strlen(response)) { + printf("Fatal error : respond: write error\n"); + return 1; + } + /* closes the connections after responding */ wolfSSL_shutdown(ssl); @@ -289,6 +316,7 @@ int main() } } } + /* free up memory used by wolfssl */ wolfSSL_CTX_free(ctx); wolfSSL_Cleanup(); From 4352c413f9bac786088283ec3b97a5c5bfb90db8 Mon Sep 17 00:00:00 2001 From: Conner Date: Wed, 31 May 2017 09:53:05 -0600 Subject: [PATCH 5/6] removed additional methods so that the code reads linearly and fixed session not resuming error in client-psk-resume --- psk/client-psk-resume.c | 65 ++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/psk/client-psk-resume.c b/psk/client-psk-resume.c index 6bb7f2b5..f26521b7 100644 --- a/psk/client-psk-resume.c +++ b/psk/client-psk-resume.c @@ -60,36 +60,11 @@ static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint, return 4; } -/* - * this function will send the inputted string to the server and then - * recieve the string from the server outputing it to the termial - */ -int SendReceive(WOLFSSL* ssl) -{ - char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ - char recvline[MAXLINE]; /* string received from the server */ - - /* write string to the server */ - if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) { - printf("Write Error to Server\n"); - return 1; - } - - /* flags if the Server stopped before the client could end */ - if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) { - printf("Client: Server Terminated Prematurely!\n"); - return 1; - } - - /* show message from the server */ - printf("Server Message: %s\n", recvline); - - return 0; -} - int main(int argc, char **argv){ int sockfd, sock, ret; + char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ + char recvline[MAXLINE]; /* string received from the server */ WOLFSSL* ssl; WOLFSSL* sslResume = 0; WOLFSSL_SESSION* session = 0; @@ -146,7 +121,19 @@ int main(int argc, char **argv){ wolfSSL_set_fd(ssl, sockfd); /* takes inputting string and outputs it to the server */ - SendReceive(ssl); + if (wolfSSL_write(ssl, sendline, sizeof(sendline)) != sizeof(sendline)) { + printf("Write Error to Server\n"); + return 1; + } + + /* flags if the Server stopped before the client could end */ + if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) { + printf("Client: Server Terminated Prematurely!\n"); + return 1; + } + + /* show message from the server */ + printf("Server Message: %s\n", recvline); /* Save the session ID to reuse */ session = wolfSSL_get_session(ssl); @@ -158,9 +145,8 @@ int main(int argc, char **argv){ /* close connection */ close(sockfd); - /* cleanup without wolfSSL_Cleanup() for now */ + /* cleanup without wolfSSL_Cleanup() and wolfSSL_CTX_free() for now */ wolfSSL_free(ssl); - wolfSSL_CTX_free(ctx); /* * resume session, start new connection and socket @@ -186,25 +172,30 @@ int main(int argc, char **argv){ return 1; } - /* takes inputting string and outputs it to the server */ - ret = SendReceive(sslResume); - if (ret != 0) { + if (wolfSSL_write(sslResume, sendline, sizeof(sendline)) != sizeof(sendline)) { + printf("Write Error to Server\n"); + return 1; + } + + /* flags if the Server stopped before the client could end */ + if (wolfSSL_read(sslResume, recvline, MAXLINE) < 0 ) { + printf("Client: Server Terminated Prematurely!\n"); return 1; } + /* show message from the server */ + printf("Server Message: %s\n", recvline); /* check to see if the session id is being reused */ if (wolfSSL_session_reused(sslResume)) { printf("reused session id\n"); } - else + else{ printf("didn't reuse session id!!!\n"); - + } /* shut down wolfSSL */ wolfSSL_shutdown(sslResume); - /* shut down socket */ close(sock); - /* clean up now with wolfSSL_Cleanup() */ wolfSSL_free(sslResume); wolfSSL_CTX_free(ctx); From 579b4d8e7dffd418b3506aca7425639d77308f8d Mon Sep 17 00:00:00 2001 From: Conner Date: Thu, 1 Jun 2017 13:15:56 -0600 Subject: [PATCH 6/6] Fixed tab indentations. Moved struct in client-psk-nonblocking to top of block --- psk/client-psk-nonblocking.c | 4 +- psk/client-psk-resume.c | 16 +-- psk/client-psk.c | 8 +- psk/server-psk-nonblocking.c | 239 +++++++++++++++++------------------ 4 files changed, 134 insertions(+), 133 deletions(-) diff --git a/psk/client-psk-nonblocking.c b/psk/client-psk-nonblocking.c index 00c80911..4f1e8465 100644 --- a/psk/client-psk-nonblocking.c +++ b/psk/client-psk-nonblocking.c @@ -79,6 +79,7 @@ int main(int argc, char **argv) fd_set recvfds, errfds; WOLFSSL_CTX* ctx; WOLFSSL* ssl; + struct timeval timeout; struct sockaddr_in servaddr; /* must include an ip address of this will flag */ @@ -171,7 +172,8 @@ int main(int argc, char **argv) printf("... client would write block\n"); } - struct timeval timeout = { (currTimeout > 0) ? currTimeout : 0, 0}; + timeout.tv_sec = currTimeout; + timeout.tv_usec = 0; /* setting to 0 microseconds */ sockfd = (int)wolfSSL_get_fd(ssl); FD_ZERO(&recvfds); diff --git a/psk/client-psk-resume.c b/psk/client-psk-resume.c index f26521b7..260b28ae 100644 --- a/psk/client-psk-resume.c +++ b/psk/client-psk-resume.c @@ -81,8 +81,8 @@ int main(int argc, char **argv){ /* create and initialize WOLFSSL_CTX structure */ if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { - fprintf(stderr, "SSL_CTX_new error.\n"); - return 1; + fprintf(stderr, "SSL_CTX_new error.\n"); + return 1; } /* create a stream socket using tcp,internet protocal IPv4, @@ -122,13 +122,13 @@ int main(int argc, char **argv){ /* takes inputting string and outputs it to the server */ if (wolfSSL_write(ssl, sendline, sizeof(sendline)) != sizeof(sendline)) { - printf("Write Error to Server\n"); - return 1; + printf("Write Error to Server\n"); + return 1; } /* flags if the Server stopped before the client could end */ if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) { - printf("Client: Server Terminated Prematurely!\n"); + printf("Client: Server Terminated Prematurely!\n"); return 1; } @@ -173,13 +173,13 @@ int main(int argc, char **argv){ } if (wolfSSL_write(sslResume, sendline, sizeof(sendline)) != sizeof(sendline)) { - printf("Write Error to Server\n"); - return 1; + printf("Write Error to Server\n"); + return 1; } /* flags if the Server stopped before the client could end */ if (wolfSSL_read(sslResume, recvline, MAXLINE) < 0 ) { - printf("Client: Server Terminated Prematurely!\n"); + printf("Client: Server Terminated Prematurely!\n"); return 1; } diff --git a/psk/client-psk.c b/psk/client-psk.c index 2bf108e7..1b0c6beb 100755 --- a/psk/client-psk.c +++ b/psk/client-psk.c @@ -96,7 +96,7 @@ int main(int argc, char **argv) if (ret != 1) { printf("inet_pton error\n"); - return 1; + return 1; } /* set up pre shared keys */ @@ -125,13 +125,13 @@ int main(int argc, char **argv) /* write string to the server */ if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) { - printf("Write Error to Server\n"); - return 1; + printf("Write Error to Server\n"); + return 1; } /* check if server ended before client could read a response */ if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) { - printf("Client: Server Terminated Prematurely!\n"); + printf("Client: Server Terminated Prematurely!\n"); return 1; } diff --git a/psk/server-psk-nonblocking.c b/psk/server-psk-nonblocking.c index 8116f8e3..5b829fdc 100644 --- a/psk/server-psk-nonblocking.c +++ b/psk/server-psk-nonblocking.c @@ -150,8 +150,8 @@ int main() } else { printf("Connection from %s, port %d\n", - inet_ntop(AF_INET, &cliAddr.sin_addr, buff, sizeof(buff)), - ntohs(cliAddr.sin_port)); + inet_ntop(AF_INET, &cliAddr.sin_addr, buff, sizeof(buff)), + ntohs(cliAddr.sin_port)); /* create WOLFSSL object */ if ((ssl = wolfSSL_new(ctx)) == NULL) { @@ -171,149 +171,148 @@ int main() ret = wolfSSL_accept(ssl); error = wolfSSL_get_error(ssl, 0); - /* clearing buffer for client reponse to prevent unexpected output*/ - memset(buf, 0, MAXLINE); - do { + /* clearing buffer for client reponse to prevent unexpected output*/ + memset(buf, 0, MAXLINE); + do { - while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || - error == SSL_ERROR_WANT_WRITE)) { + while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || + error == SSL_ERROR_WANT_WRITE)) { - /* print out for user notification */ - if (error == SSL_ERROR_WANT_READ) { - printf("... server would read block\n"); - } - else { - printf("... server would write block\n"); - } + /* print out for user notification */ + if (error == SSL_ERROR_WANT_READ) { + printf("... server would read block\n"); + } + else { + printf("... server would write block\n"); + } /* -------------------------------------------------------------------------- */ /* TCP */ /* -------------------------------------------------------------------------- */ - FD_ZERO(&recvfds); - FD_SET(sockfd, &recvfds); - FD_ZERO(&errfds); - FD_SET(sockfd, &errfds); - - nfds = sockfd + 1; - result = select(nfds, &recvfds, NULL, &errfds, &timeout); - - if (result == 0) { - select_ret = TEST_TIMEOUT; - } - else if (result > 0) { - if (FD_ISSET(sockfd, &recvfds)) { - select_ret = TEST_RECV_READY; + FD_ZERO(&recvfds); + FD_SET(sockfd, &recvfds); + FD_ZERO(&errfds); + FD_SET(sockfd, &errfds); + + nfds = sockfd + 1; + result = select(nfds, &recvfds, NULL, &errfds, &timeout); + + if (result == 0) { + select_ret = TEST_TIMEOUT; + } + else if (result > 0) { + if (FD_ISSET(sockfd, &recvfds)) { + select_ret = TEST_RECV_READY; + } + else if (FD_ISSET(sockfd, &errfds)) { + select_ret = TEST_ERROR_READY; + } + } + else { + select_ret = TEST_SELECT_FAIL; + } + + /* if tcp_select signals ready try to accept otherwise continue loop*/ + if ((select_ret == TEST_RECV_READY) || + (select_ret == TEST_ERROR_READY)) { + ret = wolfSSL_accept(ssl); + error = wolfSSL_get_error(ssl, 0); + } + else if (select_ret == TEST_TIMEOUT) { + error = SSL_ERROR_WANT_READ; + } + else { + error = SSL_FATAL_ERROR; + } } - else if (FD_ISSET(sockfd, &errfds)) { - select_ret = TEST_ERROR_READY; + /* faliure to accept */ + if (ret != SSL_SUCCESS) { + printf("Fatal error : SSL_accept failed\n"); + ret = SSL_FATAL_ERROR; + } + + if (ret != SSL_SUCCESS) { + return 1; + } + + n = wolfSSL_read(ssl, buf, MAXLINE); + if (n > 0) { + printf("%s\n", buf); } } - else { - select_ret = TEST_SELECT_FAIL; - } + while(n < 0); - /* if tcp_select signals ready try to accept otherwise continue loop*/ - if ((select_ret == TEST_RECV_READY) || - (select_ret == TEST_ERROR_READY)) { - ret = wolfSSL_accept(ssl); - error = wolfSSL_get_error(ssl, 0); - } - else if (select_ret == TEST_TIMEOUT) { - error = SSL_ERROR_WANT_READ; - } - else { - error = SSL_FATAL_ERROR; - } - } - /* faliure to accept */ - if (ret != SSL_SUCCESS) { - printf("Fatal error : SSL_accept failed\n"); - ret = SSL_FATAL_ERROR; - } - - if (ret != SSL_SUCCESS) { - return 1; - } + while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || + error == SSL_ERROR_WANT_WRITE)) { - n = wolfSSL_read(ssl, buf, MAXLINE); - if (n > 0) { - printf("%s\n", buf); - } - } - while(n < 0); - - while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || - error == SSL_ERROR_WANT_WRITE)) { - - /* print out for user notification */ - if (error == SSL_ERROR_WANT_READ) { - printf("... server would read block\n"); - } - else { - printf("... server would write block\n"); - } + /* print out for user notification */ + if (error == SSL_ERROR_WANT_READ) { + printf("... server would read block\n"); + } + else { + printf("... server would write block\n"); + } /* -------------------------------------------------------------------------- */ /* TCP */ /* -------------------------------------------------------------------------- */ - FD_ZERO(&recvfds); - FD_SET(sockfd, &recvfds); - FD_ZERO(&errfds); - FD_SET(sockfd, &errfds); + FD_ZERO(&recvfds); + FD_SET(sockfd, &recvfds); + FD_ZERO(&errfds); + FD_SET(sockfd, &errfds); - nfds = sockfd + 1; - result = select(nfds, &recvfds, NULL, &errfds, &timeout); + nfds = sockfd + 1; + result = select(nfds, &recvfds, NULL, &errfds, &timeout); - if (result == 0) { - select_ret = TEST_TIMEOUT; - } - else if (result > 0) { - if (FD_ISSET(sockfd, &recvfds)) { - select_ret = TEST_RECV_READY; - } - else if (FD_ISSET(sockfd, &errfds)) { - select_ret = TEST_ERROR_READY; - } - } - else { - select_ret = TEST_SELECT_FAIL; - } + if (result == 0) { + select_ret = TEST_TIMEOUT; + } + else if (result > 0) { + if (FD_ISSET(sockfd, &recvfds)) { + select_ret = TEST_RECV_READY; + } + else if (FD_ISSET(sockfd, &errfds)) { + select_ret = TEST_ERROR_READY; + } + } + else { + select_ret = TEST_SELECT_FAIL; + } /* if tcp_select signals ready try to accept otherwise continue loop*/ - if ((select_ret == TEST_RECV_READY) || - (select_ret == TEST_ERROR_READY)) { - ret = wolfSSL_accept(ssl); - error = wolfSSL_get_error(ssl, 0); - } - else if (select_ret == TEST_TIMEOUT) { - error = SSL_ERROR_WANT_READ; - } - else { - error = SSL_FATAL_ERROR; - } - } + if ((select_ret == TEST_RECV_READY) || + (select_ret == TEST_ERROR_READY)) { + ret = wolfSSL_accept(ssl); + error = wolfSSL_get_error(ssl, 0); + } + else if (select_ret == TEST_TIMEOUT) { + error = SSL_ERROR_WANT_READ; + } + else { + error = SSL_FATAL_ERROR; + } + } - /* faliure to accept */ - if (ret != SSL_SUCCESS) { - printf("Fatal error : SSL_accept failed\n"); - ret = SSL_FATAL_ERROR; - } + /* faliure to accept */ + if (ret != SSL_SUCCESS) { + printf("Fatal error : SSL_accept failed\n"); + ret = SSL_FATAL_ERROR; + } - if (ret != SSL_SUCCESS) { - return 1; - } - - if (wolfSSL_write(ssl, response, strlen(response)) != strlen(response)) { - printf("Fatal error : respond: write error\n"); - return 1; - } + if (ret != SSL_SUCCESS) { + return 1; + } + if ( wolfSSL_write(ssl, response, strlen(response)) != + strlen(response)) { + printf("Fatal error : respond: write error\n"); + return 1; + } - /* closes the connections after responding */ - wolfSSL_shutdown(ssl); - wolfSSL_free(ssl); if (close(connfd) == -1) { printf("Fatal error : close error\n"); return 1; } + wolfSSL_shutdown(ssl); + wolfSSL_free(ssl); } }