# Conflicts:
#	Platform/user/configm/config-server/include/configm.h
This commit is contained in:
zhanglianghy 2019-08-27 11:50:47 +08:00
commit 2b50b3b181
23 changed files with 4364 additions and 222 deletions

View File

@ -23,14 +23,15 @@
#define LOG_CONFIG_MODULE 0x00000004 #define LOG_CONFIG_MODULE 0x00000004
/*vlan config */
#define VLAN_CONFIG_MODULE 0x00000005
/*DHCP CONFIG*/
#define DHCP_CONFIG_MODULE 0x00000006
/*nat config */ /*nat config */
#define NAT_CONFIG_MODULE 0x00000008 #define NAT_CONFIG_MODULE 0x00000008
/*vlan config */
#define VLAN_CONFIG_MODULE 0x00000005
/*DHCP CONFIG*/
#define DHCP_CONFIG_MODULE 0x00000006
/************************* 模块定义结束 **********************/ /************************* 模块定义结束 **********************/
/************************ config id定义 **********************/ /************************ config id定义 **********************/
@ -54,15 +55,19 @@
#define LOG_CONFIG_REMOTE_DEL_HOST (uint64)((uint64)LOG_CONFIG_MODULE<<32|4) #define LOG_CONFIG_REMOTE_DEL_HOST (uint64)((uint64)LOG_CONFIG_MODULE<<32|4)
#define LOG_CONFIG_REMOTE_LEVEL (uint64)((uint64)LOG_CONFIG_MODULE<<32|5) #define LOG_CONFIG_REMOTE_LEVEL (uint64)((uint64)LOG_CONFIG_MODULE<<32|5)
#define LOG_CONFIG_FILE (uint64)((uint64)LOG_CONFIG_MODULE<<32|6) #define LOG_CONFIG_FILE (uint64)((uint64)LOG_CONFIG_MODULE<<32|6)
#define LOG_CONFIG_REMOTE_GET_HOST (uint64)((uint64)LOG_CONFIG_MODULE<<32|7)
#define VLAN_CONFIG (uint64)((uint64)VLAN_CONFIG_MODULE<<32|1)
#define DHCP_SUBNET_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|1)
#define DHCP_HOST_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|2)
#define DHCP_SHARED_NETWORK_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|3)
#define DHCP_RELAY_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|4)
#define DHCP_CLIENT_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|5)
#define DHCP_DHCPD_LEASE (uint64)((uint64)DHCP_CONFIG_MODULE<<32|6)
#define NAT4_CONFIG (uint64)((uint64)NAT_CONFIG_MODULE<<32|1) #define NAT4_CONFIG (uint64)((uint64)NAT_CONFIG_MODULE<<32|1)
#define DHCP_SUBNET_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|1)
#define DHCP_HOST_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|2)
#define DHCP_SHARED_NETWORK_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|3)
#define DHCP_RELAY_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|4)
#define DHCP_CLIENT_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|5)
#define DHCP_DHCPD_LEASE (uint64)((uint64)DHCP_CONFIG_MODULE<<32|6)
/************************ config id定义 end**********************/ /************************ config id定义 end**********************/

View File

@ -41,6 +41,14 @@ typedef struct _log_remote_level {
u8 level; u8 level;
} log_remote_level_t; } log_remote_level_t;
typedef struct _log_remote_host_x {
log_rfc_t rfc;
char host[LOG_HOST_SZ];
u16 port;
u8 level;
} log_remote_host_x_t;
typedef enum { typedef enum {
LOG_UNCOMPRESS = 0, LOG_UNCOMPRESS = 0,
LOG_COMPRESS LOG_COMPRESS

View File

@ -106,7 +106,9 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
/**create netconf node with connect device information */ /**create netconf node with connect device information */
final NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder(); final NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
netconfNodeBuilder.setHost(new Host(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress(connectDevice.getIp().getIpv4Address()))) netconfNodeBuilder.setHost(new Host(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress(connectDevice.getIp().getIpv4Address())))
.setPort(new PortNumber(connectDevice.getPort().getValue())); .setPort(new PortNumber(connectDevice.getPort().getValue()))
.setTcpOnly(false)
.setSchemaless(false);
String username = connectDevice.getUserName(); String username = connectDevice.getUserName();
String password = connectDevice.getPassword(); String password = connectDevice.getPassword();
if (Strings.isNullOrEmpty(username) || Strings.isNullOrEmpty(password)) { if (Strings.isNullOrEmpty(username) || Strings.isNullOrEmpty(password)) {
@ -210,7 +212,11 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
} else if (rootNode.getModificationType() == DELETE) { } else if (rootNode.getModificationType() == DELETE) {
LOG.info("onDataTreeChanged - Toaster config with path {} was deleted: old Toaster: {}", LOG.info("onDataTreeChanged - Toaster config with path {} was deleted: old Toaster: {}",
change.getRootPath().getRootIdentifier(), rootNode.getDataBefore()); change.getRootPath().getRootIdentifier(), rootNode.getDataBefore());
disConnectDevice(device.getId().getValue()); ConnectorInfo oldEntry = rootNode.getDataBefore();
oldEntry.getRemoteDevices().forEach( d -> {
disConnectDevice(d.getId().getValue());
});
} }
} }
} }
@ -302,6 +308,7 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
InstanceIdentifier<CpuInfos> iid = InstanceIdentifier<CpuInfos> iid =
InstanceIdentifier.create(Devm.class).child(CpuInfos.class); InstanceIdentifier.create(Devm.class).child(CpuInfos.class);
Optional<CpuInfos> cupInfos; Optional<CpuInfos> cupInfos;
LOG.info("开始向华为设备获取cpu信息");
try { try {
// Read from a transaction is asynchronous, but a simple // Read from a transaction is asynchronous, but a simple
// get/checkedGet makes the call synchronous // get/checkedGet makes the call synchronous

View File

@ -37,8 +37,8 @@ COMMON_SRCS = configserver.c \
user_manager_config/user_recover_config.c user_manager_config/user_group_config.c user_manager_config/user_account_config.c user_manager_config/usermanager-server/array_index.c \ user_manager_config/user_recover_config.c user_manager_config/user_group_config.c user_manager_config/user_account_config.c user_manager_config/usermanager-server/array_index.c \
user_manager_config/usermanager-server/user_group.c user_manager_config/usermanager-server/user_mod.c user_manager_config/usermanager-server/user.c \ user_manager_config/usermanager-server/user_group.c user_manager_config/usermanager-server/user_mod.c user_manager_config/usermanager-server/user.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 \ 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 nat_config/natconfig.c \
vlan_config/vlan_config.c \
# MRS Board Source Files # MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS) PLAT_LINUX_SRCS = $(COMMON_SRCS)

View File

@ -12,6 +12,7 @@
#include "../web_config/authfree.h" #include "../web_config/authfree.h"
#include "../web_config/auth_parameters.h" #include "../web_config/auth_parameters.h"
#include "natconfig.h" #include "natconfig.h"
#include "vlan_config.h"
#define RET_CODE_LEN 16 #define RET_CODE_LEN 16
#define RET_MSG_LEN 128 #define RET_MSG_LEN 128
@ -25,7 +26,11 @@
{ \ { \
LOG_CONFIG_MODULE, \ LOG_CONFIG_MODULE, \
log_config_init \ log_config_init \
} \ }, \
{ \
VLAN_CONFIG_MODULE, \
vlan_config_init \
} \
} }
/* /*
@ -128,8 +133,8 @@
FALSE, \ FALSE, \
log_console_config_chk, \ log_console_config_chk, \
log_console_config_proc, \ log_console_config_proc, \
NULL, \ log_console_config_get, \
NULL \ log_console_config_getall \
},\ },\
{\ {\
LOG_CONFIG_MONITOR, \ LOG_CONFIG_MONITOR, \
@ -137,8 +142,8 @@
FALSE, \ FALSE, \
log_monitor_config_chk, \ log_monitor_config_chk, \
log_monitor_config_proc, \ log_monitor_config_proc, \
NULL, \ log_monitor_config_get, \
NULL \ log_monitor_config_getall \
},\ },\
{\ {\
LOG_CONFIG_REMOTE_ADD_HOST, \ LOG_CONFIG_REMOTE_ADD_HOST, \
@ -159,13 +164,13 @@
NULL \ NULL \
},\ },\
{\ {\
LOG_CONFIG_REMOTE_LEVEL, \ LOG_CONFIG_REMOTE_LEVEL, \
CONFIG_FROM_WEB, \ CONFIG_FROM_WEB, \
FALSE, \ FALSE, \
log_remote_level_config_chk, \ log_remote_level_config_chk, \
log_remote_level_config_proc, \ log_remote_level_config_proc, \
NULL, \ log_remote_level_config_get, \
NULL \ log_remote_level_config_getall \
},\ },\
{\ {\
LOG_CONFIG_FILE, \ LOG_CONFIG_FILE, \
@ -173,8 +178,17 @@
FALSE, \ FALSE, \
log_file_config_chk, \ log_file_config_chk, \
log_file_config_proc, \ log_file_config_proc, \
NULL, \ log_file_config_get, \
NULL \ log_file_config_getall \
},\
{\
LOG_CONFIG_REMOTE_GET_HOST, \
CONFIG_FROM_WEB, \
FALSE, \
log_remote_host_x_config_chk, \
NULL, \
log_remote_host_x_config_get, \
log_remote_host_x_config_getall \
},\ },\
{\ {\
NAT4_CONFIG, \ NAT4_CONFIG, \
@ -184,6 +198,15 @@
nat_config_proc, \ nat_config_proc, \
NULL, \ NULL, \
nat_config_get_all \ nat_config_get_all \
},\
{\
VLAN_CONFIG, \
CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \
FALSE, \
vlan_config_chk, \
vlan_config_proc, \
vlan_config_get, \
vlan_config_get_all \
}\ }\
} }

View File

@ -40,5 +40,32 @@ ret_code log_file_config_chk(uint source, uint *config_type,
ret_code log_file_config_proc(uint source, uint config_type, ret_code log_file_config_proc(uint source, uint config_type,
pointer input, int input_len, pointer input, int input_len,
pointer output, int *output_len); pointer output, int *output_len);
ret_code log_file_config_get(uint source,
pointer input, int input_len,
pointer output, int *output_len);
ret_code log_file_config_getall(uint source,
pointer output, int *output_len);
ret_code log_remote_level_config_get(uint source,
pointer input, int input_len,
pointer output, int *output_len);
ret_code log_remote_level_config_getall(uint source,
pointer output, int *output_len);
ret_code log_console_config_get(uint source,
pointer input, int input_len,
pointer output, int *output_len);
ret_code log_console_config_getall(uint source,
pointer output, int *output_len);
ret_code log_monitor_config_get(uint source,
pointer input, int input_len,
pointer output, int *output_len);
ret_code log_monitor_config_getall(uint source,
pointer output, int *output_len);
ret_code log_remote_host_x_config_chk(uint source, uint *config_type,
pointer input, int *input_len,
pointer output, int *output_len);
ret_code log_remote_host_x_config_get(uint source,
pointer input, int input_len,
pointer output, int *output_len);
ret_code log_remote_host_x_config_getall(uint source,
pointer output, int *output_len);
#endif #endif

View File

@ -0,0 +1,191 @@
#ifndef VLAN_CONFIG_H_
#define VLAN_CONFIG_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <cjson/cJSON.h>
#include "configm.h"
#include "rpc.h"
#include "parsefile.h"
#include "netconfig.h"
#define MAX_VLAN 4096/* 0用户识别帧优先级,4095保留,共4094个 */
#define VID_MIN 2 /* vid范围2-4094 */
#define VID_MAX MAX_VLAN - 2 /* vid范围2-4094 */
#define MAX_INTERFACES 20 /* 最大接口数目 */
#define IF_NAME_LEN 20 /* 接口名最大长度 */
#define BR_VLAN_NAME_LEN 50 /* br-vlan的名字最大长度 */
#define SYSCALL_BUF_LEN 100
#define BITSPERWORD 32
#define SHIFT 5
#define MASK 0x1F
#define FILE_BUF_LINE 100
#define EACH_PORT_MAX_VLAN_NUM 5
#define ERR_DATA_LEN 500
#define VLAN_TEST_ADD_PATH "/home/eric/code/test/eric/vlan/conf/vlan_test.conf.add"
#define VLAN_TEST_DEL_PATH "/home/eric/code/test/eric/vlan/conf/vlan_test.conf.del"
#define xfree(X) \
do{ \
if(X){ \
free(X); \
X = NULL; \
} \
}while(0);
/* 操作类型ADD、MOD、DEL */
typedef enum{
OP_NONE,
OP_ADD,
OP_MOD,
OP_DEL,
}operation_type;
/* 链路类型none、access、trunk */
typedef enum{
LINK_TYPE_NONE,
LINK_TYPE_ACCESS,
LINK_TYPE_TRUNK,
LINK_TYPE_HYBIRD,
LINK_TYPE_UNKNOWN,
}interface_link_attr;
/* 接口模型1 桥接; 2 路由桥接; 3 路由 */
typedef enum{
INTERFACE_MODEL_FORWARD,
INTERFACE_MODEL_ROUTE_FORWARD,
INTERFACE_MODEL_ROUTE,
}interface_model;
/* 接口网络类型LAN、WAN */
typedef enum{
IF_TYPE_LAN,
IF_TYPE_WAN,
}interface_network_type;
/* 接口VLAN信息 */
typedef struct interface_vlan_info_t{
interface_link_attr attr;
operation_type op_type;//操作类型ADD、DEL、MOD
interface_model if_model;//接口模型
int vidcnt;//vid数量
int vlan_bitmap[MAX_VLAN/BITSPERWORD];
char if_name[IF_NAME_LEN];
}interface_vlan_info;
/* vlan个数100、200、300、400、500、600、700、800、900、1000 */
#define ADD_100 "/home/eric/code/test/eric/vlan/conf/conf.add.100"
#define ADD_100_200 "/home/eric/code/test/eric/vlan/conf/conf.add.100_200"
#define ADD_100_1000 "/home/eric/code/test/eric/vlan/conf/conf.add.100_1000"
#define DEL_100 "/home/eric/code/test/eric/vlan/conf/conf.add.100"
#define DEL_100_200 "/home/eric/code/test/eric/vlan/conf/conf.add.100_200"
#define DEL_100_1000 "/home/eric/code/test/eric/vlan/conf/conf.add.100_1000"
/* void */
void vlan_bitmap_set(int *v, int i);
void vlan_bitmap_clear(int *v, int i);
void interface_vlan_init(void);
void vlan_set_struct_stub(interface_vlan_info *if_vlan_info, operation_type op_type, int *if_vlnum);
void vlan_config_stub(operation_type op_type);
void vlan_config_format_json_stub(int *interface);
void vlan_config_get_all_stub(uint source);
void vlan_config_get_stub(void);
void vlan_config_proc_test(operation_type op_type);
void vlan_config_init(void);
void vlan_config_stub1(void);
/* int */
int vlan_bitmap_check(int *v, int i);
int if_vlan2subif(char *if_name, int vid, char *subif);
int br_if_num_stub(char *br_name, operation_type op_type);
int br_if_num(char *br_name);
int create_br_stub(char *br_vlname);
int create_br(char *br_vlname);
int br_addif_stub(char *br_name, char *if_name);
int br_addif(char *br_name, char *if_name);
int interface_br_stub(char *if_name, char *br_name);
int interface_br(char *if_name, char *br_name);
int del_br_stub(char *br_name);
int del_br(char *br_name);
int add_bridge_vlan_access(char *if_name, int vid);
int check_config(char *if_name);
int add_interface_vlan_access(char *if_name, int *vlan_bitmap, interface_model if_model);
int add_bridge_vlan_trunk(char *if_name, int *vlan_bitmap);
int add_bridge_vlan_trunk(char *if_name, int *vlan_bitmap);
int del_bridge_vlan_access(char *if_name);
int del_bridge_vlan_access(char *if_name);
int del_bridge_vlan_trunk(char *if_name);
int get_ifnode_from_global(char *if_name);
int find_insert_ifnode_from_global(char *if_name);
int get_old_interface_attr(char *if_name, interface_link_attr *attr);
int if_model_is_change(interface_model old, interface_model new);
/* typedefine */
interface_link_attr get_attr_from_ifname(char *if_name);
interface_network_type lan_or_wan_stub(char *if_name);
interface_network_type lan_or_wan(char *if_name);
interface_model get_old_interface_model(char *if_name, interface_model *if_model);
interface_model which_interface_model(char *if_name);
/* ret_code */
ret_code add_interface_vlan_trunk(char *if_name, int *vlan_bitmap, interface_model if_model);
ret_code add_interface_vlan(interface_vlan_info *if_vlan_info);
ret_code del_vlan_sub_interface(char *if_name, int *vlan_bitmap);
ret_code add_vlan_sub_interface(char *if_name, int *vlan_bitmap);
ret_code del_interface_vlan_trunk(char *if_name, int *vlan_bitmap, interface_model if_model);
ret_code refresh_if_vlan_info_op_add(interface_vlan_info *if_vlan_info);
ret_code refresh_if_vlan_info_op_del(interface_vlan_info *if_vlan_info);
ret_code vlan_save_conf_file_add(interface_vlan_info *if_vlan_info);
ret_code vlan_save_conf_file_del(interface_vlan_info *if_vlan_info);
ret_code vlan_save_conf_file(interface_vlan_info *if_vlan_info);
ret_code refresh_if_vlan_info(interface_vlan_info *if_vlan_info);
ret_code del_interface_vlan(interface_vlan_info *if_vlan_info);
ret_code vlan_config_json_parse(pointer input, interface_vlan_info *if_vlan_info, int *if_vlnum);
ret_code vlan_config_recovery(void);
ret_code vlan_get_json_parse(int *interface, pointer input);
ret_code vlan_config_format_json(int *interface, pointer output, int *output_len);
ret_code vlan_get_from_conf_file(int *interface);
ret_code vlan_config_get_all(uint source, pointer output, int *output_len);
ret_code vlan_config_json_parse_test(char *path, interface_vlan_info *if_vlan_info, int *if_vlnum);
ret_code vlan_config_json_parse_test1(operation_type op_type, interface_vlan_info *if_vlan_info, int *if_vlnum);
ret_code vlan_config_chk_test(operation_type op_type);
ret_code interface_vlan_check(interface_vlan_info *if_vlan_info);
ret_code if_attr_chk(interface_vlan_info *if_vlan_info);
ret_code op_type_chk(interface_vlan_info *if_vlan_info);
ret_code op_type_add_chk(interface_vlan_info *if_vlan_info);
ret_code op_type_del_chk(interface_vlan_info *if_vlan_info);
ret_code vid_value_chk(int vid);
ret_code vid_num_chk(char *if_name, operation_type op_type, int num);
ret_code if_name_chk(interface_vlan_info *if_vlan_info);
ret_code vlan_config_set_chk(uint source, pointer input);
ret_code vlan_operate_parse(pointer input, int *operate_type);
ret_code vlan_config_get_chk(uint source, pointer input);
ret_code vlan_config_get_all_chk(uint source, pointer input);
ret_code del_compose_if_vlan_info(char *if_name, interface_vlan_info *if_vlan_info);
ret_code interface_vlan_set(interface_vlan_info *if_vlan_info);
ret_code vlan_config_proc(uint source, uint config_type,
pointer input, int input_len,
pointer output, int *output_len);
ret_code vlan_config_get(uint source,
pointer input, int input_len,
pointer output, int *output_len);
ret_code vlan_config_chk(uint source,uint *config_type,
pointer input, int *input_len,
pointer output, int *output_len);
int del_interface_vlan_cb(BR_EVENT_TYPE event_type, br_event_t event_arg);
#endif /* VLAN_CONFIG_H_ */

