pull/1/head
Kaleb Himes 2014-05-20 16:52:19 -06:00
parent 5ce6497497
commit a13f8b8467
6 changed files with 158 additions and 114 deletions

View File

@ -1,6 +1,6 @@
DEPS = ../include/unp.h DEPS = ../include/unp.h
CC=gcc CC=gcc
CFLAGS=-Wall -I ../include CFLAGS=-Wall -I ../include -DCYASSL_DTLS
OBJ = echoserver.o OBJ = echoserver.o
#if you are on a sunOS (System V) machine, you'll need to uncomment #if you are on a sunOS (System V) machine, you'll need to uncomment
@ -9,15 +9,12 @@ OBJ = echoserver.o
#LIBS=-lsocket #LIBS=-lsocket
%.o: %.c $(DEPS) %.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS) $(CC) -c -o $@ $< $(CFLAGS)
all: server-udp server-dtls
%.0: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
server-udp: server-udp.c server-udp: server-udp.c
$(CC) -Wall -o server-udp server-udp.c -I ../include -lcyassl $(CC) -Wall -o server-udp server-udp.c -I ../include -lcyassl
server-dtls: server-dtls.c server-dtls: server-dtls.c
$(CC) -Wall -o server-dtls server-dtls.c -I ../include -lcyassl $(CC) -Wall -DCYASSL_DTLS -o server-dtls server-dtls.c -I ../include -lcyassl
.PHONY: clean .PHONY: clean

View File

@ -1,5 +1,5 @@
# generated automatically by configure from AX_AUTOMAKE_MACROS # generated automatically by configure from AX_AUTOMAKE_MACROS
# on Mon May 19 14:54:12 MDT 2014 # on Tue May 20 10:37:37 MDT 2014
AM_MAKEFLAGS += -j5 AM_MAKEFLAGS += -j5

View File

@ -1079,7 +1079,7 @@ configure: failed program was:
configure:18379: result: no configure:18379: result: no
configure:18277: checking whether pthreads work without any flags configure:18277: checking whether pthreads work without any flags
configure:18370: clang -o conftest -g -O2 -fvisibility=hidden conftest.c >&5 configure:18370: clang -o conftest -g -O2 -fvisibility=hidden conftest.c >&5
/tmp/conftest-c6ef02.o: In function `main': /tmp/conftest-c8fdce.o: In function `main':
/home/khimes/work/project1/cyassl-3.0.0/conftest.c:49: undefined reference to `pthread_create' /home/khimes/work/project1/cyassl-3.0.0/conftest.c:49: undefined reference to `pthread_create'
/home/khimes/work/project1/cyassl-3.0.0/conftest.c:50: undefined reference to `pthread_join' /home/khimes/work/project1/cyassl-3.0.0/conftest.c:50: undefined reference to `pthread_join'
/home/khimes/work/project1/cyassl-3.0.0/conftest.c:52: undefined reference to `__pthread_register_cancel' /home/khimes/work/project1/cyassl-3.0.0/conftest.c:52: undefined reference to `__pthread_register_cancel'

Binary file not shown.

View File

