182 lines
6.1 KiB
C
182 lines
6.1 KiB
C
//
|
||
// 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"
|
||
#include "ipaddr.h"
|
||
#include "uthash/utlist.h"
|
||
|
||
static PIPPOOL_INFO g_defPool = NULL;
|
||
static PPOOL_CONFIG g_pUsrCommonCfg = NULL;
|
||
static PPOOL_CONFIG g_pUsrGrpCfg = NULL;
|
||
static PPOOL_CONFIG g_pUsrCfg = NULL;
|
||
|
||
U32 get_ip_pool_addr(U32 defAddr) {
|
||
return 0;
|
||
}
|
||
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
memset(p, 0, sizeof(IPPOOL_INFO));
|
||
|
||
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 {
|
||
p->minAddr = ntohl(addr.s_addr);
|
||
}
|
||
|
||
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 {
|
||
p->maxAddr = ntohl(addr.s_addr);
|
||
}
|
||
} else {
|
||
LOG_MOD(error, ZLOG_MOD_DHCPD, "Bad DHCP range format: %s\n", tmpStr);
|
||
free(p);
|
||
continue;
|
||
}
|
||
|
||
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);
|
||
} else {
|
||
p->netMask = ntohl(addr.s_addr);
|
||
}
|
||
}
|
||
|
||
// 填充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);
|
||
} else {
|
||
p->gwAddr = ntohl(addr.s_addr);
|
||
}
|
||
}
|
||
|
||
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 {
|
||
p->primeDNS = ntohl(addr.s_addr);
|
||
}
|
||
|
||
if (inet_aton(pSecIp, &addr) == 0) {
|
||
LOG_MOD(error, ZLOG_MOD_DHCPD, "Convert ip ERROR: %s of %s\n", pSecIp, pRange->dnsSvr);
|
||
} else {
|
||
p->salveDNS = ntohl(addr.s_addr);
|
||
}
|
||
} 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 {
|
||
p->primeDNS = ntohl(addr.s_addr);
|
||
}
|
||
}
|
||
}
|
||
|
||
user_add_ip_pool(pRange->vni, p);
|
||
}
|
||
}
|
||
} |