Typedef custom structures
This commit is contained in:
parent
154f16a7f1
commit
cad50493a6
6 changed files with 52 additions and 48 deletions
10
client.c
10
client.c
|
@ -5,7 +5,7 @@
|
|||
#include "moxerver.h"
|
||||
|
||||
/* Closes client connection. */
|
||||
int client_close(struct client_t *client) {
|
||||
int client_close(client_t *client) {
|
||||
char timestamp[TIMESTAMP_LEN];
|
||||
/* force closing in case of error */
|
||||
if (close(client->socket) == -1) {
|
||||
|
@ -18,7 +18,7 @@ int client_close(struct client_t *client) {
|
|||
}
|
||||
|
||||
/* Reads data from client into client data buffer. Returns number of read bytes. */
|
||||
int client_read(struct client_t *client) {
|
||||
int client_read(client_t *client) {
|
||||
|
||||
int len;
|
||||
|
||||
|
@ -55,7 +55,7 @@ int client_read(struct client_t *client) {
|
|||
}
|
||||
|
||||
/* Sends data from a buffer to client. Returns number of sent bytes. */
|
||||
int client_write(struct client_t *client, char *databuf, int datalen) {
|
||||
int client_write(client_t *client, char *databuf, int datalen) {
|
||||
|
||||
int len;
|
||||
|
||||
|
@ -84,7 +84,7 @@ int client_write(struct client_t *client, char *databuf, int datalen) {
|
|||
}
|
||||
|
||||
/* Waits for client input in "line mode". Blocks until input arrives. */
|
||||
int client_wait_line(struct client_t *client) {
|
||||
int client_wait_line(client_t *client) {
|
||||
|
||||
fd_set read_fds;
|
||||
struct timeval tv;
|
||||
|
@ -115,7 +115,7 @@ int client_wait_line(struct client_t *client) {
|
|||
}
|
||||
|
||||
/* Waits for client to provide a username. Blocks until a username is entered. */
|
||||
int client_ask_username(struct client_t *client) {
|
||||
int client_ask_username(client_t *client) {
|
||||
|
||||
int i;
|
||||
char msg[DATABUF_LEN];
|
||||
|
|
|
@ -183,7 +183,7 @@ int main(int argc, char *argv[]) {
|
|||
/* check if new client is availabe for connection */
|
||||
if ( (client.socket == -1) && (new_client.socket != -1) ) {
|
||||
/* copy new client information */
|
||||
memcpy(&client, &new_client, sizeof(struct client_t));
|
||||
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 */
|
||||
|
|
53
moxerver.h
53
moxerver.h
|
@ -20,36 +20,39 @@
|
|||
#define USERNAME_LEN 32
|
||||
|
||||
/* Structures used for communication parameters. */
|
||||
struct server_t {
|
||||
typedef struct
|
||||
{
|
||||
int socket; /* server socket */
|
||||
struct sockaddr_in address; /* server address information */
|
||||
unsigned int port; /* server port in host byte order, practical reference */
|
||||
};
|
||||
} server_t;
|
||||
|
||||
struct client_t {
|
||||
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 in seconds from Epoch */
|
||||
char username[USERNAME_LEN]; /* username for human identification */
|
||||
char data[DATABUF_LEN]; /* buffer for data received from client */
|
||||
};
|
||||
} client_t;
|
||||
|
||||
struct tty_t {
|
||||
typedef struct
|
||||
{
|
||||
int fd; /* tty file descriptor */
|
||||
struct termios ttysetdef; /* default tty termios settings */
|
||||
struct termios ttyset; /* tty termios settings */
|
||||
char path[DEV_PATH]; /* tty device path */
|
||||
char data[DATABUF_LEN]; /* buffer for data received from tty */
|
||||
};
|
||||
} tty_t;
|
||||
|
||||
|
||||
/* Global variables used throughout the application. */
|
||||
int debug_messages; /* if > 0 debug messages will be printed */
|
||||
struct server_t server; /* main server structure */
|
||||
struct client_t client; /* connected client structure */ //TODO working with only 1 client, this can be expanded into a list
|
||||
struct client_t new_client; /* client structure for new client request */
|
||||
struct tty_t tty_dev; /* connected tty device */
|
||||
int debug_messages; /* if > 0 debug messages will be printed */
|
||||
server_t server; /* main server structure */
|
||||
client_t client; /* connected client structure */ //TODO working with only 1 client, this can be expanded into a list
|
||||
client_t new_client; /* client structure for new client request */
|
||||
tty_t tty_dev; /* connected tty device */
|
||||
|
||||
|
||||
/* Global functions used throughout the application. */
|
||||
|
@ -72,7 +75,7 @@ int time2string(time_t time, char* timestamp);
|
|||
* - 0 on success,
|
||||
* - negative errno value set appropriately by error in setup process
|
||||
*/
|
||||
int server_setup(struct server_t *server, unsigned int port);
|
||||
int server_setup(server_t *server, unsigned int port);
|
||||
|
||||
/**
|
||||
* Closes the server socket.
|
||||
|
@ -80,7 +83,7 @@ int server_setup(struct server_t *server, unsigned int port);
|
|||
* Returns:
|
||||
* 0 always, but internally tries closing again if it fails
|
||||
*/
|
||||
int server_close(struct server_t *server);
|
||||
int server_close(server_t *server);
|
||||
|
||||
/**
|
||||
* Accepts incoming client connection.
|
||||
|
@ -89,7 +92,7 @@ int server_close(struct server_t *server);
|
|||
* - 0 on success,
|
||||
* - negative errno value set appropriately by error in setup process
|
||||
*/
|
||||
int server_accept(struct server_t *server, struct client_t *accepted_client);
|
||||
int server_accept(server_t *server, client_t *accepted_client);
|
||||
|
||||
/**
|
||||
* Thread function handling new client connections.
|
||||
|
@ -113,7 +116,7 @@ void* server_new_client_thread(void *args);
|
|||
* Returns:
|
||||
* 0 always, but internally tries closing again if it fails
|
||||
*/
|
||||
int client_close(struct client_t *client);
|
||||
int client_close(client_t *client);
|
||||
|
||||
/**
|
||||
* Reads data from client into client data buffer.
|
||||
|
@ -124,7 +127,7 @@ int client_close(struct client_t *client);
|
|||
* - negative ENODATA value (-ENODATA) if client disconnected,
|
||||
* - negative errno value set appropriately by error in reading
|
||||
*/
|
||||
int client_read(struct client_t *client);
|
||||
int client_read(client_t *client);
|
||||
|
||||
/**
|
||||
* Sends data from a buffer to client.
|
||||
|
@ -133,7 +136,7 @@ int client_read(struct client_t *client);
|
|||
* - number of sent bytes on success,
|
||||
* - negative errno value set appropriately by error in sending
|
||||
*/
|
||||
int client_write(struct client_t *client, char *databuf, int datalen);
|
||||
int client_write(client_t *client, char *databuf, int datalen);
|
||||
|
||||
/**
|
||||
* Waits for client input in "line mode", where client sends a whole line of characters.
|
||||
|
@ -143,7 +146,7 @@ int client_write(struct client_t *client, char *databuf, int datalen);
|
|||
* - 0 on success
|
||||
* - negative value if error occurred
|
||||
*/
|
||||
int client_wait_line(struct client_t *client);
|
||||
int client_wait_line(client_t *client);
|
||||
|
||||
/**
|
||||
* Waits for client to provide a username.
|
||||
|
@ -153,7 +156,7 @@ int client_wait_line(struct client_t *client);
|
|||
* - 0 on success
|
||||
* - negative value if error occurred
|
||||
*/
|
||||
int client_ask_username(struct client_t *client);
|
||||
int client_ask_username(client_t *client);
|
||||
|
||||
|
||||
/* Functions handling details related to telnet protocol. */
|
||||
|
@ -165,7 +168,7 @@ int client_ask_username(struct client_t *client);
|
|||
* - 0 on success
|
||||
* - negative value if error occurred
|
||||
*/
|
||||
int telnet_set_character_mode(struct client_t *client);
|
||||
int telnet_set_character_mode(client_t *client);
|
||||
|
||||
/**
|
||||
* Handles special characters in data buffer after receiving them from client.
|
||||
|
@ -189,19 +192,19 @@ 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);
|
||||
int tty_open(tty_t *tty_dev);
|
||||
|
||||
/* Closes the tty device. */
|
||||
int tty_close(struct tty_t *tty_dev);
|
||||
int tty_close(tty_t *tty_dev);
|
||||
|
||||
/* Reconfigures the tty device. */
|
||||
int tty_reconfigure(struct tty_t *tty_dev, struct termios newttyset);
|
||||
int tty_reconfigure(tty_t *tty_dev, struct termios newttyset);
|
||||
|
||||
/* Reads incoming data from tty device to tty data buffer. */
|
||||
int tty_read(struct tty_t *tty_dev);
|
||||
int tty_read(tty_t *tty_dev);
|
||||
|
||||
/* Sends data from a buffer to tty device. */
|
||||
int tty_write(struct tty_t *tty_dev, char *databuf, int datalen);
|
||||
int tty_write(tty_t *tty_dev, char *databuf, int datalen);
|
||||
|
||||
/* Main tty thread function */
|
||||
void *tty_thread_func(void *arg);
|
||||
|
|
12
server.c
12
server.c
|
@ -5,7 +5,7 @@
|
|||
#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) {
|
||||
int server_setup(server_t *server, unsigned int port) {
|
||||
|
||||
int opt;
|
||||
char timestamp[TIMESTAMP_LEN];
|
||||
|
@ -59,7 +59,7 @@ int server_setup(struct server_t *server, unsigned int port) {
|
|||
}
|
||||
|
||||
/* Closes the server socket. */
|
||||
int server_close(struct server_t *server) {
|
||||
int server_close(server_t *server) {
|
||||
char timestamp[TIMESTAMP_LEN];
|
||||
/* force closing in case of error */
|
||||
if (close(server->socket) == -1) {
|
||||
|
@ -71,7 +71,7 @@ int server_close(struct server_t *server) {
|
|||
}
|
||||
|
||||
/* Accepts incoming client connection. */
|
||||
int server_accept(struct server_t *server, struct client_t *accepted_client) {
|
||||
int server_accept(server_t *server, client_t *accepted_client) {
|
||||
|
||||
int namelen;
|
||||
char timestamp[TIMESTAMP_LEN];
|
||||
|
@ -108,14 +108,14 @@ void* server_new_client_thread(void *args) {
|
|||
|
||||
char msg[DATABUF_LEN];
|
||||
char timestamp[TIMESTAMP_LEN];
|
||||
struct client_t temp_client;
|
||||
client_t temp_client;
|
||||
|
||||
/* accept new connection request */
|
||||
if (server_accept(&server, &temp_client) != 0) return (void *) -1;
|
||||
|
||||
/* if no client is connected then make new client available for connection */
|
||||
if (client.socket == -1) {
|
||||
memcpy(&new_client, &temp_client, sizeof(struct client_t));
|
||||
memcpy(&new_client, &temp_client, sizeof(client_t));
|
||||
return (void *) 0;
|
||||
}
|
||||
|
||||
|
@ -131,7 +131,7 @@ void* server_new_client_thread(void *args) {
|
|||
}
|
||||
|
||||
/* make new client available for connection */
|
||||
memcpy(&new_client, &temp_client, sizeof(struct client_t));
|
||||
memcpy(&new_client, &temp_client, sizeof(client_t));
|
||||
|
||||
/* inform new client that port is already in use */
|
||||
time2string(client.last_active, timestamp);
|
||||
|
|
11
telnet.c
11
telnet.c
|
@ -5,13 +5,14 @@
|
|||
#include "moxerver.h"
|
||||
|
||||
/* structure for holding telnet option name and value */
|
||||
struct telnet_option_t {
|
||||
typedef struct
|
||||
{
|
||||
const char *name;
|
||||
char value;
|
||||
};
|
||||
} telnet_option_t;
|
||||
|
||||
/* supported telnet option values */
|
||||
struct telnet_option_t telnet_options[] = {
|
||||
telnet_option_t telnet_options[] = {
|
||||
{"WILL", 251},
|
||||
{"WONT", 252},
|
||||
{"DO", 253},
|
||||
|
@ -45,7 +46,7 @@ static char telnet_option_value(const char* name) {
|
|||
}
|
||||
|
||||
/* Sends telnet option command. Returns 0 on success, -1 on failure. */
|
||||
static int telnet_send_command(struct client_t *client, const char* option, const char* command) {
|
||||
static int telnet_send_command(client_t *client, const char* option, const char* command) {
|
||||
|
||||
char data[3];
|
||||
|
||||
|
@ -77,7 +78,7 @@ static int telnet_handle_command(char *databuf, int datalen) {
|
|||
}
|
||||
|
||||
/* Tells client to go into "character" mode. */
|
||||
int telnet_set_character_mode(struct client_t *client) {
|
||||
int telnet_set_character_mode(client_t *client) {
|
||||
|
||||
int err = 0;
|
||||
|
||||
|
|
12
tty.c
12
tty.c
|
@ -10,7 +10,7 @@
|
|||
#define TTY_DEF_BAUD_RATE B115200
|
||||
|
||||
/* Opens the tty device and configures it. */
|
||||
int tty_open(struct tty_t *tty_dev) {
|
||||
int tty_open(tty_t *tty_dev) {
|
||||
int fd;
|
||||
// PROPOSAL:
|
||||
// open tty device to get file descriptor @tty_dev.fd
|
||||
|
@ -59,7 +59,7 @@ int tty_open(struct tty_t *tty_dev) {
|
|||
}
|
||||
|
||||
/* Closes the tty device. */
|
||||
int tty_close(struct tty_t *tty_dev) {
|
||||
int tty_close(tty_t *tty_dev) {
|
||||
|
||||
int fd = tty_dev->fd;
|
||||
tty_dev->fd = -1;
|
||||
|
@ -78,7 +78,7 @@ int tty_close(struct tty_t *tty_dev) {
|
|||
}
|
||||
|
||||
/* Reconfigures the tty device. */
|
||||
int tty_reconfigure(struct tty_t *tty_dev, struct termios newttyset) {
|
||||
int tty_reconfigure(tty_t *tty_dev, struct termios newttyset) {
|
||||
// not sure how to organize this:
|
||||
// 1. parameters in external termios struct, copied @tty_dev.ttyset, applied with tcsetattr()
|
||||
// 2. parameters directly @tty_dev.ttyset, applied with tcsetattr()
|
||||
|
@ -86,13 +86,13 @@ int tty_reconfigure(struct tty_t *tty_dev, struct termios newttyset) {
|
|||
}
|
||||
|
||||
/* Reads incoming data from tty device to tty data buffer. */
|
||||
int tty_read(struct tty_t *tty_dev) {
|
||||
int tty_read(tty_t *tty_dev) {
|
||||
// read and save @tty_dev.data
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Sends data from a buffer to tty device. */
|
||||
int tty_write(struct tty_t *tty_dev, char *databuf, int datalen) {
|
||||
int tty_write(tty_t *tty_dev, char *databuf, int datalen) {
|
||||
write(tty_dev->fd, databuf, datalen);
|
||||
// databuf should point to client data buffer
|
||||
return 0;
|
||||
|
@ -101,7 +101,7 @@ int tty_write(struct tty_t *tty_dev, char *databuf, int datalen) {
|
|||
/* Main thread for reading and writing to tty device */
|
||||
void *tty_thread_func(void *arg) {
|
||||
//char c;
|
||||
struct tty_t *tty_dev = (struct tty_t*)arg;
|
||||
tty_t *tty_dev = (tty_t*)arg;
|
||||
struct timeval tv;
|
||||
ssize_t br = 0;
|
||||
int ret;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue