修复新版本lede中应用过滤服务挂掉的问题

This commit is contained in:
derry 2022-03-24 16:34:13 +08:00
parent 3b1893dc12
commit 3ec903e86d
3 changed files with 122 additions and 75 deletions

View File

@ -33,6 +33,83 @@ char CLASS_NAME_TABLE[MAX_APP_TYPE][MAX_CLASS_NAME_LEN];
const char *config_path = "./config";
static struct uci_context *uci_ctx = NULL;
static struct uci_package *uci_appfilter;
int uci_get_int_value(struct uci_context *ctx, char *key)
{
struct uci_element *e;
struct uci_ptr ptr;
int ret = -1;
int dummy;
char *parameters ;
char param_tmp[128] = {0};
strcpy(param_tmp, key);
if (uci_lookup_ptr(ctx, &ptr, param_tmp, true) != UCI_OK) {
return ret;
}
if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
ctx->err = UCI_ERR_NOTFOUND;
goto done;
}
e = ptr.last;
switch(e->type) {
case UCI_TYPE_SECTION:
ret = -1;
goto done;
case UCI_TYPE_OPTION:
ret = atoi(ptr.o->v.string);
goto done;
default:
break;
}
done:
if (ptr.p)
uci_unload(ctx, ptr.p);
return ret;
}
int uci_get_value(struct uci_context *ctx, char *key, char *output, int out_len)
{
struct uci_element *e;
struct uci_ptr ptr;
int ret = UCI_OK;
int dummy;
char *parameters ;
char param_tmp[128] = {0};
strcpy(param_tmp, key);
if (uci_lookup_ptr(ctx, &ptr, param_tmp, true) != UCI_OK) {
ret = 1;
return ret;
}
if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
ctx->err = UCI_ERR_NOTFOUND;
ret = 1;
goto done;
}
e = ptr.last;
switch(e->type) {
case UCI_TYPE_SECTION:
snprintf(output, out_len, "%s", ptr.s->type);
break;
case UCI_TYPE_OPTION:
snprintf(output, out_len, "%s", ptr.o->v.string);
break;
default:
ret = 1;
break;
}
done:
if (ptr.p)
uci_unload(ctx, ptr.p);
return ret;
}
//
static struct uci_package *
config_init_package(const char *config)
@ -150,56 +227,35 @@ void dump_af_time(af_ctl_time_t *t)
af_ctl_time_t *load_appfilter_ctl_time_config(void)
{
char start_time_str[64] = {0};
char end_time_str[64] = {0};
char days_str[64] = {0};
int ret = 0;
af_ctl_time_t *t = NULL;
appfilter_config_alloc();
struct uci_context *ctx = uci_alloc_context();
if (!ctx)
return NULL;
struct uci_section *time_sec = uci_lookup_section(uci_ctx, uci_appfilter, "time");
if (!time_sec)
{
printf("get time section failed\n");
appfilter_config_free();
ret |= uci_get_value(ctx, "appfilter.time.start_time", start_time_str, sizeof(start_time_str));
ret |= uci_get_value(ctx, "appfilter.time.end_time", end_time_str, sizeof(end_time_str));
ret |= uci_get_value(ctx, "appfilter.time.days", days_str, sizeof(days_str));
if (ret != 0){
printf("time config error\n");
return NULL;
}
if (!check_time_valid(start_time_str) || !check_time_valid(end_time_str)){
printf("format error\n");
return NULL;
}
t = malloc(sizeof(af_ctl_time_t));
if (!t)
{
appfilter_config_free();
return NULL;
}
memset(t, 0x0, sizeof(af_ctl_time_t));
char *start_time_str = uci_lookup_option_string(uci_ctx, time_sec, "start_time");
if (check_time_valid(start_time_str))
sscanf(start_time_str, "%d:%d", &t->start.hour, &t->start.min);
else
{
printf("start time check failed\n");
free(t);
t = NULL;
goto EXIT1;
}
char *end_time_str = uci_lookup_option_string(uci_ctx, time_sec, "end_time");
sscanf(start_time_str, "%d:%d", &t->start.hour, &t->start.min);
sscanf(end_time_str, "%d:%d", &t->end.hour, &t->end.min);
if (check_time_valid(end_time_str))
sscanf(end_time_str, "%d:%d", &t->end.hour, &t->end.min);
else
{
printf("end time check failed\n");
free(t);
t = NULL;
goto EXIT2;
}
char *days_str = uci_lookup_option_string(uci_ctx, time_sec, "days");
if (!days_str)
{
printf("not found days\n");
goto EXIT2;
}
char *p = strtok(days_str, " ");
if (!p)
goto EXIT3;
goto EXIT;
do
{
int day = atoi(p);
@ -208,45 +264,24 @@ af_ctl_time_t *load_appfilter_ctl_time_config(void)
else
ret = 0;
} while (p = strtok(NULL, " "));
EXIT3:
if (days_str)
free(days_str);
EXIT2:
if (end_time_str)
free(end_time_str);
EXIT1:
if (start_time_str)
free(start_time_str);
appfilter_config_free();
EXIT:
uci_free_context(ctx);
return t;
}
int config_get_appfilter_enable(void)
{
int ret = 0;
int enable = 0;
af_ctl_time_t *t = NULL;
appfilter_config_alloc();
struct uci_section *global_sec = uci_lookup_section(uci_ctx, uci_appfilter, "global");
if (!global_sec)
{
printf("get global section failed\n");
appfilter_config_free();
struct uci_context *ctx = uci_alloc_context();
if (!ctx)
return NULL;
}
char *enable_opt = uci_lookup_option_string(uci_ctx, global_sec, "enable");
if (!enable_opt)
{
printf("enable option not found.\n");
appfilter_config_free();
return 0;
}
enable = atoi(enable_opt);
free(enable_opt);
appfilter_config_free();
enable = uci_get_int_value(ctx, "appfilter.global.enable");
if (enable < 0)
enable = 0;
uci_free_context(ctx);
return enable;
}

View File

@ -45,6 +45,8 @@ void appfilter_nl_handler(struct uloop_fd *u, unsigned int ev)
struct iovec iov = {buf, sizeof(buf)};
struct nlmsghdr *h;
char *mac = NULL;
printf("%s %d\n", __func__, __LINE__);
struct msghdr msg = {
.msg_name = &nladdr,
.msg_namelen = sizeof(nladdr),
@ -137,6 +139,7 @@ void appfilter_nl_handler(struct uloop_fd *u, unsigned int ev)
int type = appid / 1000;
int id = appid % 1000;
printf("%s %d\n", __func__, __LINE__);
node->stat[type - 1][id - 1].total_time += REPORT_INTERVAL_SECS;
@ -151,6 +154,8 @@ void appfilter_nl_handler(struct uloop_fd *u, unsigned int ev)
}
else
{
printf("%s %d\n", __func__, __LINE__);
visit_info_t *visit_node = (visit_info_t *)calloc(1, sizeof(visit_info_t));
visit_node->action = action;
visit_node->appid = appid;
@ -158,6 +163,8 @@ void appfilter_nl_handler(struct uloop_fd *u, unsigned int ev)
visit_node->first_time = cur_time.tv_sec - MIN_VISIT_TIME;
visit_node->next = NULL;
add_visit_info_node(&node->visit_htable[hash], visit_node);
printf("%s %d\n", __func__, __LINE__);
//printf("add visit info curtime=%d\n", cur_time.tv_sec);
}
}
@ -173,6 +180,7 @@ int send_msg_to_kernel(int fd, void *msg, int len)
daddr.nl_family = AF_NETLINK;
daddr.nl_pid = 0; // to kernel
daddr.nl_groups = 0;
printf("%s %d\n", __func__, __LINE__);
int ret = 0;
struct nlmsghdr *nlh = NULL;
@ -200,6 +208,8 @@ int send_msg_to_kernel(int fd, void *msg, int len)
perror("sendto error\n");
return -1;
}
printf("%s %d\n", __func__, __LINE__);
return 0;
}

