|
|
|
@ -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 (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 ("server: could not establish SSL connection:%s\n", strerror(errno));
|
|
|
|
printf ("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);
|
|
|
|
if (i > 0) {
|
|
|
|
if (i > 0) {
|
|
|
|
int c;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
|
|
|
|
printf ("server: got: '%s'\n", buffer);
|
|
|
|
printf (" server: got: '%s'\n", buffer);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usleep (250000);
|
|
|
|
else {
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -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
|
|
|
|
|