1f9d51c26Sderry/*
2f9d51c26SderryCopyright (C) 2020 Derry <destan19@126.com>
3f9d51c26Sderry
4f9d51c26SderryPermission is hereby granted, free of charge, to any person obtaining a copy
5f9d51c26Sderryof this software and associated documentation files (the "Software"), to deal
6f9d51c26Sderryin the Software without restriction, including without limitation the rights
7f9d51c26Sderryto use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8f9d51c26Sderrycopies of the Software, and to permit persons to whom the Software is
9f9d51c26Sderryfurnished to do so, subject to the following conditions:
10f9d51c26Sderry
11f9d51c26SderryThe above copyright notice and this permission notice shall be included in
12f9d51c26Sderryall copies or substantial portions of the Software.
13f9d51c26Sderry
14f9d51c26SderryTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15f9d51c26SderryIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16f9d51c26SderryFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17f9d51c26SderryAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18f9d51c26SderryLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19f9d51c26SderryOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20f9d51c26SderryTHE SOFTWARE.
21f9d51c26Sderry*/
22f9d51c26Sderry#include <unistd.h>
23f9d51c26Sderry#include <stdlib.h>
24f9d51c26Sderry#include <string.h>
25f9d51c26Sderry#include <libubox/uloop.h>
26f9d51c26Sderry#include <libubox/utils.h>
27f9d51c26Sderry#include <libubus.h>
28f9d51c26Sderry#include "appfilter_user.h"
29f9d51c26Sderry#include "appfilter_netlink.h"
30f9d51c26Sderry#include "appfilter_ubus.h"
31f9d51c26Sderry#include "appfilter_config.h"
32f9d51c26Sderry#include <time.h>
33f9d51c26Sderry#include <netinet/in.h>
34f9d51c26Sderry#include <arpa/inet.h>
35f9d51c26Sderry#include "appfilter.h"
36f9d51c26Sderry
37f9d51c26Sderry
38f9d51c26Sderryvoid check_appfilter_enable(void)
39f9d51c26Sderry{
40f9d51c26Sderry    int enable = 1;
41f9d51c26Sderry    struct tm *t;
42f9d51c26Sderry	af_ctl_time_t *af_t = NULL;
43f9d51c26Sderry    time_t tt;
44f9d51c26Sderry    time(&tt);
45f9d51c26Sderry    enable = config_get_appfilter_enable();
46f9d51c26Sderry
47f9d51c26Sderry    if (0 == enable)
48f9d51c26Sderry        goto EXIT;
49f9d51c26Sderry    af_t = load_appfilter_ctl_time_config();
50f9d51c26Sderry    if (!af_t)
51f9d51c26Sderry    {
52f9d51c26Sderry        enable = 0;
53f9d51c26Sderry        goto EXIT;
54f9d51c26Sderry    }
55f9d51c26Sderry
56f9d51c26Sderry    t = localtime(&tt);
57f9d51c26Sderry    if (af_t->days[t->tm_wday] != 1)
58f9d51c26Sderry    {
59f9d51c26Sderry        if (af_t->time_mode == 0){
60f9d51c26Sderry            enable = 0;
61f9d51c26Sderry            goto EXIT;
62f9d51c26Sderry        }
63f9d51c26Sderry    }
64f9d51c26Sderry
65f9d51c26Sderry    int cur_mins = t->tm_hour * 60 + t->tm_min;
66f9d51c26Sderry    if (((af_t->start.hour * 60 + af_t->start.min < cur_mins) && (cur_mins < af_t->end.hour * 60 + af_t->end.min))
67f9d51c26Sderry        || ((af_t->start2.hour * 60 + af_t->start2.min < cur_mins) && (cur_mins < af_t->end2.hour * 60 + af_t->end2.min))
68f9d51c26Sderry    )
69f9d51c26Sderry    {
70f9d51c26Sderry        if (af_t->time_mode == 0){
71f9d51c26Sderry            enable = 1;
72f9d51c26Sderry        }
73f9d51c26Sderry        else{
74f9d51c26Sderry            enable = 0;
75f9d51c26Sderry        }
76f9d51c26Sderry    }
77f9d51c26Sderry    else{
78f9d51c26Sderry        if (af_t->time_mode == 0){
79f9d51c26Sderry            enable = 0;
80f9d51c26Sderry        }
81f9d51c26Sderry        else{
82f9d51c26Sderry            enable = 1;
83f9d51c26Sderry        }
84f9d51c26Sderry    }
85f9d51c26SderryEXIT:
86f9d51c26Sderry    if (enable)
87f9d51c26Sderry    {
88f9d51c26Sderry        system("echo 1 >/proc/sys/oaf/enable ");
89f9d51c26Sderry    }
90f9d51c26Sderry    else
91f9d51c26Sderry        system("echo 0 >/proc/sys/oaf/enable ");
92f9d51c26Sderry	if (af_t)
93f9d51c26Sderry   		free(af_t);
94f9d51c26Sderry}
95f9d51c26Sderry
96f9d51c26Sderryvoid update_lan_ip(void){
97f9d51c26Sderry    char ip_str[32] = {0};
98f9d51c26Sderry	char mask_str[32] = {0};
99f9d51c26Sderry    struct in_addr addr;
100f9d51c26Sderry	struct in_addr mask_addr;
101f9d51c26Sderry    char cmd_buf[128] = {0};
102f9d51c26Sderry    u_int32_t lan_ip = 0;
103f9d51c26Sderry	u_int32_t lan_mask = 0;
104f9d51c26Sderry
105f9d51c26Sderry    exec_with_result_line(CMD_GET_LAN_IP, ip_str, sizeof(ip_str));
106f9d51c26Sderry    if (strlen(ip_str) < MIN_INET_ADDR_LEN){
107f9d51c26Sderry        sprintf(cmd_buf, "echo 0 >/proc/sys/oaf/lan_ip");
108f9d51c26Sderry    }
109f9d51c26Sderry    else{
110f9d51c26Sderry        inet_aton(ip_str, &addr);
111f9d51c26Sderry        lan_ip = addr.s_addr;
112f9d51c26Sderry        sprintf(cmd_buf, "echo %u >/proc/sys/oaf/lan_ip", lan_ip);
113f9d51c26Sderry    }
114f9d51c26Sderry	system(cmd_buf);
115f9d51c26Sderry    exec_with_result_line(CMD_GET_LAN_MASK, mask_str, sizeof(mask_str));
116f9d51c26Sderry
117f9d51c26Sderry    if (strlen(mask_str) < MIN_INET_ADDR_LEN){
118f9d51c26Sderry        sprintf(cmd_buf, "echo 0 >/proc/sys/oaf/lan_mask");
119f9d51c26Sderry    }
120f9d51c26Sderry    else{
121f9d51c26Sderry        inet_aton(mask_str, &mask_addr);
122f9d51c26Sderry        lan_mask = mask_addr.s_addr;
123f9d51c26Sderry        sprintf(cmd_buf, "echo %u >/proc/sys/oaf/lan_mask", lan_mask);
124f9d51c26Sderry    }
125f9d51c26Sderry    system(cmd_buf);
126f9d51c26Sderry}
127f9d51c26Sderry
128f9d51c26Sderryvoid dev_list_timeout_handler(struct uloop_timeout *t)
129f9d51c26Sderry{
130f9d51c26Sderry    dump_dev_list();
131f9d51c26Sderry    check_dev_visit_info_expire();
132f9d51c26Sderry    flush_expire_visit_info();
133f9d51c26Sderry    //dump_dev_visit_list();
134f9d51c26Sderry    update_lan_ip();
135f9d51c26Sderry    check_appfilter_enable();
136f9d51c26Sderry    if (check_dev_expire()){
137f9d51c26Sderry        flush_expire_visit_info();
138f9d51c26Sderry        flush_dev_expire_node();
139f9d51c26Sderry    }
140f9d51c26Sderry    uloop_timeout_set(t, 10000);
141f9d51c26Sderry}
142f9d51c26Sderry
143f9d51c26Sderrystruct uloop_timeout dev_tm = {
144f9d51c26Sderry    .cb = dev_list_timeout_handler};
145f9d51c26Sderry
146f9d51c26Sderrystatic struct uloop_fd appfilter_nl_fd = {
147f9d51c26Sderry    .cb = appfilter_nl_handler,
148f9d51c26Sderry};
149f9d51c26Sderry
150f9d51c26Sderryint main(int argc, char **argv)
151f9d51c26Sderry{
152f9d51c26Sderry    int ret = 0;
153f9d51c26Sderry    uloop_init();
154f9d51c26Sderry    printf("init appfilter\n");
155f9d51c26Sderry    init_dev_node_htable();
156f9d51c26Sderry    init_app_name_table();
157f9d51c26Sderry    init_app_class_name_table();
158f9d51c26Sderry    if (appfilter_ubus_init() < 0)
159f9d51c26Sderry    {
160f9d51c26Sderry        fprintf(stderr, "Failed to connect to ubus\n");
161f9d51c26Sderry        return 1;
162f9d51c26Sderry    }
163f9d51c26Sderry
164f9d51c26Sderry
165f9d51c26Sderry    appfilter_nl_fd.fd = appfilter_nl_init();
166f9d51c26Sderry    uloop_fd_add(&appfilter_nl_fd, ULOOP_READ);
167f9d51c26Sderry    af_msg_t msg;
168f9d51c26Sderry    msg.action = AF_MSG_INIT;
169f9d51c26Sderry    send_msg_to_kernel(appfilter_nl_fd.fd, (void *)&msg, sizeof(msg));
170f9d51c26Sderry    uloop_timeout_set(&dev_tm, 5000);
171f9d51c26Sderry    uloop_timeout_add(&dev_tm);
172f9d51c26Sderry    uloop_run();
173f9d51c26Sderry    uloop_done();
174f9d51c26Sderry    return 0;
175f9d51c26Sderry}
176