2022-10-11 01:19:15 +00:00
|
|
|
//
|
|
|
|
// Created by dongwenzhe on 2022/10/9.
|
|
|
|
//
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <memory.h>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include "opendhcpd.h"
|
|
|
|
#include "s2j/cJSON.h"
|
2022-11-07 06:22:42 +00:00
|
|
|
#include "haywire.h"
|
2022-11-08 02:38:22 +00:00
|
|
|
#include "misc.h"
|
2022-10-11 01:19:15 +00:00
|
|
|
#include <net/if_arp.h>
|
2022-11-21 09:13:44 +00:00
|
|
|
#include <libconfig.h>
|
2022-11-22 01:49:33 +00:00
|
|
|
#include <zlog.h>
|
2022-11-21 09:13:44 +00:00
|
|
|
#include "config.h"
|
2022-12-03 10:00:55 +00:00
|
|
|
#include "proto.h"
|
2022-12-05 10:14:07 +00:00
|
|
|
#include "user_errno.h"
|
2022-10-11 01:19:15 +00:00
|
|
|
|
2022-11-07 06:22:42 +00:00
|
|
|
extern data2 cfig;
|
|
|
|
extern bool kRunning;
|
|
|
|
extern dhcpMap dhcpCache;
|
|
|
|
extern time_t t;
|
2022-12-07 03:15:14 +00:00
|
|
|
extern int ERR_PARAM;
|
2022-10-11 01:19:15 +00:00
|
|
|
|
2022-12-06 03:08:29 +00:00
|
|
|
static int dhcp_get_user_info(data19 *req, const char *pRequest) {
|
2022-10-17 07:50:45 +00:00
|
|
|
char logBuff[512];
|
2022-12-05 10:14:07 +00:00
|
|
|
const char *pStrContent;
|
2022-10-11 01:19:15 +00:00
|
|
|
dhcpMap::iterator p;
|
|
|
|
|
2022-11-23 02:46:36 +00:00
|
|
|
dzlog_debug("Input: %s\n", pRequest);
|
2022-10-11 01:19:15 +00:00
|
|
|
|
|
|
|
if (pRequest == nullptr || strlen(pRequest) == 0) {
|
|
|
|
sprintf(logBuff, "Requeset Json");
|
|
|
|
logDHCPMess(logBuff, 1);
|
2022-12-06 03:08:29 +00:00
|
|
|
return ERR_INPUT_PARAMS;
|
2022-10-11 01:19:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 10:14:07 +00:00
|
|
|
pStrContent = proto_decode_context(pRequest, nullptr, nullptr);
|
2022-10-11 01:19:15 +00:00
|
|
|
|
2022-12-05 10:14:07 +00:00
|
|
|
if (pStrContent == nullptr) {
|
|
|
|
sprintf(logBuff, "Requeset Json error %s", pRequest);
|
|
|
|
logDHCPMess(logBuff, 1);
|
2022-12-06 03:08:29 +00:00
|
|
|
return ERR_PROTO_DECODE;
|
2022-10-11 01:19:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 10:14:07 +00:00
|
|
|
cJSON *pRoot = cJSON_Parse(pStrContent);
|
|
|
|
free((void *)pStrContent);
|
2022-10-11 01:19:15 +00:00
|
|
|
|
2022-12-05 10:14:07 +00:00
|
|
|
if (!pRoot) {
|
2022-12-06 03:08:29 +00:00
|
|
|
return ERR_JSON_PRASE_OBJ;
|
2022-10-11 01:19:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 10:14:07 +00:00
|
|
|
cJSON *pUserMac = cJSON_GetObjectItem(pRoot, "userMac");
|
2022-10-11 01:19:15 +00:00
|
|
|
|
|
|
|
if (!pUserMac) {
|
|
|
|
cJSON_Delete(pRoot);
|
2022-12-06 03:08:29 +00:00
|
|
|
return ERR_JSON_PRASE_OBJ;
|
2022-10-11 01:19:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 06:22:42 +00:00
|
|
|
req->memSize = (int)(2048 + (135 * dhcpCache.size()) + (cfig.dhcpSize * 26));
|
2022-10-11 01:19:15 +00:00
|
|
|
req->dp = (char *)calloc(1, req->memSize);
|
|
|
|
|
|
|
|
if (!req->dp) {
|
|
|
|
sprintf(logBuff, "Memory Error");
|
|
|
|
logDHCPMess(logBuff, 1);
|
2022-12-06 03:08:29 +00:00
|
|
|
return ERR_JSON_PRASE_OBJ;
|
2022-10-11 01:19:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 10:00:55 +00:00
|
|
|
// cJSON *pRspRoot = cJSON_CreateObject();
|
|
|
|
// cJSON_AddNumberToObject(pRspRoot, "version", 3);
|
|
|
|
// cJSON_AddNumberToObject(pRspRoot, "cryptoType", 0);
|
|
|
|
// cJSON_AddNumberToObject(pRspRoot, "timeStamp", (unsigned int)time(nullptr));
|
2022-10-11 01:19:15 +00:00
|
|
|
|
|
|
|
cJSON *pRspMsg = cJSON_CreateObject();
|
|
|
|
cJSON *pMsgArray = cJSON_CreateArray();
|
|
|
|
|
|
|
|
cJSON_AddItemToObject(pRspMsg, "userInfo", pMsgArray);
|
|
|
|
|
|
|
|
for (int i = 0; i < cJSON_GetArraySize(pUserMac); i++) {
|
|
|
|
char tempbuff[512];
|
|
|
|
cJSON *pItem = cJSON_GetArrayItem(pUserMac, i);
|
|
|
|
cJSON *pRspItem = cJSON_CreateObject();
|
|
|
|
|
2022-11-07 06:22:42 +00:00
|
|
|
p = dhcpCache.find(pItem->valuestring);
|
2022-10-11 01:19:15 +00:00
|
|
|
cJSON_AddStringToObject(pRspItem, "userMac", pItem->valuestring);
|
|
|
|
|
2022-11-07 06:22:42 +00:00
|
|
|
if (p != dhcpCache.end()) {
|
2022-10-11 01:19:15 +00:00
|
|
|
data7 *dhcpEntry = p->second;
|
|
|
|
|
|
|
|
cJSON_AddStringToObject(pRspItem, "ip", IP2String(tempbuff, dhcpEntry->ip));
|
|
|
|
cJSON_AddStringToObject(pRspItem, "hostname", dhcpEntry->hostname);
|
|
|
|
if (dhcpEntry->display && dhcpEntry->expiry >= t) {
|
|
|
|
tm *ttm = localtime(&dhcpEntry->expiry);
|
|
|
|
strftime(tempbuff, sizeof(tempbuff), "%d-%b-%y %X", ttm);
|
|
|
|
cJSON_AddStringToObject(pRspItem, "leaseExpiry", tempbuff);
|
|
|
|
} else {
|
|
|
|
cJSON_AddStringToObject(pRspItem, "leaseExpiry", "Expiry");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cJSON_AddStringToObject(pRspItem, "ip", "");
|
|
|
|
cJSON_AddStringToObject(pRspItem, "hostname", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON_AddItemToArray(pMsgArray, pRspItem);
|
|
|
|
}
|
|
|
|
|
2022-12-05 10:14:07 +00:00
|
|
|
cJSON_AddNumberToObject(pRspMsg, "status", ERR_SUCCESS);
|
|
|
|
cJSON_AddStringToObject(pRspMsg, "message", getErrorEnumDesc(ERR_SUCCESS));
|
|
|
|
|
2022-12-03 10:00:55 +00:00
|
|
|
const char *pStrPro = proto_create_new(pRspMsg, 200);
|
|
|
|
|
|
|
|
//cJSON_AddItemToObject(pRspRoot, "msgContent", pRspMsg);
|
2022-10-11 01:19:15 +00:00
|
|
|
|
2022-10-17 07:50:45 +00:00
|
|
|
char *fp = req->dp;
|
2022-10-17 07:45:08 +00:00
|
|
|
//char *maxData = req->dp + (req->memSize - 512);
|
2022-10-11 01:19:15 +00:00
|
|
|
|
|
|
|
//fp += sprintf(fp, send200, strlen(rspBuf));
|
2022-12-03 10:00:55 +00:00
|
|
|
fp += sprintf(fp, "%s", pStrPro);
|
2022-12-03 10:07:12 +00:00
|
|
|
req->bytes = (int)(fp - req->dp);
|
2022-10-11 01:19:15 +00:00
|
|
|
|
|
|
|
cJSON_Delete(pRoot);
|
2022-12-03 10:02:47 +00:00
|
|
|
free((void *)pStrPro);
|
2022-12-06 03:08:29 +00:00
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
2022-10-11 01:19:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 03:08:29 +00:00
|
|
|
static int dhcp_get_all_user(data19 *req) {
|
2022-10-11 01:19:15 +00:00
|
|
|
char logBuff[512];
|
2022-10-17 07:45:08 +00:00
|
|
|
data7 *dhcpEntry;
|
2022-10-11 01:19:15 +00:00
|
|
|
dhcpMap::iterator p;
|
|
|
|
|
2022-11-07 06:22:42 +00:00
|
|
|
req->memSize = (int)(2048 + (135 * dhcpCache.size()) + (cfig.dhcpSize * 26));
|
2022-10-11 01:19:15 +00:00
|
|
|
req->dp = (char *)calloc(1, req->memSize);
|
|
|
|
|
|
|
|
if (!req->dp) {
|
|
|
|
sprintf(logBuff, "Memory Error");
|
|
|
|
logDHCPMess(logBuff, 1);
|
2022-12-06 03:08:29 +00:00
|
|
|
return ERR_MALLOC_MEMORY;
|
2022-10-11 01:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *fp = req->dp;
|
|
|
|
char *maxData = req->dp + (req->memSize - 512);
|
|
|
|
|
|
|
|
cJSON *pRspMsg = cJSON_CreateObject();
|
|
|
|
cJSON *pMsgArray = cJSON_CreateArray();
|
|
|
|
|
|
|
|
cJSON_AddItemToObject(pRspMsg, "users", pMsgArray);
|
|
|
|
|
2022-11-07 06:22:42 +00:00
|
|
|
for (p = dhcpCache.begin(); kRunning && p != dhcpCache.end() && fp < maxData; p++) {
|
2022-10-11 01:19:15 +00:00
|
|
|
//cJSON *pRspItem = cJSON_CreateObject();
|
|
|
|
if ((dhcpEntry = p->second)) {
|
|
|
|
cJSON_AddStringToObject(pMsgArray, "", dhcpEntry->mapname);
|
|
|
|
}
|
|
|
|
|
|
|
|
//cJSON_AddItemToArray(pMsgArray, pRspItem);
|
|
|
|
}
|
|
|
|
|
2022-12-03 10:07:12 +00:00
|
|
|
//cJSON_AddItemToObject(pRspRoot, "msgContent", pRspMsg);
|
2022-12-05 10:14:07 +00:00
|
|
|
cJSON_AddNumberToObject(pRspMsg, "status", ERR_SUCCESS);
|
|
|
|
cJSON_AddStringToObject(pRspMsg, "message", getErrorEnumDesc(ERR_SUCCESS));
|
2022-10-11 01:19:15 +00:00
|
|
|
|
2022-12-03 10:07:12 +00:00
|
|
|
const char *pStrPro = proto_create_new(pRspMsg, 200);
|
2022-10-11 01:19:15 +00:00
|
|
|
|
2022-12-03 10:07:12 +00:00
|
|
|
fp += sprintf(fp, "%s", pStrPro);
|
2022-10-11 01:19:15 +00:00
|
|
|
req->bytes = (int)(fp - req->dp);
|
2022-12-03 10:07:12 +00:00
|
|
|
|
|
|
|
free((void *)pStrPro);
|
2022-12-06 03:08:29 +00:00
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
2022-10-11 01:19:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
#define VALUE_TO_DHCP_TLV(buf, val, tag) \
|
|
|
|
do { \
|
|
|
|
int valSize; \
|
|
|
|
int numbytes = myTokenize((buf), (val), "/,.", true); \
|
|
|
|
if (numbytes <= 255) { \
|
|
|
|
char *ptr = (buf); \
|
|
|
|
valSize = 0; \
|
|
|
|
for (; *ptr; ptr = myGetToken(ptr, 1)) { \
|
|
|
|
if (isInt(ptr)) { \
|
|
|
|
hoption[valSize] = STR2INT(ptr); \
|
|
|
|
valSize++; \
|
|
|
|
} else { \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
memcpy((val), hoption, valSize); \
|
|
|
|
if (buffSize > valSize + 2) { \
|
|
|
|
*dp = (tag); \
|
|
|
|
dp++; \
|
|
|
|
*dp = valSize; \
|
|
|
|
dp++; \
|
|
|
|
memcpy(dp, (val), valSize); \
|
|
|
|
dp += valSize; \
|
|
|
|
buffSize -= (valSize + 2); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static void add_options(OBJ_DHCP_RNG pRange, data20 *optionData) {
|
|
|
|
char buff[1024];
|
|
|
|
MYBYTE hoption[256];
|
2022-11-23 06:56:54 +00:00
|
|
|
MYBYTE *dp = optionData->options;
|
|
|
|
MYWORD buffSize = sizeof(dhcp_packet) - sizeof(dhcp_header);
|
2022-11-24 07:41:55 +00:00
|
|
|
char value[256] = {0};
|
|
|
|
|
|
|
|
*dp = 0;
|
2022-11-23 06:56:54 +00:00
|
|
|
dp++;
|
|
|
|
|
|
|
|
//dhcp_range
|
|
|
|
addDHCPRange(pRange.rangAddr);
|
2022-12-07 03:15:14 +00:00
|
|
|
if(ERR_PARAM != ERR_SUCCESS){
|
|
|
|
return;
|
|
|
|
}
|
2022-11-23 06:56:54 +00:00
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
if (strlen(pRange.subnet) != 0) {
|
|
|
|
strcpy(value, pRange.subnet);
|
|
|
|
VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_NETMASK);
|
|
|
|
optionData->mask = (*((MYDWORD *)value));
|
|
|
|
}
|
2022-11-23 06:56:54 +00:00
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
if (strlen(pRange.dnsSvr) != 0) {
|
|
|
|
strcpy(value, pRange.dnsSvr);
|
|
|
|
VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_DNS);
|
|
|
|
}
|
2022-11-23 06:56:54 +00:00
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
if (strlen(pRange.gateway) != 0) {
|
|
|
|
strcpy(value, pRange.gateway);
|
|
|
|
VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_ROUTER);
|
2022-11-23 06:56:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//lease
|
|
|
|
if (pRange.lease != 0) {
|
|
|
|
MYDWORD j;
|
|
|
|
j = pRange.lease;
|
|
|
|
if (buffSize > 6) {
|
|
|
|
*dp = DHCP_OPTION_IPADDRLEASE;
|
|
|
|
dp++;
|
|
|
|
*dp = 4;
|
|
|
|
dp++;
|
|
|
|
dp += pUInt(dp, j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*dp = DHCP_OPTION_END;
|
|
|
|
dp++;
|
|
|
|
optionData->optionSize = (dp - optionData->options);
|
|
|
|
}
|
|
|
|
|
2022-12-06 03:08:29 +00:00
|
|
|
static int add_dhcpd_rangeset(data19 *req, const char *pRequest) {
|
2022-11-24 07:41:55 +00:00
|
|
|
char logBuff[512];
|
2022-12-06 03:08:29 +00:00
|
|
|
const char *pStrContent;
|
2022-11-23 06:56:54 +00:00
|
|
|
OBJ_DHCP_RNG pRange;
|
2022-11-24 07:41:55 +00:00
|
|
|
char *fp;
|
|
|
|
cJSON *pRspRoot;
|
|
|
|
cJSON *pExpandArray;
|
2022-11-21 09:13:44 +00:00
|
|
|
|
2022-12-07 03:15:14 +00:00
|
|
|
ERR_PARAM = ERR_SUCCESS;
|
2022-11-22 01:49:33 +00:00
|
|
|
dzlog_debug("Input: %s\n", pRequest);
|
2022-11-21 09:13:44 +00:00
|
|
|
|
|
|
|
if (pRequest == nullptr || strlen(pRequest) == 0) {
|
|
|
|
sprintf(logBuff, "Requeset Json");
|
|
|
|
logDHCPMess(logBuff, 1);
|
2022-12-06 03:08:29 +00:00
|
|
|
return ERR_INPUT_PARAMS;
|
2022-11-21 09:13:44 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 03:08:29 +00:00
|
|
|
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);
|
|
|
|
|
2022-11-21 09:13:44 +00:00
|
|
|
if (!pRoot) {
|
2022-12-06 03:08:29 +00:00
|
|
|
return ERR_JSON_PRASE_OBJ;
|
2022-11-21 09:13:44 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 03:08:29 +00:00
|
|
|
cJSON *prange_set = cJSON_GetObjectItem(pRoot, "rangeSet");
|
2022-11-21 09:13:44 +00:00
|
|
|
|
|
|
|
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);
|
2022-11-24 07:41:55 +00:00
|
|
|
cJSON_Delete(pRoot);
|
2022-12-06 03:08:29 +00:00
|
|
|
return ERR_MALLOC_MEMORY;
|
2022-11-21 09:13:44 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 08:46:52 +00:00
|
|
|
fp = req->dp;
|
|
|
|
pRspRoot = cJSON_CreateObject();
|
2022-11-24 07:41:55 +00:00
|
|
|
pExpandArray = cJSON_CreateArray();
|
2022-12-06 03:08:29 +00:00
|
|
|
cJSON_AddItemToObject(pRspRoot, "rangeSet", pExpandArray);
|
2022-11-21 09:13:44 +00:00
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
for (int i = 0; i < cJSON_GetArraySize(prange_set); i++) {
|
2022-12-03 08:46:52 +00:00
|
|
|
cJSON *pItem = cJSON_GetArrayItem(prange_set, i);
|
2022-12-06 03:08:29 +00:00
|
|
|
cJSON *pdhcp_range = cJSON_GetObjectItem(pItem, "dhcpRange");
|
2022-12-03 08:46:52 +00:00
|
|
|
cJSON *pEx_range = cJSON_CreateObject();
|
2022-11-21 09:13:44 +00:00
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
if (!pdhcp_range) {
|
2022-12-06 03:08:29 +00:00
|
|
|
continue;
|
2022-11-24 07:41:55 +00:00
|
|
|
}
|
2022-11-21 09:13:44 +00:00
|
|
|
|
2022-12-06 03:08:29 +00:00
|
|
|
cJSON_AddStringToObject(pEx_range, "dhcpRange", pdhcp_range->valuestring);
|
|
|
|
|
|
|
|
cJSON *psubnet_mask = cJSON_GetObjectItem(pItem, "netmask");
|
|
|
|
cJSON *pdomain_server = cJSON_GetObjectItem(pItem, "domainServer");
|
2022-11-24 07:41:55 +00:00
|
|
|
cJSON *pgateway = cJSON_GetObjectItem(pItem, "gateway");
|
2022-12-06 03:08:29 +00:00
|
|
|
cJSON *please_time = cJSON_GetObjectItem(pItem, "leaseTime");
|
2022-11-21 09:13:44 +00:00
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
memset(&pRange, 0, sizeof(OBJ_DHCP_RNG));
|
2022-11-23 06:56:54 +00:00
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
strcpy(pRange.rangAddr, pdhcp_range->valuestring);
|
|
|
|
if (psubnet_mask) {
|
|
|
|
strcpy(pRange.subnet, psubnet_mask->valuestring);
|
|
|
|
}
|
2022-11-21 09:13:44 +00:00
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
if (pdomain_server) {
|
|
|
|
strcpy(pRange.dnsSvr, pdomain_server->valuestring);
|
|
|
|
}
|
2022-11-21 09:13:44 +00:00
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
if (pgateway) {
|
|
|
|
strcpy(pRange.gateway, pgateway->valuestring);
|
|
|
|
}
|
2022-11-21 09:13:44 +00:00
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
if (please_time) {
|
2022-12-06 03:08:29 +00:00
|
|
|
pRange.lease = please_time->valueint;
|
2022-11-24 07:41:55 +00:00
|
|
|
}
|
2022-11-23 06:56:54 +00:00
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
//写入cfig
|
|
|
|
MYBYTE m = cfig.rangeCount;
|
|
|
|
data20 optionData {};
|
|
|
|
optionData.rangeSetInd = m;
|
2022-11-21 09:13:44 +00:00
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
MYBYTE *options = nullptr;
|
|
|
|
cfig.rangeSet[optionData.rangeSetInd].active = true;
|
2022-11-21 09:13:44 +00:00
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
add_options(pRange, &optionData);
|
2022-12-07 03:15:14 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-11-24 07:41:55 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-12-07 03:15:14 +00:00
|
|
|
cJSON_AddNumberToObject(pEx_range, "status", ERR_SUCCESS);
|
|
|
|
cJSON_AddStringToObject(pEx_range, "message", getErrorEnumDesc(ERR_SUCCESS));
|
2022-11-24 07:41:55 +00:00
|
|
|
cJSON_AddItemToArray(pExpandArray, pEx_range);
|
|
|
|
}
|
2022-11-21 09:13:44 +00:00
|
|
|
|
2022-12-06 03:08:29 +00:00
|
|
|
const char *pStrPro = proto_create_new(pRspRoot, 200);
|
|
|
|
|
|
|
|
fp += sprintf(fp, "%s", pStrPro);
|
2022-11-21 09:13:44 +00:00
|
|
|
|
2022-11-24 07:41:55 +00:00
|
|
|
cJSON_Delete(pRoot);
|
2022-11-21 09:13:44 +00:00
|
|
|
|
|
|
|
req->bytes = (int)(fp - req->dp);
|
2022-12-06 03:08:29 +00:00
|
|
|
free((void *)pStrPro);
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
2022-11-21 09:13:44 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 03:15:14 +00:00
|
|
|
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;
|
2022-12-08 03:49:14 +00:00
|
|
|
memset(&cfig.dhcpRanges[m], 0, sizeof(struct data19));
|
2022-12-07 03:15:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-12-08 03:49:14 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-17 07:50:45 +00:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma ide diagnostic ignored "cert-err34-c"
|
2022-11-08 02:38:22 +00:00
|
|
|
int getHwAddr(char *buff, char *mac) {
|
2022-10-11 01:19:15 +00:00
|
|
|
if (buff == nullptr || mac == nullptr) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-10-17 07:45:08 +00:00
|
|
|
int i;
|
2022-10-11 01:19:15 +00:00
|
|
|
unsigned int p[6];
|
|
|
|
|
|
|
|
if (sscanf(mac, "%x:%x:%x:%x:%x:%x", &p[0], &p[1], &p[2], &p[3], &p[4], &p[5]) < 6) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 6; i++) {
|
|
|
|
buff[i] = (char)p[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2022-10-17 07:50:45 +00:00
|
|
|
#pragma clang diagnostic pop
|
2022-10-11 01:19:15 +00:00
|
|
|
|
|
|
|
int arpSet(const char *ifname, char *ipStr, char *mac) {
|
|
|
|
if (ifname == nullptr || ipStr == nullptr || mac == nullptr) {
|
2022-11-23 02:46:36 +00:00
|
|
|
dzlog_error("para is null.\n");
|
2022-10-11 01:19:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct arpreq req {};
|
|
|
|
struct sockaddr_in *sin;
|
2022-10-17 07:45:08 +00:00
|
|
|
int ret;
|
|
|
|
int sock_fd;
|
2022-10-11 01:19:15 +00:00
|
|
|
|
|
|
|
memset(&req, 0, sizeof(struct arpreq));
|
|
|
|
sin = (struct sockaddr_in *)&req.arp_pa;
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_addr.s_addr = inet_addr(ipStr);
|
|
|
|
//arp_dev长度为[16],注意越界
|
|
|
|
strncpy(req.arp_dev, ifname, 15);
|
|
|
|
req.arp_flags = ATF_PERM | ATF_COM;
|
|
|
|
|
|
|
|
if (getHwAddr((char *)req.arp_ha.sa_data, mac) < 0) {
|
2022-11-23 02:46:36 +00:00
|
|
|
dzlog_error("get mac error.\n");
|
2022-10-11 01:19:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sock_fd < 0) {
|
2022-11-23 02:46:36 +00:00
|
|
|
dzlog_error("get socket error.\n");
|
2022-10-11 01:19:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ioctl(sock_fd, SIOCSARP, &req);
|
|
|
|
if (ret < 0) {
|
2022-11-23 02:46:36 +00:00
|
|
|
dzlog_error("ioctl error.\n");
|
2022-10-11 01:19:15 +00:00
|
|
|
close(sock_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sock_fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-17 07:45:08 +00:00
|
|
|
sockaddr_in get_cliAddr(char *nicif, char *tempbuff, data9 *req) {
|
2022-10-11 01:19:15 +00:00
|
|
|
arpSet(nicif, IP2String(tempbuff, req->dhcpp.header.bp_yiaddr), req->chaddr);
|
|
|
|
|
|
|
|
sockaddr_in cliAddr {};
|
|
|
|
memcpy(&cliAddr, &req->remote, sizeof(sockaddr_in));
|
|
|
|
cliAddr.sin_addr.s_addr = inet_addr(IP2String(tempbuff, req->dhcpp.header.bp_yiaddr));
|
|
|
|
return cliAddr;
|
|
|
|
}
|
2022-11-07 06:22:42 +00:00
|
|
|
|
2022-11-08 02:38:22 +00:00
|
|
|
static void response_complete(void *user_data) {
|
|
|
|
auto *req = (data19 *)user_data;
|
|
|
|
if (req) {
|
2022-11-07 06:22:42 +00:00
|
|
|
|
2022-11-08 02:38:22 +00:00
|
|
|
if (req->dp) {
|
|
|
|
free(req->dp);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 06:42:41 +00:00
|
|
|
static void proto_response_error(hw_http_response *response, int httpCode, const char *httpCodeStr, int errCode) {
|
2022-12-05 10:14:07 +00:00
|
|
|
cJSON *pRspMsg = cJSON_CreateObject();
|
|
|
|
|
|
|
|
cJSON_AddNumberToObject(pRspMsg, "status", errCode);
|
|
|
|
cJSON_AddStringToObject(pRspMsg, "message", getErrorEnumDesc(errCode));
|
|
|
|
|
|
|
|
const char *pStrPro = proto_create_new(pRspMsg, httpCode);
|
|
|
|
|
|
|
|
hw_http_response_send_error(response, httpCodeStr, pStrPro);
|
|
|
|
free((void *)pStrPro);
|
|
|
|
}
|
|
|
|
|
2022-12-06 06:42:41 +00:00
|
|
|
static void opendhcp_http_info(http_request *request, hw_http_response *response, void *UNUSED(user_data)) {
|
2022-11-08 02:38:22 +00:00
|
|
|
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));
|
|
|
|
|
2022-11-08 10:09:54 +00:00
|
|
|
if (req == nullptr) {
|
2022-12-05 10:14:07 +00:00
|
|
|
proto_response_error(response, 500, HTTP_STATUS_500, ERR_MALLOC_MEMORY);
|
2022-11-08 02:38:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-11-07 06:22:42 +00:00
|
|
|
|
2022-11-08 02:38:22 +00:00
|
|
|
if (request->method != HW_HTTP_GET) {
|
2022-12-05 10:14:07 +00:00
|
|
|
proto_response_error(response, 405, HTTP_STATUS_405, ERR_HTTP_UNSUP_METHOD);
|
2022-11-08 02:38:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(req, 0, sizeof(struct data19));
|
2022-11-07 06:22:42 +00:00
|
|
|
|
|
|
|
SETSTRING(content_type_name, "Content-Type");
|
|
|
|
SETSTRING(content_type_value, "text/html");
|
|
|
|
hw_set_response_header(response, &content_type_name, &content_type_value);
|
|
|
|
|
2022-11-08 02:38:22 +00:00
|
|
|
SETSTRING(status_code, HTTP_STATUS_200);
|
|
|
|
prepareUserHtmlRespStatus(req);
|
|
|
|
SETSTRING(body, req->dp);
|
2022-11-07 06:22:42 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-11-08 02:38:22 +00:00
|
|
|
hw_http_response_send(response, req, response_complete);
|
2022-11-07 06:22:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 06:42:41 +00:00
|
|
|
static void opendhcp_http_get_userinfo(http_request *request, hw_http_response *response, void *UNUSED(user_data)) {
|
2022-11-08 02:38:22 +00:00
|
|
|
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;
|
2022-12-06 03:08:29 +00:00
|
|
|
int ret;
|
2022-11-08 02:38:22 +00:00
|
|
|
|
|
|
|
auto *req = (data19 *)malloc(sizeof(struct data19));
|
|
|
|
|
2022-11-08 10:09:54 +00:00
|
|
|
if (req == nullptr) {
|
2022-12-05 10:14:07 +00:00
|
|
|
proto_response_error(response, 500, HTTP_STATUS_500, ERR_MALLOC_MEMORY);
|
2022-11-08 02:38:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-08 03:08:43 +00:00
|
|
|
if (request->method != HW_HTTP_POST) {
|
2022-12-05 10:14:07 +00:00
|
|
|
proto_response_error(response, 405, HTTP_STATUS_405, ERR_HTTP_UNSUP_METHOD);
|
2022-11-08 02:38:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-11-07 06:22:42 +00:00
|
|
|
|
2022-11-08 02:38:22 +00:00
|
|
|
memset(req, 0, sizeof(struct data19));
|
2022-11-07 06:22:42 +00:00
|
|
|
|
|
|
|
SETSTRING(content_type_name, "Content-Type");
|
2022-12-03 10:00:55 +00:00
|
|
|
SETSTRING(content_type_value, "application/json");
|
2022-11-07 06:22:42 +00:00
|
|
|
hw_set_response_header(response, &content_type_name, &content_type_value);
|
|
|
|
|
2022-11-08 02:38:22 +00:00
|
|
|
SETSTRING(status_code, HTTP_STATUS_200);
|
2022-12-06 03:08:29 +00:00
|
|
|
ret = dhcp_get_user_info(req, request->body->value);
|
|
|
|
if (ret != ERR_SUCCESS) {
|
|
|
|
proto_response_error(response, 500, HTTP_STATUS_500, ret);
|
2022-12-05 10:14:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-11-08 02:38:22 +00:00
|
|
|
SETSTRING(body, req->dp);
|
2022-11-07 06:22:42 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-11-08 02:38:22 +00:00
|
|
|
hw_http_response_send(response, req, response_complete);
|
2022-11-07 06:22:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 06:42:41 +00:00
|
|
|
static void opendhcp_http_get_alluser(http_request *request, hw_http_response *response, void *UNUSED(user_data)) {
|
2022-11-08 02:38:22 +00:00
|
|
|
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;
|
2022-12-06 03:08:29 +00:00
|
|
|
int ret;
|
2022-11-08 02:38:22 +00:00
|
|
|
|
|
|
|
auto *req = (data19 *)malloc(sizeof(struct data19));
|
|
|
|
|
2022-11-08 10:09:54 +00:00
|
|
|
if (req == nullptr) {
|
2022-12-05 10:14:07 +00:00
|
|
|
proto_response_error(response, 500, HTTP_STATUS_500, ERR_MALLOC_MEMORY);
|
2022-11-08 02:38:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request->method != HW_HTTP_GET) {
|
2022-12-05 10:14:07 +00:00
|
|
|
proto_response_error(response, 405, HTTP_STATUS_405, ERR_HTTP_UNSUP_METHOD);
|
2022-11-08 02:38:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-11-07 06:22:42 +00:00
|
|
|
|
2022-11-08 02:38:22 +00:00
|
|
|
memset(req, 0, sizeof(struct data19));
|
2022-11-07 06:22:42 +00:00
|
|
|
|
|
|
|
SETSTRING(content_type_name, "Content-Type");
|
2022-12-05 10:14:07 +00:00
|
|
|
SETSTRING(content_type_value, "application/json");
|
2022-11-07 06:22:42 +00:00
|
|
|
hw_set_response_header(response, &content_type_name, &content_type_value);
|
|
|
|
|
2022-11-08 02:38:22 +00:00
|
|
|
SETSTRING(status_code, HTTP_STATUS_200);
|
2022-12-06 03:08:29 +00:00
|
|
|
ret = dhcp_get_all_user(req);
|
|
|
|
if (ret != ERR_SUCCESS) {
|
|
|
|
proto_response_error(response, 500, HTTP_STATUS_500, ret);
|
|
|
|
return;
|
|
|
|
}
|
2022-11-08 02:38:22 +00:00
|
|
|
SETSTRING(body, req->dp);
|
2022-11-07 06:22:42 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-11-08 02:38:22 +00:00
|
|
|
hw_http_response_send(response, req, response_complete);
|
2022-11-07 06:22:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 03:15:14 +00:00
|
|
|
static void opendhcp_http_add_rangeset(http_request *request, hw_http_response *response, void *UNUSED(user_data)) {
|
2022-11-21 09:13:44 +00:00
|
|
|
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;
|
2022-12-06 03:08:29 +00:00
|
|
|
int ret;
|
2022-11-21 09:13:44 +00:00
|
|
|
|
|
|
|
auto *req = (data19 *)malloc(sizeof(struct data19));
|
|
|
|
|
2022-11-22 01:49:33 +00:00
|
|
|
if (req == nullptr) {
|
2022-11-21 09:13:44 +00:00
|
|
|
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");
|
2022-12-06 03:08:29 +00:00
|
|
|
SETSTRING(content_type_value, "application/json");
|
2022-11-21 09:13:44 +00:00
|
|
|
hw_set_response_header(response, &content_type_name, &content_type_value);
|
|
|
|
|
|
|
|
SETSTRING(status_code, HTTP_STATUS_200);
|
2022-12-06 03:08:29 +00:00
|
|
|
ret = add_dhcpd_rangeset(req, request->body->value);
|
|
|
|
if (ret != ERR_SUCCESS) {
|
|
|
|
proto_response_error(response, 500, HTTP_STATUS_500, ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-21 09:13:44 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-12-07 03:15:14 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-12-08 03:49:14 +00:00
|
|
|
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);
|
|
|
|
}
|
2022-11-24 07:01:06 +00:00
|
|
|
/**
|
|
|
|
* 添加配置文件监听接口
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
int opendhcp_add_listener() {
|
|
|
|
int i;
|
|
|
|
vector listen_ip = config_get_dhcp_listen_on();
|
|
|
|
|
|
|
|
for (i = 0; listen_ip && i < vect_size(listen_ip); i++) {
|
|
|
|
const char *pIp = (const char *)vect_get_at(listen_ip, i);
|
|
|
|
if (pIp && strlen(pIp) > 0) {
|
|
|
|
MYDWORD addr = inet_addr(pIp);
|
|
|
|
addServer(cfig.specifiedServers, MAX_SERVERS, addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 增加 DHCP Server HTTP服务接口
|
|
|
|
*/
|
2022-11-07 06:22:42 +00:00
|
|
|
void opendhcp_init_http_server() {
|
2022-11-10 09:48:30 +00:00
|
|
|
static int added = FALSE;
|
|
|
|
|
|
|
|
if (!added) {
|
|
|
|
hw_http_add_route("/", opendhcp_http_info, nullptr);
|
2022-12-07 03:15:14 +00:00
|
|
|
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);
|
2022-12-08 03:49:14 +00:00
|
|
|
hw_http_add_route("dhcp/query/rangeset", opendhcp_http_query_rangeset, nullptr);
|
2022-11-10 09:48:30 +00:00
|
|
|
added = TRUE;
|
|
|
|
}
|
2022-11-21 09:13:44 +00:00
|
|
|
}
|