Merge pull request #54 from connerWolfSSL/master
Conner Becker PSK tutorial/examples updates and fixespull/55/head
commit
f0290e0ef2
|
@ -45,80 +45,6 @@ enum {
|
||||||
TEST_ERROR_READY
|
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.
|
*psk client set up.
|
||||||
*/
|
*/
|
||||||
|
@ -143,39 +69,18 @@ static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint,
|
||||||
return 4;
|
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 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_CTX* ctx;
|
||||||
WOLFSSL* ssl;
|
WOLFSSL* ssl;
|
||||||
struct sockaddr_in servaddr;;
|
struct timeval timeout;
|
||||||
|
struct sockaddr_in servaddr;
|
||||||
|
|
||||||
/* must include an ip address of this will flag */
|
/* must include an ip address of this will flag */
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
|
@ -245,25 +150,89 @@ int main(int argc, char **argv)
|
||||||
/* invokes the fcntl callable service to set file status flags.
|
/* 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 block an open, a read, or a write on the file
|
||||||
* (do not wait for terminal input. If an error occurs,
|
* (do not wait for terminal input. If an error occurs,
|
||||||
* stop program*/
|
* stop program */
|
||||||
flags = fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
|
flags = fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
|
||||||
if (flags < 0) {
|
if (flags < 0) {
|
||||||
printf("fcntl set failed\n");
|
printf("fcntl set failed\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* setting up and running nonblocking socket */
|
/* setting up and running nonblocking socket */
|
||||||
ret = NonBlockingSSL_Connect(ssl);
|
ret = wolfSSL_connect(ssl);
|
||||||
if (ret != 0) {
|
error = wolfSSL_get_error(ssl, 0);
|
||||||
return 1;
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout.tv_sec = currTimeout;
|
||||||
|
timeout.tv_usec = 0; /* setting to 0 microseconds */
|
||||||
|
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 */
|
/* takes inputting string and outputs it to the server */
|
||||||
ret = SendReceive(ssl);
|
/* write string to the server */
|
||||||
if (ret != 0) {
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* show message from the server */
|
||||||
|
printf("Server Message: %s\n", recvline);
|
||||||
|
|
||||||
/* cleanup */
|
/* cleanup */
|
||||||
wolfSSL_free(ssl);
|
wolfSSL_free(ssl);
|
||||||
|
|
||||||
|
|
|
@ -60,36 +60,11 @@ static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint,
|
||||||
return 4;
|
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 main(int argc, char **argv){
|
||||||
|
|
||||||
int sockfd, sock, ret;
|
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* ssl;
|
||||||
WOLFSSL* sslResume = 0;
|
WOLFSSL* sslResume = 0;
|
||||||
WOLFSSL_SESSION* session = 0;
|
WOLFSSL_SESSION* session = 0;
|
||||||
|
@ -106,8 +81,8 @@ int main(int argc, char **argv){
|
||||||
|
|
||||||
/* create and initialize WOLFSSL_CTX structure */
|
/* create and initialize WOLFSSL_CTX structure */
|
||||||
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
|
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
|
||||||
fprintf(stderr, "SSL_CTX_new error.\n");
|
fprintf(stderr, "SSL_CTX_new error.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* create a stream socket using tcp,internet protocal IPv4,
|
/* create a stream socket using tcp,internet protocal IPv4,
|
||||||
|
@ -146,7 +121,19 @@ int main(int argc, char **argv){
|
||||||
wolfSSL_set_fd(ssl, sockfd);
|
wolfSSL_set_fd(ssl, sockfd);
|
||||||
|
|
||||||
/* takes inputting string and outputs it to the server */
|
/* 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 */
|
/* Save the session ID to reuse */
|
||||||
session = wolfSSL_get_session(ssl);
|
session = wolfSSL_get_session(ssl);
|
||||||
|
@ -158,9 +145,8 @@ int main(int argc, char **argv){
|
||||||
/* close connection */
|
/* close connection */
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
|
|
||||||
/* cleanup without wolfSSL_Cleanup() for now */
|
/* cleanup without wolfSSL_Cleanup() and wolfSSL_CTX_free() for now */
|
||||||
wolfSSL_free(ssl);
|
wolfSSL_free(ssl);
|
||||||
wolfSSL_CTX_free(ctx);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* resume session, start new connection and socket
|
* resume session, start new connection and socket
|
||||||
|
@ -186,25 +172,30 @@ int main(int argc, char **argv){
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* takes inputting string and outputs it to the server */
|
if (wolfSSL_write(sslResume, sendline, sizeof(sendline)) != sizeof(sendline)) {
|
||||||
ret = SendReceive(sslResume);
|
printf("Write Error to Server\n");
|
||||||
if (ret != 0) {
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* show message from the server */
|
||||||
|
printf("Server Message: %s\n", recvline);
|
||||||
/* check to see if the session id is being reused */
|
/* check to see if the session id is being reused */
|
||||||
if (wolfSSL_session_reused(sslResume)) {
|
if (wolfSSL_session_reused(sslResume)) {
|
||||||
printf("reused session id\n");
|
printf("reused session id\n");
|
||||||
}
|
}
|
||||||
else
|
else{
|
||||||
printf("didn't reuse session id!!!\n");
|
printf("didn't reuse session id!!!\n");
|
||||||
|
}
|
||||||
/* shut down wolfSSL */
|
/* shut down wolfSSL */
|
||||||
wolfSSL_shutdown(sslResume);
|
wolfSSL_shutdown(sslResume);
|
||||||
|
|
||||||
/* shut down socket */
|
/* shut down socket */
|
||||||
close(sock);
|
close(sock);
|
||||||
|
|
||||||
/* clean up now with wolfSSL_Cleanup() */
|
/* clean up now with wolfSSL_Cleanup() */
|
||||||
wolfSSL_free(sslResume);
|
wolfSSL_free(sslResume);
|
||||||
wolfSSL_CTX_free(ctx);
|
wolfSSL_CTX_free(ctx);
|
||||||
|
|
|
@ -58,36 +58,11 @@ static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint,
|
||||||
return 4;
|
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 main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int ret, sockfd;
|
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* ssl;
|
||||||
WOLFSSL_CTX* ctx;
|
WOLFSSL_CTX* ctx;
|
||||||
struct sockaddr_in servaddr;;
|
struct sockaddr_in servaddr;;
|
||||||
|
@ -121,7 +96,7 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
if (ret != 1) {
|
if (ret != 1) {
|
||||||
printf("inet_pton error\n");
|
printf("inet_pton error\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set up pre shared keys */
|
/* set up pre shared keys */
|
||||||
|
@ -148,11 +123,20 @@ int main(int argc, char **argv)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* takes inputting string and outputs it to the server */
|
/* write string to the server */
|
||||||
ret = SendReceive(ssl);
|
if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) {
|
||||||
if (ret != 0) {
|
printf("Write Error to Server\n");
|
||||||
return 1;
|
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 */
|
/* cleanup */
|
||||||
wolfSSL_free(ssl);
|
wolfSSL_free(ssl);
|
||||||
|
|
|
@ -37,31 +37,12 @@
|
||||||
* this function will send the inputted string to the server and then
|
* this function will send the inputted string to the server and then
|
||||||
* recieve the string from the server outputing it to the termial
|
* 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 main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int sockfd, ret;
|
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;
|
struct sockaddr_in servaddr;
|
||||||
|
|
||||||
/* must include an ip address or this will flag */
|
/* 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 */
|
/* takes inputting string and outputs it to the server */
|
||||||
ret = SendReceive(sockfd);
|
/* write string to the server */
|
||||||
if (ret != 0){
|
if (write(sockfd, sendline, strlen(sendline)) != strlen(sendline)) {
|
||||||
printf("Send Recieve Error");
|
printf("Write Error to Server\n");
|
||||||
return 1;
|
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 socket and connection */
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
|
|
||||||
|
|
|
@ -47,127 +47,6 @@ enum{
|
||||||
TEST_ERROR_READY
|
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.
|
* 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 main()
|
||||||
{
|
{
|
||||||
int listenfd, connfd;
|
int n; /* length of string read */
|
||||||
int opt;
|
int ret;
|
||||||
struct sockaddr_in cliAddr, servAddr;
|
int error;
|
||||||
char buff[MAXLINE];
|
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;
|
socklen_t cliLen;
|
||||||
WOLFSSL_CTX* ctx;
|
WOLFSSL_CTX* ctx;
|
||||||
|
struct sockaddr_in cliAddr, servAddr;
|
||||||
|
struct timeval timeout = {currTimeout, 0};
|
||||||
|
|
||||||
wolfSSL_Init();
|
wolfSSL_Init();
|
||||||
|
|
||||||
|
@ -259,8 +150,8 @@ int main()
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
printf("Connection from %s, port %d\n",
|
printf("Connection from %s, port %d\n",
|
||||||
inet_ntop(AF_INET, &cliAddr.sin_addr, buff, sizeof(buff)),
|
inet_ntop(AF_INET, &cliAddr.sin_addr, buff, sizeof(buff)),
|
||||||
ntohs(cliAddr.sin_port));
|
ntohs(cliAddr.sin_port));
|
||||||
|
|
||||||
/* create WOLFSSL object */
|
/* create WOLFSSL object */
|
||||||
if ((ssl = wolfSSL_new(ctx)) == NULL) {
|
if ((ssl = wolfSSL_new(ctx)) == NULL) {
|
||||||
|
@ -268,27 +159,163 @@ int main()
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
wolfSSL_set_fd(ssl, connfd);
|
wolfSSL_set_fd(ssl, connfd);
|
||||||
|
sockfd = wolfSSL_get_fd(ssl);
|
||||||
|
|
||||||
/* set wolfSSL and socket to non blocking and respond */
|
/* set wolfSSL and socket to non blocking and respond */
|
||||||
wolfSSL_set_using_nonblock(ssl, 1);
|
wolfSSL_set_using_nonblock(ssl, 1);
|
||||||
if (fcntl(connfd, F_SETFL, O_NONBLOCK) < 0) {
|
if (fcntl(connfd, F_SETFL, O_NONBLOCK) < 0) {
|
||||||
printf("Fatal error : fcntl set failed\n");
|
printf("Fatal error : fcntl set failed\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (Respond(ssl) != 0) {
|
|
||||||
printf("Fatal error : respond error\n");
|
ret = wolfSSL_accept(ssl);
|
||||||
return 1;
|
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 */
|
/* closes the connections after responding */
|
||||||
wolfSSL_shutdown(ssl);
|
|
||||||
wolfSSL_free(ssl);
|
|
||||||
if (close(connfd) == -1) {
|
if (close(connfd) == -1) {
|
||||||
printf("Fatal error : close error\n");
|
printf("Fatal error : close error\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
wolfSSL_shutdown(ssl);
|
||||||
|
wolfSSL_free(ssl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* free up memory used by wolfssl */
|
/* free up memory used by wolfssl */
|
||||||
wolfSSL_CTX_free(ctx);
|
wolfSSL_CTX_free(ctx);
|
||||||
wolfSSL_Cleanup();
|
wolfSSL_Cleanup();
|
||||||
|
|
|
@ -36,31 +36,6 @@
|
||||||
#define LISTENQ 1024
|
#define LISTENQ 1024
|
||||||
#define SERV_PORT 11111
|
#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.
|
* 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 main()
|
||||||
{
|
{
|
||||||
|
int n; /* length of string read */
|
||||||
int listenfd, connfd;
|
int listenfd, connfd;
|
||||||
int opt;
|
int opt;
|
||||||
struct sockaddr_in cliAddr, servAddr;
|
|
||||||
char buff[MAXLINE];
|
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;
|
socklen_t cliLen;
|
||||||
WOLFSSL_CTX* ctx;
|
WOLFSSL_CTX* ctx;
|
||||||
|
|
||||||
|
@ -160,8 +138,25 @@ int main()
|
||||||
printf("Fatal error : wolfSSL_new error\n");
|
printf("Fatal error : wolfSSL_new error\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* sets the file descriptor of the socket for the ssl session */
|
||||||
wolfSSL_set_fd(ssl, connfd);
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,34 +42,17 @@ void err_sys(const char *err, ...)
|
||||||
printf("Fatal error : %s\n", 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 main()
|
||||||
{
|
{
|
||||||
int listenfd, connfd;
|
int listenfd, connfd;
|
||||||
int opt;
|
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;
|
struct sockaddr_in cliAddr, servAddr;
|
||||||
char buff[MAXLINE];
|
|
||||||
socklen_t cliLen;
|
socklen_t cliLen;
|
||||||
|
|
||||||
/* find a socket , 0 for using TCP option */
|
/* find a socket , 0 for using TCP option */
|
||||||
listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
@ -109,8 +92,19 @@ int main()
|
||||||
printf("Connection from %s, port %d\n",
|
printf("Connection from %s, port %d\n",
|
||||||
inet_ntop(AF_INET, &cliAddr.sin_addr, buff, sizeof(buff)),
|
inet_ntop(AF_INET, &cliAddr.sin_addr, buff, sizeof(buff)),
|
||||||
ntohs(cliAddr.sin_port));
|
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 */
|
/* closes the connections after responding */
|
||||||
if (close(connfd) == -1) {
|
if (close(connfd) == -1) {
|
||||||
err_sys("close error");
|
err_sys("close error");
|
||||||
|
|
Loading…
Reference in New Issue