Use a wrapper for logging
This commit is contained in:
parent
8725d1c1f1
commit
7973b5a5e8
7 changed files with 91 additions and 99 deletions
16
client.c
16
client.c
|
@ -13,8 +13,7 @@ void client_close(client_t *client)
|
||||||
client->socket = -1;
|
client->socket = -1;
|
||||||
|
|
||||||
time2string(time(NULL), timestamp);
|
time2string(time(NULL), timestamp);
|
||||||
fprintf(stderr,"[%s] socket closed for client %s @ %s\n", __func__,
|
LOG("socket closed for client %s @ %s", client->ip_string, timestamp);
|
||||||
client->ip_string, timestamp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int client_read(client_t *client)
|
int client_read(client_t *client)
|
||||||
|
@ -25,15 +24,13 @@ int client_read(client_t *client)
|
||||||
len = recv(client->socket, client->data, sizeof(client->data)-1, 0);
|
len = recv(client->socket, client->data, sizeof(client->data)-1, 0);
|
||||||
if (len == -1)
|
if (len == -1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
|
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||||
errno, strerror(errno));
|
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
/* a disconnected client socket is ready for reading but read returns 0 */
|
/* a disconnected client socket is ready for reading but read returns 0 */
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s:%d] no data available from client\n",
|
LOG("[@%d] no data available from client", __LINE__);
|
||||||
__func__, __LINE__);
|
|
||||||
return -ENODATA;
|
return -ENODATA;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +40,7 @@ int client_read(client_t *client)
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < len; i++)
|
for(i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "client %s <- %u '%c'\n",
|
LOG("client %s <- %u '%c'",
|
||||||
client->ip_string,
|
client->ip_string,
|
||||||
(unsigned char) client->data[i],
|
(unsigned char) client->data[i],
|
||||||
(unsigned char) client->data[i]);
|
(unsigned char) client->data[i]);
|
||||||
|
@ -72,7 +69,7 @@ int client_write(client_t *client, char *databuf, int datalen)
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < datalen; i++)
|
for(i = 0; i < datalen; i++)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "client %s -> %u '%c'\n",
|
LOG("client %s -> %u '%c'",
|
||||||
client->ip_string,
|
client->ip_string,
|
||||||
(unsigned char) databuf[i],
|
(unsigned char) databuf[i],
|
||||||
(unsigned char) databuf[i]);
|
(unsigned char) databuf[i]);
|
||||||
|
@ -83,8 +80,7 @@ int client_write(client_t *client, char *databuf, int datalen)
|
||||||
len = send(client->socket, databuf, datalen, 0);
|
len = send(client->socket, databuf, datalen, 0);
|
||||||
if (len == -1)
|
if (len == -1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
|
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||||
errno, strerror(errno));
|
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
common.h
14
common.h
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdarg.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
@ -12,12 +13,25 @@
|
||||||
|
|
||||||
/* ========================================================================== */
|
/* ========================================================================== */
|
||||||
|
|
||||||
|
#define APPNAME "moxerver"
|
||||||
#define BUFFER_LEN 128 /* length of a data buffer */
|
#define BUFFER_LEN 128 /* length of a data buffer */
|
||||||
|
|
||||||
/* ========================================================================== */
|
/* ========================================================================== */
|
||||||
|
|
||||||
int debug_messages; /* if > 0 debug messages will be printed */
|
int debug_messages; /* if > 0 debug messages will be printed */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper for printing a log message to stderr.
|
||||||
|
* Uses a "printf" syntax with format and arguments. The newline character '\n'
|
||||||
|
* is appended to the message.
|
||||||
|
*/
|
||||||
|
#define LOG(...) \
|
||||||
|
do { \
|
||||||
|
fprintf(stderr, "[%s][%s] ", APPNAME, __func__); \
|
||||||
|
fprintf(stderr, __VA_ARGS__); \
|
||||||
|
fprintf(stderr, "\n"); \
|
||||||
|
} while(0)
|
||||||
|
|
||||||
/* ========================================================================== */
|
/* ========================================================================== */
|
||||||
|
|
||||||
#define TIMESTAMP_FORMAT "%Y-%m-%dT%H:%M:%S" /* follow ISO 8601 format */
|
#define TIMESTAMP_FORMAT "%Y-%m-%dT%H:%M:%S" /* follow ISO 8601 format */
|
||||||
|
|
37
moxerver.c
37
moxerver.c
|
@ -9,8 +9,6 @@
|
||||||
#include <parser.h>
|
#include <parser.h>
|
||||||
#include <signal.h> /* handling quit signals */
|
#include <signal.h> /* handling quit signals */
|
||||||
|
|
||||||
#define NAME "moxerver"
|
|
||||||
|
|
||||||
#define CONFILE "moxanix.cfg"
|
#define CONFILE "moxanix.cfg"
|
||||||
|
|
||||||
/* ========================================================================== */
|
/* ========================================================================== */
|
||||||
|
@ -27,7 +25,7 @@ tty_t tty_dev; /* connected tty device */
|
||||||
static void usage()
|
static void usage()
|
||||||
{
|
{
|
||||||
//TODO maybe some styling should be done
|
//TODO maybe some styling should be done
|
||||||
fprintf(stdout, "Usage: %s -p tcp_port [-t tty_path] [-d] [-h]\n", NAME);
|
fprintf(stdout, "Usage: %s -p tcp_port [-t tty_path] [-d] [-h]\n", APPNAME);
|
||||||
fprintf(stdout, "\t-t\ttty dev path (if not specified %s needs to be defined)\n", CONFILE);
|
fprintf(stdout, "\t-t\ttty dev path (if not specified %s needs to be defined)\n", CONFILE);
|
||||||
fprintf(stdout, "\t-d\tturns on debug messages\n");
|
fprintf(stdout, "\t-d\tturns on debug messages\n");
|
||||||
fprintf(stdout, "\n");
|
fprintf(stdout, "\n");
|
||||||
|
@ -36,7 +34,7 @@ static void usage()
|
||||||
/* Performs resource cleanup. */
|
/* Performs resource cleanup. */
|
||||||
void cleanup()
|
void cleanup()
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] performing cleanup\n", NAME);
|
LOG("performing cleanup");
|
||||||
|
|
||||||
// TODO: maybe pthread_kill() should be used for thread cleanup?
|
// TODO: maybe pthread_kill() should be used for thread cleanup?
|
||||||
|
|
||||||
|
@ -58,7 +56,7 @@ void cleanup()
|
||||||
void quit_handler(int signum)
|
void quit_handler(int signum)
|
||||||
{
|
{
|
||||||
/* perform cleanup and exit with 0 */
|
/* perform cleanup and exit with 0 */
|
||||||
fprintf(stderr, "[%s] received signal %d\n", NAME, signum);
|
LOG("received signal %d", signum);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,12 +67,12 @@ int parse_handler(void *user, const char *section, const char *name, const char
|
||||||
|
|
||||||
if (!strcmp(name, "speed") && (unsigned int)atoi(section) == server.port)
|
if (!strcmp(name, "speed") && (unsigned int)atoi(section) == server.port)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] setting %s speed for port %s\n", __func__, value, section);
|
LOG("setting %s speed for port %s", value, section);
|
||||||
|
|
||||||
if (cfsetispeed(&(tty_dev.ttyset), baud_to_speed(atoi(value))) < 0 ||
|
if (cfsetispeed(&(tty_dev.ttyset), baud_to_speed(atoi(value))) < 0 ||
|
||||||
cfsetospeed(&(tty_dev.ttyset), baud_to_speed(atoi(value))) < 0)
|
cfsetospeed(&(tty_dev.ttyset), baud_to_speed(atoi(value))) < 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] error configuring tty device speed\n", NAME);
|
LOG("error configuring tty device speed");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,7 +102,7 @@ int main(int argc, char *argv[])
|
||||||
/* zero init tty_dev */
|
/* zero init tty_dev */
|
||||||
if (cfsetispeed(&(tty_dev.ttyset), B0) < 0 ||
|
if (cfsetispeed(&(tty_dev.ttyset), B0) < 0 ||
|
||||||
cfsetospeed(&(tty_dev.ttyset), B0) < 0) {
|
cfsetospeed(&(tty_dev.ttyset), B0) < 0) {
|
||||||
fprintf(stderr, "[%s] error configuring tty device speed\n", NAME);
|
LOG("error configuring tty device speed");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,7 +128,7 @@ int main(int argc, char *argv[])
|
||||||
case 't':
|
case 't':
|
||||||
if ((strnlen(optarg, TTY_DEV_PATH_LEN) == 0) ||
|
if ((strnlen(optarg, TTY_DEV_PATH_LEN) == 0) ||
|
||||||
(strnlen(optarg, TTY_DEV_PATH_LEN) > (TTY_DEV_PATH_LEN - 1))) {
|
(strnlen(optarg, TTY_DEV_PATH_LEN) > (TTY_DEV_PATH_LEN - 1))) {
|
||||||
fprintf(stderr, "[%s] error: tty path was not specified\n\n", NAME);
|
LOG("error: tty path was not specified\n");
|
||||||
usage();
|
usage();
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
|
@ -148,7 +146,7 @@ int main(int argc, char *argv[])
|
||||||
usage();
|
usage();
|
||||||
return 0;
|
return 0;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "[%s] error parsing arguments\n", NAME);
|
LOG("error parsing arguments");
|
||||||
usage();
|
usage();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -162,36 +160,35 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
/* parse config file if any */
|
/* parse config file if any */
|
||||||
if (!def_conf && ((ret = ini_parse(CONFILE, &parse_handler, NULL)) == -1)) {
|
if (!def_conf && ((ret = ini_parse(CONFILE, &parse_handler, NULL)) == -1)) {
|
||||||
fprintf(stderr, "[%s] error opening config file %s\n", NAME, CONFILE);
|
LOG("error opening config file %s", CONFILE);
|
||||||
usage();
|
usage();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
else if (!def_conf && ret) {
|
else if (!def_conf && ret) {
|
||||||
fprintf(stderr, "[%s] error parsing config file %s on line %d\n", NAME, CONFILE, ret);
|
LOG("error parsing config file %s on line %d", CONFILE, ret);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!strcmp(tty_dev.path, "")) {
|
if (!strcmp(tty_dev.path, "")) {
|
||||||
fprintf(stderr, "[%s] error: no tty device path given for TCP port: %d\n"
|
LOG("error: no tty device path given for TCP port: %d\n"
|
||||||
"\t\t-> check config file %s\n", NAME, tcp_port, CONFILE);
|
"\t\t-> check config file %s", tcp_port, CONFILE);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* open tty device */
|
/* open tty device */
|
||||||
if (tty_open(&tty_dev) < 0) {
|
if (tty_open(&tty_dev) < 0) {
|
||||||
fprintf(stderr, "[%s] error: opening of tty device at %s failed\n"
|
LOG("error: opening of tty device at %s failed\n"
|
||||||
"\t\t-> continuing in echo mode\n", NAME, tty_dev.path);
|
"\t\t-> continuing in echo mode", tty_dev.path);
|
||||||
debug_messages = 1;
|
debug_messages = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "[%s] TCP port: %d, TTY device path: %s\n", NAME, tcp_port, tty_dev.path);
|
LOG("TCP port: %d, TTY device path: %s", tcp_port, tty_dev.path);
|
||||||
|
|
||||||
/* start thread function that handles tty device */
|
/* start thread function that handles tty device */
|
||||||
resources_t r = {&server, &client, &new_client, &tty_dev};
|
resources_t r = {&server, &client, &new_client, &tty_dev};
|
||||||
ret = pthread_create(&tty_thread, NULL, thread_tty_data, &r);
|
ret = pthread_create(&tty_thread, NULL, thread_tty_data, &r);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
fprintf(stderr, "[%s] error starting serial monitor thread"
|
LOG("error starting serial monitor thread, pthread_create returned %d", ret);
|
||||||
", pthread_create returned %d\n", NAME, ret);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +196,7 @@ int main(int argc, char *argv[])
|
||||||
thread_client_data(&r);
|
thread_client_data(&r);
|
||||||
|
|
||||||
/* unexpected break from client data loop, cleanup and exit with -1 */
|
/* unexpected break from client data loop, cleanup and exit with -1 */
|
||||||
fprintf(stderr, "[%s] unexpected condition\n", NAME);
|
LOG("unexpected condition");
|
||||||
cleanup();
|
cleanup();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
32
server.c
32
server.c
|
@ -16,52 +16,47 @@ int server_setup(server_t *server, unsigned int port)
|
||||||
server->socket = socket(AF_INET, SOCK_STREAM, 0);
|
server->socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (server->socket == -1)
|
if (server->socket == -1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
|
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||||
errno, strerror(errno));
|
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
fprintf(stderr,"[%s] socket created\n", __func__);
|
LOG("socket created");
|
||||||
|
|
||||||
/* try to avoid "Address already in use" error */
|
/* try to avoid "Address already in use" error */
|
||||||
opt = 1; /* true value for setsockopt option */
|
opt = 1; /* true value for setsockopt option */
|
||||||
if (setsockopt(server->socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) == -1)
|
if (setsockopt(server->socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) == -1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
|
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||||
errno, strerror(errno));
|
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
/* turn off Nagle algorithm */
|
/* turn off Nagle algorithm */
|
||||||
opt = 1; /* true value for setsockopt option */
|
opt = 1; /* true value for setsockopt option */
|
||||||
if (setsockopt(server->socket, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(int)) == -1)
|
if (setsockopt(server->socket, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(int)) == -1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
|
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||||
errno, strerror(errno));
|
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* bind server address to a socket */
|
/* bind server address to a socket */
|
||||||
if (bind(server->socket, (struct sockaddr *) &server->address, sizeof(server->address)) == -1)
|
if (bind(server->socket, (struct sockaddr *) &server->address, sizeof(server->address)) == -1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
|
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||||
errno, strerror(errno));
|
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
fprintf(stderr,"[%s] bind successful\n", __func__);
|
LOG("bind successful");
|
||||||
|
|
||||||
/* save server port number */
|
/* save server port number */
|
||||||
server->port = port;
|
server->port = port;
|
||||||
fprintf(stderr,"[%s] assigned port %u\n", __func__, server->port); // ntohs(server->address.sin_port)
|
LOG("assigned port %u", server->port); // ntohs(server->address.sin_port)
|
||||||
|
|
||||||
/* listen for a client connection, allow (max-1) connections in queue */
|
/* listen for a client connection, allow (max-1) connections in queue */
|
||||||
if (listen(server->socket, (SERVER_MAX_CONNECTIONS - 1)) == -1)
|
if (listen(server->socket, (SERVER_MAX_CONNECTIONS - 1)) == -1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
|
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||||
errno, strerror(errno));
|
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
time2string(time(NULL), timestamp);
|
time2string(time(NULL), timestamp);
|
||||||
fprintf(stderr,"[%s] server is up @ %s\n", __func__, timestamp);
|
LOG("server is up @ %s", timestamp);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,7 +71,7 @@ void server_close(server_t *server)
|
||||||
}
|
}
|
||||||
|
|
||||||
time2string(time(NULL), timestamp);
|
time2string(time(NULL), timestamp);
|
||||||
fprintf(stderr,"[%s] socket closed, server is down @ %s\n", __func__, timestamp);
|
LOG("socket closed, server is down @ %s", timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
int server_accept(server_t *server, client_t *accepted_client)
|
int server_accept(server_t *server, client_t *accepted_client)
|
||||||
|
@ -89,13 +84,13 @@ int server_accept(server_t *server, client_t *accepted_client)
|
||||||
accepted_client->socket = accept(server->socket, (struct sockaddr *) &accepted_client->address, (socklen_t *) &namelen);
|
accepted_client->socket = accept(server->socket, (struct sockaddr *) &accepted_client->address, (socklen_t *) &namelen);
|
||||||
if (accepted_client->socket == -1)
|
if (accepted_client->socket == -1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno));
|
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
/* make the client socket non-blocking */
|
/* make the client socket non-blocking */
|
||||||
if (fcntl(accepted_client->socket, F_SETFL, O_NONBLOCK) == -1)
|
if (fcntl(accepted_client->socket, F_SETFL, O_NONBLOCK) == -1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno));
|
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +104,6 @@ int server_accept(server_t *server, client_t *accepted_client)
|
||||||
|
|
||||||
/* print client information */
|
/* print client information */
|
||||||
time2string(accepted_client->last_active, timestamp);
|
time2string(accepted_client->last_active, timestamp);
|
||||||
fprintf(stderr, "[%s] accepted client %s @ %s\n",
|
LOG("accepted client %s @ %s", accepted_client->ip_string, timestamp);
|
||||||
__func__, accepted_client->ip_string, timestamp);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
#include <task_threads.h>
|
#include <task_threads.h>
|
||||||
#include <telnet.h>
|
#include <telnet.h>
|
||||||
|
|
||||||
#define NAME "moxerver"
|
|
||||||
|
|
||||||
void* thread_new_client_connection(void *args)
|
void* thread_new_client_connection(void *args)
|
||||||
{
|
{
|
||||||
client_t temp_client;
|
client_t temp_client;
|
||||||
|
@ -27,8 +25,7 @@ void* thread_new_client_connection(void *args)
|
||||||
client_close(&temp_client);
|
client_close(&temp_client);
|
||||||
|
|
||||||
time2string(time(NULL), timestamp);
|
time2string(time(NULL), timestamp);
|
||||||
fprintf(stderr, "[%s] rejected new client request %s @ %s\n", __func__,
|
LOG("rejected new client request %s @ %s", temp_client.ip_string, timestamp);
|
||||||
temp_client.ip_string, timestamp);
|
|
||||||
|
|
||||||
return (void *) 0;
|
return (void *) 0;
|
||||||
}
|
}
|
||||||
|
@ -72,8 +69,7 @@ void* thread_new_client_connection(void *args)
|
||||||
* connect this new client */
|
* connect this new client */
|
||||||
client_close(r->client);
|
client_close(r->client);
|
||||||
|
|
||||||
fprintf(stderr, "[%s] dropped client %s @ %s\n", __func__,
|
LOG("dropped client %s @ %s", r->client->ip_string, timestamp);
|
||||||
r->client->ip_string, timestamp);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -81,8 +77,7 @@ void* thread_new_client_connection(void *args)
|
||||||
client_close(r->new_client);
|
client_close(r->new_client);
|
||||||
|
|
||||||
time2string(time(NULL), timestamp);
|
time2string(time(NULL), timestamp);
|
||||||
fprintf(stderr, "[%s] rejected new client request %s @ %s\n", __func__,
|
LOG("rejected new client request %s @ %s", temp_client.ip_string, timestamp);
|
||||||
temp_client.ip_string, timestamp);
|
|
||||||
|
|
||||||
return (void *) 1;
|
return (void *) 1;
|
||||||
}
|
}
|
||||||
|
@ -101,8 +96,7 @@ void* thread_tty_data(void *args)
|
||||||
/* get resources from args */
|
/* get resources from args */
|
||||||
resources_t *r = (resources_t*) args;
|
resources_t *r = (resources_t*) args;
|
||||||
|
|
||||||
fprintf(stderr, "[%s] tty thread started with device: %s\n",
|
LOG("tty thread started with device: %s", r->tty_dev->path);
|
||||||
NAME, r->tty_dev->path);
|
|
||||||
|
|
||||||
/* loop with timeouts waiting for data from the tty device */
|
/* loop with timeouts waiting for data from the tty device */
|
||||||
while (1)
|
while (1)
|
||||||
|
@ -125,11 +119,11 @@ void* thread_tty_data(void *args)
|
||||||
|
|
||||||
if (debug_messages)
|
if (debug_messages)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] tty thread alive\n", NAME);
|
LOG("tty thread alive");
|
||||||
}
|
}
|
||||||
} /* end main while() loop */
|
} /* end main while() loop */
|
||||||
|
|
||||||
fprintf(stderr, "[%s] tty thread stopped\n", NAME);
|
LOG("tty thread stopped");
|
||||||
|
|
||||||
return (void*) 0;
|
return (void*) 0;
|
||||||
}
|
}
|
||||||
|
@ -154,7 +148,7 @@ void* thread_client_data(void *args)
|
||||||
/* copy new client information */
|
/* copy new client information */
|
||||||
memcpy(r->client, r->new_client, sizeof(client_t));
|
memcpy(r->client, r->new_client, sizeof(client_t));
|
||||||
r->new_client->socket = -1;
|
r->new_client->socket = -1;
|
||||||
fprintf(stderr, "[%s] client %s connected\n", NAME, r->client->ip_string);
|
LOG("client %s connected", r->client->ip_string);
|
||||||
/* ask client to provide a username before going to "character" mode */
|
/* ask client to provide a username before going to "character" mode */
|
||||||
if (client_ask_username(r->client) != 0)
|
if (client_ask_username(r->client) != 0)
|
||||||
{
|
{
|
||||||
|
@ -187,7 +181,7 @@ void* thread_client_data(void *args)
|
||||||
if (ret == -1)
|
if (ret == -1)
|
||||||
{
|
{
|
||||||
//TODO do we really break here and stop server when select returns an error?
|
//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));
|
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/* handle incoming data on server and client sockets */
|
/* handle incoming data on server and client sockets */
|
||||||
|
@ -196,12 +190,12 @@ void* thread_client_data(void *args)
|
||||||
/* check for new connection requests */
|
/* check for new connection requests */
|
||||||
if (FD_ISSET(r->server->socket, &read_fds))
|
if (FD_ISSET(r->server->socket, &read_fds))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] received client connection request\n", NAME);
|
LOG("received client connection request");
|
||||||
/* handle new client connection request in a separate thread */
|
/* handle new client connection request in a separate thread */
|
||||||
if (pthread_create(&new_client_thread, NULL, thread_new_client_connection, r) != 0)
|
if (pthread_create(&new_client_thread, NULL, thread_new_client_connection, r) != 0)
|
||||||
{
|
{
|
||||||
/* print error but continue waiting for connection requests */
|
/* print error but continue waiting for connection requests */
|
||||||
fprintf(stderr, "[%s] problem with handling client connection request\n", NAME);
|
LOG("problem with handling client connection request");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -213,7 +207,7 @@ void* thread_client_data(void *args)
|
||||||
/* check if client disconnected */
|
/* check if client disconnected */
|
||||||
if (ret == -ENODATA)
|
if (ret == -ENODATA)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] client %s disconnected\n", NAME, r->client->ip_string);
|
LOG("client %s disconnected", r->client->ip_string);
|
||||||
/* close client connection and continue waiting for new clients */
|
/* close client connection and continue waiting for new clients */
|
||||||
client_close(r->client);
|
client_close(r->client);
|
||||||
continue;
|
continue;
|
||||||
|
@ -222,7 +216,7 @@ void* thread_client_data(void *args)
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
/* print error but continue waiting for new data */
|
/* print error but continue waiting for new data */
|
||||||
fprintf(stderr, "[%s] problem reading client\n", NAME);
|
LOG("problem reading from client, but will continue");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
/* otherwise, pass received client data to the tty device */
|
/* otherwise, pass received client data to the tty device */
|
||||||
|
@ -245,8 +239,8 @@ void* thread_client_data(void *args)
|
||||||
time_t current_time = time(NULL);
|
time_t current_time = time(NULL);
|
||||||
if (debug_messages)
|
if (debug_messages)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] client last active %u seconds ago\n",
|
LOG("client last active %u seconds ago",
|
||||||
NAME, (unsigned int) (current_time - r->client->last_active));
|
(unsigned int) (current_time - r->client->last_active));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* do something while listening for client connections? */
|
/* do something while listening for client connections? */
|
||||||
|
@ -254,7 +248,7 @@ void* thread_client_data(void *args)
|
||||||
{
|
{
|
||||||
if (debug_messages)
|
if (debug_messages)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] listening for client connection\n", NAME);
|
LOG("listening for client connection");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
6
telnet.c
6
telnet.c
|
@ -59,7 +59,7 @@ static void telnet_handle_command(char *databuf, int datalen)
|
||||||
* we set the client, we don't adapt to client commands */
|
* we set the client, we don't adapt to client commands */
|
||||||
if (databuf[0] == telnet_option_value("IAC"))
|
if (databuf[0] == telnet_option_value("IAC"))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] received %s %s\n", __func__,
|
LOG("received %s %s",
|
||||||
telnet_option_name(databuf[1]),
|
telnet_option_name(databuf[1]),
|
||||||
telnet_option_name(databuf[2]));
|
telnet_option_name(databuf[2]));
|
||||||
}
|
}
|
||||||
|
@ -127,14 +127,14 @@ void telnet_filter_client_write(char *databuf, int *datalen)
|
||||||
/* pressed ENTER */
|
/* pressed ENTER */
|
||||||
if (databuf[i] == 13)
|
if (databuf[i] == 13)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] handling ENTER\n", __func__);
|
LOG("handling ENTER");
|
||||||
newdata[newlen++] = '\r';
|
newdata[newlen++] = '\r';
|
||||||
newdata[newlen++] = '\n';
|
newdata[newlen++] = '\n';
|
||||||
}
|
}
|
||||||
/* pressed BACKSPACE */
|
/* pressed BACKSPACE */
|
||||||
if (databuf[i] == 127)
|
if (databuf[i] == 127)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] handling BACKSPACE\n", __func__);
|
LOG("handling BACKSPACE");
|
||||||
newdata[newlen++] = 8;
|
newdata[newlen++] = 8;
|
||||||
newdata[newlen++] = ' ';
|
newdata[newlen++] = ' ';
|
||||||
newdata[newlen++] = 8;
|
newdata[newlen++] = 8;
|
||||||
|
|
25
tty.c
25
tty.c
|
@ -1,6 +1,5 @@
|
||||||
#include <tty.h>
|
#include <tty.h>
|
||||||
|
|
||||||
#define NAME "tty"
|
|
||||||
#define TTY_DEFAULT_BAUDRATE B115200
|
#define TTY_DEFAULT_BAUDRATE B115200
|
||||||
|
|
||||||
int tty_open(tty_t *tty_dev)
|
int tty_open(tty_t *tty_dev)
|
||||||
|
@ -16,8 +15,8 @@ int tty_open(tty_t *tty_dev)
|
||||||
/* store default termios settings */
|
/* store default termios settings */
|
||||||
if (tcgetattr(tty_dev->fd, &(tty_dev->ttysetold)))
|
if (tcgetattr(tty_dev->fd, &(tty_dev->ttysetold)))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] error reading device default config\n"
|
LOG("[@%d] error reading device default config\n"
|
||||||
"\t\t-> default config will not be restored upon exit", __func__);
|
"\t\t-> default config will not be restored upon exit", __LINE__);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set tty device parameters */
|
/* set tty device parameters */
|
||||||
|
@ -35,20 +34,20 @@ int tty_open(tty_t *tty_dev)
|
||||||
if (cfgetispeed(&(tty_dev->ttyset)) == baud_to_speed(0) &&
|
if (cfgetispeed(&(tty_dev->ttyset)) == baud_to_speed(0) &&
|
||||||
cfsetispeed(&(tty_dev->ttyset), TTY_DEFAULT_BAUDRATE) < 0)
|
cfsetispeed(&(tty_dev->ttyset), TTY_DEFAULT_BAUDRATE) < 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] error configuring tty device speed\n", __func__);
|
LOG("error configuring tty device speed");
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
if (cfgetospeed(&(tty_dev->ttyset)) == baud_to_speed(0) &&
|
if (cfgetospeed(&(tty_dev->ttyset)) == baud_to_speed(0) &&
|
||||||
cfsetospeed(&(tty_dev->ttyset), TTY_DEFAULT_BAUDRATE) < 0)
|
cfsetospeed(&(tty_dev->ttyset), TTY_DEFAULT_BAUDRATE) < 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] error configuring tty device speed\n", __func__);
|
LOG("error configuring tty device speed");
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* apply tty device settings */
|
/* apply tty device settings */
|
||||||
if (tcsetattr(tty_dev->fd, TCSANOW, &(tty_dev->ttyset)) < 0)
|
if (tcsetattr(tty_dev->fd, TCSANOW, &(tty_dev->ttyset)) < 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] error configuring tty device\n", __func__);
|
LOG("error configuring tty device");
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,11 +59,11 @@ int tty_close(tty_t *tty_dev)
|
||||||
int fd = tty_dev->fd;
|
int fd = tty_dev->fd;
|
||||||
tty_dev->fd = -1;
|
tty_dev->fd = -1;
|
||||||
|
|
||||||
fprintf(stderr, "[%s] closing tty device \n", __func__);
|
LOG("closing tty device");
|
||||||
|
|
||||||
if (tcsetattr(fd, TCSANOW, &(tty_dev->ttysetold)) < 0)
|
if (tcsetattr(fd, TCSANOW, &(tty_dev->ttysetold)) < 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s] error restoring tty device default config\n", __func__);
|
LOG("[@%d] error restoring tty device default config", __LINE__);
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,8 +82,7 @@ int tty_read(tty_t *tty_dev)
|
||||||
len = read(tty_dev->fd, tty_dev->data, BUFFER_LEN);
|
len = read(tty_dev->fd, tty_dev->data, BUFFER_LEN);
|
||||||
if (len == -1)
|
if (len == -1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
|
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||||
errno, strerror(errno));
|
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +92,7 @@ int tty_read(tty_t *tty_dev)
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < len; i++)
|
for(i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "tty <- %u '%c'\n",
|
LOG("tty <- %u '%c'",
|
||||||
(unsigned char) tty_dev->data[i],
|
(unsigned char) tty_dev->data[i],
|
||||||
(unsigned char) tty_dev->data[i]);
|
(unsigned char) tty_dev->data[i]);
|
||||||
}
|
}
|
||||||
|
@ -110,8 +108,7 @@ int tty_write(tty_t *tty_dev, char *databuf, int datalen)
|
||||||
len = write(tty_dev->fd, databuf, datalen);
|
len = write(tty_dev->fd, databuf, datalen);
|
||||||
if (len == -1)
|
if (len == -1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
|
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||||
errno, strerror(errno));
|
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,7 +118,7 @@ int tty_write(tty_t *tty_dev, char *databuf, int datalen)
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < datalen; i++)
|
for(i = 0; i < datalen; i++)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "tty -> %u '%c'\n",
|
LOG("tty -> %u '%c'",
|
||||||
(unsigned char) databuf[i],
|
(unsigned char) databuf[i],
|
||||||
(unsigned char) databuf[i]);
|
(unsigned char) databuf[i]);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue