vcpe/srcs/service/dhcpd/ip_pool.c

132 lines
4.3 KiB
C
Raw Normal View History

//
// 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"
#define GEN_IP_POOL_HASH_KEY(p) (sprintf((p)->poolKey, "%08X-%08X", (p)->minAddr, (p)->maxAddr))
static PIPPOOL_INFO g_defPool = NULL;
U32 get_ip_pool_addr(U32 defAddr) {
return 0;
}
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;
}
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 = 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 = addr.s_addr;
}
// 填充POOL Hash Key
GEN_IP_POOL_HASH_KEY(p);
} else {
LOG_MOD(error, ZLOG_MOD_DHCPD, "Bad DHCP range format: %s\n", tmpStr);
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 = addr.s_addr;
}
}
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 = 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 = 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 = 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 = addr.s_addr;
}
}
}
user_add_ip_pool(pRange->vni, p);
}
}
}