diff --git a/srcs/libs/configure/config.c b/srcs/libs/configure/config.c index abe1922..beef0dc 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; diff --git a/srcs/libs/include/config.h b/srcs/libs/include/config.h index 4a0379b..af60272 100644 --- a/srcs/libs/include/config.h +++ b/srcs/libs/include/config.h @@ -2,6 +2,9 @@ // Created by xajhu on 2021/7/1 0001. // +#include "zvector/zvector.h" +#include "uthash/uthash.h" + #ifndef DAEMON_AGENT_INCLUDE_CONFIG_H #define DAEMON_AGENT_INCLUDE_CONFIG_H #ifdef __cplusplus @@ -10,6 +13,7 @@ extern "C" { #define DEFAULT_INTEGRAL_ERR_VALUE (0x0AFFFFAA) + typedef struct { char rangAddr[256]; char subnet[20]; @@ -65,6 +69,25 @@ 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 *config_get_ssl_ca_path(); diff --git a/srcs/libs/include/zvector/zvector.h b/srcs/libs/include/zvector/zvector.h index 5b73ff6..09c76de 100644 --- a/srcs/libs/include/zvector/zvector.h +++ b/srcs/libs/include/zvector/zvector.h @@ -229,8 +229,13 @@ zvect_retval vect_sem_post(const vector v); * corresponds to the top of a * Stack. */ -void vect_push(vector const v, const void *item); - +#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 @@ -478,8 +483,13 @@ void vect_qsort(vector const v, int (*compare_func)(const void *, const void*)); * int i = 5; * vect_bsearch(v, &i, my_compare); */ -bool vect_bsearch(vector const v, const void *key, int (*f1)(const void *, const void *), zvect_index *item_index); - +#ifdef __cplusplus + extern "C"{ +#endif + bool vect_bsearch(vector const v, const void *key, int (*f1)(const void *, const void *), zvect_index *item_index); +#ifdef __cplusplus + } +#endif /* * vect_add_ordered allows the insertion of new items in * an ordered fashion. Please note that for this to work diff --git a/srcs/open_dhcp/query.cpp b/srcs/open_dhcp/query.cpp index 5a9afc1..82e0f0d 100644 --- a/srcs/open_dhcp/query.cpp +++ b/srcs/open_dhcp/query.cpp @@ -12,18 +12,21 @@ using namespace std; #include #include #include -#include #include #include "opendhcpd.h" #include "s2j/cJSON.h" #include "haywire.h" #include "misc.h" #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]; @@ -163,6 +166,153 @@ 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]; + + printf("Input: %s\n", pRequest); + + if (pRequest == nullptr || strlen(pRequest) == 0) { + sprintf(logBuff, "Requeset Json"); + logDHCPMess(logBuff, 1); + return; + } + + cJSON *pRoot = cJSON_Parse(pRequest); + if (!pRoot) { + 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"); + + req->memSize = (int)(2048 + (135 * dhcpCache.size()) + (cfig.dhcpSize * 26)); + req->dp = (char *)calloc(1, req->memSize); + + if (!req->dp) { + sprintf(logBuff, "Memory Error"); + logDHCPMess(logBuff, 1); + return; + } + char *fp = req->dp; + char *maxData = req->dp + (req->memSize - 512); + + cJSON *pRspRoot = cJSON_CreateObject(); + + OBJ_DHCP_RNG pRange; + + strcpy(pRange.rangAddr, pdhcp_range->valuestring); + + 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; + } + + //写入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; + dp++; + + addDHCPRange(pRange.rangAddr); + opData->mask = (*((MYDWORD *)pRange.subnet)); + if(please_time){ + MYDWORD j; + j = strtol(please_time->valuestring, nullptr, 10); + cfig.lease = j; + } + *dp = DHCP_OPTION_END; + dp++; + opData->optionSize = (dp - opData->options); + + MYBYTE *options = nullptr; + cfig.rangeSet[optionData.rangeSetInd].active = true; + + 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(pRspRoot); + + req->bytes = (int)(fp - req->dp); +} + #pragma clang diagnostic push #pragma ide diagnostic ignored "cert-err34-c" int getHwAddr(char *buff, char *mac) { @@ -376,6 +526,48 @@ 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)){ + hw_string status_code; + hw_string content_type_name; + hw_string content_type_value; + hw_string body; + hw_string keep_alive_name; + hw_string keep_alive_value; + + auto *req = (data19 *)malloc(sizeof(struct data19)); + + if(req == nullptr) { + hw_http_response_send_error(response, HTTP_STATUS_500, "memory error"); + return; + } + + if (request->method != HW_HTTP_POST) { + hw_http_response_send_error(response, HTTP_STATUS_405, HTTP_STATUS_405); + return; + } + + memset(req, 0, sizeof(struct data19)); + SETSTRING(content_type_name, "Content-Type"); + SETSTRING(content_type_value, "text/html"); + hw_set_response_header(response, &content_type_name, &content_type_value); + + SETSTRING(status_code, HTTP_STATUS_200); + expand_range_set(req, request->body->value); + SETSTRING(body, req->dp); + hw_set_body(response, &body); + hw_set_response_status_code(response, &status_code); + + if (request->keep_alive) { + SETSTRING(keep_alive_name, "Connection"); + SETSTRING(keep_alive_value, "close"); + hw_set_response_header(response, &keep_alive_name, &keep_alive_value); + } else { + hw_set_http_version(response, 1, 0); + } + + hw_http_response_send(response, req, response_complete); +} + void opendhcp_init_http_server() { static int added = FALSE; @@ -383,6 +575,7 @@ void opendhcp_init_http_server() { hw_http_add_route("/", opendhcp_http_info, nullptr); hw_http_add_route("getuser", opendhcp_http_get_userinfo, nullptr); hw_http_add_route("allusers", opendhcp_http_get_alluser, nullptr); + hw_http_add_route("expansion", opendhcp_http_expand_rangeset, nullptr); added = TRUE; } -} +} \ No newline at end of file