secgateway/Product/user/user_auth/hashtable.c

71 lines
1.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "uthash.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
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);
}
}