diff --git a/Product/user/user_auth/hashtable.c b/Product/user/user_auth/hashtable.c deleted file mode 100644 index 46b4869b4..000000000 --- a/Product/user/user_auth/hashtable.c +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include -#include -#include "uthash.h" -#include -#include -#include - -struct user_struct -{ - int id; //用户ID - uint32_t ip; //用户IP,以IP为索引 - UT_hash_handle hh; -}; - -//initialize to NULL -struct user_struct *users = NULL; - -//add user information -void add_user(uint32_t user_ip, int user_id) -{ - struct user_struct *s; - HASH_FIND_INT(users, &user_ip, s); /*ip already in the hash? */ - if(s == NULL) - { - s = (struct user_struct *)malloc(sizeof *s); - s->ip = user_ip; - HASH_ADD_INT(users, ip, s); - } - s->id = user_id; -} - -//find user information -struct user_struct *find_user(uint32_t user_ip) -{ - struct user_struct *s; - HASH_FIND_INT(users, &user_ip, s); - return s; -} - -void delete_user(struct user_struct *user) -{ - HASH_DEL(users, user); /* user: pointer to delete */ - free(user); -} - -//delect all uesr'informations -void delete_all() -{ - struct user_struct *current_user, *tmp; - HASH_ITER(hh, users, current_user, tmp) - { - HASH_DEL(users,current_user); /* delete; users advances to next */ - free(current_user); - } -} - -//printf user information -void print_users() -{ - struct user_struct *s; - char str[32]; - for(s=users; s != NULL; s=(struct user_struct*)(s->hh.next)) - { - //printf("user ip %s: user id %d\n", s->ip, s->id); - inet_ntop(AF_INET, (void *)&s->ip, str, 32); - printf(" user_ip: %s user_id: %d \n", str, s->id); - } -} - diff --git a/Product/user/user_auth/hlist.h b/Product/user/user_auth/hlist.h new file mode 100644 index 000000000..e049f555f --- /dev/null +++ b/Product/user/user_auth/hlist.h @@ -0,0 +1,136 @@ +#ifndef __HLIST_H +#define __HLIST_H + +struct hlist_head { + struct hlist_node *first; +}; + +struct hlist_node { + struct hlist_node *next, **pprev; +}; + +#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) + +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) + +#define HLIST_HEAD_INIT { .first = NULL } +#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } + +#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) + +static inline void INIT_HLIST_NODE(struct hlist_node *h) +{ + h->next = NULL; + h->pprev = NULL; +} + +static inline int hlist_unhashed(const struct hlist_node *h) +{ + return !h->pprev; +} + +static inline int hlist_empty(const struct hlist_head *h) +{ + return !h->first; +} + +static inline void __hlist_del(struct hlist_node *n) +{ + struct hlist_node *next = n->next; + struct hlist_node **pprev = n->pprev; + *pprev = next; + if (next) + next->pprev = pprev; +} + +static inline void hlist_del(struct hlist_node *n) +{ + __hlist_del(n); + n->next = NULL; + n->pprev = NULL; +} + +static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) +{ + struct hlist_node *first = h->first; + n->next = first; + if (first) + first->pprev = &n->next; + h->first = n; + n->pprev = &h->first; +} + + +static inline void hlist_del_init(struct hlist_node *n) +{ + if (!hlist_unhashed(n)) { + __hlist_del(n); + INIT_HLIST_NODE(n); + } +} + +static inline void hlist_add_before(struct hlist_node *n, + struct hlist_node *next) +{ + n->pprev = next->pprev; + n->next = next; + next->pprev = &n->next; + *(n->pprev) = n; +} + +static inline void hlist_add_after(struct hlist_node *n, + struct hlist_node *next) +{ + next->next = n->next; + n->next = next; + next->pprev = &n->next; + + if(next->next) + next->next->pprev = &next->next; +} + +static inline void hlist_move_list(struct hlist_head *old, + struct hlist_head *new) +{ + new->first = old->first; + if (new->first) + new->first->pprev = &new->first; + old->first = NULL; +} + +#define hlist_entry(ptr, type, member) container_of(ptr,type,member) + + +#define hlist_for_each_safe(pos, n, head) \ + for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ + pos = n) + +#define hlist_for_each_entry(tpos, pos, head, member) \ + for (pos = (head)->first; \ + pos && ({ prefetch(pos->next); 1;}) && \ + ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ + pos = pos->next) + +#define hlist_for_each_entry_continue(tpos, pos, member) \ + for (pos = (pos)->next; \ + pos && ({ prefetch(pos->next); 1;}) && \ + ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ + pos = pos->next) + +#define hlist_for_each_entry_from(tpos, pos, member) \ + for (; pos && ({ prefetch(pos->next); 1;}) && \ + ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ + pos = pos->next) + +#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ + for (pos = (head)->first; \ + pos && ({ n = pos->next; 1; }) && \ + ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ + pos = n) + + + +#endif + \ No newline at end of file diff --git a/Product/user/user_auth/ku-hashtable.c b/Product/user/user_auth/ku-hashtable.c new file mode 100644 index 000000000..eeeaaf2e2 --- /dev/null +++ b/Product/user/user_auth/ku-hashtable.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include +#include +#include "hlist.h" +#include "k-hashtable.h" + +struct hlist_head *hash; +struct hlist_node *p = NULL, *n = NULL ; +int i = 0; +USER_INFO *pNode ; + +/*计算hash值 */ +struct hlist_head *call_hash(struct hlist_head *hash, uint32_t ip) +{ + unsigned int val = ip % 100; + return &hash[val]; +} + +/*初始化函数 */ +int Init_hash() +{ + hash = (struct hlist_head*)malloc(sizeof(*hash)*100); + if(NULL == hash) + { + printf("alloc error\n"); + return -1; + } + + for(i = 0; i < 100; i++) + INIT_HLIST_HEAD(&hash[i]); +} + + +/*查找用户信息*/ +struct user_info *kfind_user(uint32_t user_ip) +{ + hlist_for_each_safe(p,n,call_hash(hash,user_ip)) + { + pNode = hlist_entry(p, struct user_info ,hnode); + if(pNode != NULL) + printf("user_id :%d\n",pNode->id); + return pNode; + } +} + +/*增加用户信息*/ +int kadd_user(uint32_t user_ip, int user_id) +{ + USER_INFO *pNode =NULL; + hlist_for_each_safe(p,n,call_hash(hash, user_ip)) /*查找ip是否存在hash表中 */ + { + pNode = hlist_entry(p, struct user_info ,hnode); + if(pNode != NULL) + printf("IP ALEADY EXISTED\n"); + } + + if (pNode == NULL) + { + pNode = (struct user_info *)malloc(sizeof(struct user_info)); + if (NULL == pNode) + { + return -1; + } + memset(pNode,0,sizeof(struct user_info)); + INIT_HLIST_NODE(&pNode->hnode); + pNode->ip = user_ip; + hlist_add_head(&pNode->hnode, call_hash(hash, user_ip)); + } + pNode->id = user_id; + +} + +/*删除用户信息 */ +void kdelete_user(int user_ip) +{ + hlist_for_each_safe(p,n,call_hash(hash,user_ip)) + { + pNode = hlist_entry(p, struct user_info ,hnode); + hlist_del(&pNode->hnode); + free(pNode); + } +} + +/*删除所有的hash节点 */ +void kdelete_all() +{ + for(i = 0; i < 100; i++) + { + hlist_for_each_safe(p,n,&hash[i]) + { + pNode = hlist_entry(p, struct user_info ,hnode); + hlist_del(&pNode->hnode); + free(pNode); + } + } +} + +/*打印所有信息信息 */ +void kprintf_users() +{ + for(i = 0; i < 100; i++) + { + hlist_for_each_safe(p,n,&hash[i]) + { + char str[32]; + pNode = hlist_entry(p, struct user_info ,hnode); + if(pNode != NULL) + inet_ntop(AF_INET, (void *)&pNode->ip, str, 32); + printf("user_ip :%s user_id:%d\n", str, pNode->id); + } + } +} + diff --git a/Product/user/user_auth/ku-hashtable.h b/Product/user/user_auth/ku-hashtable.h new file mode 100644 index 000000000..1bd852a02 --- /dev/null +++ b/Product/user/user_auth/ku-hashtable.h @@ -0,0 +1,33 @@ +#ifndef K_HASHTABLE_H +#define K_HASHTABLE_H +#include +#include "hlist.h" + +typedef struct user_info{ + struct hlist_node hnode; + int id; + uint32_t ip; +}USER_INFO; + +/*计算hash值 */ +struct hlist_head *call_hash(struct hlist_head *hash, uint32_t ip); + +/*初始化函数 */ +int Init_hash(); + +/*查找用户信息*/ +struct user_info *kfind_user(uint32_t user_ip); + +/*增加用户信息*/ +int kadd_user(uint32_t user_ip, int user_id); + +/*删除用户信息 */ +void kdelete_user(int user_ip); + +/*删除所有的hash节点 */ +void kdelete_all(); + +/*打印所有信息信息 */ +void kprintf_users(); + +#endif \ No newline at end of file diff --git a/Product/user/user_auth/u-hashtable.c b/Product/user/user_auth/u-hashtable.c new file mode 100644 index 000000000..2db599223 --- /dev/null +++ b/Product/user/user_auth/u-hashtable.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include "u-hashtable.h" + +/*initialize */ +USER_STRUCT *users = NULL; + +/*add user information */ +void uadd_user(uint32_t user_ip, USER user_info) +{ + USER_STRUCT *s ; + HASH_FIND_INT(users, &user_ip, s); /*ip already in the hash? */ + if(s == NULL) + { + s = (struct user_struct *)malloc(sizeof *s); + s->ip = user_ip; + HASH_ADD_INT(users, ip, s); + } + + s->info = user_info; +} + +/*find user information */ +USER_STRUCT *ufind_user(uint32_t user_ip) +{ + USER_STRUCT *s; + HASH_FIND_INT(users, &user_ip, s); + printf("user_nams: %s user_id:%d usergroud_id:%d\n", s->info.name, s->info.user_id, s->info.usergroud_id); + return s; +} + +/*delete user information */ +void udelete_user(USER_STRUCT *user) +{ + HASH_DEL(users, user); /* user: pointer to delete */ + free(user); +} + +/*delect all uesr'informations */ +void udelete_all() +{ + USER_STRUCT *current_user, *tmp; + HASH_ITER(hh, users, current_user, tmp) + { + HASH_DEL(users,current_user); /* delete; users advances to next */ + free(current_user); + } +} + +/*printf user information */ +void uprint_users() +{ + USER_STRUCT *s; + char str[32]; + for(s=users; s != NULL; s=(USER_STRUCT *)(s->hh.next)) + { + inet_ntop(AF_INET, (void *)&s->ip, str, 32); + printf(" user_ip: %s user_nams: %s user_id:%d usergroud_id:%d\n", str, s->info.name, s->info.user_id, s->info.usergroud_id); + } +} diff --git a/Product/user/user_auth/u-hashtable.h b/Product/user/user_auth/u-hashtable.h new file mode 100644 index 000000000..38702564b --- /dev/null +++ b/Product/user/user_auth/u-hashtable.h @@ -0,0 +1,40 @@ +#ifndef U_HASHTABLE_H +#define U_HASHTABLE_H +#include +#include +#include "uthash.h" + +typedef struct user_info +{ + char name[32]; /*用户名 */ + int user_id; /*用户ID */ + int usergroud_id; /*用户组ID */ + uint64_t message_num; /*下行报文数 */ + uint64_t byte_num; /*下行字节数*/ + time_t online_time; /*在线时间 */ +}USER; + + +typedef struct user_struct +{ + uint32_t ip; /*用户IP key*/ + USER info; /*用户信息 */ + UT_hash_handle hh; +}USER_STRUCT; + +/*add user information */ +void uadd_user(uint32_t user_ip, USER user_info); + +/*find user information */ +USER_STRUCT *ufind_user(uint32_t user_ip); + +/*delete user information */ +void udelete_user(USER_STRUCT *user); + +/*delect all uesr'informations */ +void udelete_all(); + +/*printf user information */ +void uprint_users(); + +#endif \ No newline at end of file