View File

@ -1,3 +1,5 @@
#include <errno.h>
#include "log_config_cm.h" #include "log_config_cm.h"
#include "rpc_server.h" #include "rpc_server.h"
#include "rpc_client.h" #include "rpc_client.h"
@ -20,3 +22,220 @@ ret_code log_rpc_exec(char* service_name, char* method_name, pointer input, int
} }
/* 仅适用于以“key=value”方式保存的配置解析适用于log-sched.conf的解析 */
ret_code cm_log_get_keyvalue_from_file(const char *file_str, const char *key_str, char *value_str, int value_len)
{
ret_code ret = RET_ERR;
FILE *fp = NULL;
char *pos = NULL;
if (NULL == file_str || '\0' == file_str[0] || NULL == key_str || (NULL == value_str && value_len>0))
{
ULOG_ERR(g_log_h, "get log key-value from file: bad input");
return ret;
}
fp = fopen(file_str, "r");
if (NULL == fp) {
return ret;
}
if (fseek(fp, 0, SEEK_SET) == -1)
{
ULOG_ERR(g_log_h, "Seeknig config to begin is faiure:%s", strerror(errno));
fclose(fp);
return ret;
}
ssize_t n;
char *line = NULL;
while ((getline(&line, &n, fp)) != -1)
{
pos = strstr(line, key_str);
if (pos != NULL)
{
/* 跳过key与value间的"=" */
strcpy(value_str, pos+strlen(key_str)+1);
/* 滤除掉段尾的回车换行 */
if ('\n' == value_str[strlen(value_str)-1])
{
value_str[strlen(value_str)-1] = '\0';
}
ULOG_DEBUG(g_log_h, "get config %s:%s", key_str, value_str);
break;
}
}
ret = RET_OK;
if (NULL != fp)
{
fclose(fp);
}
return ret;
}
/* 检查的文件存在本函数返回1否则本函数返回0 */
int cm_log_check_file_is_exist(const char *file_str)
{
int is_exist = 0;
if (NULL == file_str || '\0' == file_str[0])
{
is_exist = 0;
}
else
{
/* 如果文件存在access返回0不存在access返回-1 */
is_exist = ((access(file_str, 0) == 0) ? 1 : 0);
}
return is_exist;
}
/* 适用于log-console.conf与log-pty.conf的解析 */
ret_code cm_log_get_module_from_file(const char *file_str, char *value_str, int value_len)
{
ret_code ret = RET_ERR;
FILE *fp = NULL;
char *pos = NULL;
char *pos2 = NULL;
size_t tmp_len = 0;
if (NULL == file_str || '\0' == file_str[0] || (NULL == value_str && value_len>0))
{
ULOG_ERR(g_log_h, "get log module from file: bad input");
return ret;
}
fp = fopen(file_str, "r");
if (NULL == fp)
{
return ret;
}
if (fseek(fp, 0, SEEK_SET) == -1)
{
ULOG_ERR(g_log_h, "Seeknig config to begin is faiure:%s", strerror(errno));
fclose(fp);
return ret;
}
ssize_t n;
char *line = NULL;
while ((getline(&line, &n, fp)) != -1)
{
pos = strstr(line, "\"[");
pos2 = strstr(line, "]\"");
tmp_len = pos2-pos-strlen("\"[");
if (pos != NULL && pos2 != NULL && tmp_len < value_len)
{
strncpy(value_str, pos+strlen("\"["), tmp_len);
value_str[tmp_len] = '\0';
ULOG_DEBUG(g_log_h, "get module: %s", value_str);
break;
}
}
ret = RET_OK;
if (NULL != fp)
{
fclose(fp);
}
return ret;
}
/* 适用于log-console.conf与log-pty.conf的解析 */
ret_code cm_log_get_level_from_file(const char *file_str, u8 *value)
{
ret_code ret = RET_ERR;
FILE *fp = NULL;
ssize_t n;
char *line = NULL;
char *sub_line = NULL;
char *pos = NULL;
char *pos2 = NULL;
size_t tmp_len = 0;
char tmp_str[128] = "";
int tmp_value;
if (NULL == file_str || '\0' == file_str[0] || NULL == value)
{
ULOG_ERR(g_log_h, "get log level from file: bad input");
}
fp = fopen(file_str, "r");
if (NULL == fp)
{
return ret;
}
if (fseek(fp, 0, SEEK_SET) == -1)
{
ULOG_ERR(g_log_h, "Seeknig config to begin is faiure:%s", strerror(errno));
fclose(fp);
return ret;
}
while ((getline(&line, &n, fp)) != -1)
{
pos = line;
if (NULL == strstr(pos, ".="))
{
continue;
}
while (1)
{
sub_line = strstr(pos, ".=");
if (NULL == sub_line)
{
break;
}
if (strlen(sub_line) <= strlen(".="))
{
break;
}
pos = sub_line+strlen(".=");
}
pos2 = strstr(pos, " ");
if (pos != NULL && pos2 != NULL)
{
tmp_len = pos2-pos;
strncpy(tmp_str, pos, tmp_len);
tmp_str[tmp_len] = '\0';
tmp_value = log_str_to_level(tmp_str);
if( -1 == tmp_value)
{
continue;
}
*value = (u8)tmp_value;
ULOG_DEBUG(g_log_h, "get level: %u", *value);
break;
}
}
ret = RET_OK;
if (NULL != fp)
{
fclose(fp);
}
return ret;
}

View File

@ -52,5 +52,31 @@ extern rpc_client *g_log_client;
extern ulog_t *g_log_h; extern ulog_t *g_log_h;
ret_code log_rpc_exec(char* service_name, char* method_name, pointer input, int input_len, int last_lenth); ret_code log_rpc_exec(char* service_name, char* method_name, pointer input, int input_len, int last_lenth);
ret_code cm_log_get_keyvalue_from_file(const char *file_str, const char *key_str, char *value_str, int value_len);
#endif int cm_log_check_file_is_exist(const char *file_str);
ret_code cm_log_get_module_from_file(const char *file_str, char *value_str, int value_len);
ret_code cm_log_get_level_from_file(const char *file_str, u8 *value);
#ifndef CM_LOG_CONF_CONSOLE_FILE
#define CM_LOG_CONF_CONSOLE_FILE "/etc/rsyslog.d/log-console.conf"
#endif
#ifndef CM_LOG_CONF_PTY_FILE
#define CM_LOG_CONF_PTY_FILE "/etc/rsyslog.d/log-pty.conf"
#endif
#ifndef CM_LOG_CONF_REMOTE_FILE
#define CM_LOG_CONF_REMOTE_FILE "/etc/rsyslog.d/log-remote.conf"
#endif
#ifndef CM_LOG_CONF_REMOTE_LEVEL_FILE
#define CM_LOG_CONF_REMOTE_LEVEL_FILE "/etc/log-sched.conf"
#endif
#ifndef CM_LOG_CONF_LOGFILE_FILE
#define CM_LOG_CONF_LOGFILE_FILE "/etc/log-sched.conf"
#endif
#endif

View File

