Compare commits

...

3 Commits

1
.gitignore vendored

@ -9,6 +9,7 @@ test-tcpserver
test-udp test-udp
.depend .depend
.Makefile.rules .Makefile.rules
Makefile.rules
# ---> Eclipse # ---> Eclipse
.metadata .metadata

@ -23,6 +23,7 @@ help:
echo "set up configuration" echo "set up configuration"
echo " make configwindows to generate the windows build" echo " make configwindows to generate the windows build"
echo " make configlinux to generate the linix build" echo " make configlinux to generate the linix build"
echo " make keygen create self signed certificate"
configlinux: clean configlinux: clean
cp -f Makefile.rules.linux Makefile.rules cp -f Makefile.rules.linux Makefile.rules

@ -61,13 +61,16 @@ using namespace std;
* global functions needed for networking * global functions needed for networking
* *
*/ */
extern int UDPTCPNetwork_init;
int dns_filladdr (string host, string port, int ai_family, int dns_filladdr (string host, string port, int ai_family,
struct sockaddr_storage *sAddr); struct sockaddr_storage *sAddr);
char *itoa(char* buffer, int number, int size); char *itoa(char* buffer, int number, int size);
void UDPTCPNetwork_Startup(); void UDPTCPNetwork_Startup();
extern int UDPTCPNetwork_init;
#define UDPTCPNetwork() if(UDPTCPNetwork_init == 0) UDPTCPNetwork_Startup() #define UDPTCPNetwork() if(UDPTCPNetwork_init == 0) UDPTCPNetwork_Startup()
int file_is_readable (const char *fname);
/************************************************************************ /************************************************************************
* *
* udp related functions * udp related functions

@ -1,7 +1,11 @@
/*
*
*/
#include "UDPTCPNetwork.h" #include "UDPTCPNetwork.h"
#include <stdio.h> #include <stdio.h>
#include <fcntl.h>
#include <unistd.h> /* close() */ #include <unistd.h> /* close() */
#include <string.h> /* memset() */ #include <string.h> /* memset() */
@ -39,7 +43,7 @@ int dns_filladdr (string host, string port, int ai_family, struct sockaddr_stora
char* itoa(char* buffer, int number, int size) { char* itoa(char* buffer, int number, int size) {
snprintf (buffer, size, "%d", number); snprintf (buffer, size, "%d", number);
return buffer; return buffer;
} };
void UDPTCPNetwork_Startup() { void UDPTCPNetwork_Startup() {
@ -65,4 +69,16 @@ void UDPTCPNetwork_Startup() {
} }
#endif #endif
UDPTCPNetwork_init = 1; UDPTCPNetwork_init = 1;
} };
int file_is_readable (const char *fname) {
int f;
if ((f = open(fname, O_RDONLY)) == -1) return 0;
close (f);
return 1;
};

@ -101,6 +101,9 @@ int SSLSocket::SetCertificat(string certf, string keyf) {
certfile = certf; certfile = certf;
keyfile = keyf; keyfile = keyf;
if (!file_is_readable(certf.c_str())) return 0;
if (!file_is_readable(keyf.c_str())) return 0;
return 1; return 1;
}; };

