summaryrefslogtreecommitdiff
path: root/access
diff options
context:
space:
mode:
author崔一鸣 <[email protected]>2018-11-16 15:41:49 +0800
committer崔一鸣 <[email protected]>2018-11-16 15:41:49 +0800
commit957151da0b0c0ad7be5c6577d8cb7619cbcbeae0 (patch)
tree57cfc85eea39c4ebbfce93eb1ccff2f1a15e83b3 /access
parentde5be3b41f49183163a6576728b9578ad81d8d66 (diff)
增加和mrl通过udp通信模块,修复一些已知bug
Diffstat (limited to 'access')
-rw-r--r--access/CMakeLists.txt6
-rw-r--r--access/include/ip_mgr.h24
-rw-r--r--access/include/nat.h2
-rw-r--r--access/include/udp_server.h8
-rw-r--r--access/src/ip_mgr.cpp65
-rw-r--r--access/src/main.cpp120
-rw-r--r--access/src/nat.cpp146
-rw-r--r--access/src/udp_server.cpp52
-rw-r--r--access/src/vpn_monitor.cpp54
-rw-r--r--access/test/test_MESA_htable.cpp18
-rw-r--r--access/test/test_maat_redis.cpp78
11 files changed, 407 insertions, 166 deletions
diff --git a/access/CMakeLists.txt b/access/CMakeLists.txt
index 6fa5bf6..25f60f0 100644
--- a/access/CMakeLists.txt
+++ b/access/CMakeLists.txt
@@ -1,4 +1,4 @@
-add_executable(mgw src/main.cpp src/vpn_monitor.cpp src/nat.cpp src/ip_mgr.cpp)
+add_executable(mgw src/main.cpp src/vpn_monitor.cpp src/nat.cpp src/ip_mgr.cpp src/udp_server.cpp)
target_include_directories(mgw PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
@@ -15,7 +15,7 @@ target_link_libraries(mgw pthread dl
# target_link_libraries(tfe -Wl,--whole-archive pangu-http -Wl,--no-whole-archive)
-#[[add_executable(test_maat_redis test/test_maat_redis.cpp)
+add_executable(test_maat_redis test/test_maat_redis.cpp)
target_include_directories(test_maat_redis PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
@@ -26,7 +26,7 @@ target_link_libraries(test_maat_redis pthread dl
MESA_htable
wiredcfg
MESA_field_stat
- maatframe)]]
+ maatframe)
add_executable(test_tun test/test_tun.cpp)
diff --git a/access/include/ip_mgr.h b/access/include/ip_mgr.h
index 6d4f73d..21e65a5 100644
--- a/access/include/ip_mgr.h
+++ b/access/include/ip_mgr.h
@@ -1,13 +1,33 @@
-struct cand_ip
+#define IP_MGR_MAC_LEN 18
+#define IP_MGR_IP_LEN 16
+#define IP_MGR_PORT_LEN 6
+
+struct ip_mgr_cand_ip
{
uint32_t ip;
int reference;
+ struct ip_mgr_vxlan_info *vxlan_info;
};
+struct ip_mgr_vxlan_info
+{
+ char vxlan_outer_local_mac[IP_MGR_MAC_LEN];//接收到的数据包的本地设备MAC地址
+ char vxlan_outer_gdev_mac[IP_MGR_MAC_LEN];//接收到的数据包的串行设备MAC地址
+ char vxlan_outer_local_ip[IP_MGR_IP_LEN];
+ char vxlan_outer_gdev_ip[IP_MGR_IP_LEN];//串行设备IP
+ char vxlan_outer_gdev_port[IP_MGR_PORT_LEN];
+ char vxlan_outer_local_port[IP_MGR_PORT_LEN];
+ int vxlan_encap_type;
+ int vxlan_link_id;
+ int vxlan_link_dir;
+ char vxlan_inner_smac[IP_MGR_MAC_LEN];
+ char vxlan_inner_dmac[IP_MGR_MAC_LEN];
+};
+
struct ip_mgr_handle;
struct ip_mgr_handle *ip_mgr_init(void *logger, Maat_feather_t feather, struct htable_opts* opts);
void ip_mgr_destroy(struct ip_mgr_handle *handle);
-struct cand_ip* ip_mgr_candidata_ip_get(struct ip_mgr_handle *handle, const char *user_name);
+struct ip_mgr_cand_ip* ip_mgr_candidata_ip_get(struct ip_mgr_handle *handle, const char *user_name);
bool ip_mgr_candidate_ip_verify(struct ip_mgr_handle *handle, uint32_t ip);
diff --git a/access/include/nat.h b/access/include/nat.h
index a18c119..1dd7384 100644
--- a/access/include/nat.h
+++ b/access/include/nat.h
@@ -4,5 +4,5 @@
struct nat_handle;
struct nat_handle * nat_init(MESA_htable_handle htable, void *logger, struct htable_opts* opts);
void nat_destroy(struct nat_handle *handle);
-int nat_src_convert(struct nat_handle *handle, struct ip_mgr_handle *_ip_mgr_handle, char *buff, int len);
+int nat_src_convert(struct nat_handle *handle, struct ip_mgr_handle *_ip_mgr_handle, char *buff, int len, struct ip_mgr_vxlan_info **vxlan);
int nat_dest_convert(struct nat_handle *handle, char *buff, int len); \ No newline at end of file
diff --git a/access/include/udp_server.h b/access/include/udp_server.h
new file mode 100644
index 0000000..930dc13
--- /dev/null
+++ b/access/include/udp_server.h
@@ -0,0 +1,8 @@
+struct udp_server_args
+{
+ void *logger;
+ const char *profile;
+ struct nat_handle *_nat_handle;
+ struct mgw_tun_handle *tun_handle;
+};
+void * thread_udp_server(void *args);
diff --git a/access/src/ip_mgr.cpp b/access/src/ip_mgr.cpp
index 0322463..8d8803a 100644
--- a/access/src/ip_mgr.cpp
+++ b/access/src/ip_mgr.cpp
@@ -43,9 +43,10 @@ struct ip_verify_htable_value
uint32_t ip;
bool is_valid;
};
+
struct cand_ip_htable_value
{
- struct cand_ip *ip_group[GROUP_IP_MAX];
+ struct ip_mgr_cand_ip *ip_group[GROUP_IP_MAX];
unsigned int num;
};
@@ -98,7 +99,7 @@ static long user_policy_htable_query_cb(void *data, const uchar *key, uint size,
}
-struct cand_ip* ip_mgr_candidata_ip_get(struct ip_mgr_handle *handle, const char *user_name)
+struct ip_mgr_cand_ip* ip_mgr_candidata_ip_get(struct ip_mgr_handle *handle, const char *user_name)
{
void *logger = handle->logger;
long user_policy_cb_rtn = 0;
@@ -141,7 +142,13 @@ static void user_info_htable_data_free_cb(void *data)
static void cand_ip_htable_data_free_cb(void *data)
{
- FREE(&data);
+ struct cand_ip_htable_value *value = (struct cand_ip_htable_value *)data;
+ int num = value->num;
+ for(int i = 0; i < num; i++)
+ {
+ FREE(&(value->ip_group[i]));
+ }
+ FREE(&value);
}
static void ip_verify_htable_data_free_cb(void *data)
@@ -248,17 +255,18 @@ static void Maat_cand_ip_update_cb(int table_id, const char* table_line, void* a
struct ip_mgr_handle *handle = (struct ip_mgr_handle *)args;
void *logger = handle->logger;
MGW_LOG_INFO(logger, "Maat_redis: update callback, table is CAND_IP_TABLE");
+ //MGW_LOG_INFO(logger, "Maat_redis: update callback, table_line is %s", table_line);
int config_id, group_id, addr_type, location, link_id, encap_type, direction, is_valid;
- char ip_addr[MGW_SYMBOL_MAX];
- char mrl_ip[MGW_SYMBOL_MAX];
- char outer_sport[MGW_SYMBOL_MAX];
- char outer_dport[MGW_SYMBOL_MAX];
- char outer_sip[MGW_SYMBOL_MAX];
- char outer_dip[MGW_SYMBOL_MAX];
- char outer_smac[MGW_SYMBOL_MAX];
- char outer_dmac[MGW_SYMBOL_MAX];
- char inner_smac[MGW_SYMBOL_MAX];
- char inner_dmac[MGW_SYMBOL_MAX];
+ char ip_addr[IP_MGR_IP_LEN];
+ char mrl_ip[IP_MGR_IP_LEN];
+ char outer_sport[IP_MGR_PORT_LEN];
+ char outer_dport[IP_MGR_PORT_LEN];
+ char outer_sip[IP_MGR_IP_LEN];
+ char outer_dip[IP_MGR_IP_LEN];
+ char outer_smac[IP_MGR_MAC_LEN];
+ char outer_dmac[IP_MGR_MAC_LEN];
+ char inner_smac[IP_MGR_MAC_LEN];
+ char inner_dmac[IP_MGR_MAC_LEN];
sscanf(table_line, "%d %d %d %s %d %s %d %d %d %s %s %s %s %s %s %s %s %d", &config_id, &group_id, &addr_type, ip_addr, &location, mrl_ip, &link_id, &encap_type, &direction, outer_sport, outer_dport,
outer_sip, outer_dip, outer_smac, outer_dmac, inner_smac, inner_dmac, &is_valid);
uint32_t ip = inet_addr(ip_addr);
@@ -278,9 +286,24 @@ static void Maat_cand_ip_update_cb(int table_id, const char* table_line, void* a
return;
}
}
- struct cand_ip *_cand_ip = ALLOC(struct cand_ip, 1);
+ struct ip_mgr_cand_ip *_cand_ip = ALLOC(struct ip_mgr_cand_ip, 1);
_cand_ip->ip = ip;
- _cand_ip->reference = 0;
+ _cand_ip->reference = 0;
+ struct ip_mgr_vxlan_info *vxlan_info = ALLOC(struct ip_mgr_vxlan_info, 1);
+
+ strncpy(vxlan_info->vxlan_outer_local_port, outer_sport, IP_MGR_PORT_LEN);
+ strncpy(vxlan_info->vxlan_outer_gdev_port, outer_dport, IP_MGR_PORT_LEN);
+ strncpy(vxlan_info->vxlan_outer_local_ip, outer_sip, IP_MGR_IP_LEN);
+ strncpy(vxlan_info->vxlan_outer_gdev_ip, outer_dip, IP_MGR_IP_LEN);
+ strncpy(vxlan_info->vxlan_outer_local_mac, outer_smac, IP_MGR_MAC_LEN);
+ strncpy(vxlan_info->vxlan_outer_gdev_mac, outer_dmac, IP_MGR_MAC_LEN);
+ strncpy(vxlan_info->vxlan_inner_smac, inner_smac, IP_MGR_MAC_LEN);
+ strncpy(vxlan_info->vxlan_inner_dmac, inner_dmac, IP_MGR_MAC_LEN);
+ vxlan_info->vxlan_link_id = link_id;
+ vxlan_info->vxlan_link_dir = direction;
+ vxlan_info->vxlan_encap_type = encap_type;
+
+ _cand_ip->vxlan_info = vxlan_info;
int num = ip_group->num;
ip_group->ip_group[num] = _cand_ip;
ip_group->num ++;
@@ -356,8 +379,16 @@ struct ip_mgr_handle *ip_mgr_init(void *logger, Maat_feather_t feather, struct h
handle->user_policy_htable = mgw_utils_create_htable("user_info_htable", opts, (void *)user_info_htable_data_free_cb, NULL);
handle->cand_ip_htable = mgw_utils_create_htable("cand_ip_htable", opts, (void *)cand_ip_htable_data_free_cb, NULL);
handle->ip_verify_htable = mgw_utils_create_htable("ip_verify_htable", opts, (void *)ip_verify_htable_data_free_cb, NULL);
- Maat_plugin_register(feather, "USER_POLICY_TABLE", Maat_user_policy_start_cb, Maat_user_policy_update_cb, Maat_user_policy_finish_cb, handle);
- Maat_plugin_register(feather, "CAND_IP_TABLE", Maat_cand_ip_start_cb, Maat_cand_ip_update_cb, Maat_cand_ip_finish_cb, handle);
+ int rtn = Maat_plugin_register(feather, "IR_POLICY", Maat_user_policy_start_cb, Maat_user_policy_update_cb, Maat_user_policy_finish_cb, handle);
+ if(rtn == -1)
+ {
+ return NULL;
+ }
+ rtn = Maat_plugin_register(feather, "IR_CANDIDATE_IP", Maat_cand_ip_start_cb, Maat_cand_ip_update_cb, Maat_cand_ip_finish_cb, handle);
+ if(rtn == -1)
+ {
+ return NULL;
+ }
return handle;
}
diff --git a/access/src/main.cpp b/access/src/main.cpp
index 7a92bc3..61b0b15 100644
--- a/access/src/main.cpp
+++ b/access/src/main.cpp
@@ -3,6 +3,16 @@
#include "mgw_tun.h"
#include "nat.h"
#include "ip_mgr.h"
+#include "mgw_socket.h"
+#include "udp_server.h"
+
+struct udp_client_handle
+{
+ int socket_fd;
+ char dip[MGW_SYMBOL_MAX]; //not used
+ uint16_t dport;
+ void *logger;
+};
struct mgw_handle
{
@@ -13,6 +23,7 @@ struct mgw_handle
struct mgw_tun_handle *mgw_tun_handle_s;
struct nat_handle *nat_handle_s;
struct ip_mgr_handle *ip_mgr_handle_s;
+ struct udp_client_handle *udp_client_handle_s;
};
static void wrapped_Maat_set_feather_opt(void *logger, Maat_feather_t feather, enum MAAT_INIT_OPT type, const void* value, int size)
@@ -74,7 +85,7 @@ static struct htable_opts * htable_opt_init(const char* profile, void *logger)
MESA_load_profile_int_def(profile, section, "mho_screen_print_ctrl", &(_htable_opts->mho_screen_print_ctrl), 0);
MESA_load_profile_int_def(profile, section, "mho_thread_safe", &(_htable_opts->mho_thread_safe), 1);
MESA_load_profile_int_def(profile, section, "mho_mutex_num", &(_htable_opts->mho_mutex_num), 16);
- MESA_load_profile_int_def(profile, section, "mho_hash_slot_size", &(_htable_opts->mho_hash_slot_size), 16);
+ MESA_load_profile_int_def(profile, section, "mho_hash_slot_size", &(_htable_opts->mho_hash_slot_size), 16000);
MESA_load_profile_int_def(profile, section, "mho_expire_time", &(_htable_opts->mho_expire_time), 0);
MGW_LOG_INFO(logger, "MESA_prof_load, [%s]:\n mho_screen_print_ctrl: %d\n mho_thread_safe: %d\n mho_mutex_num: %d\n mho_hash_slot_size: %d\n mho_expire_time: %d",
"htable_opt", _htable_opts->mho_screen_print_ctrl, _htable_opts->mho_thread_safe, _htable_opts->mho_mutex_num, _htable_opts->mho_hash_slot_size, _htable_opts->mho_expire_time);
@@ -86,10 +97,39 @@ void ip2user_htable_free_data_cb(void *data)
FREE(&data);
}
+struct udp_client_handle * udp_client_init(const char *profile, void *logger)
+{
+ const char *section = "mrl";
+ char ip[MGW_SYMBOL_MAX];
+ uint16_t port;
+ MESA_load_profile_string_def(profile, section, "ip", ip, MGW_SYMBOL_MAX, "192.168.10.242");
+ MESA_load_profile_int_def(profile, section, "port", (int *)&port, 23456);
+ MGW_LOG_INFO(logger, "MESA_prof_load, [%s]:\n ip: %s\n port: %d", "mrl", ip, port);
+ int socket_fd = mgw_socket_init();
+ if(socket_fd < 0)
+ {
+ MGW_LOG_ERROR(logger, "mgw_socket: Failed at create socket, errno is %d, %s", errno, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ struct udp_client_handle *handle = ALLOC(struct udp_client_handle, 1);
+ handle->logger = logger;
+ handle->dport = port;
+ handle->socket_fd = socket_fd;
+ strncpy(handle->dip, ip, MGW_SYMBOL_MAX);
+ return handle;
+}
+
static struct mgw_handle * mgw_init()
{
- const char *profile = "./conf/mgw.conf";
+ struct mgw_handle *_mgw_handle = ALLOC(struct mgw_handle, 1);
+ char *profile = ALLOC(char, MGW_SYMBOL_MAX);
+ strncpy(profile, "./conf/mgw.conf", MGW_SYMBOL_MAX);
const char *section = "global";
+
+ //init srand
+ srand((unsigned)time(NULL));
+
+ //init logger
char log_path[MGW_PATH_MAX];
MESA_load_profile_string_def(profile, section, "log_path", log_path, sizeof(log_path), "./log/mgw.log");
void *logger = MESA_create_runtime_log_handle(log_path, RLOG_LV_DEBUG);
@@ -99,13 +139,21 @@ static struct mgw_handle * mgw_init()
exit(EXIT_FAILURE);
}
MGW_LOG_INFO(logger, "MESA_prof_load, [%s]:\n log_path: %s", "global", log_path);
- struct mgw_handle *_mgw_handle = ALLOC(struct mgw_handle, 1);
_mgw_handle->logger = logger;
+ _mgw_handle->profile = (const char *)profile;
+
+ //init htable_opt
struct htable_opts * _htable_opts = htable_opt_init(profile, logger);
_mgw_handle->htable_opts_s = _htable_opts;
- Maat_feather_t Maat_feather = Maat_init(profile, logger);
+
+ //init Maat
+ Maat_feather_t Maat_feather = Maat_init((const char *)profile, logger);
_mgw_handle->Maat_feather = Maat_feather;
+
+ //init tun
_mgw_handle->mgw_tun_handle_s = mgw_tun_init("tun_mgw", logger);
+
+ //init nat
MESA_htable_handle ip2user_htable = mgw_utils_create_htable("ip2user_htable", _mgw_handle->htable_opts_s, (void *)ip2user_htable_free_data_cb, NULL);
if(ip2user_htable == NULL)
{
@@ -113,21 +161,45 @@ static struct mgw_handle * mgw_init()
exit(EXIT_FAILURE);
}
_mgw_handle->nat_handle_s = nat_init(ip2user_htable, logger, _htable_opts);
+
+ //init ip_mgr
+ struct ip_mgr_handle * _ip_mgr_handle = ip_mgr_init(logger, Maat_feather, _htable_opts);
+ if(unlikely(_ip_mgr_handle == NULL))
+ {
+ MGW_LOG_ERROR(logger, "Failed at init_ip_mgr");
+ exit(EXIT_FAILURE);
+ }
+ _mgw_handle->ip_mgr_handle_s = _ip_mgr_handle;
+
+ //create thread_vpn_monitor
pthread_t thread_id;
- struct vpn_monitor_args args = {.ip2user_htable = ip2user_htable, .logger = logger};
- int rtn = pthread_create(&thread_id, NULL, thread_vpn_monitor, (void *)&args);
+ struct vpn_monitor_args *args = ALLOC(struct vpn_monitor_args, 1);
+ args->ip2user_htable = ip2user_htable;
+ args->logger = logger;
+ args->profile = (const char *)profile;
+ int rtn = pthread_create(&thread_id, NULL, thread_vpn_monitor, (void *)args);
if(unlikely(rtn != 0))
{
MGW_LOG_ERROR(logger, "Failed at creating thread_vpn_monitor");
exit(EXIT_FAILURE);
}
- struct ip_mgr_handle * _ip_mgr_handle = ip_mgr_init(logger, Maat_feather, _htable_opts);
- if(unlikely(_ip_mgr_handle == NULL))
+
+ //create thread_udp_server
+ struct udp_server_args *_udp_server_args = ALLOC(struct udp_server_args, 1);
+ _udp_server_args->logger = logger;
+ _udp_server_args->profile = profile;
+ _udp_server_args->_nat_handle = _mgw_handle->nat_handle_s;
+ _udp_server_args->tun_handle = _mgw_handle->mgw_tun_handle_s;
+ rtn = pthread_create(&thread_id, NULL, thread_udp_server, (void *)_udp_server_args);
+ if(unlikely(rtn != 0))
{
- MGW_LOG_ERROR(logger, "Failed at init_ip_mgr");
+ MGW_LOG_ERROR(logger, "Failed at creating thread_udp_server");
exit(EXIT_FAILURE);
}
- _mgw_handle->ip_mgr_handle_s = _ip_mgr_handle;
+
+ //init udp client socket
+ struct udp_client_handle *_udp_client_handle = udp_client_init(profile, logger);
+ _mgw_handle->udp_client_handle_s = _udp_client_handle;
return _mgw_handle;
}
@@ -137,13 +209,39 @@ static void mgw_destroy(struct mgw_handle *_mgw_handle)
Maat_burn_feather(_mgw_handle->Maat_feather);
}
+static void send_data_to_mrl(struct udp_client_handle *handle, char *buff, int len, struct ip_mgr_vxlan_info *vxlan_info)
+{
+ void *logger = handle->logger;
+ int socket_fd = handle->socket_fd;
+ memcpy(buff + len, vxlan_info, sizeof(struct ip_mgr_vxlan_info));
+ len += sizeof(struct ip_mgr_vxlan_info);
+ uint32_t dip = inet_addr(vxlan_info->vxlan_outer_local_ip);
+ uint16_t dport = htons(handle->dport);
+ int rtn = mgw_socket_udp_send(socket_fd, buff, len, dip, dport);
+ if (rtn < 0)
+ {
+ MGW_LOG_ERROR(logger, "mgw_socket: Failed at send udp data, errno is %d, %s", errno, strerror(errno));
+ return;
+ }
+ MGW_LOG_INFO(logger, "mgw_socket: Succeed to send udp data, len is %d", len);
+}
+
static void mgw_run(struct mgw_handle *handle)
{
+ void *logger = handle->logger;
while(1)
{
char buff[MGW_PACKET_MAX];
int len = mgw_tun_read(handle->mgw_tun_handle_s, buff, MGW_PACKET_MAX);
- nat_src_convert(handle->nat_handle_s, handle->ip_mgr_handle_s, buff, len);
+ printf("tun: len is %d\n", len);
+ struct ip_mgr_vxlan_info *vxlan_info = NULL;
+ int rtn = nat_src_convert(handle->nat_handle_s, handle->ip_mgr_handle_s, buff, len, &vxlan_info);
+ if(rtn == NAT_COVERT_FAILURE)
+ {
+ MGW_LOG_ERROR(logger, "Failed at nat_src_convert");
+ continue;
+ }
+ send_data_to_mrl(handle->udp_client_handle_s, buff, len, vxlan_info);
}
}
diff --git a/access/src/nat.cpp b/access/src/nat.cpp
index a9a9543..8bfec61 100644
--- a/access/src/nat.cpp
+++ b/access/src/nat.cpp
@@ -29,13 +29,6 @@ struct ip_port_pair
uint16_t port;
};
-struct candidate_port
-{
- unsigned int access_id : 2;
- unsigned int random : 6;
- unsigned int hash : 8;
-};
-
struct nat_ctx
{
void *logger;
@@ -54,8 +47,8 @@ static int sess_get_from_packet(const char *buff, int len, struct session *sess)
}
struct iphdr *_iphdr = (struct iphdr *)buff;
int iphdr_len = _iphdr->ihl * 4;
- sess->sip = ntohl(_iphdr->saddr);
- sess->dip = ntohl(_iphdr->daddr);
+ sess->sip = _iphdr->saddr;
+ sess->dip = _iphdr->daddr;
sess->proto = _iphdr->protocol;
switch(sess->proto)
{
@@ -66,8 +59,8 @@ static int sess_get_from_packet(const char *buff, int len, struct session *sess)
return -1;
}
struct tcphdr *_tcphdr = (struct tcphdr *)(buff + iphdr_len);
- sess->sport = ntohs(_tcphdr->source);
- sess->dport = ntohs(_tcphdr->dest);
+ sess->sport = _tcphdr->source;
+ sess->dport = _tcphdr->dest;
break;
}
case PROTO_UDP:
@@ -77,8 +70,8 @@ static int sess_get_from_packet(const char *buff, int len, struct session *sess)
return -1;
}
struct udphdr *_udphdr = (struct udphdr *)(buff + iphdr_len);
- sess->sport = ntohs(_udphdr->source);
- sess->dport = ntohs(_udphdr->dest);
+ sess->sport = _udphdr->source;
+ sess->dport = _udphdr->dest;
break;
}
case PROTO_ICMP:
@@ -98,6 +91,7 @@ static int sess_get_from_packet(const char *buff, int len, struct session *sess)
static int packet_src_replace(const char *buff, int len, struct ip_port_pair *pair)
{
+
struct iphdr *_iphdr = (struct iphdr *)buff;
int iphdr_len = _iphdr->ihl * 4;
_iphdr->saddr = pair->ip;
@@ -212,27 +206,28 @@ static long dnat_htable_query_cb(void *data, const uchar *key, uint size, void *
}
}
-static uint16_t get_candidate_port(int access_id, struct session *sess)
+static uint16_t get_candidate_port(int access_id, struct session *sess, uint32_t cand_ip)
{
- struct candidate_port *port = ALLOC(struct candidate_port, 1);
- port->access_id = 0;
- unsigned int random = mgw_utils_get_random(64);
- port->random = random;
- unsigned int hash = (sess->sip)^(sess->dip)^(sess->sport)^(sess->dport)^(sess->proto);
- hash &= 0xff;
- port->hash = hash;
- u_int16_t _port = *((u_int16_t *)port);
- return _port;
+ u_int16_t random = mgw_utils_get_random(64);
+ uint16_t hash = ((sess->dip)^(htons(sess->dport))^(sess->proto)^cand_ip)&0xff;
+ uint16_t port = (access_id << 14) + (random << 8) + hash;
+ return htons(port);
}
-static struct ip_port_pair * snat_pair_get(struct nat_handle *handle, struct ip_mgr_handle *_ip_mgr_handle, const char *user_name, struct session *snat_key)
+static struct ip_port_pair * snat_pair_get(struct nat_handle *handle, struct ip_mgr_handle *_ip_mgr_handle,
+ const char *user_name, struct session *snat_key, struct ip_mgr_vxlan_info **vxlan_info)
{
int retry_times = 10;
void *logger = handle->logger;
for(int i = 0; i < retry_times; i++)
{
- struct cand_ip* _cand_ip = ip_mgr_candidata_ip_get(_ip_mgr_handle, user_name);
- u_int16_t port = get_candidate_port(handle->access_id, snat_key);
+ struct ip_mgr_cand_ip* _cand_ip = ip_mgr_candidata_ip_get(_ip_mgr_handle, user_name);
+ if(_cand_ip == NULL)
+ {
+ MGW_LOG_INFO(logger, "Failed to find ip and port, user_name is %s", user_name);
+ return NULL;
+ }
+ u_int16_t port = get_candidate_port(handle->access_id, snat_key, _cand_ip->ip);
struct session dnat_key;
dnat_key.sip = snat_key->dip;
dnat_key.sport = snat_key->dport;
@@ -253,6 +248,7 @@ static struct ip_port_pair * snat_pair_get(struct nat_handle *handle, struct ip_
selected_pair->ip = _cand_ip->ip;
_cand_ip->reference ++;
selected_pair->port = port;
+ *vxlan_info = _cand_ip->vxlan_info;
return selected_pair;
}
}
@@ -260,7 +256,7 @@ static struct ip_port_pair * snat_pair_get(struct nat_handle *handle, struct ip_
return NULL;
}
-int nat_src_convert(struct nat_handle *handle, struct ip_mgr_handle *_ip_mgr_handle, char *buff, int len)
+int nat_src_convert(struct nat_handle *handle, struct ip_mgr_handle *_ip_mgr_handle, char *buff, int len, struct ip_mgr_vxlan_info **vxlan_info)
{
void *logger = handle->logger;
//get session
@@ -271,6 +267,16 @@ int nat_src_convert(struct nat_handle *handle, struct ip_mgr_handle *_ip_mgr_han
MGW_LOG_ERROR(logger, "Failed at parse packet, len is %d", len);
return NAT_COVERT_FAILURE;
}
+ char snat_key_sip[MGW_SYMBOL_MAX];
+ char snat_key_dip[MGW_SYMBOL_MAX];
+ char snat_value_ip[MGW_SYMBOL_MAX];
+ char dnat_key_sip[MGW_SYMBOL_MAX];
+ char dnat_key_dip[MGW_SYMBOL_MAX];
+ char dnat_value_ip[MGW_SYMBOL_MAX];
+ mgw_utils_inet_ntoa(snat_key.sip, snat_key_sip);
+ mgw_utils_inet_ntoa(snat_key.dip, snat_key_dip);
+ MGW_LOG_INFO(logger, "Before snat: session is <%s:%d %s:%d %d>",
+ snat_key_sip, ntohs(snat_key.sport), snat_key_dip, ntohs(snat_key.dport), snat_key.proto);
//query snat_htable,get ip_port pair
long snat_cb_rtn = -1;
struct ip_port_pair snat_value;
@@ -278,8 +284,9 @@ int nat_src_convert(struct nat_handle *handle, struct ip_mgr_handle *_ip_mgr_han
bool ip_is_valid = ip_mgr_candidate_ip_verify(_ip_mgr_handle, snat_value.ip);
if(snat_cb_rtn == HTABLE_KEY_EXISTED && ip_is_valid == true)
{
- MGW_LOG_INFO(logger, "MESA_htable: key existed, table is %s, key is <%s:%d %s,%d, %d>, value is <%s, %d>", "ip2user_htable",
- mgw_utils_inet_ntoa(snat_key.sip), snat_key.sport, mgw_utils_inet_ntoa(snat_key.dip), snat_key.dport, snat_key.proto, mgw_utils_inet_ntoa(snat_value.ip), snat_value.port);
+ mgw_utils_inet_ntoa(snat_value.ip, snat_value_ip);
+ MGW_LOG_INFO(logger, "MESA_htable: key existed, table is %s, key is <%s:%d %s:%d %d>, value is <%s, %d>", "ip2user_htable",
+ snat_key_sip, ntohs(snat_key.sport), snat_key_dip, ntohs(snat_key.dport), snat_key.proto, snat_value_ip, snat_value.port);
packet_src_replace(buff, len, &snat_value);
}
else
@@ -287,12 +294,12 @@ int nat_src_convert(struct nat_handle *handle, struct ip_mgr_handle *_ip_mgr_han
//if session exists in sant but ip is valid. del session from snat and dnat
if(snat_cb_rtn == HTABLE_KEY_EXISTED)
{
- int rtn = MESA_htable_del(handle->snat_htable, (const unsigned char *)(&snat_key), sizeof(snat_key), NULL);
+ rtn = MESA_htable_del(handle->snat_htable, (const unsigned char *)(&snat_key), sizeof(snat_key), NULL);
// data may be freed because of expire
if(rtn < 0 && rtn != MESA_HTABLE_RET_NOT_FOUND)
{
MGW_LOG_ERROR(handle->logger, "MESA_htable: failed at del, errno is %d, table is %s, key is <%s:%d %s,%d, %d>", rtn, "snat_htable",
- mgw_utils_inet_ntoa(snat_key.sip), snat_key.sport, mgw_utils_inet_ntoa(snat_key.dip), snat_key.dport, snat_key.proto);
+ snat_key_sip, ntohs(snat_key.sport), snat_key_dip, ntohs(snat_key.dport), snat_key.proto);
return NAT_COVERT_FAILURE;;
}
struct session dnat_key;
@@ -301,11 +308,13 @@ int nat_src_convert(struct nat_handle *handle, struct ip_mgr_handle *_ip_mgr_han
dnat_key.dip = snat_value.ip;
dnat_key.dport = snat_value.port;
dnat_key.proto = snat_key.proto;
+ mgw_utils_inet_ntoa(dnat_key.sip, dnat_key_sip);
+ mgw_utils_inet_ntoa(dnat_key.dip, dnat_key_dip);
rtn = MESA_htable_del(handle->dnat_htable, (const unsigned char *)(&dnat_key), sizeof(dnat_key), NULL);
if(rtn < 0 && rtn != MESA_HTABLE_RET_NOT_FOUND)
{
MGW_LOG_ERROR(handle->logger, "MESA_htable: failed at del, errno is %d, table is %s, key is <%s:%d %s,%d, %d>", rtn, "dnat_htable",
- mgw_utils_inet_ntoa(dnat_key.sip), dnat_key.sport, mgw_utils_inet_ntoa(snat_key.dip), dnat_key.dport, dnat_key.proto);
+ snat_key_sip, ntohs(dnat_key.sport), snat_key_dip, ntohs(snat_key.dport), snat_key.proto);
return NAT_COVERT_FAILURE;;
}
}
@@ -314,19 +323,22 @@ int nat_src_convert(struct nat_handle *handle, struct ip_mgr_handle *_ip_mgr_han
user_name = (char *)MESA_htable_search(handle->ip2user_htable, (const unsigned char *)(&sip), sizeof(sip));
if(user_name != NULL)
{
- struct ip_port_pair *snat_value = snat_pair_get(handle, _ip_mgr_handle, user_name, &snat_key);
+ struct ip_port_pair *snat_value = snat_pair_get(handle, _ip_mgr_handle, user_name, &snat_key, vxlan_info);
if(snat_value == NULL)
{
return NAT_COVERT_FAILURE;
}
//add sess to snat, dnat
- int rtn = MESA_htable_add(handle->snat_htable, (const unsigned char *)(&snat_key), sizeof(snat_key), (const void*)snat_value);
+ rtn = MESA_htable_add(handle->snat_htable, (const unsigned char *)(&snat_key), sizeof(snat_key), (const void*)snat_value);
+ mgw_utils_inet_ntoa(snat_value->ip, snat_value_ip);
if(rtn < 0)
{
- MGW_LOG_INFO(logger, "MESA_htable: add to %s, key is <%s:%d %s,%d, %d>, value is <%s, %d>", "snat_htable",
- mgw_utils_inet_ntoa(snat_key.sip), snat_key.sport, mgw_utils_inet_ntoa(snat_key.dip), snat_key.dport, snat_key.proto, mgw_utils_inet_ntoa(snat_value->ip), snat_value->port);
+ MGW_LOG_ERROR(logger, "MESA_htable: Failed at add to %s, key is <%s:%d %s,%d, %d>, value is <%s, %d>", "snat_htable",
+ snat_key_sip, ntohs(snat_key.sport), snat_key_dip, ntohs(snat_key.dport), snat_key.proto, snat_value_ip, ntohs(snat_value->port));
return NAT_COVERT_FAILURE;
}
+ MGW_LOG_INFO(logger, "MESA_htable: Succeed at add to %s, key is <%s:%d %s,%d, %d>, value is <%s, %d>", "snat_htable",
+ snat_key_sip, ntohs(snat_key.sport), snat_key_dip, ntohs(snat_key.dport), snat_key.proto, snat_value_ip, ntohs(snat_value->port));
struct session dnat_key;
dnat_key.sip = snat_key.dip;
dnat_key.sport = snat_key.dport;
@@ -336,49 +348,85 @@ int nat_src_convert(struct nat_handle *handle, struct ip_mgr_handle *_ip_mgr_han
struct ip_port_pair *dnat_value = ALLOC(struct ip_port_pair, 1);
dnat_value->ip = snat_key.sip;
dnat_value->port = snat_key.sport;
+ mgw_utils_inet_ntoa(dnat_key.sip, dnat_key_sip);
+ mgw_utils_inet_ntoa(dnat_key.dip, dnat_key_dip);
+ mgw_utils_inet_ntoa(dnat_value->ip, dnat_value_ip);
rtn = MESA_htable_add(handle->dnat_htable, (const unsigned char *)(&dnat_key), sizeof(dnat_key), (const void*)dnat_value);
if(rtn < 0)
{
- MGW_LOG_INFO(logger, "MESA_htable: add to %s, key is <%s:%d %s,%d, %d>, value is <%s, %d>", "dnat_htable",
- mgw_utils_inet_ntoa(dnat_key.sip), dnat_key.sport, mgw_utils_inet_ntoa(dnat_key.dip), dnat_key.dport, dnat_key.proto, mgw_utils_inet_ntoa(dnat_value->ip), dnat_value->port);
+ MGW_LOG_ERROR(logger, "MESA_htable: Failed at add to %s, key is <%s:%d %s,%d, %d>, value is <%s, %d>", "dnat_htable",
+ dnat_key_sip, ntohs(dnat_key.sport), dnat_key_dip, ntohs(dnat_key.dport), dnat_key.proto, dnat_value_ip, ntohs(dnat_value->port));
return NAT_COVERT_FAILURE;
}
- packet_src_replace(buff, len, dnat_value);
+ MGW_LOG_INFO(logger, "MESA_htable: Succeed at add to %s, key is <%s:%d %s,%d, %d>, value is <%s, %d>", "dnat_htable",
+ dnat_key_sip, ntohs(dnat_key.sport), dnat_key_dip, ntohs(dnat_key.dport), dnat_key.proto, dnat_value_ip, ntohs(dnat_value->port));
+ packet_src_replace(buff, len, snat_value);
+ rtn = sess_get_from_packet(buff, len, &snat_key);
+ if(rtn == -1)
+ {
+ MGW_LOG_ERROR(logger, "Failed at parse packet, len is %d", len);
+ return NAT_COVERT_FAILURE;
+ }
+ mgw_utils_inet_ntoa(snat_key.sip, snat_key_sip);
+ mgw_utils_inet_ntoa(snat_key.dip, snat_key_dip);
+ MGW_LOG_INFO(logger, "After snat: session is <%s:%d %s:%d %d>",
+ snat_key_sip, ntohs(snat_key.sport), snat_key_dip, ntohs(snat_key.dport), snat_key.proto);
return NAT_COVERT_SUCCESS;
}
else
{
- MGW_LOG_ERROR(logger, "Failed at find user_name, ip %s", mgw_utils_inet_ntoa(sip));
+ MGW_LOG_ERROR(logger, "Failed at find user_name, ip %s", snat_key_sip);
return NAT_COVERT_FAILURE;
}
}
}
+
int nat_dest_convert(struct nat_handle *handle, char *buff, int len)
{
void *logger = handle->logger;
//get session
- struct session *sess = ALLOC(struct session, 1);
- int rtn = sess_get_from_packet(buff, len, sess);
+ struct session dnat_key;
+ int rtn = sess_get_from_packet(buff, len, &dnat_key);
if(rtn == -1)
{
- MGW_LOG_ERROR(logger, "Failed at sess_get_from_packet, packet len is %d", len);
+ MGW_LOG_ERROR(logger, "Failed at parse packet, packet len is %d", len);
return NAT_COVERT_FAILURE;
}
+ char dnat_key_sip[MGW_SYMBOL_MAX];
+ char dnat_key_dip[MGW_SYMBOL_MAX];
+ char dnat_value_ip[MGW_SYMBOL_MAX];
+ mgw_utils_inet_ntoa(dnat_key.sip, dnat_key_sip);
+ mgw_utils_inet_ntoa(dnat_key.dip, dnat_key_dip);
+ MGW_LOG_INFO(logger, "Before dnat: session is <%s:%d %s:%d %d>",
+ dnat_key_sip, ntohs(dnat_key.sport), dnat_key_dip, ntohs(dnat_key.dport), dnat_key.proto);
+
//query dnat,get ip and port
long dnat_cb_rtn = -1;
- struct ip_port_pair pair;
- MESA_htable_search_cb(handle->dnat_htable, (const unsigned char *)(&sess), sizeof(sess), dnat_htable_query_cb, (void *)(&pair), &dnat_cb_rtn);
+ struct ip_port_pair dnat_value;
+ MESA_htable_search_cb(handle->dnat_htable, (const unsigned char *)(&dnat_key), sizeof(dnat_key), dnat_htable_query_cb, (void *)(&dnat_value), &dnat_cb_rtn);
if(dnat_cb_rtn == HTABLE_KEY_EXISTED)
{
+ mgw_utils_inet_ntoa(dnat_value.ip, dnat_value_ip);
MGW_LOG_INFO(logger, "MESA_htable: key existed, table is %s, key is <%s:%d %s,%d, %d>, value is <%s, %d>", "dnat_htable",
- mgw_utils_inet_ntoa(sess->sip), sess->sport, mgw_utils_inet_ntoa(sess->dip), sess->dport, sess->proto, mgw_utils_inet_ntoa(pair.ip), pair.port);
- packet_dest_replace(buff, len, &pair);
+ dnat_key_sip, ntohs(dnat_key.sport), dnat_key_dip, ntohs(dnat_key.dport), dnat_key.proto, dnat_value_ip, ntohs(dnat_value.port));
+ packet_dest_replace(buff, len, &dnat_value);
+ rtn = sess_get_from_packet(buff, len, &dnat_key);
+ if(rtn == -1)
+ {
+ MGW_LOG_ERROR(logger, "Failed at parse packet, len is %d", len);
+ return NAT_COVERT_FAILURE;
+ }
+ mgw_utils_inet_ntoa(dnat_key.sip, dnat_key_sip);
+ mgw_utils_inet_ntoa(dnat_key.dip, dnat_key_dip);
+ MGW_LOG_INFO(logger, "After snat: session is <%s:%d %s:%d %d>",
+ dnat_key_sip, ntohs(dnat_key.sport), dnat_key_dip, ntohs(dnat_key.dport), dnat_key.proto);
+ return NAT_COVERT_SUCCESS;
}
else
{
- MGW_LOG_ERROR(logger, "MESA_htable: key not existed, table is %s, key is <%s:%d %s,%d, %d>", "dnat_htable",
- mgw_utils_inet_ntoa(sess->sip), sess->sport, mgw_utils_inet_ntoa(sess->dip), sess->dport, sess->proto);
+ MGW_LOG_INFO(logger, "MESA_htable: key not existed, table is %s, key is <%s:%d %s,%d, %d>", "dnat_htable",
+ dnat_key_sip, ntohs(dnat_key.sport), dnat_key_dip, ntohs(dnat_key.dport), dnat_key.proto);
return NAT_COVERT_FAILURE;
}
}
diff --git a/access/src/udp_server.cpp b/access/src/udp_server.cpp
new file mode 100644
index 0000000..f8a0d76
--- /dev/null
+++ b/access/src/udp_server.cpp
@@ -0,0 +1,52 @@
+#include "mgw_utils.h"
+#include "mgw_socket.h"
+#include "udp_server.h"
+#include "nat.h"
+#include "mgw_tun.h"
+
+void * thread_udp_server(void *args)
+{
+ struct udp_server_args *_args = (struct udp_server_args *)args;
+ void *logger = _args->logger;
+ const char *profile = _args->profile;
+ struct nat_handle *_nat_handle = _args->_nat_handle;
+ struct mgw_tun_handle *tun_handle = _args->tun_handle;
+ const char *section = "udp_server";
+ char ip[MGW_SYMBOL_MAX];
+ u_int16_t port;
+ MESA_load_profile_string_def(profile, section, "ip", ip, MGW_SYMBOL_MAX, "192.168.11.137");
+ MESA_load_profile_int_def(profile, section, "port", (int *)&port, 33456);
+ MGW_LOG_INFO(logger, "MESA_prof_load, [%s]:\n ip: %s\n port: %d", "udp_server", ip, port);
+ int socket_fd = mgw_socket_init();
+ if(socket_fd < 0)
+ {
+ MGW_LOG_ERROR(logger, "mgw_socket: Failed at create socket, errno is %d, %s", errno, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ int rtn = mgw_socket_bind(socket_fd, ip, port);
+ if(rtn < 0)
+ {
+ MGW_LOG_ERROR(logger, "mgw_socket: Failed at bind, ip is %s, port is %d, errno is %d, %s", ip, port, errno, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ char buff[MGW_PACKET_MAX];
+ while(1)
+ {
+ //EAGAIN if socket is no-blocking, eg. fcntl()
+ int len = mgw_socket_udp_recv(socket_fd, buff);
+ if (len < 0)
+ {
+ MGW_LOG_ERROR(logger, "mgw_socket: Failed at recv udp data, errno is %d, %s", errno, strerror(errno));
+ continue;
+ }
+ MGW_LOG_INFO(logger, "mgw_socket: Succeed to recv udp data, len is %d", len);
+ int rtn = nat_dest_convert(_nat_handle, buff, len);
+ if(rtn == NAT_COVERT_FAILURE)
+ {
+ MGW_LOG_ERROR(logger, "Failed to nat_dest_convert");
+ continue;
+ }
+ //write to tun
+ mgw_tun_write(tun_handle, buff, len);
+ }
+} \ No newline at end of file
diff --git a/access/src/vpn_monitor.cpp b/access/src/vpn_monitor.cpp
index c0b94c5..9f3e249 100644
--- a/access/src/vpn_monitor.cpp
+++ b/access/src/vpn_monitor.cpp
@@ -27,13 +27,33 @@ void ip2user_htable_data_free_cb(void *data)
FREE(&data);
}
+static long ip2user_cb_to_del_key(void *data, const uchar *key, uint size, void *user_arg)
+{
+ unsigned char _key[MGW_SYMBOL_MAX];
+ memcpy((char *)_key, key, size);
+ _key[size] = '\0';
+ struct vpn_monitor_handle *handle = (struct vpn_monitor_handle *)user_arg;
+ if(data != NULL)
+ {
+ MGW_LOG_INFO(handle->logger, "MESA_htable: key existed, table is %s, key is %s", "ip2user_htable", _key);
+ int rtn = MESA_htable_del(handle->ip2user_htable, key, size, ip2user_htable_data_free_cb);
+ if(rtn != 0)
+ {
+ MGW_LOG_ERROR(handle->logger, "MESA_htable: failed at del, errno is %d, table is %s, key is %s", rtn, "ip2user_htable", _key);
+ return HTABLE_QUERY_CB_FAILURE;
+ }
+ MGW_LOG_INFO(handle->logger, "MESA_htable: succeed at del, table is %s, key is %s", "ip2user_htable", _key);
+ }
+ return HTABLE_QUERY_CB_SUCCESS;
+}
+
struct vpn_monitor_handle * vpn_monitor_init(void *args)
{
struct vpn_monitor_args *_args = (struct vpn_monitor_args *)args;
struct vpn_monitor_handle *handle = ALLOC(struct vpn_monitor_handle, 1);
handle->logger = _args->logger;
- handle->ip2user_htable = _args->logger;
+ handle->ip2user_htable = _args->ip2user_htable;
const char *profile = _args->profile;
const char *section = "vpn_server";
char hub_list_str[MGW_STRING_MAX];
@@ -73,6 +93,7 @@ struct vpn_monitor_handle * vpn_monitor_init(void *args)
handle->hubs[handle->hub_num].dhcp_end_ip = _dhcp_end_ip;
handle->hub_num ++;
}
+ FREE(&args);
return handle;
}
@@ -81,26 +102,6 @@ void * vpn_monitor_destroy(struct vpn_monitor_handle *handle)
FREE(&handle);
}
-static long ip2user_cb_to_del_key(void *data, const uchar *key, uint size, void *user_arg)
-{
- unsigned char _key[MGW_SYMBOL_MAX];
- strncpy((char *)_key, (char *)key, size);
- _key[size] = '\0';
- struct vpn_monitor_handle *handle = (struct vpn_monitor_handle *)user_arg;
- if(data != NULL)
- {
- MGW_LOG_INFO(handle->logger, "MESA_htable: key existed, table is %s, key is %s", "ip2user_htable", _key);
- int rtn = MESA_htable_del(handle->ip2user_htable, key, size, ip2user_htable_data_free_cb);
- if(rtn != 0)
- {
- MGW_LOG_ERROR(handle->logger, "MESA_htable: failed at del, errno is %d, table is %s, key is %s", rtn, "ip2user_htable", _key);
- return HTABLE_QUERY_CB_FAILURE;
- }
- MGW_LOG_INFO(handle->logger, "MESA_htable: succeed at del, table is %s, key is %s", "ip2user_htable", _key);
- }
- return HTABLE_QUERY_CB_SUCCESS;
-}
-
static void get_ip2user_by_vpncmd(struct vpn_monitor_handle *handle)
{
@@ -169,7 +170,7 @@ static void get_ip2user_by_vpncmd(struct vpn_monitor_handle *handle)
sscanf(dhcp_ip, "%s", ip);
//find ip in dhcp range
- unsigned int _ip = inet_network(ip);
+ uint32_t _ip = inet_network(ip);
if(_ip == INADDR_NONE)
{
MGW_LOG_ERROR(handle->logger, "Invalid dhcp_ip: %s", ip);
@@ -182,10 +183,9 @@ static void get_ip2user_by_vpncmd(struct vpn_monitor_handle *handle)
}
//set ip2user
long cb_rtn = -1;
- size_t key_size = sizeof(_ip);
- //printf("serach: key is %s\n", ip);
- MESA_htable_search_cb(handle->ip2user_htable, (const unsigned char *)(&_ip), key_size, ip2user_cb_to_del_key, (void *)handle, &cb_rtn);
- int rtn = MESA_htable_add(handle->ip2user_htable, (const unsigned char *)(&_ip), key_size, user_name);
+ uint32_t key = inet_addr(ip);
+ MESA_htable_search_cb(handle->ip2user_htable, (const unsigned char *)(&key), sizeof(key), ip2user_cb_to_del_key, (void *)handle, &cb_rtn);
+ int rtn = MESA_htable_add(handle->ip2user_htable, (const unsigned char *)(&key), sizeof(key), user_name);
if(rtn < 0)
{
MGW_LOG_ERROR(handle->logger, "MESA_htable: failed at add, table is %s, key is %s, value is %s", "ip2user_htable", ip, user_name);
@@ -202,8 +202,8 @@ void * thread_vpn_monitor(void* args)
struct vpn_monitor_handle *handle = vpn_monitor_init(args);
while(1)
{
- sleep(3);
printf("vpn monitor--------------\n");
get_ip2user_by_vpncmd(handle);
+ sleep(3600);
}
} \ No newline at end of file
diff --git a/access/test/test_MESA_htable.cpp b/access/test/test_MESA_htable.cpp
index 3355e1a..c6cd416 100644
--- a/access/test/test_MESA_htable.cpp
+++ b/access/test/test_MESA_htable.cpp
@@ -21,8 +21,22 @@ void test_htable_data_free_cb(void *data)
//FREE(&data);
}
+
+struct candidate_port
+{
+ unsigned char access_id : 2;
+ unsigned char random : 6;
+ unsigned char hash : 8;
+};
+
int main()
{
+ struct candidate_port port;
+ printf("size of port is %d\n", sizeof(candidate_port));
+ port.access_id = 0x3;
+ port.random = 0x39;
+ port.hash = 0xff;
+ printf("port is %0x\n", port);
/*
const char *profile = "./conf/mgw.conf";
void *logger = MESA_create_runtime_log_handle("./log/mgw.log", RLOG_LV_DEBUG);
@@ -39,9 +53,5 @@ int main()
*/
//char ip[MGW_SYMBOL_MAX] = "1.0.0.0";
//uint32_t _ip = inet_addr(ip);
- char *ip;
- uint32_t _ip = 1;
- ip = inet_ntoa(*(struct in_addr *)&_ip);
- printf("_ip is %s\n", ip);
}
diff --git a/access/test/test_maat_redis.cpp b/access/test/test_maat_redis.cpp
index 70d056c..4d80092 100644
--- a/access/test/test_maat_redis.cpp
+++ b/access/test/test_maat_redis.cpp
@@ -5,42 +5,35 @@
#include <unistd.h>
#include "mgw_utils.h"
-
-#ifdef __cplusplus
-extern "C"
-{
-#endif
-
-struct mgw_ctx g_mgw_ctx;
-
void wrapped_Maat_set_feather_opt(Maat_feather_t feather, enum MAAT_INIT_OPT type, const void* value, int size)
{
int rtn = Maat_set_feather_opt(feather, type, value, size);
if(unlikely(rtn < 0))
{
- MGW_LOG_ERROR(g_mgw_ctx.logger, "Failed at Maat_set_feather_opt, type is %d", type);
exit(EXIT_FAILURE);
}
}
-void Maat_init()
+Maat_feather_t Maat_init()
{
// load conf
+ const char *profile = "./conf/mgw.conf";
const char *section = "Maat";
char table_info_path[MGW_PATH_MAX];
int max_thread_num;
char Maat_redis_ip[MGW_STRING_MAX];
int Maat_redis_port;
char stat_file_path[MGW_PATH_MAX];
- MESA_load_profile_string_def(g_mgw_ctx.profile, section, "table_info_path", table_info_path, sizeof(table_info_path), "./conf/table_info.conf");
- MESA_load_profile_int_def(g_mgw_ctx.profile, section, "max_thread_num", &max_thread_num, 1);
- MESA_load_profile_string_def(g_mgw_ctx.profile, section, "Maat_redis_ip", Maat_redis_ip, sizeof(Maat_redis_ip), "127.0.0.1");
- MESA_load_profile_int_def(g_mgw_ctx.profile, section, "Maat_redis_port", &Maat_redis_port, 6379);
- MESA_load_profile_string_def(g_mgw_ctx.profile, section, "stat_file_path", stat_file_path, sizeof(stat_file_path), "./log/Maat_stat.log");
+ MESA_load_profile_string_def(profile, section, "table_info_path", table_info_path, sizeof(table_info_path), "./conf/table_info.conf");
+ MESA_load_profile_int_def(profile, section, "max_thread_num", &max_thread_num, 1);
+ MESA_load_profile_string_def(profile, section, "Maat_redis_ip", Maat_redis_ip, sizeof(Maat_redis_ip), "127.0.0.1");
+ MESA_load_profile_int_def(profile, section, "Maat_redis_port", &Maat_redis_port, 6379);
+ MESA_load_profile_string_def(profile, section, "stat_file_path", stat_file_path, sizeof(stat_file_path), "./log/Maat_stat.log");
// init Maat
Maat_feather_t feather = NULL;
- feather = Maat_feather(max_thread_num, table_info_path, g_mgw_ctx.logger);
+ void *logger = MESA_create_runtime_log_handle("./log/test_maat_redis.log", RLOG_LV_DEBUG);
+ feather = Maat_feather(max_thread_num, table_info_path, logger);
if(feather == NULL)
{
exit(EXIT_FAILURE);
@@ -58,34 +51,13 @@ void Maat_init()
int rtn = Maat_initiate_feather(feather);
if(unlikely(rtn < 0))
{
- MGW_LOG_ERROR(g_mgw_ctx.logger, "Failed at Maat_initiate_feather");
+ MGW_LOG_ERROR(logger, "Failed at Maat_initiate_feather");
exit(EXIT_FAILURE);
}
- g_mgw_ctx.Maat_feather = feather;
-}
-void mgw_init()
-{
- g_mgw_ctx.profile = "./conf/mgw.conf";
- const char *section = "global";
- char log_path[MGW_PATH_MAX];
- MESA_load_profile_string_def(g_mgw_ctx.profile, section, "log_path", log_path, sizeof(log_path), "./log/mgw.log");
- void *logger = MESA_create_runtime_log_handle(log_path, RLOG_LV_DEBUG);
- if (unlikely(logger == NULL))
- {
- MGW_LOG_ERROR(g_mgw_ctx.logger, "Failed at creating logger: %s", log_path);
- exit(EXIT_FAILURE);
- }
- g_mgw_ctx.logger = logger;
- Maat_init();
-}
-
-void mgw_destroy()
-{
- MESA_destroy_runtime_log_handle(g_mgw_ctx.logger);
- Maat_burn_feather(g_mgw_ctx.Maat_feather);
+ return feather;
}
-#define TEST_CMD_LINE_NUM 4
+#define TEST_CMD_LINE_NUM 1
void Maat_test_set_cmd_line(Maat_feather_t feather)
{
const struct Maat_line_t *p_line[TEST_CMD_LINE_NUM];
@@ -97,26 +69,30 @@ void Maat_test_set_cmd_line(Maat_feather_t feather)
{
line_rule[i].label_id=0;
//line_rule[i].rule_id=(int)Maat_cmd_incrby(feather,"TEST_PLUG_SEQ", 1);
- line_rule[i].rule_id = i + 1;
- line_rule[i].table_name="QD_ENTRY_INFO";
- snprintf(table_line[i],sizeof(table_line[i]),"1\t192.168.0.1\t%d\t1",100+i);
+ line_rule[i].rule_id = 1;
+ line_rule[i].table_name="IR_POLICY";
+ //1\t101\t0\tuser\t1
+ snprintf(table_line[i],sizeof(table_line[i]),"1\t1\t0\ttesting1\t1");
line_rule[i].table_line=table_line[i];
line_rule[i].expire_after=0;
p_line[i]=line_rule+i;
}
- //ret=Maat_cmd_set_lines(feather, p_line, TEST_CMD_LINE_NUM, MAAT_OP_ADD);
- //assert(ret>0);
+ ret=Maat_cmd_set_lines(feather, p_line, TEST_CMD_LINE_NUM, MAAT_OP_DEL);
+ assert(ret>0);
+ /*
for(i=0;i<TEST_CMD_LINE_NUM;i++)
{
line_rule[i].table_line=NULL;
}
ret=Maat_cmd_set_lines(feather, p_line,TEST_CMD_LINE_NUM, MAAT_OP_DEL);
assert(ret>0);
+ */
return;
}
+/*
void Maat_redis_start_cb(int update_type,void* u_para)
{
MGW_LOG_INFO(g_mgw_ctx.logger, "call Maat_redis_start_cb");
@@ -171,11 +147,12 @@ int Maat_test_plugin_table(Maat_feather_t feather,const char* table_name,
}
return ret;
}
-
+*/
int main(int argc, char* argv[])
{
- mgw_init();
+ Maat_feather_t Maat_feather = Maat_init();
+ /*
Maat_test_plugin_table(g_mgw_ctx.Maat_feather, "QD_ENTRY_INFO",
Maat_redis_start_cb,
Maat_redis_update_cb,
@@ -183,11 +160,8 @@ int main(int argc, char* argv[])
g_mgw_ctx.Maat_feather,
g_mgw_ctx.logger);
sleep(10);
- Maat_test_set_cmd_line(g_mgw_ctx.Maat_feather);
+ */
+ Maat_test_set_cmd_line(Maat_feather);
sleep(3600);
}
-#ifdef __cplusplus
-}
-#endif
-