main.c revision 59aa2a1f
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		printf("cur weekday not match");
47		enable = 0;
48		goto EXIT;
49	}
50	if (af_t->start.hour <= af_t->end.hour){
51		int cur_mins = t->tm_hour * 60 + t->tm_min;
52		if ((af_t->start.hour * 60 + af_t->start.min > cur_mins )
53			|| (cur_mins > af_t->end.hour * 60 + af_t->end.min)){
54			enable = 0;
55			printf(" not match hour and min\n");
56		}
57	}
58	else
59		enable = 0;
60EXIT:
61	if (enable){
62		system("echo 1 >/proc/sys/oaf/enable ");
63	}
64	else
65		system("echo 0 >/proc/sys/oaf/enable ");
66	free(af_t);
67}
68
69void dev_list_timeout_handler(struct uloop_timeout *t){
70    dump_dev_list();
71	dump_dev_visit_list();
72	check_appfilter_enable();
73	uloop_timeout_set(t, 10000);
74}
75
76struct uloop_timeout dev_tm={
77	.cb = dev_list_timeout_handler
78};
79
80static struct uloop_fd appfilter_nl_fd = {
81	.cb = appfilter_nl_handler,
82};
83
84int main(int argc, char **argv)
85{
86    int ret = 0;
87	uloop_init();
88    printf("init appfilter\n");
89    init_dev_node_htable();
90	init_app_name_table();
91	init_app_class_name_table();
92	if (appfilter_ubus_init() < 0) {
93		fprintf(stderr, "Failed to connect to ubus\n");
94		return 1;
95	}
96
97    appfilter_nl_fd.fd = appfilter_nl_init();
98    uloop_fd_add(&appfilter_nl_fd, ULOOP_READ);
99	uloop_timeout_set(&dev_tm, 5000);
100    uloop_timeout_add(&dev_tm);
101	uloop_run();
102	uloop_done();
103	return 0;
104}
105
106