View File

@ -38,6 +38,7 @@ void check_appfilter_enable(void)
time_t tt;
time(&tt);
enable = config_get_appfilter_enable();
if (0 == enable)
goto EXIT;
af_t = load_appfilter_ctl_time_config();
@ -46,13 +47,14 @@ void check_appfilter_enable(void)
enable = 0;
goto EXIT;
}
t = localtime(&tt);
if (af_t->days[t->tm_wday] != 1)
{
enable = 0;
goto EXIT;
}
if (af_t->start.hour <= af_t->end.hour)
{
int cur_mins = t->tm_hour * 60 + t->tm_min;
@ -64,6 +66,7 @@ void check_appfilter_enable(void)
else
enable = 0;
EXIT:
if (enable)
{
system("echo 1 >/proc/sys/oaf/enable ");
@ -76,8 +79,8 @@ EXIT:
void dev_list_timeout_handler(struct uloop_timeout *t)
{
dump_dev_list();
dump_dev_list();
check_dev_visit_info_expire();
flush_expire_visit_info();
//dump_dev_visit_list();
@ -111,7 +114,6 @@ int main(int argc, char **argv)
uloop_fd_add(&appfilter_nl_fd, ULOOP_READ);
af_msg_t msg;
msg.action = AF_MSG_INIT;
send_msg_to_kernel(appfilter_nl_fd.fd, (void *)&msg, sizeof(msg));
uloop_timeout_set(&dev_tm, 5000);
uloop_timeout_add(&dev_tm);