OCT 修改接口支持同时增加多个地址块

This commit is contained in:
dongwenze 2022-11-24 15:41:55 +08:00
parent 876760acf6
commit e33051f987
1 changed files with 117 additions and 115 deletions

View File

@ -22,8 +22,6 @@ 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;
@ -167,83 +165,73 @@ static void sendAllLists(data19 *req) {
req->bytes = (int)(fp - req->dp);
}
#define VALUE_TO_DHCP_TLV(buf, val, tag) \
do { \
int valSize; \
int numbytes = myTokenize((buf), (val), "/,.", true); \
if (numbytes <= 255) { \
char *ptr = (buf); \
valSize = 0; \
for (; *ptr; ptr = myGetToken(ptr, 1)) { \
if (isInt(ptr)) { \
hoption[valSize] = STR2INT(ptr); \
valSize++; \
} else { \
break; \
} \
} \
memcpy((val), hoption, valSize); \
if (buffSize > valSize + 2) { \
*dp = (tag); \
dp++; \
*dp = valSize; \
dp++; \
memcpy(dp, (val), valSize); \
dp += valSize; \
buffSize -= (valSize + 2); \
} \
} \
} while (0)
static void add_options(OBJ_DHCP_RNG pRange, data20 *optionData) {
char buff[1024];
MYBYTE hoption[256];
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};
char value[256] = {0};
*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;
if (strlen(pRange.subnet) != 0) {
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]){
VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_NETMASK);
optionData->mask = (*((MYDWORD *)value));
}
if(buffSize > valSize + 2){
*dp = opTag;
dp++;
*dp = valSize;
dp++;
memcpy(dp, value, valSize);
dp += valSize;
buffSize -= (valSize + 2);
if (strlen(pRange.dnsSvr) != 0) {
strcpy(value, pRange.dnsSvr);
VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_DNS);
}
if (strlen(pRange.gateway) != 0) {
strcpy(value, pRange.gateway);
VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_ROUTER);
}
//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;
}
}
@ -255,7 +243,9 @@ static void add_options(OBJ_DHCP_RNG pRange, data20* optionData){
static void expand_range_set(data19 *req, const char *pRequest) {
char logBuff[512];
OBJ_DHCP_RNG pRange;
memset(&pRange, 0, sizeof(OBJ_DHCP_RNG));
char *fp;
cJSON *pRspRoot;
cJSON *pExpandArray;
dzlog_debug("Input: %s\n", pRequest);
@ -270,17 +260,7 @@ static void expand_range_set(data19 *req, const char *pRequest) {
return;
}
cJSON *pdhcp_range = cJSON_GetObjectItem(pRoot, "dhcp_range");
if (!pdhcp_range) {
cJSON_Delete(pRoot);
return;
}
cJSON *psubnet_mask = cJSON_GetObjectItem(pRoot, "subnet_mask");
cJSON *pdomain_server = cJSON_GetObjectItem(pRoot, "domain_server");
cJSON *pgateway = cJSON_GetObjectItem(pRoot, "gateway");
cJSON *please_time = cJSON_GetObjectItem(pRoot, "lease_time");
cJSON *prange_set = cJSON_GetObjectItem(pRoot, "range_set");
req->memSize = (int)(2048 + (135 * dhcpCache.size()) + (cfig.dhcpSize * 26));
req->dp = (char *)calloc(1, req->memSize);
@ -288,15 +268,34 @@ static void expand_range_set(data19 *req, const char *pRequest) {
if (!req->dp) {
sprintf(logBuff, "Memory Error");
logDHCPMess(logBuff, 1);
cJSON_Delete(pRoot);
return;
}
char *fp = req->dp;
char *maxData = req->dp + (req->memSize - 512);
cJSON *pRspRoot = cJSON_CreateObject();
fp = req->dp;
pRspRoot = cJSON_CreateObject();
pExpandArray = cJSON_CreateArray();
cJSON_AddItemToObject(pRspRoot, "expansion", pExpandArray);
for (int i = 0; i < cJSON_GetArraySize(prange_set); i++) {
char tempbuff[512];
cJSON *pItem = cJSON_GetArrayItem(prange_set, i);
cJSON *pdhcp_range = cJSON_GetObjectItem(pItem, "dhcp_range");
cJSON *pEx_range = cJSON_CreateObject();
if (!pdhcp_range) {
cJSON_Delete(pRoot);
return;
}
cJSON *psubnet_mask = cJSON_GetObjectItem(pItem, "subnet_mask");
cJSON *pdomain_server = cJSON_GetObjectItem(pItem, "domain_server");
cJSON *pgateway = cJSON_GetObjectItem(pItem, "gateway");
cJSON *please_time = cJSON_GetObjectItem(pItem, "lease_time");
memset(&pRange, 0, sizeof(OBJ_DHCP_RNG));
strcpy(pRange.rangAddr, pdhcp_range->valuestring);
if (psubnet_mask) {
strcpy(pRange.subnet, psubnet_mask->valuestring);
}
@ -336,11 +335,14 @@ static void expand_range_set(data19 *req, const char *pRequest) {
}
cfig.rangeCount = (char)(m + 1);
cJSON_AddNumberToObject(pRspRoot, "expand_start", cfig.dhcpRanges[m].rangeStart);
cJSON_AddNumberToObject(pRspRoot, "expand_end", cfig.dhcpRanges[m].rangeEnd);
cJSON_AddNumberToObject(pEx_range, "expand_start", cfig.dhcpRanges[m].rangeStart);
cJSON_AddNumberToObject(pEx_range, "expand_end", cfig.dhcpRanges[m].rangeEnd);
cJSON_AddItemToArray(pExpandArray, pEx_range);
}
fp += sprintf(fp, "%s", cJSON_Print(pRspRoot));
cJSON_Delete(pRoot);
cJSON_Delete(pRspRoot);
req->bytes = (int)(fp - req->dp);