2023-03-29 09:02:21 +00:00
|
|
|
|
//
|
|
|
|
|
// Created by xajhuang on 2023/3/23.
|
|
|
|
|
//
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include "ip_pool.h"
|
|
|
|
|
#include "zvector/zvector.h"
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#include "user_errno.h"
|
|
|
|
|
#include "zlog_module.h"
|
|
|
|
|
#include "user_mgr.h"
|
2023-04-07 07:56:38 +00:00
|
|
|
|
#include "ipaddr.h"
|
2023-04-25 01:17:08 +00:00
|
|
|
|
#include "uthash/utlist.h"
|
2023-03-29 09:02:21 +00:00
|
|
|
|
|
2023-04-25 01:17:08 +00:00
|
|
|
|
static PIPPOOL_INFO g_defPool = NULL;
|
|
|
|
|
static PPOOL_CONFIG g_pUsrCommonCfg = NULL;
|
|
|
|
|
static PPOOL_CONFIG g_pUsrGrpCfg = NULL;
|
|
|
|
|
static PPOOL_CONFIG g_pUsrCfg = NULL;
|
2023-03-29 09:02:21 +00:00
|
|
|
|
|
|
|
|
|
U32 get_ip_pool_addr(U32 defAddr) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 01:17:08 +00:00
|
|
|
|
static void add_usr_pool_cfg(U32 id, PPOOL_CONFIG pPoolList, PIPPOOL_INFO pPool) {
|
|
|
|
|
PPOOL_CTX p, pTmp;
|
|
|
|
|
PPOOL_CONFIG pCfg;
|
|
|
|
|
|
|
|
|
|
// 判断当前用户组是否存在
|
|
|
|
|
HASH_FIND_INT(pPoolList, &id, pCfg);
|
|
|
|
|
|
|
|
|
|
// 不存在则新建一个配置并保存
|
|
|
|
|
if (pCfg == NULL) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果存在,则遍历当前所有IP地址池配置,检查配置是否合法
|
|
|
|
|
LL_FOREACH_SAFE(pPoolList->pCtx, p, pTmp) {
|
|
|
|
|
if (p->minAddr > pPool->minAddr && p->maxAddr < pPool->maxAddr) {
|
|
|
|
|
LOG_MOD(error,
|
|
|
|
|
ZLOG_MOD_DHCPD,
|
|
|
|
|
"Pool [%08X, %08X] conflict with [%08X, %08X]",
|
|
|
|
|
pPool->minAddr,
|
|
|
|
|
pPool->maxAddr,
|
|
|
|
|
p->minAddr,
|
|
|
|
|
p->maxAddr);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 09:02:21 +00:00
|
|
|
|
void init_default_pool() {
|
|
|
|
|
c_vector pool = (c_vector)config_get_dhcp_server_range_set();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; (pool && i < vect_size(pool)); i++) {
|
|
|
|
|
char *pConnChar;
|
|
|
|
|
char tmpStr[64];
|
|
|
|
|
POBJ_DHCP_RNG pRange = (POBJ_DHCP_RNG)vect_get_at(pool, i);
|
|
|
|
|
|
|
|
|
|
if (pRange) {
|
|
|
|
|
struct in_addr addr;
|
|
|
|
|
PIPPOOL_INFO p = (PIPPOOL_INFO)malloc(sizeof(IPPOOL_INFO));
|
|
|
|
|
|
|
|
|
|
if (!p) {
|
|
|
|
|
LOG_MOD(error, ZLOG_MOD_OPENDHCPD, "Malloc memory error: %lu\n", sizeof(IPPOOL_INFO));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 07:56:38 +00:00
|
|
|
|
memset(p, 0, sizeof(IPPOOL_INFO));
|
|
|
|
|
|
2023-03-29 09:02:21 +00:00
|
|
|
|
if (strlen(pRange->rangAddr) == 0) {
|
|
|
|
|
LOG_MOD(error, ZLOG_MOD_OPENDHCPD, "Error ip pool configure of address\n");
|
|
|
|
|
free(p);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pRange->lease > config_get_dhcp_server_lease_time()) {
|
|
|
|
|
p->leaseTime = 0;
|
|
|
|
|
} else {
|
|
|
|
|
p->leaseTime = pRange->lease;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(tmpStr, 0, 64);
|
|
|
|
|
strcpy(tmpStr, pRange->rangAddr);
|
|
|
|
|
|
|
|
|
|
pConnChar = strchr(tmpStr, '-');
|
|
|
|
|
|
|
|
|
|
// '-' 连接IP地址类型
|
|
|
|
|
if (pConnChar) {
|
|
|
|
|
char *pSecIp = pConnChar + 1;
|
|
|
|
|
pConnChar[0] = 0;
|
|
|
|
|
|
|
|
|
|
if (inet_aton(tmpStr, &addr) == 0) {
|
|
|
|
|
LOG_MOD(error, ZLOG_MOD_DHCPD, "Convert ip ERROR: %s of %s\n", tmpStr, pRange->rangAddr);
|
|
|
|
|
free(p);
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
2023-04-07 07:56:38 +00:00
|
|
|
|
p->minAddr = ntohl(addr.s_addr);
|
2023-03-29 09:02:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inet_aton(pSecIp, &addr) == 0) {
|
|
|
|
|
LOG_MOD(error, ZLOG_MOD_DHCPD, "Convert ip ERROR: %s of %s\n", pSecIp, pRange->rangAddr);
|
|
|
|
|
free(p);
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
2023-04-07 07:56:38 +00:00
|
|
|
|
p->maxAddr = ntohl(addr.s_addr);
|
2023-03-29 09:02:21 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
LOG_MOD(error, ZLOG_MOD_DHCPD, "Bad DHCP range format: %s\n", tmpStr);
|
|
|
|
|
free(p);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 07:56:38 +00:00
|
|
|
|
if (strlen(pRange->subnet) > 0) {
|
|
|
|
|
if (inet_aton(pRange->subnet, &addr) == 0) {
|
|
|
|
|
LOG_MOD(error, ZLOG_MOD_DHCPD, "Convert ip ERROR: %s\n", pRange->subnet);
|
|
|
|
|
} else {
|
|
|
|
|
p->netMask = ntohl(addr.s_addr);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 当前网络默认IP地址池
|
|
|
|
|
if (inet_aton("255.255.255.0", &addr) == 0) {
|
|
|
|
|
LOG_MOD(error, ZLOG_MOD_DHCPD, "Convert ip ERROR: %s\n", pRange->subnet);
|
2023-03-29 09:02:21 +00:00
|
|
|
|
} else {
|
2023-04-07 07:56:38 +00:00
|
|
|
|
p->netMask = ntohl(addr.s_addr);
|
2023-03-29 09:02:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 07:56:38 +00:00
|
|
|
|
// 填充POOL Hash Key
|
|
|
|
|
p->poolKey = ipv4_get_network_addr(p->minAddr, p->netMask);
|
|
|
|
|
p->assignPool = bitset_create_with_capacity(ipv4_network_total_addr(p->netMask));
|
|
|
|
|
|
|
|
|
|
if (!p->assignPool) {
|
|
|
|
|
LOG_MOD(error,
|
|
|
|
|
ZLOG_MOD_DHCPD,
|
|
|
|
|
"Create address pool bitset ERROR: 0x%08X total address %u\n",
|
|
|
|
|
htonl(p->netMask),
|
|
|
|
|
ipv4_network_total_addr(p->netMask));
|
|
|
|
|
free(p);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strlen(pRange->gateway) > 0) {
|
|
|
|
|
if (inet_aton(pRange->gateway, &addr) == 0) {
|
|
|
|
|
LOG_MOD(error, ZLOG_MOD_DHCPD, "Convert ip ERROR: %s\n", pRange->gateway);
|
2023-03-29 09:02:21 +00:00
|
|
|
|
} else {
|
2023-04-07 07:56:38 +00:00
|
|
|
|
p->gwAddr = ntohl(addr.s_addr);
|
2023-03-29 09:02:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strlen(pRange->dnsSvr) > 0) {
|
|
|
|
|
memset(tmpStr, 0, 64);
|
|
|
|
|
strcpy(tmpStr, pRange->dnsSvr);
|
|
|
|
|
|
|
|
|
|
pConnChar = strchr(tmpStr, ',');
|
|
|
|
|
|
|
|
|
|
if (pConnChar) {
|
|
|
|
|
char *pSecIp = pConnChar + 1;
|
|
|
|
|
pConnChar[0] = 0;
|
|
|
|
|
|
|
|
|
|
if (inet_aton(tmpStr, &addr) == 0) {
|
|
|
|
|
LOG_MOD(error, ZLOG_MOD_DHCPD, "Convert ip ERROR: %s of %s\n", tmpStr, pRange->dnsSvr);
|
|
|
|
|
} else {
|
2023-04-07 07:56:38 +00:00
|
|
|
|
p->primeDNS = ntohl(addr.s_addr);
|
2023-03-29 09:02:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inet_aton(pSecIp, &addr) == 0) {
|
|
|
|
|
LOG_MOD(error, ZLOG_MOD_DHCPD, "Convert ip ERROR: %s of %s\n", pSecIp, pRange->dnsSvr);
|
|
|
|
|
} else {
|
2023-04-07 07:56:38 +00:00
|
|
|
|
p->salveDNS = ntohl(addr.s_addr);
|
2023-03-29 09:02:21 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (inet_aton(pRange->dnsSvr, &addr) == 0) {
|
|
|
|
|
LOG_MOD(error, ZLOG_MOD_DHCPD, "Convert ip ERROR: %s of %s\n", tmpStr, pRange->dnsSvr);
|
|
|
|
|
} else {
|
2023-04-07 07:56:38 +00:00
|
|
|
|
p->primeDNS = ntohl(addr.s_addr);
|
2023-03-29 09:02:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user_add_ip_pool(pRange->vni, p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|