diff --git a/Common/config_manager.h b/Common/config_manager.h index c07b5c42f..f4f8c02ae 100755 --- a/Common/config_manager.h +++ b/Common/config_manager.h @@ -32,6 +32,9 @@ /*nat config */ #define NAT_CONFIG_MODULE 0x00000008 +/*static routing*/ +#define STATIC_ROUTING_CONFIG_MODULE 0x00000009 + /************************* 模块定义结束 **********************/ /************************ config id定义 **********************/ @@ -69,8 +72,8 @@ #define NAT4_CONFIG (uint64)((uint64)NAT_CONFIG_MODULE<<32|1) - - +#define STATIC_ROUTING_CONFIG (uint64)((uint64)STATIC_ROUTING_CONFIG_MODULE<<32|1) +#define GET_ALL_ROUTING_INFO (uint64)((uint64)STATIC_ROUTING_CONFIG_MODULE<<32|2) /************************ config id定义 end**********************/ #endif diff --git a/Common/klog_api.h b/Common/klog_api.h index 5292a31b9..8a078ae2a 100644 --- a/Common/klog_api.h +++ b/Common/klog_api.h @@ -7,7 +7,7 @@ #define MAX_MODULE_NAME_SZ 16 typedef struct _klog { - char module_name[MAX_MODULE_NAME_SZ]; + char module_name[MAX_MODULE_NAME_SZ+1]; }klog_t; diff --git a/Common/ret_errno.h b/Common/ret_errno.h index 7ef6ed927..bf9688cb8 100644 --- a/Common/ret_errno.h +++ b/Common/ret_errno.h @@ -26,6 +26,8 @@ typedef uint ret_code; #define RET_EXIST 11 #define RET_FULL 12 #define RET_SENDERR 13 +#define RET_NOCMID 14 +#define RET_SRCERR 15 /* NETCONFIG_MODULE 0x00010000 ~ 0x0001ffff*/ #define RET_IPINVALID (uint)((uint)NETCONFIG_MODULE<<16|1) @@ -50,6 +52,8 @@ typedef uint ret_code; { RET_EXIST, "AlreadyExist"},\ { RET_FULL, "Full"},\ { RET_SENDERR, "SendErr"},\ + { RET_NOCMID, "CanNotFindConfig"},\ + { RET_SRCERR, "ConfigSourceErr"},\ \ { RET_IPINVALID, "IpInvalid"},\ { RET_BRNAMEERR, "BrNameInvalid"}\ diff --git a/Common/ulog_api.h b/Common/ulog_api.h index 1d199e2e6..fd2fbb90e 100755 --- a/Common/ulog_api.h +++ b/Common/ulog_api.h @@ -8,7 +8,7 @@ #define MAX_MODULE_NAME_SZ 16 typedef struct _ulog { - char module_name[MAX_MODULE_NAME_SZ]; + char module_name[MAX_MODULE_NAME_SZ+1]; } ulog_t; ulog_t *ulog_init(const char *module_name, u8 is_print); diff --git a/Makefile b/Makefile index 234e7fdc7..574c8548f 100755 --- a/Makefile +++ b/Makefile @@ -28,9 +28,9 @@ MAKE_FLAGS += -j$(shell cat /proc/cpuinfo | grep processor | wc -l) endif endif -.PHONY : demo database openrpc ulog klog klog_test conntrack netlink trace redismq usermanager configm webauth matchrule logging +.PHONY : demo database openrpc ulog klog klog_test conntrack netlink trace redismq usermanager configm webauth matchrule logging ulog_test -all: demo database openrpc ulog klog klog_test conntrack netlink trace redismq usermanager configm webauth matchrule logging +all: demo database openrpc ulog klog klog_test conntrack netlink trace redismq usermanager configm webauth matchrule logging ulog_test ifeq ($(OPT), install) #$(shell `find ../release -name "*.zip" -delete`) @@ -228,4 +228,13 @@ else ifeq ($(OPT), install) $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f module.klog_api.test.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=test_klog_api else $(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f module.klog_api.test.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=test_klog_api +endif + +ulog_test: +ifeq ($(OPT), clean) + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.ulog-test.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=test_ulog_api +else ifeq ($(OPT), install) + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.ulog-test.Makefile install MLOG=$(MLOG) MAKE_TARGET=test_ulog_api +else + $(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.ulog.ulog-test.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=test_ulog_api endif \ No newline at end of file diff --git a/Platform/build/module.rpdb.Makefile b/Platform/build/module.rpdb.Makefile new file mode 100755 index 000000000..a11fcfa4e --- /dev/null +++ b/Platform/build/module.rpdb.Makefile @@ -0,0 +1,50 @@ +# target name, the target name must have the same name of c source file +TARGET_NAME=rpdb + +# target +# for linux module driver: KO +# for application: EXE +# for dynamic library: DLL +TARGET_TYPE = KO + +# target object +# for application: APP +# for device driver: DRV +TARGET_OBJ = DRV + +# custom install dir +TARGET_BOX = + +#debug mode or release mode +DEBUG = TRUE + +PLAT_LINUX ?= TRUE +PLAT_ARM64 ?= TRUE + +VPATH = ../modules + +# source code + +# set the source file, don't used .o because of ... + +COMMON_SRCS = ./rpdb/dpi_trie_cache.c + +# MRS Board Source Files +PLAT_LINUX_SRCS = $(COMMON_SRCS) +PLAT_ARM64_SRCS = $(COMMON_SRCS) + +# gcc CFLAGS +PLAT_ARM64_CFLAGS := +PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) + +# this line must be at below of thus, because of... +include ../../Common/common.Makefile + +ifeq ($(MAKECMDGOALS), ) +$(shell find ./ -name "$(TARGET)-*.ko" -delete) +else +ifeq ($(MAKECMDGOALS), all) +$(shell find ./ -name "$(TARGET)-*.ko" -delete) +endif +endif + diff --git a/Platform/build/user.configm.Makefile b/Platform/build/user.configm.Makefile index cea8c2a28..9536bcef2 100755 --- a/Platform/build/user.configm.Makefile +++ b/Platform/build/user.configm.Makefile @@ -1,3 +1,4 @@ + # target name, the target name must have the same name of c source file TARGET_NAME=configm @@ -39,6 +40,10 @@ COMMON_SRCS = configserver.c \ log_config/log_config_console.c log_config/log_config_init.c log_config/log_config_cm.c log_config/log_config_monitor.c log_config/log_config_remote.c log_config/log_config_file.c \ nat_config/natconfig.c \ vlan_config/vlan_config.c \ + dhcp_config/dhcp_client_config.c dhcp_config/dhcp_dhcpd_lease.c dhcp_config/dhcp_host_config.c dhcp_config/dhcp_lib.c dhcp_config/dhcp_relay_config.c dhcp_config/dhcp_shared_network_config.c dhcp_config/dhcp_subnet_config.c\ + static_routing_config/static_routing_config.c \ + + # MRS Board Source Files PLAT_LINUX_SRCS = $(COMMON_SRCS) diff --git a/Platform/build/user.rpdb.Makefile b/Platform/build/user.rpdb.Makefile new file mode 100644 index 000000000..ccbbc55e2 --- /dev/null +++ b/Platform/build/user.rpdb.Makefile @@ -0,0 +1,67 @@ +# target name, the target name must have the same name of c source file +TARGET_NAME=librpdb + +# target +# for linux module driver: KO +# for application: EXE +# for dynamic library: DLL +TARGET_TYPE = DLL + +# target object +# for application: APP +# for device driver: DRV +TARGET_OBJ = APP + +# custom install dir +TARGET_BOX = + +#debug mode or release mode +DEBUG = TRUE + +PLAT_LINUX ?= TRUE +PLAT_ARM64 ?= TRUE + +VPATH = ../user/rpdb/ + +# source code + +# set the source file, don't used .o because of ... + +COMMON_SRCS = rpdb.c + +# MRS Board Source Files +PLAT_LINUX_SRCS = $(COMMON_SRCS) +PLAT_ARM64_SRCS = $(COMMON_SRCS) + +# gcc CFLAGS +PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../common/rpdb +PLAT_LINUX_CFLAGS := -fPIC -I../../Common -I../common/rpdb + + +PLAT_ARM64_LDFLAGS := -fPIC -shared +PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS) + + +#gcc libs +ARM64_LIBS := +LINUX_LIBS := + +# this line must be at below of thus, because of... +include ../../Common/common.Makefile + +ifneq ($(MAKECMDGOALS), clean) +ifneq ($(MAKECMDGOALS), cleanall) +ifneq ($(notdir $(DEPEND_LIB)), $(wildcard $(DEPEND_LIB))) +$(shell $(CP) $(DEPEND_LIB) ./) +endif +endif +endif + +ifeq ($(MAKECMDGOALS), ) +$(shell find ./ -name "$(TARGET)-*.ko" -delete) +else +ifeq ($(MAKECMDGOALS), all) +$(shell find ./ -name "$(TARGET)-*.ko" -delete) +endif +endif + diff --git a/Platform/build/user.ulog.ulog-test.Makefile b/Platform/build/user.ulog.ulog-test.Makefile new file mode 100644 index 000000000..4986b5f72 --- /dev/null +++ b/Platform/build/user.ulog.ulog-test.Makefile @@ -0,0 +1,66 @@ +# target name, the target name must have the same name of c source file +TARGET_NAME=test_ulog_api + +# target +# for linux module driver: KO +# for application: EXE +# for dynamic library: DLL +TARGET_TYPE = EXE + +# target object +# for application: APP +# for device driver: DRV +TARGET_OBJ = APP + +# custom install dir +TARGET_BOX = + +#debug mode or release mode +DEBUG = TRUE + +PLAT_LINUX ?= TRUE +PLAT_ARM64 ?= TRUE + +VPATH = ../user/ulog/ulog-test + +# source code + +# set the source file, don't used .o because of ... + +COMMON_SRCS = ulog_test.c + +# MRS Board Source Files +PLAT_LINUX_SRCS = $(COMMON_SRCS) +PLAT_ARM64_SRCS = $(COMMON_SRCS) + +# gcc CFLAGS +PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../common/ulog -I../user/ulog +PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) + + +PLAT_ARM64_LDFLAGS := +PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS) + +#gcc libs +ARM64_LIBS := -lopenrpc-arm64 -lulogapi-arm64 -lpthread -lm -lev +LINUX_LIBS := -lopenrpc-linux -lulogapi-linux -lpthread -lm -lev + +# this line must be at below of thus, because of... +include ../../Common/common.Makefile + +ifneq ($(MAKECMDGOALS), clean) +ifneq ($(MAKECMDGOALS), cleanall) +ifneq ($(notdir $(DEPEND_LIB)), $(wildcard $(DEPEND_LIB))) +$(shell $(CP) $(DEPEND_LIB) ./) +endif +endif +endif + +ifeq ($(MAKECMDGOALS), ) +$(shell find ./ -name "$(TARGET)-*.ko" -delete) +else +ifeq ($(MAKECMDGOALS), all) +$(shell find ./ -name "$(TARGET)-*.ko" -delete) +endif +endif + diff --git a/Platform/modules/klog_api/api/klog_api.c b/Platform/modules/klog_api/api/klog_api.c index 3e6237056..a25a9d763 100644 --- a/Platform/modules/klog_api/api/klog_api.c +++ b/Platform/modules/klog_api/api/klog_api.c @@ -18,8 +18,8 @@ klog_t *klog_init(const char *module_name) len = strlen(module_name); - if (len >= MAX_MODULE_NAME_SZ) { - printk(KERN_ERR"The length:%d of module_name must be less than %d, but input module_name is %s", len, MAX_MODULE_NAME_SZ, module_name); + if (len > MAX_MODULE_NAME_SZ) { + printk(KERN_ERR"The length:%d of module_name must be not more than %d, but input module_name is %s", len, MAX_MODULE_NAME_SZ, module_name); return NULL; } @@ -28,7 +28,7 @@ klog_t *klog_init(const char *module_name) printk(KERN_ERR"Allocating log memory is failure"); return NULL; } - memset(log->module_name, '\0', MAX_MODULE_NAME_SZ); + memset(log->module_name, '\0', MAX_MODULE_NAME_SZ+1); strncpy(log->module_name, module_name, len); return log; diff --git a/Platform/modules/rpdb/dpi_trie_cache.c b/Platform/modules/rpdb/dpi_trie_cache.c new file mode 100644 index 000000000..643d61131 --- /dev/null +++ b/Platform/modules/rpdb/dpi_trie_cache.c @@ -0,0 +1,296 @@ + +#include +#include +#include +#include +#include +#include +#include +#include + + +/* + * 三元组超时时间设置为120s + */ +int dpi_cache_node_timeout = 120; + +/* + * 三元组数据结构 +*/ +struct dpi_cache_node { + struct list_head list; + + union { + struct in6_addr ip6; + struct in_addr ip4; + + }ip; + + __u16 proto; + __u16 port; + __u32 appid; + uint64_t uptime; +}; + + +/* + * 初始化cache链表 + */ +#define TRIPLE_CAHCE_HASH_SIZE (2048) +struct list_head dpi_cache_head[TRIPLE_CAHCE_HASH_SIZE] = {0}; + +typedef struct pkt_info{ + unsigned int srcip; + unsigned short sport; + unsigned int dstip; + unsigned short dport; + unsigned short proto; +}pkt_info_t; + +#define POLY 0x01101 // CRC20生成多项式x^20+x^12+x^8+1即:01101 CRC32:04C11DB7L +static unsigned int crc_table[256] = {0}; + +unsigned int get_sum_poly(unsigned char data) +{ + unsigned int sum_poly = data; + int j; + sum_poly <<= 24; + for(j = 0; j < 8; j++) + { + int hi = sum_poly&0x80000000; // 取得reg的最高位 + sum_poly <<= 1; + if(hi) sum_poly = sum_poly^POLY; + } + return sum_poly; +} + +void create_crc_table(void) //在使用CRC20_key函数应该先建立crc表 +{ + int i; + for(i = 0; i < 256; i++) + { + crc_table[i] = get_sum_poly(i&0xFF); + } +} + +unsigned int CRC20_key(unsigned char* data, int len) +{ + int i; + unsigned int reg = 0xFFFFFFFF;// 0xFFFFFFFF,见后面解释 + for(i = 0; i < len; i++) + { + reg = (reg<<8) ^ crc_table[((reg>>24)&0xFF) ^ data[i]]; + } + return (reg&0XFFFFF);//得到的reg取后20作为key值 +} + +/* + * 初始化dpi三元组hash链表 + */ +void dpi_cache_list_init(void) +{ + int i = 0; + for (i = 0; i < TRIPLE_CAHCE_HASH_SIZE; i++){ + INIT_LIST_HEAD(&dpi_cache_head[i]); + } + + return; +} + + +/* + * 三元组hash值计算 + */ +int dpi_cache_node_hash(struct dpi_cache_node node) +{ + int i = 0; + int dip = 0; + + pkt_info_t info; + info.dport = node.port; + info.sport = 0; + info.srcip = 0; + info.proto = node.proto; + if (node.ip.ip4.s_addr !=0 ) { + info.dstip = node.ip.ip4.s_addr; + } else { + for (i = 0; i < 4; ++i) { + dip ^= node.ip.ip6.s6_addr32[i]; + } + info.dstip = dip; + } + + return CRC20_key((unsigned char *)&info, sizeof(pkt_info_t))&(TRIPLE_CAHCE_HASH_SIZE - 1); +} + + +/* + * src 和dst比较,并更新dst的appid,update_appid=true,update +*/ +bool dpi_cache_node_compare(struct dpi_cache_node src,struct dpi_cache_node dst,bool update_appid) +{ + int i = 0; + if (src.port != dst.port){ + return false; + } + + if (src.proto != dst.proto){ + return false; + } + + if(src.ip.ip4.s_addr != 0){ + if(src.ip.ip4.s_addr != dst.ip.ip4.s_addr){ + return false; + } + }else{ + for (i = 0; i < 4; i++){ + if (src.ip.ip6.s6_addr32[i] != dst.ip.ip6.s6_addr32[i]){ + return false; + } + } + + } + + if (update_appid){ + dst.appid = src.appid; + dst.uptime = get_jiffies_64(); + } + + return true; +} + + +/* + *遍历hash链表,查找node + */ +bool dpi_cache_node_search(struct dpi_cache_node node,int flag) +{ + struct list_head * pList; + struct dpi_cache_node *pNode; + int hash = dpi_cache_node_hash(node); + list_for_each(pList,&dpi_cache_head[hash]){ + pNode = list_entry(pList,struct dpi_cache_node,list); + if (dpi_cache_node_compare(node,*pNode,flag)){ + return true; + } + } + + return false; +} + + +/* + * DPI 信息记录,外部接口调用 +*/ +bool dpi_cache_node_add(struct dpi_cache_node node) +{ + int hash = 0; + struct dpi_cache_node *pNode =NULL; + + if (dpi_cache_node_search(node,true)) { + return true; + } + + pNode = kmalloc(sizeof(struct dpi_cache_node), GFP_KERNEL); + if (NULL == pNode) { + return false; + } + + hash = dpi_cache_node_hash(node); + pNode->uptime = get_jiffies_64(); + pNode->appid = node.appid; + list_add_tail(&pNode->list,&dpi_cache_head[hash]); + return true; +} + + +/* + * 释放hash链表上所有node节点 +*/ +void dpi_cache_list_release(void) +{ + struct list_head * pList = NULL; + struct dpi_cache_node *pNode = NULL; + int i = 0; + + for (i = 0; i < TRIPLE_CAHCE_HASH_SIZE; i++){ + list_for_each(pList,&dpi_cache_head[i]){ + pNode = list_entry(pList,struct dpi_cache_node,list); + if(pNode){ + list_del(&pNode->list); + } + + } + } +} + + +/* + *定时器超时,删除超时的节点 + */ +void dpi_cache_node_timeout_func(void) +{ + struct list_head * pList = NULL; + struct dpi_cache_node *pNode = NULL; + int i = 0; + + for (i = 0; i < TRIPLE_CAHCE_HASH_SIZE; i++){ + list_for_each(pList,&dpi_cache_head[i]){ + pNode = list_entry(pList,struct dpi_cache_node,list); + if(pNode&& + (get_jiffies_64() - pNode->uptime >= dpi_cache_node_timeout)){ + list_del(&pNode->list); + } + + + } + } +} + +/* + * 定时器操作 + */ +struct timer_list gTimer; + +void dpi_cache_timer_handler(unsigned long data) { + printk(KERN_INFO"timer pending:%d\n", timer_pending(&gTimer)); + dpi_cache_node_timeout_func(); + mod_timer(&gTimer, jiffies+msecs_to_jiffies(dpi_cache_node_timeout*1000)); + +} + +int dpi_cache_timer_init(void) { + printk(KERN_INFO"%s jiffies:%ld\n", __func__, jiffies); + printk(KERN_INFO"ji:%d,HZ:%d\n", jiffies_to_msecs(250), HZ); + init_timer(&gTimer); + gTimer.expires = jiffies + dpi_cache_node_timeout*HZ; + gTimer.function = dpi_cache_timer_handler; + add_timer(&gTimer); + printk(KERN_INFO"timer pending:%d\n", timer_pending(&gTimer)); + return 0; +} + +void dpi_cache_timer_exit(void) { + printk(KERN_INFO"%s jiffies:%ld\n", __func__, jiffies); + del_timer(&gTimer); +} + +int dpi_cahce_module_init(void) +{ + dpi_cache_timer_init(); + dpi_cache_list_init(); + return 1; +} + +int dpi_cache_module_exit(void) +{ + dpi_cache_timer_exit(); + dpi_cache_list_release(); + return 1; +} + +module_init(dpi_cahce_module_init); +module_exit(dpi_cache_timer_exit); +MODULE_LICENSE("GPL"); + + diff --git a/Platform/thirdparty/arm64/libev-arm64.so b/Platform/thirdparty/arm64/usr/local/lib/libev-arm64.so similarity index 100% rename from Platform/thirdparty/arm64/libev-arm64.so rename to Platform/thirdparty/arm64/usr/local/lib/libev-arm64.so diff --git a/Platform/thirdparty/x86_64/libev-linux.so b/Platform/thirdparty/x86_64/usr/local/lib/libev-linux.so old mode 100755 new mode 100644 similarity index 100% rename from Platform/thirdparty/x86_64/libev-linux.so rename to Platform/thirdparty/x86_64/usr/local/lib/libev-linux.so diff --git a/Platform/user/configm/config-server/configserver.c b/Platform/user/configm/config-server/configserver.c index ac1b14ed0..80fb09014 100644 --- a/Platform/user/configm/config-server/configserver.c +++ b/Platform/user/configm/config-server/configserver.c @@ -327,14 +327,14 @@ void cm_config_process(rpc_conn *conn, pointer input, int input_len, void* data) config_svr = cm_config_service_get(config_msg->config_id); if(config_svr == NULL) { - cm_return(conn, RET_NULLP, "NULL pointer"); + cm_return(conn, RET_NOCMID, "can not find config id"); return; } /*source check*/ if(!(config_svr->config_src & config_msg->source)) { - cm_return(conn, RET_CHKERR, "source check error!\r\n"); + cm_return(conn, RET_SRCERR, "source check error!\r\n"); return; } diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_lib.c b/Platform/user/configm/config-server/dhcp_config/dhcp_lib.c index c25d43113..dc61a9c5b 100644 --- a/Platform/user/configm/config-server/dhcp_config/dhcp_lib.c +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_lib.c @@ -1,5 +1,46 @@ #include "dhcp_lib.h" +void dhcp_config_init(void) +{ + ret_code ret = RET_OK; + ret = br_event_register(BR_DELETE_EVENT_PRE,del_interface_dhcp_cb); +} + +int del_interface_dhcp_cb(BR_EVENT_TYPE event_type, br_event_t event_arg) +{ + ret_code ret = RET_OK; + char *segment = get_interface_subnet(event_arg.br_name); + if(NULL == segment){ + return ret; + } + char *mask = get_interface_mask(event_arg.br_name); + if(NULL == mask){ + return ret; + } + int len = 500; + char *cmd = (char *)malloc(len + 1); + if(NULL == cmd) + { + return RET_ERR; + } + + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/%s //g' /etc/default/isc-dhcp-server", event_arg.br_name); + system(cmd); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*subnet[ \\t]+%s[ \\t]+netmask[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\,\\-]+\\}/#/g' /etc/dhcp/dhcpd.conf", segment, mask); + system(cmd); + system("sed -r -i '/#/d' /etc/dhcp/dhcpd.conf"); + if(segment){ + free(segment); + } + if(mask){ + free(mask); + } + /*ļ*/ + system("service isc-dhcp-server restart"); + return ret; +} char *getfileall(char *fname) { diff --git a/Platform/user/configm/config-server/include/configm.h b/Platform/user/configm/config-server/include/configm.h index e6c97eac7..0a68990d5 100755 --- a/Platform/user/configm/config-server/include/configm.h +++ b/Platform/user/configm/config-server/include/configm.h @@ -14,6 +14,9 @@ #include "../web_config/auth_recover_config.h" #include "natconfig.h" #include "vlan_config.h" +#include "dhcp_lib.h" +#include "static_routing.h" + #define RET_CODE_LEN 16 #define RET_MSG_LEN 128 @@ -31,7 +34,11 @@ { \ VLAN_CONFIG_MODULE, \ vlan_config_init \ - } \ + }, \ + { \ + DHCP_CONFIG_MODULE, \ + dhcp_config_init \ + } \ } /* @@ -217,6 +224,78 @@ vlan_config_proc, \ vlan_config_get, \ vlan_config_get_all \ + },\ + {\ + DHCP_HOST_CONFIG, \ + CONFIG_FROM_WEB, \ + FALSE, \ + dhcp_host_config_chk, \ + dhcp_host_config_proc, \ + dhcp_host_config_get, \ + NULL \ + },\ + {\ + DHCP_SUBNET_CONFIG, \ + CONFIG_FROM_WEB, \ + FALSE, \ + dhcp_subnet_config_chk, \ + dhcp_subnet_config_proc, \ + dhcp_subnet_config_get, \ + NULL \ + },\ + {\ + DHCP_RELAY_CONFIG, \ + CONFIG_FROM_WEB, \ + FALSE, \ + dhcp_relay_config_chk, \ + dhcp_relay_config_proc, \ + NULL, \ + NULL \ + },\ + {\ + DHCP_CLIENT_CONFIG, \ + CONFIG_FROM_WEB, \ + FALSE, \ + dhcp_client_config_chk, \ + dhcp_client_config_proc, \ + dhcp_client_get, \ + NULL \ + },\ + {\ + DHCP_DHCPD_LEASE, \ + CONFIG_FROM_WEB, \ + FALSE, \ + dhcp_dhcpd_lease_config_chk, \ + NULL, \ + NULL, \ + dhcp_dhcpd_lease_get_all \ + },\ + {\ + DHCP_SHARED_NETWORK_CONFIG, \ + CONFIG_FROM_WEB, \ + FALSE, \ + dhcp_shared_network_config_chk, \ + dhcp_shared_network_config_proc, \ + NULL, \ + dhcp_shared_network_config_get_all \ + },\ + {\ + STATIC_ROUTING_CONFIG, \ + CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ + TRUE, \ + routing_config_chk, \ + routing_config_proc, \ + NULL, \ + routing_config_get_all \ + },\ + {\ + GET_ALL_ROUTING_INFO, \ + CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ + FALSE, \ + all_routing_config_chk, \ + NULL, \ + NULL, \ + all_routing_get_all \ }\ } @@ -270,3 +349,4 @@ int cm_format_data(ret_code ret_code, cJSON *json_obj, char *output); #endif /* RPC_COMMON_H_ */ + diff --git a/Platform/user/configm/config-server/include/dhcp_lib.h b/Platform/user/configm/config-server/include/dhcp_lib.h index 8ee1c12a9..0901c6e53 100644 --- a/Platform/user/configm/config-server/include/dhcp_lib.h +++ b/Platform/user/configm/config-server/include/dhcp_lib.h @@ -20,6 +20,19 @@ #include "rpc.h" #include "netconfig.h" +#include "dhcp_client_config.h" +#include "dhcp_host_config.h" +#include "dhcp_relay_config.h" +#include "dhcp_shared_network_config.h" +#include "dhcp_subnet_config.h" +#include "dhcp_dhcpd_lease.h" + + +void dhcp_config_init(void); + +int del_interface_dhcp_cb(BR_EVENT_TYPE event_type, br_event_t event_arg); + + char *getfileall(char *fname); char *getfilefirstline(char *fname); diff --git a/Platform/user/configm/config-server/include/static_routing.h b/Platform/user/configm/config-server/include/static_routing.h new file mode 100644 index 000000000..8c19a4d2d --- /dev/null +++ b/Platform/user/configm/config-server/include/static_routing.h @@ -0,0 +1,73 @@ +#ifndef STATIC_ROUTING_H_ +#define STATIC_ROUTING_H_ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define IP_MAX_LENGTH 128 +#define INTERFACE_MAX_LENGTH 20 +#define LINE_MAX_LENGTH 128 +#define ROUTING_TABLE_MAX_NUMBER 100 + +#define STRING_LENGTH 128 +#define IPV4_GET_STATIC_ROUTING "route -n" +#define IPV6_GET_STATIC_ROUTING "route -6" +#define IPV4_VERSION 4 +#define IPV6_VERSION 6 +#define ACTION_LENGTH 10 + +#define STATIC_ROUTING_PATH "/etc/network/static_routing" + + +struct routing_string{ + int version; + char destip[IP_MAX_LENGTH]; + int netmask; + char gateway[IP_MAX_LENGTH]; + char dev[INTERFACE_MAX_LENGTH]; + int metric; +}; + + + +typedef struct routing_string routing_t; + + + +/*static routing config */ +ret_code routing_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code routing_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + + +ret_code routing_config_get_all(uint source, + pointer output, int *output_len); + + +/*ȡ·Ϣ*/ + +ret_code all_routing_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code all_routing_get_all(uint source, + pointer output, int *output_len); + + + + + +#endif diff --git a/Platform/user/configm/config-server/static_routing_config/static_routing b/Platform/user/configm/config-server/static_routing_config/static_routing new file mode 100644 index 000000000..e69de29bb diff --git a/Platform/user/configm/config-server/static_routing_config/static_routing_config.c b/Platform/user/configm/config-server/static_routing_config/static_routing_config.c new file mode 100644 index 000000000..ff4e0595d --- /dev/null +++ b/Platform/user/configm/config-server/static_routing_config/static_routing_config.c @@ -0,0 +1,1389 @@ +#include "configm.h" +#include "rpc.h" +#include "parsefile.h" +#include "static_routing.h" +#include "ipconfig.h" + +/*ṹתΪjson + +{ + "total number": 2, + "route": [{ + "version": 4, + "destip": " 192.168.5.0", + "netmask": 24, + "gateway": "192.168.121.2", + "dev": "ens33", + "metric": 100 + }, { + "version": 4, + "destip": " 192.168.5.10", + "netmask": 32, + "gateway": "192.168.121.2", + "metric": 1000 + }] +} +*/ +ret_code routing_to_json(int count, routing_t *buff_total, pointer output, int *output_len) +{ + + ret_code ret = RET_OK; + char *json_routing; + int i; + + cJSON *ret_root = cJSON_CreateObject(); + if(ret_root == NULL) + { + cJSON_Delete(ret_root); + return RET_NOMEM; + } + + cJSON_AddNumberToObject(ret_root,"total number",count); + + cJSON *json_array = cJSON_CreateArray(); + + if(json_array == NULL) + { + cJSON_Delete(json_array); + return RET_NOMEM; + } + if(count > 0) + { + for(i = 0; i < count; i++) + { + cJSON *json_obj = cJSON_CreateObject(); + if (NULL == json_obj) + { + continue; + } + + cJSON_AddNumberToObject(json_obj,"version",buff_total[i].version); + cJSON_AddStringToObject(json_obj,"destip", buff_total[i].destip); + cJSON_AddNumberToObject(json_obj,"netmask",buff_total[i].netmask); + if(strcmp(buff_total[i].gateway,"") !=0) + { + cJSON_AddStringToObject(json_obj,"gateway", buff_total[i].gateway); + } + + if(strcmp(buff_total[i].dev,"") !=0) + { + cJSON_AddStringToObject(json_obj,"dev", buff_total[i].dev); + } + cJSON_AddNumberToObject(json_obj,"metric",buff_total[i].metric); + + cJSON_AddItemToArray(json_array, json_obj); + rpc_log_info("json_obj: %s\n",cJSON_Print(json_obj)); + + } + + cJSON_AddItemToObject(ret_root,"route",json_array); + } + rpc_log_info("check: %s\n",cJSON_Print(ret_root)); + + json_routing = cJSON_PrintUnformatted(ret_root); + *output_len = strlen(json_routing) + 1; + memcpy(output, json_routing, *output_len); + + free(json_routing); + cJSON_Delete(ret_root); + + return ret; +} + +/*jsonṹ*/ +ret_code routing_json_parse(pointer input, uint *conf_type, routing_t *conf_buff) +{ + cJSON *json_obj, *operate, *route, *version, *destip, *netmask, *gateway, *dev, *metric; + + json_obj = cJSON_Parse(input); + + if(NULL == json_obj) + { + rpc_log_info("json obj is NULL \n"); + cJSON_Delete(json_obj); + return RET_INPUTERR; + } + + rpc_log_info("json input:\n %s \n", cJSON_Print(json_obj)); + + operate = cJSON_GetObjectItem(json_obj,"operate"); + + if(NULL == operate) + { + cJSON_Delete(json_obj); + return RET_INPUTERR; + } + +/*operateֵconf_typeֵ*/ + if(strcmp(operate->valuestring,"add") == 0) + { + *conf_type = CM_CONFIG_ADD; + } + else if (strcmp(operate->valuestring,"del") == 0) + { + *conf_type = CM_CONFIG_DEL; + } + else if (strcmp(operate->valuestring,"getall") == 0) + { + *conf_type = CM_CONFIG_GET_ALL; + return RET_OK; + } + else + { + return RET_INPUTERR; + } + + route = cJSON_GetObjectItem(json_obj,"route"); + + if(NULL == route) + { + cJSON_Delete(json_obj); + return RET_INPUTERR; + } + + version = cJSON_GetObjectItem(route,"version"); + if(!version) + { + return RET_INPUTERR; + } + else + { + conf_buff->version = version->valueint; + } + + rpc_log_info("version:%d \n", conf_buff->version); + + destip = cJSON_GetObjectItem(route,"destip"); + if(!destip) + { + return RET_INPUTERR; + } + else + { + strcpy(conf_buff->destip,destip->valuestring); + } + rpc_log_info("destip:%s \n", conf_buff->destip); + + netmask = cJSON_GetObjectItem(route,"netmask"); + if(!netmask) + { + return RET_INPUTERR; + } + else + { + conf_buff->netmask = netmask->valueint; + } + rpc_log_info("netmask:%d \n", conf_buff->netmask); + + gateway = cJSON_GetObjectItem(route,"gateway"); + if(!gateway) + { + //memset(conf_buff->gateway,'\0',sizeof(conf_buff->gateway)); + strcpy(conf_buff->gateway,""); + } + else + { + strcpy(conf_buff->gateway,gateway->valuestring); + } + rpc_log_info("gateway:%s \n", conf_buff->gateway); + + dev = cJSON_GetObjectItem(route,"dev"); + if(!dev) + { + strcpy(conf_buff->dev,""); + } + else + { + strcpy(conf_buff->dev,dev->valuestring); + } + rpc_log_info("dev: %s \n", conf_buff->dev); + + metric = cJSON_GetObjectItem(route,"metric"); + if(!metric) + { + return RET_INPUTERR; + } + if((metric->valueint) < 1) + { + return RET_INPUTERR; + } + else + { + conf_buff->metric = metric->valueint; + } + rpc_log_info("metric:%d \n", conf_buff->metric); + + cJSON_Delete(json_obj); + + return RET_OK; + +} + + + +/*ļ*/ +ret_code insert_into_file(char *string) +{ + FILE *f; + ret_code ret = RET_OK; + char *temp_buff[LINE_MAX_LENGTH]; + int flag = 0; + + rpc_log_info("string: %s\n",string); + + f = fopen (STATIC_ROUTING_PATH,"at+"); + if(NULL == f) + { + rpc_log_error("open config file fail\n"); + return RET_SYSERR; + } + + fseek(f,0,SEEK_SET); + while(fgets(temp_buff,sizeof(temp_buff),f) != NULL) + { + if (strstr(temp_buff,string) != NULL) + { + flag = 1; + rpc_log_info("config info exist \n"); + } + + if(fgetc(f) == EOF) + { + break; + } + + memset(temp_buff,0,sizeof(temp_buff)); + fseek(f,-1,SEEK_CUR); + } + + if(!flag) + { + fputs(string,f); + } + rpc_log_info("flag: %d\n",flag); + fclose(f); + + return ret; +} + +/*ļɾ*/ +ret_code delete_from_file(char *string) +{ + FILE *f; + ret_code ret = RET_ERR; + int length; + char buff_line[LINE_MAX_LENGTH]; + int flag =0; + + f = fopen (STATIC_ROUTING_PATH,"r+"); + if(NULL == f) + { + rpc_log_error("open config file fail\n"); + return RET_SYSERR; + } + + fseek(f,0,SEEK_END); + length = ftell(f); + + char buff[length]; + + memset(buff,0,sizeof(buff)); + memset(buff_line,0,sizeof(buff_line)); + + fseek(f,0,SEEK_SET); + + while (fgets(buff_line, sizeof(buff_line), f) != NULL) + { + rpc_log_info("check buff_line:%s\n", buff_line); + + if (strstr(buff_line, string) != NULL) + { + flag = 1; + rpc_log_info("config info match \n"); + } + else + { + strcat(buff, buff_line); + } + + if(fgetc(f) == EOF) + { + break; + } + + fseek(f,-1,SEEK_CUR); + memset(buff_line, 0, sizeof(buff_line)); + } + + fclose(f); + + rpc_log_info("flag:%d \n", flag); + + if (flag) + { + ret = conf_file_write (STATIC_ROUTING_PATH, buff); + rpc_log_info("rewrite config file \n"); + } + + return ret; +} + +/*·*/ +ret_code add_routing_exe (char *string) +{ + int status; + ret_code ret = RET_OK; + + rpc_log_info("string:%s\n",string); + + if(NULL == string) + { + return RET_INPUTERR; + } + + status = system(string); + if (-1 == status) + { + rpc_log_error("system shell error!\n"); + return RET_SYSERR; + } + else { + if (WIFEXITED(status)){ + if (0 == WEXITSTATUS(status)){ + rpc_log_info("run shell successfully!\n"); + ret = insert_into_file(string); + } + else{ + rpc_log_error("run shell script fail,script exit code:%d\n",WEXITSTATUS(status)); + return RET_SYSERR; + } + } + else { + rpc_log_error("exit status = [%d]\n",WEXITSTATUS(status)); + return RET_SYSERR; + } + } + + return ret; +} + +/*ɾ·*/ +ret_code del_routing_exe (char *string, char *string_del) +{ + int status; + ret_code ret = RET_OK; + + if(NULL == string || NULL == string_del) + { + return RET_INPUTERR; + } + + status = system(string); + if (-1 == status) + { + rpc_log_error("system shell error!\n"); + return RET_SYSERR; + } + else { + if (WIFEXITED(status)){ + if (0 == WEXITSTATUS(status)){ + rpc_log_info("run shell successfully!\n"); + delete_from_file(string_del); + /*ret = delete_from_file(string_del); //ҪǷɾûȥ·ɣûбļеģϵǷ񷵻ret + if(ret != RET_OK) + { + system(string_del); + }*/ + } + else{ + rpc_log_error("run shell script fail,script exit code:%d\n",WEXITSTATUS(status)); + ret = delete_from_file(string_del); /*ǽӿdown£·ûˣļл*/ + if (ret == RET_OK) + { + return RET_OK; + } + else + { + return RET_SYSERR; + } + } + } + else { + rpc_log_error("exit status = [%d]\n",WEXITSTATUS(status)); + return RET_SYSERR; + } + } + + return ret; + +} + + +/*жgatewaydevǷΪ*/ +void judge_para(int *flag, char *gateway, char *dev) +{ + if (strcmp(gateway,"") != 0) + { + if (strcmp(dev,"") != 0) + { + *flag = 1; + } + + else + { + *flag = 2; + } + + } + else + { + *flag = 3; + } + +} + +/*flag־ǷҪgwdev*/ +void judge_buff(int *flag, char *buff) +{ + if (strstr(buff,"gw") != NULL) + { + if (strstr(buff,"dev") != NULL) + { + *flag = 1; + } + else + { + *flag = 2; + } + } + + else + { + *flag = 3; + } + +} + +/*Ĭ·*/ +ret_code default_routing_proc(routing_t *conf_buff, uint config_type){ + + char string[STRING_LENGTH]; + char string_del[STRING_LENGTH]; + //uint config_type = conf_buff->conf_type; + char *gw_t = conf_buff->gateway; + char *dev_t = conf_buff->dev; + int metric_t = conf_buff->metric; + ret_code ret = RET_OK; + int flag = 0; + + if (conf_buff == NULL) + { + return RET_NULLP; + } + + judge_para(&flag,gw_t,dev_t); + + memset(string, 0, sizeof(string)); + memset(string_del, 0, sizeof(string_del)); + +/*config_typeжӻɾ, +versionжIPV4IPV6 +flagֵװstring*/ + + if (config_type == CM_CONFIG_ADD) + { + if(conf_buff->version == IPV4_VERSION) + { + switch (flag) { + case 1: + sprintf(string,"route add default gw %s dev %s metric %d\n", + gw_t,dev_t,metric_t); + ret = add_routing_exe(string); + break; + case 2: + sprintf(string,"route add default gw %s metric %d\n",gw_t,metric_t); + ret = add_routing_exe(string); + break; + case 3: + sprintf(string,"route add default dev %s metric %d\n",dev_t,metric_t); + ret = add_routing_exe(string); + break; + default: + return RET_ERR; + } + } + + else if (conf_buff->version == IPV6_VERSION) + { + switch (flag) { + case 1: + sprintf(string,"route -A inet6 add default gw %s dev %s metric %d\n", + gw_t,dev_t,metric_t); + ret = add_routing_exe(string); + break; + case 2: + sprintf(string,"route -A inet6 add default gw %s metric %d\n",gw_t,metric_t); + ret = add_routing_exe(string); + break; + case 3: + sprintf(string,"route -A inet6 add default dev %s metric %d\n",dev_t,metric_t); + ret = add_routing_exe(string); + break; + default: + return RET_ERR; + } + } + + else + { + return RET_ERR; + } + } + + else if (config_type == CM_CONFIG_DEL) + { + if(conf_buff->version == IPV4_VERSION) + { + switch (flag) { + case 1: + sprintf(string,"route del default gw %s dev %s metric %d", + gw_t,dev_t,metric_t); + sprintf(string_del,"route add default gw %s dev %s metric %d", + gw_t,dev_t,metric_t); + ret = del_routing_exe(string,string_del); + break; + case 2: + sprintf(string,"route del default gw %s metric %d",gw_t,metric_t); + sprintf(string_del,"route add default gw %s metric %d",gw_t,metric_t); + ret = del_routing_exe(string,string_del); + break; + case 3: + sprintf(string,"route del default dev %s metric %d",dev_t,metric_t); + sprintf(string_del,"route add default dev %s metric %d",dev_t,metric_t); + ret = del_routing_exe(string,string_del); + break; + default: + return RET_ERR; + } + } + + else if(conf_buff->version == IPV6_VERSION) + { + switch (flag) { + case 1: + sprintf(string,"route -A inet6 del default gw %s dev %s metric %d", + gw_t,dev_t,metric_t); + sprintf(string_del,"route -A inet6 add default gw %s dev %s metric %d", + gw_t,dev_t,metric_t); + ret = del_routing_exe(string,string_del); + break; + case 2: + sprintf(string,"route -A inet6 del default gw %s metric %d",gw_t,metric_t); + sprintf(string_del,"route -A inet6 add default gw %s metric %d",gw_t,metric_t); + ret = del_routing_exe(string,string_del); + break; + case 3: + sprintf(string,"route -A inet6 del default dev %s metric %d",dev_t,metric_t); + sprintf(string_del,"route -A inet6 add default dev %s metric %d",dev_t,metric_t); + ret = del_routing_exe(string,string_del); + break; + default: + return RET_ERR; + } + } + } + + else + { + return RET_ERR; + } + + return ret; + +} + +/*жIPV4/IPV6 +ж·/· +װshell*/ +ret_code routing_add(routing_t *conf_buff) +{ + char string[STRING_LENGTH]; + uint version_t = conf_buff->version; + uint netmask_t = conf_buff->netmask; + char route_type[10] = {0}; + ret_code ret = RET_OK; + char *destip_t = conf_buff->destip; + char *gw_t = conf_buff->gateway; + char *dev_t = conf_buff->dev; + int metric_t = conf_buff->metric; + int flag = 0; + + judge_para(&flag,gw_t,dev_t); + + rpc_log_info("flag: %d\n",flag); + + memset(string, 0, sizeof(string)); + + if(version_t == IPV4_VERSION) + { + if (netmask_t > 0 && netmask_t < 32) + { + strcpy(route_type,"-net"); + } + else if(netmask_t == 32) + { + strcpy(route_type,"-host"); + } + else + { + return RET_INPUTERR; + } + + switch (flag){ + case 1: + sprintf(string,"route add %s %s/%d gw %s dev %s metric %d\n",route_type,destip_t,netmask_t,gw_t,dev_t,metric_t); + ret = add_routing_exe(string); + break; + case 2: + sprintf(string,"route add %s %s/%d gw %s metric %d\n",route_type,destip_t,netmask_t,gw_t,metric_t); + ret = add_routing_exe(string); + break; + case 3: + sprintf(string,"route add %s %s/%d dev %s metric %d\n",route_type,destip_t,netmask_t,dev_t,metric_t); + ret = add_routing_exe(string); + break; + default: + return RET_INPUTERR; + } + } + + else if (version_t == IPV6_VERSION) + { + if(netmask_t < 0 || netmask_t >128) + { + return RET_INPUTERR; + } + switch(flag){ + case 1: + sprintf(string,"route -A inet6 add %s/%d gw %s dev %s metric %d\n",destip_t,netmask_t,gw_t,dev_t,metric_t); + ret = add_routing_exe(string); + break; + case 2: + sprintf(string,"route -A inet6 add %s/%d gw %s metric %d\n",destip_t,netmask_t,gw_t,metric_t); + ret = add_routing_exe(string); + break; + case 3: + sprintf(string,"route -A inet6 add %s/%d dev %s metric %d\n",destip_t,netmask_t,dev_t,metric_t); + ret = add_routing_exe(string); + break; + default: + return RET_INPUTERR; + + } + } + return ret; +} + + +ret_code routing_del(routing_t *conf_buff) +{ + char string[STRING_LENGTH]; + char string_del[STRING_LENGTH]; + uint version_t = conf_buff->version; + uint netmask_t = conf_buff->netmask; + char route_type[10] = {0}; + ret_code ret = RET_OK; + char *destip_t = conf_buff->destip; + char *gw_t = conf_buff->gateway; + char *dev_t = conf_buff->dev; + int metric_t = conf_buff->metric; + int flag = 0; + + judge_para(&flag,gw_t,dev_t); + + memset(string, 0, sizeof(string)); + memset(string_del, 0, sizeof(string_del)); + + if(version_t == IPV4_VERSION) + { + if (netmask_t > 0 && netmask_t < 32) + { + strcpy(route_type,"-net"); + } + else if(netmask_t == 32) + { + strcpy(route_type,"-host"); + } + else + { + return RET_INPUTERR; + } + + switch (flag){ + case 1: + sprintf(string,"route del %s %s/%d gw %s dev %s metric %d",route_type,destip_t,netmask_t,gw_t,dev_t,metric_t); + sprintf(string_del,"route add %s %s/%d gw %s dev %s metric %d",route_type,destip_t,netmask_t,gw_t,dev_t,metric_t); + ret = del_routing_exe(string,string_del); + break; + case 2: + sprintf(string,"route del %s %s/%d gw %s metric %d",route_type,destip_t,netmask_t,gw_t,metric_t); + sprintf(string_del,"route add %s %s/%d gw %s metric %d",route_type,destip_t,netmask_t,gw_t,metric_t); + ret = del_routing_exe(string,string_del); + break; + case 3: + sprintf(string,"route del %s %s/%d dev %s metric %d",route_type,destip_t,netmask_t,dev_t,metric_t); + sprintf(string_del,"route add %s %s/%d dev %s metric %d",route_type,destip_t,netmask_t,dev_t,metric_t); + ret = del_routing_exe(string,string_del); + break; + default: + return RET_INPUTERR; + } + } + + else if (version_t == IPV6_VERSION) + { + switch(flag){ + case 1: + sprintf(string,"route -A inet6 del %s/%d gw %s dev %s metric %d",destip_t,netmask_t,gw_t,dev_t,metric_t); + sprintf(string_del,"route -A inet6 add %s/%d gw %s dev %s metric %d",destip_t,netmask_t,gw_t,dev_t,metric_t); + ret = del_routing_exe(string,string_del); + break; + case 2: + sprintf(string,"route -A inet6 del %s/%d gw %s metric %d",destip_t,netmask_t,gw_t,metric_t); + sprintf(string_del,"route -A inet6 add %s/%d gw %s metric %d",destip_t,netmask_t,gw_t,metric_t); + ret = del_routing_exe(string,string_del); + break; + case 3: + sprintf(string,"route -A inet6 del %s/%d dev %s metric %d",destip_t,netmask_t,dev_t,metric_t); + sprintf(string_del,"route -A inet6 add %s/%d dev %s metric %d",destip_t,netmask_t,dev_t,metric_t); + ret = del_routing_exe(string,string_del); + break; + default: + return RET_INPUTERR; + + } + } + return ret; +} + +/*ļжȡ·Ϣ*/ +ret_code get_all_from_file (pointer output, int *output_len) +{ + int ret = RET_OK; + FILE *f; + char buff[LINE_MAX_LENGTH]; + int ipv4_count = 0; + int ipv6_count = 0; + int count = 0; + routing_t *buff_total; + + int flag = 0; + int version = 0; + char destip[IP_MAX_LENGTH] = {0}; + int netmask = 0; + char gateway[IP_MAX_LENGTH] = {0}; + char dev[INTERFACE_MAX_LENGTH] = {0}; + int metric = 100; + + rpc_log_info("check1\n"); + + f = fopen(STATIC_ROUTING_PATH,"r"); + + if(NULL == f) + { + rpc_log_error("open config file fail\n"); + return RET_SYSERR; + } + + buff_total = rpc_new0(routing_t, ROUTING_TABLE_MAX_NUMBER); + //buff_total = (routing_t *)malloc(ROUTING_TABLE_MAX_NUMBER*sizeof(routing_t)); + if(buff_total == NULL) + { + return RET_NOMEM; + } + + memset(buff,0,sizeof(buff)); + memset(buff_total,0,sizeof(buff_total)); + + fseek(f,0,SEEK_SET); + + while (fgets(buff,sizeof(buff),f) != NULL) + { + if(strstr(buff,"route") != NULL) + { + judge_buff(&flag,buff); + rpc_log_info("check2 flag: %d\n", flag); + + + if(strstr(buff,"inet6") != NULL) + { + version = IPV6_VERSION; + if (strstr(buff,"default") != NULL) + { + + switch(flag){ + case 1: + sscanf(buff,"%*s%*s%*s%*s%*s%*s%s%*s%s%*s%d",gateway,dev, &metric); + break; + case 2: + sscanf(buff,"%*s%*s%*s%*s%*s%*s%s%*s%d",gateway, &metric); + break; + case 3: + sscanf(buff,"%*s%*s%*s%*s%*s%*s%s%*s%d",dev, &metric); + break; + default: + ret = RET_ERR; + } + netmask = 0; + strcpy(destip,"::"); + ipv6_count++; + } + else + { + + switch(flag){ + case 1: + sscanf(buff,"%*s%*s%*s%*s%[^/]/%d%*s%s%*s%s%*s%d",destip,&netmask,gateway,dev, &metric); + break; + case 2: + sscanf(buff,"%*s%*s%*s%*s%[^/]/%d%*s%s%*s%d",destip,&netmask,gateway,&metric); + break; + case 3: + sscanf(buff,"%*s%*s%*s%*s%[^/]/%d%*s%s%*s%d",destip,&netmask,dev,&metric); + break; + default: + ret = RET_ERR; + } + ipv6_count ++; + } + } + else if (strstr(buff,"default") != NULL) + { + version = IPV4_VERSION; + switch(flag){ + case 1: + sscanf(buff,"%*s%*s%*s%*s%s%*s%s%*s%d",gateway,dev, &metric); + rpc_log_info("check3: %s %s %d\n", gateway,dev,metric); + break; + case 2: + sscanf(buff,"%*s%*s%*s%*s%s%*s%d",gateway, &metric); + break; + case 3: + sscanf(buff,"%*s%*s%*s%*s%s%*s%d",dev, &metric); + break; + default: + ret = RET_ERR; + } + netmask = 0; + strcpy(destip,"0.0.0.0"); + ipv4_count++; + } + + else + { + version = IPV4_VERSION; + switch(flag){ + case 1: + sscanf(buff,"%*s%*s%*s%[^/]/%d%*s%s%*s%s%*s%d",destip,&netmask,gateway,dev, &metric); + break; + case 2: + sscanf(buff,"%*s%*s%*s%[^/]/%d%*s%s%*s%d",destip,&netmask,gateway,&metric); + break; + case 3: + sscanf(buff,"%*s%*s%*s%[^/]/%d%*s%s%*s%d",destip,&netmask,dev,&metric); + break; + default: + ret = RET_ERR; + } + ipv4_count++; + } + + buff_total[count].version = version; + strcpy(buff_total[count].destip,destip); + buff_total[count].netmask = netmask; + strcpy(buff_total[count].gateway,gateway); + strcpy(buff_total[count].dev,dev); + buff_total[count].metric = metric; + + count ++; + rpc_log_info("check4: ipv4 count %d total count %d\n", ipv4_count,count); + + version = 0; + memset(destip,0,IP_MAX_LENGTH); + netmask = 0; + memset(gateway,0,IP_MAX_LENGTH); + memset(dev,0,INTERFACE_MAX_LENGTH); + metric = 100; + } + + if(fgetc(f) == EOF) + { + break; + } + + fseek(f,-1,SEEK_CUR); + memset(buff,0,sizeof(buff)); + } + + fclose(f); + ret = routing_to_json (count, buff_total, output, output_len); + + rpc_free(buff_total); + return ret; +} + +/*ûָ*/ +ret_code config_recovery() +{ + ret_code ret = RET_OK; + FILE *f; + char buff[LINE_MAX_LENGTH]; + char buff_total[LINE_MAX_LENGTH*ROUTING_TABLE_MAX_NUMBER]; + int status; + + f= fopen(STATIC_ROUTING_PATH,"r"); + + if(NULL == f) + { + rpc_log_error("open config file fail\n"); + return RET_SYSERR; + } + + memset(buff,0,sizeof(buff)); + memset(buff_total,0,sizeof(buff_total)); + + fseek(f,0,SEEK_SET); + + while (fgets(buff,sizeof(buff),f) != NULL) + { + if(strstr(buff,"route") != NULL) + { + + if(strlen(buff) < 3) + { + goto next_while; + } + + status = system(buff); + + if (-1 == status) + { + rpc_log_error("system shell error!\n"); + goto next_while; + } + else { + if (WIFEXITED(status)){ + if (0 == WEXITSTATUS(status)){ + rpc_log_info("run shell successfully!\n"); + rpc_log_info("routing recovery!\n"); + strcat(buff_total,buff); + goto next_while; + } + else{ + rpc_log_error("run shell script fail,script exit code:%d\n",WEXITSTATUS(status)); + goto next_while; + } + } + else { + rpc_log_error("exit status = [%d]\n",WEXITSTATUS(status)); + goto next_while; + } + } + } +next_while: + if(fgetc(f) == EOF) + { + break; + } + + fseek(f,-1,SEEK_CUR); + memset(buff,0,sizeof(buff)); + } + fclose(f); + + ret = conf_file_write(STATIC_ROUTING_PATH,buff_total); + + return ret; +} + + +/*jsonҸֵṹ*/ +ret_code routing_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len){ + + if(source == CONFIG_FROM_RECOVER1) + { + return RET_OK; + } + + if(source == CONFIG_FROM_RECOVER2) + { + return RET_CHKERR; + } + + int conf_length = sizeof(routing_t); + routing_t routing_buff = {0}; + routing_t *conf_buff = &routing_buff; + ret_code ret = RET_OK; + int code = 0; + + ret = routing_json_parse(input,config_type,conf_buff); + + rpc_log_info("conf_buff:%s\n", conf_buff->destip); + rpc_log_info("conf_buff:%d\n", *config_type); + + + /*жgatewaydevǷΪNULL*/ + if((*config_type == CM_CONFIG_ADD) || (*config_type == CM_CONFIG_DEL)) + { + /*жЭǷȷ*/ + if((conf_buff->version != IPV4_VERSION) && (conf_buff->version != IPV6_VERSION)) + { + return RET_CHKERR; + } + else if(strcmp(conf_buff->gateway,"") == 0 && strcmp(conf_buff->dev,"") == 0) + { + rpc_log_info(" gateway and dev are NULL\n"); + return RET_CHKERR; + } + + } + + else if ((*config_type == CM_CONFIG_GET_ALL)) + { + if ((*output_len)<(ROUTING_TABLE_MAX_NUMBER * sizeof(routing_t))) + { + rpc_log_info("getall check error\n"); + return RET_CHKERR; + } + } + + else + { + return RET_CHKERR; + } + + + if(conf_length <= CM_BUFF_SIZE) + { + *input_len = conf_length; + memset(input, 0, *input_len); + memcpy(input, conf_buff, conf_length); + } + else + { + ret = RET_NOMEM; + } + + return ret; +} + + + +/*ӻɾ·*/ +ret_code routing_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + + if(source == CONFIG_FROM_RECOVER1 || source == CONFIG_FROM_RECOVER2) + { + ret = config_recovery(); + return ret; + } + + char string[STRING_LENGTH]; + uint conf_type = config_type; + routing_t conf_buff = {0}; + routing_t *routing_conf = &conf_buff; + int flag = 0; + + routing_conf = (routing_t *)input; + + rpc_log_info("routing info: config_type %d, version %d, destip %s, netmask %d, gateway %s, dev %s, metric %d\n", + conf_type, + routing_conf->version, + routing_conf->destip, + routing_conf->netmask, + routing_conf->gateway, + routing_conf->dev, + routing_conf->metric); + +/*жǷĬ·*/ + if(strcmp(routing_conf->destip,"0.0.0.0") == 0 || strcmp(routing_conf->destip,"::") == 0) + { + if ((routing_conf->netmask) == 0) + { + ret = default_routing_proc(routing_conf,conf_type); + return ret; + } + else + { + return RET_INPUTERR; + } + } + + +/*̬·*/ + switch(config_type){ + case CM_CONFIG_ADD: + ret = routing_add(routing_conf); + break; + + case CM_CONFIG_DEL: + ret = routing_del(routing_conf); + break; + + default: + ret = RET_NOTSUPPORT; + } + + return ret; + +} + + +/*ȡӵľ̬·*/ +ret_code routing_config_get_all(uint source, + pointer output, int *output_len){ + + if (source == CONFIG_FROM_RECOVER1 || source == CONFIG_FROM_RECOVER2) + { + return RET_OK; + } + + rpc_log_info("in function getall\n"); + + int ret = RET_OK; + *output_len = 0; + //int code = 0; + ret = get_all_from_file(output,output_len); + + rpc_log_info("routing_config_get_all: %s\n", output); + + //RET_ERR_FORMART(ret, code, output, *output_len); + ASSERT_RET(ret); + + return ret; + +} + + + + + +/*-------------------------------------------------------------------------*/ + +ret_code all_routing_config_chk(uint source, uint * config_type, + pointer input, int * input_len, + pointer output, int * output_len) +{ + rpc_log_info("test check\n"); + + ret_code ret = RET_OK; + routing_t routing_buff = {0}; + routing_t *conf_buff = &routing_buff; + + routing_json_parse(input,config_type,conf_buff); + + rpc_log_info("conf_buff:%d\n", *config_type); + + if ((*config_type == CM_CONFIG_GET_ALL)) + { + if ((*output_len)<(ROUTING_TABLE_MAX_NUMBER * sizeof(routing_t))) + { + rpc_log_info("getall check error\n"); + return RET_CHKERR; + } + } + + return ret; +} + + + + + +/*ȡ·Ϣ*/ + +ret_code all_routing_get_all(uint source, + pointer output, int *output_len) +{ + FILE *f; + ret_code ret = RET_OK; + routing_t *conf_buff; + int count = 0; + int ipv4_number = 0; + int ipv6_number = 0; + int status; + char temp_buff[LINE_MAX_LENGTH]; + char destip[IP_MAX_LENGTH] = {0}; + char gateway[IP_MAX_LENGTH] = {0}; + char gateway_new[IP_MAX_LENGTH] = {0}; + char netmask[IP_MAX_LENGTH] = {0}; + char dev[INTERFACE_MAX_LENGTH]; + int metric = 100; + struct in_addr addr; + int netmask_int = 0; + + f = popen(IPV4_GET_STATIC_ROUTING,"r"); + if (NULL == f) + { + rpc_log_error("get ipv4 routing info fail\n"); + return RET_SYSERR; + } + + conf_buff = rpc_new0(routing_t,ROUTING_TABLE_MAX_NUMBER); + memset(conf_buff,0,sizeof(conf_buff)); + memset(temp_buff,0,LINE_MAX_LENGTH); + + fseek(f,0,SEEK_SET); + + for(int i=0;i<2;i++) + { + fgets(temp_buff,LINE_MAX_LENGTH,f); + fseek(f,0,SEEK_CUR); + memset(temp_buff,0,LINE_MAX_LENGTH); + } + + while(fgets(temp_buff,LINE_MAX_LENGTH,f) != NULL) + { + if(strlen(temp_buff) <3) + { + goto next_while; + } + + sscanf(temp_buff,"%s %s %s %*s %d %*s %*s %s",destip,gateway,netmask,&metric,dev); + inet_aton(netmask,&addr); + netmask_int = ip_masklen(addr); + conf_buff[count].version = 4; + strcpy(conf_buff[count].destip,destip); + conf_buff[count].netmask = netmask_int; + strcpy(conf_buff[count].gateway,gateway); + strcpy(conf_buff[count].dev,dev); + conf_buff[count].metric = metric; + + count ++; + ipv4_number++; + + memset(destip,0,IP_MAX_LENGTH); + memset(netmask,0,IP_MAX_LENGTH); + memset(gateway,0,IP_MAX_LENGTH); + memset(dev,0,INTERFACE_MAX_LENGTH); + metric = 100; + + goto next_while; + +next_while: + /*if (fgetc(f) == EOF) + { + break; + }*/ + fseek(f,-1,SEEK_CUR); + memset(temp_buff,0,sizeof(temp_buff)); + } + + + fclose(f); + + f = popen(IPV6_GET_STATIC_ROUTING,"r"); + if (NULL == f) + { + rpc_log_error("get ipv6 routing info fail\n"); + return RET_SYSERR; + } + + fseek(f,0,SEEK_SET); + + for(int k=0;k<2;k++) + { + fgets(temp_buff,LINE_MAX_LENGTH,f); + fseek(f,0,SEEK_CUR); + memset(temp_buff,0,LINE_MAX_LENGTH); + } + + while(fgets(temp_buff,LINE_MAX_LENGTH,f) != NULL) + { + if(strlen(temp_buff) <3) + { + fseek(f,-1,SEEK_CUR); + memset(temp_buff,0,sizeof(temp_buff)); + continue; + } + + sscanf(temp_buff,"%[^/]/%d %s %*s %d %*s %*s %s",destip,&netmask_int,gateway,&metric,dev); + rpc_log_info("check 5 after sscanf\n"); + rpc_log_info("check 6: destip %s gateway %s netmask %d metric %d dev %s\n",destip,gateway,netmask_int,metric,dev); + if(metric > 0) + { + sscanf(gateway,"[%[^]]",gateway_new); + conf_buff[count].version = 6; + if (strcmp(destip,"[::]") == 0) + { + strcpy(conf_buff[count].destip,"::"); + } + else + { + strcpy(conf_buff[count].destip,destip); + } + conf_buff[count].netmask = netmask_int; + strcpy(conf_buff[count].gateway,gateway_new); + strcpy(conf_buff[count].dev,dev); + conf_buff[count].metric = metric; + + count ++; + ipv6_number++; + + memset(destip,0,IP_MAX_LENGTH); + memset(netmask,0,IP_MAX_LENGTH); + memset(gateway,0,IP_MAX_LENGTH); + memset(dev,0,INTERFACE_MAX_LENGTH); + metric = 100; + + fseek(f,-1,SEEK_CUR); + memset(temp_buff,0,sizeof(temp_buff)); + } + + else + { + memset(destip,0,IP_MAX_LENGTH); + memset(netmask,0,IP_MAX_LENGTH); + memset(gateway,0,IP_MAX_LENGTH); + memset(dev,0,INTERFACE_MAX_LENGTH); + metric = 100; + fseek(f,-1,SEEK_CUR); + memset(temp_buff,0,sizeof(temp_buff)); + } + + } + + + fclose(f); + + ret = routing_to_json(count,conf_buff, output, output_len); + + rpc_log_info("routing_config_get_all: %s\n", output); + + rpc_free(conf_buff); + + //RET_ERR_FORMART(ret, code, output, *output_len); + ASSERT_RET(ret); + + return ret; +} + + + + + + + + + + + + + + diff --git a/Platform/user/configm/config-server/web_config/authfree.c b/Platform/user/configm/config-server/web_config/authfree.c index 7d8723ed8..edb404324 100644 --- a/Platform/user/configm/config-server/web_config/authfree.c +++ b/Platform/user/configm/config-server/web_config/authfree.c @@ -16,7 +16,7 @@ /*定义结构体数组 存在免认证规则 */ freeauth_configure_t freeauth_array[RULE_MAX_NUM] = {0}; -#define UNAMESIZE (127 + 1) +#define UNAMESIZE (60 + 1) #define SPECHAR(element) (strpbrk((element), "~!@#$%^&*()_+{}|:\"<>?\\,./;\'[]-=`")) //校验特殊字符 #ifdef FREEAUTH_ACK_COOKIES @@ -536,6 +536,8 @@ ret_code freeauth_config_mod_proc(uint source, uint config_type, int i; int cnt; cJSON *res; + cJSON *pJsonArry; + cJSON *pJsonsub; authfree_result_t authfree_result; freeauth_configure_t *freeauth_configure = NULL; uint conf_type = FREEAUTH_CONFIG_GET; @@ -564,6 +566,9 @@ ret_code freeauth_config_mod_proc(uint source, uint config_type, freeauth_configure[j].dip, freeauth_configure[j].dport, freeauth_configure[j].dport, j); } + pJsonArry= cJSON_CreateArray(); /*创建数组*/ + + /*查找要修改的免认证规则名字,不存在则退出程序 */ for(i = 0; i < RULE_MAX_NUM; i++) { /*两个字符串相等 strcmp值为0*/ @@ -602,35 +607,43 @@ ret_code freeauth_config_mod_proc(uint source, uint config_type, commcfgnl_close(); printf("cfgchannel main exit!\r\n"); #endif + + cJSON_AddItemToArray(pJsonArry, pJsonsub=cJSON_CreateObject()); /* 给创建的数组增加对象*/ - /*创建json对象 */ - res = cJSON_CreateObject(); - - if(!res) { - return RET_ERR; + /*在json对象上添加键值对*/ + char *result_message = malloc(strlen(freeauth_configure[j].name)+strlen(authfree_result.message)+1);//+1 for the zero-terminator + //in real code you would check for errors in malloc here + if (result_message == NULL) + { + return RET_NOMEM; } - /*将json对象转换成json字符串 返回处理结果*/ - cJSON_AddNumberToObject(res, "resultcode", authfree_result.resultcode); - cJSON_AddStringToObject(res, "message", authfree_result.message); - ret_char = cJSON_PrintUnformatted(res); + strcpy(result_message, freeauth_configure[j].name); + strcat(result_message, authfree_result.message); + printf("%s\n", result_message); + + cJSON_AddNumberToObject(pJsonsub, "resultcode", authfree_result.resultcode); + cJSON_AddStringToObject(pJsonsub, "message", result_message); + + ret_char = cJSON_Print(pJsonArry); ret_int = strlen(ret_char); - if(output_len) { + if(output_len) + { *output_len = ret_int; } /*超出2k的内存,报错 */ - if(ret_int >= 1024 * 2) { + if(ret_int >= 1024 * 2) + { free(ret_char); - cJSON_Delete(res); + cJSON_Delete(pJsonArry); return RET_NOMEM; } memcpy(output, ret_char, ret_int + 1); - free(ret_char); - cJSON_Delete(res); + free(ret_char); } } } @@ -651,6 +664,8 @@ ret_code freeauth_config_del_proc(uint source, uint config_type, int i; int cnt; cJSON *res; + cJSON *pJsonArry; + cJSON *pJsonsub; authfree_result_t authfree_result; freeauth_configure_t *freeauth_configure = NULL; uint conf_type = FREEAUTH_CONFIG_GET; @@ -679,6 +694,7 @@ ret_code freeauth_config_del_proc(uint source, uint config_type, printf("[%s %d]\n", freeauth_configure[j].name, j); } + pJsonArry= cJSON_CreateArray(); /*创建数组*/ /*查找要修改的免认证规则名字,不存在则退出程序 */ for(i = 0; i < RULE_MAX_NUM; i++) { @@ -719,18 +735,24 @@ ret_code freeauth_config_del_proc(uint source, uint config_type, printf("cfgchannel main exit!\r\n"); #endif - /*创建json对象 */ - res = cJSON_CreateObject(); - - if(!res) + cJSON_AddItemToArray(pJsonArry, pJsonsub=cJSON_CreateObject()); + + /*在json对象上添加键值对*/ + char *result_message = malloc(strlen(freeauth_configure[j].name)+strlen(authfree_result.message)+1);//+1 for the zero-terminator + //in real code you would check for errors in malloc here + if (result_message == NULL) { - return RET_ERR; + return RET_NOMEM; } - /*将json对象转换成json字符串 返回处理结果*/ - cJSON_AddNumberToObject(res, "resultcode", authfree_result.resultcode); - cJSON_AddStringToObject(res, "message", authfree_result.message); - ret_char = cJSON_PrintUnformatted(res); + strcpy(result_message, freeauth_configure[j].name); + strcat(result_message, authfree_result.message); + printf("%s\n", result_message); + + cJSON_AddNumberToObject(pJsonsub, "resultcode", authfree_result.resultcode); + cJSON_AddStringToObject(pJsonsub, "message", result_message); + + ret_char = cJSON_Print(pJsonArry); ret_int = strlen(ret_char); if(output_len) @@ -748,8 +770,7 @@ ret_code freeauth_config_del_proc(uint source, uint config_type, memcpy(output, ret_char, ret_int + 1); - free(ret_char); - cJSON_Delete(res); + free(ret_char); } } } diff --git a/Platform/user/rpdb/list.h b/Platform/user/rpdb/list.h new file mode 100644 index 000000000..6541971ec --- /dev/null +++ b/Platform/user/rpdb/list.h @@ -0,0 +1,641 @@ +#ifndef _LINUX_LIST_H +#define _LINUX_LIST_H + +#define container_of(ptr,type,member) ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) + + +//#if defined(__KERNEL__) || defined(_LVM_H_INCLUDE) + +//#include + +/* + * Simple doubly linked list implementation. + * + * Some of the internal functions ("__xxx") are useful when + * manipulating whole lists rather than single entries, as + * sometimes we already know the next/prev entries and we can + * generate better code by using them directly rather than + * using the generic single-entry routines. + */ + +typedef struct list_head { + struct list_head *next, *prev; +} list_t; + +#define LIST_HEAD_INIT(name) { &(name), &(name) } + +#define LIST_HEAD(name) \ + struct list_head name = LIST_HEAD_INIT(name) + +#define INIT_LIST_HEAD(ptr) do { \ + (ptr)->next = (ptr); (ptr)->prev = (ptr); \ +} while (0) + +/* + * Insert a new entry between two known consecutive entries. + * + * This is only for internal list manipulation where we know + * the prev/next entries already! + */ + +static void inline prefetch(void *p){} + +static inline void __list_add(struct list_head *new, + struct list_head *prev, + struct list_head *next) +{ + next->prev = new; + new->next = next; + new->prev = prev; + prev->next = new; +} + +/** + * list_add - add a new entry + * @new: new entry to be added + * @head: list head to add it after + * + * Insert a new entry after the specified head. + * This is good for implementing stacks. + */ +static inline void list_add(struct list_head *new, struct list_head *head) +{ + __list_add(new, head, head->next); +} + +/** + * list_add_tail - add a new entry + * @new: new entry to be added + * @head: list head to add it before + * + * Insert a new entry before the specified head. + * This is useful for implementing queues. + */ +static inline void list_add_tail(struct list_head *new, struct list_head *head) +{ + __list_add(new, head->prev, head); +} + +/* + * Delete a list entry by making the prev/next entries + * point to each other. + * + * This is only for internal list manipulation where we know + * the prev/next entries already! + */ +static inline void __list_del(struct list_head *prev, struct list_head *next) +{ + next->prev = prev; + prev->next = next; +} + +/** + * list_del - deletes entry from list. + * @entry: the element to delete from the list. + * Note: list_empty on entry does not return true after this, the entry is in an undefined state. + */ +static inline void list_del(struct list_head *entry) +{ + __list_del(entry->prev, entry->next); + entry->next = (void *) 0; + entry->prev = (void *) 0; +} + +/** + * list_replace - replace old entry by new one + * @old : the element to be replaced + * @new : the new element to insert + * + * If @old was empty, it will be overwritten. + */ +static inline void list_replace(struct list_head *old, + struct list_head *new) +{ + new->next = old->next; + new->next->prev = new; + new->prev = old->prev; + new->prev->next = new; +} + +static inline void list_replace_init(struct list_head *old, + struct list_head *new) +{ + list_replace(old, new); + INIT_LIST_HEAD(old); +} + +/** + * list_del_init - deletes entry from list and reinitialize it. + * @entry: the element to delete from the list. + */ +static inline void list_del_init(struct list_head *entry) +{ + __list_del(entry->prev, entry->next); + INIT_LIST_HEAD(entry); +} + +/** + * list_move - delete from one list and add as another's head + * @list: the entry to move + * @head: the head that will precede our entry + */ +static inline void list_move(struct list_head *list, struct list_head *head) +{ + __list_del(list->prev, list->next); + list_add(list, head); +} + +/** + * list_move_tail - delete from one list and add as another's tail + * @list: the entry to move + * @head: the head that will follow our entry + */ +static inline void list_move_tail(struct list_head *list, + struct list_head *head) +{ + __list_del(list->prev, list->next); + list_add_tail(list, head); +} + +/** + * list_empty - tests whether a list is empty + * @head: the list to test. + */ +static inline int list_empty(const struct list_head *head) +{ + return head->next == head; +} + +static inline void __list_splice(struct list_head *list, + struct list_head *head) +{ + struct list_head *first = list->next; + struct list_head *last = list->prev; + struct list_head *at = head->next; + + first->prev = head; + head->next = first; + + last->next = at; + at->prev = last; +} + +/** + * list_splice - join two lists + * @list: the new list to add. + * @head: the place to add it in the first list. + */ +static inline void list_splice(struct list_head *list, struct list_head *head) +{ + if (!list_empty(list)) + __list_splice(list, head); +} + +/** + * list_splice_init - join two lists and reinitialise the emptied list. + * @list: the new list to add. + * @head: the place to add it in the first list. + * + * The list at @list is reinitialised + */ +static inline void list_splice_init(struct list_head *list, + struct list_head *head) +{ + if (!list_empty(list)) { + __list_splice(list, head); + INIT_LIST_HEAD(list); + } +} + +/** + * list_entry - get the struct for this entry + * @ptr: the &struct list_head pointer. + * @type: the type of the struct this is embedded in. + * @member: the name of the list_head within the struct. + */ +#define list_entry(ptr, type, member) \ + container_of(ptr, type, member) + +/** + * list_first_entry - get the first element from a list + * @ptr: the list head to take the element from. + * @type: the type of the struct this is embedded in. + * @member: the name of the list_head within the struct. + * + * Note, that list is expected to be not empty. + */ +#define list_first_entry(ptr, type, member) \ + list_entry((ptr)->next, type, member) + +/** + * list_last_entry - get the last element from a list + * @ptr: the list head to take the element from. + * @type: the type of the struct this is embedded in. + * @member: the name of the list_head within the struct. + * + * Note, that list is expected to be not empty. + */ +#define list_last_entry(ptr, type, member) \ + list_entry((ptr)->prev, type, member) + +/** + * list_first_entry_or_null - get the first element from a list + * @ptr: the list head to take the element from. + * @type: the type of the struct this is embedded in. + * @member: the name of the list_head within the struct. + * + * Note that if the list is empty, it returns NULL. + */ +#define list_first_entry_or_null(ptr, type, member) \ + (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL) + +/** + * list_next_entry - get the next element in list + * @pos: the type * to cursor + * @member: the name of the list_head within the struct. + */ +#define list_next_entry(pos, member) \ + list_entry((pos)->member.next, typeof(*(pos)), member) + +/** + * list_prev_entry - get the prev element in list + * @pos: the type * to cursor + * @member: the name of the list_head within the struct. + */ +#define list_prev_entry(pos, member) \ + list_entry((pos)->member.prev, typeof(*(pos)), member) + +/** + * list_for_each - iterate over a list + * @pos: the &struct list_head to use as a loop cursor. + * @head: the head for your list. + */ +#define list_for_each(pos, head) \ + for (pos = (head)->next; pos != (head); pos = pos->next) + +/** + * list_for_each_prev - iterate over a list backwards + * @pos: the &struct list_head to use as a loop cursor. + * @head: the head for your list. + */ +#define list_for_each_prev(pos, head) \ + for (pos = (head)->prev; pos != (head); pos = pos->prev) + +/** + * list_for_each_safe - iterate over a list safe against removal of list entry + * @pos: the &struct list_head to use as a loop cursor. + * @n: another &struct list_head to use as temporary storage + * @head: the head for your list. + */ +#define list_for_each_safe(pos, n, head) \ + for (pos = (head)->next, n = pos->next; pos != (head); \ + pos = n, n = pos->next) + +/** + * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry + * @pos: the &struct list_head to use as a loop cursor. + * @n: another &struct list_head to use as temporary storage + * @head: the head for your list. + */ +#define list_for_each_prev_safe(pos, n, head) \ + for (pos = (head)->prev, n = pos->prev; \ + pos != (head); \ + pos = n, n = pos->prev) + +/** + * list_for_each_entry - iterate over list of given type + * @pos: the type * to use as a loop cursor. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + */ +#define list_for_each_entry(pos, head, member) \ + for (pos = list_first_entry(head, typeof(*pos), member); \ + &pos->member != (head); \ + pos = list_next_entry(pos, member)) + +/** + * list_for_each_entry_reverse - iterate backwards over list of given type. + * @pos: the type * to use as a loop cursor. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + */ +#define list_for_each_entry_reverse(pos, head, member) \ + for (pos = list_last_entry(head, typeof(*pos), member); \ + &pos->member != (head); \ + pos = list_prev_entry(pos, member)) + +/** + * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() + * @pos: the type * to use as a start point + * @head: the head of the list + * @member: the name of the list_head within the struct. + * + * Prepares a pos entry for use as a start point in list_for_each_entry_continue(). + */ +#define list_prepare_entry(pos, head, member) \ + ((pos) ? : list_entry(head, typeof(*pos), member)) + +/** + * list_for_each_entry_continue - continue iteration over list of given type + * @pos: the type * to use as a loop cursor. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + * + * Continue to iterate over list of given type, continuing after + * the current position. + */ +#define list_for_each_entry_continue(pos, head, member) \ + for (pos = list_next_entry(pos, member); \ + &pos->member != (head); \ + pos = list_next_entry(pos, member)) + +/** + * list_for_each_entry_continue_reverse - iterate backwards from the given point + * @pos: the type * to use as a loop cursor. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + * + * Start to iterate over list of given type backwards, continuing after + * the current position. + */ +#define list_for_each_entry_continue_reverse(pos, head, member) \ + for (pos = list_prev_entry(pos, member); \ + &pos->member != (head); \ + pos = list_prev_entry(pos, member)) + +/** + * list_for_each_entry_from - iterate over list of given type from the current point + * @pos: the type * to use as a loop cursor. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + * + * Iterate over list of given type, continuing from current position. + */ +#define list_for_each_entry_from(pos, head, member) \ + for (; &pos->member != (head); \ + pos = list_next_entry(pos, member)) + +/** + * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry + * @pos: the type * to use as a loop cursor. + * @n: another type * to use as temporary storage + * @head: the head for your list. + * @member: the name of the list_head within the struct. + */ +#define list_for_each_entry_safe(pos, n, head, member) \ + for (pos = list_first_entry(head, typeof(*pos), member), \ + n = list_next_entry(pos, member); \ + &pos->member != (head); \ + pos = n, n = list_next_entry(n, member)) + +/** + * list_for_each_entry_safe_continue - continue list iteration safe against removal + * @pos: the type * to use as a loop cursor. + * @n: another type * to use as temporary storage + * @head: the head for your list. + * @member: the name of the list_head within the struct. + * + * Iterate over list of given type, continuing after current point, + * safe against removal of list entry. + */ +#define list_for_each_entry_safe_continue(pos, n, head, member) \ + for (pos = list_next_entry(pos, member), \ + n = list_next_entry(pos, member); \ + &pos->member != (head); \ + pos = n, n = list_next_entry(n, member)) + +/** + * list_for_each_entry_safe_from - iterate over list from current point safe against removal + * @pos: the type * to use as a loop cursor. + * @n: another type * to use as temporary storage + * @head: the head for your list. + * @member: the name of the list_head within the struct. + * + * Iterate over list of given type from current point, safe against + * removal of list entry. + */ +#define list_for_each_entry_safe_from(pos, n, head, member) \ + for (n = list_next_entry(pos, member); \ + &pos->member != (head); \ + pos = n, n = list_next_entry(n, member)) + +/** + * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal + * @pos: the type * to use as a loop cursor. + * @n: another type * to use as temporary storage + * @head: the head for your list. + * @member: the name of the list_head within the struct. + * + * Iterate backwards over list of given type, safe against removal + * of list entry. + */ +#define list_for_each_entry_safe_reverse(pos, n, head, member) \ + for (pos = list_last_entry(head, typeof(*pos), member), \ + n = list_prev_entry(pos, member); \ + &pos->member != (head); \ + pos = n, n = list_prev_entry(n, member)) + +/** + * list_safe_reset_next - reset a stale list_for_each_entry_safe loop + * @pos: the loop cursor used in the list_for_each_entry_safe loop + * @n: temporary storage used in list_for_each_entry_safe + * @member: the name of the list_head within the struct. + * + * list_safe_reset_next is not safe to use in general if the list may be + * modified concurrently (eg. the lock is dropped in the loop body). An + * exception to this is if the cursor element (pos) is pinned in the list, + * and list_safe_reset_next is called after re-taking the lock and before + * completing the current iteration of the loop body. + */ +#define list_safe_reset_next(pos, n, member) \ + n = list_next_entry(pos, member) + + +/* + * Double linked lists with a single pointer list head. + * Mostly useful for hash tables where the two pointer list head is + * too wasteful. + * You lose the ability to access the tail in O(1). + */ +#define LIST_POISON1 NULL +#define LIST_POISON2 NULL + +#define READ_ONCE(x) (x) +#define WRITE_ONCE(x, val) x=(val) + + +struct hlist_head { + struct hlist_node *first; +}; + +struct hlist_node { + struct hlist_node *next, **pprev; +}; + +#define HLIST_HEAD_INIT { .first = NULL } +#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } +#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) +static inline void INIT_HLIST_NODE(struct hlist_node *h) +{ + h->next = NULL; + h->pprev = NULL; +} + +static inline int hlist_unhashed(const struct hlist_node *h) +{ + return !h->pprev; +} + +static inline int hlist_empty(const struct hlist_head *h) +{ + return !READ_ONCE(h->first); +} + +static inline void __hlist_del(struct hlist_node *n) +{ + struct hlist_node *next = n->next; + struct hlist_node **pprev = n->pprev; + + WRITE_ONCE(*pprev, next); + if (next) + next->pprev = pprev; +} + +static inline void hlist_del(struct hlist_node *n) +{ + __hlist_del(n); + n->next = LIST_POISON1; + n->pprev = LIST_POISON2; +} + +static inline void hlist_del_init(struct hlist_node *n) +{ + if (!hlist_unhashed(n)) { + __hlist_del(n); + INIT_HLIST_NODE(n); + } +} + +static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) +{ + struct hlist_node *first = h->first; + n->next = first; + if (first) + first->pprev = &n->next; + WRITE_ONCE(h->first, n); + n->pprev = &h->first; +} + +/* next must be != NULL */ +static inline void hlist_add_before(struct hlist_node *n, + struct hlist_node *next) +{ + n->pprev = next->pprev; + n->next = next; + next->pprev = &n->next; + WRITE_ONCE(*(n->pprev), n); +} + +static inline void hlist_add_behind(struct hlist_node *n, + struct hlist_node *prev) +{ + n->next = prev->next; + WRITE_ONCE(prev->next, n); + n->pprev = &prev->next; + + if (n->next) + n->next->pprev = &n->next; +} + +/* after that we'll appear to be on some hlist and hlist_del will work */ +static inline void hlist_add_fake(struct hlist_node *n) +{ + n->pprev = &n->next; +} + +static inline int hlist_fake(struct hlist_node *h) +{ + return h->pprev == &h->next; +} + +/* + * Check whether the node is the only node of the head without + * accessing head: + */ +static inline int +hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h) +{ + return !n->next && n->pprev == &h->first; +} + +/* + * Move a list from one list head to another. Fixup the pprev + * reference of the first entry if it exists. + */ +static inline void hlist_move_list(struct hlist_head *old, + struct hlist_head *new) +{ + new->first = old->first; + if (new->first) + new->first->pprev = &new->first; + old->first = NULL; +} + +#define hlist_entry(ptr, type, member) container_of(ptr,type,member) + +#define hlist_for_each(pos, head) \ + for (pos = (head)->first; pos ; pos = pos->next) + +#define hlist_for_each_safe(pos, n, head) \ + for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ + pos = n) + +#define hlist_entry_safe(ptr, type, member) \ + ({ typeof(ptr) ____ptr = (ptr); \ + ____ptr ? hlist_entry(____ptr, type, member) : NULL; \ + }) + +/** + * hlist_for_each_entry - iterate over list of given type + * @pos: the type * to use as a loop cursor. + * @head: the head for your list. + * @member: the name of the hlist_node within the struct. + */ +#define hlist_for_each_entry(pos, head, member) \ + for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ + pos; \ + pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) + +/** + * hlist_for_each_entry_continue - iterate over a hlist continuing after current point + * @pos: the type * to use as a loop cursor. + * @member: the name of the hlist_node within the struct. + */ +#define hlist_for_each_entry_continue(pos, member) \ + for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\ + pos; \ + pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) + +/** + * hlist_for_each_entry_from - iterate over a hlist continuing from current point + * @pos: the type * to use as a loop cursor. + * @member: the name of the hlist_node within the struct. + */ +#define hlist_for_each_entry_from(pos, member) \ + for (; pos; \ + pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) + +/** + * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry + * @pos: the type * to use as a loop cursor. + * @n: another &struct hlist_node to use as temporary storage + * @head: the head for your list. + * @member: the name of the hlist_node within the struct. + */ +#define hlist_for_each_entry_safe(pos, n, head, member) \ + for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\ + pos && ({ n = pos->member.next; 1; }); \ + pos = hlist_entry_safe(n, typeof(*pos), member)) + + +#endif diff --git a/Platform/user/rpdb/rpdb.c b/Platform/user/rpdb/rpdb.c new file mode 100644 index 000000000..7230b1c03 --- /dev/null +++ b/Platform/user/rpdb/rpdb.c @@ -0,0 +1,468 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "list.h" + + + +struct rpdb_mark { + struct list_head list; + union { + struct in6_addr ip6; + struct in_addr ip4; + + }ip; + + int mark; +}; + + + + +char rt_table[11][128]= { + "# reserved values", + "#", + "255 local", + "254 main", + "253 default", + "0 unspe", + "#", + "# local", + "#", + "#1 inr.ruhep" +}; + + +#define RT_TABLES_PATH ("/etc/iproute2/rt_tables") +#define IPV4_PATTERN "^([0-9]|[1-9][0-9]|1[0-9]{1,2}|2[0-4][0-9]|25[0-5]).([0-9]|[1-9][0-9]|1[0-9]{1,2}|2[0-4][0-9]|25[0-5]).([0-9]|[1-9][0-9]|1[0-9]{1,2}|2[0-4][0-9]|25[0-5]).([0-9]|[1-9][0-9]|1[0-9]{1,2}|2[0-4][0-9]|25[0-5])$" +#define IPV6_PATTERN "^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$" + +#define RPDP_MARK_HASH_DEPTH (1024) +static int gRpdbMark = 0; + +/* + * mark hash list,save mark + */ +struct list_head gRpdbMarkList[RPDP_MARK_HASH_DEPTH] = {0}; + + +/* + popen +*/ + + +void +print_result(FILE *fp) +{ + char buf[100]; + + if(!fp) { + return; + } + printf("\n>>>\n"); + memset(buf, 0, sizeof(buf)); + fgets(buf, sizeof(buf) - 1, fp) ; + + + printf("%s", buf); + + + printf("\n<<<\n"); +} + +int rpdb_popen(char *cmd) +{ + FILE *fp = NULL; + + + fp = NULL; + fp = popen(cmd, "r"); + if(!fp) + { + perror("popen"); + exit(EXIT_FAILURE); + } + print_result(fp); + pclose(fp); + sleep(1); + return 0; +} + +/* + * rpdb 初始化接口 +*/ +bool rpdb_init_route() +{ + FILE *fp = NULL; + int i = 0; + + system("echo > /etc/iproute2/rt_tables"); + fp = fopen(RT_TABLES_PATH,"w"); + if (fp) + { + for (i = 0; i < 11; i++) + { + fprintf(fp,"%s\n",rt_table[i]); + } + } + else + { + return false; + } + + fclose(fp); + + + /* + 初始化链表 + */ + for (i = 0; i < RPDP_MARK_HASH_DEPTH; i++) + { + INIT_LIST_HEAD(&gRpdbMarkList[i]); + } + + return true; +} + + + +static int gRpdbTableIndex = 1; /* 1 - 252 */ +#define RPDB_TABLE_ADD (0) +#define RPDB_TABLE_DEL (1) +bool rpdb_add_delete_table(const char* gateway,const char *tbl_name,int op) +{ + char table_name[128] = {0}; + char line_cnt[1024] = {0}; + FILE *fp = NULL; + FILE *fp_tmp = NULL; + bool ret = false; + int i = 0; + int cn_num = 0; + if (tbl_name == NULL) + { + memset(table_name,0,sizeof(table_name)); + sprintf(table_name,"table_%s",gateway); + } + else + { + if (strlen(tbl_name) < 1) /* 检查name的合法性*/ + { + memset(table_name,0,sizeof(table_name)); + sprintf(table_name,"%s",tbl_name); + } + else + { + return false; + } + } + + fp = fopen(RT_TABLES_PATH,"ra+"); + if (NULL == fp) + { + return false; + } + else + { + while(!feof(fp)) /* 遍历文件每一行,查找 路由表是否存在 */ + { + memset(line_cnt,0,1024); + fgets(line_cnt,1024,fp); + if(strstr(line_cnt,table_name)) + { + if (op == RPDB_TABLE_ADD) + { + fclose(fp); + return true; + } + } + } + } + + /* + 向文件最后一行添加 路由表项 + */ + if (op == RPDB_TABLE_ADD) + { + if (gRpdbTableIndex > 252) + { + return false; + } + + memset(line_cnt,0,1024); + sprintf(line_cnt,"%d %s",gRpdbTableIndex,table_name); + printf("RPDB_TABLE_ADD:%s\n",line_cnt); + fprintf(fp,"%s\n",line_cnt); + fclose(fp); + gRpdbTableIndex++; + return true; + } + + if (op == RPDB_TABLE_DEL) + { + fp = fopen(RT_TABLES_PATH,"r+"); + if (NULL == fp) + { + return false; + } + else + { + fp_tmp = fopen("/tmp/rt_tables","w"); + if (fp_tmp == NULL) return false; + while(!feof(fp)) /* 遍历文件每一行,查找 路由表是否存在 */ + { + memset(line_cnt,0,1024); + fgets(line_cnt,1024,fp); + if(!strstr(line_cnt,table_name)) + { + fprintf(fp_tmp,"%s",line_cnt); + } + + } + + fclose(fp); + fclose(fp_tmp); + system("cp -fr /tmp/rt_tables /etc/iproute2/rt_tables"); + } + + } + return true; +} + +/* + * 判断ip地址是否合法 + */ + +int rpdb_ipaddr_match(char *ip_addr,int flag) +{ + regex_t reg; + regmatch_t match[1]; + int retval = 0; + + if (flag == 1) + { + retval = regcomp(®, IPV4_PATTERN, REG_EXTENDED | REG_NEWLINE); + } + else + { + retval = regcomp(®, IPV6_PATTERN, REG_EXTENDED | REG_NEWLINE); + } + + retval = regexec(®,ip_addr, sizeof match / sizeof match[0], match, 0); + printf("%s is %s\n", ip_addr, retval == 0 ? "legal" : "illegal"); + regfree(®); + return retval; +} + + +int rpdb_ip_hash(struct rpdb_mark node) +{ + int ip_value = 0; + int hash = 0; + int i = 0; + if (node.ip.ip4.s_addr !=0 ) + { + ip_value = node.ip.ip4.s_addr; + } + else + { + for (i = 0; i < 4; ++i) + { + ip_value += node.ip.ip6.s6_addr32[i]; + } + } + hash = (ip_value&0xF) + ((ip_value >> 8)&0xF) + ((ip_value >> 16)&0xF) + ((ip_value >> 24)&0xF); + hash = hash&(1024-1); + return hash; +} + + +/* + * node比较 +*/ + +bool rpdb_mark_node_compare(struct rpdb_mark node1,struct rpdb_mark node2) +{ + if (node1.ip.ip4.s_addr == node2.ip.ip4.s_addr) + { + return true; + } + else + { + return false; + } +} + + +/* + * mark 查找,返回链上的node节点 + */ +struct rpdb_mark * rpdb_mark_search(struct rpdb_mark node) +{ + struct list_head * pList; + struct rpdb_mark *pNode; + int hash = rpdb_ip_hash(node); + + list_for_each(pList,&gRpdbMarkList[hash]) { + pNode = list_entry(pList,struct rpdb_mark,list); + printf("%s,%d,%d,%d\n",__FUNCTION__,__LINE__,pNode->ip.ip4.s_addr,pNode->mark); + if (rpdb_mark_node_compare(node,*pNode)){ + printf("%s,%d,mark = %d\n",__FUNCTION__,__LINE__,pNode->mark); + return pNode; + } + } + + return NULL; +} + +/* + * mark +*/ +int rpdb_mark_hash_add(struct rpdb_mark node) +{ + + struct rpdb_mark *pNode = NULL; + int hash = 0; + + pNode = rpdb_mark_search(node); + if (pNode != NULL) + { + return pNode->mark; + } + + pNode = (struct rpdb_mark*)malloc(sizeof(struct rpdb_mark)); + if (NULL == pNode) + { + return false; + } + + hash = rpdb_ip_hash(node); + node.mark = ++gRpdbMark; + memcpy((char*)pNode,(char*)&node,sizeof(struct rpdb_mark)); + + printf("%s,%d,%d,%d\n",__FUNCTION__,__LINE__,pNode->ip.ip4.s_addr,pNode->mark); + /* + 节点添加到链尾部 + */ + list_add_tail(&pNode->list,&gRpdbMarkList[hash]); + + return gRpdbMark; +} + + +/* + * 根据下一跳地址生成mark +*/ +int rpdb_gen_mark(const char* gateway) +{ + in_addr_t addr; + if (NULL == gateway) + { + return -1; + } + + addr = inet_addr(gateway); + struct rpdb_mark node; + node.ip.ip4.s_addr = addr; + return rpdb_mark_hash_add(node); +} + +/* + * 路由添加接口 +*/ +bool rpdb_add_route(const char* gateway,const char *tbl_name) +{ + char cmd[256] = {0}; + int status = 0; + char table_name[128] = {0}; + int mark = 0; + if (tbl_name == NULL) + { + memset(table_name,0,sizeof(table_name)); + sprintf(table_name,"table_%s",gateway); + } + else + { + if (strlen(tbl_name) < 1) /* 检查name的合法性*/ + { + memset(table_name,0,sizeof(table_name)); + sprintf(table_name,"%s",tbl_name); + } + else + { + return false; + } + } + + if (rpdb_add_delete_table(gateway,tbl_name,RPDB_TABLE_ADD) == false) + { + return false; + } + + if (strstr(":",gateway)) + { + sprintf(cmd,"ip -6 route add default via %s table %s",gateway,table_name); + } + else + { + sprintf(cmd,"ip route add default via %s table %s",gateway,table_name); + } + + printf("%s\n",cmd); + rpdb_popen(cmd); + + mark = rpdb_gen_mark(gateway); + memset(cmd,0,sizeof(cmd)); + sprintf(cmd,"ip rule add fwmark %d table %s",mark,table_name); + printf("%s\n",cmd); + rpdb_popen(cmd); + + return 0; +} + + +int main() +{ + //ipaddr_match("192.168.1.1",1); + + //ipaddr_match("192.168.1.300",1); + + //ipaddr_match("2000:0:0:0:0:0:0:1 ",2); + //ipaddr_match("fe80:0000:0000:0000:0204:61ff:fe9d:ffffff15",2); + struct rpdb_mark mark_value; + struct rpdb_mark mark_value1; + in_addr_t addr; + rpdb_init_route(); + rpdb_add_delete_table("1.1.2.1",NULL,RPDB_TABLE_ADD); + rpdb_add_delete_table("1.1.2.1",NULL,RPDB_TABLE_ADD); + rpdb_add_delete_table("1.2.2.1",NULL,RPDB_TABLE_ADD); + rpdb_add_delete_table("2.1.2.1",NULL,RPDB_TABLE_ADD); + + rpdb_add_delete_table("2.1.2.1",NULL,RPDB_TABLE_DEL); + //rpdb_add_delete_table("2.1.2.1",NULL,RPDB_TABLE_DEL); + //rpdb_add_delete_table("1.1.2.1",NULL,RPDB_TABLE_DEL); + addr = inet_addr("2006.6.6.6"); + mark_value.ip.ip4.s_addr = addr; + rpdb_mark_hash_add(mark_value); + rpdb_mark_hash_add(mark_value); + rpdb_mark_hash_add(mark_value); + rpdb_mark_hash_add(mark_value); + + addr = inet_addr("7.7.7.7"); + mark_value1.ip.ip4.s_addr = addr; + rpdb_mark_hash_add(mark_value1); + rpdb_mark_hash_add(mark_value1); + + + + rpdb_add_route("1.2.3.3",NULL); + rpdb_add_route("2.2.4.3",NULL); + rpdb_add_route("3.2.4.3",NULL); + rpdb_add_route("4.2.4.3",NULL); + + return 0; +} diff --git a/Platform/user/ulog/log-sched/log_console.c b/Platform/user/ulog/log-sched/log_console.c index 7ef36bfdd..17046c467 100755 --- a/Platform/user/ulog/log-sched/log_console.c +++ b/Platform/user/ulog/log-sched/log_console.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -9,46 +10,60 @@ #include "sev_sched.h" #define LOG_CONF_COSOLE_FILE_NAME "log-console.conf" - #define LOG_REDIRECT_CONSOLE "/dev/console" +#define PROC_SERIAL_INFO_PATH "/proc/tty/driver/serial" + +#define SERIAL_DRIVER_PREFIX "uart" +#define SERIAL_NO_DRIVER_KEY SERIAL_DRIVER_PREFIX":unknown" +#define SERIAL_PREFIX "ttyS" static int write_console_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) { - DIR *dir; - - if ((dir = opendir(LOG_DEV_DIR)) == NULL) { - ULOG_ERR(g_log, "Open dir:[%s] is failure:%d", LOG_DEV_DIR, strerror(errno)); - return -1; + int ret = -1; + FILE *driver_fp = NULL; + driver_fp = fopen(PROC_SERIAL_INFO_PATH, "r"); + if (driver_fp == NULL) { + ULOG_ERR(g_log, "Opening file:%s is failure:%s", PROC_SERIAL_INFO_PATH, strerror(errno)); + return ret; } - struct dirent *ptr; + char *line = NULL; + size_t n; + int num; char path[MAX_PATH_SZ]; - while ((ptr = readdir(dir)) != NULL) { - if ((strcmp(ptr->d_name, ".") == 0) - || (strcmp(ptr->d_name, "..") == 0) - || (ptr->d_type == DT_DIR)) { ///current dir OR parrent dir - ULOG_DEBUG(g_log,"The file:[%s] or directory jump over", ptr->d_name); - continue; - } - - if ((strstr(ptr->d_name, "ttyS") == NULL)) { - ULOG_DEBUG(g_log,"The file:[%s] isn't redirected", ptr->d_name); + while ((n = getline(&line, &n, driver_fp)) != -1) { + ULOG_DEBUG(g_log, "Serial line:%s", line); + if (strstr(line, SERIAL_DRIVER_PREFIX) == NULL) { + ULOG_DEBUG(g_log, "%s isn't driver line", line); continue; } + + num = atoi(line); + if (strstr(line, SERIAL_NO_DRIVER_KEY) != NULL) { + ULOG_DEBUG(g_log, "%s%d don't have serial", SERIAL_PREFIX, num); + continue; + } - ULOG_DEBUG(g_log, "ttyS name:%s", ptr->d_name); - if (snprintf(path, sizeof(path), "%s%s", LOG_DEV_DIR, ptr->d_name) < 0) { - ULOG_ERR(g_log, "Setting %s of log console is failure", ptr->d_name); - return -1; + + if (snprintf(path, sizeof(path), "%s%s%d", LOG_DEV_DIR, SERIAL_PREFIX, num) < 0) { + ULOG_ERR(g_log, "Setting %s%d of log console is failure", SERIAL_PREFIX, num); + goto END; } + ULOG_DEBUG(g_log, "ttyS name:%s", path); if (write_conf_content_authorizing(fp, level, filter_mod, path) != 0) { ULOG_ERR(g_log, "Writing tty[module:%s] of log is failure", filter_mod); - return -1; + goto END; } - - } + } + + ret = 0; +END: + if (line != NULL) { + free(line); + } - return 0; + fclose(driver_fp); + return ret; } diff --git a/Platform/user/ulog/log-sched/log_sched.c b/Platform/user/ulog/log-sched/log_sched.c index f34fda764..6c80c89e1 100755 --- a/Platform/user/ulog/log-sched/log_sched.c +++ b/Platform/user/ulog/log-sched/log_sched.c @@ -47,7 +47,7 @@ int main(int argc, char **argv) g_log = ulog_init(LOG_SCHED_MODULE_NAME, !run_daemon); if (g_log == NULL) { - fprintf(stderr, "Initiating ulog is failure"); + fprintf(stderr, "Initiating ulog is failure\n"); return -1; } diff --git a/Platform/user/ulog/ulog-api/ulog_api.c b/Platform/user/ulog/ulog-api/ulog_api.c index 4cdd75a31..5c84de244 100755 --- a/Platform/user/ulog/ulog-api/ulog_api.c +++ b/Platform/user/ulog/ulog-api/ulog_api.c @@ -13,19 +13,27 @@ ulog_t *ulog_init(const char *module_name, u8 is_print) { ulog_t *log; - u32 len = strlen(module_name); - + u32 len = 0; + + if (NULL == module_name) { + fprintf(stderr, "Bad input: module_name is NULL\n"); + return NULL; + } + + len = strlen(module_name); + if (len > MAX_MODULE_NAME_SZ) { - fprintf(stderr, "The length:%d of module_name can't more than %d", len, MAX_MODULE_NAME_SZ); + fprintf(stderr, "The length:%d of module_name can't more than %d\n", len, MAX_MODULE_NAME_SZ); return NULL; } log = (ulog_t *)malloc(sizeof(*log)); if (log == NULL) { - fprintf(stderr, "Allocating log memory is failure"); + fprintf(stderr, "Allocating log memory is failure\n"); return NULL; } strncpy(log->module_name, module_name, len); + log->module_name[len] = '\0'; int opt = LOG_PERROR |LOG_PID; if (is_print > 0) { @@ -47,7 +55,7 @@ void ulog_close(ulog_t *log) void ulog_record(const ulog_t *log, int level, const char *fmt, ...) { if (log == NULL) { - fprintf(stderr, "Log is null"); + fprintf(stderr, "Log is null\n"); return; } diff --git a/Platform/user/ulog/ulog-test/ulog_test.c b/Platform/user/ulog/ulog-test/ulog_test.c new file mode 100644 index 000000000..15cda4b88 --- /dev/null +++ b/Platform/user/ulog/ulog-test/ulog_test.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include + +#include "ulog_api.h" +#include "ulog_in.h" + + +ulog_t *g_log = NULL; + + +static void test_ulog_api() +{ + ULOG_DEBUG (g_log, "test for ulog_api %s", "debug"); + ULOG_INFO (g_log, "test for ulog_api %s", "info"); + ULOG_NOTICE (g_log, "test for ulog_api %s", "notice"); + ULOG_WARNING(g_log, "test for ulog_api %s", "warning"); + ULOG_ERR (g_log, "test for ulog_api %s", "err"); + ULOG_CRIT (g_log, "test for ulog_api %s", "crit"); + ULOG_ALERT (g_log, "test for ulog_api %s", "alert"); + ULOG_EMERG (g_log, "test for ulog_api %s", "emerg"); +} + +static void ulog_test_usage(const char *pname) +{ + if (NULL == pname) + { + return; + } + + fprintf(stderr, "--------------------------------------------------------\n"); + fprintf(stderr, " usage of %s:\n", pname); + fprintf(stderr, " %s [-d] [-a module_name] [-h]\n", pname); + fprintf(stderr, " \n", pname); + fprintf(stderr, " -d: daemon\n", pname); + fprintf(stderr, " -a: set module name, no longer than 16 bytes\n"); + fprintf(stderr, " -h: help\n"); + fprintf(stderr, "--------------------------------------------------------\n"); +} + +/* usage: + test_ulog_api + test_ulog_api -a test_ulog_api + test_ulog_api -a test2 + test_ulog_api -a 123456789012345 + test_ulog_api -a 1234567890123456 + test_ulog_api -a 12345678901234567 + + test_ulog_api -d + test_ulog_api -d -a test_ulog_api + test_ulog_api -d -a test2 + test_ulog_api -d -a 123456789012345 + test_ulog_api -d -a 1234567890123456 + test_ulog_api -d -a 12345678901234567 +*/ +int main(int argc, char **argv) +{ + char *options = "da:h"; + int opt; + u8 run_daemon = 0; + char module_name[MAX_MODULE_NAME_SZ+4] = ""; + + while ((opt = getopt(argc, argv, options)) != -1) { + switch (opt) { + case 'd': + run_daemon = 1; + break; + case 'a': + memset(module_name, 0, MAX_MODULE_NAME_SZ+4); + strncpy(module_name, optarg, MAX_MODULE_NAME_SZ+3); + break; + case 'h': + ulog_test_usage(argv[0]); + return 0; + } + } + + g_log = ulog_init(module_name, run_daemon); + if (NULL == g_log) + { + goto END; + } + + if (run_daemon) { + if (daemon(0, 0) == -1) { + ULOG_ERR(g_log, "Setting daemon running is failure:%s", strerror(errno)); + goto END; + } + } + + test_ulog_api(); + +END: + if (NULL != g_log) + { + ulog_close(g_log); + } + return 0; +} diff --git a/libs/src/lighttpd-1.4.51/src/mod_webm.c b/libs/src/lighttpd-1.4.51/src/mod_webm.c index e62a09694..26017deb8 100644 --- a/libs/src/lighttpd-1.4.51/src/mod_webm.c +++ b/libs/src/lighttpd-1.4.51/src/mod_webm.c @@ -23,10 +23,10 @@ typedef enum { WEBM_HANDLE_INVALID_INDEX = -1, WEBM_HANDLE_LOGIN, WEBM_HANDLE_HOMEPAGE, - /* ������ҳ��ȡ��չʾ��case�������������ܿ���ͨ�����ݿ��ѯ�������ȷŵ�һ�� */ + /* ҳȡչʾcaseܿͨݿѯȷŵһ */ WEBM_HANDLE_CONFIG_LIST_GROUP, - /* ���������ò�����ص�case */ + /* òصcase */ WEBM_HANDLE_CONFIG_ADD_GROUP, WEBM_HANDLE_CONFIG_UUID_GROUP, WEBM_HANDLE_CONFIG_MOD_GROUP, @@ -40,7 +40,12 @@ typedef enum { WEBM_HANDLE_INVALID_INDEX = -1, WEBM_HANDLE_CONFIG_IPV4, WEBM_HANDLE_CONFIG_VLAN_SET, WEBM_HANDLE_CONFIG_VLAN_GET, - WEBM_HANDLE_MAX + WEBM_HANDLE_MAX, + + WEBM_HANDLE_CONFIG_MOD_AUTHPARA, + WEBM_HANDLE_CONFIG_ADD_AUTHRULE, + WEBM_HANDLE_CONFIG_MOD_AUTHRULE, + WEBM_HANDLE_CONFIG_DEL_AUTHRULE } webm_handle_index; typedef struct _webm_config_st @@ -185,6 +190,37 @@ extern int webm_config_send_proc(server *srv, uint32_t config_type, uint64 confg CM_CONFIG_GET, \ VLAN_CONFIG, \ webm_config_send_proc \ + }, \ + {\ + WEBM_HANDLE_CONFIG_MOD_AUTHPARA, \ + "/FSG-CF/userauth-parameters-mod", \ + CM_CONFIG_SET, \ + FREEPARAMETERS_CONFIG , \ + webm_config_send_proc \ + }, \ +\ + {\ + WEBM_HANDLE_CONFIG_ADD_AUTHRULE, \ + "/FSG-GF/userauth-rule-add", \ + CM_CONFIG_SET, \ + AUTHFREE_CONFIG, \ + webm_config_send_proc \ + }, \ +\ + {\ + WEBM_HANDLE_CONFIG_MOD_AUTHRULE, \ + "/FSG-GF/userauth-rule-mod", \ + CM_CONFIG_SET, \ + AUTHFREE_CONFIG, \ + webm_config_send_proc \ + }, \ +\ + {\ + WEBM_HANDLE_CONFIG_DEL_AUTHRULE, \ + "/FSG-GF/userauth-rule-del", \ + CM_CONFIG_SET, \ + AUTHFREE_CONFIG, \ + webm_config_send_proc \ } \ \ } diff --git a/patchs/cJSON/0001-aarch64-cJSON-cmake-cross-compile.patch b/patchs/cJSON/0001-aarch64-cJSON-cmake-cross-compile.patch new file mode 100644 index 000000000..38eb2e45a --- /dev/null +++ b/patchs/cJSON/0001-aarch64-cJSON-cmake-cross-compile.patch @@ -0,0 +1,37 @@ +diff -uprN -x build -x comp_sql cJSON_orig/CMakeLists.txt cJSON/CMakeLists.txt +--- cJSON_orig/CMakeLists.txt 2019-08-28 11:06:20.851852257 +0800 ++++ cJSON/CMakeLists.txt 2019-08-28 11:06:52.346617001 +0800 +@@ -12,6 +12,33 @@ set(CJSON_VERSION_SO 1) + set(CJSON_UTILS_VERSION_SO 1) + set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") + ++if(PLAT_ARM64) ++ # this one is important ++ SET(CMAKE_SYSTEM_NAME Linux) ++ #this one not so much ++ SET(CMAKE_SYSTEM_VERSION 1) ++ # this is install root directory ++ SET(CMAKE_INSTALL_PREFIX /usr) ++ ++ # specify the cross compiler ++ SET(CMAKE_C_COMPILER aarch64-linux-gnu-gcc) ++ SET(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++) ++ SET(CMAKE_STRIP aarch64-linux-gnu-strip) ++ ++ # specify the cross compile and link flags ++ set(CMAKE_C_FLAGS "--sysroot=$ENV{SDKTARGETSYSROOT}") ++ set(CMAKE_SHARED_LINKER_FLAGS "--sysroot=$ENV{SDKTARGETSYSROOT}") ++ SET(CMAKE_LIBRARY_PATH $ENV{SDKTARGETSYSROOT}/usr/lib) ++ ++ # where is the target environment ++ SET(CMAKE_FIND_ROOT_PATH ${SDKTARGETSYSROOT}) ++ ++ # search for programs in the build host directories ++ SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) ++ # for libraries and headers in the target directories ++ SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) ++ SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) ++endif() + + set(custom_compiler_flags) + diff --git a/patchs/cJSON/CMakeLists.txt b/patchs/cJSON/CMakeLists.txt deleted file mode 100644 index aeceab5e4..000000000 --- a/patchs/cJSON/CMakeLists.txt +++ /dev/null @@ -1,278 +0,0 @@ -set(CMAKE_LEGACY_CYGWIN_WIN32 0) -cmake_minimum_required(VERSION 2.8.5) - -project(cJSON C) - -include(GNUInstallDirs) - -set(PROJECT_VERSION_MAJOR 1) -set(PROJECT_VERSION_MINOR 7) -set(PROJECT_VERSION_PATCH 12) -set(CJSON_VERSION_SO 1) -set(CJSON_UTILS_VERSION_SO 1) -set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") - -if(PLAT_ARM64) - # this one is important - SET(CMAKE_SYSTEM_NAME Linux) - #this one not so much - SET(CMAKE_SYSTEM_VERSION 1) - # this is install root directory - SET(CMAKE_INSTALL_PREFIX /usr) - - # specify the cross compiler - SET(CMAKE_C_COMPILER aarch64-linux-gnu-gcc) - SET(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++) - SET(CMAKE_STRIP aarch64-linux-gnu-strip) - - # specify the cross compile and link flags - set(CMAKE_C_FLAGS "--sysroot=$ENV{SDKTARGETSYSROOT}") - set(CMAKE_SHARED_LINKER_FLAGS "--sysroot=$ENV{SDKTARGETSYSROOT}") - SET(CMAKE_LIBRARY_PATH $ENV{SDKTARGETSYSROOT}/usr/lib) - - # where is the target environment - SET(CMAKE_FIND_ROOT_PATH ${SDKTARGETSYSROOT}) - - # search for programs in the build host directories - SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) - # for libraries and headers in the target directories - SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) - SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) -endif() - -set(custom_compiler_flags) - -include(CheckCCompilerFlag) -option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags" ON) -if (ENABLE_CUSTOM_COMPILER_FLAGS) - if (("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") OR ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")) - list(APPEND custom_compiler_flags - -std=c89 - -pedantic - -Wall - -Wextra - -Werror - -Wstrict-prototypes - -Wwrite-strings - -Wshadow - -Winit-self - -Wcast-align - -Wformat=2 - -Wmissing-prototypes - -Wstrict-overflow=2 - -Wcast-qual - -Wundef - -Wswitch-default - -Wconversion - -Wc++-compat - -fstack-protector-strong - -Wcomma - -Wdouble-promotion - -Wparentheses - -Wformat-overflow - -Wunused-macros - -Wmissing-variable-declarations - -Wused-but-marked-unused - -Wswitch-enum - ) - elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") - # Disable warning c4001 - nonstandard extension 'single line comment' was used - # Define _CRT_SECURE_NO_WARNINGS to disable deprecation warnings for "insecure" C library functions - list(APPEND custom_compiler_flags - /GS - /Za - /sdl - /W4 - /wd4001 - /D_CRT_SECURE_NO_WARNINGS - ) - endif() -endif() - -option(ENABLE_SANITIZERS "Enables AddressSanitizer and UndefinedBehaviorSanitizer." OFF) -if (ENABLE_SANITIZERS) - list(APPEND custom_compiler_flags - -fno-omit-frame-pointer - -fsanitize=address - -fsanitize=undefined - -fsanitize=float-divide-by-zero - -fsanitize=float-cast-overflow - -fsanitize-address-use-after-scope - -fsanitize=integer - -01 - -fno-sanitize-recover - ) -endif() - -option(ENABLE_SAFE_STACK "Enables the SafeStack instrumentation pass by the Code Pointer Integrity Project" OFF) -if (ENABLE_SAFE_STACK) - if (ENABLE_SANITIZERS) - message(FATAL_ERROR "ENABLE_SAFE_STACK cannot be used in combination with ENABLE_SANITIZERS") - endif() - list(APPEND custom_compiler_flags - -fsanitize=safe-stack - ) -endif() - -option(ENABLE_PUBLIC_SYMBOLS "Export library symbols." On) -if (ENABLE_PUBLIC_SYMBOLS) - list(APPEND custom_compiler_flags -fvisibility=hidden) - add_definitions(-DCJSON_EXPORT_SYMBOLS -DCJSON_API_VISIBILITY) -endif() -option(ENABLE_HIDDEN_SYMBOLS "Hide library symbols." Off) -if (ENABLE_HIDDEN_SYMBOLS) - add_definitions(-DCJSON_HIDE_SYMBOLS -UCJSON_API_VISIBILITY) -endif() - -# apply custom compiler flags -foreach(compiler_flag ${custom_compiler_flags}) - #remove problematic characters - string(REGEX REPLACE "[^a-zA-Z0-9]" "" current_variable ${compiler_flag}) - - CHECK_C_COMPILER_FLAG(${compiler_flag} "FLAG_SUPPORTED_${current_variable}") - if (FLAG_SUPPORTED_${current_variable}) - list(APPEND supported_compiler_flags) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${compiler_flag}") - endif() -endforeach() - -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${supported_compiler_flags}") - -option(BUILD_SHARED_LIBS "Build shared libraries" ON) -option(ENABLE_TARGET_EXPORT "Enable exporting of CMake targets. Disable when it causes problems!" ON) - -#cJSON -set(CJSON_LIB cjson) - -file(GLOB HEADERS cJSON.h) -set(SOURCES cJSON.c) - -option(BUILD_SHARED_AND_STATIC_LIBS "Build both shared and static libraries" Off) -option(CJSON_OVERRIDE_BUILD_SHARED_LIBS "Override BUILD_SHARED_LIBS with CJSON_BUILD_SHARED_LIBS" OFF) -option(CJSON_BUILD_SHARED_LIBS "Overrides BUILD_SHARED_LIBS if CJSON_OVERRIDE_BUILD_SHARED_LIBS is enabled" ON) - -if ((CJSON_OVERRIDE_BUILD_SHARED_LIBS AND CJSON_BUILD_SHARED_LIBS) OR ((NOT CJSON_OVERRIDE_BUILD_SHARED_LIBS) AND BUILD_SHARED_LIBS)) - set(CJSON_LIBRARY_TYPE SHARED) -else() - set(CJSON_LIBRARY_TYPE STATIC) -endif() - - -if (NOT BUILD_SHARED_AND_STATIC_LIBS) - add_library("${CJSON_LIB}" "${CJSON_LIBRARY_TYPE}" "${HEADERS}" "${SOURCES}") -else() - # See https://cmake.org/Wiki/CMake_FAQ#How_do_I_make_my_shared_and_static_libraries_have_the_same_root_name.2C_but_different_suffixes.3F - add_library("${CJSON_LIB}" SHARED "${HEADERS}" "${SOURCES}") - add_library("${CJSON_LIB}-static" STATIC "${HEADERS}" "${SOURCES}") - set_target_properties("${CJSON_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_LIB}") - set_target_properties("${CJSON_LIB}-static" PROPERTIES PREFIX "lib") -endif() -if (NOT WIN32) - target_link_libraries("${CJSON_LIB}" m) -endif() - -configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson.pc.in" - "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY) - -install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson") -install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig") -install(TARGETS "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" EXPORT "${CJSON_LIB}") -if (BUILD_SHARED_AND_STATIC_LIBS) - install(TARGETS "${CJSON_LIB}-static" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}") -endif() -if(ENABLE_TARGET_EXPORT) - # export library information for CMake projects - install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON") -endif() - -set_target_properties("${CJSON_LIB}" - PROPERTIES - SOVERSION "${CJSON_VERSION_SO}" - VERSION "${PROJECT_VERSION}") - -#cJSON_Utils -option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF) -if(ENABLE_CJSON_UTILS) - set(CJSON_UTILS_LIB cjson_utils) - - file(GLOB HEADERS_UTILS cJSON_Utils.h) - set(SOURCES_UTILS cJSON_Utils.c) - - if (NOT BUILD_SHARED_AND_STATIC_LIBS) - add_library("${CJSON_UTILS_LIB}" "${CJSON_LIBRARY_TYPE}" "${HEADERS_UTILS}" "${SOURCES_UTILS}") - target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}") - else() - add_library("${CJSON_UTILS_LIB}" SHARED "${HEADERS_UTILS}" "${SOURCES_UTILS}") - target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}") - add_library("${CJSON_UTILS_LIB}-static" STATIC "${HEADERS_UTILS}" "${SOURCES_UTILS}") - target_link_libraries("${CJSON_UTILS_LIB}-static" "${CJSON_LIB}-static") - set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_UTILS_LIB}") - set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES PREFIX "lib") - endif() - - configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson_utils.pc.in" - "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY) - - install(TARGETS "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" EXPORT "${CJSON_UTILS_LIB}") - if (BUILD_SHARED_AND_STATIC_LIBS) - install(TARGETS "${CJSON_UTILS_LIB}-static" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}") - endif() - install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson") - install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig") - if(ENABLE_TARGET_EXPORT) - # export library information for CMake projects - install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON") - endif() - - set_target_properties("${CJSON_UTILS_LIB}" - PROPERTIES - SOVERSION "${CJSON_UTILS_VERSION_SO}" - VERSION "${PROJECT_VERSION}") -endif() - -# create the other package config files -configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfig.cmake.in" - ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY) -configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfigVersion.cmake.in" - ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY) - -# Install package config files -install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake - ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake - DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON") - -option(ENABLE_CJSON_TEST "Enable building cJSON test" ON) -if(ENABLE_CJSON_TEST) - enable_testing() - - set(TEST_CJSON cJSON_test) - add_executable("${TEST_CJSON}" test.c) - target_link_libraries("${TEST_CJSON}" "${CJSON_LIB}") - - add_test(NAME ${TEST_CJSON} COMMAND "${CMAKE_CURRENT_BINARY_DIR}/${TEST_CJSON}") - - # Disable -fsanitize=float-divide-by-zero for cJSON_test - if (FLAG_SUPPORTED_fsanitizefloatdividebyzero) - if ("${CMAKE_VERSION}" VERSION_LESS "2.8.12") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize=float-divide-by-zero") - else() - target_compile_options(${TEST_CJSON} PRIVATE "-fno-sanitize=float-divide-by-zero") - endif() - endif() - - #"check" target that automatically builds everything and runs the tests - add_custom_target(check - COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure - DEPENDS ${TEST_CJSON}) -endif() - -# Enable the use of locales -option(ENABLE_LOCALES "Enable the use of locales" ON) -if(ENABLE_LOCALES) - add_definitions(-DENABLE_LOCALES) -endif() - -add_subdirectory(tests) -add_subdirectory(fuzzing) diff --git a/patchs/mysql-5.3.27/0001-aarch64-mysql-5.7.27-cmake-cross-compile-and-fix-compile-error.patch b/patchs/mysql-5.3.27/0001-aarch64-mysql-5.7.27-cmake-cross-compile-and-fix-compile-error.patch new file mode 100644 index 000000000..bd2fd3022 --- /dev/null +++ b/patchs/mysql-5.3.27/0001-aarch64-mysql-5.7.27-cmake-cross-compile-and-fix-compile-error.patch @@ -0,0 +1,70 @@ +diff -uprN -x build -x comp_sql mysql-5.7.27_orig/CMakeLists.txt mysql-5.7.27/CMakeLists.txt +--- mysql-5.7.27_orig/CMakeLists.txt 2019-06-10 22:43:20.000000000 +0800 ++++ mysql-5.7.27/CMakeLists.txt 2019-08-28 09:08:51.947233400 +0800 +@@ -43,7 +43,33 @@ IF(CMAKE_VERSION VERSION_EQUAL "3.0.0" O + ENDIF() + + MESSAGE(STATUS "Running cmake version ${CMAKE_VERSION}") ++if(PLAT_ARM64) ++ # this one is important ++ SET(CMAKE_SYSTEM_NAME Linux) ++ #this one not so much ++ SET(CMAKE_SYSTEM_VERSION 1) ++ # this is install root directory ++ SET(CMAKE_INSTALL_PREFIX /usr) ++ ++ # specify the cross compiler ++ SET(CMAKE_C_COMPILER aarch64-linux-gnu-gcc) ++ SET(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++) ++ SET(CMAKE_STRIP aarch64-fsl-linux-strip) ++ ++ # specify the cross compile and link flags ++ set(CMAKE_C_FLAGS "--sysroot=$ENV{SDKTARGETSYSROOT}") ++ set(CMAKE_SHARED_LINKER_FLAGS "--sysroot=$ENV{SDKTARGETSYSROOT}") ++ SET(CMAKE_LIBRARY_PATH $ENV{SDKTARGETSYSROOT}/usr/lib) + ++ # where is the target environment ++ SET(CMAKE_FIND_ROOT_PATH ${SDKTARGETSYSROOT}) ++ ++ # search for programs in the build host directories ++ SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) ++ # for libraries and headers in the target directories ++ SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) ++ SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) ++endif() + # Will set GIT_EXECUTABLE and GIT_FOUND + FIND_PACKAGE(Git) + +diff -uprN -x build -x comp_sql mysql-5.7.27_orig/storage/innobase/lock/lock0lock.cc mysql-5.7.27/storage/innobase/lock/lock0lock.cc +--- mysql-5.7.27_orig/storage/innobase/lock/lock0lock.cc 2019-06-10 22:43:20.000000000 +0800 ++++ mysql-5.7.27/storage/innobase/lock/lock0lock.cc 2019-08-28 09:29:44.927757019 +0800 +@@ -1647,7 +1647,7 @@ RecLock::mark_trx_for_rollback(trx_t* tr + bool cas; + os_thread_id_t thread_id = os_thread_get_curr_id(); + +- cas = os_compare_and_swap_thread_id(&trx->killed_by, 0, thread_id); ++ cas = os_compare_and_swap_lint(&trx->killed_by, 0, thread_id); + + ut_a(cas); + +diff -uprN -x build -x comp_sql mysql-5.7.27_orig/storage/innobase/trx/trx0trx.cc mysql-5.7.27/storage/innobase/trx/trx0trx.cc +--- mysql-5.7.27_orig/storage/innobase/trx/trx0trx.cc 2019-06-10 22:43:20.000000000 +0800 ++++ mysql-5.7.27/storage/innobase/trx/trx0trx.cc 2019-08-28 09:33:18.500084963 +0800 +@@ -193,7 +193,7 @@ trx_init( + if (!TrxInInnoDB::is_async_rollback(trx)) { + + os_thread_id_t thread_id = trx->killed_by; +- os_compare_and_swap_thread_id(&trx->killed_by, thread_id, 0); ++ os_compare_and_swap_uint32(&trx->killed_by, thread_id, 0); + + /* Note: Do not set to 0, the ref count is decremented inside + the TrxInInnoDB() destructor. We only need to clear the flags. */ +@@ -3381,7 +3381,7 @@ trx_kill_blocking(trx_t* trx) + ut_ad(victim_trx->version == version); + + os_thread_id_t thread_id = victim_trx->killed_by; +- os_compare_and_swap_thread_id(&victim_trx->killed_by, ++ os_compare_and_swap_uint32(&victim_trx->killed_by, + thread_id, 0); + + victim_trx->in_innodb &= TRX_FORCE_ROLLBACK_MASK; diff --git a/patchs/mysql-5.3.27/cmake_cmd.txt b/patchs/mysql-5.3.27/cmake_cmd.txt new file mode 100644 index 000000000..6e63c32b3 --- /dev/null +++ b/patchs/mysql-5.3.27/cmake_cmd.txt @@ -0,0 +1,3 @@ + cmake -G "Unix Makefiles" -DPLAT_ARM64=TRUE ../ -DWITH_BOOST=/home/hx/my_projects/boost_1_59_0 \ + -DSTACK_DIRECTION=1 -DHAVE_IB_GCC_ATOMIC_COMPARE_EXCHANGE=TRUE -DWITH_UNIXODBC=TRUE \ + -DWITH_EMBEDDED_SERVER=TRUE -DCMAKE_INSTALL_PREFIX=/usr -DWITH_DEBUG=FALSE -DWITH_UNIT_TESTS=FALSE \ No newline at end of file diff --git a/patchs/mysql-connector-5.3.13/0001-aarch64-cmake-cross-compile.patch b/patchs/mysql-connector-5.3.13/0001-aarch64-cmake-cross-compile.patch new file mode 100644 index 000000000..c1f45a793 --- /dev/null +++ b/patchs/mysql-connector-5.3.13/0001-aarch64-cmake-cross-compile.patch @@ -0,0 +1,36 @@ +--- mysql-connector-odbc-5.3.13-src/CMakeLists.txt 2019-04-16 00:56:34.000000000 +0800 ++++ mysql-connector-odbc-5.3.13-src_arm/CMakeLists.txt 2019-08-28 11:42:59.514993586 +0800 +@@ -56,6 +56,33 @@ endmacro() + endif() + + #----------------------------------------------------- ++if(PLAT_ARM64) ++ # this one is important ++ SET(CMAKE_SYSTEM_NAME Linux) ++ #this one not so much ++ SET(CMAKE_SYSTEM_VERSION 1) ++ # this is install root directory ++ SET(CMAKE_INSTALL_PREFIX /usr) ++ ++ # specify the cross compiler ++ SET(CMAKE_C_COMPILER aarch64-linux-gnu-gcc) ++ SET(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++) ++ SET(CMAKE_STRIP aarch64-fsl-linux-strip) ++ ++ # specify the cross compile and link flags ++ set(CMAKE_C_FLAGS "--sysroot=$ENV{SDKTARGETSYSROOT}") ++ set(CMAKE_SHARED_LINKER_FLAGS "--sysroot=$ENV{SDKTARGETSYSROOT}") ++ SET(CMAKE_LIBRARY_PATH $ENV{SDKTARGETSYSROOT}/usr/lib) ++ ++ # where is the target environment ++ SET(CMAKE_FIND_ROOT_PATH ${SDKTARGETSYSROOT}) ++ ++ # search for programs in the build host directories ++ SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) ++ # for libraries and headers in the target directories ++ SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) ++ SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) ++endif() + + FIND_PACKAGE(Threads) + diff --git a/patchs/mysql-connector-5.3.13/cmake_cmd.txt b/patchs/mysql-connector-5.3.13/cmake_cmd.txt new file mode 100644 index 000000000..8c9a9846d --- /dev/null +++ b/patchs/mysql-connector-5.3.13/cmake_cmd.txt @@ -0,0 +1,3 @@ +cmake -G "Unix Makefiles" -DPLAT_ARM64=TRUE ../ -DWITH_UNIXODBC=TRUE -DSIZEOF_INT=4 \ + -DHAVE_POSIX_TIMERS=TRUE -DHAVE_STRUCT_TIMESPEC=TRUE -DSIZEOF_CHARP=4 \ + -DHAVE_PWD_H=TRUE -DHAVE_GETRUSAGE=FALSE -DDISABLE_GUI=TRUE \ No newline at end of file