@ -1,5 +1,7 @@
#include "log_config.h" #include "log_config.h"
#include "log_config_cm.h" #include "log_config_cm.h"
#include "rpc.h"
ret_code log_console_config_chk(uint source, uint *config_type, ret_code log_console_config_chk(uint source, uint *config_type,
pointer input, int *input_len, pointer input, int *input_len,
@ -14,3 +16,105 @@ ret_code log_console_config_proc(uint source, uint config_type,
{ {
return log_rpc_exec(SERIVCE_LOG_CONSOLE_NAME, CONF_LOG_CONSOLE_FUNC, input, input_len, sizeof(log_console_t)); return log_rpc_exec(SERIVCE_LOG_CONSOLE_NAME, CONF_LOG_CONSOLE_FUNC, input, input_len, sizeof(log_console_t));
} }
static ret_code cm_get_log_console_elems(log_console_t *log_console_conf, int *err_no)
{
char tmp_module[MAX_MODULE_NAME_SZ] = "";
u8 tmp_level = 0;
if (NULL == log_console_conf || NULL == err_no)
{
return RET_ERR;
}
memset(log_console_conf, 0, sizeof(*log_console_conf));
if (cm_log_check_file_is_exist(CM_LOG_CONF_CONSOLE_FILE) != 1)
{
log_console_conf->on = LOG_OFF;
}
else
{
log_console_conf->on = LOG_ON;
memset(tmp_module, 0, MAX_MODULE_NAME_SZ);
if (cm_log_get_module_from_file(CM_LOG_CONF_CONSOLE_FILE, tmp_module, MAX_MODULE_NAME_SZ) == 0)
{
strncpy(log_console_conf->module, tmp_module, MAX_MODULE_NAME_SZ);
}
if (cm_log_get_level_from_file(CM_LOG_CONF_CONSOLE_FILE, &tmp_level) == 0)
{
log_console_conf->level = tmp_level;
}
}
ULOG_DEBUG(g_log_h, "get log console :level=%u, is_on=%s, module=%s",
log_console_conf->level,
log_console_conf->on==LOG_ON?"on":"off",
log_console_conf->module);
return RET_OK;
}
static ret_code cm_log_console_format_json(log_console_t *log_console_conf,
pointer output, int *outlen)
{
char *json_log_file = NULL;
/* create Student JSON object */
s2j_create_json_obj(json_obj);
if (json_obj == NULL)
{
return RET_NOMEM;
}
s2j_json_set_basic_element(json_obj, log_console_conf, int, level);
s2j_json_set_basic_element(json_obj, log_console_conf, int, on);
s2j_json_set_basic_element(json_obj, log_console_conf, string, module);
json_log_file = cJSON_PrintUnformatted(json_obj);
*outlen = strlen(json_log_file) + 1;
memcpy(output, json_log_file, *outlen);
free(json_log_file);
cJSON_Delete(json_obj);
return RET_OK;
}
ret_code log_console_config_get(uint source,
pointer input, int input_len,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
log_console_t *log_console_conf = NULL;
int err_no = 0;
log_console_conf = (log_console_t *)input;
ret = cm_get_log_console_elems(log_console_conf, &err_no);
RET_ERR_FORMART(ret, err_no, output, *output_len);
ASSERT_RET(ret);
cm_log_console_format_json(log_console_conf, output, output_len);
return ret;
}
ret_code log_console_config_getall(uint source,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
log_console_t *log_console_conf = NULL;
int err_no = 0;
log_console_conf = rpc_new(log_console_t, 1);
ret = log_console_config_get(source, (pointer)log_console_conf, sizeof(log_console_t)*1, output, output_len);
rpc_free(log_console_conf);
return ret;
}

View File

@ -1,5 +1,6 @@
#include "log_config.h" #include "log_config.h"
#include "log_config_cm.h" #include "log_config_cm.h"
#include "rpc.h"
#define FILE_JSON(s, o) { \ #define FILE_JSON(s, o) { \
s2j_struct_get_basic_element(s, o, int, level); \ s2j_struct_get_basic_element(s, o, int, level); \
@ -9,6 +10,8 @@
S2J_STRUCT_GET_STRING_ELEMENT_N(s, o, del_over_size, MAX_U64_SZ); \ S2J_STRUCT_GET_STRING_ELEMENT_N(s, o, del_over_size, MAX_U64_SZ); \
} }
#define LOG_CONF_KEY_FILE_MAX_SIZE_STR "file.max_size"
ret_code log_file_config_chk(uint source, uint *config_type, ret_code log_file_config_chk(uint source, uint *config_type,
pointer input, int *input_len, pointer input, int *input_len,
pointer output, int *output_len) pointer output, int *output_len)
@ -24,3 +27,94 @@ ret_code log_file_config_proc(uint source, uint config_type,
} }
static ret_code cm_get_log_file_elems(log_file_t *log_file_conf, int *err_no)
{
char value[MAX_LINE_SZ] = "";
if (NULL == log_file_conf || NULL == err_no)
{
return RET_ERR;
}
memset(log_file_conf, 0, sizeof(*log_file_conf));
/* level、path、is_compress、del_over_days暂未实现 */
/* 日志文件大小上限del_over_size */
memset(value, 0, MAX_LINE_SZ);
if (cm_log_get_keyvalue_from_file(CM_LOG_CONF_LOGFILE_FILE, LOG_CONF_KEY_FILE_MAX_SIZE_STR, value, sizeof(value)) == 0)
{
if (strlen(value) < MAX_U64_SZ)
{
memset(log_file_conf->del_over_size, 0, MAX_U64_SZ);
strncpy(log_file_conf->del_over_size, value, strlen(value));
}
}
ULOG_DEBUG(g_log_h, "get log file del_over_size:%s", log_file_conf->del_over_size);
return RET_OK;
}
static ret_code cm_log_file_format_json(log_file_t *log_file_conf,
pointer output, int *outlen)
{
char *json_log_file = NULL;
/* create Student JSON object */
s2j_create_json_obj(json_obj);
if (json_obj == NULL)
{
return RET_NOMEM;
}
s2j_json_set_basic_element(json_obj, log_file_conf, int, level);
s2j_json_set_basic_element(json_obj, log_file_conf, string, path);
s2j_json_set_basic_element(json_obj, log_file_conf, int, is_compress);
s2j_json_set_basic_element(json_obj, log_file_conf, int, del_over_days);
s2j_json_set_basic_element(json_obj, log_file_conf, string, del_over_size);
json_log_file = cJSON_PrintUnformatted(json_obj);
*outlen = strlen(json_log_file) + 1;
memcpy(output, json_log_file, *outlen);
free(json_log_file);
cJSON_Delete(json_obj);
return RET_OK;
}
ret_code log_file_config_get(uint source,
pointer input, int input_len,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
log_file_t *log_file_conf = NULL;
int err_no = 0;
log_file_conf = (log_file_t *)input;
ret = cm_get_log_file_elems(log_file_conf, &err_no);
RET_ERR_FORMART(ret, err_no, output, *output_len);
ASSERT_RET(ret);
cm_log_file_format_json(log_file_conf, output, output_len);
return ret;
}
ret_code log_file_config_getall(uint source,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
log_file_t *log_file_conf = NULL;
int err_no = 0;
log_file_conf = rpc_new(log_file_t, 1);
ret = log_file_config_get(source, (pointer)log_file_conf, sizeof(log_file_t)*1, output, output_len);
rpc_free(log_file_conf);
return ret;
}

View File

