ready for publishing
parent
bb7f0702a2
commit
62e48b50d9
|
@ -39,11 +39,12 @@
|
||||||
#define SERV_PORT 11111 /* define our server port number */
|
#define SERV_PORT 11111 /* define our server port number */
|
||||||
#define MSGLEN 4096
|
#define MSGLEN 4096
|
||||||
|
|
||||||
|
CYASSL_CTX* ctx;
|
||||||
static int cleanup; /* To handle shutdown */
|
static int cleanup; /* To handle shutdown */
|
||||||
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;
|
void AwaitDGram(); /* Separate out Handling Datagrams */
|
||||||
void sig_handler(const int sig);
|
void sig_handler(const int sig);
|
||||||
|
|
||||||
void sig_handler(const int sig)
|
void sig_handler(const int sig)
|
||||||
|
@ -55,15 +56,21 @@ void sig_handler(const int sig)
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void AwaitDGram()
|
void AwaitDGram()
|
||||||
{
|
{
|
||||||
char ack[] = "I hear you fashizzle!";
|
char ack[] = "I hear you fashizzle!\n";
|
||||||
int listenfd = 0; /* Initialize our socket */
|
|
||||||
socklen_t clilen; /* length of address' */
|
|
||||||
int recvlen = 0; /* length of message */
|
|
||||||
char buff[MSGLEN]; /* the incoming message */
|
char buff[MSGLEN]; /* the incoming message */
|
||||||
|
int listenfd = 0; /* Initialize our socket */
|
||||||
|
int recvlen = 0; /* length of message */
|
||||||
|
int res = 1;
|
||||||
|
int on = 1;
|
||||||
|
int connfd = 0;
|
||||||
|
|
||||||
|
socklen_t len = sizeof(on);
|
||||||
|
socklen_t clilen;
|
||||||
|
clilen = sizeof(cliaddr);
|
||||||
|
unsigned char b[1500];
|
||||||
|
CYASSL* ssl = NULL;
|
||||||
|
|
||||||
while (cleanup != 1) {
|
while (cleanup != 1) {
|
||||||
|
|
||||||
|
@ -83,18 +90,13 @@ void AwaitDGram()
|
||||||
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
servaddr.sin_port = htons(SERV_PORT);
|
servaddr.sin_port = htons(SERV_PORT);
|
||||||
|
|
||||||
|
|
||||||
/* Eliminate socket already in use error */
|
/* Eliminate socket already in use error */
|
||||||
int res = 1;
|
|
||||||
int on = 1;
|
|
||||||
socklen_t len = sizeof(on);
|
|
||||||
res = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
|
res = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
printf("Setsockopt SO_REUSEADDR failed.\n");
|
printf("Setsockopt SO_REUSEADDR failed.\n");
|
||||||
cleanup = 1;
|
cleanup = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*Bind Socket*/
|
/*Bind Socket*/
|
||||||
if (bind(listenfd,
|
if (bind(listenfd,
|
||||||
(struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
|
(struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
|
||||||
|
@ -105,9 +107,6 @@ void AwaitDGram()
|
||||||
printf("Awaiting client connection on port %d\n", SERV_PORT);
|
printf("Awaiting client connection on port %d\n", SERV_PORT);
|
||||||
|
|
||||||
/* set clilen to |cliaddr| */
|
/* set clilen to |cliaddr| */
|
||||||
clilen = sizeof(cliaddr);
|
|
||||||
unsigned char b[1500];
|
|
||||||
int connfd = 0;
|
|
||||||
|
|
||||||
|
|
||||||
connfd = (int)recvfrom(listenfd, (char *)&b, sizeof(b), MSG_PEEK,
|
connfd = (int)recvfrom(listenfd, (char *)&b, sizeof(b), MSG_PEEK,
|
||||||
|
@ -131,8 +130,6 @@ void AwaitDGram()
|
||||||
}
|
}
|
||||||
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) {
|
||||||
|
@ -140,11 +137,9 @@ void AwaitDGram()
|
||||||
cleanup = 1;
|
cleanup = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* set the session ssl to client connection port */
|
/* set the session ssl to client connection port */
|
||||||
CyaSSL_set_fd(ssl, listenfd);
|
CyaSSL_set_fd(ssl, listenfd);
|
||||||
|
|
||||||
|
|
||||||
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];
|
||||||
|
@ -154,17 +149,12 @@ void AwaitDGram()
|
||||||
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);
|
||||||
|
|
||||||
buff[recvlen] = 0;
|
buff[recvlen] = 0;
|
||||||
printf("I heard this: \"%s\"\n", buff);
|
printf("I heard this: \"%s\"\n", buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (recvlen < 0) {
|
if (recvlen < 0) {
|
||||||
int readErr = CyaSSL_get_error(ssl, 0);
|
int readErr = CyaSSL_get_error(ssl, 0);
|
||||||
if(readErr != SSL_ERROR_WANT_READ) {
|
if(readErr != SSL_ERROR_WANT_READ) {
|
||||||
|
@ -172,19 +162,15 @@ void AwaitDGram()
|
||||||
cleanup = 1;
|
cleanup = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (CyaSSL_write(ssl, ack, sizeof(ack)) < 0) {
|
if (CyaSSL_write(ssl, ack, sizeof(ack)) < 0) {
|
||||||
printf("CyaSSL_write fail.\n");
|
printf("CyaSSL_write fail.\n");
|
||||||
cleanup = 1;
|
cleanup = 1;
|
||||||
|
} else {
|
||||||
|
printf("lost the connection to client\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
|
||||||
printf("lost the connection to client\n");
|
|
||||||
|
|
||||||
printf("reply sent \"%s\"\n", ack);
|
printf("reply sent \"%s\"\n", ack);
|
||||||
|
|
||||||
|
|
||||||
CyaSSL_set_fd(ssl, 0);
|
CyaSSL_set_fd(ssl, 0);
|
||||||
CyaSSL_shutdown(ssl);
|
CyaSSL_shutdown(ssl);
|
||||||
CyaSSL_free(ssl);
|
CyaSSL_free(ssl);
|
||||||
|
@ -193,7 +179,6 @@ void AwaitDGram()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
/* structures for signal handling */
|
/* structures for signal handling */
|
||||||
|
@ -203,7 +188,7 @@ int main(int argc, char** argv)
|
||||||
* 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;
|
||||||
|
|
Loading…
Reference in New Issue