main.c revision f9d51c26
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> 33#include <netinet/in.h> 34#include <arpa/inet.h> 35#include "appfilter.h" 36 37 38void check_appfilter_enable(void) 39{ 40 int enable = 1; 41 struct tm *t; 42 af_ctl_time_t *af_t = NULL; 43 time_t tt; 44 time(&tt); 45 enable = config_get_appfilter_enable(); 46 47 if (0 == enable) 48 goto EXIT; 49 af_t = load_appfilter_ctl_time_config(); 50 if (!af_t) 51 { 52 enable = 0; 53 goto EXIT; 54 } 55 56 t = localtime(&tt); 57 if (af_t->days[t->tm_wday] != 1) 58 { 59 if (af_t->time_mode == 0){ 60 enable = 0; 61 goto EXIT; 62 } 63 } 64 65 int cur_mins = t->tm_hour * 60 + t->tm_min; 66 if (((af_t->start.hour * 60 + af_t->start.min < cur_mins) && (cur_mins < af_t->end.hour * 60 + af_t->end.min)) 67 || ((af_t->start2.hour * 60 + af_t->start2.min < cur_mins) && (cur_mins < af_t->end2.hour * 60 + af_t->end2.min)) 68 ) 69 { 70 if (af_t->time_mode == 0){ 71 enable = 1; 72 } 73 else{ 74 enable = 0; 75 } 76 } 77 else{ 78 if (af_t->time_mode == 0){ 79 enable = 0; 80 } 81 else{ 82 enable = 1; 83 } 84 } 85EXIT: 86 if (enable) 87 { 88 system("echo 1 >/proc/sys/oaf/enable "); 89 } 90 else 91 system("echo 0 >/proc/sys/oaf/enable "); 92 if (af_t) 93 free(af_t); 94} 95 96void update_lan_ip(void){ 97 char ip_str[32] = {0}; 98 char mask_str[32] = {0}; 99 struct in_addr addr; 100 struct in_addr mask_addr; 101 char cmd_buf[128] = {0}; 102 u_int32_t lan_ip = 0; 103 u_int32_t lan_mask = 0; 104 105 exec_with_result_line(CMD_GET_LAN_IP, ip_str, sizeof(ip_str)); 106 if (strlen(ip_str) < MIN_INET_ADDR_LEN){ 107 sprintf(cmd_buf, "echo 0 >/proc/sys/oaf/lan_ip"); 108 } 109 else{ 110 inet_aton(ip_str, &addr); 111 lan_ip = addr.s_addr; 112 sprintf(cmd_buf, "echo %u >/proc/sys/oaf/lan_ip", lan_ip); 113 } 114 system(cmd_buf); 115 exec_with_result_line(CMD_GET_LAN_MASK, mask_str, sizeof(mask_str)); 116 117 if (strlen(mask_str) < MIN_INET_ADDR_LEN){ 118 sprintf(cmd_buf, "echo 0 >/proc/sys/oaf/lan_mask"); 119 } 120 else{ 121 inet_aton(mask_str, &mask_addr); 122 lan_mask = mask_addr.s_addr; 123 sprintf(cmd_buf, "echo %u >/proc/sys/oaf/lan_mask", lan_mask); 124 } 125 system(cmd_buf); 126} 127 128void dev_list_timeout_handler(struct uloop_timeout *t) 129{ 130 dump_dev_list(); 131 check_dev_visit_info_expire(); 132 flush_expire_visit_info(); 133 //dump_dev_visit_list(); 134 update_lan_ip(); 135 check_appfilter_enable(); 136 if (check_dev_expire()){ 137 flush_expire_visit_info(); 138 flush_dev_expire_node(); 139 } 140 uloop_timeout_set(t, 10000); 141} 142 143struct uloop_timeout dev_tm = { 144 .cb = dev_list_timeout_handler}; 145 146static struct uloop_fd appfilter_nl_fd = { 147 .cb = appfilter_nl_handler, 148}; 149 150int main(int argc, char **argv) 151{ 152 int ret = 0; 153 uloop_init(); 154 printf("init appfilter\n"); 155 init_dev_node_htable(); 156 init_app_name_table(); 157 init_app_class_name_table(); 158 if (appfilter_ubus_init() < 0) 159 { 160 fprintf(stderr, "Failed to connect to ubus\n"); 161 return 1; 162 } 163 164 165 appfilter_nl_fd.fd = appfilter_nl_init(); 166 uloop_fd_add(&appfilter_nl_fd, ULOOP_READ); 167 af_msg_t msg; 168 msg.action = AF_MSG_INIT; 169 send_msg_to_kernel(appfilter_nl_fd.fd, (void *)&msg, sizeof(msg)); 170 uloop_timeout_set(&dev_tm, 5000); 171 uloop_timeout_add(&dev_tm); 172 uloop_run(); 173 uloop_done(); 174 return 0; 175} 176