main.c revision 837b8830
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 if (0 == enable) 42 goto EXIT; 43 af_t = load_appfilter_ctl_time_config(); 44 if (!af_t) 45 { 46 enable = 0; 47 goto EXIT; 48 } 49 50 t = localtime(&tt); 51 if (af_t->days[t->tm_wday] != 1) 52 { 53 enable = 0; 54 goto EXIT; 55 } 56 if (af_t->start.hour <= af_t->end.hour) 57 { 58 int cur_mins = t->tm_hour * 60 + t->tm_min; 59 if ((af_t->start.hour * 60 + af_t->start.min > cur_mins) || (cur_mins > af_t->end.hour * 60 + af_t->end.min)) 60 { 61 enable = 0; 62 } 63 } 64 else 65 enable = 0; 66EXIT: 67 if (enable) 68 { 69 system("echo 1 >/proc/sys/oaf/enable "); 70 } 71 else 72 system("echo 0 >/proc/sys/oaf/enable "); 73 if (af_t) 74 free(af_t); 75} 76 77void dev_list_timeout_handler(struct uloop_timeout *t) 78{ 79 dump_dev_list(); 80 81 check_dev_visit_info_expire(); 82 flush_expire_visit_info(); 83 //dump_dev_visit_list(); 84 check_appfilter_enable(); 85 //todo: dev list expire 86 uloop_timeout_set(t, 10000); 87} 88 89struct uloop_timeout dev_tm = { 90 .cb = dev_list_timeout_handler}; 91 92static struct uloop_fd appfilter_nl_fd = { 93 .cb = appfilter_nl_handler, 94}; 95 96int main(int argc, char **argv) 97{ 98 int ret = 0; 99 uloop_init(); 100 printf("init appfilter\n"); 101 init_dev_node_htable(); 102 init_app_name_table(); 103 init_app_class_name_table(); 104 if (appfilter_ubus_init() < 0) 105 { 106 fprintf(stderr, "Failed to connect to ubus\n"); 107 return 1; 108 } 109 110 appfilter_nl_fd.fd = appfilter_nl_init(); 111 uloop_fd_add(&appfilter_nl_fd, ULOOP_READ); 112 af_msg_t msg; 113 msg.action = AF_MSG_INIT; 114 115 send_msg_to_kernel(appfilter_nl_fd.fd, (void *)&msg, sizeof(msg)); 116 uloop_timeout_set(&dev_tm, 5000); 117 uloop_timeout_add(&dev_tm); 118 uloop_run(); 119 uloop_done(); 120 return 0; 121} 122