main.c revision e1b21d21
1/*
2  Copyright (C) 2020 Derry <destan19@126.com>
3
4  Permission is hereby granted, free of charge, to any person obtaining a copy
5  of this software and associated documentation files (the "Software"), to deal
6  in the Software without restriction, including without limitation the rights
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  copies of the Software, and to permit persons to whom the Software is
9  furnished to do so, subject to the following conditions:
10
11  The above copyright notice and this permission notice shall be included in
12  all copies or substantial portions of the Software.
13
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  THE SOFTWARE.
21*/
22#include <unistd.h>
23#include <stdlib.h>
24#include <string.h>
25#include <libubox/uloop.h>
26#include <libubox/utils.h>
27#include <libubus.h>
28#include "appfilter_user.h"
29#include "appfilter_netlink.h"
30#include "appfilter_ubus.h"
31#include "appfilter_config.h"
32#include <time.h>
33void check_appfilter_enable(void){
34	int enable = 1;
35	struct tm *t;
36	time_t tt;
37	time(&tt);
38	af_ctl_time_t *af_t = load_appfilter_ctl_time_config();
39	if (!af_t){
40		enable = 0;
41		goto EXIT;
42	}
43
44	t = localtime(&tt);
45	if (af_t->days[t->tm_wday] != 1){
46		enable = 0;
47		goto EXIT;
48	}
49	if (af_t->start.hour <= af_t->end.hour){
50		int cur_mins = t->tm_hour * 60 + t->tm_min;
51		if ((af_t->start.hour * 60 + af_t->start.min > cur_mins )
52			|| (cur_mins > af_t->end.hour * 60 + af_t->end.min)){
53			enable = 0;
54		}
55	}
56	else
57		enable = 0;
58EXIT:
59	if (enable){
60		system("echo 1 >/proc/sys/oaf/enable ");
61	}
62	else
63		system("echo 0 >/proc/sys/oaf/enable ");
64	free(af_t);
65}
66
67void dev_list_timeout_handler(struct uloop_timeout *t){
68    dump_dev_list();
69//	dump_dev_visit_list();
70	check_appfilter_enable();
71//todo: dev list expire
72	uloop_timeout_set(t, 10000);
73}
74
75struct uloop_timeout dev_tm={
76	.cb = dev_list_timeout_handler
77};
78
79static struct uloop_fd appfilter_nl_fd = {
80	.cb = appfilter_nl_handler,
81};
82
83int main(int argc, char **argv)
84{
85    int ret = 0;
86	uloop_init();
87    printf("init appfilter\n");
88    init_dev_node_htable();
89	init_app_name_table();
90	init_app_class_name_table();
91	if (appfilter_ubus_init() < 0) {
92		fprintf(stderr, "Failed to connect to ubus\n");
93		return 1;
94	}
95
96    appfilter_nl_fd.fd = appfilter_nl_init();
97    uloop_fd_add(&appfilter_nl_fd, ULOOP_READ);
98	af_msg_t msg;
99	msg.action = AF_MSG_INIT;
100
101	send_msg_to_kernel(appfilter_nl_fd.fd, (void *)&msg, sizeof(msg));
102	uloop_timeout_set(&dev_tm, 5000);
103    uloop_timeout_add(&dev_tm);
104	uloop_run();
105	uloop_done();
106	return 0;
107}
108
109