@ -1,41 +1,24 @@
#include <stdio.h> /* standard in/out procedures */ #include <stdio.h> /* standard in/out procedures */
#include <stdlib.h> /* defines system calls */ #include <stdlib.h> /* defines system calls */
#include <string.h> /* necessary for memset */ #include <string.h> /* necessary for memset */
#include <netdb.h> #include <netdb.h>
#include <sys/socket.h> /* used for all socket calls */ #include <sys/socket.h> /* used for all socket calls */
#include <netinet/in.h> /* used for sockaddr_in */ #include <netinet/in.h> /* used for sockaddr_in */
#include <arpa/inet.h> #include <arpa/inet.h>
#include <cyassl/ssl.h> /* include the dtls library */ #include <cyassl/ssl.h>
#include "unp.h" #include <cyassl/test.h>
#include <errno.h>
#define SERV_PORT 11111 /* define our server port number */ #define SERV_PORT 11111 /* define our server port number */
#define MSGLEN 80 /* limit incoming message size */ #define MSGLEN 4096
static int cleanup; /* handles shutdown */ static int cleanup; /* To handle shutdown */
void sig_handler(const int sig) void sig_handler(const int sig)
{
printf("\nSIGINT handled.\n");
cleanup = 1;
return;
}
str_echo(CYASSL* ssl)
{ {
int n; printf("\nSIGINT handled.\n");
char buf[MSGLEN]; cleanup = 1;
exit(0);
while ( (n = CyaSSL_read(ssl, buf, MSGLEN)) > 0) {
if (CyaSSL_write(ssl, buf, n) != n {
err_sys("CyaSSL_write failed");
}
}
if ( n < 0 )
printf("CyaSSL_read error = %d\n", CyaSSL_get_error(ssl,n));
else if ( n == 0 )
printf("The peer has closed the connection.\n");
} }
int int
@ -43,110 +26,174 @@ main(int argc, char** argv)
{ {
/* CREATE THE SOCKET */ /* CREATE THE SOCKET */
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 */
int sockfd; /* Initialize our socket */ int listenfd; /* Initialize our socket */
CYASSL* cyasockfd; /* sockfd for cyassl */ socklen_t clilen; /* length of address' */
socklen_t addrlen = sizeof(cliaddr);/* length of address' */ int recvlen; /* length of message */
int recvlen; /* number of bytes recieved */ // int connfd; /* the connection the client makes */
int msgnum = 0; /* the messages we reveive in order */ char buff[MSGLEN]; /* the incoming message */
char buff[MSGLEN]; /* the incoming message */ struct sigaction act, oact; /* structures for signal handling */
struct sigaction act, oact;
/* 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. */
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;
sigaction(SIGINT, &act, &oact); sigaction(SIGINT, &act, &oact);
CyaSSL_Init(); /* initialize cyassl */ CyaSSL_Init(); // Initialize CyaSSL
CYASSL_CTX* ctx; /* initialize the context */ CYASSL_CTX* ctx;
/* Create and initialize CYASSL_CTX structure */ /* Create and initialize CYASSL_CTX structure */
if ( (ctx = CyaSSL_CTX_new(CyaTLSv1_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);
} }
/* Load CA certificates into CYASSL_CTX */ /* Load CA certificates into CYASSL_CTX */
if (CyaSSL_CTX_load_verify_locations(ctx,"certs/ca-cert.pem",0) != if (CyaSSL_CTX_load_verify_locations(ctx,"certs/ca-cert.pem",0) !=
SSL_SUCCESS) { SSL_SUCCESS) {
fprintf(stderr, "Error loading CA certs certs/ca-cert.pem, " fprintf(stderr, "Error loading certs/ca-cert.pem, "
"please check the file.\n"); "please check the file.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* Load server certificate into CYASSL_CTX */ /* Load server certificate into CYASSL_CTX */
if (CyaSSL_CTX_use_certificate_file(ctx,"certs/server-cert.pem", if (CyaSSL_CTX_use_certificate_file(ctx,"certs/server-cert.pem",
SSL_FILETYPE_PEM) != SSL_SUCCESS) { SSL_FILETYPE_PEM) != SSL_SUCCESS) {
fprintf(stderr, "Error loading server certs certs/server-cert.pem, " fprintf(stderr, "Error loading certs/server-cert.pem, "
"please check the file.\n"); "please check the file.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* Load server key into CYASSL_CTX */ /* Load server key into CYASSL_CTX */
if (CyaSSL_CTX_use_PrivateKey_file(ctx,"certs/server-key.pem", if (CyaSSL_CTX_use_PrivateKey_file(ctx,"certs/server-key.pem",
SSL_FILETYPE_PEM) != SSL_SUCCESS) { SSL_FILETYPE_PEM) != SSL_SUCCESS) {
fprintf(stderr, "Error loading server keys certs/server-key.pem, " fprintf(stderr, "Error loading certs/server-key.pem, "
"please check the file.\n"); "please check the file.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* create a UDP/IP socket */ /* create a UDP/IP socket */
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
/* setsockopt: Eliminates "ERROR on bind: Addr in use" error. */
optval = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int));
/* INADDR_ANY = IP address and socket = 11111, modify SERV_PORT to change */ if((listenfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
memset(&servaddr, sizeof(servaddr)); {
perror("cannot create socket");
return 0;
}
/* printf("created socket descriptor = %d\n", sockfd); */
/* INADDR_ANY=IP address and socket = 11111, modify SERV_PORT to change */
memset((char *)&servaddr, 0, sizeof(servaddr));
/* Use Datagram Service */
servaddr.sin_family = AF_INET; servaddr.sin_family = AF_INET;
/* host-to-network-long conversion (htonl) */ /* host-to-network-long conversion (htonl) */
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
/* host-to-network-short conversion (htons) */ /* host-to-network-short conversion (htons) */
servaddr.sin_port = htons(SERV_PORT); servaddr.sin_port = htons(SERV_PORT);
/* Eliminate socket already in use error */
Bind(sockfd, (SA *) &servaddr, sizeof(servaddr)); int res, on = 1;
Listen(sockfd, LISTENQ); socklen_t len = sizeof(on);
res = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
if(res<0){
err_sys("setsockopt SO_REUSEADDR failed\n");
}
/* bind the socket */
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("bind failed");
return 0;
}
/* loop, listen for client, print received, reply to client */ /* loop, listen for client, print received, reply to client */
while (cleanup != 1) { while (cleanup != 1) {
printf("waiting for client message on port %d\n", SERV_PORT); printf("waiting for client message on port %d\n", SERV_PORT);
CYASSL* ssl; /* initialize arg */
clilen = sizeof(cliaddr); /* set clilen to |cliaddr| */
unsigned char b[1500];
int n;
if ( ( recvlen = accept(sockfd, (SA *)&cliaddr, &addrlen)) < 0 ) n = (int)recvfrom(listenfd, (char*)b, sizeof(b), MSG_PEEK,
{ (struct sockaddr*)&cliaddr, &clilen);
if (errno == EINTR)
continue; /* back to while() */ printf("connection attempt made.\n");
else if(n > 0) {
err_sys("accept error\n"); if(connect(listenfd, (const struct sockaddr*)&cliaddr,
} sizeof(cliaddr)) != 0)
printf("heard on port %d, a message from %s\n", err_sys("udp connect failed\n");
ntohs(cliaddr.sin_port), inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff));
printf("made it this far");
if ( (ssl = CyaSSL_new(ctx)) == NULL) {
printf("got into the if");
fprintf(stderr, "CyaSSL_new error.\n");
exit(EXIT_FAILURE);
} }
// else
CyaSSL_set_fd(ssl, recvlen); // err_sys("recvfrom failed\n");
str_echo(ssl);
CyasSSL_free(ssl); // connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
Close(recvlen); //
printf("reset recvlen in the buf to zero"); // if ( connfd == -1 ){
printf("I heard this: \"%s\"\n", buff); // printf("connfd = %d\n", connfd);
// printf("accept error\n");
// }
else{
printf("connected!\n");
/* Create the CYASSL Object */
if((ssl = CyaSSL_new(ctx))== NULL){
fprintf(stderr, "CyaSSL_new error.\n");
exit(EXIT_FAILURE);
}
/* set the session ssl to clientfd */
CyaSSL_set_fd(ssl, listenfd);
for(;;){
if (CyaSSL_accept(ssl) != SSL_SUCCESS) {
int err = CyaSSL_get_error(ssl, 0);
char buffer[CYASSL_MAX_ERROR_SZ];
printf("error = %d, %s\n", err, CyaSSL_ERR_error_string(err, buffer));
err_sys("SSL_accept failed\n");
}
if( (recvlen = CyaSSL_read(ssl, buff, MSGLEN)) > 0){
printf("heard %d bytes\n", recvlen);
if (recvlen > 0) {
buff[recvlen] = 0;
printf("I heard this: \"%s\"\n", buff);
}
if (recvlen < 0){
int readErr = CyaSSL_get_error(ssl, 0);
if(readErr != SSL_ERROR_WANT_READ)
err_sys("SSL_read failed");
}
if (CyaSSL_write(ssl, buff, strlen(buff)) < 0){
perror("sendto");
printf("reply sent \"%s\"\n", buff);
}
/* continues to loop, use "Ctrl+C" to terminate listening */
}
else{
printf("lost the connection to client\n");
break;
}
}
} }
CyaSSL_CTX_free(ctx); }
printf("CyaSSL_CTX freed up\n"); return(0);
CyaSSL_Cleanup();
printf("CyaSSL freed up");
exit(EXIT_SUCCESS);
} }