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-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-10-11 01:19:15 +00:00
|
|
|
|
2022-11-07 06:22:42 +00:00
|
|
|
static void sendUserList(data19 *req, const char *pRequest) {
|
2022-10-17 07:50:45 +00:00
|
|
|
char logBuff[512];
|
2022-10-11 01:19:15 +00:00
|
|
|
dhcpMap::iterator p;
|
|
|
|
|
|
|
|
printf("Input: %s\n", pRequest);
|
|
|
|
|
|
|
|
if (pRequest == nullptr || strlen(pRequest) == 0) {
|
|
|
|
sprintf(logBuff, "Requeset Json");
|
|
|
|
logDHCPMess(logBuff, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON *pRoot = cJSON_Parse(pRequest);
|
|
|
|
|
|
|
|
if (!pRoot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON *pMsgContent = cJSON_GetObjectItem(pRoot, "msgContent");
|
|
|
|
|
|
|
|
if (!pMsgContent) {
|
|
|
|
cJSON_Delete(pRoot);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON *pUserMac = cJSON_GetObjectItem(pMsgContent, "userMac");
|
|
|
|
|
|
|
|
if (!pUserMac) {
|
|
|
|
cJSON_Delete(pRoot);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON *pRspRoot = cJSON_CreateObject();
|
|
|
|
cJSON_AddNumberToObject(pRspRoot, "version", 3);
|
|
|
|
cJSON_AddNumberToObject(pRspRoot, "cryptoType", 0);
|
|
|
|
cJSON_AddNumberToObject(pRspRoot, "timeStamp", (unsigned int)time(nullptr));
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON_AddItemToObject(pRspRoot, "msgContent", pRspMsg);
|
|
|
|
|
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));
|
|
|
|
fp += sprintf(fp, "%s", cJSON_Print(pRspRoot));
|
|
|
|
|
|
|
|
cJSON_Delete(pRoot);
|
|
|
|
cJSON_Delete(pRspRoot);
|
|
|
|
|
|
|
|
req->bytes = (int)(fp - req->dp);
|
|
|
|
}
|
|
|
|
|
2022-11-07 06:22:42 +00:00
|
|
|
static void sendAllLists(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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *fp = req->dp;
|
|
|
|
char *maxData = req->dp + (req->memSize - 512);
|
|
|
|
|
|
|
|
cJSON *pRspRoot = cJSON_CreateObject();
|
|
|
|
cJSON_AddNumberToObject(pRspRoot, "version", 3);
|
|
|
|
cJSON_AddNumberToObject(pRspRoot, "cryptoType", 0);
|
|
|
|
cJSON_AddNumberToObject(pRspRoot, "timeStamp", (unsigned int)time(nullptr));
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON_AddItemToObject(pRspRoot, "msgContent", pRspMsg);
|
|
|
|
|
|
|
|
fp += sprintf(fp, "%s", cJSON_Print(pRspRoot));
|
|
|
|
|
|
|
|
cJSON_Delete(pRspRoot);
|
|
|
|
|
|
|
|
req->bytes = (int)(fp - req->dp);
|
|
|
|
}
|
2022-11-22 08:13:42 +00:00
|
|
|
#if 0
|
2022-11-21 09:13:44 +00:00
|
|
|
static void expand_range_set(data19 *req, const char *pRequest) {
|
2022-11-22 01:49:33 +00:00
|
|
|
char logBuff[512];
|
2022-11-21 09:13:44 +00:00
|
|
|
|
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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON *pRoot = cJSON_Parse(pRequest);
|
|
|
|
if (!pRoot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-22 01:49:33 +00:00
|
|
|
cJSON *pdhcp_range = cJSON_GetObjectItem(pRoot, "dhcp_range");
|
2022-11-21 09:13:44 +00:00
|
|
|
|
|
|
|
if (!pdhcp_range) {
|
|
|
|
cJSON_Delete(pRoot);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-22 01:49:33 +00:00
|
|
|
cJSON *psubnet_mask = cJSON_GetObjectItem(pRoot, "subnet_mask");
|
2022-11-21 09:13:44 +00:00
|
|
|
cJSON *pdomain_server = cJSON_GetObjectItem(pRoot, "domain_server");
|
2022-11-22 01:49:33 +00:00
|
|
|
cJSON *pgateway = cJSON_GetObjectItem(pRoot, "gateway");
|
|
|
|
cJSON *please_time = cJSON_GetObjectItem(pRoot, "lease_time");
|
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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char *fp = req->dp;
|
|
|
|
char *maxData = req->dp + (req->memSize - 512);
|
|
|
|
|
|
|
|
cJSON *pRspRoot = cJSON_CreateObject();
|
|
|
|
|
|
|
|
|
2022-11-22 01:49:33 +00:00
|
|
|
vector dhcp_rang = config_get_dhcp_server_range_set();
|
2022-11-21 09:13:44 +00:00
|
|
|
|
2022-11-22 01:49:33 +00:00
|
|
|
for(int i = 0; i < vect_size(dhcp_rang); i++) {
|
|
|
|
add_array_obj("", (POBJ_DHCP_RNG)vect_get_at(dhcp_rang, i), pRange);
|
2022-11-21 09:13:44 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 01:49:33 +00:00
|
|
|
// //写入CONFIG_ITEM
|
|
|
|
// PCONFIG_ITEM pItem, pTmp;
|
|
|
|
// HASH_ITER(hh, g_pConfigItem, pItem, pTmp) {
|
|
|
|
// if (pItem->valType == VALUE_TYPE_ARRAY_OBJ) {
|
|
|
|
// add_array_obj(pItem->pcfgKey, pItem, pRange);
|
|
|
|
// }
|
|
|
|
// }
|
2022-11-21 09:13:44 +00:00
|
|
|
|
|
|
|
//写入cfig
|
|
|
|
MYBYTE m = cfig.rangeCount;
|
|
|
|
data20 optionData {};
|
|
|
|
optionData.rangeSetInd = m;
|
|
|
|
|
2022-11-22 01:49:33 +00:00
|
|
|
data20 *opData = &optionData;
|
|
|
|
MYBYTE *dp = opData->options;
|
|
|
|
*dp = 0;
|
2022-11-21 09:13:44 +00:00
|
|
|
dp++;
|
|
|
|
|
|
|
|
addDHCPRange(pRange.rangAddr);
|
|
|
|
opData->mask = (*((MYDWORD *)pRange.subnet));
|
2022-11-22 01:49:33 +00:00
|
|
|
if (please_time) {
|
2022-11-21 09:13:44 +00:00
|
|
|
MYDWORD j;
|
2022-11-22 01:49:33 +00:00
|
|
|
j = strtol(please_time->valuestring, nullptr, 10);
|
2022-11-21 09:13:44 +00:00
|
|
|
cfig.lease = j;
|
|
|
|
}
|
|
|
|
*dp = DHCP_OPTION_END;
|
|
|
|
dp++;
|
|
|
|
opData->optionSize = (dp - opData->options);
|
|
|
|
|
2022-11-22 01:49:33 +00:00
|
|
|
MYBYTE *options = nullptr;
|
2022-11-21 09:13:44 +00:00
|
|
|
cfig.rangeSet[optionData.rangeSetInd].active = true;
|
|
|
|
|
|
|
|
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;
|
2022-11-22 01:49:33 +00:00
|
|
|
if (!cfig.dhcpRanges[m].mask) {
|
2022-11-21 09:13:44 +00:00
|
|
|
cfig.dhcpRanges[m].mask = cfig.mask;
|
2022-11-22 01:49:33 +00:00
|
|
|
}
|
|
|
|
cfig.rangeCount = (char)(m + 1);
|
2022-11-21 09:13:44 +00:00
|
|
|
|
|
|
|
cJSON_AddNumberToObject(pRspRoot, "expand_start", cfig.dhcpRanges[m].rangeStart);
|
|
|
|
cJSON_AddNumberToObject(pRspRoot, "expand_end", cfig.dhcpRanges[m].rangeEnd);
|
|
|
|
|
|
|
|
fp += sprintf(fp, "%s", cJSON_Print(pRspRoot));
|
|
|
|
|
|
|
|
cJSON_Delete(pRspRoot);
|
|
|
|
|
|
|
|
req->bytes = (int)(fp - req->dp);
|
|
|
|
}
|
2022-11-22 08:13:42 +00:00
|
|
|
#endif
|
2022-11-21 09:13:44 +00:00
|
|
|
|
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) {
|
|
|
|
printf("para is null.\n");
|
|
|
|
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) {
|
|
|
|
printf("get mac error.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sock_fd < 0) {
|
|
|
|
printf("get socket error.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ioctl(sock_fd, SIOCSARP, &req);
|
|
|
|
if (ret < 0) {
|
|
|
|
printf("ioctl error.\n");
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void opendhcp_http_info(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;
|
|
|
|
|
|
|
|
auto *req = (data19 *)malloc(sizeof(struct data19));
|
|
|
|
|
2022-11-08 10:09:54 +00:00
|
|
|
if (req == nullptr) {
|
2022-11-08 02:38:22 +00:00
|
|
|
hw_http_response_send_error(response, HTTP_STATUS_500, "memory error");
|
|
|
|
return;
|
|
|
|
}
|
2022-11-07 06:22:42 +00:00
|
|
|
|
2022-11-08 02:38:22 +00:00
|
|
|
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));
|
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-11-08 02:38:22 +00:00
|
|
|
static void opendhcp_http_get_userinfo(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;
|
|
|
|
|
|
|
|
auto *req = (data19 *)malloc(sizeof(struct data19));
|
|
|
|
|
2022-11-08 10:09:54 +00:00
|
|
|
if (req == nullptr) {
|
2022-11-08 02:38:22 +00:00
|
|
|
hw_http_response_send_error(response, HTTP_STATUS_500, "memory error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-08 03:08:43 +00:00
|
|
|
if (request->method != HW_HTTP_POST) {
|
2022-11-08 02:38:22 +00:00
|
|
|
hw_http_response_send_error(response, HTTP_STATUS_405, HTTP_STATUS_405);
|
|
|
|
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");
|
|
|
|
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);
|
|
|
|
sendUserList(req, request->body->value);
|
|
|
|
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-11-08 02:38:22 +00:00
|
|
|
static void opendhcp_http_get_alluser(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;
|
|
|
|
|
|
|
|
auto *req = (data19 *)malloc(sizeof(struct data19));
|
|
|
|
|
2022-11-08 10:09:54 +00:00
|
|
|
if (req == nullptr) {
|
2022-11-08 02:38:22 +00:00
|
|
|
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;
|
|
|
|
}
|
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");
|
|
|
|
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);
|
|
|
|
sendAllLists(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-11-22 01:49:33 +00:00
|
|
|
static void opendhcp_http_expand_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;
|
|
|
|
|
|
|
|
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");
|
|
|
|
SETSTRING(content_type_value, "text/html");
|
|
|
|
hw_set_response_header(response, &content_type_name, &content_type_value);
|
|
|
|
|
|
|
|
SETSTRING(status_code, HTTP_STATUS_200);
|
2022-11-22 08:13:42 +00:00
|
|
|
//expand_range_set(req, request->body->value);
|
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-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);
|
|
|
|
hw_http_add_route("getuser", opendhcp_http_get_userinfo, nullptr);
|
|
|
|
hw_http_add_route("allusers", opendhcp_http_get_alluser, nullptr);
|
2022-11-21 09:13:44 +00:00
|
|
|
hw_http_add_route("expansion", opendhcp_http_expand_rangeset, nullptr);
|
2022-11-10 09:48:30 +00:00
|
|
|
added = TRUE;
|
|
|
|
}
|
2022-11-21 09:13:44 +00:00
|
|
|
}
|