From 8614a46de33b5aeb10bcf35b100e380325de0334 Mon Sep 17 00:00:00 2001 From: ChenLing Date: Mon, 29 Jul 2019 10:35:08 +0800 Subject: [PATCH 1/2] =?UTF-8?q?ADD=20=20aaa-12=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=86=85=E6=A0=B8=E6=80=81hash=E8=A1=A8=E7=9A=84=E5=A2=9E?= =?UTF-8?q?=E5=88=A0=E6=94=B9=E6=9F=A5=E5=8A=9F=E8=83=BD=20RCA=EF=BC=9A=20?= =?UTF-8?q?SOL=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 --- Makefile | 22 +++- Product/build/module.khash.Makefile | 77 +++++++++++++ Product/modules/userhash/k-userhash.c | 156 ++++++++++++++++++++++++++ Product/modules/userhash/k-userhash.h | 37 ++++++ 4 files changed, 290 insertions(+), 2 deletions(-) create mode 100644 Product/build/module.khash.Makefile create mode 100644 Product/modules/userhash/k-userhash.c create mode 100644 Product/modules/userhash/k-userhash.h diff --git a/Makefile b/Makefile index 2e109ee7e..5e06bcfb2 100755 --- a/Makefile +++ b/Makefile @@ -28,9 +28,9 @@ MAKE_FLAGS += -j$(shell cat /proc/cpuinfo | grep processor | wc -l) endif endif -.PHONY : demo conntrack netlink trace openrpc redismq usermanager configm ulog database +.PHONY : demo conntrack netlink trace openrpc redismq usermanager configm ulog database webauth khashtable -all: demo conntrack netlink trace openrpc redismq usermanager configm ulog database +all: demo conntrack netlink trace openrpc redismq usermanager configm ulog database webauth khashtable ifeq ($(OPT), install) #$(shell `find ../release -name "*.zip" -delete`) @@ -180,3 +180,21 @@ else ifeq ($(OPT), install) else $(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.database.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=database endif + +webauth: +ifeq ($(OPT), clean) + $(MLOG)make $(MAKE_FLAGS) -C Product/build -f user.web-auth.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=webauth +else ifeq ($(OPT), install) + $(MLOG)make $(MAKE_FLAGS) -C Product/build -f user.web-auth.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=webauth +else + $(MLOG)make all $(MAKE_FLAGS) -C Product/build -f user.web-auth.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=webauth +endif + +khashtable: +ifeq ($(OPT), clean) + $(MLOG)make $(MAKE_FLAGS) -C Product/build -f module.khash.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=khashtable +else ifeq ($(OPT), install) + $(MLOG)make $(MAKE_FLAGS) -C Product/build -f module.khash.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=khashtable +else + $(MLOG)make all $(MAKE_FLAGS) -C Product/build -f module.khash.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=khashtable +endif \ No newline at end of file diff --git a/Product/build/module.khash.Makefile b/Product/build/module.khash.Makefile new file mode 100644 index 000000000..e8420b73c --- /dev/null +++ b/Product/build/module.khash.Makefile @@ -0,0 +1,77 @@ +# target name, the target name must have the same name of c source file +TARGET_NAME=khashtable + +# target +# for linux module driver: KO +# for application: EXE +# for dynamic library: DLL +TARGET_TYPE = KO + +# target object +# for application: APP +# for device driver: DRV +TARGET_OBJ = DRV + +# custom install dir +TARGET_BOX = + +#debug mode or release mode +DEBUG = TRUE + +PLAT_LINUX ?= TRUE +PLAT_ARM64 ?= FALSE + +VPATH = ../modules/userhash + +# source code + +# set the source file, don't used .o because of ... + +COMMON_SRCS = k-userhash.c + +# MRS Board Source Files +PLAT_LINUX_SRCS = $(COMMON_SRCS) +PLAT_ARM64_SRCS = $(COMMON_SRCS) + +# gcc CFLAGS +PLAT_ARM64_CFLAGS := -I../../Common -I../common +PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) + + +PLAT_ARM64_LDFLAGS := +PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS) + + +#gcc libs +ARM64_LIBS := +LINUX_LIBS := + +ifeq ($(PLAT_ARM64), TRUE) +DEPEND_LIB += +USER_CLEAN_ITEMS += +endif + +ifeq ($(PLAT_LINUX), TRUE) +DEPEND_LIB += +USER_CLEAN_ITEMS += +endif + +# this line must be at below of thus, because of... +include ../../Common/common.Makefile + +ifneq ($(MAKECMDGOALS), clean) +ifneq ($(MAKECMDGOALS), cleanall) +ifneq ($(notdir $(DEPEND_LIB)), $(wildcard $(DEPEND_LIB))) +$(shell $(CP) $(DEPEND_LIB) ./) +endif +endif +endif + +ifeq ($(MAKECMDGOALS), ) +$(shell find ./ -name "$(TARGET)-*.ko" -delete) +else +ifeq ($(MAKECMDGOALS), all) +$(shell find ./ -name "$(TARGET)-*.ko" -delete) +endif +endif + diff --git a/Product/modules/userhash/k-userhash.c b/Product/modules/userhash/k-userhash.c new file mode 100644 index 000000000..a9564c505 --- /dev/null +++ b/Product/modules/userhash/k-userhash.c @@ -0,0 +1,156 @@ +#include "k-userhash.h" +#include + +/*定义大小为HASH_SIZE的hashtable */ +static struct list_head hash_array[HASH_SIZE]; + +/*init hashtable */ +void init_hashMap(void) +{ + int i; + for(i = 0; i < HASH_SIZE; i++) + INIT_LIST_HEAD(&hash_array[i]); +} + +/*获取hashtable位置索引 */ +int get_hash_index(uint32_t ip) +{ + unsigned int val = ip % HASH_SIZE; + return val; +} + + +/* search node */ +USERINFO *search_user(uint32_t ip) +{ + int index; + struct list_head *pos; + USERINFO *pNode; + + index = get_hash_index(ip); + + /*判断表中该单元是否为NLL */ + /* list_empty - tests whether a list is empty*/ + /* @head: the list to test.*/ + if( list_empty(&hash_array[index])) + { + return NULL; + } + + /*查找用户IP */ + list_for_each(pos, &hash_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, USERINFO, hnode); + if(pNode != NULL) + printk("user ip:%d user id:%d\n", pNode->user_ip, pNode->user_id); + return pNode; + } + + return NULL; +} + + +/*add user */ +int add_user(uint32_t ip, int id) +{ + int index; + USERINFO *pNode = NULL; + + /*获取hashtable位置索引 */ + index = get_hash_index(ip); + pNode = search_user(ip); + + if( NULL == pNode) + { + /*分配存储空间——GFP_KERNEL内核内存的正常分配. 可能睡眠. */ + pNode = (USERINFO *)kmalloc(sizeof(USERINFO), GFP_KERNEL); + if (pNode == NULL) + { + return -1; + } + + pNode->user_ip = ip; + + } + + printk("IP ALEADY EXISTED\n"); + pNode->user_id = id; + return 0; +} + + +/*delete a node */ +void del_user(uint32_t ip) +{ + int index; + struct list_head *pos; + USERINFO *pNode; + index = get_hash_index(ip); + + /*判断hashtable中该单元是否为空 */ + if( list_empty(&hash_array[index])) + { + printk("node is NULL\n"); + return; + } + + /*查找用户ip */ + list_for_each(pos, &hash_array[index]) + { + pNode = list_entry(pos, USERINFO, hnode); + if(pNode->user_ip == ip) + list_del(&pNode->hnode); + kfree(pNode); + } + + return; +} + +/*delete all node */ +void free_all_user(void) +{ + int i; + struct list_head *pos, *n; + USERINFO *pNode; + + /*判断hashtable单元是否为NULL */ + for(i = 0; i < HASH_SIZE; i++) + { + if(list_empty(&hash_array[i])) + continue; + list_for_each_safe(pos, n, &hash_array[i]) + { + pNode = list_entry(pos, USERINFO, hnode); + list_del(&pNode->hnode); + kfree(pNode); + } + } + +} + +/*printf all nodes */ +void printk_all_user(void) +{ + int i; + struct list_head *pos, *n; + USERINFO * pNode; + + for(i = 0; i < HASH_SIZE; i++) + continue; + list_for_each_safe(pos, n, &hash_array[i]) + { + pNode = list_entry(pos, USERINFO, hnode); + printk("user ip:%d user id:%d\n", pNode->user_ip, pNode->user_id); + } + +} + + + + + diff --git a/Product/modules/userhash/k-userhash.h b/Product/modules/userhash/k-userhash.h new file mode 100644 index 000000000..194a1b78f --- /dev/null +++ b/Product/modules/userhash/k-userhash.h @@ -0,0 +1,37 @@ +#ifndef _KUSERHASH_H +#define _KUSERHASH_H + +#include + +/*定义hash表大小 */ +#define HASH_SIZE 100 + +typedef struct userinfo +{ + struct list_head hnode; + int user_id; + uint32_t user_ip; +}USERINFO; + +/*init hashtable */ +void init_hashMap(void); + +/*获取hashtable位置索引 */ +int get_hash_index(uint32_t ip); + +/* search node */ +USERINFO *search_user(uint32_t ip); + +/*add user */ +int add_user(uint32_t ip, int id); + +/*delete a node */ +void del_user(uint32_t ip); + +/*delete all node */ +void free_all_user(void); + +/*printf all nodes */ +void printk_all_user(void); + +#endif \ No newline at end of file From b64957428afc9c5cfe922dce2d8d645bf88b6781 Mon Sep 17 00:00:00 2001 From: ChenLing Date: Mon, 29 Jul 2019 10:50:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Mod=20=20aaa-12=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E8=AE=A4=E8=AF=81=E7=BB=93=E6=9E=9C=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E8=A1=A8=E4=B8=BA=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F?= =?UTF-8?q?=EF=BC=8Cweb=5Fauth.c=E6=96=87=E4=BB=B6=E4=B8=AD=E5=8A=A0main?= =?UTF-8?q?=E5=87=BD=E6=95=B0=20RCA=EF=BC=9A=20SOL=EF=BC=9A=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=BA=BA=EF=BC=9A=20=E6=A3=80=E8=A7=86=E4=BA=BA?= =?UTF-8?q?=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Product/user/user_auth/web_auth.c | 18 ++++++++++++++++++ .../user_manager/usermanager-auth/user_auth.c | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Product/user/user_auth/web_auth.c b/Product/user/user_auth/web_auth.c index 82b19946e..0fffcb6c9 100644 --- a/Product/user/user_auth/web_auth.c +++ b/Product/user/user_auth/web_auth.c @@ -165,6 +165,24 @@ ret_code user_auth(pointer content, RESULT *uresult) return ret; } +int main(int args, char** argv) +{ + RESULT *output; + ret_code ret; + /*创建内存地址 */ + output = (RESULT*)malloc(sizeof(RESULT)); + if (NULL == output) + { + ret = RET_NOMEM; + return -1; + } + char *test = "{\"username\": \"用户02\",\"password\": \"123456\"}"; + ret = user_auth(test, output); + + printf("%d\n", ret); + +return 0; +} diff --git a/Product/user/user_manager/usermanager-auth/user_auth.c b/Product/user/user_manager/usermanager-auth/user_auth.c index f98ca0c64..e99f7b2c2 100644 --- a/Product/user/user_manager/usermanager-auth/user_auth.c +++ b/Product/user/user_manager/usermanager-auth/user_auth.c @@ -59,7 +59,7 @@ do { \ }; \ /* 定义用户认证结果记录表 */ -static USER_AUTH_LIST g_user_auth_ret_table[AUTH_USER_INDEX_MAX] = { 0 }; +USER_AUTH_LIST g_user_auth_ret_table[AUTH_USER_INDEX_MAX] = { 0 }; /* * config_lock_time 锁定后-时间,单位(分钟)