Load mac list from dhcp leases file

This commit is contained in:
Derry 2021-03-07 22:41:20 -08:00
parent 59aa2a1f40
commit 488298ccc8
6 changed files with 55 additions and 31 deletions

View File

@ -108,7 +108,7 @@ table.imagetable td {
tr.insertCell(-1).innerHTML = hostname;
tr.insertCell(-1).innerHTML = "<a href='<%=url('admin/network/appfilter/')%>"+devlist[i].mac+"'>"+devlist[i].mac+"</a>";
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;

View File

@ -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");

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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

View File

@ -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);
}