From bdf36796bfdc950d0fb01f21be37e7c4bb61a031 Mon Sep 17 00:00:00 2001 From: Igor Socec Date: Wed, 19 Mar 2014 11:20:16 +0100 Subject: [PATCH] improved handling for list of telnet commands --- client.c | 5 ++++- moxerver.c | 7 ++++++- moxerver.h | 2 ++ server.c | 5 ++++- telnet.c | 27 ++++++++++++++++----------- tty.c | 4 ++++ 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/client.c b/client.c index 02c0c1c..93314cb 100644 --- a/client.c +++ b/client.c @@ -1,5 +1,8 @@ -#include "moxerver.h" +/* + * Handling communication with clients. + */ +#include "moxerver.h" /* Closes client connection. */ int client_close(struct client_t *client) { diff --git a/moxerver.c b/moxerver.c index e4d6483..842402e 100644 --- a/moxerver.c +++ b/moxerver.c @@ -1,3 +1,9 @@ +/* + * Main server application. + * Handles client connections on specific TCP port and allows bidirectional + * communication with a specific TTY device. + */ + #include "moxerver.h" #include /* handling quit signals */ #include @@ -8,7 +14,6 @@ #define PORT_MIN 4001 /* minimum TCP port number */ #define PORT_MAX 4008 /* maximum TCP port number */ - /* Prints help message. */ static void usage() { //TODO maybe some styling should be done diff --git a/moxerver.h b/moxerver.h index fac79c3..0ce11a0 100644 --- a/moxerver.h +++ b/moxerver.h @@ -140,6 +140,8 @@ int telnet_handle_client_read(char *databuf, int *datalen); int telnet_handle_client_write(char *databuf, int *datalen); +/* Functions handling communication with tty device. */ + /* Opens the tty device and configures it. */ int tty_open(struct tty_t *tty_dev); /* Closes the tty device. */ diff --git a/server.c b/server.c index aa32f17..d7236bf 100644 --- a/server.c +++ b/server.c @@ -1,5 +1,8 @@ -#include "moxerver.h" +/* + * Handling server operation. + */ +#include "moxerver.h" /* Sets up the server on specific port, binds to a socket and listens for client connections. */ int server_setup(struct server_t *server, unsigned int port) { diff --git a/telnet.c b/telnet.c index f46617f..31c8a3e 100644 --- a/telnet.c +++ b/telnet.c @@ -1,5 +1,8 @@ -#include "moxerver.h" +/* + * Handling details related to telnet protocol. + */ +#include "moxerver.h" /* structure for holding telnet option name and value */ struct telnet_option_t { @@ -17,25 +20,27 @@ struct telnet_option_t telnet_options[] = { {"ECHO", 1}, {"SGA", 3}, {"LINEMODE", 34}, + {NULL, 0} + /* this list must end with {NULL, 0} */ }; -#define TELNET_OPTIONS_COUNT 8 /* keep this up with the number of supported options */ -//TODO implement this list with last element being NULL so we don't need to keep count /* Returns telnet option name based on the value. */ static const char* telnet_option_name(int value) { - int i; - for (i = 0; i < TELNET_OPTIONS_COUNT; i ++) - if (telnet_options[i].value == value) - return telnet_options[i].name; + int i = 0; + while (telnet_options[i].name) { + if (telnet_options[i].value == value) return telnet_options[i].name; + i++; + } return '\0'; } /* Returns telnet option value based on the name. */ static char telnet_option_value(const char* name) { - int i; - for (i = 0; i < TELNET_OPTIONS_COUNT; i ++) - if (!strcmp(telnet_options[i].name, name)) - return telnet_options[i].value; + int i = 0; + while (telnet_options[i].name) { + if (!strcmp(telnet_options[i].name, name)) return telnet_options[i].value; + i++; + } return 0; } diff --git a/tty.c b/tty.c index 410d438..cd3d2c2 100644 --- a/tty.c +++ b/tty.c @@ -1,3 +1,7 @@ +/* + * Handling communication with tty device. + */ + #include "moxerver.h" #define TTY_THREAD_TIMEOUT_SEC 30