2023-03-29 09:02:21 +00:00
|
|
|
|
//
|
|
|
|
|
// Created by xajhuang on 2023/3/23.
|
|
|
|
|
//
|
|
|
|
|
|
2023-04-07 07:56:38 +00:00
|
|
|
|
#include <time.h>
|
2023-03-29 09:02:21 +00:00
|
|
|
|
#include "lease.h"
|
2023-03-31 06:32:32 +00:00
|
|
|
|
#include "user_errno.h"
|
|
|
|
|
#include "zlog_module.h"
|
|
|
|
|
#include "database.h"
|
2023-04-07 07:56:38 +00:00
|
|
|
|
#include "user_mgr.h"
|
2023-04-23 10:06:27 +00:00
|
|
|
|
#include "rfc2131.h"
|
|
|
|
|
#include "dhcp_network.h"
|
2023-04-25 01:17:08 +00:00
|
|
|
|
#include "db_interface.h"
|
2023-03-31 06:32:32 +00:00
|
|
|
|
|
2023-04-07 07:56:38 +00:00
|
|
|
|
#define PREALLOC_IP_TIMEOUT (60)
|
2023-03-29 09:02:21 +00:00
|
|
|
|
|
2023-04-07 07:56:38 +00:00
|
|
|
|
typedef struct {
|
|
|
|
|
U32 ipAddr;
|
2023-04-23 10:06:27 +00:00
|
|
|
|
U8 macAddr[ETH_ALEN];
|
2023-04-07 07:56:38 +00:00
|
|
|
|
U32 bitset;
|
|
|
|
|
PIPPOOL_INFO pCtx;
|
|
|
|
|
U32 timeStamp;
|
|
|
|
|
UT_hash_handle hh;
|
|
|
|
|
} PRE_ALLOC_IP, *PPRE_ALLOC_IP;
|
|
|
|
|
|
|
|
|
|
//static PMAC_FILTER g_allowTbl = NULL;
|
|
|
|
|
//static PMAC_FILTER g_blackListTbl = NULL;
|
2023-04-25 01:17:08 +00:00
|
|
|
|
//static PPRE_ALLOC_IP g_pPreAllocIp = NULL;
|
|
|
|
|
|
|
|
|
|
U32 lease_is_pre_assign(PDHCP_REQ pReq) {
|
2023-04-23 10:06:27 +00:00
|
|
|
|
char macStr[20] = {0};
|
2023-04-25 01:17:08 +00:00
|
|
|
|
U32 ipAddr;
|
2023-04-23 10:06:27 +00:00
|
|
|
|
|
|
|
|
|
MAC_TO_STR(pReq->cliMac, macStr);
|
2023-04-25 01:17:08 +00:00
|
|
|
|
if (lease_get_pre_assign(pReq->uid, macStr, pReq->hostName, &ipAddr) == ERR_ITEM_EXISTS) {
|
|
|
|
|
return ipAddr;
|
2023-04-23 10:06:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 01:17:08 +00:00
|
|
|
|
return 0;
|
2023-04-23 10:06:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//int pre_alloc_dhcp_res(U32 uid, const char *pMac, U32 *pOutIp, PIPPOOL_INFO *pOutPool) {
|
|
|
|
|
int pre_alloc_dhcp_res(PDHCP_REQ pReq, U32 *pOutIp, PIPPOOL_INFO *pOutPool) {
|
2023-04-07 07:56:38 +00:00
|
|
|
|
PIPPOOL_INFO pPool, pTemp;
|
|
|
|
|
PPRE_ALLOC_IP pNewIp, pTmp, pCache = NULL;
|
|
|
|
|
PIPPOOL_INFO pUserPool;
|
|
|
|
|
|
2023-04-23 10:06:27 +00:00
|
|
|
|
if (pReq == NULL || pOutIp == NULL || pOutPool == NULL) {
|
2023-04-07 07:56:38 +00:00
|
|
|
|
LOG_MOD(error, ZLOG_MOD_DHCPD, "Input params error: %p, %p\n", pOutIp, pOutPool);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 10:06:27 +00:00
|
|
|
|
pUserPool = user_get_pool(pReq->uid);
|
2023-04-07 07:56:38 +00:00
|
|
|
|
if (pUserPool == NULL) {
|
|
|
|
|
LOG_MOD(error, ZLOG_MOD_DHCPD, "Can't found avaliable address pool\n");
|
|
|
|
|
return -ERR_DHCP_NO_POOL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 遍历当前用户所有IP地址池
|
|
|
|
|
HASH_ITER(hh, pUserPool, pPool, pTemp) {
|
|
|
|
|
U32 addr;
|
|
|
|
|
|
|
|
|
|
// 查看是否预分配过该设备
|
2023-04-25 01:17:08 +00:00
|
|
|
|
if ((addr = lease_is_pre_assign(pReq)) != 0) {
|
|
|
|
|
*pOutIp = addr;
|
|
|
|
|
*pOutPool = pPool;
|
|
|
|
|
return ERR_SUCCESS;
|
2023-04-07 07:56:38 +00:00
|
|
|
|
}
|
2023-04-25 01:17:08 +00:00
|
|
|
|
|
2023-04-07 07:56:38 +00:00
|
|
|
|
while ((addr = bitset_minimum(pPool->assignPool)) > 0) {
|
|
|
|
|
U32 ipAddr = (pPool->minAddr & pPool->netMask) + addr;
|
|
|
|
|
|
|
|
|
|
// 查找租约配置文件中是否记录了曾经分配的地址, 如果已经分配则获取下一个
|
|
|
|
|
// TODO: add process
|
|
|
|
|
// if (FALSE) {
|
|
|
|
|
// bitset_set(pPool->assignPool, addr);
|
|
|
|
|
// continue;
|
|
|
|
|
// }
|
|
|
|
|
|
2023-04-25 01:17:08 +00:00
|
|
|
|
// 查找IP地址是否已经被预分配, 未分配直接分配IP地址
|
|
|
|
|
if (lease_ip_is_pre_assign(pReq->uid, ipAddr) == FALSE) {
|
2023-04-23 10:06:27 +00:00
|
|
|
|
lease_db_add_pre_assign(pReq, ipAddr, pPool);
|
2023-04-07 07:56:38 +00:00
|
|
|
|
|
|
|
|
|
*pOutIp = ipAddr;
|
|
|
|
|
*pOutPool = pPool;
|
|
|
|
|
|
|
|
|
|
bitset_cls_bit(pPool->assignPool, addr);
|
2023-04-21 08:04:43 +00:00
|
|
|
|
|
|
|
|
|
LOG_MOD(trace, ZLOG_MOD_DHCPD, "Select ipaddr %08X at %d of %p\n", ipAddr, addr, pPool->assignPool);
|
2023-04-07 07:56:38 +00:00
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 01:17:08 +00:00
|
|
|
|
// 清理所有超时的预分配IP
|
|
|
|
|
lease_clearup_timeout_pre_assign();
|
2023-04-07 07:56:38 +00:00
|
|
|
|
// 没有可预分配的IP,报错
|
2023-04-23 10:06:27 +00:00
|
|
|
|
LOG_MOD(error,
|
|
|
|
|
ZLOG_MOD_DHCPD,
|
|
|
|
|
"No free ipaddress in poll: uid = %u, pool = 0x%08X\n",
|
|
|
|
|
pReq->uid,
|
|
|
|
|
pUserPool->poolKey);
|
2023-04-07 07:56:38 +00:00
|
|
|
|
return -ERR_DHCP_NO_ADDR;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-31 06:32:32 +00:00
|
|
|
|
int dhcp_lease_init() {
|
|
|
|
|
int rc = 0;
|
|
|
|
|
|
2023-04-25 01:17:08 +00:00
|
|
|
|
rc = lease_init_database();
|
2023-03-31 06:32:32 +00:00
|
|
|
|
|
|
|
|
|
if (rc != ERR_SUCCESS) {
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|