Merge pull request #55 from abrahamsonn/dtls-branch

Dtls branch
pull/57/head^2
Chris Conlon 2017-06-02 16:35:15 -06:00 committed by GitHub
commit c15ac5e8a3
10 changed files with 954 additions and 535 deletions

View File

@ -31,7 +31,6 @@ server-dtls-nonblocking: server-dtls-nonblocking.o
server-dtls-threaded: server-dtls-threaded.o
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
.PHONY: clean all
clean:

View File

@ -48,119 +48,29 @@ enum {
TEST_ERROR_READY
};
/* Tcp select using dtls nonblocking function */
static int dtls_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;
}
/* Connect using Nonblocking - DTLS version */
static void NonBlockingDTLS_Connect (WOLFSSL* ssl)
{
int ret = wolfSSL_connect(ssl);
int error = wolfSSL_get_error(ssl, 0);
int sockfd = (int)wolfSSL_get_fd(ssl);
int select_ret;
while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ ||
error == SSL_ERROR_WANT_WRITE)) {
int currTimeout = 1;
if (error == SSL_ERROR_WANT_READ) {
printf("... client would read block\n");
}
else {
printf("... client would write block\n");
}
currTimeout = wolfSSL_dtls_get_current_timeout(ssl);
select_ret = dtls_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 && !wolfSSL_dtls(ssl)) {
error = 2;
}
else if (select_ret == TEST_TIMEOUT && wolfSSL_dtls(ssl) &&
wolfSSL_dtls_got_timeout(ssl) >= 0) {
error = 2;
}
else {
error = SSL_FATAL_ERROR;
}
}
if (ret != SSL_SUCCESS) {
printf("SSL_connect failed with");
}
}
/* Main send and receive function */
void DatagramClient (WOLFSSL* ssl)
{
int n = 0;
char sendLine[MAXLINE], recvLine[MAXLINE - 1];
while (fgets(sendLine, MAXLINE, stdin) != NULL) {
while ((wolfSSL_write(ssl, sendLine, strlen(sendLine))) !=
strlen(sendLine)) {
printf("SSL_write failed");
}
while ((n = wolfSSL_read(ssl, recvLine, sizeof(recvLine)-1)) <= 0) {
int readErr = wolfSSL_get_error(ssl, 0);
if(readErr != SSL_ERROR_WANT_READ) {
printf("wolfSSL_read failed");
}
}
recvLine[n] = '\0';
fputs(recvLine, stdout);
}
}
int main (int argc, char** argv)
{
int sockfd = 0;
struct sockaddr_in servAddr;
const char* host = argv[1];
WOLFSSL* ssl = 0;
WOLFSSL_CTX* ctx = 0;
WOLFSSL* sslResume = 0;
WOLFSSL_SESSION* session = 0;
char cert_array[] = "../certs/ca-cert.pem";
char* certs = cert_array;
char* srTest = "testing session resume";
/* standard variables used in a dtls client*/
int sockfd = 0;
struct sockaddr_in servAddr;
const char* host = argv[1];
WOLFSSL* ssl = 0;
WOLFSSL_CTX* ctx = 0;
char cert_array[] = "../certs/ca-cert.pem";
char* certs = cert_array;
/* variables used for non-blocking DTLS connect */
int ret = wolfSSL_connect(ssl);
int error = wolfSSL_get_error(ssl, 0);
int nb_sockfd = (int) wolfSSL_get_fd(ssl);
int select_ret;
int currTimeout;
int nfds;
int result;
fd_set recvfds, errfds;
struct timeval timeout;
if (argc != 2) {
printf("usage: udpcli <IP address>\n");
printf("usage: udpcli <IP address>\n");
return 1;
}
@ -169,18 +79,18 @@ int main (int argc, char** argv)
/* wolfSSL_Debugging_ON();*/
if ( (ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method())) == NULL) {
fprintf(stderr, "wolfSSL_CTX_new error.\n");
return(EXIT_FAILURE);
fprintf(stderr, "wolfSSL_CTX_new error.\n");
return(EXIT_FAILURE);
}
if (wolfSSL_CTX_load_verify_locations(ctx,certs, 0) != SSL_SUCCESS) {
fprintf(stderr, "Error loading %s, please check the file.\n", certs);
return(EXIT_FAILURE);
fprintf(stderr, "Error loading %s, please check the file.\n", certs);
return(EXIT_FAILURE);
}
ssl = wolfSSL_new(ctx);
if (ssl == NULL) {
printf("unable to get ssl object");
printf("unable to get ssl object");
return 1;
}
@ -202,63 +112,105 @@ int main (int argc, char** argv)
wolfSSL_set_fd(ssl, sockfd);
wolfSSL_set_using_nonblock(ssl, 1);
fcntl(sockfd, F_SETFL, O_NONBLOCK);
NonBlockingDTLS_Connect(ssl);
DatagramClient(ssl);
while ( (wolfSSL_write(ssl, srTest, sizeof(srTest))) != sizeof(srTest)) {
printf("failed to write");
return 1;
/*****************************************************************************/
/* Non-blocking code for DTLS connect */
while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ ||
error == SSL_ERROR_WANT_WRITE)) {
/* Variables that will reset upon every iteration */
currTimeout = wolfSSL_dtls_get_current_timeout(ssl);
nfds = nb_sockfd + 1;
timeout = (struct timeval) { (currTimeout > 0) ? currTimeout : 0, 0};
if (error == SSL_ERROR_WANT_READ) {
printf("... client would read block\n");
}
else {
printf("... client would write block\n");
}
/* Tcp select using dtls nonblocking functionality */
FD_ZERO(&recvfds);
FD_SET(nb_sockfd, &recvfds);
FD_ZERO(&errfds);
FD_SET(nb_sockfd, &errfds);
result = select(nfds, &recvfds, NULL, &errfds, &timeout);
select_ret = TEST_SELECT_FAIL;
if (result == 0) {
select_ret = TEST_TIMEOUT;
}
else if (result > 0) {
if (FD_ISSET(nb_sockfd, &recvfds)) {
select_ret = TEST_RECV_READY;
}
else if (FD_ISSET(nb_sockfd, &errfds)) {
select_ret = TEST_ERROR_READY;
}
}
/* End "Tcp select ..." code */
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 && !wolfSSL_dtls(ssl)) {
error = 2;
}
else if (select_ret == TEST_TIMEOUT && wolfSSL_dtls(ssl) &&
wolfSSL_dtls_got_timeout(ssl) >= 0) {
error = 2;
}
else {
error = SSL_FATAL_ERROR;
}
}
session = wolfSSL_get_session(ssl);
sslResume = wolfSSL_new(ctx);
if (ret != SSL_SUCCESS) {
printf("SSL_connect failed with");
}
/* */
/*****************************************************************************/
/*****************************************************************************/
/* Code for sending datagram to server */
int n = 0;
char sendLine[MAXLINE], recvLine[MAXLINE - 1];
while (fgets(sendLine, MAXLINE, stdin) != NULL) {
while ( ( wolfSSL_write(ssl, sendLine, strlen(sendLine))) !=
strlen(sendLine)) {
printf("SSL_write failed");
}
while ( (n = wolfSSL_read(ssl, recvLine, sizeof(recvLine)-1)) <= 0) {
int readErr = wolfSSL_get_error(ssl, 0);
if(readErr != SSL_ERROR_WANT_READ) {
printf("wolfSSL_read failed");
}
}
recvLine[n] = '\0';
fputs(recvLine, stdout);
}
/* */
/*****************************************************************************/
wolfSSL_shutdown(ssl);
wolfSSL_free(ssl);
close(sockfd);
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(SERV_PORT);
if (inet_pton(AF_INET, host, &servAddr.sin_addr) < 1) {
printf("Error and/or invalid IP address");
return 1;
}
wolfSSL_dtls_set_peer(sslResume, &servAddr, sizeof(servAddr));
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("cannot create a socket.");
return 1;
}
wolfSSL_set_fd(sslResume, sockfd);
wolfSSL_set_session(sslResume, session);
wolfSSL_set_using_nonblock(sslResume, 1);
fcntl(sockfd, F_SETFL, O_NONBLOCK);
NonBlockingDTLS_Connect(sslResume);
if (wolfSSL_session_reused(sslResume)) {
printf("reused session id\n");
}
else {
printf("didn't reuse session id!!!\n");
}
DatagramClient(sslResume);
while ((wolfSSL_write(sslResume, srTest, sizeof(srTest))) != sizeof(srTest))
{
printf("failed to write");
return 1;
}
sleep(1);
wolfSSL_shutdown(sslResume);
wolfSSL_free(sslResume);
close(sockfd);
wolfSSL_CTX_free(ctx);
wolfSSL_Cleanup();
return 0;
}

