diff --git a/srcs/open_dhcp/opendhcpd.cpp b/srcs/open_dhcp/opendhcpd.cpp index 4965f48..f0a3f03 100644 --- a/srcs/open_dhcp/opendhcpd.cpp +++ b/srcs/open_dhcp/opendhcpd.cpp @@ -40,6 +40,7 @@ using namespace std; #include "opendhcpd.h" #include "dhcpd.h" #include "task_manager.h" +#include "user_errno.h" void on_system_exit(void *p); //Global Variables @@ -55,6 +56,7 @@ data71 lump; bool kRunning = true; dhcpMap dhcpCache; char serviceName[] = "OpenDHCPServer"; +int ERR_PARAM; //char tempbuff[512] = ""; //char logBuff[256]; //char extbuff[256] = ""; @@ -3051,6 +3053,7 @@ void addDHCPRange(char *dp) { (range->rangeEnd >= rs && range->rangeEnd <= re)) { sprintf(logBuff, "Warning: DHCP Range %s overlaps with another range, ignored", dp); logDHCPMess(logBuff, 1); + ERR_PARAM = ERR_ITEM_EXISTS; return; } } @@ -3074,6 +3077,7 @@ void addDHCPRange(char *dp) { sprintf(logBuff, "DHCP Ranges Load, Memory Allocation Error"); logDHCPMess(logBuff, 1); + ERR_PARAM = ERR_MALLOC_MEMORY; return; } } diff --git a/srcs/open_dhcp/query.cpp b/srcs/open_dhcp/query.cpp index aa9ef26..0bb508f 100644 --- a/srcs/open_dhcp/query.cpp +++ b/srcs/open_dhcp/query.cpp @@ -28,6 +28,7 @@ extern data2 cfig; extern bool kRunning; extern dhcpMap dhcpCache; extern time_t t; +extern int ERR_PARAM; static int dhcp_get_user_info(data19 *req, const char *pRequest) { char logBuff[512]; @@ -216,6 +217,9 @@ static void add_options(OBJ_DHCP_RNG pRange, data20 *optionData) { //dhcp_range addDHCPRange(pRange.rangAddr); + if(ERR_PARAM != ERR_SUCCESS){ + return; + } if (strlen(pRange.subnet) != 0) { strcpy(value, pRange.subnet); @@ -259,6 +263,7 @@ static int add_dhcpd_rangeset(data19 *req, const char *pRequest) { cJSON *pRspRoot; cJSON *pExpandArray; + ERR_PARAM = ERR_SUCCESS; dzlog_debug("Input: %s\n", pRequest); if (pRequest == nullptr || strlen(pRequest) == 0) { @@ -309,8 +314,6 @@ static int add_dhcpd_rangeset(data19 *req, const char *pRequest) { } cJSON_AddStringToObject(pEx_range, "dhcpRange", pdhcp_range->valuestring); - cJSON_AddNumberToObject(pEx_range, "status", ERR_SUCCESS); - cJSON_AddStringToObject(pEx_range, "message", getErrorEnumDesc(ERR_SUCCESS)); cJSON *psubnet_mask = cJSON_GetObjectItem(pItem, "netmask"); cJSON *pdomain_server = cJSON_GetObjectItem(pItem, "domainServer"); @@ -345,6 +348,12 @@ static int add_dhcpd_rangeset(data19 *req, const char *pRequest) { cfig.rangeSet[optionData.rangeSetInd].active = true; add_options(pRange, &optionData); + if(ERR_PARAM != ERR_SUCCESS){ + cJSON_AddNumberToObject(pEx_range, "status", ERR_PARAM); + cJSON_AddStringToObject(pEx_range, "message", getErrorEnumDesc(ERR_PARAM)); + cJSON_AddItemToArray(pExpandArray, pEx_range); + break; + } if (optionData.optionSize > 3) { options = (MYBYTE *)calloc(1, optionData.optionSize); @@ -359,6 +368,8 @@ static int add_dhcpd_rangeset(data19 *req, const char *pRequest) { } cfig.rangeCount = (char)(m + 1); + cJSON_AddNumberToObject(pEx_range, "status", ERR_SUCCESS); + cJSON_AddStringToObject(pEx_range, "message", getErrorEnumDesc(ERR_SUCCESS)); cJSON_AddItemToArray(pExpandArray, pEx_range); } @@ -374,6 +385,122 @@ static int add_dhcpd_rangeset(data19 *req, const char *pRequest) { return ERR_SUCCESS; } +static int delete_dhcpd_rangeset(data19 *req, const char *pRequest){ + char logBuff[512]; + const char *pStrContent; + char *fp; + cJSON *pRspRoot; + cJSON *pdelArray; + + dzlog_debug("Input: %s\n", pRequest); + + if (pRequest == nullptr || strlen(pRequest) == 0) { + sprintf(logBuff, "Requeset Json"); + logDHCPMess(logBuff, 1); + return ERR_INPUT_PARAMS; + } + + pStrContent = proto_decode_context(pRequest, nullptr, nullptr); + if (pStrContent == nullptr) { + sprintf(logBuff, "Requeset Json error %s", pRequest); + logDHCPMess(logBuff, 1); + return ERR_PROTO_DECODE; + } + + cJSON *pRoot = cJSON_Parse(pStrContent); + free((void *)pStrContent); + if (!pRoot) { + return ERR_JSON_PRASE_OBJ; + } + + cJSON *pdhcp_range = cJSON_GetObjectItem(pRoot, "dhcpRange"); + + 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); + cJSON_Delete(pRoot); + return ERR_MALLOC_MEMORY; + } + + fp = req->dp; + pRspRoot = cJSON_CreateObject(); + pdelArray = cJSON_CreateArray(); + cJSON_AddItemToObject(pRspRoot, "rangeSet", pdelArray); + + for(int i = 0; i < cJSON_GetArraySize(pdhcp_range); i++){ + cJSON *pdelRange = cJSON_GetArrayItem(pdhcp_range, i); + cJSON *pdel_Item = cJSON_CreateObject(); + char del_range[256]; + bool deleted_flag = false; + + if(!pdelRange){ + continue; + } + + strcpy(del_range, pdelRange->valuestring); + + MYDWORD st_addr; + MYDWORD en_addr; + char start[128]; + char end[128]; + mySplit(start, end, del_range, '-'); + st_addr = htonl(inet_addr(start)); + en_addr = htonl(inet_addr(end)); + + cJSON_AddStringToObject(pdel_Item, "dhcpRange", pdelRange->valuestring); + if(st_addr && en_addr && st_addr <= en_addr){ + MYBYTE m = 0; + + for (; m < cfig.rangeCount && cfig.dhcpRanges[m].rangeStart; m++) { + if(!deleted_flag){ + if((st_addr == cfig.dhcpRanges[m].rangeStart) && (en_addr == cfig.dhcpRanges[m].rangeEnd)){ + deleted_flag = true; + if(m == cfig.rangeCount-1){ + memset(&cfig.dhcpRanges[m], 0, sizeof(struct data19)); + } + continue; + }else if(st_addr == cfig.dhcpRanges[m].rangeStart||en_addr == cfig.dhcpRanges[m].rangeEnd){ + deleted_flag = false; + break; + } + }else{ + memcpy(&(cfig.dhcpRanges[m-1]), &(cfig.dhcpRanges[m]), sizeof(struct data19)); + } + } + }else{ + cJSON_AddNumberToObject(pdel_Item, "status", ERR_INPUT_PARAMS); + cJSON_AddStringToObject(pdel_Item, "message", getErrorEnumDesc(ERR_INPUT_PARAMS)); + cJSON_AddItemToArray(pdelArray, pdel_Item); + break; + } + + if(deleted_flag){ + cfig.rangeCount--; + }else{ + cJSON_AddNumberToObject(pdel_Item, "status", ERR_INPUT_PARAMS); + cJSON_AddStringToObject(pdel_Item, "message", getErrorEnumDesc(ERR_INPUT_PARAMS)); + cJSON_AddItemToArray(pdelArray, pdel_Item); + break; + } + + cJSON_AddNumberToObject(pdel_Item, "status", ERR_SUCCESS); + cJSON_AddStringToObject(pdel_Item, "message", getErrorEnumDesc(ERR_SUCCESS)); + cJSON_AddItemToArray(pdelArray, pdel_Item); + } + + const char *pStrPro = proto_create_new(pRspRoot, 200); + fp += sprintf(fp, "%s", pStrPro); + + cJSON_Delete(pRoot); + req->bytes = (int)(fp - req->dp); + free((void*)pStrPro); + + return ERR_SUCCESS; +} + #pragma clang diagnostic push #pragma ide diagnostic ignored "cert-err34-c" int getHwAddr(char *buff, char *mac) { @@ -609,7 +736,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_add_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; @@ -657,6 +784,54 @@ static void opendhcp_http_expand_rangeset(http_request *request, hw_http_respons hw_http_response_send(response, req, response_complete); } +static void opendhcp_http_delete_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; + int ret; + + 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, "application/json"); + hw_set_response_header(response, &content_type_name, &content_type_value); + + SETSTRING(status_code, HTTP_STATUS_200); + ret = delete_dhcpd_rangeset(req, request->body->value); + if (ret != ERR_SUCCESS) { + proto_response_error(response, 500, HTTP_STATUS_500, ret); + return; + } + + 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); +} + /** * 添加配置文件监听接口 * @return @@ -684,9 +859,10 @@ void opendhcp_init_http_server() { if (!added) { hw_http_add_route("/", opendhcp_http_info, nullptr); - hw_http_add_route("dchp/info/getuser", opendhcp_http_get_userinfo, nullptr); - hw_http_add_route("dchp/info/allusers", opendhcp_http_get_alluser, nullptr); - hw_http_add_route("dchp/config/rangeset", opendhcp_http_expand_rangeset, nullptr); + hw_http_add_route("dhcp/info/getuser", opendhcp_http_get_userinfo, nullptr); + hw_http_add_route("dhcp/info/allusers", opendhcp_http_get_alluser, nullptr); + hw_http_add_route("dhcp/config/rangeset", opendhcp_http_add_rangeset, nullptr); + hw_http_add_route("dhcp/delete/rangeset", opendhcp_http_delete_rangeset, nullptr); added = TRUE; } } \ No newline at end of file