1
0
mirror of https://github.com/lxsang/antd-tunnel-plugin synced 2024-11-16 01:38:22 +01:00

reduce frame sie

This commit is contained in:
lxsang 2020-11-29 13:26:09 +01:00
parent 1938f8f099
commit 5a3f4fc81d
2 changed files with 39 additions and 59 deletions

Binary file not shown.

View File

@ -20,12 +20,12 @@
#define HOT_LINE_SOCKET "antd_hotline.sock" #define HOT_LINE_SOCKET "antd_hotline.sock"
#define SOCK_DIR_NAME "channels" #define SOCK_DIR_NAME "channels"
#define MAX_CHANNEL_ID 65535u
#define MAX_CHANNEL_PATH (sizeof(__plugin__.tmpdir) + strlen(SOCK_DIR_NAME) + strlen(HOT_LINE_SOCKET) + 2) #define MAX_CHANNEL_PATH (sizeof(__plugin__.tmpdir) + strlen(SOCK_DIR_NAME) + strlen(HOT_LINE_SOCKET) + 2)
#if VERIFY_HEADER #define MSG_MAGIC_BEGIN (uint16_t)0x414e //AN
#define MSG_MAGIC_BEGIN 0x414e5444 //ANTD #define MSG_MAGIC_END (uint16_t)0x5444 //TD
#define MSG_MAGIC_END 0x44544e41 //DTNA
#endif
#define CHANNEL_OK (uint8_t)0x0 #define CHANNEL_OK (uint8_t)0x0
#define CHANNEL_ERROR (uint8_t)0x1 #define CHANNEL_ERROR (uint8_t)0x1
@ -44,9 +44,9 @@ typedef struct {
typedef struct { typedef struct {
uint8_t type; uint8_t type;
int channel_id; uint16_t channel_id;
int client_id; uint16_t client_id;
int size; uint16_t size;
} antd_tunnel_msg_h_t; } antd_tunnel_msg_h_t;
typedef struct{ typedef struct{
@ -55,7 +55,7 @@ typedef struct{
} antd_tunnel_msg_t; } antd_tunnel_msg_t;
/** /**
* Message is sent in the following format * Message is sent in the following format
* |BEGIN MAGIC(4)|MSG TYPE(1)| CHANNEL ID (4)| CLIENT ID (4)| data length (4)| data(m) | END MAGIC(4)| * |BEGIN MAGIC(2)|MSG TYPE(1)| CHANNEL ID (2)| CLIENT ID (2)| data length (2)| data(m) | END MAGIC(2)|
*/ */
typedef struct { typedef struct {
@ -118,9 +118,9 @@ static int mk_socket(const char* name, char* path)
return fd; return fd;
} }
static int msg_check_number(int fd, int number) static int msg_check_number(int fd, uint16_t number)
{ {
int value; uint16_t value;
if(read(fd,&value,sizeof(value)) == -1) if(read(fd,&value,sizeof(value)) == -1)
{ {
ERROR("Unable to read integer value on socket %d: %s", fd, strerror(errno)); ERROR("Unable to read integer value on socket %d: %s", fd, strerror(errno));
@ -128,7 +128,7 @@ static int msg_check_number(int fd, int number)
} }
if(number != value) if(number != value)
{ {
ERROR("Value mismatches: %0x%04X, expected %0x%04X", value, number); ERROR("Value mismatches: %0x%02X, expected %0x%02X", value, number);
return -1; return -1;
} }
return 0; return 0;
@ -156,7 +156,7 @@ static int msg_read_string(int fd, char* buffer, uint8_t max_length)
} }
*/ */
static uint8_t* msg_read_payload(int fd, int* size) static uint8_t* msg_read_payload(int fd, uint16_t* size)
{ {
uint8_t* data; uint8_t* data;
if(read(fd,size,sizeof(*size)) == -1) if(read(fd,size,sizeof(*size)) == -1)
@ -187,13 +187,11 @@ static uint8_t* msg_read_payload(int fd, int* size)
static int msg_read(int fd, antd_tunnel_msg_t* msg) static int msg_read(int fd, antd_tunnel_msg_t* msg)
{ {
msg->data = NULL; msg->data = NULL;
#ifdef VERIFY_HEADER
if(msg_check_number(fd, MSG_MAGIC_BEGIN) == -1) if(msg_check_number(fd, MSG_MAGIC_BEGIN) == -1)
{ {
ERROR("Unable to check begin magic number on socket: %d", fd); ERROR("Unable to check begin magic number on socket: %d", fd);
return -1; return -1;
} }
#endif
if(read(fd,&msg->header.type,sizeof(msg->header.type)) == -1) if(read(fd,&msg->header.type,sizeof(msg->header.type)) == -1)
{ {
ERROR("Unable to read msg type: %s", strerror(errno)); ERROR("Unable to read msg type: %s", strerror(errno));
@ -219,7 +217,6 @@ static int msg_read(int fd, antd_tunnel_msg_t* msg)
ERROR("Unable to read msg payload data"); ERROR("Unable to read msg payload data");
return -1; return -1;
} }
#ifdef VERIFY_HEADER
if(msg_check_number(fd, MSG_MAGIC_END) == -1) if(msg_check_number(fd, MSG_MAGIC_END) == -1)
{ {
if(msg->data) if(msg->data)
@ -229,21 +226,18 @@ static int msg_read(int fd, antd_tunnel_msg_t* msg)
ERROR("Unable to check end magic number"); ERROR("Unable to check end magic number");
return -1; return -1;
} }
#endif
return 0; return 0;
} }
static int msg_write(int fd, antd_tunnel_msg_t* msg) static int msg_write(int fd, antd_tunnel_msg_t* msg)
{ {
#ifdef VERIFY_HEADER
// write begin magic number // write begin magic number
int number = MSG_MAGIC_BEGIN; uint16_t number = MSG_MAGIC_BEGIN;
if(write(fd,&number, sizeof(number)) == -1) if(write(fd,&number, sizeof(number)) == -1)
{ {
ERROR("Unable to write begin magic number: %s", strerror(errno)); ERROR("Unable to write begin magic number: %s", strerror(errno));
return -1; return -1;
} }
#endif
// write type // write type
if(write(fd,&msg->header.type, sizeof(msg->header.type)) == -1) if(write(fd,&msg->header.type, sizeof(msg->header.type)) == -1)
{ {
@ -277,45 +271,35 @@ static int msg_write(int fd, antd_tunnel_msg_t* msg)
return -1; return -1;
} }
} }
#ifdef VERIFY_HEADER
number = MSG_MAGIC_END; number = MSG_MAGIC_END;
if(write(fd,&number, sizeof(number)) == -1) if(write(fd,&number, sizeof(number)) == -1)
{ {
ERROR("Unable to write end magic number: %s", strerror(errno)); ERROR("Unable to write end magic number: %s", strerror(errno));
return -1; return -1;
} }
#endif
return 0; return 0;
} }
static void write_msg_to_client(antd_tunnel_msg_t* msg, antd_client_t* client) static void write_msg_to_client(antd_tunnel_msg_t* msg, antd_client_t* client)
{ {
uint8_t* buffer; uint8_t* buffer;
int long_value = 0; uint16_t u16 = 0;
int offset = 0; int offset = 0;
long_value = msg->header.size + buffer = (uint8_t*) malloc(msg->header.size +
#ifdef VERIFY_HEADER
sizeof((int)MSG_MAGIC_BEGIN) + sizeof((int)MSG_MAGIC_BEGIN) +
#endif
sizeof(msg->header.type) + sizeof(msg->header.type) +
sizeof(msg->header.channel_id) + sizeof(msg->header.channel_id) +
sizeof(msg->header.client_id) + sizeof(msg->header.client_id) +
sizeof(msg->header.size) sizeof(msg->header.size) +
#ifdef VERIFY_HEADER sizeof((int)MSG_MAGIC_END));
+sizeof((int)MSG_MAGIC_END)
#endif
;
buffer = (uint8_t*) malloc(long_value);
if(buffer == NULL) if(buffer == NULL)
{ {
ERROR("unable to allocate memory for write"); ERROR("unable to allocate memory for write");
return; return;
} }
#ifdef VERIFY_HEADER
// magic // magic
long_value = (int) MSG_MAGIC_BEGIN; u16 = MSG_MAGIC_BEGIN;
(void)memcpy(buffer,&long_value,sizeof(long_value)); (void)memcpy(buffer,&u16,sizeof(u16));
offset += sizeof(long_value); offset += sizeof(u16);
#endif
// type // type
(void)memcpy(buffer+offset,&msg->header.type,sizeof(msg->header.type)); (void)memcpy(buffer+offset,&msg->header.type,sizeof(msg->header.type));
offset += sizeof(msg->header.type); offset += sizeof(msg->header.type);
@ -332,12 +316,10 @@ static void write_msg_to_client(antd_tunnel_msg_t* msg, antd_client_t* client)
(void)memcpy(buffer+offset,msg->data,msg->header.size); (void)memcpy(buffer+offset,msg->data,msg->header.size);
offset += msg->header.size; offset += msg->header.size;
#ifdef VERIFY_HEADER
// magic end // magic end
long_value = (int) MSG_MAGIC_END; u16 = MSG_MAGIC_END;
(void)memcpy(buffer+offset,&long_value,sizeof(long_value)); (void)memcpy(buffer+offset,&u16,sizeof(u16));
offset += sizeof(long_value); offset += sizeof(u16);
#endif
// write it to the websocket // write it to the websocket
ws_b(client,buffer, offset); ws_b(client,buffer, offset);
@ -353,7 +335,7 @@ static void unsubscribe(bst_node_t* node, void** argv, int argc)
antd_tunnel_msg_t msg; antd_tunnel_msg_t msg;
if(node->data != NULL) if(node->data != NULL)
{ {
msg.header.channel_id = simple_hash(channel->name); msg.header.channel_id = hash(channel->name, MAX_CHANNEL_ID);
msg.header.client_id = node->key; msg.header.client_id = node->key;
msg.header.type = CHANNEL_UNSUBSCRIBE; msg.header.type = CHANNEL_UNSUBSCRIBE;
msg.header.size = 0; msg.header.size = 0;
@ -381,7 +363,7 @@ static void channel_open(int fd, const char* name)
char buffer[BUFFLEN]; char buffer[BUFFLEN];
antd_tunnel_channel_t* channel = NULL; antd_tunnel_channel_t* channel = NULL;
bst_node_t* node; bst_node_t* node;
int hash_val = simple_hash(name); int hash_val = hash(name, MAX_CHANNEL_ID);
antd_tunnel_msg_t msg; antd_tunnel_msg_t msg;
msg.data = (uint8_t*)buffer; msg.data = (uint8_t*)buffer;
msg.header.channel_id = 0; msg.header.channel_id = 0;
@ -762,7 +744,8 @@ static void process_client_message(antd_tunnel_msg_t* msg, antd_client_t* client
{ {
(void)memcpy(buff, msg->data, msg->header.size); (void)memcpy(buff, msg->data, msg->header.size);
buff[msg->header.size] = '\0'; buff[msg->header.size] = '\0';
hash_val = simple_hash(buff); hash_val = hash(buff, MAX_CHANNEL_ID);
LOG("Requested channel: [%s]: %d", buff, hash_val);
} }
else else
{ {
@ -807,7 +790,7 @@ static void process_client_message(antd_tunnel_msg_t* msg, antd_client_t* client
} }
else else
{ {
(void) snprintf(buff, BUFFLEN, "Channel not found"); (void) snprintf(buff, BUFFLEN, "Channel not found: %d", hash_val);
msg->header.size = strlen(buff); msg->header.size = strlen(buff);
msg->data = (uint8_t*)buff; msg->data = (uint8_t*)buff;
ERROR("%s", buff); ERROR("%s", buff);
@ -837,7 +820,7 @@ static void unsubscribe_notify_handle(bst_node_t* node, void** argv, int argc)
if(channel != NULL) if(channel != NULL)
{ {
msg.header.type = CHANNEL_UNSUBSCRIBE; msg.header.type = CHANNEL_UNSUBSCRIBE;
msg.header.channel_id = simple_hash(channel->name); msg.header.channel_id = hash(channel->name, MAX_CHANNEL_ID);
msg.header.client_id = node->key; msg.header.client_id = node->key;
msg.header.size = 0; msg.header.size = 0;
msg.data = NULL; msg.data = NULL;
@ -887,7 +870,8 @@ void *handle(void *rq_data)
struct timeval timeout; struct timeval timeout;
int status; int status;
fd_set fd_in; fd_set fd_in;
int long_value, offset; int offset;
uint16_t u16;
task->priority++; task->priority++;
void * argv[1]; void * argv[1];
@ -955,18 +939,16 @@ void *handle(void *rq_data)
if(h->plen == 0) if(h->plen == 0)
{ {
offset = 0; offset = 0;
#ifdef VERIFY_HEADER
// verify begin magic // verify begin magic
(void)memcpy(&long_value, buffer,sizeof(long_value)); (void)memcpy(&u16, buffer,sizeof(u16));
offset += sizeof(long_value); offset += sizeof(u16);
if(long_value != MSG_MAGIC_BEGIN) if(u16 != MSG_MAGIC_BEGIN)
{ {
ERROR("Invalid begin magic number: %d, expected %d", long_value, MSG_MAGIC_BEGIN); ERROR("Invalid begin magic number: %d, expected %d", u16, MSG_MAGIC_BEGIN);
free(buffer); free(buffer);
free(h); free(h);
return task; return task;
} }
#endif
// msgtype // msgtype
(void) memcpy(&msg.header.type, buffer + offset, sizeof(msg.header.type)); (void) memcpy(&msg.header.type, buffer + offset, sizeof(msg.header.type));
offset += sizeof(msg.header.type); offset += sizeof(msg.header.type);
@ -987,18 +969,16 @@ void *handle(void *rq_data)
msg.data = buffer + offset; msg.data = buffer + offset;
offset += msg.header.size; offset += msg.header.size;
#ifdef VERIFY_HEADER
// verify end magic // verify end magic
(void)memcpy(&long_value, buffer + offset ,sizeof(long_value)); (void)memcpy(&u16, buffer + offset ,sizeof(u16));
offset += sizeof(long_value); offset += sizeof(u16);
if(long_value != MSG_MAGIC_END) if(u16 != MSG_MAGIC_END)
{ {
ERROR("Invalid end magic number: %d, expected %d", long_value, MSG_MAGIC_END); ERROR("Invalid end magic number: %d, expected %d", u16, MSG_MAGIC_END);
free(buffer); free(buffer);
free(h); free(h);
return task; return task;
} }
#endif
// now we have the message // now we have the message
pthread_mutex_lock(&g_tunnel.lock); pthread_mutex_lock(&g_tunnel.lock);