View File

@ -39,50 +39,35 @@
#define MAXLINE 4096
#define SERV_PORT 11111
/* Send and receive function */
void DatagramClient (WOLFSSL* ssl)
{
int n = 0;
char sendLine[MAXLINE], recvLine[MAXLINE - 1];
while (fgets(sendLine, MAXLINE, stdin) != NULL) {
if ( (wolfSSL_write(ssl, sendLine, strlen(sendLine))) !=
strlen(sendLine)) {
printf("SSL_write failed");
}
n = wolfSSL_read(ssl, recvLine, sizeof(recvLine)-1);
if (n < 0) {
int readErr = wolfSSL_get_error(ssl, 0);
if(readErr != SSL_ERROR_WANT_READ)
printf("wolfSSL_read failed");
}
recvLine[n] = '\0';
fputs(recvLine, stdout);
}
}
int main (int argc, char** argv)
{
int sockfd = 0;
struct sockaddr_in servAddr;
const char* host = argv[1];
WOLFSSL* ssl = 0;
WOLFSSL_CTX* ctx = 0;
WOLFSSL* sslResume = 0;
WOLFSSL_SESSION* session = 0;
char* srTest = "testing session resume";
char cert_array[] = "../certs/ca-cert.pem";
char* certs = cert_array;
/* standard variables used in a dtls client*/
int sockfd = 0;
int err1;
int readErr;
struct sockaddr_in servAddr;
const char* host = argv[1];
WOLFSSL* ssl = 0;
WOLFSSL_CTX* ctx = 0;
WOLFSSL* sslResume = 0;
WOLFSSL_SESSION* session = 0;
char* srTest = "testing session resume";
char cert_array[] = "../certs/ca-cert.pem";
char buffer[80];
char* certs = cert_array;
/* variables used in a dtls client for session reuse*/
int recvlen;
char sendLine[MAXLINE];
char recvLine[MAXLINE - 1];
if (argc != 2) {
printf("usage: udpcli <IP address>\n");
return 1;
}
wolfSSL_Init();
/* Un-comment the following line to enable debugging */
/* wolfSSL_Debugging_ON(); */
if ( (ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method())) == NULL) {
@ -118,22 +103,53 @@ int main (int argc, char** argv)
wolfSSL_set_fd(ssl, sockfd);
if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
int err1 = wolfSSL_get_error(ssl, 0);
char buffer[80];
printf("err = %d, %s\n", err1, wolfSSL_ERR_error_string(err1, buffer));
printf("SSL_connect failed");
err1 = wolfSSL_get_error(ssl, 0);
memset(buffer, 0, 80);
printf("err = %d, %s\n", err1, wolfSSL_ERR_error_string(err1, buffer));
printf("SSL_connect failed");
return 1;
}
DatagramClient(ssl);
/*****************************************************************************/
/* Code for sending datagram to server */
/* Loop while the user gives input or until an EOF is read */
while( fgets(sendLine, MAXLINE, stdin) != NULL ) {
/* Attempt to send sendLine to the server */
if ( ( wolfSSL_write(ssl, sendLine, strlen(sendLine))) !=
strlen(sendLine) ) {
printf("Error: wolfSSL_write failed.\n");
}
/* Attempt to read a message from server and store it in recvLine */
recvlen = wolfSSL_read(ssl, recvLine, sizeof(recvLine) - 1);
/* Error checking wolfSSL_read */
if (recvlen < 0) {
readErr = wolfSSL_get_error(ssl, 0);
if (readErr != SSL_ERROR_WANT_READ) {
printf("Error: wolfSSL_read failed.\n");
}
}
recvLine[recvlen] = '\0';
fputs(recvLine, stdout);
}
/* */
/*****************************************************************************/
/* Keep track of the old session information */
wolfSSL_write(ssl, srTest, sizeof(srTest));
session = wolfSSL_get_session(ssl);
sslResume = wolfSSL_new(ctx);
/* Cleanup the memory used by the old session & ssl object */
wolfSSL_shutdown(ssl);
wolfSSL_free(ssl);
close(sockfd);
/* Perform setup with new variables/old session information */
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(SERV_PORT);
@ -150,10 +166,17 @@ int main (int argc, char** argv)
}
wolfSSL_set_fd(sslResume, sockfd);
/* New method call - specifies to the WOLFSSL object to use the *
* given WOLFSSL_SESSION object */
wolfSSL_set_session(sslResume, session);
wolfSSL_set_fd(sslResume, sockfd);
if (wolfSSL_connect(sslResume) != SSL_SUCCESS) {
printf("SSL_connect failed");
err1 = wolfSSL_get_error(sslResume, 0);
memset(buffer, 0, 80);
printf("err = %d, %s\n", err1, wolfSSL_ERR_error_string(err1, buffer));
printf("SSL_connect failed on session reuse\n");
return 1;
}
@ -164,10 +187,42 @@ int main (int argc, char** argv)
printf("didn't reuse session id!!!\n");
}
DatagramClient(sslResume);
/*****************************************************************************/
/* Code for sending datagram to server */
/* Clear out variables for reuse */
recvlen = 0;
memset(sendLine, 0, MAXLINE);
memset(recvLine, 0, MAXLINE - 1);
/* Loop while the user gives input or until an EOF is read */
while( fgets(sendLine, MAXLINE, stdin) != NULL ) {
/* Attempt to send sendLine to the server */
if ( ( wolfSSL_write(ssl, sendLine, strlen(sendLine))) !=
strlen(sendLine) ) {
printf("Error: wolfSSL_write failed.\n");
}
/* Attempt to read a message from server and store it in recvLine */
recvlen = wolfSSL_read(ssl, recvLine, sizeof(recvLine) - 1);
/* Error checking wolfSSL_read */
if (recvlen < 0) {
readErr = wolfSSL_get_error(ssl, 0);
if (readErr != SSL_ERROR_WANT_READ) {
printf("Error: wolfSSL_read failed.\n");
}
}
recvLine[recvlen] = '\0';
fputs(recvLine, stdout);
}
/* */
/*****************************************************************************/
wolfSSL_write(sslResume, srTest, sizeof(srTest));
/* Cleanup memory used for storing the session information */
wolfSSL_shutdown(sslResume);
wolfSSL_free(sslResume);
@ -177,4 +232,3 @@ int main (int argc, char** argv)
return 0;
}