@ -1,5 +1,6 @@
#include "log_config.h" #include "log_config.h"
#include "log_config_cm.h" #include "log_config_cm.h"
#include "rpc.h"
ret_code log_monitor_config_chk(uint source, uint *config_type, ret_code log_monitor_config_chk(uint source, uint *config_type,
pointer input, int *input_len, pointer input, int *input_len,
@ -15,3 +16,105 @@ ret_code log_monitor_config_proc(uint source, uint config_type,
return log_rpc_exec(SERVICE_LOG_PTY_NAME, CONF_LOG_PTY_FUNC, input, input_len, sizeof(log_console_t)); return log_rpc_exec(SERVICE_LOG_PTY_NAME, CONF_LOG_PTY_FUNC, input, input_len, sizeof(log_console_t));
} }
static ret_code cm_get_log_monitor_elems(log_console_t *log_console_conf, int *err_no)
{
char tmp_module[MAX_MODULE_NAME_SZ] = "";
u8 tmp_level = 0;
if (NULL == log_console_conf || NULL == err_no)
{
return RET_ERR;
}
memset(log_console_conf, 0, sizeof(*log_console_conf));
if (cm_log_check_file_is_exist(CM_LOG_CONF_PTY_FILE) != 1)
{
log_console_conf->on = LOG_OFF;
}
else
{
log_console_conf->on = LOG_ON;
memset(tmp_module, 0, MAX_MODULE_NAME_SZ);
if (cm_log_get_module_from_file(CM_LOG_CONF_PTY_FILE, tmp_module, MAX_MODULE_NAME_SZ) == 0)
{
strncpy(log_console_conf->module, tmp_module, MAX_MODULE_NAME_SZ);
}
if (cm_log_get_level_from_file(CM_LOG_CONF_PTY_FILE, &tmp_level) == 0)
{
log_console_conf->level = tmp_level;
}
}
ULOG_DEBUG(g_log_h, "get log monitor :level=%u, is_on=%s, module=%s",
log_console_conf->level,
log_console_conf->on==LOG_ON?"on":"off",
log_console_conf->module);
return RET_OK;
}
static ret_code cm_log_monitor_format_json(log_console_t *log_console_conf,
pointer output, int *outlen)
{
char *json_log_file = NULL;
/* create Student JSON object */
s2j_create_json_obj(json_obj);
if (json_obj == NULL)
{
return RET_NOMEM;
}
s2j_json_set_basic_element(json_obj, log_console_conf, int, level);
s2j_json_set_basic_element(json_obj, log_console_conf, int, on);
s2j_json_set_basic_element(json_obj, log_console_conf, string, module);
json_log_file = cJSON_PrintUnformatted(json_obj);
*outlen = strlen(json_log_file) + 1;
memcpy(output, json_log_file, *outlen);
free(json_log_file);
cJSON_Delete(json_obj);
return RET_OK;
}
ret_code log_monitor_config_get(uint source,
pointer input, int input_len,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
log_console_t *log_console_conf = NULL;
int err_no = 0;
log_console_conf = (log_console_t *)input;
ret = cm_get_log_monitor_elems(log_console_conf, &err_no);
RET_ERR_FORMART(ret, err_no, output, *output_len);
ASSERT_RET(ret);
cm_log_monitor_format_json(log_console_conf, output, output_len);
return ret;
}
ret_code log_monitor_config_getall(uint source,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
log_console_t *log_console_conf = NULL;
int err_no = 0;
log_console_conf = rpc_new(log_console_t, 1);
ret = log_monitor_config_get(source, (pointer)log_console_conf, sizeof(log_console_t)*1, output, output_len);
rpc_free(log_console_conf);
return ret;
}

View File

@ -1,5 +1,10 @@
#include <errno.h>
#include "log_config.h" #include "log_config.h"
#include "log_config_cm.h" #include "log_config_cm.h"
#include "rpc.h"
#define MAX_REMOTE_HOST_CNT 8
#define REMOTE_HOST_JSON(s, o) { \ #define REMOTE_HOST_JSON(s, o) { \
s2j_struct_get_basic_element(s, o, int, rfc); \ s2j_struct_get_basic_element(s, o, int, rfc); \
@ -11,6 +16,15 @@
s2j_struct_get_basic_element(s, o, int, level); \ s2j_struct_get_basic_element(s, o, int, level); \
} }
#define REMOTE_HOST_X_JSON(s, o) { \
s2j_struct_get_basic_element(s, o, int, rfc); \
s2j_struct_get_basic_element(s, o, int, port); \
S2J_STRUCT_GET_STRING_ELEMENT_N(s, o, host, LOG_HOST_SZ); \
s2j_struct_get_basic_element(s, o, int, level); \
}
#define LOG_CONF_KEY_REMOTE_LEVEL "remote.level"
ret_code log_remote_host_config_chk(uint source, uint *config_type, ret_code log_remote_host_config_chk(uint source, uint *config_type,
@ -48,4 +62,554 @@ ret_code log_remote_level_config_proc(uint source, uint config_type,
return log_rpc_exec(SERVICE_LOG_REMOTE_NAME, CONF_LOG_REMOTE_LEVEL_FUNC, input, input_len, sizeof(log_remote_level_t)); return log_rpc_exec(SERVICE_LOG_REMOTE_NAME, CONF_LOG_REMOTE_LEVEL_FUNC, input, input_len, sizeof(log_remote_level_t));
} }
static ret_code cm_get_log_remote_level_elems(log_remote_level_t *log_remote_level, int *err_no)
{
char value[MAX_LINE_SZ] = "";
int tmp_value = 0;
if (NULL == log_remote_level || NULL == err_no)
{
return RET_ERR;
}
memset(log_remote_level, 0, sizeof(*log_remote_level));
memset(value, 0, MAX_LINE_SZ);
if (cm_log_get_keyvalue_from_file(CM_LOG_CONF_REMOTE_LEVEL_FILE, LOG_CONF_KEY_REMOTE_LEVEL, value, sizeof(value)) == 0)
{
tmp_value = atoi(value);
log_remote_level->level = (u8)tmp_value;
}
ULOG_DEBUG(g_log_h, "get log remote level:%u", log_remote_level->level);
return RET_OK;
}
static ret_code cm_log_remote_level_format_json(log_remote_level_t *log_remote_level,
pointer output, int *outlen)
{
char *json_log_file = NULL;
/* create Student JSON object */
s2j_create_json_obj(json_obj);
if (json_obj == NULL)
{
return RET_NOMEM;
}
s2j_json_set_basic_element(json_obj, log_remote_level, int, level);
json_log_file = cJSON_PrintUnformatted(json_obj);
*outlen = strlen(json_log_file) + 1;
memcpy(output, json_log_file, *outlen);
free(json_log_file);
cJSON_Delete(json_obj);
return RET_OK;
}
ret_code log_remote_level_config_get(uint source,
pointer input, int input_len,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
log_remote_level_t *log_remote_level = NULL;
int err_no = 0;
log_remote_level = (log_remote_level_t *)input;
ret = cm_get_log_remote_level_elems(log_remote_level, &err_no);
RET_ERR_FORMART(ret, err_no, output, *output_len);
ASSERT_RET(ret);
cm_log_remote_level_format_json(log_remote_level, output, output_len);
return ret;
}
ret_code log_remote_level_config_getall(uint source,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
log_remote_level_t *log_remote_level = NULL;
int err_no = 0;
log_remote_level = rpc_new(log_remote_level_t, 1);
ret = log_remote_level_config_get(source, (pointer)log_remote_level, sizeof(log_remote_level_t)*1, output, output_len);
rpc_free(log_remote_level);
return ret;
}
/* *.=emerg;*.=alert;*.=crit;*.=err;*.=warn;*.=notice;*.=info @1.1.1.1:514:RFC3164fmt
* *.=emerg;*.=alert;*.=crit;*.=err;*.=warn;*.=notice;*.=info @@1.1.1.2:514:RFC5424fmt */
static ret_code cm_log_prase_host_from_str(const char *input_str, log_remote_host_x_t *ret_host)
{
char *pos = NULL;
char *pos2 = NULL;
size_t tmp_len = 0;
char tmp_str[128] = "";
int tmp_value;
int is_find = 0;
ret_code ret = RET_ERR;
if(NULL == input_str || '\0' == input_str[0] || NULL == ret_host)
{
return RET_ERR;
}
pos = input_str;
memset(tmp_str, 0, sizeof(tmp_str));
/* 提取日志等级 */
while (1)
{
pos2 = strstr(pos, ".=");
if (NULL == pos2)
{
break;
}
if (strlen(pos2) <= strlen(".="))
{
break;
}
pos = pos2+strlen(".=");
}
pos2 = strstr(pos, " ");
if (NULL == pos || NULL == pos2)
{
goto END;
}
tmp_len = pos2-pos;
strncpy(tmp_str, pos, tmp_len);
tmp_str[tmp_len] = '\0';
tmp_value = log_str_to_level(tmp_str);
if ( -1 == tmp_value)
{
goto END;
}
ret_host->level = (u8)tmp_value;
/* 提取IP地址 */
pos = strstr(pos2, "@");
if (NULL == pos)
{
goto END;
}
pos ++;
if('@' == pos[0])
{
pos++;
}
pos2 = strstr(pos, ":");
if (NULL == pos2)
{
goto END;
}
tmp_len = pos2-pos;
if (tmp_len == 0 || tmp_len >= sizeof(ret_host->host))
{
goto END;
}
memset(ret_host->host, 0, sizeof(ret_host->host));
strncpy(ret_host->host, pos, tmp_len);
/* 提取端口 */
pos2 ++;
pos = pos2;
pos2 = strstr(pos, ":");
if (pos2 == NULL)
{
goto END;
}
tmp_len = pos2-pos;
strncpy(tmp_str, pos, tmp_len);
tmp_str[tmp_len] = '\0';
tmp_value = atoi(tmp_str);
if (tmp_value < 0)
{
goto END;
}
ret_host->port = (u16)tmp_value;
/* 提取日志存放格式 */
pos2++;
pos = pos2;
pos2 = strstr(pos, " ");
if (pos2 != NULL)
{
tmp_len = pos2-pos;
}
else
{
tmp_len = strlen(pos);
}
strncpy(tmp_str, pos, tmp_len);
tmp_str[tmp_len] = '\0';
if ('\n' == tmp_str[tmp_len-1])
{
tmp_str[tmp_len-1] = '\0';
}
tmp_value = -1;
for (int i = 0; i < (sizeof(rfc_tbl) / sizeof(rfc_key_fmt)); i++)
{
if (strcasecmp(tmp_str, rfc_tbl[i].fmt) == 0)
{
tmp_value = rfc_tbl[i].rfc;
break;
}
}
if (-1 == tmp_value)
{
goto END;
}
ret_host->rfc = tmp_value;
// ULOG_DEBUG(g_log_h, "get remote host, addr:%s, port:%u, log_format:%d, level:%u",
// ret_host->host, ret_host->port, ret_host->rfc, ret_host->level);
is_find = 1;
END:
if (is_find != 1)
{
memset(ret_host, 0, sizeof(*ret_host));
}
ret = RET_OK;
return ret;
}
static ret_code cm_log_get_host_from_file_by_addr(const char *file_str, const char *addr_str, log_remote_host_x_t *ret_host)
{
FILE *fp = NULL;
ssize_t n;
char *line = NULL;
char tmp_str[128] = "";
int is_find = 0;
ret_code ret = RET_ERR;
if (NULL == file_str || '\0' == file_str[0]
|| NULL == addr_str || strlen(addr_str) >= LOG_HOST_SZ
|| NULL == ret_host) {
ULOG_ERR(g_log_h, "get log host from file: bad input");
}
memset(ret_host, 0, sizeof(*ret_host));
memcpy(ret_host->host, addr_str, strlen(addr_str));
fp = fopen(file_str, "r");
if (NULL == fp) {
return ret;
}
if (fseek(fp, 0, SEEK_SET) == -1)
{
ULOG_ERR(g_log_h, "Seeknig config to begin is faiure:%s", strerror(errno));
fclose(fp);
return ret;
}
while ((getline(&line, &n, fp)) != -1)
{
memset(tmp_str, 0, sizeof(tmp_str));
snprintf(tmp_str, "@%s:", addr_str);
if (NULL == strstr(line, tmp_str))
{
continue;
}
if ( cm_log_prase_host_from_str(line, ret_host) == RET_OK )
{
is_find = 1;
}
ULOG_DEBUG(g_log_h, "get remote host: addr=%s, port=%u, log_format=%d, level=%u",
ret_host->host, ret_host->port, ret_host->rfc, ret_host->level);
break;
}
if (is_find != 1)
{
memset(ret_host, 0, sizeof(*ret_host));
}
ret = RET_OK;
if (NULL != fp)
{
fclose(fp);
}
return ret;
}
static ret_code cm_log_getall_host_from_file(const char *file_str, log_remote_host_x_t *ret_hosts, int *ret_cnt)
{
log_remote_host_x_t hosts[MAX_REMOTE_HOST_CNT];
FILE *fp = NULL;
ssize_t n;
char *line = NULL;
size_t tmp_len = 0;
char tmp_str[128] = "";
int tmp_value;
int cnt = 0;
ret_code ret = RET_ERR;
if (NULL == file_str || '\0' == file_str[0]
|| NULL == ret_hosts || NULL == ret_cnt)
{
ULOG_ERR(g_log_h, "get all log host from file: bad input");
}
fp = fopen(file_str, "r");
if (NULL == fp)
{
return ret;
}
if (fseek(fp, 0, SEEK_SET) == -1)
{
ULOG_ERR(g_log_h, "Seeknig config to begin is faiure:%s", strerror(errno));
fclose(fp);
return ret;
}
cnt = 0;
while ((getline(&line, &n, fp)) != -1)
{
if (NULL == strstr(line, "@"))
{
continue;
}
if ( cm_log_prase_host_from_str(line, hosts+cnt) != RET_OK )
{
continue;
}
if (hosts[cnt].host[0] == '\0')
{
/* 无效数据 */
memset(hosts+cnt, 0, sizeof(log_remote_host_x_t));
continue;
}
ULOG_DEBUG(g_log_h, "getall remote host, host[%d]: addr=%s, port=%u, log_format=%d, level=%u",
cnt, hosts[cnt].host, hosts[cnt].port, hosts[cnt].rfc, hosts[cnt].level);
cnt ++;
if (cnt >= MAX_REMOTE_HOST_CNT)
{
break;
}
}
if (cnt > 0)
{
memcpy(ret_hosts, &hosts, sizeof(log_remote_host_x_t)*cnt);
}
*ret_cnt = cnt;
ret = RET_OK;
if (NULL != fp)
{
fclose(fp);
}
return ret;
}
static ret_code cm_get_log_remote_host_elems(log_remote_host_x_t *host_x, int *err_no)
{
log_remote_host_x_t input_host;
log_remote_host_x_t output_host;
ret_code ret = RET_ERR;
if (NULL == host_x || NULL == err_no)
{
return RET_ERR;
}
memcpy(&input_host, host_x, sizeof(log_remote_host_x_t));
memset(&output_host, 0, sizeof(output_host));
if (cm_log_get_host_from_file_by_addr(CM_LOG_CONF_REMOTE_FILE, input_host.host, &output_host) == RET_OK)
{
memcpy(host_x, &output_host, sizeof(output_host));
}
return RET_OK;
}
static ret_code cm_getall_log_remote_host_elems(log_remote_host_x_t *host_xs, int *cnt, int *err_no)
{
ret_code ret = RET_ERR;
if (NULL == host_xs || NULL == err_no)
{
return RET_ERR;
}
if (cm_log_getall_host_from_file(CM_LOG_CONF_REMOTE_FILE, host_xs, cnt) != RET_OK)
{
return RET_ERR;
}
return RET_OK;
}
static ret_code cm_log_remote_host_x_format_json(log_remote_host_x_t *host_x,
pointer output, int *outlen)
{
char *json_log_file = NULL;
/* create Student JSON object */
s2j_create_json_obj(json_obj);
if (json_obj == NULL)
{
return RET_NOMEM;
}
s2j_json_set_basic_element(json_obj, host_x, int, port);
s2j_json_set_basic_element(json_obj, host_x, int, rfc);
s2j_json_set_basic_element(json_obj, host_x, string, host);
s2j_json_set_basic_element(json_obj, host_x, int, level);
json_log_file = cJSON_PrintUnformatted(json_obj);
*outlen = strlen(json_log_file) + 1;
memcpy(output, json_log_file, *outlen);
free(json_log_file);
cJSON_Delete(json_obj);
return RET_OK;
}
ret_code cm_log_remote_host_x_format_json_array(log_remote_host_x_t *host_xs,
int count, pointer output, int *outlen)
{
log_remote_host_x_t tem_buff = {0};
log_remote_host_x_t *host_x = &tem_buff;
cJSON *json_array = NULL;
char *json_str;
int i;
json_array = cJSON_CreateArray();
if(json_array == NULL)
{
return RET_NOMEM;
}
for(i = 0; i < count; i++)
{
memcpy(host_x, &host_xs[i], sizeof(log_remote_host_x_t));
/* create Student JSON object */
s2j_create_json_obj(json_obj);
if(json_obj == NULL)
{
return RET_NOMEM;
}
cJSON_AddItemToArray(json_array, json_obj);
s2j_json_set_basic_element(json_obj, host_x, int, port);
s2j_json_set_basic_element(json_obj, host_x, int, rfc);
s2j_json_set_basic_element(json_obj, host_x, string, host);
s2j_json_set_basic_element(json_obj, host_x, int, level);
}
json_str = cJSON_PrintUnformatted(json_array);
*outlen = strlen(json_str) + 1;
memcpy(output, json_str, *outlen);
free(json_str);
cJSON_Delete(json_array);
return RET_OK;
}
ret_code log_remote_host_x_config_chk(uint source, uint *config_type,
pointer input, int *input_len,
pointer output, int *output_len)
{
COMMON_CHK(log_remote_host_x_t, input, input_len, "remote host_x", REMOTE_HOST_X_JSON);
}
ret_code log_remote_host_x_config_get(uint source,
pointer input, int input_len,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
log_remote_host_x_t *host_x = NULL;
int err_no = 0;
host_x = (log_remote_host_x_t *)input;
ret = cm_get_log_remote_host_elems(host_x, &err_no);
RET_ERR_FORMART(ret, err_no, output, *output_len);
ASSERT_RET(ret);
cm_log_remote_host_x_format_json(host_x, output, output_len);
return ret;
}
ret_code log_remote_host_x_config_getall(uint source,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
log_remote_host_x_t *host_x = NULL;
int cnt = 0;
int err_no = 0;
host_x = rpc_new(log_remote_host_x_t, MAX_REMOTE_HOST_CNT);
ret = cm_getall_log_remote_host_elems(host_x, &cnt, &err_no);
RET_ERR_FORMART(ret, err_no, output, *output_len);
ASSERT_RET(ret);
cm_log_remote_host_x_format_json_array(host_x, cnt, output, output_len);
ULOG_DEBUG(g_log_h, "log_remote_host_x_config_getall, output=%s, output_len=%d",
output, *output_len);
rpc_free(host_x);
return ret;
}

File diff suppressed because it is too large Load Diff

View File

@ -248,7 +248,7 @@ ret_code authpara_config_proc(uint source, uint config_type,
authpara_config_json_parse(input, &conf_type, &auth_parameters); authpara_config_json_parse(input, &conf_type, &auth_parameters);
if(input_len != sizeof(auth_parameters_t)) { if(input_len < sizeof(auth_parameters_t)) {
return RET_INPUTERR; return RET_INPUTERR;
} }
@ -302,12 +302,14 @@ ret_code authpara_config_proc(uint source, uint config_type,
} }
#endif #endif
#if 0
/*Portal server的port通过redis消息队列接口发布给web server*/ /*Portal server的port通过redis消息队列接口发布给web server*/
memset(auth_port, 0, 20); memset(auth_port, 0, 20);
sprintf(auth_port, "%d ", auth_parameters.port); sprintf(auth_port, "%d ", auth_parameters.port);
printf("The number 'port' is %d and the string 'port' is %s. \n", auth_parameters.port, auth_port); printf("The number 'port' is %d and the string 'port' is %s. \n", auth_parameters.port, auth_port);
local_portal_port(auth_port); local_portal_port(auth_port);
#endif
/*创建json对象 */ /*创建json对象 */
res = cJSON_CreateObject(); res = cJSON_CreateObject();
@ -319,6 +321,8 @@ ret_code authpara_config_proc(uint source, uint config_type,
} }
/*将json对象转换成json字符串 返回处理结果*/ /*将json对象转换成json字符串 返回处理结果*/
printf("resultcode = %d\n", configure_result->resultcode);
printf("message = %s\n", configure_result->message);
cJSON_AddNumberToObject(res, "resultcode", configure_result->resultcode); cJSON_AddNumberToObject(res, "resultcode", configure_result->resultcode);
cJSON_AddStringToObject(res, "message", configure_result->message); cJSON_AddStringToObject(res, "message", configure_result->message);
ret_char = cJSON_PrintUnformatted(res); ret_char = cJSON_PrintUnformatted(res);
@ -343,4 +347,3 @@ ret_code authpara_config_proc(uint source, uint config_type,
free(configure_result); free(configure_result);
return RET_OK; return RET_OK;
} }

