From b85c2522ac06ef0e4844e66c0de39a2ec166f64b Mon Sep 17 00:00:00 2001 From: huangxin Date: Thu, 24 Nov 2022 14:28:19 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E6=9B=B4=E6=96=B0DHCP=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E8=A7=A3=E6=9E=90=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/open_dhcp/opendhcpd.cpp | 2 - srcs/open_dhcp/opendhcpd.h | 2 + srcs/open_dhcp/query.cpp | 117 +++++++++++++++++++++++++---------- 3 files changed, 85 insertions(+), 36 deletions(-) diff --git a/srcs/open_dhcp/opendhcpd.cpp b/srcs/open_dhcp/opendhcpd.cpp index d4f8ca2..507c865 100644 --- a/srcs/open_dhcp/opendhcpd.cpp +++ b/srcs/open_dhcp/opendhcpd.cpp @@ -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; diff --git a/srcs/open_dhcp/opendhcpd.h b/srcs/open_dhcp/opendhcpd.h index 8544504..710aef5 100644 --- a/srcs/open_dhcp/opendhcpd.h +++ b/srcs/open_dhcp/opendhcpd.h @@ -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; diff --git a/srcs/open_dhcp/query.cpp b/srcs/open_dhcp/query.cpp index cf49585..15cbd8b 100644 --- a/srcs/open_dhcp/query.cpp +++ b/srcs/open_dhcp/query.cpp @@ -22,8 +22,6 @@ using namespace std; #include #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);