@ -21,20 +21,23 @@ void server () {
// //
// start the server // start the server
printf ("server: starting server\n");
if (tcpserver.Listen(DEFAULT_PORT) != 1) { if (tcpserver.Listen(DEFAULT_PORT) != 1) {
printf ("cloud not start the tcp server\n"); printf ("server: cloud not start the tcp server\n");
exit (1); exit (1);
} }
// //
// init SSL // init SSL
printf ("server: setting up certificates\n");
if (ssl.SetCertificat("cert.pem", "privkey.pem") != 1) { if (ssl.SetCertificat("cert.pem", "privkey.pem") != 1) {
printf ("SetCertificat error:%s\n", strerror(errno)); printf ("server: SetCertificat error:%s\n", strerror(errno));
exit (1); exit (1);
} }
// //
// check for connections // check for connections
printf ("server: wait for connection\n");
for (;time_now - time_start < 10; time_now = time(NULL)) { for (;time_now - time_start < 10; time_now = time(NULL)) {
connection = tcpserver.Accept(); connection = tcpserver.Accept();
if (connection != NULL) { if (connection != NULL) {
@ -42,17 +45,10 @@ void server () {
// someone connected - create new process // someone connected - create new process
// take care of parallel processing (parent is always the server) // take care of parallel processing (parent is always the server)
// //
printf (" server: got a connection forking new process\n"); printf ("server: someone connected.\n");
pid = fork(); printf ("server: accept ssl connection\n");
if (pid == 0) {
//
// child process - always close server since it will handeled
// by the parent process. Make sure the client exits and never
// returns.
tcpserver.Close();
if (ssl.Accept(connection->GetSocket(), 0) != 1) { if (ssl.Accept(connection->GetSocket(), 0) != 1) {
printf ("could not establish SSL connection:%s\n", strerror(errno)); printf ("server: could not establish SSL connection:%s\n", strerror(errno));
exit (1); exit (1);
} }
i = ssl.Read(buffer, NET_BUFFERSIZE); i = ssl.Read(buffer, NET_BUFFERSIZE);
@ -63,24 +59,18 @@ void server () {
for (c = 0; c < i; c++) buffer[c] = toupper(buffer[c]); for (c = 0; c < i; c++) buffer[c] = toupper(buffer[c]);
ssl.Write(buffer, i); ssl.Write(buffer, i);
} }
else {
printf ("server: nothing to read. close connection.\n");
}
// //
// just delete the class object, it will close the client connection // just delete the class object, it will close the client connection
ssl.Close(); ssl.Close();
delete (connection); delete (connection);
connection = NULL;
//
// exit child process
exit (1);
} }
else { usleep (250000);
//
// parent process - just close the client connection
// it will be handeled by the child process.
delete (connection);
}
}
usleep (25000);
} }
printf ("server: terminating server process.\n");
}; };
@ -94,16 +84,19 @@ void client () {
// //
// connect to the server // connect to the server
printf ("client: connect to localhost\n");
if (tcpclient.Connect ("localhost", DEFAULT_PORT) != 1) { if (tcpclient.Connect ("localhost", DEFAULT_PORT) != 1) {
printf ("cloud not connect to server\n"); printf ("client: cloud not connect to server\n");
exit (1); exit (1);
} }
printf ("client: connected\n");
res = ssl.Connect(tcpclient.GetSocket(), 100); printf ("client: start SSL connection\n");
if (res == -1) { if (ssl.Connect(tcpclient.GetSocket(), 100) != 1) {
printf ("could not establish SSL connection:errno:%s sslerror:%s\n", strerror(errno), ssl.GetSSLErrorText(ssl.sslerror).c_str()); printf ("client: could not establish SSL connection:errno:%s sslerror:%s\n", strerror(errno), ssl.GetSSLErrorText(ssl.sslerror).c_str());
exit (1); exit (1);
} }
printf ("client: ssl connected.\n");
// //
// send some data // send some data
@ -111,17 +104,25 @@ void client () {
printf ("client: send '%s' to the server.\n", buffer); printf ("client: send '%s' to the server.\n", buffer);
if (ssl.Write(buffer, strlen (buffer)) != strlen (buffer)) { if (ssl.Write(buffer, strlen (buffer)) != strlen (buffer)) {
printf ("could not send all data. errno:%s sslerror:%s\n", strerror(errno), ssl.GetSSLErrorText(ssl.sslerror).c_str()); printf ("client: could not send all data. errno:%s sslerror:%s\n", strerror(errno), ssl.GetSSLErrorText(ssl.sslerror).c_str());
exit (1); exit (1);
} }
// //
// read some data (wait maximum 10x1000ms) // read some data (wait maximum 10x1000ms)
for (i = 10; i > 0; i--) for (i = 10; i > 0; i--) {
if (ssl.Read(buffer, NET_BUFFERSIZE) > 0) { printf ("client: try to read\n");
if ((res = ssl.Read(buffer, NET_BUFFERSIZE)) > 0) {
printf ("client: got '%s' from server.\n", buffer); printf ("client: got '%s' from server.\n", buffer);
break; break;
} }
else if (res < 0) {
printf ("client: error on read: Error: %s\n", strerror(errno));
}
else {
printf ("client: no data\n");
}
}
// //
// close connection // close connection

Loading…
Cancel
Save