Compare commits

..

No commits in common. '5f35e868d55b0ef21f35316040106428427b5494' and 'dfd75f2f679089f7d7fdf67276cfa5b99f0cedfb' have entirely different histories.

1
.gitignore vendored

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

@ -23,7 +23,6 @@ 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,16 +61,13 @@ 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,11 +1,7 @@
/*
*
*/
#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() */
@ -43,7 +39,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() {
@ -69,16 +65,4 @@ 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,9 +101,6 @@ 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,23 +21,20 @@ 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 ("server: cloud not start the tcp server\n"); printf ("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 ("server: SetCertificat error:%s\n", strerror(errno)); printf ("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) {
@ -45,32 +42,45 @@ 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: someone connected.\n"); printf (" server: got a connection forking new process\n");
printf ("server: accept ssl connection\n"); pid = fork();
if (ssl.Accept(connection->GetSocket(), 0) != 1) { if (pid == 0) {
printf ("server: could not establish SSL connection:%s\n", strerror(errno)); //
// 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) {
printf ("could not establish SSL connection:%s\n", strerror(errno));
exit (1);
}
i = ssl.Read(buffer, NET_BUFFERSIZE);
if (i > 0) {
int c;
printf (" server: got: '%s'\n", buffer);
for (c = 0; c < i; c++) buffer[c] = toupper(buffer[c]);
ssl.Write(buffer, i);
}
//
// just delete the class object, it will close the client connection
ssl.Close();
delete (connection);
//
// exit child process
exit (1); exit (1);
} }
i = ssl.Read(buffer, NET_BUFFERSIZE);
if (i > 0) {
int c;
printf ("server: got: '%s'\n", buffer);
for (c = 0; c < i; c++) buffer[c] = toupper(buffer[c]);
ssl.Write(buffer, i);
}
else { else {
printf ("server: nothing to read. close connection.\n"); //
// parent process - just close the client connection
// it will be handeled by the child process.
delete (connection);
} }
//
// just delete the class object, it will close the client connection
ssl.Close();
delete (connection);
connection = NULL;
} }
usleep (250000); usleep (25000);
} }
printf ("server: terminating server process.\n");
}; };
@ -84,45 +94,34 @@ 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 ("client: cloud not connect to server\n"); printf ("cloud not connect to server\n");
exit (1); exit (1);
} }
printf ("client: connected\n");
printf ("client: start SSL connection\n"); res = ssl.Connect(tcpclient.GetSocket(), 100);
if (ssl.Connect(tcpclient.GetSocket(), 100) != 1) { if (res == -1) {
printf ("client: could not establish SSL connection:errno:%s sslerror:%s\n", strerror(errno), ssl.GetSSLErrorText(ssl.sslerror).c_str()); printf ("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
snprintf (buffer, NET_BUFFERSIZE, "nur ein kleiner Test."); snprintf (buffer, NET_BUFFERSIZE, "nur ein kleiner Test.");
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 ("client: could not send all data. errno:%s sslerror:%s\n", strerror(errno), ssl.GetSSLErrorText(ssl.sslerror).c_str()); printf ("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--)
printf ("client: try to read\n"); if (ssl.Read(buffer, NET_BUFFERSIZE) > 0) {
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