diff --git a/srcs/open_dhcp/query.cpp b/srcs/open_dhcp/query.cpp index cf49585..314993a 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,83 +165,73 @@ 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]){ - 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; + if (strlen(pRange.dnsSvr) != 0) { + strcpy(value, pRange.dnsSvr); + VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_DNS); + } - 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); - } + 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; } } @@ -253,9 +241,11 @@ 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; + 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,59 +268,81 @@ 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); - strcpy(pRange.rangAddr, pdhcp_range->valuestring); + 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 (psubnet_mask) { - strcpy(pRange.subnet, psubnet_mask->valuestring); + 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); + } + + 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; + + 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); + } + + cfig.dhcpRanges[m].rangeSetInd = optionData.rangeSetInd; + cfig.dhcpRanges[m].options = options; + cfig.dhcpRanges[m].mask = optionData.mask; + if (!cfig.dhcpRanges[m].mask) { + cfig.dhcpRanges[m].mask = cfig.mask; + } + cfig.rangeCount = (char)(m + 1); + + 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); } - 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; - - 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); - } - - cfig.dhcpRanges[m].rangeSetInd = optionData.rangeSetInd; - cfig.dhcpRanges[m].options = options; - cfig.dhcpRanges[m].mask = optionData.mask; - if (!cfig.dhcpRanges[m].mask) { - cfig.dhcpRanges[m].mask = cfig.mask; - } - cfig.rangeCount = (char)(m + 1); - - cJSON_AddNumberToObject(pRspRoot, "expand_start", cfig.dhcpRanges[m].rangeStart); - cJSON_AddNumberToObject(pRspRoot, "expand_end", cfig.dhcpRanges[m].rangeEnd); - fp += sprintf(fp, "%s", cJSON_Print(pRspRoot)); + cJSON_Delete(pRoot); cJSON_Delete(pRspRoot); req->bytes = (int)(fp - req->dp);