secgateway/Platform/user/configm/config-server/agingtime_config/agingtime.c

239 lines
5.5 KiB
C
Raw Normal View History

#include "../include/parsefile.h"
#include "../include/configm.h"
#include "../../../netlink_uapi/libnetlinku.h"
#include "../../../../common/rpc/rpc.h"
#include "../include/agingtime.h"
#include <cjson/cJSON.h>
#include "../../../../../Common/s2j/s2j.h"
#include "../../../../../Common/commuapinl.h"
#ifdef AGINGTIME_ACK_COOKIES
#define CFG_AGINGTIME_ACK_COOKIES
#endif
/*全局变量,存放配置的用户老化时间 */
int *aging_time = NULL;
/*下发用户老化时间配置到内核态 */
int set_agingtimecfg_waitack(int *agingtime)
{
int agingtime_len = 0;
struct nlmsghdr *ack = NULL;
struct nlmsghdr **answer = &ack;
struct{
struct nlmsghdr n;
char buf[1024];
} req ={
.n.nlmsg_len = NLMSG_LENGTH(0),
#ifdef CFG_AGINGTIME_ACK_COOKIES
.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,/*set NLM_F_ACKuse kernel auto ack*/
#else
.n.nlmsg_flags = NLM_F_REQUEST, /*not use kernel auto ack */
#endif
.n.nlmsg_type = AGINGTIME_CFG, /*用户态发送给内核态的用户老化时间消息 */
.n.nlmsg_pid = getpid(),
};
/*判断要发送的数据是否为NULL,不为NULL,打印出来 */
if (agingtime == NULL)
{
printf("set_agingtimecfg_waitack is error: input struct_agingtime is NULL.\r\n");
return -1;
}else
{
printf("set_freeauthcfg_waitack :agingtime %d\n", *agingtime);
}
/*计算需要发送的数据的长度 */
agingtime_len = sizeof(int);
printf("%d\n", agingtime_len);
/*可选属性 */
commnl_addattr_l(&req.n, sizeof(req), 1, agingtime, agingtime_len);
/*发送组装好的netlink消息 */
if(pdeliv_talk(1, &req.n, answer) < 0)
{
printf("set_user_agingtime_waitack rcv ack msg faild.\r\n");
return -2;
}
else
{
printf("set_user_agingtime_waitack rcv ack msg success.\r\n");
}
if(*answer != NULL)
{
printf("set_user_agingtime_waitack rcv answer.\r\n");
}
else{
printf("set_user_agingtime_waitack rcv answer error.\r\n");
return -3;
}
#ifdef CFG_AGINGTIME_ACK_COOKIES
/*recv answer*/
if((*answer)->nlmsg_type == NLMSG_ERROR){
nl_debugfs_extack(*answer);
}
#else
/*recv answer*/
if((*answer)->nlmsg_type == AGINGTIME_CFG)
{
nl_debugfs(*answer);
}
#endif
return 0;
}
/*判断配置的老化时间是否有效老化时间大于0 */
/*input格式 '{\"type\":0, \"time\":24}' */
ret_code agingtime_config_chk(uint source, uint config_type,
pointer input, int input_len,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
cJSON *cjson, *time, *res;
char * ret_char = NULL;
int * ret_int = NULL;
/*JSON字符串到JSON格式 */
cjson = cJSON_Parse(input);
if(!cjson)
{
ret = RET_INPUTERR;
ASSERT_RET(ret);
return ret;
}
/*获取键值内容 */
time = cJSON_GetObjectItem(cjson , "time");
if(!time)
{
ret = RET_INPUTERR;
ASSERT_RET(ret);
cJSON_Delete(cjson);
return ret;
}
if(time->valueint < 0)
{
ret = RET_ERR;
ASSERT_RET(ret);
return ret;
}
/*创建json对象 */
res = cJSON_CreateObject();
if(!res)
{
ret = RET_ERR;
ASSERT_RET(ret);
return ret;
}
cJSON_AddNumberToObject(res, "time", time->valueint);
/*将json对象转换成json字符串 */
ret_char = cJSON_PrintUnformatted(res);
ret_int = (int*)ret_char;
memcpy(output, ret_int, sizeof(ret_int)+1);
cJSON_Delete(res);
return RET_OK;
}
/*系统管理模块将配置的用户老化时间通过netlink下发到内核态 */
/*输入和输出的参数形式都为JSON字符串 '{"time": 30}' */
ret_code agingtime_config_proc(uint source, uint config_type,
pointer input, int input_len,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
cJSON *cjson, *time, *res;
int * agingtime = NULL;
char * ret_char = NULL;
int * ret_int = NULL;
/*JSON字符串到JSON格式 */
cjson = cJSON_Parse(input);
if(!cjson)
{
ret = RET_INPUTERR;
ASSERT_RET(ret);
return ret;
}
/*获取键值内容 */
time = cJSON_GetObjectItem(cjson , "time");
if(!time)
{
ret = RET_INPUTERR;
ASSERT_RET(ret);
cJSON_Delete(cjson);
return ret;
}
else
{
int a = time->valueint;
agingtime = &a;
}
rpc_log_info("agingtime configure: agingtime %d\n", time->valueint);
/*用户态下发到内核态auth_hook */
int r = -1;
printf("cfgchannel main begin:\r\n");
/*创建通道 */
r = commcfgnl_open();
if(r < 0)
{
printf(" pdlivnl_open fail, exit.\r\n");
return RET_ERR;
}
/*下发配置到内核态 */
r = set_agingtimecfg_waitack(agingtime);
if(r < 0)
{
printf("set_cfg_debug_waitack failed.\r\n");
return RET_ERR;
}
/*关闭netlink通道 */
commcfgnl_close();
printf("cfgchannel main exit!\r\n");
/*创建json对象 */
res = cJSON_CreateObject();
if(!res)
{
ret = RET_ERR;
ASSERT_RET(ret);
return ret;
}
cJSON_AddNumberToObject(res, "result", r);
/*将json对象转换成json字符串 */
ret_char = cJSON_PrintUnformatted(res);
ret_int =(int*)ret_char;
memcpy(output, ret_int, sizeof(ret_int)+1);
cJSON_Delete(res);
/*把免认证规则的配置信息存入全局变量 */
aging_time = agingtime;
return RET_OK;
}