From d56098caca714987c06645770010120cda1062b6 Mon Sep 17 00:00:00 2001 From: huangxin Date: Tue, 22 Nov 2022 09:49:33 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E4=BF=AE=E6=94=B9DHCP=20Rang?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E8=AF=BB=E5=8F=96=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/libs/configure/config.c | 50 ++++++++------ srcs/libs/configure/config_help.c | 4 ++ srcs/libs/include/config.h | 25 +------ srcs/libs/include/zvector/zvector.h | 13 ++-- srcs/open_dhcp/query.cpp | 101 ++++++++-------------------- 5 files changed, 69 insertions(+), 124 deletions(-) diff --git a/srcs/libs/configure/config.c b/srcs/libs/configure/config.c index 04fd9cc..2fd6de3 100644 --- a/srcs/libs/configure/config.c +++ b/srcs/libs/configure/config.c @@ -71,25 +71,25 @@ do { ADD_CFG_ITEM(CFG_DHCP_RANGE_SET, "application.dhcp_server.range_set", VALUE_TYPE_ARRAY_OBJ, "", "DHCP IP pool"); \ } while (0)// clang-format on -//typedef union { -// long long longValue; -// char *strValue; -// long double floatValue; -// vector array; -//} CFG_VALUE, *PCFG_VALUE; -// -//typedef struct { -// CONFIG_ITEM_ID cfgId; -// const char *pcfgKey; -// CONFIG_VALUE_TYPE valType; -// const char *defaultValue; -// int isChanged; -// const char *pMessage; -// const char *pStrId; -// CFG_VALUE value; -// -// UT_hash_handle hh; -//} CONFIG_ITEM, *PCONFIG_ITEM; +typedef union { + long long longValue; + char *strValue; + long double floatValue; + vector array; +} CFG_VALUE, *PCFG_VALUE; + +typedef struct { + CONFIG_ITEM_ID cfgId; + const char *pcfgKey; + CONFIG_VALUE_TYPE valType; + const char *defaultValue; + int isChanged; + const char *pMessage; + const char *pStrId; + CFG_VALUE value; + + UT_hash_handle hh; +} CONFIG_ITEM, *PCONFIG_ITEM; static config_t g_cfgContent; static const char *g_cfgFilePath; @@ -299,7 +299,7 @@ static int setConfigItemValue(PCONFIG_ITEM pItem, const char *pValue) { } else if (pItem->valType == VALUE_TYPE_ARRAY_STR) { pItem->value.array = vect_create(128, 512, ZV_SEC_WIPE); } else if (pItem->valType == VALUE_TYPE_ARRAY_OBJ) { - pItem->value.array = vect_create(128, sizeof(OBJ_DHCP_RNG), ZV_SEC_WIPE); + pItem->value.array = vect_create(128, sizeof(OBJ_DHCP_RNG), ZV_SEC_WIPE); } else { return -ERR_UN_SUPPORT; } @@ -655,6 +655,16 @@ const char *cfg_get_string_value(CONFIG_ITEM_ID id) { } } +const vector cfg_get_vector(CONFIG_ITEM_ID id) { + PCFG_VALUE pVal = cfg_get_value(id); + + if (pVal) { + return pVal->array; + } else { + return NULL; + } +} + int init_config_system(const char *pCfgFile, const char *pKey) { if (!file_exists(pCfgFile)) { dzlog_error("Configuration file [%s] not exists\n", pCfgFile); diff --git a/srcs/libs/configure/config_help.c b/srcs/libs/configure/config_help.c index a885918..e894893 100644 --- a/srcs/libs/configure/config_help.c +++ b/srcs/libs/configure/config_help.c @@ -3,6 +3,10 @@ // #include "config.h" +const vector config_get_dhcp_server_range_set() { + return cfg_get_vector(CFG_DHCP_RANGE_SET); +} + const char* config_get_http_server_addr() { return cfg_get_string_value(CFG_HTTP_SVR_ADDR); } diff --git a/srcs/libs/include/config.h b/srcs/libs/include/config.h index 0bb0943..1dc2714 100644 --- a/srcs/libs/include/config.h +++ b/srcs/libs/include/config.h @@ -3,7 +3,6 @@ // #include "zvector/zvector.h" -#include "uthash/uthash.h" #ifndef DAEMON_AGENT_INCLUDE_CONFIG_H #define DAEMON_AGENT_INCLUDE_CONFIG_H @@ -13,7 +12,6 @@ extern "C" { #define DEFAULT_INTEGRAL_ERR_VALUE (0x0AFFFFAA) - typedef struct { char rangAddr[256]; char subnet[20]; @@ -69,27 +67,8 @@ typedef enum { CONFIG_ITEM_ID_MAX } CONFIG_ITEM_ID; -typedef union { - long long longValue; - char *strValue; - long double floatValue; - vector array; -} CFG_VALUE, *PCFG_VALUE; - -typedef struct { - CONFIG_ITEM_ID cfgId; - const char *pcfgKey; - CONFIG_VALUE_TYPE valType; - const char *defaultValue; - int isChanged; - const char *pMessage; - const char *pStrId; - CFG_VALUE value; - UT_hash_handle hh; -} CONFIG_ITEM, *PCONFIG_ITEM; - const char *get_cur_process_dir(); -const char*get_cur_process_name(); +const char *get_cur_process_name(); const char *config_get_ssl_ca_path(); const char *cfg_get_config_directory(); @@ -116,6 +95,7 @@ int cfg_get_zero_mq_port(); const char *cfg_get_zero_mq_data_path(); const char *cfg_get_string_value(CONFIG_ITEM_ID id); +const vector cfg_get_vector(CONFIG_ITEM_ID id); long double cfg_get_float_value(CONFIG_ITEM_ID id); int cfg_get_bool_value(CONFIG_ITEM_ID id); long long cfg_get_integral_value(CONFIG_ITEM_ID id); @@ -133,6 +113,7 @@ const char *config_get_vxlan_pkg_filter(); const char *config_get_http_server_addr(); unsigned int config_get_http_server_port(); int config_get_http_server_tcp_nodelay(); +const vector config_get_dhcp_server_range_set(); #ifdef __cplusplus } #endif diff --git a/srcs/libs/include/zvector/zvector.h b/srcs/libs/include/zvector/zvector.h index 09c76de..9c2578f 100644 --- a/srcs/libs/include/zvector/zvector.h +++ b/srcs/libs/include/zvector/zvector.h @@ -15,8 +15,8 @@ # pragma once #endif -#ifdef _cplusplus -extern "C" { +#ifdef __cplusplus +extern "C"{ #endif // Requires standard C libraries: @@ -229,13 +229,8 @@ zvect_retval vect_sem_post(const vector v); * corresponds to the top of a * Stack. */ -#ifdef __cplusplus - extern "C"{ -#endif void vect_push(vector const v, const void *item); -#ifdef __cplusplus - } -#endif + /* * vect_pop(v) "pops" (returns) the element * at the back of the vector as @@ -645,7 +640,7 @@ void vect_merge(vector const v1, vector v2); #endif // ZVECT_SFMD_EXTENSIONS -#ifdef _cplusplus +#ifdef __cplusplus } #endif diff --git a/srcs/open_dhcp/query.cpp b/srcs/open_dhcp/query.cpp index 82e0f0d..39ac641 100644 --- a/srcs/open_dhcp/query.cpp +++ b/srcs/open_dhcp/query.cpp @@ -19,14 +19,13 @@ using namespace std; #include "misc.h" #include #include +#include #include "config.h" extern data2 cfig; extern bool kRunning; extern dhcpMap dhcpCache; extern time_t t; -extern config_t g_cfgContent; -extern PCONFIG_ITEM g_pConfigItem; static void sendUserList(data19 *req, const char *pRequest) { char logBuff[512]; @@ -166,39 +165,10 @@ static void sendAllLists(data19 *req) { req->bytes = (int)(fp - req->dp); } -static int cmp_dhcpcfg(const void *a, const void *b) { - auto pV1 = (POBJ_DHCP_RNG)a; - auto pV2 = (POBJ_DHCP_RNG)b; - - if (strcmp(pV1->rangAddr, pV2->rangAddr) == 0 && strcmp(pV1->subnet, pV2->subnet) == 0 - && strcmp(pV1->dnsSvr, pV2->dnsSvr) == 0 && strcmp(pV1->gateway, pV2->gateway) == 0 - && pV1->lease == pV2->lease) { - return 0; - } - - return -1; -} - -static void add_array_obj(const char *pKeyName, PCONFIG_ITEM pValue, OBJ_DHCP_RNG pRange){ - OBJ_DHCP_RNG pAdd; - zvect_index idx = -1; - switch (pValue->cfgId) { - case CFG_DHCP_RANGE_SET:{ - if (!vect_bsearch(pValue->value.array, &pRange, cmp_dhcpcfg, &idx)){ - memcpy(&pAdd, &pRange, sizeof(OBJ_DHCP_RNG)); - vect_push(pValue->value.array, &pAdd); - pValue->isChanged = TRUE; - printf("++++Add %s\n", pAdd.rangAddr); - } - } - default: ; - } -} - static void expand_range_set(data19 *req, const char *pRequest) { - char logBuff[512]; + char logBuff[512]; - printf("Input: %s\n", pRequest); + dzlog_debug("Input: %s\n", pRequest); if (pRequest == nullptr || strlen(pRequest) == 0) { sprintf(logBuff, "Requeset Json"); @@ -211,17 +181,17 @@ static void expand_range_set(data19 *req, const char *pRequest) { return; } - cJSON * pdhcp_range = cJSON_GetObjectItem(pRoot, "dhcp_range"); + cJSON *pdhcp_range = cJSON_GetObjectItem(pRoot, "dhcp_range"); if (!pdhcp_range) { cJSON_Delete(pRoot); return; } - cJSON *psubnet_mask = cJSON_GetObjectItem(pRoot, "subnet_mask"); + 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 *pgateway = cJSON_GetObjectItem(pRoot, "gateway"); + cJSON *please_time = cJSON_GetObjectItem(pRoot, "lease_time"); req->memSize = (int)(2048 + (135 * dhcpCache.size()) + (cfig.dhcpSize * 26)); req->dp = (char *)calloc(1, req->memSize); @@ -236,59 +206,43 @@ static void expand_range_set(data19 *req, const char *pRequest) { cJSON *pRspRoot = cJSON_CreateObject(); - OBJ_DHCP_RNG pRange; - strcpy(pRange.rangAddr, pdhcp_range->valuestring); + vector dhcp_rang = config_get_dhcp_server_range_set(); - if(psubnet_mask) { - strcpy(pRange.subnet, psubnet_mask->valuestring); - } else { - strcpy(pRange.subnet, "255.255.255.0"); - } - if(pdomain_server){ - strcpy(pRange.dnsSvr, pdomain_server->valuestring); - } else { - strcpy(pRange.dnsSvr, "114.114.114.114,8.8.8.8"); - } - if(pgateway){ - strcpy(pRange.gateway, pgateway->valuestring); - } - if(please_time){ - pRange.lease = atoi(please_time->valuestring); - }else{ - pRange.lease = 360; + for(int i = 0; i < vect_size(dhcp_rang); i++) { + add_array_obj("", (POBJ_DHCP_RNG)vect_get_at(dhcp_rang, i), pRange); } - //写入CONFIG_ITEM - PCONFIG_ITEM pItem, pTmp; - HASH_ITER(hh, g_pConfigItem, pItem, pTmp){ - if(pItem->valType == VALUE_TYPE_ARRAY_OBJ){ - add_array_obj(pItem->pcfgKey, pItem, pRange); - } - } +// //写入CONFIG_ITEM +// PCONFIG_ITEM pItem, pTmp; +// HASH_ITER(hh, g_pConfigItem, pItem, pTmp) { +// if (pItem->valType == VALUE_TYPE_ARRAY_OBJ) { +// add_array_obj(pItem->pcfgKey, pItem, pRange); +// } +// } //写入cfig MYBYTE m = cfig.rangeCount; data20 optionData {}; optionData.rangeSetInd = m; - data20* opData = &optionData; - MYBYTE *dp = opData->options; - *dp = 0; + data20 *opData = &optionData; + MYBYTE *dp = opData->options; + *dp = 0; dp++; addDHCPRange(pRange.rangAddr); opData->mask = (*((MYDWORD *)pRange.subnet)); - if(please_time){ + if (please_time) { MYDWORD j; - j = strtol(please_time->valuestring, nullptr, 10); + j = strtol(please_time->valuestring, nullptr, 10); cfig.lease = j; } *dp = DHCP_OPTION_END; dp++; opData->optionSize = (dp - opData->options); - MYBYTE *options = nullptr; + MYBYTE *options = nullptr; cfig.rangeSet[optionData.rangeSetInd].active = true; if (optionData.optionSize > 3) { @@ -299,9 +253,10 @@ static void expand_range_set(data19 *req, const char *pRequest) { cfig.dhcpRanges[m].rangeSetInd = optionData.rangeSetInd; cfig.dhcpRanges[m].options = options; cfig.dhcpRanges[m].mask = optionData.mask; - if (!cfig.dhcpRanges[m].mask) + if (!cfig.dhcpRanges[m].mask) { cfig.dhcpRanges[m].mask = cfig.mask; - cfig.rangeCount = (char)(m+1); + } + cfig.rangeCount = (char)(m + 1); cJSON_AddNumberToObject(pRspRoot, "expand_start", cfig.dhcpRanges[m].rangeStart); cJSON_AddNumberToObject(pRspRoot, "expand_end", cfig.dhcpRanges[m].rangeEnd); @@ -526,7 +481,7 @@ static void opendhcp_http_get_alluser(http_request *request, hw_http_response *r hw_http_response_send(response, req, response_complete); } -static void opendhcp_http_expand_rangeset(http_request *request, hw_http_response *response, void *UNUSED(user_data)){ +static void opendhcp_http_expand_rangeset(http_request *request, hw_http_response *response, void *UNUSED(user_data)) { hw_string status_code; hw_string content_type_name; hw_string content_type_value; @@ -536,7 +491,7 @@ static void opendhcp_http_expand_rangeset(http_request *request, hw_http_respons auto *req = (data19 *)malloc(sizeof(struct data19)); - if(req == nullptr) { + if (req == nullptr) { hw_http_response_send_error(response, HTTP_STATUS_500, "memory error"); return; }