From 488298ccc8feb90e2b2af3b2f294180ffbc1a632 Mon Sep 17 00:00:00 2001 From: Derry Date: Sun, 7 Mar 2021 22:41:20 -0800 Subject: [PATCH] Load mac list from dhcp leases file --- .../luasrc/view/admin_network/user_status.htm | 2 +- open-app-filter/src/appfilter_netlink.c | 5 +++ open-app-filter/src/appfilter_ubus.c | 37 ++++++------------- open-app-filter/src/appfilter_user.c | 36 +++++++++++++++++- open-app-filter/src/appfilter_user.h | 1 + open-app-filter/src/main.c | 5 +-- 6 files changed, 55 insertions(+), 31 deletions(-) diff --git a/luci-app-oaf/luasrc/view/admin_network/user_status.htm b/luci-app-oaf/luasrc/view/admin_network/user_status.htm index 7d178a4..cf0834e 100755 --- a/luci-app-oaf/luasrc/view/admin_network/user_status.htm +++ b/luci-app-oaf/luasrc/view/admin_network/user_status.htm @@ -108,7 +108,7 @@ table.imagetable td { tr.insertCell(-1).innerHTML = hostname; tr.insertCell(-1).innerHTML = ""+devlist[i].mac+"'>"+devlist[i].mac+""; tr.insertCell(-1).innerHTML = devlist[i].ip; - var app_list_str=""; + var app_list_str="--"; for (var j = 0; j < devlist[i].applist.length; j++){ console.log(devlist[i].applist[j].name); app_list_str+=devlist[i].applist[j].name; diff --git a/open-app-filter/src/appfilter_netlink.c b/open-app-filter/src/appfilter_netlink.c index bfc02d7..763ece0 100755 --- a/open-app-filter/src/appfilter_netlink.c +++ b/open-app-filter/src/appfilter_netlink.c @@ -97,6 +97,11 @@ void appfilter_nl_handler(struct uloop_fd *u, unsigned int ev) if (!node){ node = add_dev_node(mac); + if (!node){ + printf("add dev node failed\n"); + json_object_put(root); + return; + } } struct json_object *ip_obj = json_object_object_get(root, "ip"); diff --git a/open-app-filter/src/appfilter_ubus.c b/open-app-filter/src/appfilter_ubus.c index 0ff24fa..e125db0 100755 --- a/open-app-filter/src/appfilter_ubus.c +++ b/open-app-filter/src/appfilter_ubus.c @@ -51,17 +51,12 @@ void get_hostname_by_mac(char *mac, char *hostname){ } char line_buf[256] = {0}; while(fgets(line_buf, sizeof(line_buf), fp)){ - printf("line buf = %s\n", line_buf); - char hostname_buf[128] = {0}; - char mac_buf[32] = {0}; - sscanf(line_buf, "%*s %s %*s %s", mac_buf, hostname_buf); - if (0 == strcmp(mac, mac_buf)){ - printf("match mac:%s\n", mac_buf); - strcpy(hostname, hostname_buf); - } - else{ - printf("not match mac:%s\n", mac_buf); - } + char hostname_buf[128] = {0}; + char mac_buf[32] = {0}; + sscanf(line_buf, "%*s %s %*s %s", mac_buf, hostname_buf); + if (0 == strcmp(mac, mac_buf)){ + strcpy(hostname, hostname_buf); + } } fclose(fp); @@ -214,23 +209,11 @@ appfilter_handle_visit_list(struct ubus_context *ctx, struct ubus_object *obj, } - -#if 0 -blobmsg_add_string(b, "hostname", "unknown"); -blobmsg_add_string(b, "mac", node->mac); -blobmsg_add_string(b, "ip", node->ip); -blobmsg_add_string(b, "appname", "unknown"); -blobmsg_add_u32(b, "appid", p_info->appid); -blobmsg_add_u32(b, "latest_action", p_info->action); -blobmsg_add_u32(b, "first_time", p_info->first_time); -blobmsg_add_u32(b, "latest_time", p_info->latest_time); - -#endif - typedef struct app_visit_time_info{ int app_id; unsigned long long total_time; }app_visit_time_info_t; + int visit_time_compare(const void *a, const void *b){ app_visit_time_info_t *p1 = (app_visit_time_info_t *)a; app_visit_time_info_t *p2 = (app_visit_time_info_t *)b; @@ -257,14 +240,16 @@ void update_top5_app(dev_node_t *node, app_visit_time_info_t top5_app_list[]){ qsort((void *)app_visit_array, app_visit_num, sizeof(app_visit_time_info_t), visit_time_compare); + #if 0 for (i = 0; i < app_visit_num; i++){ printf("appid %d-----------total time %llu\n", app_visit_array[i].app_id, app_visit_array[i].total_time); } + #endif for (i = 0; i < 5; i++){ top5_app_list[i] = app_visit_array[i]; - printf("appid %d-----------total time %llu\n", app_visit_array[i].app_id, - app_visit_array[i].total_time); + //printf("appid %d-----------total time %llu\n", app_visit_array[i].app_id, + // app_visit_array[i].total_time); } } diff --git a/open-app-filter/src/appfilter_user.c b/open-app-filter/src/appfilter_user.c index 15e785f..bbb3347 100755 --- a/open-app-filter/src/appfilter_user.c +++ b/open-app-filter/src/appfilter_user.c @@ -32,7 +32,7 @@ #include "appfilter_user.h" dev_node_t *dev_hash_table[MAX_DEV_NODE_HASH_SIZE]; - +int g_cur_user_num = 0; unsigned int hash_mac(unsigned char *mac) { if (!mac) @@ -64,6 +64,10 @@ void init_dev_node_htable(){ dev_node_t *add_dev_node(char *mac){ unsigned int hash = 0; + if (g_cur_user_num >= MAX_SUPPORT_USER_NUM){ + printf("error, user num reach max %d\n", g_cur_user_num); + return NULL; + } hash = hash_mac(mac); if (hash >= MAX_DEV_NODE_HASH_SIZE){ printf("hash code error %d\n", hash); @@ -80,6 +84,7 @@ dev_node_t *add_dev_node(char *mac){ node->next = dev_hash_table[hash]; dev_hash_table[hash] = node; } + g_cur_user_num++; printf("add mac:%s to htable[%d]....success\n", mac, hash); return node; } @@ -124,9 +129,38 @@ char * format_time(int timetamp){ return strdup(time_buf); } +void update_dev_from_dhcp_lease_file(void){ + char line_buf[256] = {0}; + char hostname_buf[128] = {0}; + char mac_buf[32] = {0}; + char ip_buf[32] = {0}; + + FILE *fp = fopen("/tmp/dhcp.leases", "r"); + if (!fp){ + printf("open dhcp lease file....failed\n"); + return; + } + while(fgets(line_buf, sizeof(line_buf), fp)){ + if (strlen(line_buf) <= 16) + continue; + sscanf(line_buf, "%*s %s %s %s", mac_buf, ip_buf, hostname_buf); + dev_node_t *node = find_dev_node(mac_buf); + if (!node){ + node = add_dev_node(mac_buf); + } + if (node && strlen(ip_buf) > 0) + strcpy(node->ip, ip_buf); + + if (node && strlen(hostname_buf) > 0) + strcpy(node->hostname, hostname_buf); + } + fclose(fp); +} + void dump_dev_list(void){ int i, j; int count = 0; + update_dev_from_dhcp_lease_file(); FILE *fp = fopen(OAF_DEV_LIST_FILE, "w"); if (!fp){ return; diff --git a/open-app-filter/src/appfilter_user.h b/open-app-filter/src/appfilter_user.h index 41b8140..76c3738 100755 --- a/open-app-filter/src/appfilter_user.h +++ b/open-app-filter/src/appfilter_user.h @@ -26,6 +26,7 @@ #define MAX_VISIT_HASH_SIZE 64 #define MAX_DEV_NODE_HASH_SIZE 64 #define MAX_HOSTNAME_SIZE 64 +#define MAX_SUPPORT_USER_NUM 64 #define OAF_VISIT_LIST_FILE "/tmp/visit_list" #define OAF_DEV_LIST_FILE "/tmp/dev_list" #define MIN_VISIT_TIME 5 // default 5s diff --git a/open-app-filter/src/main.c b/open-app-filter/src/main.c index 4712e39..a2d8bd8 100755 --- a/open-app-filter/src/main.c +++ b/open-app-filter/src/main.c @@ -43,7 +43,6 @@ void check_appfilter_enable(void){ t = localtime(&tt); if (af_t->days[t->tm_wday] != 1){ - printf("cur weekday not match"); enable = 0; goto EXIT; } @@ -52,7 +51,6 @@ void check_appfilter_enable(void){ if ((af_t->start.hour * 60 + af_t->start.min > cur_mins ) || (cur_mins > af_t->end.hour * 60 + af_t->end.min)){ enable = 0; - printf(" not match hour and min\n"); } } else @@ -68,8 +66,9 @@ EXIT: void dev_list_timeout_handler(struct uloop_timeout *t){ dump_dev_list(); - dump_dev_visit_list(); +// dump_dev_visit_list(); check_appfilter_enable(); +//todo: dev list expire uloop_timeout_set(t, 10000); }