From 40aace4969144fd890788874439b003740f0e2ea Mon Sep 17 00:00:00 2001 From: huangxin Date: Tue, 6 Dec 2022 11:08:29 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E9=87=8D=E6=9E=84=20DHCP=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=9B=B8=E5=85=B3=E5=8D=8F=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/opendhcp.ini | 4 +- config/vcpe.cfg | 2 +- srcs/libs/include/user_errno.h | 3 +- srcs/open_dhcp/query.cpp | 98 ++++++++++++++++++++++------------ srcs/vcpe_main.c | 17 ++++-- 5 files changed, 82 insertions(+), 42 deletions(-) diff --git a/config/opendhcp.ini b/config/opendhcp.ini index e67e089..b90da89 100644 --- a/config/opendhcp.ini +++ b/config/opendhcp.ini @@ -12,8 +12,8 @@ # Requests from relay agents look for matching range to relay agent IP. # upto 125 interfaces can be specified # Default is All static Interfaces -192.168.30.1 -192.168.100.1 +# 192.168.30.1 +# 192.168.100.1 [LOGGING] diff --git a/config/vcpe.cfg b/config/vcpe.cfg index f4599f9..67dc4b5 100644 --- a/config/vcpe.cfg +++ b/config/vcpe.cfg @@ -85,7 +85,7 @@ application: # 2:采用AES128加密后的base64编码格式 # 3:采用3DES加密后的base64编码格式 # 4:采用AES256加密后的base64编码格式 - crypto_type = 2; + crypto_type = 0; crypto_key = "AES@rX2qZWVwGVlYTJLf/6X22w=="; }; diff --git a/srcs/libs/include/user_errno.h b/srcs/libs/include/user_errno.h index e9286c3..bf64c06 100644 --- a/srcs/libs/include/user_errno.h +++ b/srcs/libs/include/user_errno.h @@ -59,7 +59,8 @@ extern "C" { ERR_CODE(ERR_MISC_GET_GATEWAY, "获取网卡网关地址失败") \ ERR_CODE(ERR_MISC_GET_MACADDR, "获取网卡MAC地址失败") \ ERR_CODE(ERR_MENU_EXIT, "菜单执行完后自动退出") \ - ERR_CODE(ERR_HTTP_UNSUP_METHOD, "不支持的 HTTP 请求方法") + ERR_CODE(ERR_HTTP_UNSUP_METHOD, "不支持的 HTTP 请求方法") \ + ERR_CODE(ERR_PROTO_DECODE, "HTTP 协议解析失败") #define GENERATE_ENUM(ENUM, x) ENUM, diff --git a/srcs/open_dhcp/query.cpp b/srcs/open_dhcp/query.cpp index 6728ea3..8da3e3f 100644 --- a/srcs/open_dhcp/query.cpp +++ b/srcs/open_dhcp/query.cpp @@ -29,7 +29,7 @@ extern bool kRunning; extern dhcpMap dhcpCache; extern time_t t; -static void sendUserList(data19 *req, const char *pRequest) { +static int dhcp_get_user_info(data19 *req, const char *pRequest) { char logBuff[512]; const char *pStrContent; dhcpMap::iterator p; @@ -39,7 +39,7 @@ static void sendUserList(data19 *req, const char *pRequest) { if (pRequest == nullptr || strlen(pRequest) == 0) { sprintf(logBuff, "Requeset Json"); logDHCPMess(logBuff, 1); - return; + return ERR_INPUT_PARAMS; } pStrContent = proto_decode_context(pRequest, nullptr, nullptr); @@ -47,21 +47,21 @@ static void sendUserList(data19 *req, const char *pRequest) { if (pStrContent == nullptr) { sprintf(logBuff, "Requeset Json error %s", pRequest); logDHCPMess(logBuff, 1); - return; + return ERR_PROTO_DECODE; } cJSON *pRoot = cJSON_Parse(pStrContent); free((void *)pStrContent); if (!pRoot) { - return; + return ERR_JSON_PRASE_OBJ; } cJSON *pUserMac = cJSON_GetObjectItem(pRoot, "userMac"); if (!pUserMac) { cJSON_Delete(pRoot); - return; + return ERR_JSON_PRASE_OBJ; } req->memSize = (int)(2048 + (135 * dhcpCache.size()) + (cfig.dhcpSize * 26)); @@ -70,7 +70,7 @@ static void sendUserList(data19 *req, const char *pRequest) { if (!req->dp) { sprintf(logBuff, "Memory Error"); logDHCPMess(logBuff, 1); - return; + return ERR_JSON_PRASE_OBJ; } // cJSON *pRspRoot = cJSON_CreateObject(); @@ -127,9 +127,11 @@ static void sendUserList(data19 *req, const char *pRequest) { cJSON_Delete(pRoot); free((void *)pStrPro); + + return ERR_SUCCESS; } -static void sendAllLists(data19 *req) { +static int dhcp_get_all_user(data19 *req) { char logBuff[512]; data7 *dhcpEntry; dhcpMap::iterator p; @@ -140,7 +142,7 @@ static void sendAllLists(data19 *req) { if (!req->dp) { sprintf(logBuff, "Memory Error"); logDHCPMess(logBuff, 1); - return; + return ERR_MALLOC_MEMORY; } char *fp = req->dp; @@ -170,6 +172,8 @@ static void sendAllLists(data19 *req) { req->bytes = (int)(fp - req->dp); free((void *)pStrPro); + + return ERR_SUCCESS; } #define VALUE_TO_DHCP_TLV(buf, val, tag) \ @@ -247,8 +251,9 @@ static void add_options(OBJ_DHCP_RNG pRange, data20 *optionData) { optionData->optionSize = (dp - optionData->options); } -static void expand_range_set(data19 *req, const char *pRequest) { +static int add_dhcpd_rangeset(data19 *req, const char *pRequest) { char logBuff[512]; + const char *pStrContent; OBJ_DHCP_RNG pRange; char *fp; cJSON *pRspRoot; @@ -259,15 +264,25 @@ static void expand_range_set(data19 *req, const char *pRequest) { if (pRequest == nullptr || strlen(pRequest) == 0) { sprintf(logBuff, "Requeset Json"); logDHCPMess(logBuff, 1); - return; + return ERR_INPUT_PARAMS; } - cJSON *pRoot = cJSON_Parse(pRequest); + 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; + return ERR_JSON_PRASE_OBJ; } - cJSON *prange_set = cJSON_GetObjectItem(pRoot, "range_set"); + cJSON *prange_set = cJSON_GetObjectItem(pRoot, "rangeSet"); req->memSize = (int)(2048 + (135 * dhcpCache.size()) + (cfig.dhcpSize * 26)); req->dp = (char *)calloc(1, req->memSize); @@ -276,29 +291,31 @@ static void expand_range_set(data19 *req, const char *pRequest) { sprintf(logBuff, "Memory Error"); logDHCPMess(logBuff, 1); cJSON_Delete(pRoot); - return; + return ERR_MALLOC_MEMORY; } fp = req->dp; pRspRoot = cJSON_CreateObject(); pExpandArray = cJSON_CreateArray(); - cJSON_AddItemToObject(pRspRoot, "expansion", pExpandArray); + cJSON_AddItemToObject(pRspRoot, "rangeSet", pExpandArray); 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 *pdhcp_range = cJSON_GetObjectItem(pItem, "dhcpRange"); cJSON *pEx_range = cJSON_CreateObject(); if (!pdhcp_range) { - cJSON_Delete(pRoot); - return; + continue; } - cJSON *psubnet_mask = cJSON_GetObjectItem(pItem, "subnet_mask"); - cJSON *pdomain_server = cJSON_GetObjectItem(pItem, "domain_server"); + 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"); cJSON *pgateway = cJSON_GetObjectItem(pItem, "gateway"); - cJSON *please_time = cJSON_GetObjectItem(pItem, "lease_time"); + cJSON *please_time = cJSON_GetObjectItem(pItem, "leaseTime"); memset(&pRange, 0, sizeof(OBJ_DHCP_RNG)); @@ -316,7 +333,7 @@ static void expand_range_set(data19 *req, const char *pRequest) { } if (please_time) { - pRange.lease = STR2INT(please_time->valuestring); + pRange.lease = please_time->valueint; } //写入cfig @@ -342,17 +359,19 @@ static void expand_range_set(data19 *req, const char *pRequest) { } 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); } - fp += sprintf(fp, "%s", cJSON_Print(pRspRoot)); + const char *pStrPro = proto_create_new(pRspRoot, 200); + + fp += sprintf(fp, "%s", pStrPro); cJSON_Delete(pRoot); - cJSON_Delete(pRspRoot); req->bytes = (int)(fp - req->dp); + free((void *)pStrPro); + + return ERR_SUCCESS; } #pragma clang diagnostic push @@ -508,6 +527,7 @@ static void opendhcp_http_get_userinfo(http_request *request, hw_string body; hw_string keep_alive_name; hw_string keep_alive_value; + int ret; auto *req = (data19 *)malloc(sizeof(struct data19)); @@ -528,10 +548,9 @@ static void opendhcp_http_get_userinfo(http_request *request, hw_set_response_header(response, &content_type_name, &content_type_value); SETSTRING(status_code, HTTP_STATUS_200); - sendUserList(req, request->body->value); - - if (req->dp == nullptr) { - proto_response_error(response, 500, HTTP_STATUS_500, ERR_JSON_PRASE_OBJ); + ret = dhcp_get_user_info(req, request->body->value); + if (ret != ERR_SUCCESS) { + proto_response_error(response, 500, HTTP_STATUS_500, ret); return; } SETSTRING(body, req->dp); @@ -558,6 +577,7 @@ static void opendhcp_http_get_alluser(http_request *request, hw_string body; hw_string keep_alive_name; hw_string keep_alive_value; + int ret; auto *req = (data19 *)malloc(sizeof(struct data19)); @@ -578,7 +598,11 @@ static void opendhcp_http_get_alluser(http_request *request, hw_set_response_header(response, &content_type_name, &content_type_value); SETSTRING(status_code, HTTP_STATUS_200); - sendAllLists(req); + ret = dhcp_get_all_user(req); + 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); @@ -603,6 +627,7 @@ static void opendhcp_http_expand_rangeset(http_request *request, hw_string body; hw_string keep_alive_name; hw_string keep_alive_value; + int ret; auto *req = (data19 *)malloc(sizeof(struct data19)); @@ -618,11 +643,16 @@ static void opendhcp_http_expand_rangeset(http_request *request, memset(req, 0, sizeof(struct data19)); SETSTRING(content_type_name, "Content-Type"); - SETSTRING(content_type_value, "text/html"); + SETSTRING(content_type_value, "application/json"); hw_set_response_header(response, &content_type_name, &content_type_value); SETSTRING(status_code, HTTP_STATUS_200); - expand_range_set(req, request->body->value); + ret = add_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); diff --git a/srcs/vcpe_main.c b/srcs/vcpe_main.c index 2254320..fe798a0 100644 --- a/srcs/vcpe_main.c +++ b/srcs/vcpe_main.c @@ -46,6 +46,17 @@ static void lwip_init_env() { #endif int main(int argc, char **argv) { + const char *pJson = + "{\n" + " \"ver\": 3,\n" + " \"cryptoType\": 1,\n" + " \"timeStamp\": 1599187216753,\n" + " \"msgContent\": " + "\"ewogICAgInVzZXJNYWMiOiBbCiAgICAgICIwMDowYzoyOTpiNTpkMzozYSIsCiAgICAgICIwMDowYzoyOTpiNTpk" + "MzozYiIsCiAgICAgICIw" + "MDowYzoyOTowZDo2Nzo2ZSIsCiAgICBdCn0=\"\n" + "}"; + int ret; #ifdef OPENDHCPDDNS_ON return dual_server_main(argc, argv); @@ -56,7 +67,7 @@ int main(int argc, char **argv) { ret = menu_run(argc, argv); - if(ret == ERR_MENU_EXIT) { + if (ret == ERR_MENU_EXIT) { user_uninit(); exit(0); } else if (ret != ERR_SUCCESS) { @@ -71,11 +82,9 @@ int main(int argc, char **argv) { pppoe_session_init(); #endif - proto_create_new(NULL, 0); - task_manager_run(); - while(!is_system_cleanup()) { + while (!is_system_cleanup()) { sleep(1); }