View File

@ -223,10 +223,10 @@ ret_code freeauth_config_json_parse(pointer input, uint *conf_type, freeauth_con
freeauth_buff->dport = struct_freeauth->dport; freeauth_buff->dport = struct_freeauth->dport;
freeauth_buff->flag = struct_freeauth->flag; freeauth_buff->flag = struct_freeauth->flag;
} }
cJSON_Delete(cjson); cJSON_Delete(cjson);
s2j_delete_struct_obj(struct_freeauth); s2j_delete_struct_obj(struct_freeauth);
return RET_OK; return RET_OK;
} }
@ -278,6 +278,8 @@ ret_code authpara_config_json_parse_array(pointer input, uint *conf_type, freeau
memset(freeauth_buff, 0, sizeof(freeauth_configure_t) * iCount); memset(freeauth_buff, 0, sizeof(freeauth_configure_t) * iCount);
*fb = freeauth_buff;
for(i = 0; i < iCount; i++) { for(i = 0; i < iCount; i++) {
cJSON *pArrayItem = cJSON_GetArrayItem(data, i); cJSON *pArrayItem = cJSON_GetArrayItem(data, i);
@ -325,16 +327,19 @@ ret_code authpara_config_json_parse_array(pointer input, uint *conf_type, freeau
freeauth_buff->flag = flag->valueint; freeauth_buff->flag = flag->valueint;
} }
#if 0
printf("freeauth_buff->name = %p\n", &freeauth_buff->name);
printf("freeauth_buff->name = %p\n", &freeauth_buff->name);
printf("freeauth_buff->name = %s\n", freeauth_buff->name);
#if 0
for (int j = 0; j < iCount; j++) for (int j = 0; j < iCount; j++)
{ {
printf("[%d %s %d %d %d %d]\n",pbuf[j].rule_priority, pbuf[j].name, pbuf[j].sip, printf("[%d %s %d %d %d %d]\n", freeauth_buff->rule_priority, freeauth_buff->name, freeauth_buff->sip,
pbuf[j].dip, pbuf[j].dport, pbuf[j].flag); freeauth_buff->dip, freeauth_buff->dport, freeauth_buff->flag);
freeauth_buff++;
} }
#endif #endif
freeauth_buff++; freeauth_buff++;
} }
} }
@ -344,8 +349,7 @@ ret_code authpara_config_json_parse_array(pointer input, uint *conf_type, freeau
} }
cJSON_Delete(cjson); cJSON_Delete(cjson);
*fb = freeauth_buff;
return RET_OK; return RET_OK;
} }
@ -386,20 +390,36 @@ ret_code freeauth_config_add_proc(uint source, uint config_type,
freeauth_config_json_parse(input, &conf_type, freeauth_configure); freeauth_config_json_parse(input, &conf_type, freeauth_configure);
/*校验用户名长度*/ /*校验用户名长度*/
if(input_len != sizeof(freeauth_configure_t) || NULL == freeauth_configure->name || if(input_len < sizeof(freeauth_configure_t) || NULL == freeauth_configure->name ||
(UNAMESIZE) < strlen(freeauth_configure->name) || 0 == strlen(freeauth_configure->name)) { (UNAMESIZE) < strlen(freeauth_configure->name) || 0 == strlen(freeauth_configure->name)) {
free(freeauth_configure); free(freeauth_configure);
printf("the lenth is error\n");
return RET_INPUTERR; return RET_INPUTERR;
} }
/* 校验用户名中不含特殊字符 */ /* 校验用户名中不含特殊字符 */
if(SPECHAR(freeauth_configure->name)) { if(SPECHAR(freeauth_configure->name)) {
free(freeauth_configure); free(freeauth_configure);
printf("username 含有特殊字符\n");
return RET_INPUTERR; return RET_INPUTERR;
} }
#if 0
/*校验优先级是否重名,如果优先级已经存在 则退出程序*/
for(i = 0; i < RULE_MAX_NUM; i++){
if(freeauth_array[i].rule_priority == freeauth_configure->rule_priority) {
printf("%s(%d) freeauth_array[%d] = %p\n", __FUNCTION__, __LINE__, i, &freeauth_array[i]);
free(freeauth_configure);
return RET_EXIST;
}
}
#endif
/*校验端口号*/
if((freeauth_configure->dport < DPORT_MIN_NUM) || (freeauth_configure->dport > DPORT_MAX_NUM)) { if((freeauth_configure->dport < DPORT_MIN_NUM) || (freeauth_configure->dport > DPORT_MAX_NUM)) {
free(freeauth_configure); free(freeauth_configure);
printf("the port is error\n");
return RET_IPINVALID; return RET_IPINVALID;
} }
@ -408,8 +428,39 @@ ret_code freeauth_config_add_proc(uint source, uint config_type,
/*两个字符串相等 strcmp值为0*/ /*两个字符串相等 strcmp值为0*/
if(0 == strcmp(freeauth_array[i].name, freeauth_configure->name)) { if(0 == strcmp(freeauth_array[i].name, freeauth_configure->name)) {
printf("%s(%d) freeauth_array[%d] = %p\n", __FUNCTION__, __LINE__, i, &freeauth_array[i]); printf("%s(%d) freeauth_array[%d] = %p\n", __FUNCTION__, __LINE__, i, &freeauth_array[i]);
free(freeauth_configure); printf("local user is existed\n");
return RET_EXIST;
/*创建json对象 */
res = cJSON_CreateObject();
if(!res) {
free(freeauth_configure);
return RET_ERR;
}
/*将json对象转换成json字符串 返回处理结果*/
cJSON_AddNumberToObject(res, "resultcode", 2);
cJSON_AddStringToObject(res, "message", "rule existed");
ret_char = cJSON_PrintUnformatted(res);
ret_int = strlen(ret_char);
if(output_len) {
*output_len = ret_int;
}
/*超出2k的内存报错 */
if(ret_int >= 1024 * 2) {
free(ret_char);
cJSON_Delete(res);
return RET_NOMEM;
}
memcpy(output, ret_char, ret_int + 1);
free(ret_char);
cJSON_Delete(res);
return RET_OK;
} }
} }
@ -492,10 +543,26 @@ ret_code freeauth_config_mod_proc(uint source, uint config_type,
unsigned int ret_int = 0; unsigned int ret_int = 0;
authpara_config_json_parse_array(input, &conf_type, &freeauth_configure, &cnt); authpara_config_json_parse_array(input, &conf_type, &freeauth_configure, &cnt);
if(input_len != sizeof(freeauth_configure_t)) { if(input_len < sizeof(freeauth_configure_t)) {
return RET_INPUTERR; return RET_INPUTERR;
} }
printf("打印全局数组内全部元素\n");
/*打印数组内全部元素*/
for(i = 0; i < RULE_MAX_NUM; i++)
{
printf("[%d %s %d %d %d %d %d]\n", freeauth_array[i].rule_priority, freeauth_array[i].name, freeauth_array[i].sip,
freeauth_array[i].dip, freeauth_array[i].dport, freeauth_array[i].dport, i);
}
printf("打印传过来的json串\n");
for(int j = 0; j < cnt; j++)
{
printf("[%d %s %d %d %d %d %d]\n", freeauth_configure[j].rule_priority, freeauth_configure[j].name, freeauth_configure[j].sip,
freeauth_configure[j].dip, freeauth_configure[j].dport, freeauth_configure[j].dport, j);
}
/*查找要修改的免认证规则名字,不存在则退出程序 */ /*查找要修改的免认证规则名字,不存在则退出程序 */
for(i = 0; i < RULE_MAX_NUM; i++) { for(i = 0; i < RULE_MAX_NUM; i++) {
@ -590,33 +657,42 @@ ret_code freeauth_config_del_proc(uint source, uint config_type,
char *ret_char = NULL; char *ret_char = NULL;
unsigned int ret_int = 0; unsigned int ret_int = 0;
#if 0
freeauth_configure = (freeauth_configure_t *)malloc(sizeof(freeauth_configure_t));
if(freeauth_configure == NULL) {
return RET_NAMEINVAL;
}
#endif
authpara_config_json_parse_del_array(input, &conf_type, &freeauth_configure, &cnt); authpara_config_json_parse_del_array(input, &conf_type, &freeauth_configure, &cnt);
if(input_len != sizeof(freeauth_configure_t)) { if(input_len < sizeof(freeauth_configure_t)) {
return RET_INPUTERR; return RET_INPUTERR;
} }
printf("打印全局数组内全部元素\n");
/*打印数组内全部元素*/
for(i = 0; i < RULE_MAX_NUM; i++)
{
printf("[%d %s %d %d %d %d %d]\n", freeauth_array[i].rule_priority, freeauth_array[i].name, freeauth_array[i].sip,
freeauth_array[i].dip, freeauth_array[i].dport, freeauth_array[i].dport, i);
}
printf("打印传过来的json串\n");
for(int j = 0; j < cnt; j++)
{
printf("[%s %d]\n", freeauth_configure[j].name, j);
}
/*查找要修改的免认证规则名字,不存在则退出程序 */ /*查找要修改的免认证规则名字,不存在则退出程序 */
for(i = 0; i < RULE_MAX_NUM; i++) { for(i = 0; i < RULE_MAX_NUM; i++) {
/*两个字符串相等 strcmp值为0*/ /*两个字符串相等 strcmp值为0*/
for(int j = 0; j < cnt; j++) { for(int j = 0; j < cnt; j++) {
if(0 == strcmp(freeauth_array[i].name, freeauth_configure[j].name))
{
if(0 == strcmp(freeauth_array[i].name, freeauth_configure[j].name)) { printf("%s %d\n",freeauth_array[i].name, i);
printf("%s %d\n", freeauth_array[i].name, i); printf("%s %d\n",freeauth_configure[j].name, j);
printf("%s %d\n", freeauth_configure[j].name, j);
printf("%s(%d) freeauth_array[%d] = %p\n", __FUNCTION__, __LINE__, i, &freeauth_array[j]); printf("%s(%d) freeauth_array[%d] = %p\n", __FUNCTION__, __LINE__, i, &freeauth_array[j]);
/*数据库修改 存入全局变量*/
/*数据库删除 存入全局变量*/
del_authfree(freeauth_configure[j].name, &authfree_result); del_authfree(freeauth_configure[j].name, &authfree_result);
#if 0 #if 0
/*用户态下发到内核态auth_hook */ /*用户态下发到内核态auth_hook */
int r = -1; int r = -1;
@ -646,7 +722,8 @@ ret_code freeauth_config_del_proc(uint source, uint config_type,
/*创建json对象 */ /*创建json对象 */
res = cJSON_CreateObject(); res = cJSON_CreateObject();
if(!res) { if(!res)
{
return RET_ERR; return RET_ERR;
} }
@ -656,12 +733,14 @@ ret_code freeauth_config_del_proc(uint source, uint config_type,
ret_char = cJSON_PrintUnformatted(res); ret_char = cJSON_PrintUnformatted(res);
ret_int = strlen(ret_char); ret_int = strlen(ret_char);
if(output_len) { if(output_len)
{
*output_len = ret_int; *output_len = ret_int;
} }
/*超出2k的内存报错 */ /*超出2k的内存报错 */
if(ret_int >= 1024 * 2) { if(ret_int >= 1024 * 2)
{
free(ret_char); free(ret_char);
cJSON_Delete(res); cJSON_Delete(res);
return RET_NOMEM; return RET_NOMEM;
@ -670,15 +749,15 @@ ret_code freeauth_config_del_proc(uint source, uint config_type,
memcpy(output, ret_char, ret_int + 1); memcpy(output, ret_char, ret_int + 1);
free(ret_char); free(ret_char);
cJSON_Delete(res); cJSON_Delete(res);
} }
} }
} }
if(freeauth_configure) { if(freeauth_configure) {
free(freeauth_configure); free(freeauth_configure);
} }
return RET_OK; return RET_OK;
} }

View File

@ -56,7 +56,7 @@ void add_authfree(int rule_priority, char *name, uint32_t sip, uint32_t dip, int
} }
printf("开始连接数据库\n"); printf("开始连接数据库\n");
/* 连接数据库 */ /* 连接数据库 */
authfree_hdbc = connect_database(AUTHFREE_DATABASE_ID); authfree_hdbc = connect_database(AUTHFREE_DATABASE_ID);
@ -70,7 +70,7 @@ void add_authfree(int rule_priority, char *name, uint32_t sip, uint32_t dip, int
ret = create_database_table(AUTHFREE_DATABASE_ID, authfree_hdbc, "authfree", "create table authfree(rule_priority bigint, name character(32), sip bigint, dip bigint, dport bigint, flag bigint)"); ret = create_database_table(AUTHFREE_DATABASE_ID, authfree_hdbc, "authfree", "create table authfree(rule_priority bigint, name character(32), sip bigint, dip bigint, dport bigint, flag bigint)");
printf("%d \n", ret); printf("%d \n", ret);
#if 0
/*查询数据库是否存在该权限规则*/ /*查询数据库是否存在该权限规则*/
char *select_sql = "SELECT rule_priority, name, sip, dip, dport, flag FROM `authfree` WHERE name = ?"; char *select_sql = "SELECT rule_priority, name, sip, dip, dport, flag FROM `authfree` WHERE name = ?";
ret_sql = select_datebase_by_number(AUTHFREE_DATABASE_ID, authfree_hdbc, "authfree", select_sql, 1, 0, &num, 1, ret_sql = select_datebase_by_number(AUTHFREE_DATABASE_ID, authfree_hdbc, "authfree", select_sql, 1, 0, &num, 1,
@ -78,52 +78,55 @@ void add_authfree(int rule_priority, char *name, uint32_t sip, uint32_t dip, int
if(NULL != ret_sql) { if(NULL != ret_sql) {
/*用户名已存在*/ /*用户名已存在*/
printf("用户名已存在");
disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc); disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc);
authfree_result->resultcode = RULE_EXISTED; authfree_result->resultcode = RULE_EXISTED;
authfree_result->message = get_sql_ret_message(authfree_result->resultcode); authfree_result->message = get_sql_ret_message(authfree_result->resultcode);
return; return;
} else { } else {
/* 根据指定信息查询数据库的获取的结果的条目数 条目数大于10 则不能再添加 */ }
char *select_num = "SELECT rule_priority, name, sip, dip, dport, flag FROM `authfree`"; #endif
ret = get_select_datebase_number(AUTHFREE_DATABASE_ID, authfree_hdbc, "authfree", select_num, &num_sql, 6,
DB_DATA_INT_TYPE, sizeof(rule_priority), rule_priority, /* 根据指定信息查询数据库的获取的结果的条目数 条目数大于10 则不能再添加 */
DB_DATA_STRING_TYPE, strlen(name) + 1, name, char *select_num = "SELECT rule_priority, name, sip, dip, dport, flag FROM `authfree`";
DB_DATA_INT_TYPE, sizeof(sip), sip, ret = get_select_datebase_number(AUTHFREE_DATABASE_ID, authfree_hdbc, "authfree", select_num, &num_sql, 6,
DB_DATA_INT_TYPE, sizeof(dip), dip, DB_DATA_INT_TYPE, sizeof(rule_priority), rule_priority,
DB_DATA_INT_TYPE, sizeof(dport), dport, DB_DATA_STRING_TYPE, strlen(name) + 1, name,
DB_DATA_INT_TYPE, sizeof(flag), flag); DB_DATA_INT_TYPE, sizeof(sip), sip,
printf("num_sql = %d \n", num_sql); DB_DATA_INT_TYPE, sizeof(dip), dip,
printf("ret = %d \n", ret); DB_DATA_INT_TYPE, sizeof(dport), dport,
DB_DATA_INT_TYPE, sizeof(flag), flag);
printf("num_sql = %d \n", num_sql);
printf("ret = %d \n", ret);
if(num_sql > RULE_MAX_NUM) { if(num_sql > RULE_MAX_NUM) {
/*添加失败*/ /*添加失败*/
disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc); // ret_release记录日志 //disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc); // ret_release记录日志
authfree_result->resultcode = RULE_ID_MAX; authfree_result->resultcode = RULE_ID_MAX;
authfree_result->message = get_sql_ret_message(authfree_result->resultcode); authfree_result->message = get_sql_ret_message(authfree_result->resultcode);
return; return;
} }
/* 向authfree表中添加未认证权限名称、内部源IP地址、目的IP地址、目的端口号 */ /* 向authfree表中添加未认证权限名称、内部源IP地址、目的IP地址、目的端口号 */
char *addfree_sql = "INSERT INTO `authfree` SET rule_priority = ?, name = ?, sip = ?, dip = ?, dport = ?, flag = ?"; char *addfree_sql = "INSERT INTO `authfree` SET rule_priority = ?, name = ?, sip = ?, dip = ?, dport = ?, flag = ?";
ret_add = update_database(AUTHFREE_DATABASE_ID, authfree_hdbc, DB_OP_INSERT, AUTHFREE_TABLE, addfree_sql, 6, ret_add = update_database(AUTHFREE_DATABASE_ID, authfree_hdbc, DB_OP_INSERT, AUTHFREE_TABLE, addfree_sql, 6,
DB_DATA_INT_TYPE, sizeof(rule_priority), rule_priority, DB_DATA_INT_TYPE, sizeof(rule_priority), rule_priority,
DB_DATA_STRING_TYPE, strlen(name) + 1, name, DB_DATA_STRING_TYPE, strlen(name) + 1, name,
DB_DATA_INT_TYPE, sizeof(sip), sip, DB_DATA_INT_TYPE, sizeof(sip), sip,
DB_DATA_INT_TYPE, sizeof(dip), dip, DB_DATA_INT_TYPE, sizeof(dip), dip,
DB_DATA_INT_TYPE, sizeof(dport), dport, DB_DATA_INT_TYPE, sizeof(dport), dport,
DB_DATA_INT_TYPE, sizeof(flag), flag); DB_DATA_INT_TYPE, sizeof(flag), flag);
printf("the value of ret:%d\n", ret_add); printf("the value of ret:%d\n", ret_add);
if(0 != ret_add) { if(0 != ret_add) {
/*添加失败*/ /*添加失败*/
disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc); // ret_release记录日志 //disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc); // ret_release记录日志
authfree_result->resultcode = ADD_RULE_ERR; authfree_result->resultcode = ADD_RULE_ERR;
authfree_result->message = get_sql_ret_message(authfree_result->resultcode); authfree_result->message = get_sql_ret_message(authfree_result->resultcode);
return; return;
} }
}
disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc); //disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc);
/*存未认证权限数数组*/ /*存未认证权限数数组*/
for(i = 0; i < RULE_MAX_NUM; i++) { for(i = 0; i < RULE_MAX_NUM; i++) {
@ -147,11 +150,20 @@ void add_authfree(int rule_priority, char *name, uint32_t sip, uint32_t dip, int
} }
} }
/*打印数组内全部元素*/
for(i = 0; i < RULE_MAX_NUM; i++)
{
printf("[%d %s %d %d %d %d %d]\n", freeauth_array[i].rule_priority, freeauth_array[i].name, freeauth_array[i].sip,
freeauth_array[i].dip, freeauth_array[i].dport, freeauth_array[i].dport, i);
}
/*添加成功*/ /*添加成功*/
authfree_result->resultcode = ADD_RULE_OK; authfree_result->resultcode = ADD_RULE_OK;
authfree_result->message = get_sql_ret_message(authfree_result->resultcode); authfree_result->message = get_sql_ret_message(authfree_result->resultcode);
} }
/*修改未认证权限*/ /*修改未认证权限*/
void mod_authfree(int rule_priority, char *name, uint32_t sip, uint32_t dip, int dport, int flag, authfree_result_t *authfree_result) void mod_authfree(int rule_priority, char *name, uint32_t sip, uint32_t dip, int dport, int flag, authfree_result_t *authfree_result)
{ {
@ -181,6 +193,7 @@ void mod_authfree(int rule_priority, char *name, uint32_t sip, uint32_t dip, int
ret = create_database_table(AUTHFREE_DATABASE_ID, authfree_hdbc, "authfree", "create table authfree(name character(32), sip bigint, dip bigint, dport bigint)"); ret = create_database_table(AUTHFREE_DATABASE_ID, authfree_hdbc, "authfree", "create table authfree(name character(32), sip bigint, dip bigint, dport bigint)");
printf("%d \n", ret); printf("%d \n", ret);
#if 0
/*查询数据库是否存在该权限规则*/ /*查询数据库是否存在该权限规则*/
char *select_sql = "SELECT name, sip, dip, dport FROM `authfree`WHERE name = ?"; char *select_sql = "SELECT name, sip, dip, dport FROM `authfree`WHERE name = ?";
ret_sql = select_datebase_by_number(AUTHFREE_DATABASE_ID, authfree_hdbc, "authfree", select_sql, 1, 0, &num, 1, ret_sql = select_datebase_by_number(AUTHFREE_DATABASE_ID, authfree_hdbc, "authfree", select_sql, 1, 0, &num, 1,
@ -193,27 +206,29 @@ void mod_authfree(int rule_priority, char *name, uint32_t sip, uint32_t dip, int
authfree_result->message = get_sql_ret_message(authfree_result->resultcode); authfree_result->message = get_sql_ret_message(authfree_result->resultcode);
return; return;
} else { } else {
/*修改authfree表中内部源IP地址、目的IP地址、目的端口号 未认证权限名称不能修改 */ }
char *modfree_sql = "UPDATE `authfree` SET rule_priority = ?, sip = ?, dip = ?, dport = ? ,flag = ? WHERE name = ?"; #endif
ret_mod = update_database(AUTHFREE_DATABASE_ID, authfree_hdbc, DB_OP_UPDATE, AUTHFREE_TABLE, modfree_sql, 6,
/*修改authfree表中内部源IP地址、目的IP地址、目的端口号 未认证权限名称不能修改 */
char *modfree_sql = "UPDATE `authfree` SET rule_priority = ?, sip = ?, dip = ?, dport = ? ,flag = ? WHERE name = ?";
ret_mod = update_database(AUTHFREE_DATABASE_ID, authfree_hdbc, DB_OP_UPDATE, AUTHFREE_TABLE, modfree_sql, 6,
DB_DATA_INT_TYPE, sizeof(rule_priority), rule_priority, DB_DATA_INT_TYPE, sizeof(rule_priority), rule_priority,
DB_DATA_INT_TYPE, sizeof(sip), sip, DB_DATA_INT_TYPE, sizeof(sip), sip,
DB_DATA_INT_TYPE, sizeof(dip), dip, DB_DATA_INT_TYPE, sizeof(dip), dip,
DB_DATA_INT_TYPE, sizeof(dport), dport, DB_DATA_INT_TYPE, sizeof(dport), dport,
DB_DATA_INT_TYPE, sizeof(flag), flag, DB_DATA_INT_TYPE, sizeof(flag), flag,
DB_DATA_STRING_TYPE, strlen(name) + 1, name); DB_DATA_STRING_TYPE, strlen(name) + 1, name);
printf("the value of ret:%d\n", ret_mod); printf("the value of ret:%d\n", ret_mod);
if(0 != ret_mod) { if(0 != ret_mod) {
/*修改失败*/ /*修改失败*/
disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc); // ret_release记录日志 //disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc); // ret_release记录日志
authfree_result->resultcode = MOD_RULE_ERR; authfree_result->resultcode = MOD_RULE_ERR;
authfree_result->message = get_sql_ret_message(authfree_result->resultcode); authfree_result->message = get_sql_ret_message(authfree_result->resultcode);
return; return;
}
} }
disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc); //disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc);
/*修改对应未认证权限数数组*/ /*修改对应未认证权限数数组*/
for(i = 0; i < RULE_MAX_NUM; i++) { for(i = 0; i < RULE_MAX_NUM; i++) {
@ -231,15 +246,13 @@ void mod_authfree(int rule_priority, char *name, uint32_t sip, uint32_t dip, int
} }
} }
#if 0
/*打印数组内全部元素*/ /*打印数组内全部元素*/
for(i = 0; i < RULE_MAX_NUM; i++) { for(i = 0; i < RULE_MAX_NUM; i++)
printf("%s %d %d %d %d\n", freeauth_array[i].name, freeauth_array[i].sip, freeauth_array[i].dip, freeauth_array[i].dport, i); {
printf("[%d %s %d %d %d %d %d]\n", freeauth_array[i].rule_priority, freeauth_array[i].name, freeauth_array[i].sip,
freeauth_array[i].dip, freeauth_array[i].dport, freeauth_array[i].dport, i);
} }
#endif
/*修改成功*/ /*修改成功*/
authfree_result->resultcode = MOD_RULE_OK; authfree_result->resultcode = MOD_RULE_OK;
authfree_result->message = get_sql_ret_message(authfree_result->resultcode); authfree_result->message = get_sql_ret_message(authfree_result->resultcode);
@ -275,32 +288,38 @@ void del_authfree(char *name, authfree_result_t *authfree_result)
ret = create_database_table(AUTHFREE_DATABASE_ID, authfree_hdbc, "authfree", "create table authfree(name character(32), sip bigint, dip bigint, dport bigint)"); ret = create_database_table(AUTHFREE_DATABASE_ID, authfree_hdbc, "authfree", "create table authfree(name character(32), sip bigint, dip bigint, dport bigint)");
printf("%d \n", ret); printf("%d \n", ret);
#if 0
/*查询数据库是否存在该权限规则*/ /*查询数据库是否存在该权限规则*/
char *select_sql = "SELECT rule_priority, name, sip, dip, dport, flag FROM `authfree`WHERE name = ?"; char *select_sql = "SELECT rule_priority, name, sip, dip, dport, flag FROM `authfree`WHERE name = ?";
ret_sql = select_datebase_by_number(AUTHFREE_DATABASE_ID, authfree_hdbc, "authfree", select_sql, 1, 0, &num, 1, ret_sql = select_datebase_by_number(AUTHFREE_DATABASE_ID, authfree_hdbc, "authfree", select_sql, 1, 0, &num, 1,
DB_DATA_STRING_TYPE, strlen(name) + 1, name); DB_DATA_STRING_TYPE, strlen(name) + 1, name);
if(NULL == ret_sql) { if(NULL == ret_sql) {
/*未认证权限用户名不存在*/ /*未认证权限用户名不存在*/
authfree_result->resultcode = RULE_NOT_FOUNT; authfree_result->resultcode = RULE_NOT_FOUNT;
authfree_result->message = get_sql_ret_message(authfree_result->resultcode); authfree_result->message = get_sql_ret_message(authfree_result->resultcode);
return; return;
} else { } else {
/*删除authfree表中未认证权限名称、内部源IP地址、目的IP地址、目的端口号 */
char *delfree_sql = "DELETE FROM authfree WHERE name = ?";
int ret_del = update_database(AUTHFREE_DATABASE_ID, authfree_hdbc, DB_OP_DEL, AUTHFREE_TABLE, delfree_sql, 1,
DB_DATA_STRING_TYPE, strlen(name) + 1, name);
printf("the value of ret:%d\n", ret_del);
if(0 != ret_del) { }
disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc); // ret_release记录日志 #endif
authfree_result->resultcode = DEL_RULE_ERR; /*删除失败*/
authfree_result->message = get_sql_ret_message(authfree_result->resultcode); /*删除authfree表中未认证权限名称、内部源IP地址、目的IP地址、目的端口号 */
return; char *delfree_sql = "DELETE FROM authfree WHERE name = ?";
} ret_del = update_database(AUTHFREE_DATABASE_ID, authfree_hdbc, DB_OP_DEL, AUTHFREE_TABLE, delfree_sql, 1,
DB_DATA_STRING_TYPE, strlen(name) + 1, name);
printf("the value of ret:%d\n", ret_del);
if(0 != ret_del) {
//disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc); // ret_release记录日志
authfree_result->resultcode = DEL_RULE_ERR; /*删除失败*/
authfree_result->message = get_sql_ret_message(authfree_result->resultcode);
return;
} }
disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc); //disconnect_database(AUTHFREE_DATABASE_ID, authfree_hdbc);
/*修改对应未认证权限数数组*/ /*修改对应未认证权限数数组*/
for(i = 0; i < RULE_MAX_NUM; i++) { for(i = 0; i < RULE_MAX_NUM; i++) {
@ -314,17 +333,9 @@ void del_authfree(char *name, authfree_result_t *authfree_result)
} }
} }
#if 0
/*打印数组内全部元素*/
for(i = 0; i < RULE_MAX_NUM; i++) {
printf("[%d %s %d %d %d %d %d]\n", freeauth_array[i].rule_priority, freeauth_array[i].name, freeauth_array[i].sip,
freeauth_array[i].dip, freeauth_array[i].dport, freeauth_array[i].dport, i);
}
#endif
/*删除成功*/ /*删除成功*/
authfree_result->resultcode = DEL_RUL_OK; authfree_result->resultcode = DEL_RUL_OK;
authfree_result->message = get_sql_ret_message(authfree_result->resultcode); authfree_result->message = get_sql_ret_message(authfree_result->resultcode);
} }

