OCT 1.增加查询地址池接口

This commit is contained in:
dongwenze 2022-12-08 11:49:14 +08:00
parent 6ec5f520a2
commit 4d848f2724
1 changed files with 154 additions and 3 deletions

View File

@ -458,9 +458,7 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest){
if(!deleted_flag){ if(!deleted_flag){
if((st_addr == cfig.dhcpRanges[m].rangeStart) && (en_addr == cfig.dhcpRanges[m].rangeEnd)){ if((st_addr == cfig.dhcpRanges[m].rangeStart) && (en_addr == cfig.dhcpRanges[m].rangeEnd)){
deleted_flag = true; deleted_flag = true;
if(m == cfig.rangeCount-1){ memset(&cfig.dhcpRanges[m], 0, sizeof(struct data19));
memset(&cfig.dhcpRanges[m], 0, sizeof(struct data19));
}
continue; continue;
}else if(st_addr == cfig.dhcpRanges[m].rangeStart||en_addr == cfig.dhcpRanges[m].rangeEnd){ }else if(st_addr == cfig.dhcpRanges[m].rangeStart||en_addr == cfig.dhcpRanges[m].rangeEnd){
deleted_flag = false; deleted_flag = false;
@ -501,6 +499,111 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest){
return ERR_SUCCESS; return ERR_SUCCESS;
} }
static void revert(unsigned int* num) {
unsigned int v = *((unsigned int *)num) ;
v = ((v & 0x000000FF) << 24) | ((v & 0x0000FF00) << 8) |
((v & 0x00FF0000) >> 8) | ((v & 0xFF000000) >> 24) ;
*((unsigned int *)num) = v ;
}
static int query_dhcpd_rangeset(data19 *req){
char logBuff[512];
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 ERR_MALLOC_MEMORY;
}
char *fp = req->dp;
char *maxData = req->dp + (req->memSize - 512);
cJSON *pRspMsg = cJSON_CreateObject();
cJSON *pMsgArray = cJSON_CreateArray();
cJSON_AddItemToObject(pRspMsg, "rangeSet", pMsgArray);
for (char rangeInd = 0; rangeInd < cfig.rangeCount; rangeInd++){
char addrStart[64];
char addrEnd[64];
char rangeSet[128];
char rangeMask[128];
char domainServer[128];
char gateway[128];
cJSON *pRangeItem = cJSON_CreateObject();
unsigned int lease;
memset(domainServer, 0 , 128);
IP2String(addrStart, ntohl(cfig.dhcpRanges[rangeInd].rangeStart));
IP2String(addrEnd, ntohl(cfig.dhcpRanges[rangeInd].rangeEnd));
IP2String(rangeMask, cfig.dhcpRanges[rangeInd].mask);
sprintf(rangeSet, "%s-%s", addrStart, addrEnd);
cJSON_AddStringToObject(pRangeItem, "dhcpRange", rangeSet);
cJSON_AddStringToObject(pRangeItem, "netmask", rangeMask);
MYBYTE *opPointer = cfig.dhcpRanges[rangeInd].options;
data3 op {};
if (opPointer) {
opPointer++;
while (*opPointer && *opPointer != DHCP_OPTION_END) {
unsigned int tmpVal = 0;
unsigned char dnsSize;
unsigned char offset = 0;
char dns_op[64];
op.opt_code = *opPointer;
opPointer++;
op.size = *opPointer;
opPointer++;
memcpy(op.value, opPointer, op.size);
if(op.opt_code == DHCP_OPTION_DNS){
dnsSize = op.size;
do {
tmpVal = fIP(op.value + offset);
revert(&tmpVal);
IP2String(dns_op, ntohl(tmpVal));
sprintf(domainServer, "%s%s", domainServer, dns_op);
if(dnsSize != 4){
sprintf(domainServer, "%s,", domainServer);
}
dnsSize -= 4;
offset = op.size - dnsSize;
}while (dnsSize != 0);
cJSON_AddStringToObject(pRangeItem, "domainServer", domainServer);
}else if(op.opt_code == DHCP_OPTION_ROUTER){
tmpVal = fIP(op.value);
revert(&tmpVal);
IP2String(gateway, ntohl(tmpVal));
cJSON_AddStringToObject(pRangeItem, "gateway", gateway);
}else if(op.opt_code == DHCP_OPTION_IPADDRLEASE){
lease = fUInt(op.value);
cJSON_AddNumberToObject(pRangeItem, "lease", lease);
}
opPointer += op.size;
}
}
cJSON_AddItemToArray(pMsgArray, pRangeItem);
}
cJSON_AddNumberToObject(pRspMsg, "status", ERR_SUCCESS);
cJSON_AddStringToObject(pRspMsg, "message", getErrorEnumDesc(ERR_SUCCESS));
const char *pStrPro = proto_create_new(pRspMsg, 200);
fp += sprintf(fp, "%s", pStrPro);
req->bytes = (int)(fp - req->dp);
free((void *)pStrPro);
return ERR_SUCCESS;
}
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma ide diagnostic ignored "cert-err34-c" #pragma ide diagnostic ignored "cert-err34-c"
int getHwAddr(char *buff, char *mac) { int getHwAddr(char *buff, char *mac) {
@ -832,6 +935,53 @@ static void opendhcp_http_delete_rangeset(http_request *request, hw_http_respons
hw_http_response_send(response, req, response_complete); hw_http_response_send(response, req, response_complete);
} }
static void opendhcp_http_query_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_GET) {
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 = query_dhcpd_rangeset(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);
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 * @return
@ -863,6 +1013,7 @@ void opendhcp_init_http_server() {
hw_http_add_route("dhcp/info/allusers", opendhcp_http_get_alluser, 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/config/rangeset", opendhcp_http_add_rangeset, nullptr);
hw_http_add_route("dhcp/delete/rangeset", opendhcp_http_delete_rangeset, nullptr); hw_http_add_route("dhcp/delete/rangeset", opendhcp_http_delete_rangeset, nullptr);
hw_http_add_route("dhcp/query/rangeset", opendhcp_http_query_rangeset, nullptr);
added = TRUE; added = TRUE;
} }
} }