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