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