main.c revision c5e4f1c9
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 // dump_dev_visit_list(); 81 check_appfilter_enable(); 82 //todo: dev list expire 83 uloop_timeout_set(t, 10000); 84} 85 86struct uloop_timeout dev_tm = { 87 .cb = dev_list_timeout_handler}; 88 89static struct uloop_fd appfilter_nl_fd = { 90 .cb = appfilter_nl_handler, 91}; 92 93int main(int argc, char **argv) 94{ 95 int ret = 0; 96 uloop_init(); 97 printf("init appfilter\n"); 98 init_dev_node_htable(); 99 init_app_name_table(); 100 init_app_class_name_table(); 101 if (appfilter_ubus_init() < 0) 102 { 103 fprintf(stderr, "Failed to connect to ubus\n"); 104 return 1; 105 } 106 107 appfilter_nl_fd.fd = appfilter_nl_init(); 108 uloop_fd_add(&appfilter_nl_fd, ULOOP_READ); 109 af_msg_t msg; 110 msg.action = AF_MSG_INIT; 111 112 send_msg_to_kernel(appfilter_nl_fd.fd, (void *)&msg, sizeof(msg)); 113 uloop_timeout_set(&dev_tm, 5000); 114 uloop_timeout_add(&dev_tm); 115 uloop_run(); 116 uloop_done(); 117 return 0; 118} 119