main.c revision 10e8c008
1/* 2 Copyright (C) 2020 Derry <destan19@126.com> 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy 5 of this software and associated documentation files (the "Software"), to deal 6 in the Software without restriction, including without limitation the rights 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 copies of the Software, and to permit persons to whom the Software is 9 furnished to do so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in 12 all copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 THE 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 int enable = 1; 35 struct tm *t; 36 time_t tt; 37 time(&tt); 38 enable = config_get_appfilter_enable(); 39 if (0 == enable) 40 goto EXIT; 41 af_ctl_time_t *af_t = load_appfilter_ctl_time_config(); 42 if (!af_t){ 43 enable = 0; 44 goto EXIT; 45 } 46 47 t = localtime(&tt); 48 if (af_t->days[t->tm_wday] != 1){ 49 enable = 0; 50 goto EXIT; 51 } 52 if (af_t->start.hour <= af_t->end.hour){ 53 int cur_mins = t->tm_hour * 60 + t->tm_min; 54 if ((af_t->start.hour * 60 + af_t->start.min > cur_mins ) 55 || (cur_mins > af_t->end.hour * 60 + af_t->end.min)){ 56 enable = 0; 57 } 58 } 59 else 60 enable = 0; 61EXIT: 62 if (enable){ 63 system("echo 1 >/proc/sys/oaf/enable "); 64 } 65 else 66 system("echo 0 >/proc/sys/oaf/enable "); 67 free(af_t); 68} 69 70void dev_list_timeout_handler(struct uloop_timeout *t){ 71 dump_dev_list(); 72// dump_dev_visit_list(); 73 check_appfilter_enable(); 74//todo: dev list expire 75 uloop_timeout_set(t, 10000); 76} 77 78struct uloop_timeout dev_tm={ 79 .cb = dev_list_timeout_handler 80}; 81 82static struct uloop_fd appfilter_nl_fd = { 83 .cb = appfilter_nl_handler, 84}; 85 86int main(int argc, char **argv) 87{ 88 int ret = 0; 89 uloop_init(); 90 printf("init appfilter\n"); 91 init_dev_node_htable(); 92 init_app_name_table(); 93 init_app_class_name_table(); 94 if (appfilter_ubus_init() < 0) { 95 fprintf(stderr, "Failed to connect to ubus\n"); 96 return 1; 97 } 98 99 appfilter_nl_fd.fd = appfilter_nl_init(); 100 uloop_fd_add(&appfilter_nl_fd, ULOOP_READ); 101 af_msg_t msg; 102 msg.action = AF_MSG_INIT; 103 104 send_msg_to_kernel(appfilter_nl_fd.fd, (void *)&msg, sizeof(msg)); 105 uloop_timeout_set(&dev_tm, 5000); 106 uloop_timeout_add(&dev_tm); 107 uloop_run(); 108 uloop_done(); 109 return 0; 110} 111 112