OCT 1. 更新DHCP 配置解析逻辑

This commit is contained in:
huangxin 2022-11-24 14:28:19 +08:00
parent 876760acf6
commit b85c2522ac
3 changed files with 85 additions and 36 deletions

View File

@ -41,8 +41,6 @@ using namespace std;
#include "dhcpd.h"
#include "task_manager.h"
#define STR2INT(val) ((int)strtol((val), nullptr, 10))
void on_system_exit(void *p);
//Global Variables
timeval tv;

View File

@ -70,6 +70,8 @@ typedef struct in_pktinfo IN_PKTINFO;
#define LPSOCKADDR sockaddr *
#define closesocket close
#define STR2INT(val) ((int)strtol((val), nullptr, 10))
struct data7//cache
{
char *mapname;

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,43 +165,90 @@ static void sendAllLists(data19 *req) {
req->bytes = (int)(fp - req->dp);
}
static void add_options(OBJ_DHCP_RNG pRange, data20* optionData){
#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};
*dp = 0;
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) {
strcpy(value, pRange.subnet);
VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_NETMASK);
optionData->mask = (*((MYDWORD *)value));
}
if(strlen(pRange.subnet) != 0 && !map_Flag[0]){
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);
}
#if 0
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;
opTag = DHCP_OPTION_NETMASK;
strcpy(value, pRange.subnet);
}else if(strlen(pRange.dnsSvr) != 0 && !map_Flag[1]){
} else if (strlen(pRange.dnsSvr) != 0 && !map_Flag[1]) {
map_Flag[1] = true;
opTag = DHCP_OPTION_DNS;
opTag = DHCP_OPTION_DNS;
strcpy(value, pRange.dnsSvr);
}else if(strlen(pRange.gateway) != 0 && !map_Flag[2]){
} else if (strlen(pRange.gateway) != 0 && !map_Flag[2]) {
map_Flag[2] = true;
opTag = DHCP_OPTION_ROUTER;
opTag = DHCP_OPTION_ROUTER;
strcpy(value, pRange.gateway);
}else
break;
} else {
continue;
}
int numbytes = myTokenize(buff, value, "/,.", true);
if(numbytes > 255)
if (numbytes > 255) {
break;
else{
char *ptr = buff;
} else {
char *ptr = buff;
valSize = 0;
for (; *ptr; ptr = myGetToken(ptr, 1)) {
if (isInt(ptr)) {
@ -217,11 +262,11 @@ static void add_options(OBJ_DHCP_RNG pRange, data20* optionData){
memcpy(value, hoption, valSize);
}
if(opTag == DHCP_OPTION_NETMASK && map_Flag[0]){
if (opTag == DHCP_OPTION_NETMASK && map_Flag[0]) {
optionData->mask = (*((MYDWORD *)value));
}
if(buffSize > valSize + 2){
if (buffSize > valSize + 2) {
*dp = opTag;
dp++;
*dp = valSize;
@ -230,20 +275,20 @@ static void add_options(OBJ_DHCP_RNG pRange, data20* optionData){
dp += valSize;
buffSize -= (valSize + 2);
}
}
#endif
//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;
}
}
@ -253,9 +298,10 @@ static void add_options(OBJ_DHCP_RNG pRange, data20* optionData){
}
static void expand_range_set(data19 *req, const char *pRequest) {
char logBuff[512];
char logBuff[512];
OBJ_DHCP_RNG pRange;
memset(&pRange, 0, sizeof(OBJ_DHCP_RNG));
char *fp;
cJSON *pRspRoot;
dzlog_debug("Input: %s\n", pRequest);
@ -288,12 +334,14 @@ 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();
memset(&pRange, 0, sizeof(OBJ_DHCP_RNG));
strcpy(pRange.rangAddr, pdhcp_range->valuestring);
@ -301,15 +349,15 @@ static void expand_range_set(data19 *req, const char *pRequest) {
strcpy(pRange.subnet, psubnet_mask->valuestring);
}
if (pdomain_server){
if (pdomain_server) {
strcpy(pRange.dnsSvr, pdomain_server->valuestring);
}
if (pgateway){
if (pgateway) {
strcpy(pRange.gateway, pgateway->valuestring);
}
if (please_time){
if (please_time) {
pRange.lease = STR2INT(please_time->valuestring);
}
@ -341,6 +389,7 @@ static void expand_range_set(data19 *req, const char *pRequest) {
fp += sprintf(fp, "%s", cJSON_Print(pRspRoot));
cJSON_Delete(pRoot);
cJSON_Delete(pRspRoot);
req->bytes = (int)(fp - req->dp);