Improve source tree
This commit is contained in:
parent
61d4e2fc64
commit
efc292af2c
17 changed files with 77 additions and 35 deletions
54
moxerver/Makefile
Normal file
54
moxerver/Makefile
Normal file
|
@ -0,0 +1,54 @@
|
|||
# target name
|
||||
TARGET = moxerver
|
||||
|
||||
# directory for build results
|
||||
BUILDDIR = build.dir
|
||||
|
||||
# ==============================================================================
|
||||
|
||||
# add include directories
|
||||
INCDIRS = -I.
|
||||
# add library directories
|
||||
LIBDIRS = -L.
|
||||
# list used libraries
|
||||
#LIBS = -lm
|
||||
LIBS = -lpthread
|
||||
|
||||
# ==============================================================================
|
||||
|
||||
# compiler and flags
|
||||
CC = gcc
|
||||
CFLAGS = -Wall $(INCDIRS) $(LIBDIRS) $(LIBS)
|
||||
|
||||
# ==============================================================================
|
||||
|
||||
# build everything in a dedicated directory $(OUTDIR)
|
||||
|
||||
# objects are .o files created from all .c files in the directory (same name)
|
||||
OBJECTS = $(patsubst %.c, $(BUILDDIR)/%.o, $(wildcard *.c))
|
||||
# headers are all .h files in the directory
|
||||
HEADERS = $(wildcard *.h)
|
||||
|
||||
# all objects are built from their .c files in the directory
|
||||
$(BUILDDIR)/%.o: %.c
|
||||
mkdir -p $(BUILDDIR)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
# target is built from all object files
|
||||
$(BUILDDIR)/$(TARGET): $(OBJECTS)
|
||||
$(CC) $(OBJECTS) $(CFLAGS) -o $@
|
||||
|
||||
# ==============================================================================
|
||||
|
||||
# supported make options (clean, install...)
|
||||
.PHONY: default all clean
|
||||
|
||||
# all calls all other options
|
||||
all: default
|
||||
|
||||
# default builds target
|
||||
default: $(BUILDDIR)/$(TARGET)
|
||||
|
||||
# clean removes object files and target (ignore errors with "-" before commands)
|
||||
clean:
|
||||
-rm -rf $(BUILDDIR)
|
164
moxerver/client.c
Normal file
164
moxerver/client.c
Normal file
|
@ -0,0 +1,164 @@
|
|||
#include <client.h>
|
||||
#include <telnet.h>
|
||||
|
||||
void client_close(client_t *client)
|
||||
{
|
||||
char timestamp[TIMESTAMP_LEN];
|
||||
|
||||
/* force closing in case of error */
|
||||
if (close(client->socket) == -1)
|
||||
{
|
||||
close(client->socket);
|
||||
}
|
||||
client->socket = -1;
|
||||
|
||||
time2string(time(NULL), timestamp);
|
||||
LOG("socket closed for client %s @ %s", client->ip_string, timestamp);
|
||||
}
|
||||
|
||||
int client_read(client_t *client)
|
||||
{
|
||||
int len;
|
||||
|
||||
/* read data from the client */
|
||||
len = recv(client->socket, client->data, sizeof(client->data)-1, 0);
|
||||
if (len == -1)
|
||||
{
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
/* a disconnected client socket is ready for reading but read returns 0 */
|
||||
if (len == 0)
|
||||
{
|
||||
LOG("[@%d] no data available from client", __LINE__);
|
||||
return -ENODATA;
|
||||
}
|
||||
|
||||
//TODO let's print received bytes during development phase...
|
||||
if (debug_messages)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < len; i++)
|
||||
{
|
||||
LOG("client %s <- %u '%c'",
|
||||
client->ip_string,
|
||||
(unsigned char) client->data[i],
|
||||
(unsigned char) client->data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/* handle special telnet characters coming from the client */
|
||||
telnet_filter_client_read(client->data, &len);
|
||||
|
||||
/* grab current time and store it as client's last activity */
|
||||
client->last_active = time(NULL);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
int client_write(client_t *client, char *databuf, int datalen)
|
||||
{
|
||||
int len;
|
||||
|
||||
/* handle special telnet characters to display them correctly on client */
|
||||
//telnet_filter_client_write(databuf, &datalen);
|
||||
|
||||
//TODO let's print received bytes during development phase...
|
||||
if (debug_messages)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < datalen; i++)
|
||||
{
|
||||
LOG("client %s -> %u '%c'",
|
||||
client->ip_string,
|
||||
(unsigned char) databuf[i],
|
||||
(unsigned char) databuf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/* send data to the client */
|
||||
len = send(client->socket, databuf, datalen, 0);
|
||||
if (len == -1)
|
||||
{
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
int client_wait_line(client_t *client)
|
||||
{
|
||||
fd_set read_fds;
|
||||
struct timeval tv;
|
||||
|
||||
client->data[0] = '\0';
|
||||
/* loop waiting for client input */
|
||||
while (client->data[0] == '\0')
|
||||
{
|
||||
/* setup select() parameters */
|
||||
tv.tv_sec = 15; /* 15 second timeout */
|
||||
tv.tv_usec = 0;
|
||||
FD_ZERO(&read_fds);
|
||||
FD_SET(client->socket, &read_fds);
|
||||
/* send prompt character to the client */
|
||||
client_write(client, "> ", 2);
|
||||
/* block until input arrives */
|
||||
if (select((client->socket)+1, &read_fds, NULL, NULL, &tv) <= 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (FD_ISSET(client->socket, &read_fds))
|
||||
{
|
||||
/* read client input */
|
||||
if (client_read(client) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
/* we don't want empty data so ignore data starting with \r or \n */
|
||||
if ( (client->data[0] == '\r') || (client->data[0] == '\n') )
|
||||
{
|
||||
client->data[0] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int client_ask_username(client_t *client)
|
||||
{
|
||||
int i;
|
||||
char msg[BUFFER_LEN];
|
||||
|
||||
/* show username request to the client */
|
||||
snprintf(msg, BUFFER_LEN,
|
||||
"\nPlease provide a username to identify yourself to"
|
||||
"other users (max %d characters):\n", USERNAME_LEN);
|
||||
client_write(client, msg, strlen(msg));
|
||||
|
||||
/* wait for client input */
|
||||
if (client_wait_line(client) != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* save received data as client username */
|
||||
for (i = 0; i < USERNAME_LEN; i++)
|
||||
{
|
||||
if ( (client->data[i] == '\r') || (client->data[i] == '\n') )
|
||||
{
|
||||
/* don't include \r or \n in the username */
|
||||
client->username[i] = '\0';
|
||||
break;
|
||||
}
|
||||
client->username[i] = client->data[i];
|
||||
}
|
||||
|
||||
/* show welcome message to client */
|
||||
snprintf(msg, BUFFER_LEN,
|
||||
"\nWelcome %s!\n\n", client->username);
|
||||
client_write(client, msg, strlen(msg));
|
||||
|
||||
return 0;
|
||||
}
|
63
moxerver/client.h
Normal file
63
moxerver/client.h
Normal file
|
@ -0,0 +1,63 @@
|
|||
/* Handles communication with a client. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <common.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#define USERNAME_LEN 32
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int socket; /* client socket */
|
||||
struct sockaddr_in address; /* client address information */
|
||||
char ip_string[INET_ADDRSTRLEN]; /* client IP address as a string */
|
||||
time_t last_active; /* time of client's last activity */
|
||||
char username[USERNAME_LEN]; /* username for human identification */
|
||||
char data[BUFFER_LEN]; /* buffer for received data */
|
||||
} client_t;
|
||||
|
||||
/**
|
||||
* Closes a client connection.
|
||||
*/
|
||||
void client_close(client_t *client);
|
||||
|
||||
/**
|
||||
* Reads data from a client into the client data buffer.
|
||||
* Also updates the timestamp of the client's last activity.
|
||||
*
|
||||
* Returns:
|
||||
* - number of read bytes on success,
|
||||
* - negative ENODATA value (-ENODATA) if the client has disconnected,
|
||||
* - negative errno value set by an error while reading
|
||||
*/
|
||||
int client_read(client_t *client);
|
||||
|
||||
/**
|
||||
* Sends data from a buffer to the client.
|
||||
*
|
||||
* Returns:
|
||||
* - number of sent bytes on success,
|
||||
* - negative errno value set by an error while sending
|
||||
*/
|
||||
int client_write(client_t *client, char *databuf, int datalen);
|
||||
|
||||
/**
|
||||
* Waits for input from the client in "line mode" (client sends a whole line of
|
||||
* characters). The function blocks until input arrives.
|
||||
*
|
||||
* Returns:
|
||||
* - 0 on success
|
||||
* - negative value if an error occurred
|
||||
*/
|
||||
int client_wait_line(client_t *client);
|
||||
|
||||
/**
|
||||
* Asks the client to provide a username.
|
||||
* Blocks until a string is provided.
|
||||
*
|
||||
* Returns:
|
||||
* - 0 on success
|
||||
* - negative value if an error occurred
|
||||
*/
|
||||
int client_ask_username(client_t *client);
|
43
moxerver/common.h
Normal file
43
moxerver/common.h
Normal file
|
@ -0,0 +1,43 @@
|
|||
/* Common header file reused within the project. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
/* ========================================================================== */
|
||||
|
||||
#define APPNAME "moxerver"
|
||||
#define BUFFER_LEN 128 /* length of a data buffer */
|
||||
|
||||
/* ========================================================================== */
|
||||
|
||||
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_LEN 20+1 /* size of the timestamp format above */
|
||||
|
||||
/**
|
||||
* Converts time in "seconds from Epoch" to a conveniently formatted string.
|
||||
*/
|
||||
void time2string(time_t time, char* timestamp);
|
184
moxerver/moxerver.c
Normal file
184
moxerver/moxerver.c
Normal file
|
@ -0,0 +1,184 @@
|
|||
/*
|
||||
* Main server application.
|
||||
* Handles client connections on specific TCP port and allows bidirectional
|
||||
* communication with a specific TTY device.
|
||||
*/
|
||||
|
||||
#include <common.h>
|
||||
#include <task_threads.h>
|
||||
#include <signal.h> /* handling quit signals */
|
||||
|
||||
/* ========================================================================== */
|
||||
|
||||
/* global resources */
|
||||
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 the help message. */
|
||||
static void usage()
|
||||
{
|
||||
//TODO maybe some styling should be done
|
||||
fprintf(stdout, "Usage: %s -p tcp_port -t tty_path -b baud_rate [-d] [-h]\n", APPNAME);
|
||||
fprintf(stdout, "\t-d\tturns on debug messages\n");
|
||||
fprintf(stdout, "\n");
|
||||
}
|
||||
|
||||
/* Performs resource cleanup. */
|
||||
void cleanup()
|
||||
{
|
||||
LOG("performing cleanup");
|
||||
|
||||
// TODO: maybe pthread_kill() should be used for thread cleanup?
|
||||
|
||||
/* close the client */
|
||||
if (client.socket != -1)
|
||||
{
|
||||
client_close(&client);
|
||||
}
|
||||
/* close the tty device */
|
||||
if (tty_dev.fd != -1)
|
||||
{
|
||||
tty_close(&tty_dev);
|
||||
}
|
||||
/* close the server */
|
||||
server_close(&server);
|
||||
}
|
||||
|
||||
/* Handles received quit signals, use it for all quit signals of interest. */
|
||||
void quit_handler(int signum)
|
||||
{
|
||||
/* perform cleanup and exit with 0 */
|
||||
LOG("received signal %d", signum);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void time2string(time_t time, char* timestamp)
|
||||
{
|
||||
strftime(timestamp, TIMESTAMP_LEN, TIMESTAMP_FORMAT, localtime(&time));
|
||||
}
|
||||
|
||||
/* MoxaNix main program loop. */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ret;
|
||||
unsigned int tcp_port = -1;
|
||||
|
||||
pthread_t tty_thread;
|
||||
|
||||
/* initialize tty_dev */
|
||||
if (cfsetispeed(&(tty_dev.ttyset), B0) < 0 ||
|
||||
cfsetospeed(&(tty_dev.ttyset), B0) < 0)
|
||||
{
|
||||
LOG("error configuring tty device speed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* enable catching and handling some quit signals, SIGKILL can't be caught */
|
||||
signal(SIGTERM, quit_handler);
|
||||
signal(SIGQUIT, quit_handler);
|
||||
signal(SIGINT, quit_handler);
|
||||
|
||||
/* check argument count */
|
||||
if (argc <= 1)
|
||||
{
|
||||
usage();
|
||||
return -1;
|
||||
}
|
||||
/* grab arguments */
|
||||
debug_messages = 0;
|
||||
while ((ret = getopt(argc, argv, ":p:t:b:dh")) != -1)
|
||||
{
|
||||
size_t path_len;
|
||||
speed_t baudrate;
|
||||
switch (ret)
|
||||
{
|
||||
/* get server port number */
|
||||
case 'p':
|
||||
tcp_port = (unsigned int) atoi(optarg);
|
||||
if (tcp_port < 0)
|
||||
{
|
||||
LOG("error, invalid TCP port value\n");
|
||||
usage();
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
/* get tty device path */
|
||||
case 't':
|
||||
path_len = strnlen(optarg, TTY_DEV_PATH_LEN);
|
||||
/* check correct path size */
|
||||
if ((path_len == 0) || (path_len > (TTY_DEV_PATH_LEN - 1)))
|
||||
{
|
||||
LOG("error with tty path length: should be <%d\n", TTY_DEV_PATH_LEN);
|
||||
usage();
|
||||
return -1;
|
||||
}
|
||||
/* otherwise, set tty device path in tty_dev struct */
|
||||
else
|
||||
{
|
||||
strcpy(tty_dev.path, optarg);
|
||||
}
|
||||
break;
|
||||
/* get tty device baud rate */
|
||||
case 'b':
|
||||
baudrate = baud_to_speed(atoi(optarg));
|
||||
if (cfsetispeed(&(tty_dev.ttyset), baudrate) < 0 ||
|
||||
cfsetospeed(&(tty_dev.ttyset), baudrate) < 0)
|
||||
{
|
||||
LOG("error configuring tty device baud rate, check configuration");
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
/* enable debug messages */
|
||||
case 'd':
|
||||
debug_messages = 1;
|
||||
break;
|
||||
/* print help and exit */
|
||||
case 'h':
|
||||
usage();
|
||||
return 0;
|
||||
default:
|
||||
LOG("error parsing arguments");
|
||||
usage();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* start server */
|
||||
client.socket = -1;
|
||||
new_client.socket = -1;
|
||||
if (server_setup(&server, tcp_port) < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* open tty device */
|
||||
tty_dev.fd = -1;
|
||||
if (tty_open(&tty_dev) < 0)
|
||||
{
|
||||
LOG("error: opening of tty device at %s failed\n"
|
||||
"\t\t-> continuing in echo mode", tty_dev.path);
|
||||
debug_messages = 1;
|
||||
}
|
||||
|
||||
LOG("Running with TCP port: %d, TTY device path: %s", tcp_port, tty_dev.path);
|
||||
|
||||
/* 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) {
|
||||
LOG("error starting serial monitor thread, pthread_create returned %d", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* start thread function (in this thread) that handles client data */
|
||||
thread_client_data(&r);
|
||||
|
||||
/* unexpected break from client data loop, cleanup and exit with -1 */
|
||||
LOG("unexpected condition");
|
||||
cleanup();
|
||||
return -1;
|
||||
}
|
109
moxerver/server.c
Normal file
109
moxerver/server.c
Normal file
|
@ -0,0 +1,109 @@
|
|||
#include <server.h>
|
||||
#include <netinet/tcp.h> /* TCP_NODELAY */
|
||||
#include <arpa/inet.h>
|
||||
|
||||
int server_setup(server_t *server, unsigned int port)
|
||||
{
|
||||
int opt;
|
||||
char timestamp[TIMESTAMP_LEN];
|
||||
|
||||
/* set up server address information */
|
||||
server->address.sin_family = AF_INET; /* use IPv4 address family */
|
||||
server->address.sin_port = htons(port); /* set up port number (htons for network byte order) */
|
||||
server->address.sin_addr.s_addr = INADDR_ANY; /* use local address */
|
||||
|
||||
/* create stream socket using TCP */
|
||||
server->socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (server->socket == -1)
|
||||
{
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
LOG("socket created");
|
||||
|
||||
/* try to avoid "Address already in use" error */
|
||||
opt = 1; /* true value for setsockopt option */
|
||||
if (setsockopt(server->socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) == -1)
|
||||
{
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
/* turn off Nagle algorithm */
|
||||
opt = 1; /* true value for setsockopt option */
|
||||
if (setsockopt(server->socket, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(int)) == -1)
|
||||
{
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
|
||||
/* bind server address to a socket */
|
||||
if (bind(server->socket, (struct sockaddr *) &server->address, sizeof(server->address)) == -1)
|
||||
{
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
LOG("bind successful");
|
||||
|
||||
/* save server port number */
|
||||
server->port = port;
|
||||
LOG("assigned port %u", server->port); // ntohs(server->address.sin_port)
|
||||
|
||||
/* listen for a client connection, allow (max-1) connections in queue */
|
||||
if (listen(server->socket, (SERVER_MAX_CONNECTIONS - 1)) == -1)
|
||||
{
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
|
||||
time2string(time(NULL), timestamp);
|
||||
LOG("server is up @ %s", timestamp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void server_close(server_t *server)
|
||||
{
|
||||
char timestamp[TIMESTAMP_LEN];
|
||||
|
||||
/* force closing in case of error */
|
||||
if (close(server->socket) == -1)
|
||||
{
|
||||
close(server->socket);
|
||||
}
|
||||
|
||||
time2string(time(NULL), timestamp);
|
||||
LOG("socket closed, server is down @ %s", timestamp);
|
||||
}
|
||||
|
||||
int server_accept(server_t *server, client_t *accepted_client)
|
||||
{
|
||||
int namelen;
|
||||
char timestamp[TIMESTAMP_LEN];
|
||||
|
||||
/* accept connection request */
|
||||
namelen = sizeof(accepted_client->address);
|
||||
accepted_client->socket = accept(server->socket, (struct sockaddr *) &accepted_client->address, (socklen_t *) &namelen);
|
||||
if (accepted_client->socket == -1)
|
||||
{
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
/* make the client socket non-blocking */
|
||||
if (fcntl(accepted_client->socket, F_SETFL, O_NONBLOCK) == -1)
|
||||
{
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
|
||||
/* get client IP address as a human readable string */
|
||||
inet_ntop(accepted_client->address.sin_family,
|
||||
&accepted_client->address.sin_addr.s_addr,
|
||||
accepted_client->ip_string, INET_ADDRSTRLEN);
|
||||
|
||||
/* grab current time and store it as client's last activity*/
|
||||
accepted_client->last_active = time(NULL);
|
||||
|
||||
/* print client information */
|
||||
time2string(accepted_client->last_active, timestamp);
|
||||
LOG("accepted client %s @ %s", accepted_client->ip_string, timestamp);
|
||||
return 0;
|
||||
}
|
41
moxerver/server.h
Normal file
41
moxerver/server.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
/* Handles server operation. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <common.h>
|
||||
#include <client.h>
|
||||
|
||||
/* 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
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int socket; /* server socket */
|
||||
struct sockaddr_in address; /* server address information */
|
||||
unsigned int port; /* server port in host byte order */
|
||||
} server_t;
|
||||
|
||||
/**
|
||||
* Sets up the server on a specific port, binds to a socket and listens for
|
||||
* client connections.
|
||||
*
|
||||
* Returns:
|
||||
* - 0 on success,
|
||||
* - negative errno value set by an error in the setup process
|
||||
*/
|
||||
int server_setup(server_t *server, unsigned int port);
|
||||
|
||||
/**
|
||||
* Closes the server socket.
|
||||
*/
|
||||
void server_close(server_t *server);
|
||||
|
||||
/**
|
||||
* Accepts an incoming client connection.
|
||||
*
|
||||
* Returns:
|
||||
* - 0 on success,
|
||||
* - negative errno value set by an error in the process
|
||||
*/
|
||||
int server_accept(server_t *server, client_t *accepted_client);
|
259
moxerver/task_threads.c
Normal file
259
moxerver/task_threads.c
Normal file
|
@ -0,0 +1,259 @@
|
|||
#include <task_threads.h>
|
||||
#include <telnet.h>
|
||||
|
||||
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);
|
||||
LOG("rejected new client request %s @ %s", 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. */
|
||||
|
||||
/* 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(temp_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(temp_client.socket, msg, strlen(msg), 0);
|
||||
|
||||
/* wait for new client input */
|
||||
client_wait_line(&temp_client);
|
||||
|
||||
/* check new client confirmation */
|
||||
if (strncmp(temp_client.data, "YES DROP", 8) == 0)
|
||||
{
|
||||
/* drop the currently connected client */
|
||||
client_close(r->client);
|
||||
/* accept the new client */
|
||||
memcpy(r->new_client, &temp_client, sizeof(client_t));
|
||||
|
||||
LOG("dropped client %s @ %s", r->client->ip_string, timestamp);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* reject this client request */
|
||||
client_close(&temp_client);
|
||||
|
||||
time2string(time(NULL), timestamp);
|
||||
LOG("rejected new client request %s @ %s", 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;
|
||||
|
||||
LOG("tty thread started with device: %s", 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);
|
||||
if (r->client->socket != -1)
|
||||
{
|
||||
client_write(r->client, r->tty_dev->data, ret);
|
||||
}
|
||||
}
|
||||
|
||||
if (debug_messages)
|
||||
{
|
||||
LOG("tty thread alive");
|
||||
}
|
||||
} /* end main while() loop */
|
||||
|
||||
LOG("tty thread stopped");
|
||||
|
||||
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) )
|
||||
{
|
||||
/* ask the new client to provide a username before going to "character" mode */
|
||||
if (client_ask_username(r->new_client) != 0)
|
||||
{
|
||||
/* close new client if not able to provide a username */
|
||||
client_close(r->new_client);
|
||||
continue;
|
||||
}
|
||||
/* copy new client information */
|
||||
memcpy(r->client, r->new_client, sizeof(client_t));
|
||||
r->new_client->socket = -1;
|
||||
LOG("client %s connected", r->client->ip_string);
|
||||
/* 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?
|
||||
LOG("[@%d] error %d: %s", __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))
|
||||
{
|
||||
LOG("received client connection request");
|
||||
/* 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 */
|
||||
LOG("problem with handling client connection request");
|
||||
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)
|
||||
{
|
||||
LOG("client %s disconnected", 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 */
|
||||
LOG("problem reading from client, but will continue");
|
||||
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)
|
||||
{
|
||||
LOG("client last active %u seconds ago",
|
||||
(unsigned int) (current_time - r->client->last_active));
|
||||
}
|
||||
}
|
||||
/* do something while listening for client connections? */
|
||||
else
|
||||
{
|
||||
if (debug_messages)
|
||||
{
|
||||
LOG("listening for client connection");
|
||||
}
|
||||
}
|
||||
}
|
||||
} /* end main while() loop */
|
||||
|
||||
return (void*) 0;
|
||||
}
|
60
moxerver/task_threads.h
Normal file
60
moxerver/task_threads.h
Normal file
|
@ -0,0 +1,60 @@
|
|||
/* Thread functions for handling top level tasks. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <client.h>
|
||||
#include <server.h>
|
||||
#include <tty.h>
|
||||
#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 */
|
||||
|
||||
typedef struct
|
||||
{
|
||||
server_t *server;
|
||||
client_t *client;
|
||||
client_t *new_client;
|
||||
tty_t *tty_dev;
|
||||
} resources_t;
|
||||
|
||||
/**
|
||||
* The thread function handling new client connections.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The function handles global resources through the pointer to a "resources_t"
|
||||
* structure passed as the input argument.
|
||||
*
|
||||
* Returns:
|
||||
* Return value from this thread function is not used.
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* Returns:
|
||||
* Return value from this thread function is not used.
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* Returns:
|
||||
* Return value from this thread function is not used.
|
||||
*/
|
||||
void* thread_client_data(void *args);
|
153
moxerver/telnet.c
Normal file
153
moxerver/telnet.c
Normal file
|
@ -0,0 +1,153 @@
|
|||
#include <telnet.h>
|
||||
|
||||
/* structure for holding telnet option name and value */
|
||||
typedef struct
|
||||
{
|
||||
const char *name;
|
||||
char value;
|
||||
} telnet_option_t;
|
||||
|
||||
/* supported telnet option values */
|
||||
telnet_option_t telnet_options[] =
|
||||
{
|
||||
{"WILL", 251},
|
||||
{"WONT", 252},
|
||||
{"DO", 253},
|
||||
{"DONT", 254},
|
||||
{"IAC", 255},
|
||||
{"ECHO", 1},
|
||||
{"SGA", 3},
|
||||
{"LINEMODE", 34},
|
||||
{NULL, 0}
|
||||
/* this list must end with {NULL, 0} */
|
||||
};
|
||||
|
||||
/* Returns the name of a telnet option based on the value. */
|
||||
static const char* telnet_option_name(int value)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; telnet_options[i].name != NULL; i++)
|
||||
{
|
||||
if (telnet_options[i].value == value)
|
||||
{
|
||||
return telnet_options[i].name;
|
||||
}
|
||||
}
|
||||
/* default value */
|
||||
return '\0';
|
||||
}
|
||||
|
||||
/* Returns the value of a telnet option based on the name. */
|
||||
static char telnet_option_value(const char* name)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; telnet_options[i].name != NULL; i++)
|
||||
{
|
||||
if (strcmp(telnet_options[i].name, name) == 0)
|
||||
{
|
||||
return telnet_options[i].value;
|
||||
}
|
||||
}
|
||||
/* default value */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Handles a received telnet option command. */
|
||||
static void telnet_handle_command(char *databuf, int datalen)
|
||||
{
|
||||
/* just print received commands:
|
||||
* we set the client, we don't adapt to client commands */
|
||||
if (databuf[0] == telnet_option_value("IAC"))
|
||||
{
|
||||
LOG("received %s %s",
|
||||
telnet_option_name(databuf[1]),
|
||||
telnet_option_name(databuf[2]));
|
||||
}
|
||||
}
|
||||
|
||||
void telnet_message_set_character_mode(char *databuf)
|
||||
{
|
||||
/* send a predefined set of commands proven to work */
|
||||
|
||||
databuf[0] = telnet_option_value("IAC");
|
||||
databuf[1] = telnet_option_value("WILL");
|
||||
databuf[2] = telnet_option_value("ECHO");
|
||||
|
||||
databuf[3] = telnet_option_value("IAC");
|
||||
databuf[4] = telnet_option_value("WILL");
|
||||
databuf[5] = telnet_option_value("SGA");
|
||||
|
||||
/* this one really depends on telnet client */
|
||||
databuf[6] = telnet_option_value("IAC");
|
||||
databuf[7] = telnet_option_value("WONT");
|
||||
databuf[8] = telnet_option_value("LINEMODE");
|
||||
|
||||
//TODO Do we verify client response? What do we do if the response is not how we expected?
|
||||
}
|
||||
|
||||
void telnet_filter_client_read(char *databuf, int *datalen)
|
||||
{
|
||||
int i;
|
||||
char newdata[BUFFER_LEN];
|
||||
int newlen = 0;
|
||||
|
||||
/* process data using a new buffer */
|
||||
for (i = 0; i < *datalen; i++)
|
||||
{
|
||||
/* handle and discard telnet commands */
|
||||
if (databuf[i] == telnet_option_value("IAC"))
|
||||
{
|
||||
telnet_handle_command((databuf+i), 3);
|
||||
i += 2;
|
||||
}
|
||||
/* let other data pass through */
|
||||
else
|
||||
{
|
||||
newdata[newlen++] = databuf[i];
|
||||
}
|
||||
}
|
||||
/* overwrite the data with the new buffer */
|
||||
for (i = 0; i < newlen; i++)
|
||||
{
|
||||
databuf[i] = newdata[i];
|
||||
}
|
||||
/* update data length */
|
||||
*datalen = newlen;
|
||||
}
|
||||
|
||||
void telnet_filter_client_write(char *databuf, int *datalen)
|
||||
{
|
||||
int i;
|
||||
char newdata[BUFFER_LEN]; // TODO: maybe use realloc, this is risky
|
||||
int newlen = 0;
|
||||
|
||||
/* process data using a new buffer */
|
||||
for (i = 0; i < *datalen; i++)
|
||||
{
|
||||
/* pressed ENTER */
|
||||
if (databuf[i] == 13)
|
||||
{
|
||||
LOG("handling ENTER");
|
||||
newdata[newlen++] = '\r';
|
||||
newdata[newlen++] = '\n';
|
||||
}
|
||||
/* pressed BACKSPACE */
|
||||
if (databuf[i] == 127)
|
||||
{
|
||||
LOG("handling BACKSPACE");
|
||||
newdata[newlen++] = 8;
|
||||
newdata[newlen++] = ' ';
|
||||
newdata[newlen++] = 8;
|
||||
}
|
||||
else {
|
||||
newdata[newlen++] = databuf[i];
|
||||
}
|
||||
}
|
||||
/* overwrite the data with the new buffer */
|
||||
for (i = 0; i < newlen; i++)
|
||||
{
|
||||
databuf[i] = newdata[i];
|
||||
}
|
||||
/* update data length */
|
||||
*datalen = newlen;
|
||||
}
|
29
moxerver/telnet.h
Normal file
29
moxerver/telnet.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
/* Handles details related to telnet protocol. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <common.h>
|
||||
|
||||
#define TELNET_MSG_LEN_CHARMODE 9
|
||||
|
||||
/**
|
||||
* Creates a telnet protocol message that tells client to go into "character"
|
||||
* mode. The passed data buffer must be big enough to hold the message payload
|
||||
* with the size defined by TELNET_MSG_LEN_CHARMODE.
|
||||
* Operates directly on the passed data buffer.
|
||||
*/
|
||||
void telnet_message_set_character_mode(char *databuf);
|
||||
|
||||
/**
|
||||
* Handles special characters in the data buffer after receiving them from the
|
||||
* client. Used to filter out the handshake commands of telnet protocol.
|
||||
* Operates directly on the passed data buffer and modifies the payload length.
|
||||
*/
|
||||
void telnet_filter_client_read(char *databuf, int *datalen);
|
||||
|
||||
/**
|
||||
* Handles special characters in the data buffer before sending them to the
|
||||
* client. Used to correctly echo the characters to the telnet client.
|
||||
* Operates directly on the passed data buffer and modifies the payload length.
|
||||
*/
|
||||
void telnet_filter_client_write(char *databuf, int *datalen);
|
218
moxerver/tty.c
Normal file
218
moxerver/tty.c
Normal file
|
@ -0,0 +1,218 @@
|
|||
#include <tty.h>
|
||||
|
||||
#define TTY_DEFAULT_BAUDRATE B115200
|
||||
|
||||
int tty_open(tty_t *tty_dev)
|
||||
{
|
||||
/* open tty device to get the file descriptor */
|
||||
tty_dev->fd = open (tty_dev->path, O_RDWR | O_NOCTTY | O_SYNC);
|
||||
if (tty_dev->fd < 0)
|
||||
{
|
||||
tty_dev->fd = -1;
|
||||
return -errno;
|
||||
}
|
||||
|
||||
/* store default termios settings */
|
||||
if (tcgetattr(tty_dev->fd, &(tty_dev->ttysetold)))
|
||||
{
|
||||
LOG("[@%d] error reading device default config\n"
|
||||
"\t\t-> default config will not be restored upon exit", __LINE__);
|
||||
}
|
||||
|
||||
/* set tty device parameters */
|
||||
tty_dev->ttyset.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR |
|
||||
PARMRK | INPCK | ISTRIP | IXON);
|
||||
tty_dev->ttyset.c_oflag &= ~(OCRNL | ONLCR | ONLRET |
|
||||
ONOCR | OFILL | OLCUC | OPOST);
|
||||
tty_dev->ttyset.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
|
||||
tty_dev->ttyset.c_cflag &= ~(CSIZE | PARENB);
|
||||
tty_dev->ttyset.c_cflag |= CS8 | CREAD;
|
||||
tty_dev->ttyset.c_cc[VMIN] = 1;
|
||||
tty_dev->ttyset.c_cc[VTIME] = 5;
|
||||
|
||||
/* if speed is set to B0 (e.g. cfg file not provided), use default values */
|
||||
if (cfgetispeed(&(tty_dev->ttyset)) == baud_to_speed(0) &&
|
||||
cfsetispeed(&(tty_dev->ttyset), TTY_DEFAULT_BAUDRATE) < 0)
|
||||
{
|
||||
LOG("error configuring tty device speed");
|
||||
return -errno;
|
||||
}
|
||||
if (cfgetospeed(&(tty_dev->ttyset)) == baud_to_speed(0) &&
|
||||
cfsetospeed(&(tty_dev->ttyset), TTY_DEFAULT_BAUDRATE) < 0)
|
||||
{
|
||||
LOG("error configuring tty device speed");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
/* apply tty device settings */
|
||||
if (tcsetattr(tty_dev->fd, TCSANOW, &(tty_dev->ttyset)) < 0)
|
||||
{
|
||||
LOG("error configuring tty device");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int tty_close(tty_t *tty_dev)
|
||||
{
|
||||
int fd = tty_dev->fd;
|
||||
tty_dev->fd = -1;
|
||||
|
||||
LOG("closing tty device");
|
||||
|
||||
if (tcsetattr(fd, TCSANOW, &(tty_dev->ttysetold)) < 0)
|
||||
{
|
||||
LOG("[@%d] error restoring tty device default config", __LINE__);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
if (close(fd) < 0)
|
||||
{
|
||||
return -errno;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int tty_read(tty_t *tty_dev)
|
||||
{
|
||||
int len;
|
||||
|
||||
len = read(tty_dev->fd, tty_dev->data, BUFFER_LEN);
|
||||
if (len == -1)
|
||||
{
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
|
||||
//TODO let's print received bytes during development phase...
|
||||
if (debug_messages)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < len; i++)
|
||||
{
|
||||
LOG("tty <- %u '%c'",
|
||||
(unsigned char) tty_dev->data[i],
|
||||
(unsigned char) tty_dev->data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
int tty_write(tty_t *tty_dev, char *databuf, int datalen)
|
||||
{
|
||||
int len;
|
||||
|
||||
len = write(tty_dev->fd, databuf, datalen);
|
||||
if (len == -1)
|
||||
{
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
|
||||
//TODO let's print received bytes during development phase...
|
||||
if (debug_messages)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < datalen; i++)
|
||||
{
|
||||
LOG("tty -> %u '%c'",
|
||||
(unsigned char) databuf[i],
|
||||
(unsigned char) databuf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
int speed_to_baud(speed_t speed)
|
||||
{
|
||||
switch (speed)
|
||||
{
|
||||
case B0:
|
||||
return 0;
|
||||
case B50:
|
||||
return 50;
|
||||
case B75:
|
||||
return 75;
|
||||
case B110:
|
||||
return 110;
|
||||
case B134:
|
||||
return 134;
|
||||
case B150:
|
||||
return 150;
|
||||
case B200:
|
||||
return 200;
|
||||
case B300:
|
||||
return 300;
|
||||
case B600:
|
||||
return 600;
|
||||
case B1200:
|
||||
return 1200;
|
||||
case B1800:
|
||||
return 1800;
|
||||
case B2400:
|
||||
return 2400;
|
||||
case B4800:
|
||||
return 4800;
|
||||
case B9600:
|
||||
return 9600;
|
||||
case B19200:
|
||||
return 19200;
|
||||
case B38400:
|
||||
return 38400;
|
||||
case B57600:
|
||||
return 57600;
|
||||
case B115200:
|
||||
return 115200;
|
||||
default:
|
||||
return 115200;
|
||||
}
|
||||
}
|
||||
|
||||
speed_t baud_to_speed(int baud)
|
||||
{
|
||||
switch (baud)
|
||||
{
|
||||
case 0:
|
||||
return B0;
|
||||
case 50:
|
||||
return B50;
|
||||
case 75:
|
||||
return B75;
|
||||
case 110:
|
||||
return B110;
|
||||
case 134:
|
||||
return B134;
|
||||
case 150:
|
||||
return B150;
|
||||
case 200:
|
||||
return B200;
|
||||
case 300:
|
||||
return B300;
|
||||
case 600:
|
||||
return B600;
|
||||
case 1200:
|
||||
return B1200;
|
||||
case 1800:
|
||||
return B1800;
|
||||
case 2400:
|
||||
return B2400;
|
||||
case 4800:
|
||||
return B4800;
|
||||
case 9600:
|
||||
return B9600;
|
||||
case 19200:
|
||||
return B19200;
|
||||
case 38400:
|
||||
return B38400;
|
||||
case 57600:
|
||||
return B57600;
|
||||
case 115200:
|
||||
return B115200;
|
||||
default:
|
||||
return B115200;
|
||||
}
|
||||
}
|
66
moxerver/tty.h
Normal file
66
moxerver/tty.h
Normal file
|
@ -0,0 +1,66 @@
|
|||
/* Handles communication with a tty device. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <common.h>
|
||||
#include <termios.h>
|
||||
|
||||
#define TTY_DEV_PATH_LEN 128
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int fd; /* tty device file descriptor */
|
||||
struct termios ttysetold; /* previous termios settings */
|
||||
struct termios ttyset; /* current termios settings */
|
||||
char path[TTY_DEV_PATH_LEN]; /* tty device path */
|
||||
char data[BUFFER_LEN]; /* buffer for received data */
|
||||
} tty_t;
|
||||
|
||||
/**
|
||||
* Opens the tty device and configures it.
|
||||
* The old device settings are saved.
|
||||
*
|
||||
* Returns:
|
||||
* - 0 on success
|
||||
* - negative errno value if an error occurred
|
||||
*/
|
||||
int tty_open(tty_t *tty_dev);
|
||||
|
||||
/**
|
||||
* Closes the tty device connection.
|
||||
* Also applies the old device settings.
|
||||
*
|
||||
* Returns:
|
||||
* - 0 on success
|
||||
* - negative errno value if an error occurred
|
||||
*/
|
||||
int tty_close(tty_t *tty_dev);
|
||||
|
||||
/**
|
||||
* Reads incoming data from tty device to tty data buffer.
|
||||
*
|
||||
* Returns:
|
||||
* - number of read bytes on success,
|
||||
* - negative errno value set by an error while reading
|
||||
*/
|
||||
int tty_read(tty_t *tty_dev);
|
||||
|
||||
/**
|
||||
* Sends data from a buffer to tty device.
|
||||
*
|
||||
* Returns:
|
||||
* - number of sent bytes on success,
|
||||
* - negative errno value set by an error while sending
|
||||
*/
|
||||
int tty_write(tty_t *tty_dev, char *databuf, int datalen);
|
||||
|
||||
/**
|
||||
* Converts POSIX speed_t to a baud rate.
|
||||
* The values of the constants for speed_t are not themselves portable.
|
||||
*/
|
||||
int speed_to_baud(speed_t speed);
|
||||
|
||||
/**
|
||||
* Converts a numeric baud rate to a POSIX speed_t.
|
||||
*/
|
||||
speed_t baud_to_speed(int baud);
|
Loading…
Add table
Add a link
Reference in a new issue