Use dedicated thread functions for certain tasks.

This commit is contained in:
Igor Socec 2016-11-19 15:29:16 +01:00
parent 9703b42b3a
commit d73794b11d
5 changed files with 288 additions and 241 deletions

View file

@ -1,4 +1,4 @@
/* Common header file reused within the project */
/* Common header file reused within the project. */
#pragma once

View file

@ -4,13 +4,21 @@
* communication with a specific TTY device.
*/
#include <moxerver.h>
#include <common.h>
#include <task_threads.h>
#include <parser.h>
#include <signal.h> /* handling quit signals */
#include <pthread.h>
#define NAME "moxerver"
#define CONFILE "moxanix.cfg"
/* Global variables used throughout the application. */
server_t server; /* main server */
client_t client; /* connected client */
client_t new_client; /* reserved for a new client request */
tty_t tty_dev; /* connected tty device */
/* Prints help message. */
static void usage()
{
@ -80,231 +88,9 @@ void time2string(time_t time, char* timestamp)
strftime(timestamp, TIMESTAMP_LEN, TIMESTAMP_FORMAT, localtime(&time));
}
void* thread_new_client_connection(void *args)
{
char msg[BUFFER_LEN];
char timestamp[TIMESTAMP_LEN];
client_t temp_client;
/* get resources from args */
resources_t r = *((resources_t*) args);
/* accept new connection request */
if (server_accept(r.server, &temp_client) != 0)
{
return (void *) -1;
}
/* if no client is connected then make the new client available for connection */
if (r.client->socket == -1)
{
memcpy(r.new_client, &temp_client, sizeof(client_t));
return (void *) 0;
}
/* if there is already a new client being handled then reject this client request */
if (r.new_client->socket != -1)
{
sprintf(msg, "\nToo many connection requests, please try later.\n");
send(temp_client.socket, msg, strlen(msg), 0);
client_close(&temp_client);
time2string(time(NULL), timestamp);
fprintf(stderr, "[%s] rejected new client request %s @ %s\n", __func__,
temp_client.ip_string, timestamp);
return (void *) 0;
}
/* make the new client available for connection */
memcpy(r.new_client, &temp_client, sizeof(client_t));
/* inform the new client that the port is already in use */
time2string(r.client->last_active, timestamp);
sprintf(msg, "\nPort %u is already being used!\nCurrent user and last activity:\n%s @ %s\n",
r.server->port, r.client->username, timestamp);
send(r.new_client->socket, msg, strlen(msg), 0);
/* ask the new client if the currently connected client should be dropped */
sprintf(msg, "\nDo you want to drop the current user?\n"
"If yes then please type YES DROP (in uppercase):\n");
send(r.new_client->socket, msg, strlen(msg), 0);
/* wait for client input */
client_wait_line(r.new_client);
/* check client confirmation */
if (strncmp(r.new_client->data, "YES DROP", 8) == 0)
{
/* drop the connected client */
client_close(r.client);
fprintf(stderr, "[%s] dropped client %s @ %s\n", __func__,
r.client->ip_string, timestamp);
}
else
{
/* reject this client request */
client_close(r.new_client);
time2string(time(NULL), timestamp);
fprintf(stderr, "[%s] rejected new client request %s @ %s\n", __func__,
temp_client.ip_string, timestamp);
return (void *) 1;
}
return (void *) 0;
}
void* thread_tty_data(void *args)
{
struct timeval tv;
ssize_t br = 0;
int ret;
fd_set read_fds;
/* get resources from args */
resources_t r = *((resources_t*) args);
fprintf(stderr, "[%s] tty thread started with passed argument: %s\n",
NAME, r.tty_dev->path);
while (1)
{
/* setup parameters for select() */
tv.tv_sec = TTY_WAIT_TIMEOUT;
tv.tv_usec = 0;
FD_ZERO(&read_fds);
FD_SET(r.tty_dev->fd, &read_fds);
/* wait with select() */
ret = select(r.tty_dev->fd + 1, &read_fds, NULL, NULL, &tv);
if (ret > 0 && FD_ISSET(r.tty_dev->fd, &read_fds))
{
br = tty_read(r.tty_dev);
client_write(r.client, r.tty_dev->data, br);
}
else
{
}
if (debug_messages)
{
fprintf(stderr, "[%s] tty thread alive\n", NAME);
}
}
fprintf(stderr, "[%s] tty thread stoped\n", NAME);
return (void*) 0;
}
void* thread_client_data(void *args)
{
int ret;
fd_set read_fds;
int fdmax;
struct timeval tv;
pthread_t new_client_thread;
/* loop with timeouts waiting for client connection requests and data */
while (1) {
/* check if new client is available for connection */
if ( (client.socket == -1) && (new_client.socket != -1) ) {
/* copy new client information */
memcpy(&client, &new_client, sizeof(client_t));
new_client.socket = -1;
fprintf(stderr, "[%s] client %s connected\n", NAME, client.ip_string);
/* ask client to provide a username before going to "character" mode */
if (client_ask_username(&client) != 0) {
/* close client if not able to provide a username */
client_close(&client);
continue;
}
/* put client in "character" mode */
char msg[TELNET_MSG_LEN_CHARMODE];
telnet_message_set_character_mode(msg);
client_write(&client, msg, TELNET_MSG_LEN_CHARMODE);
}
/* setup parameters for select() */
tv.tv_sec = SERVER_WAIT_TIMEOUT;
tv.tv_usec = 0;
FD_ZERO(&read_fds);
FD_SET(server.socket, &read_fds);
if (client.socket != -1) {
FD_SET(client.socket, &read_fds); /* wait for client if connected */
}
fdmax = (server.socket > client.socket) ? server.socket : client.socket;
/* wait with select() */
ret = select(fdmax+1, &read_fds, NULL, NULL, &tv);
if (ret == -1) {
//TODO do we really break here and stop server when select returns an error?
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno));
break;
}
if (ret > 0) {
/* check server status */
if (FD_ISSET(server.socket, &read_fds)) {
fprintf(stderr, "[%s] received client connection request\n", NAME);
/* handle new client connection request in a separate thread */
resources_t r = {&server, &client, &new_client, &tty_dev};
if (pthread_create(&new_client_thread, NULL, thread_new_client_connection, &r) != 0) {
/* print error but continue waiting for connection request */
fprintf(stderr, "[%s] problem with handling client connection request\n", NAME);
continue;
}
}
/* check client status if connected */
if ( (client.socket != -1) && FD_ISSET(client.socket, &read_fds) ) {
/* read client data */
ret = client_read(&client);
/* check if client disconnected or other errors occurred */
if (ret == -ENODATA) {
fprintf(stderr, "[%s] client %s disconnected\n", NAME, client.ip_string);
/* close client connection and continue waiting for new clients */
client_close(&client);
continue;
}
if ( ret < 0) {
/* print error but continue waiting for new data */
fprintf(stderr, "[%s] problem reading client\n", NAME);
continue;
}
/* pass received data to tty device */
if (tty_dev.fd != -1) {
tty_write(&tty_dev, client.data, ret);
}
}
}
if (ret == 0) {
/* do something with inactive client */
if (client.socket != -1) {
//TODO we could drop client if inactive for some time
time_t current_time = time(NULL);
if (debug_messages) {
fprintf(stderr, "[%s] client last active %u seconds ago\n", NAME,
(unsigned int) (current_time - client.last_active));
}
}
/* do something while listening for client connections */
else {
if (debug_messages) {
fprintf(stderr, "[%s] listening for client connection\n", NAME);
}
}
}
} /* END while() loop */
return (void*) 0;
}
/* MoxaNix main program loop. */
int main(int argc, char *argv[]) {
int main(int argc, char *argv[])
{
int ret;
unsigned int tcp_port = -1;
int def_conf = 0; // is default config used or from .cfg file
@ -396,7 +182,7 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "[%s] TCP port: %d, TTY device path: %s\n", NAME, tcp_port, tty_dev.path);
/* start thread that handles tty device */
/* start thread function that handles tty device */
resources_t r = {&server, &client, &new_client, &tty_dev};
ret = pthread_create(&tty_thread, NULL, thread_tty_data, &r);
if (ret) {
@ -406,7 +192,7 @@ int main(int argc, char *argv[]) {
}
/* start thread function (in this thread) that handles client data */
thread_client_data(NULL);
thread_client_data(&r);
/* unexpected break from client data loop */
fprintf(stderr, "[%s] unexpected condition\n", NAME);

View file

@ -5,7 +5,7 @@
#include <common.h>
#include <client.h>
/* The serial-server scenario makes sense only for 1 connected client.
/* The serial-server scenario only makes sense for 1 connected client.
* Allow 1 extra connection to reject new clients with an explanation. */
#define SERVER_MAX_CONNECTIONS 2

264
task_threads.c Normal file
View file

@ -0,0 +1,264 @@
#include <task_threads.h>
#include <telnet.h>
#define NAME "moxerver"
void* thread_new_client_connection(void *args)
{
client_t temp_client;
char msg[BUFFER_LEN];
char timestamp[TIMESTAMP_LEN];
/* get resources from args */
resources_t *r = (resources_t*) args;
/* accept new connection request */
if (server_accept(r->server, &temp_client) != 0)
{
return (void *) -1;
}
/* if there is already a new client request being handled then reject this one */
if (r->new_client->socket != -1)
{
sprintf(msg, "\nToo many connection requests, please try later.\n");
send(temp_client.socket, msg, strlen(msg), 0);
client_close(&temp_client);
time2string(time(NULL), timestamp);
fprintf(stderr, "[%s] rejected new client request %s @ %s\n", __func__,
temp_client.ip_string, timestamp);
return (void *) 0;
}
/* otherwise the next step depends on the status of the current client */
else
{
/* if no client is connected then immediately accept the new client */
if (r->client->socket == -1)
{
memcpy(r->new_client, &temp_client, sizeof(client_t));
return (void *) 0;
}
else
{
/* Reaching this point means there is already a connected client
* but this request could be accepted. Ask the new client if the
* current client connection should be dropped. */
/* temporarily accept the new client */
memcpy(r->new_client, &temp_client, sizeof(client_t));
/* inform the new client that the port is already in use */
time2string(r->client->last_active, timestamp);
sprintf(msg, "\nPort %u is already being used!\n"
"Current user and last activity:\n%s @ %s\n",
r->server->port, r->client->username, timestamp);
send(r->new_client->socket, msg, strlen(msg), 0);
/* ask the new client if the current client should be dropped */
sprintf(msg, "\nDo you want to drop the current user?\n"
"If yes then please type YES DROP (in uppercase):\n");
send(r->new_client->socket, msg, strlen(msg), 0);
/* wait for new client input */
client_wait_line(r->new_client);
/* check new client confirmation */
if (strncmp(r->new_client->data, "YES DROP", 8) == 0)
{
/* drop the currently connected client, it will automatically
* connect this new client */
client_close(r->client);
fprintf(stderr, "[%s] dropped client %s @ %s\n", __func__,
r->client->ip_string, timestamp);
}
else
{
/* reject this client request */
client_close(r->new_client);
time2string(time(NULL), timestamp);
fprintf(stderr, "[%s] rejected new client request %s @ %s\n", __func__,
temp_client.ip_string, timestamp);
return (void *) 1;
}
}
}
return (void *) 0;
}
void* thread_tty_data(void *args)
{
struct timeval tv;
fd_set read_fds;
int ret;
/* get resources from args */
resources_t *r = (resources_t*) args;
fprintf(stderr, "[%s] tty thread started with device: %s\n",
NAME, r->tty_dev->path);
/* loop with timeouts waiting for data from the tty device */
while (1)
{
/* set parameters for select() */
tv.tv_sec = TTY_WAIT_TIMEOUT;
tv.tv_usec = 0;
FD_ZERO(&read_fds);
FD_SET(r->tty_dev->fd, &read_fds);
/* wait with select() */
ret = select(r->tty_dev->fd + 1, &read_fds, NULL, NULL, &tv);
if ( (ret > 0) && (FD_ISSET(r->tty_dev->fd, &read_fds)) )
{
/* pass data from tty device to client */
ret = tty_read(r->tty_dev);
client_write(r->client, r->tty_dev->data, ret);
}
if (debug_messages)
{
fprintf(stderr, "[%s] tty thread alive\n", NAME);
}
} /* end main while() loop */
fprintf(stderr, "[%s] tty thread stopped\n", NAME);
return (void*) 0;
}
void* thread_client_data(void *args)
{
struct timeval tv;
fd_set read_fds;
int fdmax;
int ret;
pthread_t new_client_thread;
/* get resources from args */
resources_t *r = (resources_t*) args;
/* loop with timeouts waiting for client data or new connection requests */
while (1)
{
/* check if there is no connected client, but a new client is available */
if ( (r->client->socket == -1) && (r->new_client->socket != -1) )
{
/* copy new client information */
memcpy(r->client, r->new_client, sizeof(client_t));
r->new_client->socket = -1;
fprintf(stderr, "[%s] client %s connected\n", NAME, r->client->ip_string);
/* ask client to provide a username before going to "character" mode */
if (client_ask_username(r->client) != 0)
{
/* close client if not able to provide a username */
client_close(r->client);
continue;
}
/* put client in "character" mode */
char msg[TELNET_MSG_LEN_CHARMODE];
telnet_message_set_character_mode(msg);
client_write(r->client, msg, TELNET_MSG_LEN_CHARMODE);
}
/* setup parameters for select() */
tv.tv_sec = SERVER_WAIT_TIMEOUT;
tv.tv_usec = 0;
FD_ZERO(&read_fds);
/* always wait for new connections on server socket */
FD_SET(r->server->socket, &read_fds);
/* wait for client only if connected */
if (r->client->socket != -1)
{
FD_SET(r->client->socket, &read_fds);
}
fdmax = (r->server->socket > r->client->socket) ? r->server->socket : r->client->socket;
/* wait with select() */
ret = select(fdmax+1, &read_fds, NULL, NULL, &tv);
/* handle errors from select() */
if (ret == -1)
{
//TODO do we really break here and stop server when select returns an error?
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno));
break;
}
/* handle incoming data on server and client sockets */
if (ret > 0)
{
/* check for new connection requests */
if (FD_ISSET(r->server->socket, &read_fds))
{
fprintf(stderr, "[%s] received client connection request\n", NAME);
/* handle new client connection request in a separate thread */
if (pthread_create(&new_client_thread, NULL, thread_new_client_connection, r) != 0)
{
/* print error but continue waiting for connection requests */
fprintf(stderr, "[%s] problem with handling client connection request\n", NAME);
continue;
}
}
/* check client status only if connected */
if ( (r->client->socket != -1) && FD_ISSET(r->client->socket, &read_fds) )
{
/* read client data */
ret = client_read(r->client);
/* check if client disconnected */
if (ret == -ENODATA)
{
fprintf(stderr, "[%s] client %s disconnected\n", NAME, r->client->ip_string);
/* close client connection and continue waiting for new clients */
client_close(r->client);
continue;
}
/* ignore read errors */
if (ret < 0)
{
/* print error but continue waiting for new data */
fprintf(stderr, "[%s] problem reading client\n", NAME);
continue;
}
/* otherwise, pass received client data to the tty device */
else
{
if (r->tty_dev->fd != -1)
{
tty_write(r->tty_dev, r->client->data, ret);
}
}
}
}
/* handle timeout from select() */
if (ret == 0)
{
/* do something with inactive client */
if (r->client->socket != -1)
{
//TODO we could drop client if inactive for some time
time_t current_time = time(NULL);
if (debug_messages)
{
fprintf(stderr, "[%s] client last active %u seconds ago\n",
NAME, (unsigned int) (current_time - r->client->last_active));
}
}
/* do something while listening for client connections? */
else
{
if (debug_messages)
{
fprintf(stderr, "[%s] listening for client connection\n", NAME);
}
}
}
} /* end main while() loop */
return (void*) 0;
}

View file

@ -1,22 +1,15 @@
/* Thread functions for handling top level tasks. */
#pragma once
#include <common.h>
#include <telnet.h>
#include <client.h>
#include <server.h>
#include <tty.h>
#define CONFILE "moxanix.cfg"
#include <pthread.h>
#define SERVER_WAIT_TIMEOUT 2 /* seconds for select() timeout in server loop */
#define TTY_WAIT_TIMEOUT 5 /* seconds for select() timeout in tty loop */
/* Global variables used throughout the application. */
server_t server; /* main server */
client_t client; /* connected client */
client_t new_client; /* reserved for a new client request */
tty_t tty_dev; /* connected tty device */
typedef struct
{
server_t *server;
@ -28,7 +21,7 @@ typedef struct
/**
* The thread function handling new client connections.
*
* If there is no connected clients then the first client request is accepted.
* If there is no connected client then the first client request is accepted.
* If there is a connected client then the new client is asked if the currently
* connected client should be dropped.
*
@ -43,6 +36,8 @@ void* thread_new_client_connection(void *args);
/**
* The thread function handling data from the tty device.
*
* The incoming tty device data is sent directly to the connected client.
*
* The function handles global resources through the pointer to a "resources_t"
* structure passed as the input argument.
*
@ -54,6 +49,8 @@ void* thread_tty_data(void *args);
/**
* The thread function handling data from the connected client.
*
* The incoming client data is sent directly to the tty device.
*
* The function handles global resources through the pointer to a "resources_t"
* structure passed as the input argument.
*