2022-05-10 06:43:27 +00:00
|
|
|
//
|
|
|
|
// Created by xajhuang on 2022/5/11.
|
|
|
|
//
|
2022-06-02 08:18:43 +00:00
|
|
|
#include <misc.h>
|
2023-02-06 07:10:02 +00:00
|
|
|
#include "zlog_module.h"
|
2022-05-10 06:43:27 +00:00
|
|
|
#include "user_info.h"
|
|
|
|
#include "user_errno.h"
|
|
|
|
|
2023-02-06 07:10:02 +00:00
|
|
|
#ifdef USED_LWIP
|
|
|
|
|
2022-06-13 10:49:12 +00:00
|
|
|
static PUSER_INFO_CONTEXT g_pUserByIdList = NULL;
|
2022-06-06 03:46:09 +00:00
|
|
|
static PUSER_INFO_CONTEXT g_pUserByTagsList = NULL;
|
2022-06-15 06:36:09 +00:00
|
|
|
static PUSER_INFO_CONTEXT g_pUserByMacList = NULL;
|
2022-06-06 03:46:09 +00:00
|
|
|
static uv_rwlock_t g_userLock;
|
|
|
|
|
|
|
|
static USER_PARAMS g_userInfo[] = {
|
2022-12-06 06:42:41 +00:00
|
|
|
{0, 400, 24, 371, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x03}, "user1", "password1"},
|
|
|
|
{1, 300, 16, 12, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x02}, "user2", "password1"},
|
|
|
|
{2, 300, 14, 15, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x04}, "user3", "password1"},
|
|
|
|
{3, 400, 24, 17, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x04}, "xajhuang", "aaaHuang1"},
|
2022-05-10 06:43:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void user_info_init() {
|
2022-06-02 08:18:43 +00:00
|
|
|
uv_rwlock_init(&g_userLock);
|
|
|
|
|
2022-06-13 10:49:12 +00:00
|
|
|
//user_info_add(0, &g_userInfo[0]);
|
2022-06-13 03:35:48 +00:00
|
|
|
//user_info_add(1, &g_userInfo[1]);
|
2022-06-08 11:36:16 +00:00
|
|
|
//user_info_add(2, &g_userInfo[2]);
|
2022-06-15 07:28:16 +00:00
|
|
|
//user_info_add(3, &g_userInfo[3]);
|
2022-05-10 06:43:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 03:46:09 +00:00
|
|
|
void user_info_change_status(PUSER_INFO_CONTEXT pInfo, USER_STATUS status) {
|
2022-06-02 08:18:43 +00:00
|
|
|
pInfo->user_status = status;
|
|
|
|
}
|
|
|
|
|
2022-06-06 03:46:09 +00:00
|
|
|
int user_info_add(unsigned int userid, PUSER_PARAMS pInfo) {
|
2022-06-02 08:18:43 +00:00
|
|
|
PUSER_INFO_CONTEXT pUser;
|
|
|
|
|
|
|
|
uv_rwlock_rdlock(&g_userLock);
|
2022-06-06 03:46:09 +00:00
|
|
|
HASH_FIND(hh_id, g_pUserByIdList, &userid, sizeof(unsigned int), pUser);
|
2022-06-02 08:18:43 +00:00
|
|
|
uv_rwlock_rdunlock(&g_userLock);
|
|
|
|
|
2022-06-06 03:46:09 +00:00
|
|
|
if (pUser != NULL) {
|
2022-06-02 08:18:43 +00:00
|
|
|
return -ERR_ITEM_EXISTS;
|
|
|
|
}
|
|
|
|
|
2022-05-10 06:43:27 +00:00
|
|
|
if (pInfo) {
|
2022-06-02 08:18:43 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-06-02 08:18:43 +00:00
|
|
|
memset(pList, 0, sizeof(USER_INFO_CONTEXT));
|
|
|
|
|
2022-06-06 03:46:09 +00:00
|
|
|
pList->user_status = STATUS_USER_NEW;
|
2022-05-10 06:43:27 +00:00
|
|
|
pList->userid = userid;
|
2022-06-06 03:46:09 +00:00
|
|
|
pList->vxlan.vni = pInfo->vni;
|
|
|
|
pList->vxlan.q1 = pInfo->q1;
|
|
|
|
pList->vxlan.q2 = pInfo->q2;
|
2022-05-10 06:43:27 +00:00
|
|
|
pList->user_info.pppoe_user = strdup(pInfo->pppoe_user);
|
|
|
|
pList->user_info.pppoe_passwd = strdup(pInfo->pppoe_passwd);
|
2022-06-15 06:36:09 +00:00
|
|
|
memcpy(pList->mac_addr, pInfo->mac_addr, 6);
|
2022-05-10 06:43:27 +00:00
|
|
|
memcpy(pList->user_info.mac_addr, pInfo->mac_addr, 6);
|
|
|
|
|
2022-06-02 08:18:43 +00:00
|
|
|
pList->session.status = STATUS_TASK_INIT;
|
|
|
|
|
|
|
|
uv_rwlock_wrlock(&g_userLock);
|
2022-06-06 03:46:09 +00:00
|
|
|
HASH_ADD(hh_id, g_pUserByIdList, userid, sizeof(unsigned int), pList);
|
|
|
|
HASH_ADD(hh_vxlan, g_pUserByTagsList, vxlan, sizeof(VXLAN_TAG), pList);
|
2022-06-15 06:36:09 +00:00
|
|
|
HASH_ADD(hh_mac, g_pUserByMacList, mac_addr, 6, pList);
|
2022-06-02 08:18:43 +00:00
|
|
|
uv_rwlock_wrunlock(&g_userLock);
|
2022-06-13 10:49:12 +00:00
|
|
|
|
2023-02-06 07:10:02 +00:00
|
|
|
LOG_MSG(debug,
|
|
|
|
ZLOG_MOD_USER,
|
|
|
|
"Add user: id = %u, vni = %u, q1 = %u, q2 = %u, ppp_user = %s, mac = %02X:%02X:%02X:%02X:%02X:%02X\n",
|
|
|
|
userid,
|
|
|
|
pInfo->vni,
|
|
|
|
pInfo->q1,
|
|
|
|
pInfo->q2,
|
|
|
|
pInfo->pppoe_user,
|
|
|
|
pList->mac_addr[0],
|
|
|
|
pList->mac_addr[1],
|
|
|
|
pList->mac_addr[2],
|
|
|
|
pList->mac_addr[3],
|
|
|
|
pList->mac_addr[4],
|
|
|
|
pList->mac_addr[5]);
|
2022-05-10 06:43:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 08:18:43 +00:00
|
|
|
return ERR_SUCCESS;
|
2022-05-10 06:43:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 06:36:09 +00:00
|
|
|
struct netif *get_user_nic_by_mac(const unsigned char mac[6]) {
|
|
|
|
PUSER_INFO_CONTEXT pUser;
|
|
|
|
uv_rwlock_rdlock(&g_userLock);
|
|
|
|
HASH_FIND(hh_mac, g_pUserByMacList, mac, 6, pUser);
|
|
|
|
uv_rwlock_rdunlock(&g_userLock);
|
|
|
|
|
|
|
|
if (pUser) {
|
|
|
|
return pUser->session.nicif;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PUSER_INFO_CONTEXT get_user_by_tag(PVXLAN_TAG pTag) {
|
|
|
|
if (pTag) {
|
|
|
|
PUSER_INFO_CONTEXT pUser;
|
|
|
|
|
|
|
|
uv_rwlock_rdlock(&g_userLock);
|
|
|
|
HASH_FIND(hh_vxlan, g_pUserByTagsList, pTag, sizeof(VXLAN_TAG), pUser);
|
|
|
|
uv_rwlock_rdunlock(&g_userLock);
|
|
|
|
|
|
|
|
if (pUser) {
|
|
|
|
return pUser;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-05-10 06:43:27 +00:00
|
|
|
void user_info_remove(unsigned int userid) {
|
2022-06-02 08:18:43 +00:00
|
|
|
PUSER_INFO_CONTEXT pInfo;
|
2022-05-10 06:43:27 +00:00
|
|
|
|
2022-06-02 08:18:43 +00:00
|
|
|
uv_rwlock_rdlock(&g_userLock);
|
2022-06-06 03:46:09 +00:00
|
|
|
HASH_FIND(hh_id, g_pUserByIdList, &userid, sizeof(unsigned int), pInfo);
|
2022-06-02 08:18:43 +00:00
|
|
|
uv_rwlock_rdunlock(&g_userLock);
|
|
|
|
|
|
|
|
if (pInfo) {
|
2022-06-15 10:08:19 +00:00
|
|
|
pInfo->session.retry.timeout = 0;
|
|
|
|
pInfo->session.status = STATUS_TASK_DELETE;
|
2022-06-02 08:18:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void user_info_delete(unsigned int userid) {
|
|
|
|
PUSER_INFO_CONTEXT pInfo;
|
|
|
|
|
|
|
|
uv_rwlock_rdlock(&g_userLock);
|
2022-06-06 03:46:09 +00:00
|
|
|
HASH_FIND(hh_id, g_pUserByIdList, &userid, sizeof(unsigned int), pInfo);
|
2022-06-02 08:18:43 +00:00
|
|
|
uv_rwlock_rdunlock(&g_userLock);
|
2022-05-10 06:43:27 +00:00
|
|
|
|
|
|
|
if (pInfo) {
|
2022-06-28 08:53:22 +00:00
|
|
|
uv_rwlock_wrlock(&g_userLock);
|
2022-06-06 03:46:09 +00:00
|
|
|
HASH_DELETE(hh_id, g_pUserByIdList, pInfo);
|
|
|
|
HASH_DELETE(hh_vxlan, g_pUserByTagsList, pInfo);
|
2022-06-28 08:53:22 +00:00
|
|
|
uv_rwlock_wrunlock(&g_userLock);
|
2022-05-10 06:43:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 08:18:43 +00:00
|
|
|
int user_info_modify(unsigned int userid, PUSER_INFO pUser) {
|
|
|
|
PUSER_INFO_CONTEXT pInfo;
|
|
|
|
|
|
|
|
uv_rwlock_rdlock(&g_userLock);
|
2022-06-06 03:46:09 +00:00
|
|
|
HASH_FIND(hh_id, g_pUserByIdList, &userid, sizeof(unsigned int), pInfo);
|
2022-06-02 08:18:43 +00:00
|
|
|
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) {
|
2022-06-06 03:46:09 +00:00
|
|
|
free((void *)pInfo->user_info.pppoe_user);
|
2022-06-02 08:18:43 +00:00
|
|
|
pInfo->user_info.pppoe_user = strdup(pUser->pppoe_user);
|
|
|
|
isUpgrade = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(pInfo->user_info.pppoe_passwd, pUser->pppoe_passwd) != 0) {
|
2022-06-06 03:46:09 +00:00
|
|
|
free((void *)pInfo->user_info.pppoe_passwd);
|
2022-06-02 08:18:43 +00:00
|
|
|
pInfo->user_info.pppoe_passwd = strdup(pUser->pppoe_passwd);
|
|
|
|
isUpgrade = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isUpgrade) {
|
2022-06-06 03:46:09 +00:00
|
|
|
pInfo->user_status = STATUS_USER_MODIFY;
|
2022-06-02 08:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-06-02 08:18:43 +00:00
|
|
|
PUSER_INFO_CONTEXT pInfo;
|
2022-05-10 06:43:27 +00:00
|
|
|
|
2022-06-06 03:46:09 +00:00
|
|
|
HASH_FIND(hh_id, g_pUserByIdList, &userid, sizeof(unsigned int), pInfo);
|
2022-05-10 06:43:27 +00:00
|
|
|
|
|
|
|
if (pInfo) {
|
|
|
|
return &pInfo->user_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:29:05 +00:00
|
|
|
PUSER_INFO_CONTEXT get_all_user_by_id() {
|
2022-06-06 03:46:09 +00:00
|
|
|
return g_pUserByIdList;
|
2022-06-02 08:18:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 03:29:05 +00:00
|
|
|
PUSER_INFO_CONTEXT get_all_user_by_tag() {
|
|
|
|
return g_pUserByTagsList;
|
|
|
|
}
|
|
|
|
|
2022-06-06 03:46:09 +00:00
|
|
|
uv_rwlock_t *get_user_lock() {
|
2022-06-02 08:18:43 +00:00
|
|
|
return &g_userLock;
|
2023-02-06 07:10:02 +00:00
|
|
|
}
|
|
|
|
#endif
|