OCT 1. 重构 DHCP 接口相关协议

This commit is contained in:
huangxin 2022-12-06 11:08:29 +08:00
parent b9abec4f16
commit 40aace4969
5 changed files with 82 additions and 42 deletions

View File

@ -12,8 +12,8 @@
# Requests from relay agents look for matching range to relay agent IP. # Requests from relay agents look for matching range to relay agent IP.
# upto 125 interfaces can be specified # upto 125 interfaces can be specified
# Default is All static Interfaces # Default is All static Interfaces
192.168.30.1 # 192.168.30.1
192.168.100.1 # 192.168.100.1
[LOGGING] [LOGGING]

View File

@ -85,7 +85,7 @@ application:
# 2采用AES128加密后的base64编码格式 # 2采用AES128加密后的base64编码格式
# 3采用3DES加密后的base64编码格式 # 3采用3DES加密后的base64编码格式
# 4采用AES256加密后的base64编码格式 # 4采用AES256加密后的base64编码格式
crypto_type = 2; crypto_type = 0;
crypto_key = "AES@rX2qZWVwGVlYTJLf/6X22w=="; crypto_key = "AES@rX2qZWVwGVlYTJLf/6X22w==";
}; };

View File

@ -59,7 +59,8 @@ extern "C" {
ERR_CODE(ERR_MISC_GET_GATEWAY, "获取网卡网关地址失败") \ ERR_CODE(ERR_MISC_GET_GATEWAY, "获取网卡网关地址失败") \
ERR_CODE(ERR_MISC_GET_MACADDR, "获取网卡MAC地址失败") \ ERR_CODE(ERR_MISC_GET_MACADDR, "获取网卡MAC地址失败") \
ERR_CODE(ERR_MENU_EXIT, "菜单执行完后自动退出") \ 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, #define GENERATE_ENUM(ENUM, x) ENUM,

View File

@ -29,7 +29,7 @@ extern bool kRunning;
extern dhcpMap dhcpCache; extern dhcpMap dhcpCache;
extern time_t t; 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]; char logBuff[512];
const char *pStrContent; const char *pStrContent;
dhcpMap::iterator p; dhcpMap::iterator p;
@ -39,7 +39,7 @@ static void sendUserList(data19 *req, const char *pRequest) {
if (pRequest == nullptr || strlen(pRequest) == 0) { if (pRequest == nullptr || strlen(pRequest) == 0) {
sprintf(logBuff, "Requeset Json"); sprintf(logBuff, "Requeset Json");
logDHCPMess(logBuff, 1); logDHCPMess(logBuff, 1);
return; return ERR_INPUT_PARAMS;
} }
pStrContent = proto_decode_context(pRequest, nullptr, nullptr); pStrContent = proto_decode_context(pRequest, nullptr, nullptr);
@ -47,21 +47,21 @@ static void sendUserList(data19 *req, const char *pRequest) {
if (pStrContent == nullptr) { if (pStrContent == nullptr) {
sprintf(logBuff, "Requeset Json error %s", pRequest); sprintf(logBuff, "Requeset Json error %s", pRequest);
logDHCPMess(logBuff, 1); logDHCPMess(logBuff, 1);
return; return ERR_PROTO_DECODE;
} }
cJSON *pRoot = cJSON_Parse(pStrContent); cJSON *pRoot = cJSON_Parse(pStrContent);
free((void *)pStrContent); free((void *)pStrContent);
if (!pRoot) { if (!pRoot) {
return; return ERR_JSON_PRASE_OBJ;
} }
cJSON *pUserMac = cJSON_GetObjectItem(pRoot, "userMac"); cJSON *pUserMac = cJSON_GetObjectItem(pRoot, "userMac");
if (!pUserMac) { if (!pUserMac) {
cJSON_Delete(pRoot); cJSON_Delete(pRoot);
return; return ERR_JSON_PRASE_OBJ;
} }
req->memSize = (int)(2048 + (135 * dhcpCache.size()) + (cfig.dhcpSize * 26)); 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) { if (!req->dp) {
sprintf(logBuff, "Memory Error"); sprintf(logBuff, "Memory Error");
logDHCPMess(logBuff, 1); logDHCPMess(logBuff, 1);
return; return ERR_JSON_PRASE_OBJ;
} }
// cJSON *pRspRoot = cJSON_CreateObject(); // cJSON *pRspRoot = cJSON_CreateObject();
@ -127,9 +127,11 @@ static void sendUserList(data19 *req, const char *pRequest) {
cJSON_Delete(pRoot); cJSON_Delete(pRoot);
free((void *)pStrPro); free((void *)pStrPro);
return ERR_SUCCESS;
} }
static void sendAllLists(data19 *req) { static int dhcp_get_all_user(data19 *req) {
char logBuff[512]; char logBuff[512];
data7 *dhcpEntry; data7 *dhcpEntry;
dhcpMap::iterator p; dhcpMap::iterator p;
@ -140,7 +142,7 @@ static void sendAllLists(data19 *req) {
if (!req->dp) { if (!req->dp) {
sprintf(logBuff, "Memory Error"); sprintf(logBuff, "Memory Error");
logDHCPMess(logBuff, 1); logDHCPMess(logBuff, 1);
return; return ERR_MALLOC_MEMORY;
} }
char *fp = req->dp; char *fp = req->dp;
@ -170,6 +172,8 @@ static void sendAllLists(data19 *req) {
req->bytes = (int)(fp - req->dp); req->bytes = (int)(fp - req->dp);
free((void *)pStrPro); free((void *)pStrPro);
return ERR_SUCCESS;
} }
#define VALUE_TO_DHCP_TLV(buf, val, tag) \ #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); 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]; char logBuff[512];
const char *pStrContent;
OBJ_DHCP_RNG pRange; OBJ_DHCP_RNG pRange;
char *fp; char *fp;
cJSON *pRspRoot; cJSON *pRspRoot;
@ -259,15 +264,25 @@ static void expand_range_set(data19 *req, const char *pRequest) {
if (pRequest == nullptr || strlen(pRequest) == 0) { if (pRequest == nullptr || strlen(pRequest) == 0) {
sprintf(logBuff, "Requeset Json"); sprintf(logBuff, "Requeset Json");
logDHCPMess(logBuff, 1); 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) { 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->memSize = (int)(2048 + (135 * dhcpCache.size()) + (cfig.dhcpSize * 26));
req->dp = (char *)calloc(1, req->memSize); 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"); sprintf(logBuff, "Memory Error");
logDHCPMess(logBuff, 1); logDHCPMess(logBuff, 1);
cJSON_Delete(pRoot); cJSON_Delete(pRoot);
return; return ERR_MALLOC_MEMORY;
} }
fp = req->dp; fp = req->dp;
pRspRoot = cJSON_CreateObject(); pRspRoot = cJSON_CreateObject();
pExpandArray = cJSON_CreateArray(); pExpandArray = cJSON_CreateArray();
cJSON_AddItemToObject(pRspRoot, "expansion", pExpandArray); cJSON_AddItemToObject(pRspRoot, "rangeSet", pExpandArray);
for (int i = 0; i < cJSON_GetArraySize(prange_set); i++) { for (int i = 0; i < cJSON_GetArraySize(prange_set); i++) {
char tempbuff[512];
cJSON *pItem = cJSON_GetArrayItem(prange_set, i); 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(); cJSON *pEx_range = cJSON_CreateObject();
if (!pdhcp_range) { if (!pdhcp_range) {
cJSON_Delete(pRoot); continue;
return;
} }
cJSON *psubnet_mask = cJSON_GetObjectItem(pItem, "subnet_mask"); cJSON_AddStringToObject(pEx_range, "dhcpRange", pdhcp_range->valuestring);
cJSON *pdomain_server = cJSON_GetObjectItem(pItem, "domain_server"); 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 *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)); memset(&pRange, 0, sizeof(OBJ_DHCP_RNG));
@ -316,7 +333,7 @@ static void expand_range_set(data19 *req, const char *pRequest) {
} }
if (please_time) { if (please_time) {
pRange.lease = STR2INT(please_time->valuestring); pRange.lease = please_time->valueint;
} }
//写入cfig //写入cfig
@ -342,17 +359,19 @@ static void expand_range_set(data19 *req, const char *pRequest) {
} }
cfig.rangeCount = (char)(m + 1); 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); 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(pRoot);
cJSON_Delete(pRspRoot);
req->bytes = (int)(fp - req->dp); req->bytes = (int)(fp - req->dp);
free((void *)pStrPro);
return ERR_SUCCESS;
} }
#pragma clang diagnostic push #pragma clang diagnostic push
@ -508,6 +527,7 @@ static void opendhcp_http_get_userinfo(http_request *request,
hw_string body; hw_string body;
hw_string keep_alive_name; hw_string keep_alive_name;
hw_string keep_alive_value; hw_string keep_alive_value;
int ret;
auto *req = (data19 *)malloc(sizeof(struct data19)); 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); hw_set_response_header(response, &content_type_name, &content_type_value);
SETSTRING(status_code, HTTP_STATUS_200); SETSTRING(status_code, HTTP_STATUS_200);
sendUserList(req, request->body->value); ret = dhcp_get_user_info(req, request->body->value);
if (ret != ERR_SUCCESS) {
if (req->dp == nullptr) { proto_response_error(response, 500, HTTP_STATUS_500, ret);
proto_response_error(response, 500, HTTP_STATUS_500, ERR_JSON_PRASE_OBJ);
return; return;
} }
SETSTRING(body, req->dp); SETSTRING(body, req->dp);
@ -558,6 +577,7 @@ static void opendhcp_http_get_alluser(http_request *request,
hw_string body; hw_string body;
hw_string keep_alive_name; hw_string keep_alive_name;
hw_string keep_alive_value; hw_string keep_alive_value;
int ret;
auto *req = (data19 *)malloc(sizeof(struct data19)); 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); hw_set_response_header(response, &content_type_name, &content_type_value);
SETSTRING(status_code, HTTP_STATUS_200); 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); SETSTRING(body, req->dp);
hw_set_body(response, &body); hw_set_body(response, &body);
hw_set_response_status_code(response, &status_code); 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 body;
hw_string keep_alive_name; hw_string keep_alive_name;
hw_string keep_alive_value; hw_string keep_alive_value;
int ret;
auto *req = (data19 *)malloc(sizeof(struct data19)); 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)); memset(req, 0, sizeof(struct data19));
SETSTRING(content_type_name, "Content-Type"); 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); hw_set_response_header(response, &content_type_name, &content_type_value);
SETSTRING(status_code, HTTP_STATUS_200); 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); SETSTRING(body, req->dp);
hw_set_body(response, &body); hw_set_body(response, &body);
hw_set_response_status_code(response, &status_code); hw_set_response_status_code(response, &status_code);

View File

@ -46,6 +46,17 @@ static void lwip_init_env() {
#endif #endif
int main(int argc, char **argv) { 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; int ret;
#ifdef OPENDHCPDDNS_ON #ifdef OPENDHCPDDNS_ON
return dual_server_main(argc, argv); return dual_server_main(argc, argv);
@ -71,8 +82,6 @@ int main(int argc, char **argv) {
pppoe_session_init(); pppoe_session_init();
#endif #endif
proto_create_new(NULL, 0);
task_manager_run(); task_manager_run();
while (!is_system_cleanup()) { while (!is_system_cleanup()) {