vcpe/srcs/service/dhcpd/lease.c

117 lines
3.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Created by xajhuang on 2023/3/23.
//
#include <time.h>
#include "lease.h"
#include "user_errno.h"
#include "zlog_module.h"
#include "dhcp_network.h"
#include "db_interface.h"
#include "uthash/utlist.h"
//static PMAC_FILTER g_allowTbl = NULL;
//static PMAC_FILTER g_blackListTbl = NULL;
//static PPRE_ALLOC_IP g_pPreAllocIp = NULL;
U32 lease_is_pre_assign(PDHCP_REQ pReq) {
char macStr[20] = {0};
U32 ipAddr;
MAC_TO_STR(pReq->cliMac, macStr);
if (lease_get_pre_assign(pReq->uid, macStr, pReq->hostName, &ipAddr) == ERR_ITEM_EXISTS) {
return ipAddr;
}
return 0;
}
int usr_lease_lock_ip(PDHCP_USER pUser, U32 ip) {
PLOCK_IP pLock = (PLOCK_IP)malloc(sizeof(LOCK_IP));
if (pLock) {
memset(pLock, 0, sizeof(LOCK_IP));
pLock->ip = ip;
HASH_ADD_INT(pUser->plockIp, ip, pLock);
return ERR_SUCCESS;
}
return -ERR_MALLOC_MEMORY;
}
//int pre_alloc_dhcp_res(U32 uid, const char *pMac, U32 *pOutIp, PIPPOOL_INFO *pOutPool) {
int pre_alloc_dhcp_res(PDHCP_REQ pReq, PDHCP_USER pUser, U32 *pOutIp, PPOOL_CTX *pOutPool) {
PPOOL_CTX pPool, pTemp;
if (pReq == NULL || pOutIp == NULL || pOutPool == NULL) {
LOG_MOD(error, ZM_DHCP_LEASE, "Input params error: %p, %p\n", pOutIp, pOutPool);
return -ERR_INPUT_PARAMS;
}
LL_FOREACH_SAFE(pUser->pUserPool, pPool, pTemp) {
U32 addr;
// 查看是否预分配过该设备
if ((addr = lease_is_pre_assign(pReq)) != 0) {
*pOutIp = addr;
*pOutPool = pPool;
return ERR_SUCCESS;
}
addr = pPool->minAddr;
do {
PLOCK_IP pLock;
HASH_FIND_INT(pUser->plockIp, &addr, pLock);
// 该 IP 可用
if (pLock == NULL) {
#if 1
if (lease_ip_is_pre_assign(pReq->uid, addr) == FALSE) {
usr_lease_lock_ip(pUser, addr);
*pOutIp = addr;
*pOutPool = pPool;
lease_db_add_pre_assign(pReq, addr, pPool);
return ERR_SUCCESS;
}
#else
usr_lease_lock_ip(pUser, addr);
*pOutIp = addr;
*pOutPool = pPool;
lease_db_add_pre_assign(pReq, addr, pPool);
// LOG_MOD(debug, ZM_DHCP_LEASE, "User %u(%u) prepar assign ipaddr %08X of %p\n", pReq->uid, pUser->uid,
// addr, pPool);
return ERR_SUCCESS;
#endif
}
addr++;
} while (addr <= pPool->maxAddr);
}
// 清理所有超时的预分配IP
lease_clearup_timeout_pre_assign();
// 没有可预分配的IP报错
//LOG_MOD(error, ZM_DHCP_LEASE, "No free ipaddress in poll: uid = %u, pool = %p\n", pReq->uid, pUser->pUserPool);
return -ERR_DHCP_NO_ADDR;
}
int dhcp_lease_init() {
int rc;
rc = lease_init_database();
if (rc != ERR_SUCCESS) {
return rc;
}
// 清理所有超时的预分配IP
lease_clearup_timeout_pre_assign();
// lock 预分配 IP
lease_lock_pre_assign_ip();
return ERR_SUCCESS;
}