#include "../include/parsefile.h" #include "../include/configm.h" #include "../../../netlink_uapi/libnetlinku.h" #include "../../../../common/rpc/rpc.h" #include "../include/agingtime.h" #include #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_ACK:use 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; /*JSON字符串到JSON格式 */ cjson = cJSON_Parse(input); if(!cjson) { ret = RET_INPUTERR; ASSERT_RET(ret); return ret; } /*获取键值内容 */ time = cJSON_GetObjectItem(cjson , "time"); if(!time) { cJSON_Delete(cjson); ret = RET_INPUTERR; ASSERT_RET(ret); return ret; } if(time->valueint < 0) { cJSON_Delete(cjson); free(time); ret = RET_ERR; ASSERT_RET(ret); return ret; } cJSON_Delete(cjson); free(time); ASSERT_RET(ret); 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, *res, *time; int * agingtime = NULL; char * ret_char = NULL; unsigned int ret_int = 0; /*JSON字符串到JSON格式 */ cjson = cJSON_Parse(input); if(!cjson) { ret = RET_INPUTERR; ASSERT_RET(ret); return ret; } /*获取键值内容 */ time = cJSON_GetObjectItem(cjson , "time"); if(!time) { cJSON_Delete(cjson); ret = RET_INPUTERR; ASSERT_RET(ret); return ret; } else { int a = time->valueint; agingtime = &a; } rpc_log_info("agingtime configure: agingtime %d\n", *agingtime); /*用户态下发到内核态auth_hook */ int r = -1; printf("cfgchannel main begin:\r\n"); /*创建通道 */ r = commcfgnl_open(); if(r < 0) { printf(" pdlivnl_open fail, exit.\r\n"); cJSON_Delete(cjson); free(time); return RET_ERR; } /*下发配置到内核态 */ r = set_agingtimecfg_waitack(agingtime); if(r < 0) { printf("set_cfg_debug_waitack failed.\r\n"); cJSON_Delete(cjson); free(time); 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 = strlen(ret_char); if(output_len) { *output_len = ret_int; } /*超出2k的内存,报错 */ if(ret_int >= 1024 * 2) { free(time); cJSON_Delete(cjson); free(ret_char); cJSON_Delete(res); return RET_NOMEM; } memcpy(output, 0, ret_int + 1); strcpy(output, ret_char); free(ret_char); cJSON_Delete(res); cJSON_Delete(cjson); free(time); /*把免认证规则的配置信息存入全局变量 */ *aging_time= *agingtime; free(aging_time); return RET_OK; } ret_code agingtime_config_get(uint source, pointer input, int input_len, pointer output, int *output_len) { ret_code ret = RET_OK; return ret; } ret_code agingtime_config_get_all(uint source, pointer output, int *output_len) { ret_code ret = RET_OK; return ret; }