secgateway/Product/modules/match_rule/k-userhash.c

173 lines
3.9 KiB
C

#include "k-userhash.h"
#include <linux/slab.h>
#include <asm/errno.h>
#include <linux/kthread.h>
/*声明读写锁 */
DEFINE_RWLOCK(g_obj_lock);
/*定义大小为HASH_SIZE的hashtable */
struct list_head khash_array[HASH_SIZE];
/*init hashtable */
void init_hashMap(void)
{
int i;
for(i = 0; i < HASH_SIZE; i++)
INIT_LIST_HEAD(&khash_array[i]);
}
/*获取hashtable位置索引 */
int get_hash_index(__u32 ip)
{
int val = ip % HASH_SIZE;
return val;
}
/* search node */
K_USERINFO *search_user(__u32 ip)
{
int index;
struct list_head *pos;
K_USERINFO *pNode;
index = get_hash_index(ip);
/*查找用户IP */
read_lock(&g_obj_lock);
list_for_each(pos, &khash_array[index])
{
/* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type:the type of the struct this is embedded in.
* @member: the name of the list_head within the struct. */
pNode = list_entry(pos, K_USERINFO, hnode);
if((pNode != NULL) && (ip == pNode->k_user.kuser_ip))
{
printk("user ip:%ld\n", pNode->k_user.kuser_ip);
return pNode;
}
}
read_unlock(&g_obj_lock);
return NULL;
}
/*add user */
int add_user(__u32 ip, char name, int id, int gid, __u64 mes_num, __u64 bnum, __kernel_time_t time)
{
int index;
K_USERINFO *pNode = NULL;
/*获取hashtable位置索引 */
index = get_hash_index(ip);
pNode = search_user(ip);
if(NULL == pNode)
{
/*分配存储空间——GFP_KERNEL内核内存的正常分配. 可能睡眠. */
pNode = (K_USERINFO *)kmalloc(sizeof(K_USERINFO), GFP_KERNEL);
if (pNode == NULL)
{
return ENOMEM;
}
pNode->k_user.kuser_ip = ip;
list_add_tail(&pNode->hnode, &khash_array[index]);
}
else
{
printk("IP ALEADY EXISTED\n");
}
memcpy(pNode->k_user.kuser_name, name, sizeof(char)* 63);
pNode->k_user.kuser_id = id;
pNode->k_user.kgroup_id =gid;
pNode->k_user.kmessage_num =mes_num;
pNode->k_user.kbyte_num = bnum;
pNode->k_user.konline_time = time;
return 0;
}
/*delete a node */
void del_user(__u32 ip)
{
int index;
struct list_head *pos, *n;
K_USERINFO *pNode;
index = get_hash_index(ip);
/*判断hashtable中该单元是否为空 */
if( list_empty(&khash_array[index]))
{
printk("node is NULL\n");
return;
}
/*查找用户ip */
write_lock(&g_obj_lock);
list_for_each_safe(pos, n, &khash_array[index])
{
pNode = list_entry(pos, K_USERINFO, hnode);
if((pNode != NULL) && (ip == pNode->k_user.kuser_ip))
{
list_del(&pNode->hnode);
kfree(pNode);
}
}
write_unlock(&g_obj_lock);
return;
}
/*delete all node */
void free_all_user(void)
{
int i;
struct list_head *pos, *n;
K_USERINFO *pNode;
/*判断hashtable单元是否为NULL */
for(i = 0; i < HASH_SIZE; i++)
{
if(list_empty(&khash_array[i]))
continue;
write_lock(&g_obj_lock);
list_for_each_safe(pos, n, &khash_array[i])
{
pNode = list_entry(pos, K_USERINFO, hnode);
list_del(&pNode->hnode);
kfree(pNode);
}
write_unlock(&g_obj_lock);
}
}
/*printf all nodes */
void printk_all_user(void)
{
int i;
struct list_head *pos, *n;
K_USERINFO * pNode;
for(i = 0; i < HASH_SIZE; i++)
continue;
read_lock(&g_obj_lock);
list_for_each_safe(pos, n, &khash_array[i])
{
pNode = list_entry(pos, K_USERINFO, hnode);
printk("%d %s %d %d %d %d %d\n", pNode->k_user.kuser_ip, pNode->k_user.kuser_name, pNode->k_user.kuser_id, pNode->k_user.kgroup_id,
pNode->k_user.kmessage_num, pNode->k_user.kbyte_num, pNode->k_user.konline_time);
}
read_unlock(&g_obj_lock);
}