vcpe/srcs/user/user_info.c

146 lines
3.9 KiB
C
Raw Normal View History

2022-05-10 06:43:27 +00:00
//
// Created by xajhuang on 2022/5/11.
//
#include <misc.h>
2022-05-10 06:43:27 +00:00
#include "user_info.h"
#include "user_errno.h"
static PUSER_INFO_CONTEXT g_pUserList = NULL;
static uv_rwlock_t g_userLock;
2022-05-10 06:43:27 +00:00
static USER_INFO g_userInfo[] = {
{0, 1, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x02}, "xajhuang3", "aaaHuang1", STATUS_USER_NEW},
{1, 2, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x03}, "xajhuang1", "aaaHuang1", STATUS_USER_NEW},
{3, 4, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x04}, "xajhuang2", "aaaHuang1", STATUS_USER_NEW},
2022-05-10 06:43:27 +00:00
};
void user_info_init() {
uv_rwlock_init(&g_userLock);
2022-05-10 06:43:27 +00:00
user_info_add(0, &g_userInfo[0]);
user_info_add(1, &g_userInfo[1]);
user_info_add(2, &g_userInfo[2]);
2022-05-10 06:43:27 +00:00
}
void user_info_change_status(PUSER_INFO pInfo, USER_STATUS status) {
pInfo->user_status = status;
}
2022-05-10 06:43:27 +00:00
int user_info_add(unsigned int userid, PUSER_INFO pInfo) {
PUSER_INFO_CONTEXT pUser;
uv_rwlock_rdlock(&g_userLock);
HASH_FIND_INT(g_pUserList, &userid, pUser);
uv_rwlock_rdunlock(&g_userLock);
if(pUser != NULL) {
return -ERR_ITEM_EXISTS;
}
2022-05-10 06:43:27 +00:00
if (pInfo) {
PUSER_INFO_CONTEXT pList = (PUSER_INFO_CONTEXT)malloc(sizeof(USER_INFO_CONTEXT));
2022-05-10 06:43:27 +00:00
if (!pList) {
return ERR_MALLOC_MEMORY;
}
memset(pList, 0, sizeof(USER_INFO_CONTEXT));
2022-05-10 06:43:27 +00:00
pList->userid = userid;
pList->user_info.qinq_tag1 = pInfo->qinq_tag1;
pList->user_info.qinq_tag2 = pInfo->qinq_tag2;
pList->user_info.pppoe_user = strdup(pInfo->pppoe_user);
pList->user_info.pppoe_passwd = strdup(pInfo->pppoe_passwd);
pList->user_info.user_status = STATUS_USER_NEW;
2022-05-10 06:43:27 +00:00
memcpy(pList->user_info.mac_addr, pInfo->mac_addr, 6);
pList->session.status = STATUS_TASK_INIT;
uv_rwlock_wrlock(&g_userLock);
2022-05-10 06:43:27 +00:00
HASH_ADD_INT(g_pUserList, userid, pList);
uv_rwlock_wrunlock(&g_userLock);
2022-05-10 06:43:27 +00:00
}
return ERR_SUCCESS;
2022-05-10 06:43:27 +00:00
}
void user_info_remove(unsigned int userid) {
PUSER_INFO_CONTEXT pInfo;
2022-05-10 06:43:27 +00:00
uv_rwlock_rdlock(&g_userLock);
2022-05-10 06:43:27 +00:00
HASH_FIND_INT(g_pUserList, &userid, pInfo);
uv_rwlock_rdunlock(&g_userLock);
if (pInfo) {
pInfo->user_info.user_status = STATUS_USER_DELETE;
}
}
void user_info_delete(unsigned int userid) {
PUSER_INFO_CONTEXT pInfo;
uv_rwlock_rdlock(&g_userLock);
HASH_FIND_INT(g_pUserList, &userid, pInfo);
uv_rwlock_rdunlock(&g_userLock);
2022-05-10 06:43:27 +00:00
if (pInfo) {
HASH_DEL(g_pUserList, pInfo);
}
}
int user_info_modify(unsigned int userid, PUSER_INFO pUser) {
PUSER_INFO_CONTEXT pInfo;
uv_rwlock_rdlock(&g_userLock);
HASH_FIND_INT(g_pUserList, &userid, pInfo);
uv_rwlock_rdunlock(&g_userLock);
if (pInfo) {
int isUpgrade = FALSE;
if (memcmp(pInfo->user_info.mac_addr, pUser->mac_addr, 6) != 0) {
memcpy(pInfo->user_info.mac_addr, pUser->mac_addr, 6);
isUpgrade = TRUE;
}
if (strcmp(pInfo->user_info.pppoe_user, pUser->pppoe_user) != 0) {
free((void*)pInfo->user_info.pppoe_user);
pInfo->user_info.pppoe_user = strdup(pUser->pppoe_user);
isUpgrade = TRUE;
}
if (strcmp(pInfo->user_info.pppoe_passwd, pUser->pppoe_passwd) != 0) {
free((void*)pInfo->user_info.pppoe_passwd);
pInfo->user_info.pppoe_passwd = strdup(pUser->pppoe_passwd);
isUpgrade = TRUE;
}
if (isUpgrade) {
pInfo->user_info.user_status = STATUS_USER_MODIFY;
}
return ERR_SUCCESS;
} else {
return -ERR_ITEM_UNEXISTS;
}
}
2022-05-10 06:43:27 +00:00
PUSER_INFO user_info_get_by_userid(unsigned int userid) {
PUSER_INFO_CONTEXT pInfo;
2022-05-10 06:43:27 +00:00
HASH_FIND_INT(g_pUserList, &userid, pInfo);
if (pInfo) {
return &pInfo->user_info;
}
return NULL;
}
PUSER_INFO_CONTEXT get_all_user() {
2022-05-10 06:43:27 +00:00
return g_pUserList;
}
uv_rwlock_t* get_user_lock() {
return &g_userLock;
2022-05-10 06:43:27 +00:00
}