no memory leaks, complied clean, ready for publishing

pull/1/head
Kaleb Himes 2014-05-29 23:18:32 -06:00
parent 628494a7cd
commit cde22ac2bb
1 changed files with 21 additions and 21 deletions

View File

@ -40,7 +40,7 @@
#define MSGLEN 4096 #define MSGLEN 4096
static int cleanup; /* To handle shutdown */ static int cleanup; /* To handle shutdown */
void AwaitDGram(); /* Separate out Handling Datagrams */ void AwaitDGram(); /* Separate out Handling Datagrams */
struct sockaddr_in servaddr; /* our server's address */ struct sockaddr_in servaddr; /* our server's address */
struct sockaddr_in cliaddr; /* the client's address */ struct sockaddr_in cliaddr; /* the client's address */
CYASSL_CTX* ctx; CYASSL_CTX* ctx;
@ -64,13 +64,14 @@ void AwaitDGram()
while (cleanup != 1) { while (cleanup != 1) {
/* Create a UDP/IP socket */
if ( (listenfd = socket(AF_INET, SOCK_DGRAM, 0) ) < 0 ) { if ( (listenfd = socket(AF_INET, SOCK_DGRAM, 0) ) < 0 ) {
printf("Cannot create socket.\n"); printf("Cannot create socket.\n");
cleanup = 1; cleanup = 1;
} }
printf("Socket allocated\n"); printf("Socket allocated\n");
/* INADDR_ANY=IPaddr, socket = 11111, modify SERV_PORT to change */ /* clear servaddr each loop */
memset((char *)&servaddr, 0, sizeof(servaddr)); memset((char *)&servaddr, 0, sizeof(servaddr));
/* host-to-network-long conversion (htonl) */ /* host-to-network-long conversion (htonl) */
@ -92,15 +93,16 @@ void AwaitDGram()
/*Bind Socket*/ /*Bind Socket*/
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { if (bind(listenfd,
(struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
printf("Bind failed.\n"); printf("Bind failed.\n");
cleanup = 1; cleanup = 1;
} }
printf("Awaiting client connection on port %d\n", SERV_PORT); printf("Awaiting client connection on port %d\n", SERV_PORT);
CYASSL* ssl; /* initialize arg */ /* set clilen to |cliaddr| */
clilen = sizeof(cliaddr); /* set clilen to |cliaddr| */ clilen = sizeof(cliaddr);
unsigned char b[1500]; unsigned char b[1500];
int connfd = 0; int connfd = 0;
@ -108,7 +110,7 @@ void AwaitDGram()
connfd = (int)recvfrom(listenfd, (char *)&b, sizeof(b), MSG_PEEK, connfd = (int)recvfrom(listenfd, (char *)&b, sizeof(b), MSG_PEEK,
(struct sockaddr*)&cliaddr, &clilen); (struct sockaddr*)&cliaddr, &clilen);
if (connfd < 0){ if (connfd < 0) {
printf("No clients in que, enter idle state\n"); printf("No clients in que, enter idle state\n");
continue; continue;
} }
@ -121,13 +123,13 @@ void AwaitDGram()
} }
} }
else { else {
printf("Recvfrom failed.\n"); printf("Recvfrom failed.\n");
cleanup = 1; cleanup = 1;
} }
printf("Connected!\n"); printf("Connected!\n");
/* initialize arg */
CYASSL* ssl;
/* Create the CYASSL Object */ /* Create the CYASSL Object */
if (( ssl = CyaSSL_new(ctx) ) == NULL) { if (( ssl = CyaSSL_new(ctx) ) == NULL) {
@ -143,14 +145,15 @@ void AwaitDGram()
if (CyaSSL_accept(ssl) != SSL_SUCCESS) { if (CyaSSL_accept(ssl) != SSL_SUCCESS) {
int err = CyaSSL_get_error(ssl, 0); int err = CyaSSL_get_error(ssl, 0);
char buffer[80]; char buffer[80];
printf("error = %d, %s\n", err, CyaSSL_ERR_error_string(err, buffer)); printf("error = %d, %s\n", err,
CyaSSL_ERR_error_string(err, buffer));
buffer[80]= 0; buffer[80]= 0;
printf("SSL_accept failed.\n"); printf("SSL_accept failed.\n");
cleanup = 1; cleanup = 1;
} }
if (( recvlen = CyaSSL_read(ssl, buff, sizeof(buff)-1)) > 0){ if (( recvlen = CyaSSL_read(ssl, buff, sizeof(buff)-1)) > 0) {
printf("heard %d bytes\n", recvlen); printf("heard %d bytes\n", recvlen);
@ -184,23 +187,20 @@ void AwaitDGram()
CyaSSL_free(ssl); CyaSSL_free(ssl);
printf("Client left return to idle state\n"); printf("Client left return to idle state\n");
continue;
} }
} }
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
/* CREATE THE SOCKET */ /* structures for signal handling */
struct sigaction act, oact;
struct sigaction act, oact; /* structures for signal handling */
/* /*
* Define a signal handler for when the user closes the program * Define a signal handler for when the user closes the program
* with Ctrl-C. Also, turn off SA_RESTART so that the OS doesn't * with Ctrl-C. Also, turn off SA_RESTART so that the OS doesn't
* restart the call to accept() after the signal is handled. * restart the call to accept() after the signal is handled.
*/ */
act.sa_handler = sig_handler; act.sa_handler = sig_handler;
sigemptyset(&act.sa_mask); sigemptyset(&act.sa_mask);
act.sa_flags = 0; act.sa_flags = 0;
@ -212,7 +212,7 @@ int main(int argc, char** argv)
CyaSSL_Init(); /* Initialize CyaSSL */ CyaSSL_Init(); /* Initialize CyaSSL */
/* Set ctx to DTLS 1.2 */ /* Set ctx to DTLS 1.2 */
if ( (ctx = CyaSSL_CTX_new(CyaDTLSv1_2_server_method())) == NULL){ if ( (ctx = CyaSSL_CTX_new(CyaDTLSv1_2_server_method())) == NULL) {
fprintf(stderr, "CyaSSL_CTX_new error.\n"); fprintf(stderr, "CyaSSL_CTX_new error.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -239,7 +239,7 @@ int main(int argc, char** argv)
} }
AwaitDGram(); AwaitDGram();
if (cleanup ==1) if (cleanup == 1)
CyaSSL_CTX_free(ctx); CyaSSL_CTX_free(ctx);
return 0; return 0;
} }