OCT 修改接口增加的地址池配置写入options

This commit is contained in:
dongwenze 2022-11-23 14:56:54 +08:00
parent d56098caca
commit 4219626caa
1 changed files with 105 additions and 27 deletions

View File

@ -22,6 +22,8 @@ using namespace std;
#include <zlog.h>
#include "config.h"
#define STR2INT(val) ((int)strtol((val), nullptr, 10))
extern data2 cfig;
extern bool kRunning;
extern dhcpMap dhcpCache;
@ -165,8 +167,95 @@ static void sendAllLists(data19 *req) {
req->bytes = (int)(fp - req->dp);
}
static void add_options(OBJ_DHCP_RNG pRange, data20* optionData){
MYBYTE *dp = optionData->options;
MYBYTE opTag = 0;
MYBYTE valSize = 0;
MYWORD buffSize = sizeof(dhcp_packet) - sizeof(dhcp_header);
char value[512] = {0};
bool map_Flag[3]= {false};
*dp = 0;
dp++;
//dhcp_range
addDHCPRange(pRange.rangAddr);
for(int i = 0; i < 3; i++){
char buff[1024];
MYBYTE hoption[256];
if(strlen(pRange.subnet) != 0 && !map_Flag[0]){
map_Flag[0] = true;
opTag = DHCP_OPTION_NETMASK;
strcpy(value, pRange.subnet);
}else if(strlen(pRange.dnsSvr) != 0 && !map_Flag[1]){
map_Flag[1] = true;
opTag = DHCP_OPTION_DNS;
strcpy(value, pRange.dnsSvr);
}else if(strlen(pRange.gateway) != 0 && !map_Flag[2]){
map_Flag[2] = true;
opTag = DHCP_OPTION_ROUTER;
strcpy(value, pRange.gateway);
}else
break;
int numbytes = myTokenize(buff, value, "/,.", true);
if(numbytes > 255)
break;
else{
char *ptr = buff;
valSize = 0;
for (; *ptr; ptr = myGetToken(ptr, 1)) {
if (isInt(ptr)) {
hoption[valSize] = STR2INT(ptr);
valSize++;
} else {
break;
}
}
memcpy(value, hoption, valSize);
}
if(opTag == DHCP_OPTION_NETMASK && map_Flag[0]){
optionData->mask = (*((MYDWORD *)value));
}
if(buffSize > valSize + 2){
*dp = opTag;
dp++;
*dp = valSize;
dp++;
memcpy(dp, value, valSize);
dp += valSize;
buffSize -= (valSize + 2);
}
}
//lease
if (pRange.lease != 0) {
MYDWORD j;
j = pRange.lease;
opTag = 51;
if (buffSize > 6) {
*dp = DHCP_OPTION_IPADDRLEASE;
dp++;
*dp = 4;
dp++;
dp += pUInt(dp, j);
buffSize -= 6;
}
}
*dp = DHCP_OPTION_END;
dp++;
optionData->optionSize = (dp - optionData->options);
}
static void expand_range_set(data19 *req, const char *pRequest) {
char logBuff[512];
OBJ_DHCP_RNG pRange;
memset(&pRange, 0, sizeof(OBJ_DHCP_RNG));
dzlog_debug("Input: %s\n", pRequest);
@ -206,45 +295,34 @@ static void expand_range_set(data19 *req, const char *pRequest) {
cJSON *pRspRoot = cJSON_CreateObject();
strcpy(pRange.rangAddr, pdhcp_range->valuestring);
vector dhcp_rang = config_get_dhcp_server_range_set();
for(int i = 0; i < vect_size(dhcp_rang); i++) {
add_array_obj("", (POBJ_DHCP_RNG)vect_get_at(dhcp_rang, i), pRange);
if (psubnet_mask) {
strcpy(pRange.subnet, psubnet_mask->valuestring);
}
// //写入CONFIG_ITEM
// PCONFIG_ITEM pItem, pTmp;
// HASH_ITER(hh, g_pConfigItem, pItem, pTmp) {
// if (pItem->valType == VALUE_TYPE_ARRAY_OBJ) {
// add_array_obj(pItem->pcfgKey, pItem, pRange);
// }
// }
if (pdomain_server){
strcpy(pRange.dnsSvr, pdomain_server->valuestring);
}
if (pgateway){
strcpy(pRange.gateway, pgateway->valuestring);
}
if (please_time){
pRange.lease = STR2INT(please_time->valuestring);
}
//写入cfig
MYBYTE m = cfig.rangeCount;
data20 optionData {};
optionData.rangeSetInd = m;
data20 *opData = &optionData;
MYBYTE *dp = opData->options;
*dp = 0;
dp++;
addDHCPRange(pRange.rangAddr);
opData->mask = (*((MYDWORD *)pRange.subnet));
if (please_time) {
MYDWORD j;
j = strtol(please_time->valuestring, nullptr, 10);
cfig.lease = j;
}
*dp = DHCP_OPTION_END;
dp++;
opData->optionSize = (dp - opData->options);
MYBYTE *options = nullptr;
cfig.rangeSet[optionData.rangeSetInd].active = true;
add_options(pRange, &optionData);
if (optionData.optionSize > 3) {
options = (MYBYTE *)calloc(1, optionData.optionSize);
memcpy(options, optionData.options, optionData.optionSize);