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