diff --git a/srcs/open_dhcp/query.cpp b/srcs/open_dhcp/query.cpp index 64e444c..f626a51 100644 --- a/srcs/open_dhcp/query.cpp +++ b/srcs/open_dhcp/query.cpp @@ -245,6 +245,7 @@ static void expand_range_set(data19 *req, const char *pRequest) { OBJ_DHCP_RNG pRange; char *fp; cJSON *pRspRoot; + cJSON *pExpandArray; dzlog_debug("Input: %s\n", pRequest); @@ -259,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); @@ -283,53 +274,72 @@ static void expand_range_set(data19 *req, const char *pRequest) { fp = req->dp; pRspRoot = cJSON_CreateObject(); + pExpandArray = cJSON_CreateArray(); + cJSON_AddItemToObject(pRspRoot, "expansion", pExpandArray); - memset(&pRange, 0, sizeof(OBJ_DHCP_RNG)); + 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(); - strcpy(pRange.rangAddr, pdhcp_range->valuestring); + if (!pdhcp_range) { + cJSON_Delete(pRoot); + return; + } - if (psubnet_mask) { - strcpy(pRange.subnet, psubnet_mask->valuestring); + 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);