View File

@ -39,48 +39,30 @@
#define MAXLINE 4096
#define SERV_PORT 11111
/* Send and receive function */
void DatagramClient (WOLFSSL* ssl)
{
int n = 0;
char sendLine[MAXLINE], recvLine[MAXLINE - 1];
while (fgets(sendLine, MAXLINE, stdin) != NULL) {
if ( ( wolfSSL_write(ssl, sendLine, strlen(sendLine))) !=
strlen(sendLine)) {
printf("SSL_write failed");
}
n = wolfSSL_read(ssl, recvLine, sizeof(recvLine)-1);
if (n < 0) {
int readErr = wolfSSL_get_error(ssl, 0);
if(readErr != SSL_ERROR_WANT_READ) {
printf("wolfSSL_read failed");
}
}
recvLine[n] = '\0';
fputs(recvLine, stdout);
}
}
int main (int argc, char** argv)
{
int sockfd = 0;
struct sockaddr_in servAddr;
WOLFSSL* ssl = 0;
WOLFSSL_CTX* ctx = 0;
char cert_array[] = "../certs/ca-cert.pem";
char* certs = cert_array;
/* standard variables used in a dtls client*/
int n = 0;
int sockfd = 0;
int err1;
int readErr;
struct sockaddr_in servAddr;
WOLFSSL* ssl = 0;
WOLFSSL_CTX* ctx = 0;
char cert_array[] = "../certs/ca-cert.pem";
char* certs = cert_array;
char sendLine[MAXLINE];
char recvLine[MAXLINE - 1];
/* Program argument checking */
if (argc != 2) {
printf("usage: udpcli <IP address>\n");
return 1;
}
/* Initialize wolfSSL before assigning ctx */
wolfSSL_Init();
/* wolfSSL_Debugging_ON(); */
if ( (ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method())) == NULL) {
@ -88,18 +70,21 @@ int main (int argc, char** argv)
return 1;
}
/* Load certificates into ctx variable */
if (wolfSSL_CTX_load_verify_locations(ctx, certs, 0)
!= SSL_SUCCESS) {
fprintf(stderr, "Error loading %s, please check the file.\n", certs);
return 1;
}
/* Assign ssl variable */
ssl = wolfSSL_new(ctx);
if (ssl == NULL) {
printf("unable to get ssl object");
printf("unable to get ssl object");
return 1;
}
/* servAddr setup */
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(SERV_PORT);
@ -114,16 +99,45 @@ int main (int argc, char** argv)
printf("cannot create a socket.");
return 1;
}
/* Set the file descriptor for ssl and connect with ssl variable */
wolfSSL_set_fd(ssl, sockfd);
if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
int err1 = wolfSSL_get_error(ssl, 0);
err1 = wolfSSL_get_error(ssl, 0);
printf("err = %d, %s\n", err1, wolfSSL_ERR_reason_error_string(err1));
printf("SSL_connect failed");
return 1;
}
DatagramClient(ssl);
/*****************************************************************************/
/* Code for sending datagram to server */
/* Loop until the user is finished */
while (fgets(sendLine, MAXLINE, stdin) != NULL) {
/* Send sendLine to the server */
if ( ( wolfSSL_write(ssl, sendLine, strlen(sendLine)))
!= strlen(sendLine)) {
printf("SSL_write failed");
}
/* n is the # of bytes received */
n = wolfSSL_read(ssl, recvLine, sizeof(recvLine)-1);
if (n < 0) {
readErr = wolfSSL_get_error(ssl, 0);
if (readErr != SSL_ERROR_WANT_READ) {
printf("wolfSSL_read failed");
}
}
/* Add a terminating character to the generic server message */
recvLine[n] = '\0';
fputs(recvLine, stdout);
}
/* End code for sending datagram to server */
/*****************************************************************************/
/* Housekeeping */
wolfSSL_shutdown(ssl);
wolfSSL_free(ssl);
close(sockfd);

