P1 v1.4
parent
5ce6497497
commit
a13f8b8467
|
@ -1,6 +1,6 @@
|
|||
DEPS = ../include/unp.h
|
||||
CC=gcc
|
||||
CFLAGS=-Wall -I ../include
|
||||
CFLAGS=-Wall -I ../include -DCYASSL_DTLS
|
||||
OBJ = echoserver.o
|
||||
|
||||
#if you are on a sunOS (System V) machine, you'll need to uncomment
|
||||
|
@ -9,15 +9,12 @@ OBJ = echoserver.o
|
|||
#LIBS=-lsocket
|
||||
%.o: %.c $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
%.0: %.c $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
all: server-udp server-dtls
|
||||
server-udp: server-udp.c
|
||||
$(CC) -Wall -o server-udp server-udp.c -I ../include -lcyassl
|
||||
|
||||
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
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
# 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
|
||||
|
|
|
@ -1079,7 +1079,7 @@ configure: failed program was:
|
|||
configure:18379: result: no
|
||||
configure:18277: checking whether pthreads work without any flags
|
||||
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:50: undefined reference to `pthread_join'
|
||||
/home/khimes/work/project1/cyassl-3.0.0/conftest.c:52: undefined reference to `__pthread_register_cancel'
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1,41 +1,24 @@
|
|||
#include <stdio.h> /* standard in/out procedures */
|
||||
#include <stdlib.h> /* defines system calls */
|
||||
#include <string.h> /* necessary for memset */
|
||||
#include <netdb.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h> /* used for all socket calls */
|
||||
#include <netinet/in.h> /* used for sockaddr_in */
|
||||
#include <arpa/inet.h>
|
||||
#include <cyassl/ssl.h> /* include the dtls library */
|
||||
#include "unp.h"
|
||||
#include <cyassl/ssl.h>
|
||||
#include <cyassl/test.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define SERV_PORT 11111 /* define our server port number */
|
||||
#define MSGLEN 80 /* limit incoming message size */
|
||||
#define SERV_PORT 11111 /* define our server port number */
|
||||
#define MSGLEN 4096
|
||||
|
||||
static int cleanup; /* handles shutdown */
|
||||
static int cleanup; /* To handle shutdown */
|
||||
|
||||
void sig_handler(const int sig)
|
||||
{
|
||||
printf("\nSIGINT handled.\n");
|
||||
cleanup = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
str_echo(CYASSL* ssl)
|
||||
void sig_handler(const int sig)
|
||||
{
|
||||
int n;
|
||||
char buf[MSGLEN];
|
||||
|
||||
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");
|
||||
printf("\nSIGINT handled.\n");
|
||||
cleanup = 1;
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -43,110 +26,174 @@ main(int argc, char** argv)
|
|||
{
|
||||
/* CREATE THE SOCKET */
|
||||
|
||||
struct sockaddr_in servaddr; /* our server's address */
|
||||
struct sockaddr_in cliaddr; /* the client's address */
|
||||
int sockfd; /* Initialize our socket */
|
||||
CYASSL* cyasockfd; /* sockfd for cyassl */
|
||||
socklen_t addrlen = sizeof(cliaddr);/* length of address' */
|
||||
int recvlen; /* number of bytes recieved */
|
||||
int msgnum = 0; /* the messages we reveive in order */
|
||||
char buff[MSGLEN]; /* the incoming message */
|
||||
struct sigaction act, oact;
|
||||
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 */
|
||||
// int connfd; /* the connection the client makes */
|
||||
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. */
|
||||
|
||||
act.sa_handler = sig_handler;
|
||||
sigemptyset(&act.sa_mask);
|
||||
act.sa_flags = 0;
|
||||
sigaction(SIGINT, &act, &oact);
|
||||
|
||||
CyaSSL_Init(); /* initialize cyassl */
|
||||
CYASSL_CTX* ctx; /* initialize the context */
|
||||
CyaSSL_Init(); // Initialize CyaSSL
|
||||
CYASSL_CTX* ctx;
|
||||
|
||||
/* Create and initialize CYASSL_CTX structure */
|
||||
if ( (ctx = CyaSSL_CTX_new(CyaTLSv1_2_server_method())) == NULL){
|
||||
fprintf(stderr, "CyaSSL_CTX_new error.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
/* Create and initialize CYASSL_CTX structure */
|
||||
if ( (ctx = CyaSSL_CTX_new(CyaDTLSv1_2_server_method())) == NULL){
|
||||
fprintf(stderr, "CyaSSL_CTX_new error.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Load CA certificates into CYASSL_CTX */
|
||||
if (CyaSSL_CTX_load_verify_locations(ctx,"certs/ca-cert.pem",0) !=
|
||||
SSL_SUCCESS) {
|
||||
fprintf(stderr, "Error loading CA certs certs/ca-cert.pem, "
|
||||
"please check the file.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
/* Load CA certificates into CYASSL_CTX */
|
||||
if (CyaSSL_CTX_load_verify_locations(ctx,"certs/ca-cert.pem",0) !=
|
||||
SSL_SUCCESS) {
|
||||
fprintf(stderr, "Error loading certs/ca-cert.pem, "
|
||||
"please check the file.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Load server certificate into CYASSL_CTX */
|
||||
if (CyaSSL_CTX_use_certificate_file(ctx,"certs/server-cert.pem",
|
||||
SSL_FILETYPE_PEM) != SSL_SUCCESS) {
|
||||
fprintf(stderr, "Error loading server certs certs/server-cert.pem, "
|
||||
"please check the file.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
/* Load server certificate into CYASSL_CTX */
|
||||
if (CyaSSL_CTX_use_certificate_file(ctx,"certs/server-cert.pem",
|
||||
SSL_FILETYPE_PEM) != SSL_SUCCESS) {
|
||||
fprintf(stderr, "Error loading certs/server-cert.pem, "
|
||||
"please check the file.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Load server key into CYASSL_CTX */
|
||||
if (CyaSSL_CTX_use_PrivateKey_file(ctx,"certs/server-key.pem",
|
||||
SSL_FILETYPE_PEM) != SSL_SUCCESS) {
|
||||
fprintf(stderr, "Error loading server keys certs/server-key.pem, "
|
||||
"please check the file.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
/* Load server key into CYASSL_CTX */
|
||||
if (CyaSSL_CTX_use_PrivateKey_file(ctx,"certs/server-key.pem",
|
||||
SSL_FILETYPE_PEM) != SSL_SUCCESS) {
|
||||
fprintf(stderr, "Error loading certs/server-key.pem, "
|
||||
"please check the file.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
memset(&servaddr, sizeof(servaddr));
|
||||
if((listenfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
||||
{
|
||||
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;
|
||||
|
||||
/* host-to-network-long conversion (htonl) */
|
||||
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
/* host-to-network-short conversion (htons) */
|
||||
servaddr.sin_port = htons(SERV_PORT);
|
||||
|
||||
|
||||
Bind(sockfd, (SA *) &servaddr, sizeof(servaddr));
|
||||
Listen(sockfd, LISTENQ);
|
||||
/* Eliminate socket already in use error */
|
||||
int res, on = 1;
|
||||
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 */
|
||||
while (cleanup != 1) {
|
||||
while (cleanup != 1) {
|
||||
|
||||
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 )
|
||||
{
|
||||
if (errno == EINTR)
|
||||
continue; /* back to while() */
|
||||
else
|
||||
err_sys("accept error\n");
|
||||
}
|
||||
printf("heard on port %d, a message from %s\n",
|
||||
ntohs(cliaddr.sin_port), inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff));
|
||||
n = (int)recvfrom(listenfd, (char*)b, sizeof(b), MSG_PEEK,
|
||||
(struct sockaddr*)&cliaddr, &clilen);
|
||||
|
||||
printf("connection attempt made.\n");
|
||||
if(n > 0) {
|
||||
if(connect(listenfd, (const struct sockaddr*)&cliaddr,
|
||||
sizeof(cliaddr)) != 0)
|
||||
err_sys("udp connect failed\n");
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
CyaSSL_set_fd(ssl, recvlen);
|
||||
str_echo(ssl);
|
||||
CyasSSL_free(ssl);
|
||||
Close(recvlen);
|
||||
printf("reset recvlen in the buf to zero");
|
||||
printf("I heard this: \"%s\"\n", buff);
|
||||
// else
|
||||
// err_sys("recvfrom failed\n");
|
||||
|
||||
// connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
|
||||
//
|
||||
// if ( connfd == -1 ){
|
||||
// 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");
|
||||
CyaSSL_Cleanup();
|
||||
printf("CyaSSL freed up");
|
||||
exit(EXIT_SUCCESS);
|
||||
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue