From 5859a3d2522c03ce00e3cf11d81783817aaaa088 Mon Sep 17 00:00:00 2001 From: ChenLing Date: Thu, 26 Sep 2019 16:07:47 +0800 Subject: [PATCH] =?UTF-8?q?Mod=20=20aaa-12=20=E4=BF=AE=E6=94=B9web=20serve?= =?UTF-8?q?r=E7=94=A8=E6=88=B7=E6=80=81hash=E8=A1=A8=20RCA=EF=BC=9A=20SOL?= =?UTF-8?q?=EF=BC=9A=20=E4=BF=AE=E6=94=B9=E4=BA=BA=EF=BC=9Achenling=20?= =?UTF-8?q?=E6=A3=80=E8=A7=86=E4=BA=BA=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/src/lighttpd-1.4.51/src/user_hashtable.c | 74 ++++++++++--------- libs/src/lighttpd-1.4.51/src/user_hashtable.h | 10 +-- 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/libs/src/lighttpd-1.4.51/src/user_hashtable.c b/libs/src/lighttpd-1.4.51/src/user_hashtable.c index 51a7c3134..f71202171 100644 --- a/libs/src/lighttpd-1.4.51/src/user_hashtable.c +++ b/libs/src/lighttpd-1.4.51/src/user_hashtable.c @@ -12,11 +12,11 @@ static server* g_svr = NULL; extern USER_AUTH_LIST g_user_auth_ret_table[]; +#if 0 /*链表全局变量 */ struct hlist_head *hash; USER_INFO *pNode ; - /*计算hash值 */ struct hlist_head * call_hash(struct hlist_head *hash, uint32_t ip) { @@ -25,21 +25,10 @@ struct hlist_head * call_hash(struct hlist_head *hash, uint32_t ip) return &hash[val]; } - /*初始化函数 */ int Init_hash() { int i = 0; - //struct hlist_node *p = NULL, *n = NULL; - - #if 0 - USER_INFO uInfo[100]; - - for(i = 0; i < 100; i++) { - memset(&uInfo[i], 0, sizeof(USER_INFO)); - uInfo[i].val = i; - } - #endif /*创建hash头 */ hash = (struct hlist_head*)malloc(sizeof(*hash)*100); @@ -53,17 +42,6 @@ int Init_hash() for(i = 0; i < USERNUM_MAX; i++) INIT_HLIST_HEAD(&hash[i]); - #if 0 - for(i = 0; i < 100; i++) { - hlist_add_head(&uInfo[i].hnode, &hash[0]); - } - - hlist_for_each_safe(p, n, &hash[0]) { - USER_INFO* pV = hlist_entry(p, struct user_info, hnode); - log_error_write(g_svr, __FILE__, __LINE__, "dsd", i, ":", pV->val); - } - #endif - /*hsah桶普通节点分配内存 */ pNode = (struct user_info *)malloc(sizeof(struct user_info)); if (NULL == pNode) @@ -78,19 +56,44 @@ int Init_hash() return 0; } +#endif + +/*定义大小为HASH_SIZE的hashtable */ +struct hlist_head hash[USERNUM_MAX]; + +/*获取hashtable位置索引 */ +int get_uhash_index(uint32_t ip) +{ + int val = ip % USERNUM_MAX; + return val; +} + +/*初始化函数 */ +void Init_hash() +{ + int i; + for(i = 0; i < USERNUM_MAX; i++) + { + INIT_HLIST_HEAD(&hash[i]); + } + + return; +} /*通过IP查找在线用户信息*/ USER_INFO *ufind_user(uint32_t user_ip) { struct hlist_node *p = NULL, *n = NULL; + USER_INFO *pNode; log_error_write(g_svr, __FILE__, __LINE__, "s", "test"); /* 这个实际上就是一个for循环,从头到尾遍历链表。 * pos:struct hlist_node类型的一个指针; * n:struct hlist_node类型的一个指针; * head:struct hlist_head类型的一个指针,表示hlist链表的头结点。 */ - hlist_for_each_safe(p, n, call_hash(hash, user_ip)) + int index = get_uhash_index(user_ip); + hlist_for_each_safe(p, n, &hash[index]) { log_error_write(g_svr, __FILE__, __LINE__, "s", "test"); @@ -115,16 +118,19 @@ USER_INFO *ufind_user(uint32_t user_ip) USER_INFO *ufind_user_by_ippage(int page_num, int count, uint32_t user_ip) { struct hlist_node *p = NULL, *n = NULL; + USER_INFO *pNode; int usernum = 0; int num_begin; int num_end; + int index; log_error_write(g_svr, __FILE__, __LINE__, "s", "test"); /* 这个实际上就是一个for循环,从头到尾遍历链表。 * pos:struct hlist_node类型的一个指针; * n:struct hlist_node类型的一个指针; * head:struct hlist_head类型的一个指针,表示hlist链表的头结点。 */ - hlist_for_each_safe(p, n, call_hash(hash, user_ip)) + index = get_uhash_index(user_ip); + hlist_for_each_safe(p, n, &hash[index]) { log_error_write(g_svr, __FILE__, __LINE__, "s", "test"); /* p:表示struct hlist_node类型的一个地址。 @@ -158,11 +164,10 @@ USER_INFO *ufind_user_by_ippage(int page_num, int count, uint32_t user_ip) int uadd_user(uint32_t user_ip, char *name, int user_id, int group_id, uint64_t message_num, uint64_t byte_num, time_t online_time) { struct hlist_node *pos = NULL, *n = NULL; - pNode = NULL; - struct hlist_head* pVal = call_hash(hash, user_ip); + USER_INFO *pNode = NULL; + int index = get_uhash_index(user_ip); - //hlist_for_each_safe(p,n,call_hash(hash, user_ip)) /*查找ip是否存在hash表中 */ - for(pos = pVal->first; pos && ({ n = pos->next; 1; }); pos = n) + hlist_for_each_safe(pos, n, &hash[index]) /*查找ip是否存在hash表中 */ { pNode = hlist_entry(pos, struct user_info ,hnode); if((pNode != NULL) && (user_ip == pNode->auth_user.user_ip)) @@ -189,7 +194,7 @@ int uadd_user(uint32_t user_ip, char *name, int user_id, int group_id, uint64_t memset(pNode,0,sizeof(struct user_info)); INIT_HLIST_NODE(&pNode->hnode); pNode->auth_user.user_ip = user_ip; - hlist_add_head(&pNode->hnode, call_hash(hash, user_ip)); + hlist_add_head(&pNode->hnode, &hash[index]); log_error_write(g_svr, __FILE__, __LINE__, "s", "test"); } @@ -208,12 +213,12 @@ int uadd_user(uint32_t user_ip, char *name, int user_id, int group_id, uint64_t void udelete_user(int user_ip) { unsigned short check_id; - USER_INFO *result; + USER_INFO *pNode; /*查找要删除的用户IP和端口号*/ - result = ufind_user(user_ip); + pNode = ufind_user(user_ip); - if(NULL != result) + if(NULL != pNode) { /*查找用户ID,确认ID是否存在 */ check_id = g_user_auth_ret_table[pNode->auth_user.user_id].group_id; @@ -232,6 +237,7 @@ void udelete_all() { struct hlist_node *p = NULL, *n = NULL ; int i = 0; + USER_INFO *pNode; for(i = 0; i < USERNUM_MAX; i++) { @@ -250,6 +256,7 @@ int uprintf_users() struct hlist_node *p = NULL, *n = NULL ; int i = 0; int online_num = 0; + USER_INFO *pNode; log_error_write(g_svr, __FILE__, __LINE__, "s", "test"); for(i = 0; i < USERNUM_MAX; i++) @@ -286,6 +293,7 @@ void ufind_user_by_name(int page_num, int count, char *user_name, cJSON **pJson) struct hlist_node *p = NULL, *n = NULL ; cJSON *pJsonsub; cJSON *pJsonArry = *pJson; + USER_INFO *pNode; int usernum = 0; int num_begin; diff --git a/libs/src/lighttpd-1.4.51/src/user_hashtable.h b/libs/src/lighttpd-1.4.51/src/user_hashtable.h index 0e0b2d533..7b171be90 100644 --- a/libs/src/lighttpd-1.4.51/src/user_hashtable.h +++ b/libs/src/lighttpd-1.4.51/src/user_hashtable.h @@ -1,5 +1,5 @@ -#ifndef K_HASHTABLE_H -#define K_HASHTABLE_H +#ifndef U_HASHTABLE_H +#define U_HASHTABLE_H #include #include "hlist.h" #include @@ -30,11 +30,11 @@ typedef struct user_info{ }USER_INFO; -/*计算hash值 */ -struct hlist_head * call_hash(struct hlist_head *hash, uint32_t ip); +/*获取hashtable位置索引 */ +int get_uhash_index(uint32_t ip); /*初始化函数 */ -int Init_hash(); +void Init_hash(); /*通过IP查找在线用户信息*/ USER_INFO *ufind_user(uint32_t user_ip);