diff --git a/Makefile b/Makefile index 78ca69a59..9374f0554 100755 --- a/Makefile +++ b/Makefile @@ -28,9 +28,9 @@ MAKE_FLAGS += -j$(shell cat /proc/cpuinfo | grep processor | wc -l) endif endif -.PHONY : demo conntrack netlink trace openrpc +.PHONY : demo conntrack netlink trace openrpc configm -all: demo conntrack netlink trace openrpc +all: demo conntrack netlink trace openrpc configm ifeq ($(OPT), install) #$(shell `find ../release -name "*.zip" -delete`) @@ -120,3 +120,17 @@ else $(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.openrpc.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=openrpc endif +configm: +ifeq ($(OPT), clean) + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.configm.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=configm + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.configmapi.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=configm-api + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.configmtest.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=configmtest +else ifeq ($(OPT), install) + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.configm.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=configm + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.configmapi.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=configm-api + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.configmtest.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=configmtest +else + $(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.configm.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=configm + $(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.configmapi.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=configm-api + $(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.configmtest.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=configmtest +endif diff --git a/Platform/build/user.configm.Makefile b/Platform/build/user.configm.Makefile index e04abb356..e235e544c 100755 --- a/Platform/build/user.configm.Makefile +++ b/Platform/build/user.configm.Makefile @@ -1,5 +1,5 @@ # target name, the target name must have the same name of c source file -TARGET_NAME=test-trace +TARGET_NAME=configm # target # for linux module driver: KO @@ -21,7 +21,7 @@ DEBUG = TRUE PLAT_LINUX ?= TRUE PLAT_ARM64 ?= TRUE -VPATH = ../user/configm +VPATH = ../user/configm/config-server # source code @@ -34,28 +34,28 @@ PLAT_LINUX_SRCS = $(COMMON_SRCS) PLAT_ARM64_SRCS = $(COMMON_SRCS) # gcc CFLAGS -PLAT_ARM64_CFLAGS := -I../user/configm/include -I../../Common -I../common/configm -I../common/rpc -I../common/rpc/hashtable +PLAT_ARM64_CFLAGS := -I../user/configm/config-server/include -I../../Common -I../common/configm -I../common/rpc -I../common/rpc/hashtable PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) PLAT_ARM64_LDFLAGS := PLAT_LINUX_LDFLAGS := - #gcc libs -ARM64_LIBS := -lpthread -lpthread -lev -lm -LINUX_LIBS := -lpthread -lpthread -lev -lm +ARM64_LIBS := ../thirdparty/arm64/libev.so ./libopenrpc-arm64.so -lpthread -lm +LINUX_LIBS := ../thirdparty/x86_64/libev.so ./libopenrpc-linux.so -lpthread -lm ifeq ($(PLAT_ARM64), TRUE) -DEPEND_LIB += ./debug/libopenrpc.so -USER_CLEAN_ITEMS += ./libopenrpc.so +DEPEND_LIB += ../thirdparty/arm64/libev.so ./debug/libopenrpc-arm64.so +USER_CLEAN_ITEMS += ./libopenrpc-arm64.so endif ifeq ($(PLAT_LINUX), TRUE) -DEPEND_LIB += ./debug/libopenrpc.so -USER_CLEAN_ITEMS += ./debug/libopenrpc.soo +DEPEND_LIB += ../thirdparty/x86_64/libev.so ./debug/libopenrpc-linux.so +USER_CLEAN_ITEMS += ./libopenrpc-linux.so endif + # this line must be at below of thus, because of... include ../../Common/common.Makefile diff --git a/Platform/build/user.configmapi.Makefile b/Platform/build/user.configmapi.Makefile new file mode 100644 index 000000000..0a4b1a9dd --- /dev/null +++ b/Platform/build/user.configmapi.Makefile @@ -0,0 +1,77 @@ +# target name, the target name must have the same name of c source file +TARGET_NAME=libconfigmapi + +# 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/configm/config-api + +# source code + +# set the source file, don't used .o because of ... + +COMMON_SRCS = configclient.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/configm -I../common/rpc -I../common/rpc/hashtable +PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) + + +PLAT_ARM64_LDFLAGS := -fPIC -shared +PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS) + + +#gcc libs +ARM64_LIBS := ../thirdparty/arm64/libev.so ./libopenrpc-arm64.so -lpthread -lm +LINUX_LIBS := ../thirdparty/x86_64/libev.so ./libopenrpc-linux.so -lpthread -lm + +ifeq ($(PLAT_ARM64), TRUE) +DEPEND_LIB += ../thirdparty/arm64/libev.so ./debug/libopenrpc-arm64.so +USER_CLEAN_ITEMS += ./libopenrpc-arm64.so +endif + +ifeq ($(PLAT_LINUX), TRUE) +DEPEND_LIB += ../thirdparty/x86_64/libev.so ./debug/libopenrpc-linux.so +USER_CLEAN_ITEMS += ./libopenrpc-linux.so +endif + +# 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.configmtest.Makefile b/Platform/build/user.configmtest.Makefile new file mode 100644 index 000000000..b43021d1f --- /dev/null +++ b/Platform/build/user.configmtest.Makefile @@ -0,0 +1,77 @@ +# target name, the target name must have the same name of c source file +TARGET_NAME=configmtest + +# 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/configm/config-test + +# source code + +# set the source file, don't used .o because of ... + +COMMON_SRCS = configtest.c + +# MRS Board Source Files +PLAT_LINUX_SRCS = $(COMMON_SRCS) +PLAT_ARM64_SRCS = $(COMMON_SRCS) + +# gcc CFLAGS +PLAT_ARM64_CFLAGS := -I../user/configm/config-server/include -I../../Common -I../common/configm -I../common/rpc -I../common/rpc/hashtable +PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) + + +PLAT_ARM64_LDFLAGS := +PLAT_LINUX_LDFLAGS := + +#gcc libs +ARM64_LIBS := ../thirdparty/arm64/libev.so ./libopenrpc-arm64.so ./libconfigmapi-arm64.so -lpthread -lm +LINUX_LIBS := ../thirdparty/x86_64/libev.so ./libopenrpc-linux.so ./libconfigmapi-linux.so -lpthread -lm + +ifeq ($(PLAT_ARM64), TRUE) +DEPEND_LIB += ../thirdparty/arm64/libev.so ./debug/libopenrpc-arm64.so ./debug/libconfigmapi-arm64.so +USER_CLEAN_ITEMS += ./libconfigmapi-arm64.so +endif + +ifeq ($(PLAT_LINUX), TRUE) +DEPEND_LIB += ../thirdparty/x86_64/libev.so ./debug/libopenrpc-linux.so ./debug/libconfigmapi-linux.so +USER_CLEAN_ITEMS += ./libconfigmapi-linux.so +endif + + +# 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.openrpc.Makefile b/Platform/build/user.openrpc.Makefile index d263aec20..95235171c 100755 --- a/Platform/build/user.openrpc.Makefile +++ b/Platform/build/user.openrpc.Makefile @@ -1,5 +1,5 @@ # target name, the target name must have the same name of c source file -TARGET_NAME=openrpc +TARGET_NAME=libopenrpc # target # for linux module driver: KO @@ -43,11 +43,11 @@ PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS) #gcc libs -ARM64_LIBS := -lpthread -lm -LINUX_LIBS := -lpthread -lm +ARM64_LIBS := ../thirdparty/arm64/libev.so -lpthread -lm +LINUX_LIBS := ../thirdparty/x86_64/libev.so -lpthread -lm ifeq ($(PLAT_ARM64), TRUE) -DEPEND_LIB += +DEPEND_LIB += ../thirdparty/arm64/libev.so USER_CLEAN_ITEMS += endif diff --git a/Platform/common/configm/configm.h b/Platform/common/configm/configm.h index b43afe254..cb6bd4c24 100755 --- a/Platform/common/configm/configm.h +++ b/Platform/common/configm/configm.h @@ -3,14 +3,9 @@ #include "rpc_common.h" #include "ipconfig.h" - +#include "configmapi.h" /* 类型定义 */ -#define CONFIG_FROM_WEB 0x00000001 -#define CONFIG_FROM_NETOPEER 0x00000010 -#define CONFIG_FROM_RECOVER1 0x01000000 -#define CONFIG_FROM_RECOVER2 0x02000000 - /* IP CONFIG */ #define IPCONFIG_MODULE 0x00000001 @@ -52,10 +47,6 @@ #define CONFIG_RECOVERY_DONE "/tmp/config_recovery" -typedef struct _config_message config_msg_t; - -typedef struct _config_service config_service_t; - typedef ret_code (*cm_config_chk)(uint source, uint config_type, pointer input, int input_len, pointer output, int *output_len); @@ -72,16 +63,6 @@ typedef ret_code (*cm_config_get_all)(uint source, uint64 config_id, pointer output, short *single_len, int *output_len); -/* 结构体定义 */ - -/* 配置消息 */ -struct _config_message { - uint64 config_id; /*配置ID*/ - uint source; /*配置来源*/ - uint config_type; /*配置类型:add,del,set*/ - char config_buff[0]; /*配置数据缓存*/ -}; - /* 配置注册 */ struct _config_service { uint64 config_id; /* 配置ID,全局唯一,用于寻找对应的配置业务*/ @@ -94,15 +75,10 @@ struct _config_service { cm_config_get_all getall_callback; /* 获取所有配置接口 */ }; -/* 函数声明 */ +typedef struct _config_service config_service_t; + -ret_code web_config_exec_sync(uint config_type, uint64 config_id, - char* config_data, int config_len, - char**output, int *output_len); -ret_code web_config_exec_async(uint config_type, uint64 config_id, - char* config_data, int config_len, - rpc_callback callback, pointer data); #endif /* RPC_COMMON_H_ */ diff --git a/Platform/common/configm/configmapi.h b/Platform/common/configm/configmapi.h new file mode 100644 index 000000000..7f84090f4 --- /dev/null +++ b/Platform/common/configm/configmapi.h @@ -0,0 +1,32 @@ +#ifndef CONFIGMAPI_H_ +#define CONFIGMAPI_H_ + +#include "rpc_common.h" + +#define CONFIG_FROM_WEB 0x00000001 +#define CONFIG_FROM_NETOPEER 0x00000010 +#define CONFIG_FROM_RECOVER1 0x01000000 +#define CONFIG_FROM_RECOVER2 0x02000000 + +/* 结构体定义 */ + +/* 配置消息 */ +struct _config_message { + uint64 config_id; /*配置ID*/ + uint source; /*配置来源*/ + uint config_type; /*配置类型:add,del,set*/ + char config_buff[0]; /*配置数据缓存*/ +}; +typedef struct _config_message config_msg_t; +/* 函数声明 */ + +ret_code web_config_exec_sync(uint config_type, uint64 config_id, + char* config_data, int config_len, + char**output, int *output_len); + +ret_code web_config_exec_async(uint config_type, uint64 config_id, + char* config_data, int config_len, + rpc_callback callback, pointer data); + +#endif /* RPC_COMMON_H_ */ + diff --git a/Platform/thirdparty/arm64/libev.so b/Platform/thirdparty/arm64/libev.so new file mode 100644 index 000000000..b7894409f Binary files /dev/null and b/Platform/thirdparty/arm64/libev.so differ diff --git a/Platform/user/configm/config-api/configclient.c b/Platform/user/configm/config-api/configclient.c new file mode 100644 index 000000000..b54103ee1 --- /dev/null +++ b/Platform/user/configm/config-api/configclient.c @@ -0,0 +1,117 @@ +#include +#include +#include "rpc.h" +#include "configmapi.h" + + +rpc_client *config_client = NULL; + +rpc_client *config_client_get() +{ + if(config_client == NULL) + { + config_client = rpc_client_connect_ex("ConfigManger#0"); + if(config_client == NULL) + { + rpc_log_error("connect ConfigManger#0 error\n"); + return NULL; + } + } + + return config_client; +} + +ret_code config_construct_msg(uint config_type, uint64 config_id, + char* config_data, int config_len, + config_msg_t **config_msg, int *msg_len) +{ + config_msg_t *pconfig_msg; + + if(config_data == NULL) + { + return RET_NULLP; + } + + *msg_len = sizeof(config_msg_t) + config_len; + pconfig_msg= (config_msg_t *) malloc(*msg_len); + if(pconfig_msg == NULL) + { + return RET_NOMEM; + } + + pconfig_msg->source = CONFIG_FROM_WEB; + pconfig_msg->config_type = config_type; + pconfig_msg->config_id = config_id; + + memcpy(pconfig_msg->config_buff, config_data, config_len); + *config_msg = pconfig_msg; + + return RET_OK; +} + +ret_code config_destroy_msg(config_msg_t *config_msg, int msg_len) +{ + if(config_msg) + { + memset(config_msg, 0, msg_len); + free(config_msg); + } +} + + +ret_code web_config_exec_sync(uint config_type, uint64 config_id, + char* config_data, int config_len, + char**output, int *output_len) +{ + rpc_client * client = config_client_get(); + ret_code code = RET_OK ; + config_msg_t *config_msg; + int msg_len = 0; + + if(client == NULL) + { + return RET_ERR; + } + + code = config_construct_msg(config_type, config_id, config_data, + config_len, &config_msg, &msg_len); + ASSERT_RET(code); + + code = rpc_client_call(client, "ConfigManger#0", "cm_config_process", + config_msg, msg_len, (pointer)output, output_len); + ASSERT_RET_NO(code); + + config_destroy_msg(config_msg, msg_len); + + return code; +} + +ret_code web_config_exec_async(uint config_type, uint64 config_id, + char* config_data, int config_len, + rpc_callback callback, pointer data) + { + rpc_client * client = config_client_get(); + config_msg_t *config_msg; + int msg_len = 0; + + ret_code ret = RET_OK ; + + if(client == NULL) + { + return RET_ERR; + } + + ret = config_construct_msg(config_type, config_id, config_data, + config_len, &config_msg, &msg_len); + ASSERT_RET(ret); + + ret = rpc_client_call_async(client, "ConfigManger#0", "cm_config_process", + config_msg, msg_len, callback, data); + + config_destroy_msg(config_msg, msg_len); + + return ret; + } + + + diff --git a/Platform/user/configm/configserver.c b/Platform/user/configm/config-server/configserver.c old mode 100755 new mode 100644 similarity index 95% rename from Platform/user/configm/configserver.c rename to Platform/user/configm/config-server/configserver.c index a583ecc82..822919057 --- a/Platform/user/configm/configserver.c +++ b/Platform/user/configm/config-server/configserver.c @@ -1,374 +1,374 @@ - -#include -#include -#include -#include -#include -#include - -#include "rpc.h" -#include "configm.h" - - -config_service_t g_config_service[] = CONFIG_SERVICE_ARRAY; - -void test_big_data(rpc_conn *conn, pointer input, int input_len, void* data) { - char buf[input_len]; - memcpy(buf, input, input_len); - buf[input_len] = '\0'; - printf("get data is %s\n", buf); - //sleep(15); - rpc_return_null(conn); -} - - -config_service_t *cm_config_service_get(uint64 config_id) -{ - int len = sizeof(g_config_service) / sizeof(config_service_t); - int config_idx; - - for(config_idx = 0; config_idx < len; config_idx++) - { - if(config_id == g_config_service[config_idx].config_id) - { - return &(g_config_service[config_idx]); - } - } - - return NULL; -} - -ret_code cm_config_rec_pre(char **input, short **input_len, - char **output, int *output_len) -{ - /*if((access(CONFIG_RECOVERY_DONE, F_OK))!=-1) - { - return RET_ERR; - } */ - - *input = rpc_new(char, CM_BUFF_SIZE); - *input_len = rpc_new(short, CM_BUFF_SIZE); - *output = rpc_new(char, CM_BUFF_SIZE); - *output_len = CM_BUFF_SIZE; - - if(*input == NULL - || *input_len == NULL - || *output == NULL) - { - return RET_NOMEM; - } - - memset(*input, 0, CM_BUFF_SIZE); - memset(*input_len, 0, CM_BUFF_SIZE * sizeof(short)); - memset(*output, 0, CM_BUFF_SIZE); - - rpc_log_info("==>config revocery start\n"); - - return RET_OK; -} - -ret_code cm_config_rec_done(char *input, short *input_len,char *output) -{ - int fd; - - memset(input, 0, CM_BUFF_SIZE); - memset(input_len, 0, CM_BUFF_SIZE * sizeof(short)); - memset(output, 0, CM_BUFF_SIZE); - - free(input); - free(input_len); - free(output); - - fd = open(CONFIG_RECOVERY_DONE, O_CREAT); - if(fd <= 0) - { - rpc_log_error("create recovery file error"); - } - else - { - close(fd); - } - - rpc_log_info("==>config revocery done!\n"); - - return RET_OK; -} - -ret_code cm_config_get_allconfig(uint source, - config_service_t *config_svr, - char *input_array, - short *len_array, - int *total_len) -{ - ret_code ret = RET_OK; - - memset(input_array, 0, CM_BUFF_SIZE); - memset(len_array, 0, CM_BUFF_SIZE*sizeof(short)); - *total_len = 0; - - ret = config_svr->getall_callback(source, config_svr->config_id, - input_array, len_array, total_len); - ASSERT_RET(ret); - - return ret; -} - - ret_code cm_config_config_rec(uint source, - config_service_t *config_svr, - char *input_array, short *len_array, - int total_len, char *output, int *output_len) - { - uint config_type = CM_CONFIG_SET; - ret_code ret = RET_OK; - int len_tem = 0; - int idx = 0; - - if(config_svr->multi_inst == TRUE) - { - config_type = CM_CONFIG_ADD; - } - - while(len_tem < total_len) - { - config_svr->proc_callback(source, config_type, - input_array, len_array[idx], - output, output_len); - - input_array = input_array + len_array[idx]; - len_tem = len_tem + len_array[idx]; - - idx++; - } - - return RET_OK; - } - - -/* 配置恢复 */ -void cm_config_recovery() -{ - int len = sizeof(g_config_service) / sizeof(config_service_t); - config_service_t *config_svr; - char *input_array = NULL; - short *len_array = NULL; - char *output = NULL; - int output_len; - - int total_len = 0; - ret_code ret = RET_OK; - int config_idx; - int recover_idx = 1,recover_phase; - - ret = cm_config_rec_pre(&input_array, &len_array, &output, &output_len); - ASSERT_RET_VOID(ret); - - while(recover_idx <= 2) - { - if(recover_idx == 1) - { - recover_phase = CONFIG_FROM_RECOVER1; - } - else if(recover_idx == 2) - { - recover_phase = CONFIG_FROM_RECOVER2; - } - else - { - break; - } - - for(config_idx = 0; config_idx < len; config_idx++) - { - config_svr = &(g_config_service[config_idx]); - - if(config_svr->recovery != TRUE) - { - continue; - } - - cm_config_get_allconfig(recover_phase, - config_svr, input_array, - len_array, &total_len); - - cm_config_config_rec(recover_phase, config_svr, - input_array, len_array, total_len, - output, &output_len); - } - - recover_idx++; - } - - cm_config_rec_done(input_array, len_array, output); - - return; -} - -/* 配置处理入口函数,所有的配置,均在此函数中处理 */ -void cm_config_process(rpc_conn *conn, pointer input, int input_len, void* data) -{ - config_service_t *config_svr; - config_msg_t *config_msg; - char *cm_get_buff = NULL; - ret_code ret = RET_OK; - int config_len = 0; - short* single_len = NULL; - int buff_len = CM_BUFF_SIZE; - int index = 0; - - if(conn == NULL || input == NULL) - { - rpc_return_error(conn, RET_NULLP, "NULL pointer"); - return; - } - - /*parse message*/ - if(input_len < sizeof(config_msg_t)) - { - rpc_return_error(conn, RET_NOMEM, "not enough memery"); - return; - } - - config_msg = (config_msg_t *)input; - config_len = input_len - sizeof(config_msg_t); - - rpc_log_info("recv config from %d, config type is %d config id is 0x%llx ,len is %x\r\n", - config_msg->source, config_msg->config_type, config_msg->config_id, - config_len); - - config_svr = cm_config_service_get(config_msg->config_id); - if(config_svr == NULL) - { - rpc_return_error(conn, RET_NULLP, "NULL pointer"); - return; - } - - /*source check*/ - if(!(config_svr->config_id & config_msg->config_id)) - { - rpc_return_error(conn, RET_CHKERR, "source check error!\r\n"); - return; - - } - - cm_get_buff = rpc_new(char, buff_len); - if(cm_get_buff == NULL) - { - rpc_return_error(conn, RET_NOMEM, "not enough memery"); - return; - } - - memset(cm_get_buff, 0, buff_len); - - /*config check*/ - if(config_svr->chk_callback) - { - ret = config_svr->chk_callback(config_msg->source, - config_msg->config_type, - config_msg->config_buff, - config_len, - cm_get_buff, - &buff_len); - if(ret != RET_OK) - { - rpc_return_error(conn, ret, cm_get_buff); - goto exit; - } - } - - /*config exec*/ - switch(config_msg->config_type) - { - case CM_CONFIG_GET: - if(config_svr->get_callback) - { - ret = config_svr->get_callback(config_msg->source, - config_msg->config_buff, - config_len, - cm_get_buff, - &buff_len); - } - break; - - case CM_CONFIG_GET_ALL: - if(config_svr->getall_callback) - { - single_len = rpc_new(short, CM_BUFF_SIZE); - if(single_len == NULL) - { - rpc_return_error(conn, RET_NOMEM, "not enough memery"); - goto exit; - } - - ret = config_svr->getall_callback(config_msg->source, - config_svr->config_id, - cm_get_buff, - single_len, - &buff_len); - } - break; - - default: - if(config_svr->proc_callback) - { - ret = config_svr->proc_callback(config_msg->source, - config_msg->config_type, - config_msg->config_buff, - config_len, - cm_get_buff, - &buff_len); - } - break; - } - - if(buff_len > CM_BUFF_SIZE) - { - ret = RET_NOMEM; - } - - if(ret != RET_OK) - { - rpc_return_error(conn, ret, cm_get_buff); - goto exit; - } - - rpc_return(conn, cm_get_buff, buff_len); - - exit: - rpc_free(cm_get_buff); - - if(single_len != NULL) - { - rpc_free(single_len); - } - - return; -} - - -int main(int argc, char **argv) -{ - rpc_server *server; - - if (server= rpc_server_create_ex("ConfigManger#0")) - { - rpc_log_info("start server\n"); - } - else - { - rpc_log_error("start server error\n"); - return EXIT_FAILURE; - } - - /* 配置恢复 */ - cm_config_recovery(); - - /* 注册配置处理函数 */ - rpc_server_regservice(server, "ConfigManger#0", "cm_config_process", cm_config_process); - rpc_server_regservice(server, "ConfigManger#0", "test_big_data", test_big_data); - - for(;;) - { - rpc_sleep(1000); - } - return EXIT_SUCCESS; -} + +#include +#include +#include +#include +#include +#include + +#include "rpc.h" +#include "configm.h" + + +config_service_t g_config_service[] = CONFIG_SERVICE_ARRAY; + +void test_big_data(rpc_conn *conn, pointer input, int input_len, void* data) { + char buf[input_len]; + memcpy(buf, input, input_len); + buf[input_len] = '\0'; + printf("get data is %s\n", buf); + //sleep(15); + rpc_return_null(conn); +} + + +config_service_t *cm_config_service_get(uint64 config_id) +{ + int len = sizeof(g_config_service) / sizeof(config_service_t); + int config_idx; + + for(config_idx = 0; config_idx < len; config_idx++) + { + if(config_id == g_config_service[config_idx].config_id) + { + return &(g_config_service[config_idx]); + } + } + + return NULL; +} + +ret_code cm_config_rec_pre(char **input, short **input_len, + char **output, int *output_len) +{ + /*if((access(CONFIG_RECOVERY_DONE, F_OK))!=-1) + { + return RET_ERR; + } */ + + *input = rpc_new(char, CM_BUFF_SIZE); + *input_len = rpc_new(short, CM_BUFF_SIZE); + *output = rpc_new(char, CM_BUFF_SIZE); + *output_len = CM_BUFF_SIZE; + + if(*input == NULL + || *input_len == NULL + || *output == NULL) + { + return RET_NOMEM; + } + + memset(*input, 0, CM_BUFF_SIZE); + memset(*input_len, 0, CM_BUFF_SIZE * sizeof(short)); + memset(*output, 0, CM_BUFF_SIZE); + + rpc_log_info("==>config revocery start\n"); + + return RET_OK; +} + +ret_code cm_config_rec_done(char *input, short *input_len,char *output) +{ + int fd; + + memset(input, 0, CM_BUFF_SIZE); + memset(input_len, 0, CM_BUFF_SIZE * sizeof(short)); + memset(output, 0, CM_BUFF_SIZE); + + free(input); + free(input_len); + free(output); + + fd = open(CONFIG_RECOVERY_DONE, O_CREAT, 0777); + if(fd <= 0) + { + rpc_log_error("create recovery file error"); + } + else + { + close(fd); + } + + rpc_log_info("==>config revocery done!\n"); + + return RET_OK; +} + +ret_code cm_config_get_allconfig(uint source, + config_service_t *config_svr, + char *input_array, + short *len_array, + int *total_len) +{ + ret_code ret = RET_OK; + + memset(input_array, 0, CM_BUFF_SIZE); + memset(len_array, 0, CM_BUFF_SIZE*sizeof(short)); + *total_len = 0; + + ret = config_svr->getall_callback(source, config_svr->config_id, + input_array, len_array, total_len); + ASSERT_RET(ret); + + return ret; +} + + ret_code cm_config_config_rec(uint source, + config_service_t *config_svr, + char *input_array, short *len_array, + int total_len, char *output, int *output_len) + { + uint config_type = CM_CONFIG_SET; + ret_code ret = RET_OK; + int len_tem = 0; + int idx = 0; + + if(config_svr->multi_inst == TRUE) + { + config_type = CM_CONFIG_ADD; + } + + while(len_tem < total_len) + { + config_svr->proc_callback(source, config_type, + input_array, len_array[idx], + output, output_len); + + input_array = input_array + len_array[idx]; + len_tem = len_tem + len_array[idx]; + + idx++; + } + + return RET_OK; + } + + +/* 配置恢复 */ +void cm_config_recovery() +{ + int len = sizeof(g_config_service) / sizeof(config_service_t); + config_service_t *config_svr; + char *input_array = NULL; + short *len_array = NULL; + char *output = NULL; + int output_len; + + int total_len = 0; + ret_code ret = RET_OK; + int config_idx; + int recover_idx = 1,recover_phase; + + ret = cm_config_rec_pre(&input_array, &len_array, &output, &output_len); + ASSERT_RET_VOID(ret); + + while(recover_idx <= 2) + { + if(recover_idx == 1) + { + recover_phase = CONFIG_FROM_RECOVER1; + } + else if(recover_idx == 2) + { + recover_phase = CONFIG_FROM_RECOVER2; + } + else + { + break; + } + + for(config_idx = 0; config_idx < len; config_idx++) + { + config_svr = &(g_config_service[config_idx]); + + if(config_svr->recovery != TRUE) + { + continue; + } + + cm_config_get_allconfig(recover_phase, + config_svr, input_array, + len_array, &total_len); + + cm_config_config_rec(recover_phase, config_svr, + input_array, len_array, total_len, + output, &output_len); + } + + recover_idx++; + } + + cm_config_rec_done(input_array, len_array, output); + + return; +} + +/* 配置处理入口函数,所有的配置,均在此函数中处理 */ +void cm_config_process(rpc_conn *conn, pointer input, int input_len, void* data) +{ + config_service_t *config_svr; + config_msg_t *config_msg; + char *cm_get_buff = NULL; + ret_code ret = RET_OK; + int config_len = 0; + short* single_len = NULL; + int buff_len = CM_BUFF_SIZE; + int index = 0; + + if(conn == NULL || input == NULL) + { + rpc_return_error(conn, RET_NULLP, "NULL pointer"); + return; + } + + /*parse message*/ + if(input_len < sizeof(config_msg_t)) + { + rpc_return_error(conn, RET_NOMEM, "not enough memery"); + return; + } + + config_msg = (config_msg_t *)input; + config_len = input_len - sizeof(config_msg_t); + + rpc_log_info("recv config from %d, config type is %d config id is 0x%llx ,len is %x\r\n", + config_msg->source, config_msg->config_type, config_msg->config_id, + config_len); + + config_svr = cm_config_service_get(config_msg->config_id); + if(config_svr == NULL) + { + rpc_return_error(conn, RET_NULLP, "NULL pointer"); + return; + } + + /*source check*/ + if(!(config_svr->config_id & config_msg->config_id)) + { + rpc_return_error(conn, RET_CHKERR, "source check error!\r\n"); + return; + + } + + cm_get_buff = rpc_new(char, buff_len); + if(cm_get_buff == NULL) + { + rpc_return_error(conn, RET_NOMEM, "not enough memery"); + return; + } + + memset(cm_get_buff, 0, buff_len); + + /*config check*/ + if(config_svr->chk_callback) + { + ret = config_svr->chk_callback(config_msg->source, + config_msg->config_type, + config_msg->config_buff, + config_len, + cm_get_buff, + &buff_len); + if(ret != RET_OK) + { + rpc_return_error(conn, ret, cm_get_buff); + goto exit; + } + } + + /*config exec*/ + switch(config_msg->config_type) + { + case CM_CONFIG_GET: + if(config_svr->get_callback) + { + ret = config_svr->get_callback(config_msg->source, + config_msg->config_buff, + config_len, + cm_get_buff, + &buff_len); + } + break; + + case CM_CONFIG_GET_ALL: + if(config_svr->getall_callback) + { + single_len = rpc_new(short, CM_BUFF_SIZE); + if(single_len == NULL) + { + rpc_return_error(conn, RET_NOMEM, "not enough memery"); + goto exit; + } + + ret = config_svr->getall_callback(config_msg->source, + config_svr->config_id, + cm_get_buff, + single_len, + &buff_len); + } + break; + + default: + if(config_svr->proc_callback) + { + ret = config_svr->proc_callback(config_msg->source, + config_msg->config_type, + config_msg->config_buff, + config_len, + cm_get_buff, + &buff_len); + } + break; + } + + if(buff_len > CM_BUFF_SIZE) + { + ret = RET_NOMEM; + } + + if(ret != RET_OK) + { + rpc_return_error(conn, ret, cm_get_buff); + goto exit; + } + + rpc_return(conn, cm_get_buff, buff_len); + + exit: + rpc_free(cm_get_buff); + + if(single_len != NULL) + { + rpc_free(single_len); + } + + return; +} + + +int main(int argc, char **argv) +{ + rpc_server *server; + + if (server= rpc_server_create_ex("ConfigManger#0")) + { + rpc_log_info("start server\n"); + } + else + { + rpc_log_error("start server error\n"); + return EXIT_FAILURE; + } + + /* 配置恢复 */ + cm_config_recovery(); + + /* 注册配置处理函数 */ + rpc_server_regservice(server, "ConfigManger#0", "cm_config_process", cm_config_process); + rpc_server_regservice(server, "ConfigManger#0", "test_big_data", test_big_data); + + for(;;) + { + rpc_sleep(1000); + } + return EXIT_SUCCESS; +} diff --git a/Platform/user/configm/include/ipconfig.h b/Platform/user/configm/config-server/include/ipconfig.h old mode 100755 new mode 100644 similarity index 96% rename from Platform/user/configm/include/ipconfig.h rename to Platform/user/configm/config-server/include/ipconfig.h index 5b51d7350..865f81a83 --- a/Platform/user/configm/include/ipconfig.h +++ b/Platform/user/configm/config-server/include/ipconfig.h @@ -1,93 +1,93 @@ -#ifndef IPCONFIG_H_ -#define IPCONFIG_H_ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define MAX_IF_NUM 32 -#define INTERFACE_NAMSIZ 20 -/* Max bit/byte length of IPv4 address. */ -#define IPV4_MAX_BYTELEN 4 -#define IPV4_MAX_BITLEN 32 -#define IPV4_MAX_PREFIXLEN 32 -#define IPV4_DEFAULT_PREFIXLEN 16 - -#define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN) - -#define IPV4_ADDR_COPY(D,S) ipv4_addr_copy((D), (S)) - -#define IPV4_NET0(a) ((((uint)(a)) & 0xff000000) == 0x00000000) -#define IPV4_NET127(a) ((((uint)(a)) & 0xff000000) == 0x7f000000) -#define IPV4_LINKLOCAL(a) ((((uint)(a)) & 0xffff0000) == 0xa9fe0000) -#define IPV4_CLASS_DE(a) ((((uint)(a)) & 0xe0000000) == 0xe0000000) -#define IPV4_MC_LINKLOCAL(a) ((((uint)(a)) & 0xffffff00) == 0xe0000000) -#define IPV4_ADDR_SAME(A,B) ipv4_addr_same((A), (B)) - -static inline boolean ipv4_addr_same(const struct in_addr *a, - const struct in_addr *b) -{ - return (a->s_addr == b->s_addr); -} - - -static inline void ipv4_addr_copy(struct in_addr *dst, - const struct in_addr *src) -{ - dst->s_addr = src->s_addr; -} - -/* NOTE: This routine expects the address argument in network byte order. */ -static inline int ipv4_martian(struct in_addr *addr) -{ - in_addr_t ip = ntohl(addr->s_addr); - - if (IPV4_NET0(ip) || IPV4_NET127(ip) || IPV4_CLASS_DE(ip)) - { - return TRUE; - } - - return FALSE; -} - - -/* 配置消息 */ -struct _ip_config_str { - char ifname[INTERFACE_NAMSIZ]; - uchar family; - uchar prefixlen; - struct in_addr prefix __attribute__((aligned(8))); -}; - -typedef struct _ip_config_str ip_config_t; - -/* 业务模块函数声明 */ - -/* ip config */ -ret_code ip_config_chk(uint source, uint config_type, - pointer input, int input_len, - pointer output, int *output_len); - -ret_code ip_config_proc(uint source, uint config_type, - pointer input, int input_len, - pointer output, int *output_len); - -ret_code ip_config_get(uint source, - pointer input, int input_len, - pointer output, int *output_len); - -ret_code ip_config_get_all(uint source, uint64 config_id, - pointer output, short *single_len, - int *output_len); - - - -#endif /* IPCONFIG_H_ */ - +#ifndef IPCONFIG_H_ +#define IPCONFIG_H_ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_IF_NUM 32 +#define INTERFACE_NAMSIZ 20 +/* Max bit/byte length of IPv4 address. */ +#define IPV4_MAX_BYTELEN 4 +#define IPV4_MAX_BITLEN 32 +#define IPV4_MAX_PREFIXLEN 32 +#define IPV4_DEFAULT_PREFIXLEN 16 + +#define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN) + +#define IPV4_ADDR_COPY(D,S) ipv4_addr_copy((D), (S)) + +#define IPV4_NET0(a) ((((uint)(a)) & 0xff000000) == 0x00000000) +#define IPV4_NET127(a) ((((uint)(a)) & 0xff000000) == 0x7f000000) +#define IPV4_LINKLOCAL(a) ((((uint)(a)) & 0xffff0000) == 0xa9fe0000) +#define IPV4_CLASS_DE(a) ((((uint)(a)) & 0xe0000000) == 0xe0000000) +#define IPV4_MC_LINKLOCAL(a) ((((uint)(a)) & 0xffffff00) == 0xe0000000) +#define IPV4_ADDR_SAME(A,B) ipv4_addr_same((A), (B)) + +static inline boolean ipv4_addr_same(const struct in_addr *a, + const struct in_addr *b) +{ + return (a->s_addr == b->s_addr); +} + + +static inline void ipv4_addr_copy(struct in_addr *dst, + const struct in_addr *src) +{ + dst->s_addr = src->s_addr; +} + +/* NOTE: This routine expects the address argument in network byte order. */ +static inline int ipv4_martian(struct in_addr *addr) +{ + in_addr_t ip = ntohl(addr->s_addr); + + if (IPV4_NET0(ip) || IPV4_NET127(ip) || IPV4_CLASS_DE(ip)) + { + return TRUE; + } + + return FALSE; +} + + +/* 配置消息 */ +struct _ip_config_str { + char ifname[INTERFACE_NAMSIZ]; + uchar family; + uchar prefixlen; + struct in_addr prefix __attribute__((aligned(8))); +}; + +typedef struct _ip_config_str ip_config_t; + +/* 业务模块函数声明 */ + +/* ip config */ +ret_code ip_config_chk(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code ip_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code ip_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code ip_config_get_all(uint source, uint64 config_id, + pointer output, short *single_len, + int *output_len); + + + +#endif /* IPCONFIG_H_ */ + diff --git a/Platform/user/configm/include/parsefile.h b/Platform/user/configm/config-server/include/parsefile.h old mode 100755 new mode 100644 similarity index 96% rename from Platform/user/configm/include/parsefile.h rename to Platform/user/configm/config-server/include/parsefile.h index 2100c8af9..dbe0560d0 --- a/Platform/user/configm/include/parsefile.h +++ b/Platform/user/configm/config-server/include/parsefile.h @@ -1,10 +1,10 @@ -#ifndef PARSEFILE_H_ -#define PARSEFILE_H_ - -#define IFCONFIG_PATH "/etc/network/interfaces" -#define IF_BUFF_LEN 64 - -void set_if_config(char *if_name, char *conf_name, char *conf_buff); -void del_if_config(char *if_name, char *conf_buff); - -#endif +#ifndef PARSEFILE_H_ +#define PARSEFILE_H_ + +#define IFCONFIG_PATH "/etc/network/interfaces" +#define IF_BUFF_LEN 64 + +void set_if_config(char *if_name, char *conf_name, char *conf_buff); +void del_if_config(char *if_name, char *conf_buff); + +#endif diff --git a/Platform/user/configm/ipconfig/ipconfig.c b/Platform/user/configm/config-server/ipconfig/ipconfig.c old mode 100755 new mode 100644 similarity index 95% rename from Platform/user/configm/ipconfig/ipconfig.c rename to Platform/user/configm/config-server/ipconfig/ipconfig.c index 9f6a54385..1885f4f56 --- a/Platform/user/configm/ipconfig/ipconfig.c +++ b/Platform/user/configm/config-server/ipconfig/ipconfig.c @@ -1,389 +1,389 @@ -#include "configm.h" -#include "ipconfig.h" -#include "rpc.h" -#include "parsefile.h" - -uchar ip_masklen(struct in_addr netmask) -{ - uint tmp = ~ntohl(netmask.s_addr); - if (tmp) - /* clz: count leading zeroes. sadly, the behaviour of this - * builtin - * is undefined for a 0 argument, even though most CPUs give 32 - */ - return __builtin_clz(tmp); - else - return 32; -} - - -/* Convert masklen into IP address's netmask (network byte order). */ -void masklen2ip(const int masklen, struct in_addr *netmask) -{ - if(masklen < 0 || masklen > IPV4_MAX_BITLEN) - { - return; - } - - /* left shift is only defined for less than the size of the type. - * we unconditionally use long long in case the target platform - * has defined behaviour for << 32 (or has a 64-bit left shift) */ - - if (sizeof(unsigned long long) > 4) - netmask->s_addr = htonl(0xffffffffULL << (32 - masklen)); - else - netmask->s_addr = - htonl(masklen ? 0xffffffffU << (32 - masklen) : 0); -} - -void ip_save_file(ip_config_t *ip_conf, uint config_type) -{ - char *addr_name = "address"; - char *mask_name = "netmask"; - struct in_addr netmask; - char addr_buff[IF_BUFF_LEN] = {0}; - char mask_buff[IF_BUFF_LEN] = {0}; - - if(config_type == CM_CONFIG_SET) - { - sprintf(addr_buff, "address %s\n", inet_ntoa(ip_conf->prefix)); - masklen2ip(ip_conf->prefixlen, &netmask); - sprintf(mask_buff, "netmask %s\n", inet_ntoa(netmask)); - printf("%s,%s\r\n",addr_buff, mask_buff); - - set_if_config(ip_conf->ifname, addr_name, addr_buff); - set_if_config(ip_conf->ifname, mask_name, mask_buff); - } - else if(config_type == CM_CONFIG_DEL) - { - del_if_config(ip_conf->ifname, addr_name); - del_if_config(ip_conf->ifname, mask_name); - } -} - -/* call ioctl system call */ -ret_code if_ioctl(unsigned long request, caddr_t ifreq, int *ret) -{ - int sock; - int err = 0; - - *ret = 0; - - sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock < 0) - { - rpc_log_error("Cannot create UDP socket"); - return RET_SOCKERR; - } - if ((*ret = ioctl(sock, request, ifreq)) < 0) - { - err = errno; - rpc_log_error("Ioctl error: %s\n", strerror(errno)); - - } - - close(sock); - - if (*ret < 0) - { - errno = err; - *ret = err; - return RET_SYSERR; - } - - return RET_OK; -} - -ret_code if_set_prefix(ip_config_t *ip_conf, int *code) -{ - ret_code ret; - struct ifreq ifreq; - struct sockaddr_in addr; - struct sockaddr_in mask; - - strncpy(ifreq.ifr_name, ip_conf->ifname, sizeof(ifreq.ifr_name)); - addr.sin_addr = ip_conf->prefix; - addr.sin_family = ip_conf->family; - memcpy(&ifreq.ifr_addr, &addr, sizeof(struct sockaddr_in)); - - ret = if_ioctl(SIOCSIFADDR, (caddr_t)&ifreq, code); - ASSERT_RET(ret); - - if(ip_conf->prefix.s_addr != 0) - { - masklen2ip(ip_conf->prefixlen, &mask.sin_addr); - mask.sin_family = ip_conf->family; - memcpy(&ifreq.ifr_netmask, &mask, sizeof(struct sockaddr_in)); - - ret = if_ioctl(SIOCSIFNETMASK, (caddr_t)&ifreq, code); - ASSERT_RET(ret); - } - - - return 0; -} - -ret_code if_get_prefix_all(ip_config_t *ip_conf, int *cnt, int *code) -{ - struct ifreq ifreq[MAX_IF_NUM]; - struct sockaddr_in *addr; - struct ifreq netmask; - struct ifconf ifc; - int if_count = 0; - ret_code ret; - int mask_ret; - int i; - - memset(&ifc, 0, sizeof(struct ifconf)); - memset(&ifreq, 0, MAX_IF_NUM * sizeof(struct ifreq)); - - ifc.ifc_len = MAX_IF_NUM * sizeof(struct ifreq); - ifc.ifc_buf = (char *)ifreq; - - ret = if_ioctl(SIOCGIFCONF, (caddr_t)&ifc, code); - ASSERT_RET(ret); - - if_count = ifc.ifc_len / (sizeof(struct ifreq)); - - rpc_log_info("if num is %d\n", if_count); - - if(if_count * sizeof(ip_config_t) > CM_BUFF_SIZE) - { - ret = RET_NOMEM; - ASSERT_RET(ret); - } - - *cnt = 0; - for(i = 0; i < if_count; i++) - { - rpc_log_info("get interface %s info\n", ifreq[i].ifr_name); - strncpy(ip_conf[i].ifname, ifreq[i].ifr_name, INTERFACE_NAMSIZ); - ip_conf[i].family = AF_INET; - ip_conf[i].prefix = ((struct sockaddr_in *)&(ifreq[i].ifr_addr))->sin_addr; - - memset(&netmask, 0, sizeof(netmask)); - strncpy(netmask.ifr_name, ifreq[i].ifr_name, INTERFACE_NAMSIZ); - - ret = if_ioctl(SIOCGIFNETMASK, (caddr_t)&netmask, &mask_ret); - ASSERT_RET_NO(ret); - - addr = ( struct sockaddr_in * )&(netmask.ifr_netmask ); - ip_conf[i].prefixlen = ip_masklen(addr->sin_addr); - - (*cnt)++; - } - - return RET_OK; -} - -ret_code if_get_prefix(ip_config_t *ip_conf, int *code) -{ - struct sockaddr_in *addr; - struct ifreq netmask; - struct ifreq ifreq; - ret_code ret = RET_OK; - int mask_ret; - int i; - - if(ip_conf->family != AF_INET) - { - ret = RET_INPUTERR; - } - - ASSERT_RET(ret); - - memset(&ifreq, 0, sizeof(struct ifreq)); - - rpc_log_info("get interface %s info\n", ip_conf->ifname); - - strncpy(ifreq.ifr_name, ip_conf->ifname, sizeof(ifreq.ifr_name)); - - ret = if_ioctl(SIOCGIFADDR, (caddr_t)&ifreq, code); - ASSERT_RET(ret); - - - ip_conf->prefix = ((struct sockaddr_in *)&(ifreq.ifr_addr))->sin_addr; - - memset(&ifreq, 0, sizeof(ifreq)); - strncpy(ifreq.ifr_name, ip_conf->ifname, sizeof(ifreq.ifr_name)); - - ret = if_ioctl(SIOCGIFNETMASK, (caddr_t)&ifreq, &mask_ret); - ASSERT_RET_NO(ret); - - addr = ( struct sockaddr_in * )&(ifreq.ifr_netmask); - ip_conf->prefixlen = ip_masklen(addr->sin_addr); - - return ret; -} - -ret_code ip_config_set_chk(uint source,uint config_type, - pointer input, int input_len, - pointer output, int *output_len) -{ - ret_code ret = RET_OK; - ip_config_t *ip_conf; - - ip_conf = (ip_config_t *)input; - - if(input_len < sizeof(ip_config_t) - || strlen(ip_conf->ifname) == 0) - { - ret = RET_INPUTERR; - } - - if (ipv4_martian(&ip_conf->prefix)) - { - ret = RET_IPINVALID; - } - - ASSERT_RET(ret); - - if(ip_conf->prefixlen == 0 || ip_conf->prefixlen > IPV4_MAX_PREFIXLEN) - { - ip_conf->prefixlen = IPV4_DEFAULT_PREFIXLEN; - } - - return RET_OK; -} - -ret_code ip_config_get_chk(uint source,uint config_type, - pointer input, int input_len, - pointer output, int *output_len) -{ - ret_code ret = RET_OK; - ip_config_t *ip_conf; - - ip_conf = (ip_config_t *)input; - - if(input_len < sizeof(ip_config_t) - || strlen(ip_conf->ifname) == 0 ) - { - ret = RET_INPUTERR; - } - - return ret; -} - -ret_code ip_config_getall_chk(uint source,uint config_type, - pointer input, int input_len, - pointer output, int *output_len) -{ - ret_code ret = RET_OK; - - if(*output_len < MAX_IF_NUM * sizeof(ip_config_t)) - { - ret = RET_INPUTERR; - } - - return ret; -} - - -ret_code ip_config_chk(uint source,uint config_type, - pointer input, int input_len, - pointer output, int *output_len) - -{ - ret_code ret = RET_OK; - int code = 0; - - switch (config_type) - { - case CM_CONFIG_SET: - case CM_CONFIG_DEL: - ret = ip_config_set_chk(source, config_type, - input, input_len, - output, output_len); - break; - case CM_CONFIG_GET: - ret = ip_config_get_chk(source, config_type, - input, input_len, - output, output_len); - break; - case CM_CONFIG_GET_ALL: - ret = ip_config_getall_chk(source, config_type, - input, input_len, - output, output_len); - break; - default: - ret = RET_NOTSUPPORT; - } - - RET_ERR_FORMART(ret, code, output, *output_len); - return ret; -} - -ret_code ip_config_proc(uint source, uint config_type, - pointer input, int input_len, - pointer output, int *output_len) -{ - ip_config_t *ip_conf; - ret_code ret = RET_OK; - int code; - - ip_conf = (ip_config_t *)input; - - if(config_type == CM_CONFIG_DEL) - { - ip_conf->prefix.s_addr = 0; - ip_conf->prefixlen = IPV4_DEFAULT_PREFIXLEN; - } - - rpc_log_info("config type is %d, if %s ip %s prefixlen %d\n", - config_type, ip_conf->ifname, - inet_ntoa(ip_conf->prefix), - ip_conf->prefixlen); - - ret = if_set_prefix(ip_conf, &code); - - RET_ERR_FORMART(ret, code, output, *output_len); - ASSERT_RET(ret); - - ip_save_file(ip_conf, config_type); - - return RET_OK; -} - -ret_code ip_config_get(uint source, - pointer input, int input_len, - pointer output, int *output_len) -{ - ip_config_t *ip_conf; - ret_code ret = RET_OK; - int code; - - ip_conf = (ip_config_t *)input; - - ret = if_get_prefix(ip_conf, &code); - - RET_ERR_FORMART(ret, code, output, *output_len); - ASSERT_RET(ret); - - memcpy(output, ip_conf, sizeof(ip_config_t)); - *output_len = sizeof(ip_config_t); - - return ret; -} - -ret_code ip_config_get_all(uint source, uint64 config_id, - pointer output, short *single_len, - int *output_len) -{ - ip_config_t ip_conf; - ret_code ret; - int count = 0; - int code; - - rpc_log_info("ip_config_get_all\r\n"); - - ret = if_get_prefix_all((ip_config_t *)output, &count, &code); - - RET_ERR_FORMART(ret, code, output, *output_len); - ASSERT_RET(ret); - - *single_len = sizeof(ip_config_t); - *output_len = count * sizeof(ip_config_t); - - return RET_OK; - -} +#include "configm.h" +#include "ipconfig.h" +#include "rpc.h" +#include "parsefile.h" + +uchar ip_masklen(struct in_addr netmask) +{ + uint tmp = ~ntohl(netmask.s_addr); + if (tmp) + /* clz: count leading zeroes. sadly, the behaviour of this + * builtin + * is undefined for a 0 argument, even though most CPUs give 32 + */ + return __builtin_clz(tmp); + else + return 32; +} + + +/* Convert masklen into IP address's netmask (network byte order). */ +void masklen2ip(const int masklen, struct in_addr *netmask) +{ + if(masklen < 0 || masklen > IPV4_MAX_BITLEN) + { + return; + } + + /* left shift is only defined for less than the size of the type. + * we unconditionally use long long in case the target platform + * has defined behaviour for << 32 (or has a 64-bit left shift) */ + + if (sizeof(unsigned long long) > 4) + netmask->s_addr = htonl(0xffffffffULL << (32 - masklen)); + else + netmask->s_addr = + htonl(masklen ? 0xffffffffU << (32 - masklen) : 0); +} + +void ip_save_file(ip_config_t *ip_conf, uint config_type) +{ + char *addr_name = "address"; + char *mask_name = "netmask"; + struct in_addr netmask; + char addr_buff[IF_BUFF_LEN] = {0}; + char mask_buff[IF_BUFF_LEN] = {0}; + + if(config_type == CM_CONFIG_SET) + { + sprintf(addr_buff, "address %s\n", inet_ntoa(ip_conf->prefix)); + masklen2ip(ip_conf->prefixlen, &netmask); + sprintf(mask_buff, "netmask %s\n", inet_ntoa(netmask)); + printf("%s,%s\r\n",addr_buff, mask_buff); + + set_if_config(ip_conf->ifname, addr_name, addr_buff); + set_if_config(ip_conf->ifname, mask_name, mask_buff); + } + else if(config_type == CM_CONFIG_DEL) + { + del_if_config(ip_conf->ifname, addr_name); + del_if_config(ip_conf->ifname, mask_name); + } +} + +/* call ioctl system call */ +ret_code if_ioctl(unsigned long request, caddr_t ifreq, int *ret) +{ + int sock; + int err = 0; + + *ret = 0; + + sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock < 0) + { + rpc_log_error("Cannot create UDP socket"); + return RET_SOCKERR; + } + if ((*ret = ioctl(sock, request, ifreq)) < 0) + { + err = errno; + rpc_log_error("Ioctl error: %s\n", strerror(errno)); + + } + + close(sock); + + if (*ret < 0) + { + errno = err; + *ret = err; + return RET_SYSERR; + } + + return RET_OK; +} + +ret_code if_set_prefix(ip_config_t *ip_conf, int *code) +{ + ret_code ret; + struct ifreq ifreq; + struct sockaddr_in addr; + struct sockaddr_in mask; + + strncpy(ifreq.ifr_name, ip_conf->ifname, sizeof(ifreq.ifr_name)); + addr.sin_addr = ip_conf->prefix; + addr.sin_family = ip_conf->family; + memcpy(&ifreq.ifr_addr, &addr, sizeof(struct sockaddr_in)); + + ret = if_ioctl(SIOCSIFADDR, (caddr_t)&ifreq, code); + ASSERT_RET(ret); + + if(ip_conf->prefix.s_addr != 0) + { + masklen2ip(ip_conf->prefixlen, &mask.sin_addr); + mask.sin_family = ip_conf->family; + memcpy(&ifreq.ifr_netmask, &mask, sizeof(struct sockaddr_in)); + + ret = if_ioctl(SIOCSIFNETMASK, (caddr_t)&ifreq, code); + ASSERT_RET(ret); + } + + + return 0; +} + +ret_code if_get_prefix_all(ip_config_t *ip_conf, int *cnt, int *code) +{ + struct ifreq ifreq[MAX_IF_NUM]; + struct sockaddr_in *addr; + struct ifreq netmask; + struct ifconf ifc; + int if_count = 0; + ret_code ret; + int mask_ret; + int i; + + memset(&ifc, 0, sizeof(struct ifconf)); + memset(&ifreq, 0, MAX_IF_NUM * sizeof(struct ifreq)); + + ifc.ifc_len = MAX_IF_NUM * sizeof(struct ifreq); + ifc.ifc_buf = (char *)ifreq; + + ret = if_ioctl(SIOCGIFCONF, (caddr_t)&ifc, code); + ASSERT_RET(ret); + + if_count = ifc.ifc_len / (sizeof(struct ifreq)); + + rpc_log_info("if num is %d\n", if_count); + + if(if_count * sizeof(ip_config_t) > CM_BUFF_SIZE) + { + ret = RET_NOMEM; + ASSERT_RET(ret); + } + + *cnt = 0; + for(i = 0; i < if_count; i++) + { + rpc_log_info("get interface %s info\n", ifreq[i].ifr_name); + strncpy(ip_conf[i].ifname, ifreq[i].ifr_name, INTERFACE_NAMSIZ); + ip_conf[i].family = AF_INET; + ip_conf[i].prefix = ((struct sockaddr_in *)&(ifreq[i].ifr_addr))->sin_addr; + + memset(&netmask, 0, sizeof(netmask)); + strncpy(netmask.ifr_name, ifreq[i].ifr_name, sizeof(netmask.ifr_name)); + + ret = if_ioctl(SIOCGIFNETMASK, (caddr_t)&netmask, &mask_ret); + ASSERT_RET_NO(ret); + + addr = ( struct sockaddr_in * )&(netmask.ifr_netmask ); + ip_conf[i].prefixlen = ip_masklen(addr->sin_addr); + + (*cnt)++; + } + + return RET_OK; +} + +ret_code if_get_prefix(ip_config_t *ip_conf, int *code) +{ + struct sockaddr_in *addr; + struct ifreq netmask; + struct ifreq ifreq; + ret_code ret = RET_OK; + int mask_ret; + int i; + + if(ip_conf->family != AF_INET) + { + ret = RET_INPUTERR; + } + + ASSERT_RET(ret); + + memset(&ifreq, 0, sizeof(struct ifreq)); + + rpc_log_info("get interface %s info\n", ip_conf->ifname); + + strncpy(ifreq.ifr_name, ip_conf->ifname, sizeof(ifreq.ifr_name)); + + ret = if_ioctl(SIOCGIFADDR, (caddr_t)&ifreq, code); + ASSERT_RET(ret); + + + ip_conf->prefix = ((struct sockaddr_in *)&(ifreq.ifr_addr))->sin_addr; + + memset(&ifreq, 0, sizeof(ifreq)); + strncpy(ifreq.ifr_name, ip_conf->ifname, sizeof(ifreq.ifr_name)); + + ret = if_ioctl(SIOCGIFNETMASK, (caddr_t)&ifreq, &mask_ret); + ASSERT_RET_NO(ret); + + addr = ( struct sockaddr_in * )&(ifreq.ifr_netmask); + ip_conf->prefixlen = ip_masklen(addr->sin_addr); + + return ret; +} + +ret_code ip_config_set_chk(uint source,uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + ip_config_t *ip_conf; + + ip_conf = (ip_config_t *)input; + + if(input_len < sizeof(ip_config_t) + || strlen(ip_conf->ifname) == 0) + { + ret = RET_INPUTERR; + } + + if (ipv4_martian(&ip_conf->prefix)) + { + ret = RET_IPINVALID; + } + + ASSERT_RET(ret); + + if(ip_conf->prefixlen == 0 || ip_conf->prefixlen > IPV4_MAX_PREFIXLEN) + { + ip_conf->prefixlen = IPV4_DEFAULT_PREFIXLEN; + } + + return RET_OK; +} + +ret_code ip_config_get_chk(uint source,uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + ip_config_t *ip_conf; + + ip_conf = (ip_config_t *)input; + + if(input_len < sizeof(ip_config_t) + || strlen(ip_conf->ifname) == 0 ) + { + ret = RET_INPUTERR; + } + + return ret; +} + +ret_code ip_config_getall_chk(uint source,uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + + if(*output_len < MAX_IF_NUM * sizeof(ip_config_t)) + { + ret = RET_INPUTERR; + } + + return ret; +} + + +ret_code ip_config_chk(uint source,uint config_type, + pointer input, int input_len, + pointer output, int *output_len) + +{ + ret_code ret = RET_OK; + int code = 0; + + switch (config_type) + { + case CM_CONFIG_SET: + case CM_CONFIG_DEL: + ret = ip_config_set_chk(source, config_type, + input, input_len, + output, output_len); + break; + case CM_CONFIG_GET: + ret = ip_config_get_chk(source, config_type, + input, input_len, + output, output_len); + break; + case CM_CONFIG_GET_ALL: + ret = ip_config_getall_chk(source, config_type, + input, input_len, + output, output_len); + break; + default: + ret = RET_NOTSUPPORT; + } + + RET_ERR_FORMART(ret, code, output, *output_len); + return ret; +} + +ret_code ip_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ip_config_t *ip_conf; + ret_code ret = RET_OK; + int code; + + ip_conf = (ip_config_t *)input; + + if(config_type == CM_CONFIG_DEL) + { + ip_conf->prefix.s_addr = 0; + ip_conf->prefixlen = IPV4_DEFAULT_PREFIXLEN; + } + + rpc_log_info("config type is %d, if %s ip %s prefixlen %d\n", + config_type, ip_conf->ifname, + inet_ntoa(ip_conf->prefix), + ip_conf->prefixlen); + + ret = if_set_prefix(ip_conf, &code); + + RET_ERR_FORMART(ret, code, output, *output_len); + ASSERT_RET(ret); + + ip_save_file(ip_conf, config_type); + + return RET_OK; +} + +ret_code ip_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len) +{ + ip_config_t *ip_conf; + ret_code ret = RET_OK; + int code; + + ip_conf = (ip_config_t *)input; + + ret = if_get_prefix(ip_conf, &code); + + RET_ERR_FORMART(ret, code, output, *output_len); + ASSERT_RET(ret); + + memcpy(output, ip_conf, sizeof(ip_config_t)); + *output_len = sizeof(ip_config_t); + + return ret; +} + +ret_code ip_config_get_all(uint source, uint64 config_id, + pointer output, short *single_len, + int *output_len) +{ + ip_config_t ip_conf; + ret_code ret; + int count = 0; + int code; + + rpc_log_info("ip_config_get_all\r\n"); + + ret = if_get_prefix_all((ip_config_t *)output, &count, &code); + + RET_ERR_FORMART(ret, code, output, *output_len); + ASSERT_RET(ret); + + *single_len = sizeof(ip_config_t); + *output_len = count * sizeof(ip_config_t); + + return RET_OK; + +} diff --git a/Platform/user/configm/ipconfig/parsefile.c b/Platform/user/configm/config-server/ipconfig/parsefile.c old mode 100755 new mode 100644 similarity index 96% rename from Platform/user/configm/ipconfig/parsefile.c rename to Platform/user/configm/config-server/ipconfig/parsefile.c index 04c0285d7..c5bdd3b3f --- a/Platform/user/configm/ipconfig/parsefile.c +++ b/Platform/user/configm/config-server/ipconfig/parsefile.c @@ -1,452 +1,452 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "parsefile.h" - -#if 0 -/* - *从配置文件中读取相应的值 - *输入参数:1,配置文件路径 2,匹配标记 3,输出存储空间 - *并且排除了空行,“=”前后无内容,无“=”的情况 - */ -void read_config(char *conf_path,char *conf_name,char *config_buff) -{ - char config_linebuf[256]; - char line_name[40]; - char exchange_buf[256]; - char *config_sign = "="; - char *leave_line; - FILE *f; - f = fopen(conf_path,"r"); - if(f == NULL) - { - printf("OPEN CONFIG FALID/n"); - return 0; - } - fseek(f,0,SEEK_SET); - while(fgets(config_linebuf,256,f) != NULL) - { - if(strlen(config_linebuf) < 3) //判断是否是空行 - { - continue; - } - if (config_linebuf[strlen(config_linebuf)-1] == 10) //去除最后一位是/n的情况 - { - - memset(exchange_buf,0,sizeof(exchange_buf)); - strncpy(exchange_buf,config_linebuf,strlen(config_linebuf)-1); - memset(config_linebuf,0,sizeof(config_linebuf)); - strcpy(config_linebuf,exchange_buf); - } - memset(line_name,0,sizeof(line_name)); - leave_line = strstr(config_linebuf,config_sign); - if(leave_line == NULL) //去除无"="的情况 - { - continue; - } - int leave_num = leave_line - config_linebuf; - strncpy(line_name,config_linebuf,leave_num); - if(strcmp(line_name,conf_name) ==0) - { - strncpy(config_buff,config_linebuf+(leave_num+1),strlen(config_linebuf)-leave_num-1); - break; - } - if(fgetc(f)==EOF) - { - break; - } - fseek(f,-1,SEEK_CUR); - memset(config_linebuf,0,sizeof(config_linebuf)); - } - fclose(f); -} - -/* - *添加修改文件(当配置文件中存在标记字段,则进行修改,若不存在则进行添加) - * - *输入参数:1,配置文件路径 2,匹配标记 3,替换或添加的内容 - * - */ -void add_set_config(char *conf_path,char *conf_name,char *config_buff) -{ - - char config_linebuf[256]; - char line_name[40]; - char *config_sign = "="; - char *leave_line; - int alter_sign = 0; - - FILE *f; - f = fopen(conf_path,"r+"); - if(f == NULL) - { - printf("OPEN CONFIG FALID/n"); - return 0; - } - fseek(f,0,SEEK_END); - long congig_lenth = ftell(f); - int configbuf_lenth = strlen(config_buff); - configbuf_lenth = configbuf_lenth + 5; - char sum_buf[congig_lenth+configbuf_lenth]; - memset(sum_buf,0,sizeof(sum_buf)); - fseek(f,0,SEEK_SET); - while(fgets(config_linebuf,256,f) != NULL) - { - if(strlen(config_linebuf) < 3) //判断是否是空行 - { - strcat(sum_buf,config_linebuf); - continue; - } - leave_line = NULL; - leave_line = strstr(config_linebuf,config_sign); - if(leave_line == NULL) //去除无"="的情况 - { - strcat(sum_buf,config_linebuf); - continue; - } - int leave_num = leave_line - config_linebuf; - memset(line_name,0,sizeof(line_name)); - strncpy(line_name,config_linebuf,leave_num); - if(strcmp(line_name,conf_name) ==0) - { - strcat(sum_buf,config_buff); - strcat(sum_buf,"/n"); - alter_sign = 1; - } - else - { - strcat(sum_buf,config_linebuf); - } - if(fgetc(f)==EOF) - { - break; - } - fseek(f,-1,SEEK_CUR); - memset(config_linebuf,0,sizeof(config_linebuf)); - } - if(alter_sign == 0) - { - strcat(sum_buf,config_buff); - strcat(sum_buf,"/n"); - } - printf("---sum_buf---->%s<----------/n",sum_buf); - remove(conf_path); - fclose(f); - FILE *fp; - fp = fopen(conf_path,"w+"); - if(fp == NULL) - { - printf("OPEN CONFIG FALID/n"); - return 2; - } - fseek(fp,0,SEEK_SET); - fputs(sum_buf,fp); - fclose(fp); -} -/* - *删除配置文件内容 - * - *输入参数:1,匹配标记 - * - */ -void del_if_config(char *conf_name) -{ - char *conf_path = "/etc/network/interface"; - char config_linebuf[256]; - char line_name[40]; - char *config_sign = "="; - char *leave_line; - - FILE *f; - f = fopen(conf_path,"r+"); - if(f == NULL) - { - printf("OPEN CONFIG FALID/n"); - return 0; - } - fseek(f,0,SEEK_END); - long congig_lenth = ftell(f); - char sum_buf[congig_lenth+2]; - memset(sum_buf,0,sizeof(sum_buf)); - fseek(f,0,SEEK_SET); - while(fgets(config_linebuf,256,f) != NULL) - { - if(strlen(config_linebuf) < 3) //判断是否是空行 - { - strcat(sum_buf,config_linebuf); - continue; - } - leave_line = NULL; - leave_line = strstr(config_linebuf,config_sign); - if(leave_line == NULL) //去除无"="的情况 - { - strcat(sum_buf,config_linebuf); - continue; - } - int leave_num = leave_line - config_linebuf; - memset(line_name,0,sizeof(line_name)); - strncpy(line_name,config_linebuf,leave_num); - if(strcmp(line_name,conf_name) !=0) - { - strcat(sum_buf,config_linebuf); - } - - if(fgetc(f)==EOF) - { - break; - } - fseek(f,-1,SEEK_CUR); - memset(config_linebuf,0,sizeof(config_linebuf)); - } - printf("---sum_buf---->%s<----------/n",sum_buf); - remove(conf_path); - fclose(f); - FILE *fp; - fp = fopen(conf_path,"w+"); - if(fp == NULL) - { - printf("OPEN CONFIG FALID/n"); - return 2; - } - fseek(fp,0,SEEK_SET); - fputs(sum_buf,fp); - fclose(fp); -} - -#endif - -/* - *添加修改文件(当配置文件中存在标记字段,则进行修改,若不存在则进行添加) - * - *输入参数:1,接口名 2,匹配标记 3,替换或添加的内容 - * - */ - -void set_if_config(char *if_name, char *conf_name, char *conf_buff) -{ - char *conf_path = IFCONFIG_PATH; - char config_linebuf[IF_BUFF_LEN]; - char static_name[IF_BUFF_LEN] = {0}; - char iface_str[IF_BUFF_LEN] = {0}; - char iface_str2[IF_BUFF_LEN] = {0}; - char auto_str[IF_BUFF_LEN] = {0}; - char *config_sign = "iface"; - char *leave_line; - char *leave_line2; - int alter_sign = 0; - - FILE *f; - f = fopen(conf_path,"r+"); - if(f == NULL) - { - printf("OPEN CONFIG FALID\n"); - return; - } - - fseek(f,0,SEEK_END); - - long congig_lenth = ftell(f); - int configbuf_lenth = strlen(conf_buff); - configbuf_lenth = configbuf_lenth + 5; - char sum_buf[congig_lenth+configbuf_lenth]; - - memset(sum_buf,0,sizeof(sum_buf)); - fseek(f,0,SEEK_SET); - - sprintf(iface_str, "%s %s", config_sign, if_name); - sprintf(iface_str2, "auto %s", if_name); - sprintf(static_name, "iface %s inet static\n", if_name); - - leave_line = NULL; - leave_line2 = NULL; - - while(fgets(config_linebuf,IF_BUFF_LEN,f) != NULL) - { - if(strlen(config_linebuf) < 3 || leave_line2) //判断是否是空行 - { - strcat(sum_buf,config_linebuf); - continue; - } - - if(leave_line == NULL) - { - leave_line = strstr(config_linebuf, iface_str); - - if(leave_line) - { - strcat(sum_buf,static_name); - } - else - { - strcat(sum_buf,config_linebuf); - } - } - /*leave_line != NULL && leave_line2 !=NULL*/ - else if((leave_line2 = strstr(config_linebuf,iface_str)) - || (leave_line2 = strstr(config_linebuf,iface_str2))) - { - if(alter_sign == 0) - { - strcat(sum_buf,conf_buff); - alter_sign = 1; - } - - strcat(sum_buf,config_linebuf); - } - /*leave_line != NULL && leave_line2 == NULL*/ - else - { - if(strstr(config_linebuf,conf_name) != NULL) - { - strcat(sum_buf,conf_buff); - alter_sign = 1; - } - else - { - strcat(sum_buf,config_linebuf); - } - } - - if(fgetc(f)==EOF) - { - break; - } - fseek(f,-1,SEEK_CUR); - - memset(config_linebuf,0,sizeof(config_linebuf)); - } - - if(leave_line == NULL) - { - sprintf(auto_str, "auto %s\n", if_name); - sprintf(auto_str, "%s", static_name); - strcat(sum_buf,auto_str); - } - - if(alter_sign == 0) - { - //strcat(sum_buf,"\n"); - strcat(sum_buf, conf_buff); - } - - printf("---sum_buf---->%s<----------/n",sum_buf); - remove(conf_path); - fclose(f); - - FILE *fp; - fp = fopen(conf_path,"w+"); - if(fp == NULL) - { - printf("OPEN CONFIG FALID/n"); - return; - } - fseek(fp,0,SEEK_SET); - fputs(sum_buf,fp); - fclose(fp); - - return; -} - - -/* - *删除配置文件内容 - * - *输入参数:1,匹配标记 - * - */ -void del_if_config(char *if_name, char *conf_buff) -{ - char *conf_path = IFCONFIG_PATH; - char config_linebuf[IF_BUFF_LEN]; - char iface_str[IF_BUFF_LEN] = {0}; - char iface_str2[IF_BUFF_LEN] = {0}; - char auto_str[IF_BUFF_LEN] = {0}; - char *config_sign = "iface"; - char *leave_line; - char *leave_line2; - int alter_sign = 0; - - FILE *f; - f = fopen(conf_path,"r+"); - if(f == NULL) - { - printf("OPEN CONFIG FALID\n"); - return; - } - - fseek(f,0,SEEK_END); - - long congig_lenth = ftell(f); - int configbuf_lenth = strlen(conf_buff); - configbuf_lenth = configbuf_lenth + 5; - char sum_buf[congig_lenth+configbuf_lenth]; - - memset(sum_buf,0,sizeof(sum_buf)); - fseek(f,0,SEEK_SET); - - sprintf(iface_str, "%s %s", config_sign, if_name); - sprintf(iface_str2, "auto %s", if_name); - - leave_line = NULL; - leave_line2 = NULL; - - while(fgets(config_linebuf,256,f) != NULL) - { - if(strlen(config_linebuf) < 3 || leave_line2) //判断是否是空行 - { - strcat(sum_buf,config_linebuf); - continue; - } - - if(leave_line == NULL) - { - leave_line = strstr(config_linebuf, iface_str); - strcat(sum_buf,config_linebuf); - } - /*leave_line != NULL && leave_line2 !=NULL*/ - else if((leave_line2 = strstr(config_linebuf,iface_str)) - || (leave_line2 = strstr(config_linebuf,iface_str2))) - { - - strcat(sum_buf,config_linebuf); - } - /*leave_line != NULL && leave_line2 == NULL*/ - else - { - if(strstr(config_linebuf,conf_buff) == NULL) - { - strcat(sum_buf,config_linebuf); - } - } - - if(fgetc(f)==EOF) - { - break; - } - fseek(f,-1,SEEK_CUR); - - memset(config_linebuf,0,sizeof(config_linebuf)); - } - - printf("---sum_buf---->%s<----------/n",sum_buf); - remove(conf_path); - fclose(f); - - FILE *fp; - fp = fopen(conf_path,"w+"); - if(fp == NULL) - { - printf("OPEN CONFIG FALID/n"); - return; - } - fseek(fp,0,SEEK_SET); - fputs(sum_buf,fp); - fclose(fp); -} - - +#include +#include +#include +#include +#include +#include +#include + +#include "parsefile.h" + +#if 0 +/* + *从配置文件中读取相应的值 + *输入参数:1,配置文件路径 2,匹配标记 3,输出存储空间 + *并且排除了空行,“=”前后无内容,无“=”的情况 + */ +void read_config(char *conf_path,char *conf_name,char *config_buff) +{ + char config_linebuf[256]; + char line_name[40]; + char exchange_buf[256]; + char *config_sign = "="; + char *leave_line; + FILE *f; + f = fopen(conf_path,"r"); + if(f == NULL) + { + printf("OPEN CONFIG FALID/n"); + return 0; + } + fseek(f,0,SEEK_SET); + while(fgets(config_linebuf,256,f) != NULL) + { + if(strlen(config_linebuf) < 3) //判断是否是空行 + { + continue; + } + if (config_linebuf[strlen(config_linebuf)-1] == 10) //去除最后一位是/n的情况 + { + + memset(exchange_buf,0,sizeof(exchange_buf)); + strncpy(exchange_buf,config_linebuf,strlen(config_linebuf)-1); + memset(config_linebuf,0,sizeof(config_linebuf)); + strcpy(config_linebuf,exchange_buf); + } + memset(line_name,0,sizeof(line_name)); + leave_line = strstr(config_linebuf,config_sign); + if(leave_line == NULL) //去除无"="的情况 + { + continue; + } + int leave_num = leave_line - config_linebuf; + strncpy(line_name,config_linebuf,leave_num); + if(strcmp(line_name,conf_name) ==0) + { + strncpy(config_buff,config_linebuf+(leave_num+1),strlen(config_linebuf)-leave_num-1); + break; + } + if(fgetc(f)==EOF) + { + break; + } + fseek(f,-1,SEEK_CUR); + memset(config_linebuf,0,sizeof(config_linebuf)); + } + fclose(f); +} + +/* + *添加修改文件(当配置文件中存在标记字段,则进行修改,若不存在则进行添加) + * + *输入参数:1,配置文件路径 2,匹配标记 3,替换或添加的内容 + * + */ +void add_set_config(char *conf_path,char *conf_name,char *config_buff) +{ + + char config_linebuf[256]; + char line_name[40]; + char *config_sign = "="; + char *leave_line; + int alter_sign = 0; + + FILE *f; + f = fopen(conf_path,"r+"); + if(f == NULL) + { + printf("OPEN CONFIG FALID/n"); + return 0; + } + fseek(f,0,SEEK_END); + long congig_lenth = ftell(f); + int configbuf_lenth = strlen(config_buff); + configbuf_lenth = configbuf_lenth + 5; + char sum_buf[congig_lenth+configbuf_lenth]; + memset(sum_buf,0,sizeof(sum_buf)); + fseek(f,0,SEEK_SET); + while(fgets(config_linebuf,256,f) != NULL) + { + if(strlen(config_linebuf) < 3) //判断是否是空行 + { + strcat(sum_buf,config_linebuf); + continue; + } + leave_line = NULL; + leave_line = strstr(config_linebuf,config_sign); + if(leave_line == NULL) //去除无"="的情况 + { + strcat(sum_buf,config_linebuf); + continue; + } + int leave_num = leave_line - config_linebuf; + memset(line_name,0,sizeof(line_name)); + strncpy(line_name,config_linebuf,leave_num); + if(strcmp(line_name,conf_name) ==0) + { + strcat(sum_buf,config_buff); + strcat(sum_buf,"/n"); + alter_sign = 1; + } + else + { + strcat(sum_buf,config_linebuf); + } + if(fgetc(f)==EOF) + { + break; + } + fseek(f,-1,SEEK_CUR); + memset(config_linebuf,0,sizeof(config_linebuf)); + } + if(alter_sign == 0) + { + strcat(sum_buf,config_buff); + strcat(sum_buf,"/n"); + } + printf("---sum_buf---->%s<----------/n",sum_buf); + remove(conf_path); + fclose(f); + FILE *fp; + fp = fopen(conf_path,"w+"); + if(fp == NULL) + { + printf("OPEN CONFIG FALID/n"); + return 2; + } + fseek(fp,0,SEEK_SET); + fputs(sum_buf,fp); + fclose(fp); +} +/* + *删除配置文件内容 + * + *输入参数:1,匹配标记 + * + */ +void del_if_config(char *conf_name) +{ + char *conf_path = "/etc/network/interface"; + char config_linebuf[256]; + char line_name[40]; + char *config_sign = "="; + char *leave_line; + + FILE *f; + f = fopen(conf_path,"r+"); + if(f == NULL) + { + printf("OPEN CONFIG FALID/n"); + return 0; + } + fseek(f,0,SEEK_END); + long congig_lenth = ftell(f); + char sum_buf[congig_lenth+2]; + memset(sum_buf,0,sizeof(sum_buf)); + fseek(f,0,SEEK_SET); + while(fgets(config_linebuf,256,f) != NULL) + { + if(strlen(config_linebuf) < 3) //判断是否是空行 + { + strcat(sum_buf,config_linebuf); + continue; + } + leave_line = NULL; + leave_line = strstr(config_linebuf,config_sign); + if(leave_line == NULL) //去除无"="的情况 + { + strcat(sum_buf,config_linebuf); + continue; + } + int leave_num = leave_line - config_linebuf; + memset(line_name,0,sizeof(line_name)); + strncpy(line_name,config_linebuf,leave_num); + if(strcmp(line_name,conf_name) !=0) + { + strcat(sum_buf,config_linebuf); + } + + if(fgetc(f)==EOF) + { + break; + } + fseek(f,-1,SEEK_CUR); + memset(config_linebuf,0,sizeof(config_linebuf)); + } + printf("---sum_buf---->%s<----------/n",sum_buf); + remove(conf_path); + fclose(f); + FILE *fp; + fp = fopen(conf_path,"w+"); + if(fp == NULL) + { + printf("OPEN CONFIG FALID/n"); + return 2; + } + fseek(fp,0,SEEK_SET); + fputs(sum_buf,fp); + fclose(fp); +} + +#endif + +/* + *添加修改文件(当配置文件中存在标记字段,则进行修改,若不存在则进行添加) + * + *输入参数:1,接口名 2,匹配标记 3,替换或添加的内容 + * + */ + +void set_if_config(char *if_name, char *conf_name, char *conf_buff) +{ + char *conf_path = IFCONFIG_PATH; + char config_linebuf[IF_BUFF_LEN]; + char static_name[IF_BUFF_LEN] = {0}; + char iface_str[IF_BUFF_LEN] = {0}; + char iface_str2[IF_BUFF_LEN] = {0}; + char auto_str[IF_BUFF_LEN] = {0}; + char *config_sign = "iface"; + char *leave_line; + char *leave_line2; + int alter_sign = 0; + + FILE *f; + f = fopen(conf_path,"r+"); + if(f == NULL) + { + printf("OPEN CONFIG FALID\n"); + return; + } + + fseek(f,0,SEEK_END); + + long congig_lenth = ftell(f); + int configbuf_lenth = strlen(conf_buff); + configbuf_lenth = configbuf_lenth + 5; + char sum_buf[congig_lenth+configbuf_lenth]; + + memset(sum_buf,0,sizeof(sum_buf)); + fseek(f,0,SEEK_SET); + + sprintf(iface_str, "%s %s", config_sign, if_name); + sprintf(iface_str2, "auto %s", if_name); + sprintf(static_name, "iface %s inet static\n", if_name); + + leave_line = NULL; + leave_line2 = NULL; + + while(fgets(config_linebuf,IF_BUFF_LEN,f) != NULL) + { + if(strlen(config_linebuf) < 3 || leave_line2) //判断是否是空行 + { + strcat(sum_buf,config_linebuf); + continue; + } + + if(leave_line == NULL) + { + leave_line = strstr(config_linebuf, iface_str); + + if(leave_line) + { + strcat(sum_buf,static_name); + } + else + { + strcat(sum_buf,config_linebuf); + } + } + /*leave_line != NULL && leave_line2 !=NULL*/ + else if((leave_line2 = strstr(config_linebuf,iface_str)) + || (leave_line2 = strstr(config_linebuf,iface_str2))) + { + if(alter_sign == 0) + { + strcat(sum_buf,conf_buff); + alter_sign = 1; + } + + strcat(sum_buf,config_linebuf); + } + /*leave_line != NULL && leave_line2 == NULL*/ + else + { + if(strstr(config_linebuf,conf_name) != NULL) + { + strcat(sum_buf,conf_buff); + alter_sign = 1; + } + else + { + strcat(sum_buf,config_linebuf); + } + } + + if(fgetc(f)==EOF) + { + break; + } + fseek(f,-1,SEEK_CUR); + + memset(config_linebuf,0,sizeof(config_linebuf)); + } + + if(leave_line == NULL) + { + sprintf(auto_str, "auto %s\n", if_name); + sprintf(auto_str, "%s", static_name); + strcat(sum_buf,auto_str); + } + + if(alter_sign == 0) + { + //strcat(sum_buf,"\n"); + strcat(sum_buf, conf_buff); + } + + printf("---sum_buf---->%s<----------/n",sum_buf); + remove(conf_path); + fclose(f); + + FILE *fp; + fp = fopen(conf_path,"w+"); + if(fp == NULL) + { + printf("OPEN CONFIG FALID/n"); + return; + } + fseek(fp,0,SEEK_SET); + fputs(sum_buf,fp); + fclose(fp); + + return; +} + + +/* + *删除配置文件内容 + * + *输入参数:1,匹配标记 + * + */ +void del_if_config(char *if_name, char *conf_buff) +{ + char *conf_path = IFCONFIG_PATH; + char config_linebuf[IF_BUFF_LEN]; + char iface_str[IF_BUFF_LEN] = {0}; + char iface_str2[IF_BUFF_LEN] = {0}; + char auto_str[IF_BUFF_LEN] = {0}; + char *config_sign = "iface"; + char *leave_line; + char *leave_line2; + int alter_sign = 0; + + FILE *f; + f = fopen(conf_path,"r+"); + if(f == NULL) + { + printf("OPEN CONFIG FALID\n"); + return; + } + + fseek(f,0,SEEK_END); + + long congig_lenth = ftell(f); + int configbuf_lenth = strlen(conf_buff); + configbuf_lenth = configbuf_lenth + 5; + char sum_buf[congig_lenth+configbuf_lenth]; + + memset(sum_buf,0,sizeof(sum_buf)); + fseek(f,0,SEEK_SET); + + sprintf(iface_str, "%s %s", config_sign, if_name); + sprintf(iface_str2, "auto %s", if_name); + + leave_line = NULL; + leave_line2 = NULL; + + while(fgets(config_linebuf,256,f) != NULL) + { + if(strlen(config_linebuf) < 3 || leave_line2) //判断是否是空行 + { + strcat(sum_buf,config_linebuf); + continue; + } + + if(leave_line == NULL) + { + leave_line = strstr(config_linebuf, iface_str); + strcat(sum_buf,config_linebuf); + } + /*leave_line != NULL && leave_line2 !=NULL*/ + else if((leave_line2 = strstr(config_linebuf,iface_str)) + || (leave_line2 = strstr(config_linebuf,iface_str2))) + { + + strcat(sum_buf,config_linebuf); + } + /*leave_line != NULL && leave_line2 == NULL*/ + else + { + if(strstr(config_linebuf,conf_buff) == NULL) + { + strcat(sum_buf,config_linebuf); + } + } + + if(fgetc(f)==EOF) + { + break; + } + fseek(f,-1,SEEK_CUR); + + memset(config_linebuf,0,sizeof(config_linebuf)); + } + + printf("---sum_buf---->%s<----------/n",sum_buf); + remove(conf_path); + fclose(f); + + FILE *fp; + fp = fopen(conf_path,"w+"); + if(fp == NULL) + { + printf("OPEN CONFIG FALID/n"); + return; + } + fseek(fp,0,SEEK_SET); + fputs(sum_buf,fp); + fclose(fp); +} + + diff --git a/Platform/user/configm/configclient.c b/Platform/user/configm/config-test/comfigtest.c old mode 100755 new mode 100644 similarity index 76% rename from Platform/user/configm/configclient.c rename to Platform/user/configm/config-test/comfigtest.c index b54e9ed32..a5d5f4b88 --- a/Platform/user/configm/configclient.c +++ b/Platform/user/configm/config-test/comfigtest.c @@ -1,257 +1,146 @@ -#include -#include -#include "rpc.h" -#include "configm.h" -#include "ipconfig.h" - -rpc_client *config_client = NULL; - -rpc_client *config_client_get() -{ - if(config_client == NULL) - { - config_client = rpc_client_connect_ex("ConfigManger#0"); - if(config_client == NULL) - { - rpc_log_error("connect ConfigManger#0 error\n"); - return NULL; - } - } - - return config_client; -} - -ret_code config_construct_msg(uint config_type, uint64 config_id, - char* config_data, int config_len, - config_msg_t **config_msg, int *msg_len) -{ - config_msg_t *pconfig_msg; - - if(config_data == NULL) - { - return RET_NULLP; - } - - *msg_len = sizeof(config_msg_t) + config_len; - pconfig_msg= (config_msg_t *) malloc(*msg_len); - if(pconfig_msg == NULL) - { - return RET_NOMEM; - } - - pconfig_msg->source = CONFIG_FROM_WEB; - pconfig_msg->config_type = config_type; - pconfig_msg->config_id = config_id; - - memcpy(pconfig_msg->config_buff, config_data, config_len); - *config_msg = pconfig_msg; - - return RET_OK; -} - -ret_code config_destroy_msg(config_msg_t *config_msg, int msg_len) -{ - if(config_msg) - { - memset(config_msg, 0, msg_len); - free(config_msg); - } -} - - -ret_code web_config_exec_sync(uint config_type, uint64 config_id, - char* config_data, int config_len, - char**output, int *output_len) -{ - rpc_client * client = config_client_get(); - ret_code code = RET_OK ; - config_msg_t *config_msg; - int msg_len = 0; - - if(client == NULL) - { - return RET_ERR; - } - - code = config_construct_msg(config_type, config_id, config_data, - config_len, &config_msg, &msg_len); - ASSERT_RET(code); - - code = rpc_client_call(client, "ConfigManger#0", "cm_config_process", - config_msg, msg_len, (pointer)output, output_len); - ASSERT_RET_NO(code); - - config_destroy_msg(config_msg, msg_len); - - return code; -} - -ret_code web_config_exec_async(uint config_type, uint64 config_id, - char* config_data, int config_len, - rpc_callback callback, pointer data) - { - rpc_client * client = config_client_get(); - config_msg_t *config_msg; - int msg_len = 0; - - ret_code ret = RET_OK ; - - if(client == NULL) - { - return RET_ERR; - } - - ret = config_construct_msg(config_type, config_id, config_data, - config_len, &config_msg, &msg_len); - ASSERT_RET(ret); - - ret = rpc_client_call_async(client, "ConfigManger#0", "cm_config_process", - config_msg, msg_len, callback, data); - - config_destroy_msg(config_msg, msg_len); - - return ret; - } - - -//big data test -//async -//sync io - -static char* test_data = "给李彦宏先生的一封信 (2011-03-26 04:33:37)转载" - "标签: 杂谈 " - "您好,李彦宏先生。" - "上周我和出版社的朋友沈浩波先生去山东的纸厂销毁已经印刷完毕的一百多万册《独唱团》第二期,三百多吨的纸和工业垃圾一起进了化浆炉。" - "几百万的损失对您来说可能是个小数目,但是对一个出版公司来说几乎等于一年白干了,那还得是国内数得上数的大出版公司。" - "这个行业就是这么可怜的,一个一百多人的企业一年的利润还不如在上海炒一套公寓,而且分分钟要背上“黑心书商”的骂名。" - "但是沈浩波一直很高兴,因为他说和百度的谈判终于有眉目了,百度答应派人来商量百度文库的事情,李承鹏,慕容雪村,路金波,彭浩翔," - "都是文化行业里数一数二的畅销书作家,导演和出版商,大家都很激动,准备了好几个晚上各种资料。" - "于是昨天开始谈判了,您派来几个高傲的中层,始终不承认百度文库有任何的侵权行为。" - "你们不认为那包含了几乎全中国所有最新最旧图书的279万份文档是侵权,而是网民自己上传给大家共享的。" - "你这里只是一个平台。我觉得其实我们不用讨论平台不平台,侵权不侵权这个问题了,您其实什么都心知肚明。" - "您在美国有那么长时间的生活经历,现在您的妻子和女儿也都在美国,您一定知道如果百度开了一个叫百度美国的搜索引擎," - "然后把全美国所有的作家的书和所有音乐人的音乐都放在百度美国上面免费共享会是什么样的一个结果。" - "您不会这么做,您也不会和美国人去谈什么这只是一个平台,和我没关系,都是网民自己干的,互联网的精神是共享。" - "因为您知道这事儿只有在现在的中国才能成立。而且您也知道谁能欺负,谁不能欺负,您看,您就没有做一个百度影剧院,让大家共享共享最新的电影电视剧。" - "您也许不太了解出版行业,我可以简单的给您介绍一下。1999年,十二年前,我的书卖18元一本,2011年,卖25元一本,很多读者还都嫌贵。" - "您知道这十二年间,纸张,人工,物流都涨了多少倍,但出版商一直不敢提太多价,因为怕被骂,文化人脸皮都薄。" - "一本25元的书,一般作者的版税是百分之8,可以赚2块钱,其中还要交三毛钱左右的税,也就是可以赚一块七。" - "一本书如果卖两万本,已经算是畅销,一个作家两年能写一本,一本可以赚三万四,一年赚一万七,如果他光写书," - "他得不吃不喝写一百年才够在大城市的城郊买套像样的两居室。假设一本书卖10元,里面的构成是这样的,作家赚1元,印刷成本2元多," - "出版社赚1元多,书店赚5元。有点名气的作家出去签售做宣传,住的都是三星的酒店,来回能坐上飞机已经算不错了。" - "出行标准一定还不如你们的低级别员工。最近几年我已经不出席任何宣传签售活动了,但是在2004年前," - "我至少做过几十场各个城市的宣传活动,而在那个时候,我已经是行业里的畅销书作家,我从没住到过一次300以上的酒店," - "有的时候和出版社陪同的几个人得在机场等好几个小时,因为打折的那班飞机得傍晚起飞,而多住半天酒店得加钱。" - "这个行业就是这么窘迫的。这个行业里最顶尖的企业家,年收入就几百万。出版业和互联网业,本是两个级别相当的行业," - "你们是用几百亿身价和私人飞机豪华游艇来算企业家身价的,我们这个行业里的企业家们,我几乎没见过一个出行坐头等舱的。" - "我们倒不是眼红你们有钱,我们只是觉得,你们都那么富有了,为何还要一分钱都不肯花从我们这个行业里强行获得免费的知识版权。" - "音乐人还可以靠商演赚钱,而你让作家和出版行业如何生存。也许你说,传统出版会始终消亡,但那不代表出版行业就该如此的不体面。" - "而且文艺作品和出版行业是不会消亡的,只是换了一个介质,一开始它们被画在墙上,后来刻在竹子上,现在有书,未来也许有别的科技," - "但版权是永远存在的。我写这些并不是代表这个行业向你们哭穷," - "但这的确中国唯一一个拥有很多的资源与生活息息相关却没有什么财富可言的行业。尤其在盗版和侵权的伤害之下。" - "我们也不是要求你们把百度文库关了,我们只是希望百度文库可以主动对版权进行保护,等未来数字阅读成熟以后," - "说不定百度文库还能成为中国作家生活保障的来源,而不是现在这样,成为行业公敌众矢之的。因为没有永远的敌人,也没有永远的利益。" - "我在2006年还和磨铁图书的沈浩波先生打过笔仗,为了现代诗互相骂的不可开交,而现在却是朋友和合作伙伴。" - "百度文库完全可以成为造福作家的基地,而不是埋葬作家的墓地。" - "在我们这个行业里,我算是生活得好的。李彦宏先生,也许我们一样,虽不畏惧,但并不喜欢这些是非恩怨,我喜欢晒晒太阳玩泥巴," - "你喜欢晒晒太阳种种花。无论你怎么共享我的知识版权,至少咱俩还能一起晒晒太阳,毕竟我赛车还能养活自己和家庭," - "但对于大部分作家来说,他们理应靠着传统的出版和数字出版过着体面的生活。也许他们未必能够有自己的院子晒太阳。" - "您的产品会把他们赶回阴暗的小屋里为了生活不停的写,而您头上的太阳也并不会因此大一些。" - "中国那么多的写作者被迫为百度无偿的提供了无数的知识版权和流量,他们不光没有来找过百度麻烦或者要求百度分点红," - "甚至还要承受百度拥趸们的侮辱以及百度员工谈判时的蔑视。您现在是中国排名第一的企业家,作为企业家的表率," - "您必须对百度文库给出版行业带来的伤害有所表态。倘若百度文库始终不肯退一步,那我可以多走几步,也许在不远的某天," - "在您北京的办公室里往楼下望去,您可以看见我。" - " 祝 您的女儿为她的父亲感到骄傲" - " 韩寒" - "2011年 3月26日" - "end"; - -void ip_set_callback(ret_code code, pointer output, - int output_len, pointer data) -{ - int i; - - //rpc_log_info("call %s i is %d len is %d", rpc_code_format(code), *(int *)data, output_len); - - - if(code != RET_OK) - { - rpc_log_info("ERROR: %s", (char *)output); - } - -} - -int main(int argc, char **argv) -{ - - rpc_client *client = rpc_client_connect_ex("ConfigManger#0"); - ip_config_t ip_conf; - ip_config_t *pip_conf; - int i = 0, result; - ret_code code; - char* output = NULL; - int output_len; - - strcpy(ip_conf.ifname, "ens39"); - ip_conf.prefix.s_addr = inet_addr("192.168.80.1"); - ip_conf.family=AF_INET; - ip_conf.prefixlen = 24; - - code = rpc_client_call(client, "ConfigManger#0", "test_big_data", test_data, - strlen(test_data), NULL, NULL); - ASSERT_RET_NO(code); - - /*code = web_config_exec_sync(CM_CONFIG_ADD, IPCONFIG_V4, - (pointer)&ip_conf, sizeof(ip_conf), &output, &output_len); - - rpc_log_info("call CM_CONFIG_ADD %s: %s\n", rpc_code_format(code), output);*/ - - code = web_config_exec_sync(CM_CONFIG_DEL, IPCONFIG_V4, - (pointer)&ip_conf, sizeof(ip_conf), &output, &output_len); - rpc_log_info("call CM_CONFIG_DEL %s: %s\n", rpc_code_format(code), output); - - /*code = web_config_exec_sync(CM_CONFIG_SET, IPCONFIG_V4, - (pointer)&ip_conf, sizeof(ip_conf), &output, &output_len); - rpc_log_info("call CM_CONFIG_SET %s: %s\n", rpc_code_format(code), output);*/ - - code = web_config_exec_sync(CM_CONFIG_GET_ALL, IPCONFIG_V4, - (pointer)&ip_conf, sizeof(ip_conf), &output, &output_len); - - rpc_log_info("call CM_CONFIG_GET_ALL %s: %s\n", rpc_code_format(code), output); - - for(i = 0; i < output_len / sizeof(ip_config_t); i++) - { - pip_conf = (ip_config_t *)output; - printf("ifname is %s\n",pip_conf[i].ifname); - printf("family is %d\n", pip_conf[i].family); - printf("ip is %s\n", inet_ntoa(pip_conf[i].prefix)); - printf("prefix len is %d\n", pip_conf[i].prefixlen); - } - - code = web_config_exec_sync(CM_CONFIG_GET, IPCONFIG_V4, - (pointer)&ip_conf, sizeof(ip_conf), &output, &output_len); - rpc_log_info("call %s: %s\n", rpc_code_format(code), output); - - /* code = web_config_exec_async(CM_CONFIG_ADD, IPCONFIG_V4, (pointer)&ip_conf, sizeof(ip_conf), ip_set_callback, &i); - rpc_log_info("call CM_CONFIG_ADD: %s\n", rpc_code_format(code)); - - code = web_config_exec_async(CM_CONFIG_DEL, IPCONFIG_V4, (pointer)&ip_conf, sizeof(ip_conf), ip_set_callback, &i); - rpc_log_info("call CM_CONFIG_DEL: %s\n", rpc_code_format(code)); - - code = web_config_exec_async(CM_CONFIG_SET, IPCONFIG_V4, (pointer)&ip_conf, sizeof(ip_conf), ip_set_callback, &i); - rpc_log_info("call CM_CONFIG_SET: %s\n", rpc_code_format(code)); - - code = web_config_exec_async(CM_CONFIG_GET, IPCONFIG_V4, (pointer)&ip_conf, sizeof(ip_conf), ip_set_callback, &i); - rpc_log_info("call CM_CONFIG_GET: %s\n", rpc_code_format(code)); - - code = web_config_exec_async(CM_CONFIG_GET_ALL, IPCONFIG_V4, (pointer)&ip_conf, sizeof(ip_conf), ip_set_callback, &i); - rpc_log_info("call CM_CONFIG_GET_ALL: %s\n", rpc_code_format(code));*/ - - - printf("test ok\n"); - for (;;) { - rpc_sleep(1000); - } - return EXIT_SUCCESS; -} - +#include +#include +#include "rpc.h" +#include "configm.h" +#include "ipconfig.h" + +//big data test +//async +//sync io + +static char* test_data = "给李彦宏先生的一封信 (2011-03-26 04:33:37)转载" + "标签: 杂谈 " + "您好,李彦宏先生。" + "上周我和出版社的朋友沈浩波先生去山东的纸厂销毁已经印刷完毕的一百多万册《独唱团》第二期,三百多吨的纸和工业垃圾一起进了化浆炉。" + "几百万的损失对您来说可能是个小数目,但是对一个出版公司来说几乎等于一年白干了,那还得是国内数得上数的大出版公司。" + "这个行业就是这么可怜的,一个一百多人的企业一年的利润还不如在上海炒一套公寓,而且分分钟要背上“黑心书商”的骂名。" + "但是沈浩波一直很高兴,因为他说和百度的谈判终于有眉目了,百度答应派人来商量百度文库的事情,李承鹏,慕容雪村,路金波,彭浩翔," + "都是文化行业里数一数二的畅销书作家,导演和出版商,大家都很激动,准备了好几个晚上各种资料。" + "于是昨天开始谈判了,您派来几个高傲的中层,始终不承认百度文库有任何的侵权行为。" + "你们不认为那包含了几乎全中国所有最新最旧图书的279万份文档是侵权,而是网民自己上传给大家共享的。" + "你这里只是一个平台。我觉得其实我们不用讨论平台不平台,侵权不侵权这个问题了,您其实什么都心知肚明。" + "您在美国有那么长时间的生活经历,现在您的妻子和女儿也都在美国,您一定知道如果百度开了一个叫百度美国的搜索引擎," + "然后把全美国所有的作家的书和所有音乐人的音乐都放在百度美国上面免费共享会是什么样的一个结果。" + "您不会这么做,您也不会和美国人去谈什么这只是一个平台,和我没关系,都是网民自己干的,互联网的精神是共享。" + "因为您知道这事儿只有在现在的中国才能成立。而且您也知道谁能欺负,谁不能欺负,您看,您就没有做一个百度影剧院,让大家共享共享最新的电影电视剧。" + "您也许不太了解出版行业,我可以简单的给您介绍一下。1999年,十二年前,我的书卖18元一本,2011年,卖25元一本,很多读者还都嫌贵。" + "您知道这十二年间,纸张,人工,物流都涨了多少倍,但出版商一直不敢提太多价,因为怕被骂,文化人脸皮都薄。" + "一本25元的书,一般作者的版税是百分之8,可以赚2块钱,其中还要交三毛钱左右的税,也就是可以赚一块七。" + "一本书如果卖两万本,已经算是畅销,一个作家两年能写一本,一本可以赚三万四,一年赚一万七,如果他光写书," + "他得不吃不喝写一百年才够在大城市的城郊买套像样的两居室。假设一本书卖10元,里面的构成是这样的,作家赚1元,印刷成本2元多," + "出版社赚1元多,书店赚5元。有点名气的作家出去签售做宣传,住的都是三星的酒店,来回能坐上飞机已经算不错了。" + "出行标准一定还不如你们的低级别员工。最近几年我已经不出席任何宣传签售活动了,但是在2004年前," + "我至少做过几十场各个城市的宣传活动,而在那个时候,我已经是行业里的畅销书作家,我从没住到过一次300以上的酒店," + "有的时候和出版社陪同的几个人得在机场等好几个小时,因为打折的那班飞机得傍晚起飞,而多住半天酒店得加钱。" + "这个行业就是这么窘迫的。这个行业里最顶尖的企业家,年收入就几百万。出版业和互联网业,本是两个级别相当的行业," + "你们是用几百亿身价和私人飞机豪华游艇来算企业家身价的,我们这个行业里的企业家们,我几乎没见过一个出行坐头等舱的。" + "我们倒不是眼红你们有钱,我们只是觉得,你们都那么富有了,为何还要一分钱都不肯花从我们这个行业里强行获得免费的知识版权。" + "音乐人还可以靠商演赚钱,而你让作家和出版行业如何生存。也许你说,传统出版会始终消亡,但那不代表出版行业就该如此的不体面。" + "而且文艺作品和出版行业是不会消亡的,只是换了一个介质,一开始它们被画在墙上,后来刻在竹子上,现在有书,未来也许有别的科技," + "但版权是永远存在的。我写这些并不是代表这个行业向你们哭穷," + "但这的确中国唯一一个拥有很多的资源与生活息息相关却没有什么财富可言的行业。尤其在盗版和侵权的伤害之下。" + "我们也不是要求你们把百度文库关了,我们只是希望百度文库可以主动对版权进行保护,等未来数字阅读成熟以后," + "说不定百度文库还能成为中国作家生活保障的来源,而不是现在这样,成为行业公敌众矢之的。因为没有永远的敌人,也没有永远的利益。" + "我在2006年还和磨铁图书的沈浩波先生打过笔仗,为了现代诗互相骂的不可开交,而现在却是朋友和合作伙伴。" + "百度文库完全可以成为造福作家的基地,而不是埋葬作家的墓地。" + "在我们这个行业里,我算是生活得好的。李彦宏先生,也许我们一样,虽不畏惧,但并不喜欢这些是非恩怨,我喜欢晒晒太阳玩泥巴," + "你喜欢晒晒太阳种种花。无论你怎么共享我的知识版权,至少咱俩还能一起晒晒太阳,毕竟我赛车还能养活自己和家庭," + "但对于大部分作家来说,他们理应靠着传统的出版和数字出版过着体面的生活。也许他们未必能够有自己的院子晒太阳。" + "您的产品会把他们赶回阴暗的小屋里为了生活不停的写,而您头上的太阳也并不会因此大一些。" + "中国那么多的写作者被迫为百度无偿的提供了无数的知识版权和流量,他们不光没有来找过百度麻烦或者要求百度分点红," + "甚至还要承受百度拥趸们的侮辱以及百度员工谈判时的蔑视。您现在是中国排名第一的企业家,作为企业家的表率," + "您必须对百度文库给出版行业带来的伤害有所表态。倘若百度文库始终不肯退一步,那我可以多走几步,也许在不远的某天," + "在您北京的办公室里往楼下望去,您可以看见我。" + " 祝 您的女儿为她的父亲感到骄傲" + " 韩寒" + "2011年 3月26日" + "end"; + +void ip_set_callback(ret_code code, pointer output, + int output_len, pointer data) +{ + int i; + + //rpc_log_info("call %s i is %d len is %d", rpc_code_format(code), *(int *)data, output_len); + + + if(code != RET_OK) + { + rpc_log_info("ERROR: %s", (char *)output); + } + +} + +int main(int argc, char **argv) +{ + + rpc_client *client = rpc_client_connect_ex("ConfigManger#0"); + ip_config_t ip_conf; + ip_config_t *pip_conf; + int i = 0, result; + ret_code code; + char* output = NULL; + int output_len; + + strcpy(ip_conf.ifname, "ens39"); + ip_conf.prefix.s_addr = inet_addr("192.168.80.1"); + ip_conf.family=AF_INET; + ip_conf.prefixlen = 24; + + code = rpc_client_call(client, "ConfigManger#0", "test_big_data", test_data, + strlen(test_data), NULL, NULL); + ASSERT_RET_NO(code); + + /*code = web_config_exec_sync(CM_CONFIG_ADD, IPCONFIG_V4, + (pointer)&ip_conf, sizeof(ip_conf), &output, &output_len); + + rpc_log_info("call CM_CONFIG_ADD %s: %s\n", rpc_code_format(code), output);*/ + + code = web_config_exec_sync(CM_CONFIG_DEL, IPCONFIG_V4, + (pointer)&ip_conf, sizeof(ip_conf), &output, &output_len); + rpc_log_info("call CM_CONFIG_DEL %s: %s\n", rpc_code_format(code), output); + + /*code = web_config_exec_sync(CM_CONFIG_SET, IPCONFIG_V4, + (pointer)&ip_conf, sizeof(ip_conf), &output, &output_len); + rpc_log_info("call CM_CONFIG_SET %s: %s\n", rpc_code_format(code), output);*/ + + code = web_config_exec_sync(CM_CONFIG_GET_ALL, IPCONFIG_V4, + (pointer)&ip_conf, sizeof(ip_conf), &output, &output_len); + + rpc_log_info("call CM_CONFIG_GET_ALL %s: %s\n", rpc_code_format(code), output); + + for(i = 0; i < output_len / sizeof(ip_config_t); i++) + { + pip_conf = (ip_config_t *)output; + printf("ifname is %s\n",pip_conf[i].ifname); + printf("family is %d\n", pip_conf[i].family); + printf("ip is %s\n", inet_ntoa(pip_conf[i].prefix)); + printf("prefix len is %d\n", pip_conf[i].prefixlen); + } + + code = web_config_exec_sync(CM_CONFIG_GET, IPCONFIG_V4, + (pointer)&ip_conf, sizeof(ip_conf), &output, &output_len); + rpc_log_info("call %s: %s\n", rpc_code_format(code), output); + + /* code = web_config_exec_async(CM_CONFIG_ADD, IPCONFIG_V4, (pointer)&ip_conf, sizeof(ip_conf), ip_set_callback, &i); + rpc_log_info("call CM_CONFIG_ADD: %s\n", rpc_code_format(code)); + + code = web_config_exec_async(CM_CONFIG_DEL, IPCONFIG_V4, (pointer)&ip_conf, sizeof(ip_conf), ip_set_callback, &i); + rpc_log_info("call CM_CONFIG_DEL: %s\n", rpc_code_format(code)); + + code = web_config_exec_async(CM_CONFIG_SET, IPCONFIG_V4, (pointer)&ip_conf, sizeof(ip_conf), ip_set_callback, &i); + rpc_log_info("call CM_CONFIG_SET: %s\n", rpc_code_format(code)); + + code = web_config_exec_async(CM_CONFIG_GET, IPCONFIG_V4, (pointer)&ip_conf, sizeof(ip_conf), ip_set_callback, &i); + rpc_log_info("call CM_CONFIG_GET: %s\n", rpc_code_format(code)); + + code = web_config_exec_async(CM_CONFIG_GET_ALL, IPCONFIG_V4, (pointer)&ip_conf, sizeof(ip_conf), ip_set_callback, &i); + rpc_log_info("call CM_CONFIG_GET_ALL: %s\n", rpc_code_format(code));*/ + + + printf("test ok\n"); + for (;;) { + rpc_sleep(1000); + } + return EXIT_SUCCESS; +}