View File

@ -14,11 +14,12 @@
extern auth_parameters_t *auth_para; extern auth_parameters_t *auth_para;
char * mes[]={"ADDSUCCESS", "ADDFAILURE"}; char * mes[]={"mod success", "mod failure"};
/*前端type类型只有修改修改数据库中的内容返回值为code message——修改成功 修改失败*/ /*前端type类型只有修改修改数据库中的内容返回值为code message——修改成功 修改失败*/
void mod_authpara(int port, int timehorizon, int failcount, int locktime, int aging_time, configure_result_t *configure_result) void mod_authpara(int port, int timehorizon, int failcount, int locktime, int aging_time, configure_result_t *configure_result)
{ {
authparInit();
void * authpara_hdbc; void * authpara_hdbc;
char * ret_sql = NULL; char * ret_sql = NULL;
int ret; int ret;
@ -55,7 +56,7 @@ void mod_authpara(int port, int timehorizon, int failcount, int locktime, int ag
configure_result->message = mes[configure_result->resultcode]; configure_result->message = mes[configure_result->resultcode];
return; return;
} }
char *user_authpara = "UPDATE `authparas` SET port = ?, timehorizon = ?, failcount = ?, locktime = ?, aging_time = ?"; char *user_authpara = "UPDATE `authparas` SET port = ?, timehorizon = ?, failcount = ?, locktime = ?, aging_time = ?";
ret = update_database(AUTHPARA_DATABASE_ID, authpara_hdbc, DB_OP_UPDATE, AUTHPARA_TABLE, user_authpara, 5, ret = update_database(AUTHPARA_DATABASE_ID, authpara_hdbc, DB_OP_UPDATE, AUTHPARA_TABLE, user_authpara, 5,
@ -67,21 +68,21 @@ void mod_authpara(int port, int timehorizon, int failcount, int locktime, int ag
printf("the value of ret:%d\n", ret); printf("the value of ret:%d\n", ret);
if(0 != ret) if(0 != ret)
{ {
disconnect_database(AUTHPARA_DATABASE_ID , authpara_hdbc); // ret_release记录日志 //disconnect_database(AUTHPARA_DATABASE_ID , authpara_hdbc); // ret_release记录日志
configure_result->resultcode = 1; configure_result->resultcode = 1;
configure_result->message = mes[configure_result->resultcode]; configure_result->message = mes[configure_result->resultcode];
return; return;
} }
disconnect_database(AUTHPARA_DATABASE_ID , authpara_hdbc); //disconnect_database(AUTHPARA_DATABASE_ID , authpara_hdbc);
/*存全局变量*/ /*存全局变量*/
auth_para->port = port; auth_para->port = port;
auth_para->timehorizon = timehorizon; auth_para->timehorizon = timehorizon;
auth_para->failcount = failcount; auth_para->failcount = failcount;
auth_para->locktime = locktime; auth_para->locktime = locktime;
auth_para->aging_time = aging_time; auth_para->aging_time = aging_time;
configure_result->resultcode = 0; configure_result->resultcode = 0;
configure_result->message = mes[configure_result->resultcode]; configure_result->message = mes[configure_result->resultcode];
return; return;

View File

@ -5,46 +5,63 @@
#include "ulog/ulog_in.h" #include "ulog/ulog_in.h"
#include "s2j/s2j.h" #include "s2j/s2j.h"
static int logging_file_json_parse(pointer input, log_file_t *config_buff);
int conf_file(cJSON *json_obj, int argc, char **argv) int conf_file(cJSON *json_obj, int argc, char **argv)
{ {
log_file_t log_file = {0}; log_file_t log_file = {0};
log_file.is_compress = LOG_UNCOMPRESS; log_file_t log_file_out = {0};
int level = LOG_INFO; int level = LOG_INFO;
if (argc >= 3) { uint config_type = CM_CONFIG_SET;
if ((level = log_str_to_level(argv[2])) < 0) { cJSON *json_output = NULL;
ULOG_WARNING(g_log, "Unknown log level:%s", argv[2]);
return -1; log_file.is_compress = LOG_UNCOMPRESS;
}
}
log_file.level = level;
if (argc >= 4) { if (argc == 3 && strstr(argv[2], "get") != NULL)
strncpy(log_file.path, argv[3], sizeof(log_file.path)); {
} config_type = CM_CONFIG_GET;
}
else
{
config_type = CM_CONFIG_SET;
if (argc >= 3) {
if ((level = log_str_to_level(argv[2])) < 0) {
ULOG_WARNING(g_log, "Unknown log level:%s", argv[2]);
return -1;
}
}
log_file.level = level;
if (argc >= 5) { if (argc >= 4) {
if (atoi(argv[4]) > 0) { strncpy(log_file.path, argv[3], sizeof(log_file.path));
log_file.is_compress = LOG_COMPRESS; }
}
}
if (argc >= 6) { if (argc >= 5) {
int day = atoi(argv[5]); if (atoi(argv[4]) > 0) {
if (day > 0) { log_file.is_compress = LOG_COMPRESS;
log_file.del_over_days = day; }
} else if (day < 0) { }
ULOG_WARNING(g_log, "Deleting day(%d) mustn't be less than 0", day);
return -1; if (argc >= 6) {
} int day = atoi(argv[5]);
} if (day > 0) {
log_file.del_over_days = day;
if (argc >= 7) { } else if (day < 0) {
if (snprintf(log_file.del_over_size, sizeof(log_file.del_over_size), "%s", argv[6]) < 0) { ULOG_WARNING(g_log, "Deleting day(%d) mustn't be less than 0", day);
ULOG_ERR(g_log, "Building del max size:%s is filure", argv[6]); return -1;
return -1; }
} }
}
if (argc >= 7) {
if (snprintf(log_file.del_over_size, sizeof(log_file.del_over_size), "%s", argv[6]) < 0) {
ULOG_ERR(g_log, "Building del max size:%s is filure", argv[6]);
return -1;
}
}
}
s2j_json_set_basic_element(json_obj, &log_file, int, level); s2j_json_set_basic_element(json_obj, &log_file, int, level);
s2j_json_set_basic_element(json_obj, &log_file, string, path); s2j_json_set_basic_element(json_obj, &log_file, string, path);
@ -52,12 +69,63 @@ int conf_file(cJSON *json_obj, int argc, char **argv)
s2j_json_set_basic_element(json_obj, &log_file, int, del_over_days); s2j_json_set_basic_element(json_obj, &log_file, int, del_over_days);
s2j_json_set_basic_element(json_obj, &log_file, string, del_over_size); s2j_json_set_basic_element(json_obj, &log_file, string, del_over_size);
int ret = set_log_conf(json_obj, LOG_CONFIG_FILE); int ret = set_log_conf(config_type, json_obj, LOG_CONFIG_FILE, &json_output);
if (ret != 0) { if (ret != 0) {
ULOG_ERR(g_log, "File log which is configured is failure"); ULOG_ERR(g_log, "File log which is configured is failure");
} else { } else {
ULOG_DEBUG(g_log, "File log whice is configured is success"); ULOG_DEBUG(g_log, "File log whice is configured is success");
} }
if (CM_CONFIG_GET == config_type)
{
logging_file_json_parse(json_output, &log_file_out);
ULOG_DEBUG(g_log, "Log file get: level=%u, path=%s, is_compress=%d, del_over_days=%u, del_over_size=%s",
log_file_out.level,
log_file_out.path,
log_file_out.is_compress,
log_file_out.del_over_days, log_file_out.del_over_size);
}
return ret; return ret;
} }
static int logging_file_json_parse(pointer input, log_file_t *config_buff)
{
cJSON *json_obj;
if (NULL == config_buff)
{
return -1;
}
json_obj = cJSON_Parse(input);
if (!json_obj)
{
return -1;
}
s2j_create_struct_obj(log_file, log_file_t);
if (log_file == NULL)
{
cJSON_Delete(json_obj);
return -1;
}
s2j_struct_get_basic_element(log_file, json_obj, int, level);
S2J_STRUCT_GET_STRING_ELEMENT_N(log_file, json_obj, path, MAX_PATH_SZ);
s2j_struct_get_basic_element(log_file, json_obj, int, is_compress);
s2j_struct_get_basic_element(log_file, json_obj, int, del_over_days);
S2J_STRUCT_GET_STRING_ELEMENT_N(log_file, json_obj, del_over_size, MAX_U64_SZ);
memcpy(config_buff, log_file, sizeof(log_file_t));
s2j_delete_struct_obj(json_obj);
cJSON_Delete(json_obj);
return RET_OK;
}

View File

@ -5,7 +5,14 @@
#include "ulog_in.h" #include "ulog_in.h"
#include "s2j/s2j.h" #include "s2j/s2j.h"
#define DEFAULT_REMOTE_HOST 514 #define DEFAULT_REMOTE_HOST_PORT 514
#define MAX_REMOTE_HOST_CNT 8
static int logging_remote_json_parse(pointer input, log_remote_host_x_t *config_buff);
static int logging_remote_json_parse_array(pointer input,
log_remote_host_x_t *config_buff, int *cnt);
static int logging_remote_level_json_parse(pointer input, log_remote_level_t *config_buff);
static log_rfc_t get_rfc_by_name(const char *name) static log_rfc_t get_rfc_by_name(const char *name)
{ {
@ -20,22 +27,69 @@ static log_rfc_t get_rfc_by_name(const char *name)
static int conf_remote_by_config_id(cJSON *json_obj, const char *host, const u16 port, const log_rfc_t rfc, uint64 config_id) static int conf_remote_by_config_id(cJSON *json_obj, const char *host, const u16 port, const log_rfc_t rfc, uint64 config_id)
{ {
log_remote_host_t remote; log_remote_host_x_t remote = {0};
cJSON *json_output = NULL;
strncpy(remote.host, host, sizeof(remote.host)); int config_type = CM_CONFIG_SET;
remote.port = port;
remote.rfc = rfc;
if (LOG_CONFIG_REMOTE_GET_HOST == config_id)
{
if (NULL == host || '\0' == host[0])
{
config_type = CM_CONFIG_GET_ALL;
}
else
{
config_type = CM_CONFIG_GET;
strncpy(remote.host, host, sizeof(remote.host));
}
}
else
{
config_type = CM_CONFIG_SET;
strncpy(remote.host, host, sizeof(remote.host));
remote.port = port;
remote.rfc = rfc;
}
s2j_json_set_basic_element(json_obj, &remote, int, port); s2j_json_set_basic_element(json_obj, &remote, int, port);
s2j_json_set_basic_element(json_obj, &remote, int, rfc); s2j_json_set_basic_element(json_obj, &remote, int, rfc);
s2j_json_set_basic_element(json_obj, &remote, string, host); s2j_json_set_basic_element(json_obj, &remote, string, host);
s2j_json_set_basic_element(json_obj, &remote, int, level);
int ret = set_log_conf(json_obj, config_id); int ret = set_log_conf(config_type, json_obj, config_id, &json_output);
if (ret != 0) { if (ret != 0) {
ULOG_ERR(g_log, "Host of log which is configured is failure"); ULOG_ERR(g_log, "Host of log which is configured is failure");
} else { } else {
ULOG_DEBUG(g_log, "Host of log whice is configured is success"); ULOG_DEBUG(g_log, "Host of log whice is configured is success");
} }
if (LOG_CONFIG_REMOTE_GET_HOST == config_id)
{
if (CM_CONFIG_GET_ALL == config_type )
{
log_remote_host_x_t remotes_out[MAX_REMOTE_HOST_CNT] = {0};
int cnt = 0;
if (logging_remote_json_parse_array(json_output, remotes_out, &cnt) == 0)
{
for (int i=0; i<cnt; i++)
{
ULOG_DEBUG(g_log, "Log host get: host[%d]: addr=%s, port=%u, log_fmt=%u, level=%u",
i, remotes_out[i].host, remotes_out[i].port, remotes_out[i].rfc, remotes_out[i].level);
}
}
}
else
{
log_remote_host_x_t remote_out = {0};
if (logging_remote_json_parse(json_output, &remote_out) == 0)
{
ULOG_DEBUG(g_log, "Log host get: addr=%s, port=%u, log_fmt=%u, level=%u",
remote_out.host, remote_out.port, remote_out.rfc, remote_out.level);
}
}
}
return ret; return ret;
} }
@ -44,7 +98,7 @@ int conf_remote(cJSON *json_obj, int argc, char **argv)
int ret = -1; int ret = -1;
uint64 config_id; uint64 config_id;
if (argc <= 3) { if (argc < 3) {
ULOG_WARNING(g_log, "Parameter too less"); ULOG_WARNING(g_log, "Parameter too less");
return ret; return ret;
} }
@ -53,13 +107,19 @@ int conf_remote(cJSON *json_obj, int argc, char **argv)
config_id = LOG_CONFIG_REMOTE_ADD_HOST; config_id = LOG_CONFIG_REMOTE_ADD_HOST;
} else if (strcasecmp(argv[2], "del") == 0) { } else if (strcasecmp(argv[2], "del") == 0) {
config_id = LOG_CONFIG_REMOTE_DEL_HOST; config_id = LOG_CONFIG_REMOTE_DEL_HOST;
} else if (strcasecmp(argv[2], "get") == 0
|| strcasecmp(argv[2], "getall") == 0) {
config_id = LOG_CONFIG_REMOTE_GET_HOST;
} else { } else {
ULOG_WARNING(g_log, "Unknown operation command:%s", argv[2]); ULOG_WARNING(g_log, "Unknown operation command:%s", argv[2]);
return ret; return ret;
} }
char *host = argv[3]; char *host = NULL;
u16 port = DEFAULT_REMOTE_HOST; if (argc >= 4) {
host = argv[3];
}
u16 port = DEFAULT_REMOTE_HOST_PORT;
if (argc >= 5) { if (argc >= 5) {
port= atoi(argv[4]); port= atoi(argv[4]);
} }
@ -77,29 +137,178 @@ int conf_remote(cJSON *json_obj, int argc, char **argv)
int conf_remote_level(cJSON *json_obj, int argc, char **argv) int conf_remote_level(cJSON *json_obj, int argc, char **argv)
{ {
int level;
log_remote_level_t log_level = {0};
log_remote_level_t log_level_out = {0};
uint config_type = CM_CONFIG_SET;
cJSON *json_output = NULL;
if (argc < 3) { if (argc < 3) {
ULOG_WARNING(g_log, "Not input log level"); ULOG_WARNING(g_log, "Not input log level");
return -1; return -1;
} }
int level; if (argc == 3 && strstr(argv[2], "get") != NULL) {
config_type = CM_CONFIG_GET;
if ((level = log_str_to_level(argv[2])) < 0) { }
ULOG_WARNING(g_log, "Unknown log level:%s", argv[2]); else {
return -1; config_type = CM_CONFIG_SET;
}
if ((level = log_str_to_level(argv[2])) < 0) {
log_remote_level_t log_level; ULOG_WARNING(g_log, "Unknown log level:%s", argv[2]);
return -1;
log_level.level = (u8)level; }
log_level.level = (u8)level;
}
s2j_json_set_basic_element(json_obj, &log_level, int, level); s2j_json_set_basic_element(json_obj, &log_level, int, level);
int ret = set_log_conf(json_obj, LOG_CONFIG_REMOTE_LEVEL); int ret = set_log_conf(config_type, json_obj, LOG_CONFIG_REMOTE_LEVEL, &json_output);
if (ret != 0) { if (ret != 0) {
ULOG_ERR(g_log, "Remote level of log which is configured is failure"); ULOG_ERR(g_log, "Remote level of log which is configured is failure");
} else { } else {
ULOG_DEBUG(g_log, "Remote level of log whice is configured is success"); ULOG_DEBUG(g_log, "Remote level of log whice is configured is success");
} }
if (CM_CONFIG_GET == config_type) {
logging_remote_level_json_parse(json_output, &log_level_out);
ULOG_DEBUG(g_log, "Log level get: level=%u", log_level_out.level);
}
return ret; return ret;
} }
static int logging_remote_json_parse(pointer input, log_remote_host_x_t *config_buff)
{
cJSON *json_obj;
if (NULL == config_buff)
{
return -1;
}
json_obj = cJSON_Parse(input);
if(!json_obj)
{
return -1;
}
ULOG_DEBUG(g_log, "remote json parse, json input:%s \n", cJSON_Print(json_obj));
s2j_create_struct_obj(log_host_x, log_remote_host_x_t);
if(log_host_x == NULL)
{
cJSON_Delete(json_obj);
return -1;
}
s2j_struct_get_basic_element(log_host_x, json_obj, int, port);
s2j_struct_get_basic_element(log_host_x, json_obj, int, rfc);
S2J_STRUCT_GET_STRING_ELEMENT_N(log_host_x, json_obj, host, LOG_HOST_SZ);
s2j_struct_get_basic_element(log_host_x, json_obj, int, level);
memcpy(config_buff, log_host_x, sizeof(log_remote_host_x_t));
s2j_delete_struct_obj(json_obj);
cJSON_Delete(json_obj);
return 0;
}
static int logging_remote_json_parse_array(pointer input,
log_remote_host_x_t *config_buff, int *cnt)
{
cJSON *json_obj;
cJSON* p_array;
int json_cnt = 0, i = 0;
int used_cnt = 0;
json_obj = cJSON_Parse(input);
if(!json_obj)
{
return -1;
}
ULOG_DEBUG(g_log, "remote json parse array, json input:%s \n", cJSON_Print(json_obj));
json_cnt = cJSON_GetArraySize(json_obj);
if (json_cnt > MAX_REMOTE_HOST_CNT)
{
used_cnt = MAX_REMOTE_HOST_CNT;
}
else
{
used_cnt = json_cnt;
}
s2j_create_struct_obj(log_host_x, log_remote_host_x_t);
if(log_host_x == NULL)
{
cJSON_Delete(json_obj);
return -1;
}
*cnt = 0;
for (i = 0; i < used_cnt; i++)
{
p_array = cJSON_GetArrayItem(json_obj, i);
if (p_array)
{
memset(log_host_x, 0, sizeof(log_remote_host_x_t));
s2j_struct_get_basic_element(log_host_x, p_array, int, port);
s2j_struct_get_basic_element(log_host_x, p_array, int, rfc);
s2j_struct_get_basic_element(log_host_x, p_array, string, host);
s2j_struct_get_basic_element(log_host_x, p_array, int, level);
memcpy(config_buff+*cnt, log_host_x, sizeof(log_remote_host_x_t));
(*cnt)++;
}
}
s2j_delete_struct_obj(log_host_x);
cJSON_Delete(json_obj);
return 0;
}
static int logging_remote_level_json_parse(pointer input, log_remote_level_t *config_buff)
{
cJSON *json_obj;
if (NULL == config_buff)
{
return -1;
}
json_obj = cJSON_Parse(input);
if (!json_obj)
{
return -1;
}
s2j_create_struct_obj(log_remote_level, log_remote_level_t);
if (log_remote_level == NULL)
{
cJSON_Delete(json_obj);
return -1;
}
s2j_struct_get_basic_element(log_remote_level, json_obj, int, level);
memcpy(config_buff, log_remote_level, sizeof(log_remote_level_t));
s2j_delete_struct_obj(json_obj);
cJSON_Delete(json_obj);
return 0;
}

View File

@ -5,7 +5,9 @@
#include "configmapi.h" #include "configmapi.h"
#include "ulog/ulog_in.h" #include "ulog/ulog_in.h"
int set_log_conf(cJSON *json_obj, uint64 config_id)
int set_log_conf(uint config_type, cJSON *json_obj, uint64 config_id, cJSON **json_output)
{ {
int ret = -1; int ret = -1;
char *output; char *output;
@ -18,7 +20,8 @@ int set_log_conf(cJSON *json_obj, uint64 config_id)
} }
ULOG_DEBUG(g_log, "Setting log json is %s", json); ULOG_DEBUG(g_log, "Setting log json is %s", json);
ret_code ret_c = web_config_exec_sync(CM_CONFIG_SET, config_id,
ret_code ret_c = web_config_exec_sync(config_type, config_id,
json, strlen(json), json, strlen(json),
&output, &output_len); &output, &output_len);
if (ret_c != RET_OK) { if (ret_c != RET_OK) {
@ -26,6 +29,16 @@ int set_log_conf(cJSON *json_obj, uint64 config_id)
} else { } else {
ret = 0; ret = 0;
} }
if ((CM_CONFIG_GET == config_type || CM_CONFIG_GET_ALL == config_type) && json_output != NULL)
{
ULOG_DEBUG(g_log, "get outputlen:%d, output:%s", output_len, output);
*json_output = output;
}
else
{
*json_output = NULL;
}
if (json != NULL) { if (json != NULL) {
free(json); free(json);
@ -34,3 +47,37 @@ int set_log_conf(cJSON *json_obj, uint64 config_id)
return ret; return ret;
} }
int logging_terminal_json_parse(pointer input, pointer config_buff, int conf_id)
{
cJSON *json_obj = NULL;
if (NULL == config_buff) {
return -1;
}
json_obj = cJSON_Parse(input);
if (!json_obj)
{
return -1;
}
s2j_create_struct_obj(log_terminal, log_console_t);
if (NULL == log_terminal)
{
cJSON_Delete(json_obj);
return -1;
}
s2j_struct_get_basic_element(log_terminal, json_obj, int, level);
s2j_struct_get_basic_element(log_terminal, json_obj, int, on);
s2j_struct_get_basic_element(log_terminal, json_obj, string, module);
memcpy(config_buff, log_terminal, sizeof(log_console_t));
s2j_delete_struct_obj(json_obj);
cJSON_Delete(json_obj);
return 0;
}

View File

@ -13,11 +13,15 @@
extern ulog_t *g_log; extern ulog_t *g_log;
int set_log_conf(cJSON *json_obj, uint64 config_id); int set_log_conf(uint config_type, cJSON *json_obj, uint64 config_id, cJSON **json_output);
int logging_terminal_json_parse(pointer input, pointer config_buff, int conf_id);
#define CONF_TERMINAL(type, json_obj, str_level, on, module_name, conf_id) { \
#define CONF_TERMINAL(type, config_type, json_obj, str_level, on, module_name, conf_id) { \
type terminal = {0}; \ type terminal = {0}; \
type terminal_out = {0}; \
int level; \ int level; \
cJSON *json_output = NULL; \
\ \
if ((level = log_str_to_level(str_level)) < 0) { \ if ((level = log_str_to_level(str_level)) < 0) { \
ULOG_WARNING(g_log, "Unknown log level:%s", str_level); \ ULOG_WARNING(g_log, "Unknown log level:%s", str_level); \
@ -34,21 +38,40 @@ int set_log_conf(cJSON *json_obj, uint64 config_id);
s2j_json_set_basic_element(json_obj, &terminal, int, on); \ s2j_json_set_basic_element(json_obj, &terminal, int, on); \
s2j_json_set_basic_element(json_obj, &terminal, string, module); \ s2j_json_set_basic_element(json_obj, &terminal, string, module); \
\ \
int ret = set_log_conf(json_obj, conf_id); \ int ret = set_log_conf(config_type, json_obj, conf_id, &json_output); \
if (ret != 0) { \ if (ret != 0) { \
ULOG_ERR(g_log, "Setting terminal configure of log is failure"); \ ULOG_ERR(g_log, "Setting terminal configure of log is failure"); \
} else { \ } else { \
ULOG_DEBUG(g_log, "Setting terminal configure of log is success"); \ ULOG_DEBUG(g_log, "Setting terminal configure of log is success"); \
} \ } \
\
if (CM_CONFIG_GET == config_type) { \
if (logging_terminal_json_parse(json_output, &terminal_out, conf_id) == 0) { \
if (LOG_ON == terminal_out.on) { \
ULOG_DEBUG(g_log, "Log %s get: level=%u, module=%s", \
(conf_id == LOG_CONFIG_CONSOLE)?"console":"monitor", \
terminal_out.level, \
terminal_out.module); \
} else { \
ULOG_DEBUG(g_log, "Log %s get: off", \
(conf_id == LOG_CONFIG_CONSOLE)?"console":"monitor"); \
} \
} \
} \
\
return ret; \ return ret; \
} }
#define CMD_PARSE_AND_CONFIG_TERMINAL(type, json_obj, argc, argv, conf_id) { \ #define CMD_PARSE_AND_CONFIG_TERMINAL(type, json_obj, argc, argv, conf_id) { \
log_sw_t on = LOG_ON; \ log_sw_t on = LOG_ON; \
char *str_level = DEFAULT_LOG_LEVEL; \ char *str_level = DEFAULT_LOG_LEVEL; \
uint config_type = CM_CONFIG_SET; \
\
if (argc >= 3) { \ if (argc >= 3) { \
if (strcasecmp(argv[2], "off") == 0) { \ if (strcasecmp(argv[2], "off") == 0) { \
on = LOG_OFF; \ on = LOG_OFF; \
} else if (strcasecmp(argv[2], "get") == 0) { \
config_type = CM_CONFIG_GET; \
} else { \ } else { \
str_level = argv[2]; \ str_level = argv[2]; \
} \ } \
@ -59,9 +82,7 @@ int set_log_conf(cJSON *json_obj, uint64 config_id);
module = argv[3]; \ module = argv[3]; \
} \ } \
\ \
CONF_TERMINAL(type, json_obj, str_level, on, module, conf_id); \ CONF_TERMINAL(type, config_type, json_obj, str_level, on, module, conf_id); \
} }
#endif #endif

View File

@ -40,13 +40,13 @@ int main(int argc, char **argv)
{ {
int ret = -1; int ret = -1;
if (argc < 2) { if (argc < 2) {
fprintf(stdout, "Parameter too few"); fprintf(stdout, "Parameter too few\n");
return ret; return ret;
} }
g_log = ulog_init(LOGGING_MODULE_NAME, 1); g_log = ulog_init(LOGGING_MODULE_NAME, 1);
if (g_log == NULL) { if (g_log == NULL) {
fprintf(stderr, "Initiating ulog is failure"); fprintf(stderr, "Initiating ulog is failure\n");
return ret; return ret;
} }