mirror of
https://github.com/lxsang/antd-tunnel-plugin
synced 2024-11-16 09:48:21 +01:00
refactory code, fix seg error
This commit is contained in:
parent
b09e44b67d
commit
0c1630c24d
@ -14,6 +14,6 @@ AM_CPPFLAGS += -W -Wall -g -std=c99 -fPIC
|
|||||||
|
|
||||||
lib_LTLIBRARIES = tunnel.la
|
lib_LTLIBRARIES = tunnel.la
|
||||||
tunnel_la_LDFLAGS = -module -avoid-version -shared
|
tunnel_la_LDFLAGS = -module -avoid-version -shared
|
||||||
tunnel_la_SOURCES = tunnel.c bst.c
|
tunnel_la_SOURCES = tunnel.c
|
||||||
|
|
||||||
EXTRA_DIST = README.md
|
EXTRA_DIST = README.md
|
102
bst.c
102
bst.c
@ -1,102 +0,0 @@
|
|||||||
#include<stdio.h>
|
|
||||||
#include<stdlib.h>
|
|
||||||
|
|
||||||
#include "bst.h"
|
|
||||||
|
|
||||||
void bst_free(bst_node_t* root)
|
|
||||||
{
|
|
||||||
if(root != NULL)
|
|
||||||
{
|
|
||||||
bst_free(root->left);
|
|
||||||
bst_free(root->right);
|
|
||||||
free(root);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bst_node_t* bst_insert(bst_node_t* root, int key, void* data)
|
|
||||||
{
|
|
||||||
if(root == NULL)
|
|
||||||
{
|
|
||||||
root = malloc(sizeof(bst_node_t));
|
|
||||||
root->key = key;
|
|
||||||
root->data = data;
|
|
||||||
root->left = root->right = NULL;
|
|
||||||
}
|
|
||||||
else if(key < root->key)
|
|
||||||
root->left = bst_insert(root->left, key, data);
|
|
||||||
else if(key > root->key)
|
|
||||||
root->right = bst_insert(root->right, key, data);
|
|
||||||
else
|
|
||||||
root->data = data;
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
|
|
||||||
bst_node_t* bst_find_min(bst_node_t* root)
|
|
||||||
{
|
|
||||||
if(root == NULL)
|
|
||||||
return NULL;
|
|
||||||
else if(root->left == NULL)
|
|
||||||
return root;
|
|
||||||
else
|
|
||||||
return bst_find_min(root->left);
|
|
||||||
}
|
|
||||||
|
|
||||||
bst_node_t* bst_find_max(bst_node_t* root)
|
|
||||||
{
|
|
||||||
if(root == NULL)
|
|
||||||
return NULL;
|
|
||||||
else if(root->right == NULL)
|
|
||||||
return root;
|
|
||||||
else
|
|
||||||
return bst_find_max(root->right);
|
|
||||||
}
|
|
||||||
|
|
||||||
bst_node_t* bst_find(bst_node_t* root, int x)
|
|
||||||
{
|
|
||||||
if(root == NULL)
|
|
||||||
return NULL;
|
|
||||||
else if(x < root->key)
|
|
||||||
return bst_find(root->left, x);
|
|
||||||
else if(x > root->key)
|
|
||||||
return bst_find(root->right, x);
|
|
||||||
else
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bst_node_t* bst_delete(bst_node_t* root, int x)
|
|
||||||
{
|
|
||||||
bst_node_t* temp;
|
|
||||||
if(root == NULL)
|
|
||||||
return NULL;
|
|
||||||
else if(x < root->key)
|
|
||||||
root->left = bst_delete(root->left, x);
|
|
||||||
else if(x > root->key)
|
|
||||||
root->right = bst_delete(root->right, x);
|
|
||||||
else if(root->left && root->right)
|
|
||||||
{
|
|
||||||
temp = bst_find_min(root->right);
|
|
||||||
root->key = temp->key;
|
|
||||||
root->data = temp->data;
|
|
||||||
root->right = bst_delete(root->right, root->key);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
temp = root;
|
|
||||||
if(root->left == NULL)
|
|
||||||
root = root->right;
|
|
||||||
else if(root->right == NULL)
|
|
||||||
root = root->left;
|
|
||||||
free(temp);
|
|
||||||
}
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
|
|
||||||
void bst_for_each(bst_node_t* root, void (*callback)(bst_node_t*, void **, int), void** args, int argc)
|
|
||||||
{
|
|
||||||
if(root == NULL)
|
|
||||||
return;
|
|
||||||
bst_for_each(root->left, callback, args, argc);
|
|
||||||
callback(root, args, argc);
|
|
||||||
bst_for_each(root->right, callback, args, argc);
|
|
||||||
}
|
|
19
bst.h
19
bst.h
@ -1,19 +0,0 @@
|
|||||||
#ifndef BST_H
|
|
||||||
#define BST_H 1
|
|
||||||
|
|
||||||
typedef struct _tree_node
|
|
||||||
{
|
|
||||||
int key;
|
|
||||||
void* data;
|
|
||||||
struct _tree_node* left;
|
|
||||||
struct _tree_node* right;
|
|
||||||
} bst_node_t;
|
|
||||||
|
|
||||||
void bst_free(bst_node_t* root);
|
|
||||||
bst_node_t* bst_insert(bst_node_t* root, int key, void* data);
|
|
||||||
bst_node_t* bst_find_min(bst_node_t* root);
|
|
||||||
bst_node_t* bst_find_max(bst_node_t* root);
|
|
||||||
bst_node_t* bst_find(bst_node_t* root, int x);
|
|
||||||
bst_node_t* bst_delete(bst_node_t* root, int x);
|
|
||||||
void bst_for_each(bst_node_t* root, void (*callback)(bst_node_t*, void **, int), void** args, int argc);
|
|
||||||
#endif
|
|
27
tunnel.c
27
tunnel.c
@ -12,7 +12,7 @@
|
|||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
#include "bst.h"
|
#include <antd/bst.h>
|
||||||
#define MAX_CHANNEL_PATH 108
|
#define MAX_CHANNEL_PATH 108
|
||||||
#define MAX_CHANNEL_NAME 64
|
#define MAX_CHANNEL_NAME 64
|
||||||
#define HOT_LINE_SOCKET "antd_hotline.sock"
|
#define HOT_LINE_SOCKET "antd_hotline.sock"
|
||||||
@ -28,6 +28,7 @@
|
|||||||
#define CHANNEL_OPEN (uint8_t)0x4
|
#define CHANNEL_OPEN (uint8_t)0x4
|
||||||
#define CHANNEL_CLOSE (uint8_t)0x5
|
#define CHANNEL_CLOSE (uint8_t)0x5
|
||||||
#define CHANNEL_DATA (uint8_t)0x6
|
#define CHANNEL_DATA (uint8_t)0x6
|
||||||
|
#define CHANNEL_CTRL (uint8_t)0x7
|
||||||
//#define CHANNEL_LIST (uint8_t)0x7
|
//#define CHANNEL_LIST (uint8_t)0x7
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int sock;
|
int sock;
|
||||||
@ -177,6 +178,7 @@ 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;
|
||||||
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);
|
||||||
@ -460,12 +462,12 @@ static void monitor_hotline(int listen_fd)
|
|||||||
// get channel name
|
// get channel name
|
||||||
if(msg.header.size > MAX_CHANNEL_NAME)
|
if(msg.header.size > MAX_CHANNEL_NAME)
|
||||||
{
|
{
|
||||||
if(msg.data)
|
|
||||||
free(msg.data);
|
|
||||||
msg.header.type = CHANNEL_ERROR;
|
msg.header.type = CHANNEL_ERROR;
|
||||||
(void) snprintf(buff, MAX_CHANNEL_NAME, "Channel name exceeds %d bytes", MAX_CHANNEL_NAME);
|
(void) snprintf(buff, MAX_CHANNEL_NAME, "Channel name exceeds %d bytes", MAX_CHANNEL_NAME);
|
||||||
LOG("%s", buff);
|
LOG("%s", buff);
|
||||||
msg.header.size = strlen(buff);
|
msg.header.size = strlen(buff);
|
||||||
|
if(msg.data)
|
||||||
|
free(msg.data);
|
||||||
msg.data = (uint8_t*)buff;
|
msg.data = (uint8_t*)buff;
|
||||||
if(msg_write(fd, &msg) == -1)
|
if(msg_write(fd, &msg) == -1)
|
||||||
{
|
{
|
||||||
@ -478,6 +480,8 @@ static void monitor_hotline(int listen_fd)
|
|||||||
buff[msg.header.size] = '\0';
|
buff[msg.header.size] = '\0';
|
||||||
LOG("Open a new channel: %s (%d)", buff, fd);
|
LOG("Open a new channel: %s (%d)", buff, fd);
|
||||||
channel_open(fd, buff);
|
channel_open(fd, buff);
|
||||||
|
if(msg.data)
|
||||||
|
free(msg.data);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -485,6 +489,8 @@ static void monitor_hotline(int listen_fd)
|
|||||||
msg.header.type = CHANNEL_ERROR;
|
msg.header.type = CHANNEL_ERROR;
|
||||||
(void) snprintf(buff, MAX_CHANNEL_NAME, "Unsupported msg type %d in hotline", (int)msg.header.type);
|
(void) snprintf(buff, MAX_CHANNEL_NAME, "Unsupported msg type %d in hotline", (int)msg.header.type);
|
||||||
msg.header.size = strlen(buff);
|
msg.header.size = strlen(buff);
|
||||||
|
if(msg.data)
|
||||||
|
free(msg.data);
|
||||||
msg.data = (uint8_t*)buff;
|
msg.data = (uint8_t*)buff;
|
||||||
LOG("%s", buff);
|
LOG("%s", buff);
|
||||||
if(msg_write(fd, &msg) == -1)
|
if(msg_write(fd, &msg) == -1)
|
||||||
@ -515,7 +521,7 @@ static void handle_channel(bst_node_t* node, void** args, int argc)
|
|||||||
node->data = NULL;
|
node->data = NULL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LOG("Got new data on channel %s (%d)", channel->name, channel->sock);
|
//LOG("Got new data on channel %s (%d)", channel->name, channel->sock);
|
||||||
// handle msg read
|
// handle msg read
|
||||||
if(msg_read(channel->sock, &msg) == -1)
|
if(msg_read(channel->sock, &msg) == -1)
|
||||||
{
|
{
|
||||||
@ -528,13 +534,10 @@ static void handle_channel(bst_node_t* node, void** args, int argc)
|
|||||||
case CHANNEL_ERROR:
|
case CHANNEL_ERROR:
|
||||||
case CHANNEL_DATA:
|
case CHANNEL_DATA:
|
||||||
case CHANNEL_UNSUBSCRIBE:
|
case CHANNEL_UNSUBSCRIBE:
|
||||||
|
case CHANNEL_CTRL:
|
||||||
// forward message to the correct client in the channel
|
// forward message to the correct client in the channel
|
||||||
msg.header.channel_id = node->key;
|
msg.header.channel_id = node->key;
|
||||||
client = bst_find(channel->subscribers, msg.header.client_id);
|
client = bst_find(channel->subscribers, msg.header.client_id);
|
||||||
if(msg.header.type == CHANNEL_UNSUBSCRIBE)
|
|
||||||
{
|
|
||||||
channel->subscribers = bst_delete(channel->subscribers, msg.header.client_id);
|
|
||||||
}
|
|
||||||
if(client != NULL)
|
if(client != NULL)
|
||||||
{
|
{
|
||||||
rq = (antd_client_t*) client->data;
|
rq = (antd_client_t*) client->data;
|
||||||
@ -547,6 +550,10 @@ static void handle_channel(bst_node_t* node, void** args, int argc)
|
|||||||
{
|
{
|
||||||
ERROR("Unable to find client %d to write on channel %s", msg.header.client_id, channel->name);
|
ERROR("Unable to find client %d to write on channel %s", msg.header.client_id, channel->name);
|
||||||
}
|
}
|
||||||
|
if(msg.header.type == CHANNEL_UNSUBSCRIBE)
|
||||||
|
{
|
||||||
|
channel->subscribers = bst_delete(channel->subscribers, msg.header.client_id);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CHANNEL_CLOSE:
|
case CHANNEL_CLOSE:
|
||||||
@ -555,10 +562,13 @@ static void handle_channel(bst_node_t* node, void** args, int argc)
|
|||||||
node->data = NULL;
|
node->data = NULL;
|
||||||
list_put_ptr(channel_list, node);
|
list_put_ptr(channel_list, node);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
LOG("Message type %d is not supported in client-application communication", msg.header.type);
|
LOG("Message type %d is not supported in client-application communication", msg.header.type);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if(msg.data)
|
||||||
|
free(msg.data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static void set_sock_fd(bst_node_t* node, void** args, int argc)
|
static void set_sock_fd(bst_node_t* node, void** args, int argc)
|
||||||
@ -701,6 +711,7 @@ static void process_client_message(antd_tunnel_msg_t* msg, antd_client_t* client
|
|||||||
case CHANNEL_OK:
|
case CHANNEL_OK:
|
||||||
case CHANNEL_ERROR:
|
case CHANNEL_ERROR:
|
||||||
case CHANNEL_DATA:
|
case CHANNEL_DATA:
|
||||||
|
case CHANNEL_CTRL:
|
||||||
node = bst_find(g_tunnel.channels, msg->header.channel_id);
|
node = bst_find(g_tunnel.channels, msg->header.channel_id);
|
||||||
if(node)
|
if(node)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user