- improve runner
- use network byte order in frame data
- add broadcast handle
This commit is contained in:
Dany LE 2021-11-28 14:02:34 +01:00
parent 0bc8ec7da3
commit d8d0632561
14 changed files with 852 additions and 314 deletions

View File

@ -15,7 +15,7 @@ install-data-local:
EXTRA_DIST = runner.ini runnerd tunnel.h antd-tunnel-publisher.service log.h
SUBDIRS = . vterm wfifo syslog
SUBDIRS = . vterm wfifo syslog broadcast
if ENABLE_CAM
SUBDIRS += v4l2cam

11
broadcast/Makefile.am Normal file
View File

@ -0,0 +1,11 @@
AUTOMAKE_OPTIONS = foreign
AM_CPPFLAGS = -W -Wall -g -std=c99
# bin
bin_PROGRAMS = broadcast
# source files
broadcast_SOURCES = broadcast.c ../tunnel.c
broadcast_CPPFLAGS= -I../

BIN
broadcast/broadcast Executable file

Binary file not shown.

456
broadcast/broadcast.c Normal file
View File

@ -0,0 +1,456 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <antd/bst.h>
#include <antd/utils.h>
#include <antd/list.h>
#include "../tunnel.h"
#define BC_ERROR(r, fd, c, ...) \
do \
{ \
r.header.client_id = c; \
r.header.type = CHANNEL_ERROR; \
(void)snprintf(r.data, BUFFLEN, ##__VA_ARGS__); \
r.header.size = strlen(r.data); \
M_ERROR(MODULE_NAME, "%s", r.data); \
(void)msg_write(fd, &r); \
} while (0)
#define MODULE_NAME "broadcast"
#define BC_SUBSCRIPTION 0x0A
#define BC_UNSUBSCRIPTION 0x0B
#define BC_QUERY_GROUP 0x0C
#define MAX_STR_LEN 255
typedef struct
{
int ref;
char name[MAX_STR_LEN];
bst_node_t *groups;
} bc_client_t;
/**
* @brief Send notified to client.
*
* @param type
* @param bc_client broadcast client handle
* @param groupname group name
*/
static void bc_notify(bst_node_t *node, void **argv, int argc);
static void int_handler(int dummy);
static void bc_get_handle(bst_node_t *node, void **argv, int argc);
static void bc_unsubscription(bst_node_t *node, void **args, int argc);
static void unsubscribe(bst_node_t *node, void **args, int argc);
static void bc_send_query(bst_node_t *node, void **argv, int argc);
static bst_node_t *clients = NULL;
static uint8_t msg_buffer[BUFFLEN];
static volatile int running = 1;
static void int_handler(int dummy)
{
(void)dummy;
running = 0;
}
static void bc_get_handle(bst_node_t *node, void **argv, int argc)
{
(void)argc;
bc_client_t **bc_client = (bc_client_t **)argv[1];
char *name = (char *)argv[2];
bc_client_t *node_client = (bc_client_t *)node->data;
if (bc_client == NULL || name == NULL || node->data == NULL)
{
return;
}
M_DEBUG(MODULE_NAME, "comparing %s vs %s", name, node_client->name);
if (strcmp(name, node_client->name) == 0)
{
M_LOG(MODULE_NAME, "Handle for user %s exits (ref %d)", name, node_client->ref);
*bc_client = node_client;
}
}
static void bc_unsubscription(bst_node_t *node, void **args, int argc)
{
(void)argc;
tunnel_msg_t *msg = (tunnel_msg_t *)args[1];
int len = *((int *)args[3]) + 2;
if (!node)
return;
// write hash value to data
int hash = node->key;
uint32_t net32 = htonl(hash);
(void)memcpy(&msg->data[len], &net32, sizeof(net32));
msg->header.size = len + sizeof(net32);
args[2] = &hash;
M_DEBUG(MODULE_NAME, "All clients subscribed to the groupe %d is notified that user is leaving", hash);
bst_for_each(clients, bc_notify, args, 3);
}
static void unsubscribe(bst_node_t *node, void **args, int argc)
{
(void)argc;
tunnel_msg_t msg;
int *ufd = (int *)args[0];
int len;
bc_client_t *bc_client = (bc_client_t *)node->data;
void *bc_argv[] = {ufd, &msg, 0, &len};
if (!node || !node->data)
{
return;
}
bc_client->ref--;
// notify all clients in our groups that we're done
msg.header.type = CHANNEL_CTRL;
msg.data = msg_buffer;
msg.data[0] = BC_UNSUBSCRIPTION;
len = strlen(bc_client->name);
msg.data[1] = (uint8_t)len;
(void)memcpy(&msg.data[2], bc_client->name, len);
// group name
// unsubscribe
if (bc_client->ref <= 0)
{
M_DEBUG(MODULE_NAME, "User %s is leaving all its subscribed groups (%d)", bc_client->name, bc_client->groups == NULL);
bst_for_each(bc_client->groups, bc_unsubscription, bc_argv, 3);
M_DEBUG(MODULE_NAME, "Handle for user %s ref is %d, free handle data", bc_client->name, bc_client->ref);
bst_free(bc_client->groups);
free(node->data);
}
msg.header.type = CHANNEL_UNSUBSCRIBE;
msg.header.client_id = node->key;
msg.header.size = 0;
msg.data = NULL;
if (msg_write(*ufd, &msg) == -1)
{
M_ERROR(MODULE_NAME, "Unable to request unsubscribe to client %d", node->key);
}
}
static void bc_send_query(bst_node_t *node, void **argv, int argc)
{
(void)argc;
int *fd = (int *)argv[0];
tunnel_msg_t *msg = (tunnel_msg_t *)argv[1];
bc_client_t *bc_client = NULL;
int *group = (int *)argv[2];
int len = *(int *)argv[3];
if (!node->data)
{
return;
}
bc_client = (bc_client_t *)node->data;
if (bst_find(bc_client->groups, *group) == NULL)
{
return;
}
(void)memcpy(&msg->data[len], bc_client->name, strlen(bc_client->name));
msg->header.size = len + strlen(bc_client->name);
if (msg_write(*fd, msg) == -1)
{
M_ERROR(MODULE_NAME, "Unable to write notify message to client %d", node->key);
}
}
static void bc_notify(bst_node_t *node, void **argv, int argc)
{
(void)argc;
int *fd = (int *)argv[0];
tunnel_msg_t *msg = (tunnel_msg_t *)argv[1];
bc_client_t *bc_client = NULL;
int *group = (int *)argv[2];
if (!node->data)
{
return;
}
bc_client = (bc_client_t *)node->data;
if (bst_find(bc_client->groups, *group) == NULL)
{
return;
}
msg->header.client_id = node->key;
if (msg_write(*fd, msg) == -1)
{
M_ERROR(MODULE_NAME, "Unable to write notify message to client %d", node->key);
}
M_DEBUG(MODULE_NAME, "Notify message sent to client %d", node->key);
}
int main(int argc, char **argv)
{
int fd, hash;
size_t len;
tunnel_msg_t request, response;
fd_set fd_in;
int status, length;
char name[MAX_STR_LEN + 1];
void *fargv[4];
bst_node_t *node_p;
uint32_t net32;
bc_client_t *bc_client;
LOG_INIT(MODULE_NAME);
if (argc != 3)
{
printf("Usage: %s path/to/hotline/socket channel_name\n", argv[0]);
return -1;
}
signal(SIGPIPE, SIG_IGN);
signal(SIGABRT, SIG_IGN);
signal(SIGINT, int_handler);
// now try to request new channel from hotline
fd = open_unix_socket(argv[1]);
if (fd == -1)
{
M_ERROR(MODULE_NAME, "Unable to open the hotline: %s", argv[1]);
return -1;
}
request.header.type = CHANNEL_OPEN;
request.header.channel_id = 0;
request.header.client_id = 0;
M_LOG(MODULE_NAME, "Request to open the channel %s", argv[2]);
(void)strncpy(name, argv[2], sizeof(name));
request.header.size = strlen(name);
request.data = (uint8_t *)name;
if (msg_write(fd, &request) == -1)
{
M_ERROR(MODULE_NAME, "Unable to write message to hotline");
(void)close(fd);
return -1;
}
M_DEBUG(MODULE_NAME, "Wait for comfirm creation of %s", argv[2]);
// now wait for message
if (msg_read(fd, &response) == -1)
{
M_ERROR(MODULE_NAME, "Unable to read message from hotline");
(void)close(fd);
return -1;
}
if (response.header.type == CHANNEL_OK)
{
M_LOG(MODULE_NAME, "Channel created: %s", argv[2]);
}
else
{
M_ERROR(MODULE_NAME, "Channel is not created: %s. Tunnel service responds with msg of type %d", argv[2], response.header.type);
running = 0;
}
if (response.data)
free(response.data);
// now read data
fargv[0] = (void *)&fd;
while (running)
{
FD_ZERO(&fd_in);
FD_SET(fd, &fd_in);
status = select(fd + 1, &fd_in, NULL, NULL, NULL);
switch (status)
{
case -1:
M_ERROR(MODULE_NAME, "Error %d on select()\n", errno);
running = 0;
break;
case 0:
break;
// we have data
default:
if (msg_read(fd, &request) == -1)
{
M_ERROR(MODULE_NAME, "Unable to read message from channel. quit");
running = 0;
}
else
{
switch (request.header.type)
{
case CHANNEL_SUBSCRIBE:
if (request.header.size > MAX_STR_LEN - 1)
{
M_ERROR(MODULE_NAME, "User name string overflow");
}
else
{
node_p = bst_find(clients, request.header.client_id);
if (node_p)
{
M_LOG(MODULE_NAME, "Client %d is already subscript to this channel", request.header.client_id);
}
{
// store user name
bc_client = NULL;
(void)memcpy(name, request.data, request.header.size);
name[request.header.size] = '\0';
fargv[1] = &bc_client;
fargv[2] = name;
bst_for_each(clients, bc_get_handle, fargv, 3);
if (bc_client)
{
bc_client->ref++;
}
else
{
bc_client = (bc_client_t *)malloc(sizeof(bc_client_t));
(void)strncpy(bc_client->name, name, MAX_STR_LEN);
bc_client->groups = NULL;
bc_client->ref = 1;
}
clients = bst_insert(clients, request.header.client_id, bc_client);
M_LOG(MODULE_NAME, "Client %s (%d) subscribes to the chanel (ref %d)", bc_client->name, request.header.client_id, bc_client->ref);
}
}
break;
case CHANNEL_CTRL:
/**
* @brief message in format [code][group name]
*
*/
if (request.header.size > 0u && request.header.size <= MAX_STR_LEN)
{
node_p = bst_find(clients, request.header.client_id);
if (node_p && node_p->data)
{
bc_client = (bc_client_t *)node_p->data;
fargv[1] = &response;
fargv[2] = &hash;
response.header.channel_id = request.header.channel_id;
response.header.type = CHANNEL_CTRL;
response.data = msg_buffer;
response.data[0] = request.data[0];
switch (request.data[0])
{
case BC_SUBSCRIPTION:
case BC_UNSUBSCRIPTION:
/**
* @brief * The notify message is in the following format
* [1 byte type][1 byte user name size][user name][4byte gid][groupname (optional)]
*
*/
if (request.data[0] == BC_SUBSCRIPTION)
{
memcpy(name, &request.data[1], request.header.size - 1u);
name[request.header.size - 1u] = '\0';
hash = simple_hash(name);
bc_client->groups = (void *)bst_insert(bc_client->groups, hash, NULL);
M_LOG(MODULE_NAME, "Client %d subscription to broadcast group: %s (%d)", request.header.client_id, name, hash);
}
else
{
name[0] = '\0';
(void)memcpy(&hash, &request.data[1], sizeof(hash));
hash = ntohl(hash);
}
len = strlen(bc_client->name);
response.data[1] = (uint8_t)len;
(void)memcpy(&response.data[2], bc_client->name, len);
len += 2u;
// group name
net32 = htonl(hash);
(void)memcpy(&response.data[len], &net32, sizeof(net32));
len += sizeof(net32);
(void)memcpy(&response.data[len], name, strlen(name));
response.header.size = len + strlen(name);
bst_for_each(clients, bc_notify, fargv, 3);
if (request.data[0] == BC_UNSUBSCRIPTION)
{
bc_client->groups = (void *)bst_delete(bc_client->groups, hash);
M_LOG(MODULE_NAME, "Client %d leaves broadcast group: %d", request.header.client_id, hash);
}
break;
case BC_QUERY_GROUP:
(void)memcpy(&hash, &request.data[1], sizeof(hash));
hash = ntohl(hash);
net32 = htonl(hash);
(void)memcpy(&response.data[1], &net32, sizeof(net32));
len = len = sizeof(net32) + 1u;
fargv[3] = &len;
if (bst_find(bc_client->groups, hash) == NULL)
{
BC_ERROR(response, fd, request.header.client_id, "Client %d query a group that it does not belong to", request.header.client_id);
}
else
{
// data format [type][4bytes group][user]
M_LOG(MODULE_NAME, "Client %d query group: %s (%d)", request.header.client_id, name, hash);
bst_for_each(clients, bc_send_query, fargv, 4);
}
break;
default:
BC_ERROR(response, fd, request.header.client_id, "Invalid client control message: 0x%.2X", request.data[0]);
break;
}
}
else
{
BC_ERROR(response, fd, request.header.client_id, "Client %d does not previously subscribe to the channel", request.header.client_id);
}
}
else
{
BC_ERROR(response, fd, request.header.client_id, "Invalid CTRL message size: %d", request.header.size);
}
break;
case CHANNEL_DATA:
/**
* @brief Send message to a group of client
* message is in the following format
* [4bytes group id][data]
*/
(void)memcpy(&hash, &request.data[0], sizeof(hash));
hash = ntohl(hash);
fargv[1] = &request;
fargv[2] = &hash;
bst_for_each(clients, bc_notify, fargv, 3);
break;
case CHANNEL_UNSUBSCRIBE:
M_LOG(MODULE_NAME, "Client %d unsubscribes to the chanel", request.header.client_id);
node_p = bst_find(clients, request.header.client_id);
unsubscribe(node_p, fargv, 1);
clients = bst_delete(clients, request.header.client_id);
break;
default:
M_LOG(MODULE_NAME, "Client %d send message of type %d",
request.header.client_id, request.header.type);
break;
}
if (request.data)
{
free(request.data);
}
}
}
}
// unsubscribe all client
bst_for_each(clients, unsubscribe, fargv, 1);
bst_free(clients);
// close the channel
M_LOG(MODULE_NAME, "Close the channel %s (%d)", argv[2], fd);
request.header.type = CHANNEL_CLOSE;
request.header.size = 0;
request.data = NULL;
if (msg_write(fd, &request) == -1)
{
M_ERROR(MODULE_NAME, "Unable to request channel close");
}
if (msg_read(fd, &response) == 0)
{
if (response.data)
{
free(response.data);
}
}
(void)close(fd);
return 0;
}

View File

@ -1,5 +1,5 @@
# initialise autoconf and set up some basic information about the program were packaging
AC_INIT([antd-publishers], [0.1.1a], [xsang.le@gmail.com])
AC_INIT([antd-publishers], [0.1.2a], [xsang.le@gmail.com])
# Were going to use automake for this project
# [subdir-objects] if needed
@ -89,6 +89,7 @@ AC_CONFIG_FILES([
vterm/Makefile
wfifo/Makefile
syslog/Makefile
broadcast/Makefile
])
if test x"${cam_enable}" == x"yes" ; then

Binary file not shown.

BIN
dist/antd-publishers-0.1.2a.tar.gz vendored Normal file

Binary file not shown.

25
log.h
View File

@ -2,18 +2,33 @@
#define LOG_H
#include <syslog.h>
#include <assert.h>
#define LOG_INIT(m) do { \
#define LOG_INIT(m) \
do \
{ \
if ((getenv("debug") != NULL) && (strcmp(getenv("debug"), "1") == 0)) \
{ \
setlogmask(LOG_UPTO(LOG_INFO)); \
M_LOG(MODULE_NAME, "DEBUG ENABLED"); \
} \
else \
{ \
setlogmask(LOG_UPTO(LOG_NOTICE)); \
} \
openlog((m), LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER); \
} while (0)
#ifdef DEBUG
#define M_LOG(m, a, ...) syslog((LOG_NOTICE), m "_log@[%s: %d]: " a "\n", __FILE__, \
__LINE__, ##__VA_ARGS__)
#else
#define M_LOG(m, a,...) do{}while(0)
#endif
#define M_DEBUG(m, a, ...) syslog((LOG_INFO), m "_log@[%s: %d]: " a "\n", __FILE__, \
__LINE__, ##__VA_ARGS__)
#define M_ERROR(m, a, ...) syslog((LOG_ERR), m "_error@[%s: %d]: " a "\n", __FILE__, \
__LINE__, ##__VA_ARGS__)
#define ASSERT(b, m, ...) \
if (!(b)) \
{ \
M_ERROR(MODULE_NAME, "ASSERT ERROR: " m, ##__VA_ARGS__); \
assert(b); \
}
#endif

124
runner.c
View File

@ -6,76 +6,92 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <antd/list.h>
#include <antd/ini.h>
#include <antd/list.h>
#include <antd/utils.h>
#include "log.h"
#define MODULE_NAME "runner"
#define MAX_STR_LEN 255u
/** up to 20 arguments*/
#define MAX_ARGC 10u
typedef struct
{
char *name;
char strings[MAX_ARGC * 2u][MAX_STR_LEN];
char *envs[MAX_ARGC + 1];
char *params[MAX_ARGC + 1];
size_t n_params;
size_t n_envs;
size_t s_p;
} runner_cmd_t;
static volatile int running = 1;
static list_t pids;
static void int_handler(int dummy) {
static runner_cmd_t cmd;
static void int_handler(int dummy)
{
(void)dummy;
running = 0;
}
static void execute_command(list_t *plist)
{
pid_t pid;
ASSERT(cmd.name != NULL, "Invalid service handler (NULL)");
pid = fork();
ASSERT(pid != -1, "Unable to fork: %s", strerror(errno));
if (pid == 0)
{
execve(cmd.params[0], &cmd.params[0], &cmd.envs[0]);
// Nothing below this line should be executed by child process. If so,
// it means that the execl function wasn't successfull, so lets exit:
_exit(1);
}
else
{
M_LOG(MODULE_NAME, "Running service %s (%d)...", cmd.name, pid);
(void)memset(&cmd, 0, sizeof(cmd));
list_put_i(plist, pid);
}
}
static int ini_handle(void *user_data, const char *section, const char *name,
const char *value)
{
pid_t pid;
char* argv[11];
list_t list;
item_t item;
int i;
UNUSED(user_data);
if (EQU(section, "RUNNER"))
list_t *plist = (list_t *)user_data;
ASSERT(cmd.s_p < MAX_ARGC * 2u, "String buffer overflow: %ld", cmd.s_p);
ASSERT(cmd.n_params <= MAX_ARGC, "Max arguments reached %ld", cmd.n_params);
ASSERT(cmd.n_envs <= MAX_ARGC, "Max environment variables reached %ld", cmd.n_envs);
if ((cmd.name == NULL) || ! EQU(section, cmd.name))
{
if(EQU(name, "service"))
if (cmd.params[0])
{
list = split(value, " ");
i = list_size(list);
if( i > 10)
{
M_ERROR(MODULE_NAME, "Too many arguments %d, expected max 10", i);
return 0;
execute_command(plist);
}
i = 0;
list_for_each(item, list)
{
argv[i] = item->value.ptr;
i++;
cmd.n_params = 1u;
(void)strncpy(cmd.strings[cmd.s_p], section, MAX_STR_LEN);
cmd.name = cmd.strings[cmd.s_p];
cmd.s_p++;
}
argv[i] = NULL;
M_LOG(MODULE_NAME, "Running service %s...", value);
pid = fork();
if(pid == -1)
if (EQU(name, "exec"))
{
M_ERROR(MODULE_NAME, "Unable to fork: %s", strerror(errno));
return 0;
(void)strncpy(cmd.strings[cmd.s_p], value, MAX_STR_LEN);
cmd.params[0] = cmd.strings[cmd.s_p];
cmd.s_p++;
}
if(pid == 0)
if (EQU(name, "param"))
{
// child
execve(argv[0], &argv[0], NULL);
// Nothing below this line should be executed by child process. If so,
// it means that the execl function wasn't successfull, so lets exit:
_exit(1);
}
list_free(&list);
// parent
list_put_i(&pids, pid);
(void)strncpy(cmd.strings[cmd.s_p], value, MAX_STR_LEN);
cmd.params[cmd.n_params] = cmd.strings[cmd.s_p];
cmd.s_p++;
cmd.n_params++;
}
else
{
M_ERROR(MODULE_NAME, "Ignore unknown configuration %s = %s", name, value);
return 0;
}
}
else
{
return 0;
(void)snprintf(cmd.strings[cmd.s_p], MAX_STR_LEN, "%s=%s", name, value);
cmd.envs[cmd.n_envs] = cmd.strings[cmd.s_p];
cmd.s_p++;
cmd.n_envs++;
}
return 1;
}
@ -86,22 +102,24 @@ int main(int argc, char const *argv[])
item_t item;
pid_t w_pid;
int pid_count;
list_t pids;
signal(SIGPIPE, SIG_IGN);
signal(SIGABRT, SIG_IGN);
signal(SIGINT, int_handler);
if (argc != 2)
{
printf("Usage: %s /path/to/conf.ini", argv[0]);
printf("Usage: %s /path/to/conf.ini\n", argv[0]);
return -1;
}
conf_file = argv[1];
LOG_INIT(MODULE_NAME);
M_LOG(MODULE_NAME,"config file is %s", conf_file);
pids = list_init();
if (ini_parse(conf_file, ini_handle, NULL) < 0)
(void)memset(&cmd, 0, sizeof(cmd));
ASSERT(ini_parse(conf_file, ini_handle, &pids) == 0, "Can't load service from '%s'", conf_file);
if (cmd.params[0] != NULL)
{
M_ERROR(MODULE_NAME, "Can't load '%s'", conf_file);
return -1;
execute_command(&pids);
}
pid_count = list_size(pids);
// monitoring the process
@ -114,7 +132,7 @@ int main(int argc, char const *argv[])
if (w_pid == -1 || w_pid > 0)
{
// child exits
M_LOG(MODULE_NAME, "Process %d exits\n", item->value.i);
M_LOG(MODULE_NAME, "Process %d exits", item->value.i);
item->value.i = -1;
}
else

View File

@ -1,5 +1,21 @@
[RUNNER]
service=/opt/www/bin/vterm /opt/www/tmp/channels/antd_hotline.sock
service=/opt/www/bin/wfifo /opt/www/tmp/channels/antd_hotline.sock notification /var/wfifo_notification r
[vterm]
exec = /opt/www/bin/vterm
param = /opt/www/tmp/channels/antd_hotline.sock
debug = 0
[notification_fifo]
exec = /opt/www/bin/wfifo
param = /opt/www/tmp/channels/antd_hotline.sock
param = notification
param = /var/wfifo_notification
param = r
debug = 1
[broadcast]
exec = /opt/www/bin/broadcast
param = /opt/www/tmp/channels/antd_hotline.sock
param = broadcast
debug = 1
;service=/opt/www/bin/wfifo /opt/www/tmp/channels/antd_hotline.sock server_to_client /var/wfifo_s2c r
;service=/opt/www/bin/wfifo /opt/www/tmp/channels/antd_hotline.sock client_to_server /var/wfifo_c2s w

View File

@ -67,6 +67,7 @@ static int msg_check_number(int fd, uint16_t number)
M_ERROR(MODULE_NAME, "Unable to read integer value: %s", strerror(errno));
return -1;
}
value = ntohs(value);
if(number != value)
{
M_ERROR(MODULE_NAME, "Value mismatches: %04X, expected %04X", value, number);
@ -103,6 +104,7 @@ static uint8_t* msg_read_payload(int fd, uint32_t* size)
M_ERROR(MODULE_NAME, "Unable to read payload data size: %s", strerror(errno));
return NULL;
}
*size = ntohl(*size);
if(*size <= 0)
{
return NULL;
@ -168,11 +170,13 @@ int msg_read(int fd, tunnel_msg_t* msg)
M_ERROR(MODULE_NAME, "Unable to read msg channel id");
return -1;
}
msg->header.channel_id = ntohs(msg->header.channel_id);
if(guard_read(fd, &msg->header.client_id, sizeof(msg->header.client_id)) == -1)
{
M_ERROR(MODULE_NAME, "Unable to read msg client id");
return -1;
}
msg->header.client_id = ntohs(msg->header.client_id);
if((msg->data = msg_read_payload(fd, &msg->header.size)) == NULL && msg->header.size != 0)
{
M_ERROR(MODULE_NAME, "Unable to read msg payload data");
@ -193,8 +197,10 @@ int msg_read(int fd, tunnel_msg_t* msg)
int msg_write(int fd, tunnel_msg_t* msg)
{
// write begin magic number
uint16_t number = MSG_MAGIC_BEGIN;
if(guard_write(fd,&number, sizeof(number)) == -1)
uint16_t net16;
uint32_t net32;
net16 = htons(MSG_MAGIC_BEGIN);
if(guard_write(fd,&net16, sizeof(net16)) == -1)
{
M_ERROR(MODULE_NAME, "Unable to write begin magic number: %s", strerror(errno));
return -1;
@ -206,20 +212,22 @@ int msg_write(int fd, tunnel_msg_t* msg)
return -1;
}
// write channel id
if(guard_write(fd,&msg->header.channel_id, sizeof(msg->header.channel_id)) == -1)
net16 = htons(msg->header.channel_id);
if(guard_write(fd,&net16, sizeof(msg->header.channel_id)) == -1)
{
M_ERROR(MODULE_NAME, "Unable to write msg channel id: %s", strerror(errno));
return -1;
}
//write client id
if(guard_write(fd,&msg->header.client_id, sizeof(msg->header.client_id)) == -1)
net16 = htons(msg->header.client_id);
if(guard_write(fd,&net16, sizeof(msg->header.client_id)) == -1)
{
M_ERROR(MODULE_NAME, "Unable to write msg client id: %s", strerror(errno));
return -1;
}
// write payload len
if(guard_write(fd,&msg->header.size, sizeof(msg->header.size)) == -1)
net32 = htonl(msg->header.size);
if(guard_write(fd,&net32, sizeof(msg->header.size)) == -1)
{
M_ERROR(MODULE_NAME, "Unable to write msg payload length: %s", strerror(errno));
return -1;
@ -233,8 +241,8 @@ int msg_write(int fd, tunnel_msg_t* msg)
return -1;
}
}
number = MSG_MAGIC_END;
if(guard_write(fd,&number, sizeof(number)) == -1)
net16 = htons(MSG_MAGIC_END);
if(guard_write(fd,&net16, sizeof(net16)) == -1)
{
M_ERROR(MODULE_NAME, "Unable to write end magic number: %s", strerror(errno));
return -1;

View File

@ -1,6 +1,7 @@
#ifndef TUNNEL_H
#define TUNNEL_H
#include <stdint.h>
#include <netinet/in.h>
#include "log.h"
#define MAX_CHANNEL_PATH 108

View File

@ -423,6 +423,7 @@ int main(const int argc, const char **argv)
int status;
fd_set fd_in;
uint64_t expirations_count;
uint16_t net16;
void *fargv[2];
unsigned int offset = 0;
if (argc != 4)
@ -553,8 +554,10 @@ int main(const int argc, const char **argv)
msg.header.type = CHANNEL_CTRL;
msg.header.size = 6;
msg.data = (uint8_t *)buff;
(void)memcpy(buff, &video_setting.width, sizeof(video_setting.width));
(void)memcpy(buff + sizeof(video_setting.width), &video_setting.height, sizeof(video_setting.height));
net16 = htons(video_setting.width);
(void)memcpy(buff, &net16, sizeof(video_setting.width));
net16 = htons(video_setting.height);
(void)memcpy(buff + sizeof(video_setting.height), &net16, sizeof(video_setting.height));
buff[sizeof(video_setting.width) + sizeof(video_setting.height)] = video_setting.fps;
buff[sizeof(video_setting.width) + sizeof(video_setting.height) + 1] = video_setting.jpeg_quality;
if (msg_write(sock, &msg) == -1)
@ -574,8 +577,10 @@ int main(const int argc, const char **argv)
{
offset = 0;
(void)memcpy(&video_setting.width, msg.data, 2);
video_setting.width = ntohs(video_setting.width);
offset += 2;
(void)memcpy(&video_setting.height, msg.data + offset, 2);
video_setting.height = ntohs(video_setting.height);
offset += 2;
(void)memcpy(&video_setting.fps, msg.data + offset, 1);
offset++;
@ -604,8 +609,10 @@ int main(const int argc, const char **argv)
msg.header.type = CHANNEL_CTRL;
msg.header.size = 6;
msg.data = (uint8_t *)buff;
(void)memcpy(buff, &video_setting.width, sizeof(video_setting.width));
(void)memcpy(buff + sizeof(video_setting.width), &video_setting.height, sizeof(video_setting.height));
net16 = htons(video_setting.width);
(void)memcpy(buff, &net16, sizeof(video_setting.width));
net16 = htons(video_setting.height);
(void)memcpy(buff + sizeof(video_setting.height), &net16, sizeof(video_setting.height));
buff[sizeof(video_setting.width) + sizeof(video_setting.height)] = video_setting.fps;
buff[sizeof(video_setting.width) + sizeof(video_setting.height) + 1] = video_setting.jpeg_quality;
fargv[0] = (void *)&msg;

View File

@ -19,7 +19,8 @@
#define MODULE_NAME "vterm"
typedef struct{
typedef struct
{
int fdm;
pid_t pid;
int cid;
@ -29,7 +30,8 @@ static bst_node_t* processes = NULL;
static volatile int running = 1;
static void int_handler(int dummy) {
static void int_handler(int dummy)
{
(void)dummy;
running = 0;
}
@ -152,7 +154,6 @@ static void terminal_kill(int client_id, int should_delete)
if (should_delete)
processes = bst_delete(processes, node->key);
// wait child
}
}
}
@ -453,9 +454,10 @@ int main(int argc, char** argv)
{
(void)memcpy(&ncol, msg.data, sizeof(ncol));
(void)memcpy(&nrow, msg.data + sizeof(ncol), sizeof(nrow));
ncol = ntohl(ncol);
nrow = ntohl(nrow);
M_LOG(MODULE_NAME, "Client %d request terminal window resize of (%d,%d)", msg.header.client_id, ncol, nrow);
terminal_resize(msg.header.client_id, ncol, nrow);
}
else
{
@ -497,11 +499,14 @@ int main(int argc, char** argv)
list_for_each(item, list)
{
processes = bst_delete(processes, ((bst_node_t *)(item->value.ptr))->key);
if (((bst_node_t *)(item->value.ptr))->data)
{
free(((bst_node_t *)(item->value.ptr))->data);
}
item->value.ptr = NULL;
}
list_free(&list);
}
}
}