// // Created by xajhuang on 2023/3/23. // #include #include "lease.h" #include "user_errno.h" #include "zlog_module.h" #include "database.h" #include "user_mgr.h" #include "rfc2131.h" #include "dhcp_network.h" #include "db_interface.h" #define PREALLOC_IP_TIMEOUT (60) typedef struct { U32 ipAddr; U8 macAddr[ETH_ALEN]; 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; //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 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) { PIPPOOL_INFO pPool, pTemp; PPRE_ALLOC_IP pNewIp, pTmp, pCache = NULL; PIPPOOL_INFO pUserPool; if (pReq == NULL || pOutIp == NULL || pOutPool == NULL) { LOG_MOD(error, ZLOG_MOD_DHCPD, "Input params error: %p, %p\n", pOutIp, pOutPool); return -ERR_INPUT_PARAMS; } pUserPool = user_get_pool(pReq->uid); 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; // 查看是否预分配过该设备 if ((addr = lease_is_pre_assign(pReq)) != 0) { *pOutIp = addr; *pOutPool = pPool; return ERR_SUCCESS; } 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; // } // 查找IP地址是否已经被预分配, 未分配直接分配IP地址 if (lease_ip_is_pre_assign(pReq->uid, ipAddr) == FALSE) { lease_db_add_pre_assign(pReq, ipAddr, pPool); *pOutIp = ipAddr; *pOutPool = pPool; bitset_cls_bit(pPool->assignPool, addr); LOG_MOD(trace, ZLOG_MOD_DHCPD, "Select ipaddr %08X at %d of %p\n", ipAddr, addr, pPool->assignPool); return ERR_SUCCESS; } } } // 清理所有超时的预分配IP lease_clearup_timeout_pre_assign(); // 没有可预分配的IP,报错 LOG_MOD(error, ZLOG_MOD_DHCPD, "No free ipaddress in poll: uid = %u, pool = 0x%08X\n", pReq->uid, pUserPool->poolKey); return -ERR_DHCP_NO_ADDR; } int dhcp_lease_init() { int rc = 0; rc = lease_init_database(); if (rc != ERR_SUCCESS) { return rc; } return ERR_SUCCESS; }