OCT 1. 修改DHCP Rang配置读取功能

This commit is contained in:
huangxin 2022-11-22 09:49:33 +08:00
parent fbbf377b3b
commit d56098caca
5 changed files with 69 additions and 124 deletions

View File

@ -71,25 +71,25 @@ do {
ADD_CFG_ITEM(CFG_DHCP_RANGE_SET, "application.dhcp_server.range_set", VALUE_TYPE_ARRAY_OBJ, "", "DHCP IP pool"); \ ADD_CFG_ITEM(CFG_DHCP_RANGE_SET, "application.dhcp_server.range_set", VALUE_TYPE_ARRAY_OBJ, "", "DHCP IP pool"); \
} while (0)// clang-format on } while (0)// clang-format on
//typedef union { typedef union {
// long long longValue; long long longValue;
// char *strValue; char *strValue;
// long double floatValue; long double floatValue;
// vector array; vector array;
//} CFG_VALUE, *PCFG_VALUE; } CFG_VALUE, *PCFG_VALUE;
//
//typedef struct { typedef struct {
// CONFIG_ITEM_ID cfgId; CONFIG_ITEM_ID cfgId;
// const char *pcfgKey; const char *pcfgKey;
// CONFIG_VALUE_TYPE valType; CONFIG_VALUE_TYPE valType;
// const char *defaultValue; const char *defaultValue;
// int isChanged; int isChanged;
// const char *pMessage; const char *pMessage;
// const char *pStrId; const char *pStrId;
// CFG_VALUE value; CFG_VALUE value;
//
// UT_hash_handle hh; UT_hash_handle hh;
//} CONFIG_ITEM, *PCONFIG_ITEM; } CONFIG_ITEM, *PCONFIG_ITEM;
static config_t g_cfgContent; static config_t g_cfgContent;
static const char *g_cfgFilePath; static const char *g_cfgFilePath;
@ -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) { int init_config_system(const char *pCfgFile, const char *pKey) {
if (!file_exists(pCfgFile)) { if (!file_exists(pCfgFile)) {
dzlog_error("Configuration file [%s] not exists\n", pCfgFile); dzlog_error("Configuration file [%s] not exists\n", pCfgFile);

View File

@ -3,6 +3,10 @@
// //
#include "config.h" #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() { const char* config_get_http_server_addr() {
return cfg_get_string_value(CFG_HTTP_SVR_ADDR); return cfg_get_string_value(CFG_HTTP_SVR_ADDR);
} }

View File

@ -3,7 +3,6 @@
// //
#include "zvector/zvector.h" #include "zvector/zvector.h"
#include "uthash/uthash.h"
#ifndef DAEMON_AGENT_INCLUDE_CONFIG_H #ifndef DAEMON_AGENT_INCLUDE_CONFIG_H
#define DAEMON_AGENT_INCLUDE_CONFIG_H #define DAEMON_AGENT_INCLUDE_CONFIG_H
@ -13,7 +12,6 @@ extern "C" {
#define DEFAULT_INTEGRAL_ERR_VALUE (0x0AFFFFAA) #define DEFAULT_INTEGRAL_ERR_VALUE (0x0AFFFFAA)
typedef struct { typedef struct {
char rangAddr[256]; char rangAddr[256];
char subnet[20]; char subnet[20];
@ -69,27 +67,8 @@ typedef enum {
CONFIG_ITEM_ID_MAX CONFIG_ITEM_ID_MAX
} CONFIG_ITEM_ID; } 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_dir();
const char*get_cur_process_name(); const char *get_cur_process_name();
const char *config_get_ssl_ca_path(); const char *config_get_ssl_ca_path();
const char *cfg_get_config_directory(); 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_zero_mq_data_path();
const char *cfg_get_string_value(CONFIG_ITEM_ID id); 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); long double cfg_get_float_value(CONFIG_ITEM_ID id);
int cfg_get_bool_value(CONFIG_ITEM_ID id); int cfg_get_bool_value(CONFIG_ITEM_ID id);
long long cfg_get_integral_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(); const char *config_get_http_server_addr();
unsigned int config_get_http_server_port(); unsigned int config_get_http_server_port();
int config_get_http_server_tcp_nodelay(); int config_get_http_server_tcp_nodelay();
const vector config_get_dhcp_server_range_set();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -15,8 +15,8 @@
# pragma once # pragma once
#endif #endif
#ifdef _cplusplus #ifdef __cplusplus
extern "C" { extern "C"{
#endif #endif
// Requires standard C libraries: // Requires standard C libraries:
@ -229,13 +229,8 @@ zvect_retval vect_sem_post(const vector v);
* corresponds to the top of a * corresponds to the top of a
* Stack. * Stack.
*/ */
#ifdef __cplusplus
extern "C"{
#endif
void vect_push(vector const v, const void *item); void vect_push(vector const v, const void *item);
#ifdef __cplusplus
}
#endif
/* /*
* vect_pop(v) "pops" (returns) the element * vect_pop(v) "pops" (returns) the element
* at the back of the vector as * at the back of the vector as
@ -645,7 +640,7 @@ void vect_merge(vector const v1, vector v2);
#endif // ZVECT_SFMD_EXTENSIONS #endif // ZVECT_SFMD_EXTENSIONS
#ifdef _cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -19,14 +19,13 @@ using namespace std;
#include "misc.h" #include "misc.h"
#include <net/if_arp.h> #include <net/if_arp.h>
#include <libconfig.h> #include <libconfig.h>
#include <zlog.h>
#include "config.h" #include "config.h"
extern data2 cfig; extern data2 cfig;
extern bool kRunning; extern bool kRunning;
extern dhcpMap dhcpCache; extern dhcpMap dhcpCache;
extern time_t t; extern time_t t;
extern config_t g_cfgContent;
extern PCONFIG_ITEM g_pConfigItem;
static void sendUserList(data19 *req, const char *pRequest) { static void sendUserList(data19 *req, const char *pRequest) {
char logBuff[512]; char logBuff[512];
@ -166,39 +165,10 @@ static void sendAllLists(data19 *req) {
req->bytes = (int)(fp - req->dp); 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) { 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) { if (pRequest == nullptr || strlen(pRequest) == 0) {
sprintf(logBuff, "Requeset Json"); sprintf(logBuff, "Requeset Json");
@ -211,7 +181,7 @@ static void expand_range_set(data19 *req, const char *pRequest) {
return; return;
} }
cJSON * pdhcp_range = cJSON_GetObjectItem(pRoot, "dhcp_range"); cJSON *pdhcp_range = cJSON_GetObjectItem(pRoot, "dhcp_range");
if (!pdhcp_range) { if (!pdhcp_range) {
cJSON_Delete(pRoot); cJSON_Delete(pRoot);
@ -236,50 +206,34 @@ static void expand_range_set(data19 *req, const char *pRequest) {
cJSON *pRspRoot = cJSON_CreateObject(); 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) { for(int i = 0; i < vect_size(dhcp_rang); i++) {
strcpy(pRange.subnet, psubnet_mask->valuestring); add_array_obj("", (POBJ_DHCP_RNG)vect_get_at(dhcp_rang, i), pRange);
} 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;
} }
//写入CONFIG_ITEM // //写入CONFIG_ITEM
PCONFIG_ITEM pItem, pTmp; // PCONFIG_ITEM pItem, pTmp;
HASH_ITER(hh, g_pConfigItem, pItem, pTmp){ // HASH_ITER(hh, g_pConfigItem, pItem, pTmp) {
if(pItem->valType == VALUE_TYPE_ARRAY_OBJ){ // if (pItem->valType == VALUE_TYPE_ARRAY_OBJ) {
add_array_obj(pItem->pcfgKey, pItem, pRange); // add_array_obj(pItem->pcfgKey, pItem, pRange);
} // }
} // }
//写入cfig //写入cfig
MYBYTE m = cfig.rangeCount; MYBYTE m = cfig.rangeCount;
data20 optionData {}; data20 optionData {};
optionData.rangeSetInd = m; optionData.rangeSetInd = m;
data20* opData = &optionData; data20 *opData = &optionData;
MYBYTE *dp = opData->options; MYBYTE *dp = opData->options;
*dp = 0; *dp = 0;
dp++; dp++;
addDHCPRange(pRange.rangAddr); addDHCPRange(pRange.rangAddr);
opData->mask = (*((MYDWORD *)pRange.subnet)); opData->mask = (*((MYDWORD *)pRange.subnet));
if(please_time){ if (please_time) {
MYDWORD j; MYDWORD j;
j = strtol(please_time->valuestring, nullptr, 10); j = strtol(please_time->valuestring, nullptr, 10);
cfig.lease = j; cfig.lease = j;
@ -299,9 +253,10 @@ static void expand_range_set(data19 *req, const char *pRequest) {
cfig.dhcpRanges[m].rangeSetInd = optionData.rangeSetInd; cfig.dhcpRanges[m].rangeSetInd = optionData.rangeSetInd;
cfig.dhcpRanges[m].options = options; cfig.dhcpRanges[m].options = options;
cfig.dhcpRanges[m].mask = optionData.mask; cfig.dhcpRanges[m].mask = optionData.mask;
if (!cfig.dhcpRanges[m].mask) if (!cfig.dhcpRanges[m].mask) {
cfig.dhcpRanges[m].mask = cfig.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_start", cfig.dhcpRanges[m].rangeStart);
cJSON_AddNumberToObject(pRspRoot, "expand_end", cfig.dhcpRanges[m].rangeEnd); 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); 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 status_code;
hw_string content_type_name; hw_string content_type_name;
hw_string content_type_value; 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)); auto *req = (data19 *)malloc(sizeof(struct data19));
if(req == nullptr) { if (req == nullptr) {
hw_http_response_send_error(response, HTTP_STATUS_500, "memory error"); hw_http_response_send_error(response, HTTP_STATUS_500, "memory error");
return; return;
} }