From 1fd9efda94f19de777749262f0bfa05196ae55ea Mon Sep 17 00:00:00 2001 From: Kaleb Himes Date: Wed, 21 May 2014 20:50:28 -0600 Subject: [PATCH] loop still failing --- project1/server-dtls.c | 65 ++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/project1/server-dtls.c b/project1/server-dtls.c index d8b1c0fb..9d88ae18 100644 --- a/project1/server-dtls.c +++ b/project1/server-dtls.c @@ -1,3 +1,29 @@ +/* server-dtls.c + * + * Copyright (C) 2006-2014 wolfSSL Inc. + * + * This file is part of CyaSSL. + * + * CyaSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * CyaSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA + * ============================================================================= + * + * Bare-bones example of a DTLS server for instructional/learning purposes. + * Utilizes DTLS 1.2. + */ + #include /* standard in/out procedures */ #include /* defines system calls */ #include /* necessary for memset */ @@ -8,16 +34,18 @@ #include #include #include +#include -#define SERV_PORT 11111 /* define our server port number */ +#define SERV_PORT 11111 /* define our server port number */ #define MSGLEN 4096 -static int cleanup; /* To handle shutdown */ +static int cleanup; /* To handle shutdown */ void sig_handler(const int sig) { printf("\nSIGINT handled.\n"); cleanup = 1; + exit(0); } static void err_sys(const char* msg) @@ -33,17 +61,17 @@ int main(int argc, char** argv) char ack[] = "I hear you fashizzle!"; - struct sockaddr_in servaddr; /* our server's address */ - struct sockaddr_in cliaddr; /* the client's address */ - int listenfd; /* Initialize our socket */ + struct sockaddr_in servaddr; /* our server's address */ + struct sockaddr_in cliaddr; /* the client's address */ + int listenfd; /* Initialize our socket */ socklen_t clilen; /* length of address' */ int recvlen; /* length of message */ - char buff[MSGLEN]; /* the incoming message */ - struct sigaction act, oact; /* structures for signal handling */ + char buff[MSGLEN]; /* the incoming message */ + struct sigaction act, oact; /* structures for signal handling */ /* 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 - restart the call to accept() after the signal is handled. */ + * with Ctrl-C. Also, turn off SA_RESTART so that the OS doesn't + * restart the call to accept() after the signal is handled. */ act.sa_handler = sig_handler; sigemptyset(&act.sa_mask); @@ -128,12 +156,15 @@ int main(int argc, char** argv) CYASSL* ssl; /* initialize arg */ clilen = sizeof(cliaddr); /* set clilen to |cliaddr| */ unsigned char b[1500]; - int n; + int connfd; - n = (int)recvfrom(listenfd, (char*)b, sizeof(b), MSG_PEEK, + connfd = (int)recvfrom(listenfd, (char*)b, sizeof(b), MSG_PEEK, (struct sockaddr*)&cliaddr, &clilen); - if (n > 0) { + if (connfd < 0) + printf("No clients in que, enter idle state\n"); + + else if (connfd > 0) { if (connect(listenfd, (const struct sockaddr *)&cliaddr, sizeof(cliaddr)) != 0) err_sys("udp connect failed"); @@ -141,7 +172,7 @@ int main(int argc, char** argv) else err_sys("recvfrom failed"); - printf("connection attempt made.\n"); + printf("Connected!\n"); /* Create the CYASSL Object */ @@ -150,7 +181,6 @@ int main(int argc, char** argv) exit(EXIT_FAILURE); } - /* set the session ssl to client connection port */ CyaSSL_set_fd(ssl, listenfd); @@ -163,8 +193,6 @@ int main(int argc, char** argv) } - printf("connected!\n"); - if (( recvlen = CyaSSL_read(ssl, buff, sizeof(buff)-1)) > 0){ printf("heard %d bytes\n", recvlen); @@ -189,9 +217,10 @@ int main(int argc, char** argv) printf("lost the connection to client\n"); break; } - - CyaSSL_shutdown(ssl); + + CyaSSL_set_fd(ssl, connfd); CyaSSL_free(ssl); + close(connfd); printf("Client left return to idle state\n"); continue;