View File

@ -35,36 +35,16 @@
#define MAXLINE 4096
#define SERV_PORT 11111
/* send and recieve message function */
void DatagramClient (FILE* clientInput, int sockfd,
const struct sockaddr* servAddr, socklen_t servLen)
{
int n;
char sendLine[MAXLINE], recvLine[MAXLINE +1];
while (fgets(sendLine, MAXLINE, clientInput) != NULL) {
if ( ( sendto(sockfd, sendLine, strlen(sendLine) - 1, 0, servAddr,
servLen)) == -1) {
printf("error in sending");
}
if ( (n = recvfrom(sockfd, recvLine, MAXLINE, 0, NULL, NULL)) == -1) {
printf("Error in receiving");
}
recvLine[n] = 0;
fputs(recvLine, stdout);
}
}
int main(int argc, char** argv)
{
int sockfd;
struct sockaddr_in servAddr;
/* standard variables used in a udp client */
int sockfd;
int recvlen;
struct sockaddr_in servAddr;
const struct sockaddr* servAddr_in;
socklen_t servLen;
char sendLine[MAXLINE];
char recvLine[MAXLINE + 1];
if (argc != 2) {
printf("usage: udpcli <IP address>\n");
@ -82,9 +62,31 @@ int main(int argc, char** argv)
servAddr.sin_port = htons(SERV_PORT);
inet_pton(AF_INET, argv[1], &servAddr.sin_addr);
DatagramClient(stdin, sockfd, (struct sockaddr*) &servAddr,
sizeof(servAddr));
/****************************************************************************/
/* Code for sending the datagram to the server */
servAddr_in = &servAddr;
servLen = sizeof(servAddr);
/* Loop while user is giving input or until EOF is read */
while (fgets(sendLine, MAXLINE, stdin) != NULL) {
/* Attempt to send sendLine to the server */
if ( ( sendto(sockfd, sendLine, strlen(sendLine) - 1, 0, servAddr_in,
servLen)) == -1) {
printf("Error in sending.\n");
}
/* Attempt to receive recvLine from the server */
if ( (recvlen = recvfrom(sockfd, recvLine, MAXLINE, 0, NULL, NULL))
== -1) {
printf("Error in receiving.\n");
}
recvLine[recvlen] = '\0';
fputs(recvLine, stdout);
}
/* */
/****************************************************************************/
return 0;
}

View File

@ -43,6 +43,8 @@
static int cleanup; /* To handle shutdown */
void sig_handler(const int sig);
/* costumes for select_ret to wear */
enum {
TEST_SELECT_FAIL,
@ -51,6 +53,14 @@ enum {
TEST_ERROR_READY
};
/* For handling ^C interrupts passed by the user */
void sig_handler(const int sig)
{
printf("\nSIGINT %d handled\n", sig);
cleanup = 1;
return;
}
int main(int argc, char** argv)
{
/* cont short for "continue?", Loc short for "location" */
@ -88,6 +98,13 @@ int main(int argc, char** argv)
struct sockaddr_in cliAddr;
socklen_t clilen;
/* Code for handling signals */
struct sigaction act, oact;
act.sa_handler = sig_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, &oact);
/* "./config --enable-debug" and uncomment next line for debugging */
/* wolfSSL_Debugging_ON(); */

View File

@ -41,10 +41,13 @@
#define SERV_PORT 11111 /* define our server port number */
#define MSGLEN 4096
WOLFSSL_CTX* ctx; /* must be global for ThreadControl */
static int cleanup; /* To handle shutdown */
struct sockaddr_in cliAddr; /* the client's address */
struct sockaddr_in servAddr; /* our server's address */
static WOLFSSL_CTX* ctx; /* global for ThreadControl*/
static int cleanup; /* To handle shutdown */
static struct sockaddr_in cliAddr; /* the client's address */
static struct sockaddr_in servAddr; /* our server's address */
void sig_handler(const int sig);
void* ThreadControl(void*);
typedef struct {
int activefd;
@ -52,6 +55,13 @@ typedef struct {
unsigned char b[MSGLEN];
} threadArgs;
void sig_handler(const int sig)
{
printf("\nSIGINT %d handled\n", sig);
cleanup = 1;
return;
}
void* ThreadControl(void* openSock)
{
pthread_detach(pthread_self());
@ -135,6 +145,16 @@ int main(int argc, char** argv)
socklen_t cliLen;
socklen_t len = sizeof(on);
unsigned char buf[MSGLEN]; /* watch for incoming messages */
/* variables needed for threading */
threadArgs* args;
pthread_t threadid;
/* Code for handling signals */
struct sigaction act, oact;
act.sa_handler = sig_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, &oact);
/* "./config --enable-debug" and uncomment next line for debugging */
/* wolfSSL_Debugging_ON(); */
@ -202,7 +222,8 @@ int main(int argc, char** argv)
while (cleanup != 1) {
threadArgs* args;
memset(&threadid, 0, sizeof(threadid));
args = (threadArgs *) malloc(sizeof(threadArgs));
cliLen = sizeof(cliAddr);
@ -211,9 +232,15 @@ int main(int argc, char** argv)
* read any real message to struct and pass struct into thread
* for processing.
*/
bytesRcvd = (int)recvfrom(listenfd, (char *)buf, sizeof(buf), 0,
(struct sockaddr*)&cliAddr, &cliLen);
if (cleanup == 1) {
free(args);
return 1;
}
if (bytesRcvd < 0) {
printf("No clients in que, enter idle state\n");
continue;
@ -221,6 +248,7 @@ int main(int argc, char** argv)
else if (bytesRcvd > 0) {
/* put all the bytes from buf into args */
memcpy(args->b, buf, sizeof(buf));
args->size = bytesRcvd;
@ -231,21 +259,23 @@ int main(int argc, char** argv)
}
res = setsockopt(args->activefd, SOL_SOCKET, SO_REUSEADDR, &on,
len);
len);
if (res < 0) {
printf("Setsockopt SO_REUSEADDR failed.\n");
cleanup = 1;
return 1;
}
#ifdef SO_REUSEPORT
res = setsockopt(args->activefd, SOL_SOCKET,
SO_REUSEPORT, &on, len);
if (res < 0) {
printf("Setsockopt SO_REUSEPORT failed.\n");
cleanup = 1;
return 1;
}
#endif
#ifdef SO_REUSEPORT
res = setsockopt(args->activefd, SOL_SOCKET, SO_REUSEPORT, &on,
len);
if (res < 0) {
printf("Setsockopt SO_REUSEPORT failed.\n");
cleanup = 1;
return 1;
}
#endif
if (connect(args->activefd, (const struct sockaddr *)&cliAddr,
sizeof(cliAddr)) != 0) {
@ -255,18 +285,27 @@ int main(int argc, char** argv)
}
}
else {
/* else bytesRcvd = 0 */
printf("Recvfrom failed.\n");
cleanup = 1;
return 1;
}
printf("Connected!\n");
pthread_t threadid;
/* SPIN A THREAD HERE TO HANDLE "buff" and "reply/ack" */
pthread_create(&threadid, NULL, ThreadControl, args);
printf("control passed to thread control.\n");
}
if (cleanup != 1) {
/* SPIN A THREAD HERE TO HANDLE "buff" and "reply/ack" */
pthread_create(&threadid, NULL, ThreadControl, args);
printf("control passed to ThreadControl.\n");
}
else if (cleanup == 1) {
return 1;
} else {
printf("I don't know what to tell ya man\n");
}
/* clear servAddr each loop */
memset((char *)&servAddr, 0, sizeof(servAddr));
}
if (cont == 1) {
wolfSSL_CTX_free(ctx);

View File

@ -44,6 +44,15 @@ static int cleanup; /* To handle shutdown */
struct sockaddr_in servAddr; /* our server's address */
struct sockaddr_in cliaddr; /* the client's address */
void sig_handler(const int sig);
void sig_handler(const int sig)
{
printf("\nSIGINT %d handled\n", sig);
cleanup = 1;
return;
}
int main(int argc, char** argv)
{
/* cont short for "continue?", Loc short for "location" */
@ -60,10 +69,17 @@ int main(int argc, char** argv)
int listenfd = 0; /* Initialize our socket */
WOLFSSL* ssl = NULL;
socklen_t cliLen;
socklen_t len = sizeof(on);
socklen_t len = sizeof(int);
unsigned char b[MSGLEN]; /* watch for incoming messages */
char buff[MSGLEN]; /* the incoming message */
char ack[] = "I hear you fashizzle!\n";
/* Code for handling signals */
struct sigaction act, oact;
act.sa_handler = sig_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, &oact);
/* "./config --enable-debug" and uncomment next line for debugging */
/* wolfSSL_Debugging_ON(); */
@ -97,6 +113,7 @@ int main(int argc, char** argv)
/* Await Datagram */
while (cleanup != 1) {
/* Create a UDP/IP socket */
if ((listenfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
printf("Cannot create socket.\n");
@ -122,8 +139,7 @@ int main(int argc, char** argv)
}
/*Bind Socket*/
if (bind(listenfd,
(struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
if (bind(listenfd, (struct sockaddr*)&servAddr, sizeof(servAddr)) < 0) {
printf("Bind failed.\n");
cleanup = 1;
cont = 1;
@ -204,6 +220,12 @@ int main(int argc, char** argv)
printf("Client left cont to idle state\n");
cont = 0;
}
/* With the "continue" keywords, it is possible for the loop to exit *
* without changing the value of cont */
if (cleanup == 1) {
cont = 1;
}
if (cont == 1) {
wolfSSL_CTX_free(ctx);
@ -212,3 +234,4 @@ int main(int argc, char** argv)
return 0;
}

View File

@ -35,7 +35,7 @@
#define SERV_PORT 11111 /* define our server port number */
#define MSGLEN 4096 /* limit incoming message size */
int main (int argc, char** argv)
int main (void)
{
int sockfd; /* Initialize our socket */
int recvLen; /* number of bytes recieved */

File diff suppressed because it is too large Load Diff