OCT 新建query文件保存修改的代码
This commit is contained in:
parent
349e6d24d1
commit
da68990550
|
@ -8,6 +8,6 @@ FILE(GLOB DHCPD_HEADS ./*.h)
|
|||
|
||||
ADD_DEFINITIONS(-Wno-format-overflow -std=c++11)
|
||||
#ADD_EXECUTABLE(${PROJECT_TARGET} opendhcpd.cpp ${DHCPD_HEADS})
|
||||
ADD_LIBRARY(${DHCPD_PROJECT_TARGET} opendhcpd.cpp ${DHCPD_HEADS})
|
||||
ADD_LIBRARY(${DHCPD_PROJECT_TARGET} opendhcpd.cpp ${DHCPD_HEADS} query.cpp)
|
||||
|
||||
#TARGET_LINK_LIBRARIES(${PROJECT_TARGET} -lpthread)
|
|
@ -40,7 +40,7 @@ using namespace std;
|
|||
#include <syslog.h>
|
||||
#include "opendhcpd.h"
|
||||
#include "dhcpd.h"
|
||||
#include "s2j/cJSON.h"
|
||||
//#include "s2j/cJSON.h"
|
||||
|
||||
#define STR2INT(val) ((int)strtol((val), nullptr, 10))
|
||||
|
||||
|
@ -218,9 +218,6 @@ const data4 opData[] = {
|
|||
{"NextServer", 254, 3, true},
|
||||
};
|
||||
|
||||
static void sendUserList(data19 *req, const char *pRequest);
|
||||
static void sendAllLists(data19 *req);
|
||||
|
||||
int dhcpd_main(int argc, char **argv) {
|
||||
char logBuff[256] = "";
|
||||
|
||||
|
@ -617,25 +614,6 @@ MYBYTE pIP(void *raw, MYDWORD data) {
|
|||
return sizeof(MYDWORD);
|
||||
}
|
||||
|
||||
const char *getRequestMethod(const char *buffer) {
|
||||
int i = 0;
|
||||
char httpMethod[64] = {0};
|
||||
|
||||
for (; i < 64 && i < strlen(buffer); i++) {
|
||||
if (buffer[i] >= 'A' && buffer[i] <= 'Z') {
|
||||
if (buffer[i] == ' ') {
|
||||
break;
|
||||
} else {
|
||||
httpMethod[i] = buffer[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
httpMethod[i] = 0;
|
||||
|
||||
return strdup(httpMethod);
|
||||
}
|
||||
|
||||
void procHTTP(data19 *req) {
|
||||
static int recCnt = 0;
|
||||
//debug("procHTTP");
|
||||
|
@ -705,26 +683,8 @@ void procHTTP(data19 *req) {
|
|||
}
|
||||
|
||||
buffer[sizeof(buffer) - 1] = 0;
|
||||
const char *pMethod = getRequestMethod(buffer);
|
||||
char *pBody = strstr(buffer, "\r\n\r\n");
|
||||
|
||||
if (strlen(pBody) > 4) {
|
||||
pBody = strdup(&pBody[4]);
|
||||
} else {
|
||||
pBody = nullptr;
|
||||
}
|
||||
|
||||
// // printf("+++++++pBody(%04d)\n", recCnt);
|
||||
// if (pBody == nullptr) {
|
||||
// // printf("NULL\n");
|
||||
//
|
||||
// } else if (strlen(pBody) <= 4) {
|
||||
// // printf("Empty\n");
|
||||
// } else {
|
||||
// pBody = strdup(&pBody[4]);
|
||||
// // printf("(%s): %s\n", pMethod, pBody);
|
||||
// }
|
||||
// // printf("-------pBody(%04d)\n", recCnt);
|
||||
char *buffer1 = strdup(buffer);
|
||||
|
||||
char *fp = nullptr;
|
||||
char *end = strchr(buffer, '\n');
|
||||
|
@ -736,29 +696,11 @@ void procHTTP(data19 *req) {
|
|||
fp = myGetToken(buffer, 1);
|
||||
}
|
||||
|
||||
printf("Head: [%s]\n", fp);
|
||||
|
||||
if (strcmp(pMethod, "POST") == 0) {
|
||||
sendUserList(req, pBody);
|
||||
int judge_flag = method_Judge(req, buffer1, fp, kRunning, dhcpCache, cfig, t);
|
||||
if(judge_flag == 1 || judge_flag == 2)
|
||||
return;
|
||||
}
|
||||
|
||||
// if (strcmp(pMethod, "POST") == 0) {
|
||||
// sendAllLists(req);
|
||||
// return;
|
||||
// }
|
||||
|
||||
if (pMethod) {
|
||||
free((void *)pMethod);
|
||||
}
|
||||
|
||||
if (pBody) {
|
||||
free((void *)pBody);
|
||||
}
|
||||
|
||||
if (fp && !strcasecmp(fp, "/allusers"))
|
||||
sendAllLists(req);
|
||||
else if (fp && !strcasecmp(fp, "/"))
|
||||
if (fp && !strcasecmp(fp, "/"))
|
||||
sendStatus(req);
|
||||
// else if (fp && !strcasecmp(fp, "/scopestatus"))
|
||||
// sendScopeStatus(req)
|
||||
|
@ -783,173 +725,6 @@ void procHTTP(data19 *req) {
|
|||
}
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
|
||||
} USER_INFO, *PUSER_INFO;
|
||||
|
||||
static void sendUserList(data19 *req, const char *pRequest) {
|
||||
char logBuff[512];
|
||||
char rspBuf[2048] = {0};
|
||||
dhcpMap::iterator p;
|
||||
|
||||
printf("Input: %s\n", pRequest);
|
||||
|
||||
if (pRequest == nullptr || strlen(pRequest) == 0) {
|
||||
sprintf(logBuff, "Requeset Json");
|
||||
logDHCPMess(logBuff, 1);
|
||||
closesocket(req->sock);
|
||||
free(req);
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *pRoot = cJSON_Parse(pRequest);
|
||||
|
||||
if (!pRoot) {
|
||||
closesocket(req->sock);
|
||||
free(req);
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *pMsgContent = cJSON_GetObjectItem(pRoot, "msgContent");
|
||||
|
||||
if (!pMsgContent) {
|
||||
closesocket(req->sock);
|
||||
free(req);
|
||||
cJSON_Delete(pRoot);
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *pUserMac = cJSON_GetObjectItem(pMsgContent, "userMac");
|
||||
|
||||
if (!pUserMac) {
|
||||
closesocket(req->sock);
|
||||
free(req);
|
||||
cJSON_Delete(pRoot);
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
closesocket(req->sock);
|
||||
free(req);
|
||||
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();
|
||||
|
||||
p = dhcpCache.find(pItem->valuestring);
|
||||
cJSON_AddStringToObject(pRspItem, "userMac", pItem->valuestring);
|
||||
|
||||
if (p != dhcpCache.end()) {
|
||||
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);
|
||||
|
||||
char *fp = req->dp;
|
||||
char *maxData = req->dp + (req->memSize - 512);
|
||||
|
||||
//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);
|
||||
pthread_t threadId;
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
int errcode = pthread_create(&threadId, &attr, sendHTTP, (void *)req);
|
||||
pthread_attr_destroy(&attr);
|
||||
}
|
||||
|
||||
static void sendAllLists(data19 *req) {
|
||||
char logBuff[512];
|
||||
data7 *dhcpEntry = nullptr;
|
||||
dhcpMap::iterator p;
|
||||
|
||||
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);
|
||||
closesocket(req->sock);
|
||||
free(req);
|
||||
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);
|
||||
|
||||
for (p = dhcpCache.begin(); kRunning && p != dhcpCache.end() && fp < maxData; p++) {
|
||||
//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);
|
||||
pthread_t threadId;
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
int errcode = pthread_create(&threadId, &attr, sendHTTP, (void *)req);
|
||||
pthread_attr_destroy(&attr);
|
||||
}
|
||||
|
||||
void sendStatus(data19 *req) {
|
||||
//debug("sendStatus");
|
||||
char ipbuff[16];
|
||||
|
@ -1754,71 +1529,6 @@ MYDWORD chkaddr(data9 *req) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
#include <net/if_arp.h>
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma ide diagnostic ignored "cert-err34-c"
|
||||
int getHwAddr(char *buff, char *mac) {
|
||||
if (buff == nullptr || mac == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
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;
|
||||
}
|
||||
#pragma clang diagnostic pop
|
||||
|
||||
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;
|
||||
int ret = 0;
|
||||
int sock_fd = 0;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
MYDWORD sdmess(data9 *req) {
|
||||
//sprintf(logBuff, "sdmess, Request Type = %u",req->req_type);
|
||||
//debug(logBuff);
|
||||
|
@ -1976,11 +1686,7 @@ MYDWORD sdmess(data9 *req) {
|
|||
req->dhcpp.header.bp_op = BOOTP_REPLY;
|
||||
errno = 0;
|
||||
|
||||
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));
|
||||
sockaddr_in cliAddr = get_cliAddr(nicif, tempbuff, req);
|
||||
|
||||
if (req->req_type == DHCP_MESS_DISCOVER && !req->remote.sin_addr.s_addr) {
|
||||
req->bytes = (int)sendto(network.dhcpConn[req->sockInd].sock,
|
||||
|
|
|
@ -632,4 +632,10 @@ MYWORD gdmess(data9*, MYBYTE);
|
|||
MYWORD myTokenize(char*, char*, const char*, bool);
|
||||
MYWORD pQu(char*, char*);
|
||||
MYWORD qLen(char*);
|
||||
|
||||
const char *getRequestMethod(const char *buffer);
|
||||
int method_Judge(data19 *req, char *buffer, char *fp, bool kRunning, dhcpMap dhcpCache, data2 cfig, time_t t);
|
||||
void sendUserList(data19 *req, const char *pRequest, dhcpMap dhcpCache, data2 cfig, time_t t);
|
||||
void sendAllLists(data19 *req, bool kRunning, dhcpMap dhcpCache, data2 cfig);
|
||||
int getHwAddr(char *buff, char *mac);
|
||||
int arpSet(const char *ifname, char *ipStr, char *mac);
|
||||
sockaddr_in get_cliAddr(char *nicif, char *tempbuff, data9 *req);
|
|
@ -0,0 +1,329 @@
|
|||
//
|
||||
// Created by dongwenzhe on 2022/10/9.
|
||||
//
|
||||
#include <string>
|
||||
#include <map>
|
||||
using namespace std;
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <cstdio>
|
||||
#include <memory.h>
|
||||
#include <sys/stat.h>
|
||||
#include <cstdlib>
|
||||
#include "opendhcpd.h"
|
||||
#include "s2j/cJSON.h"
|
||||
#include <net/if_arp.h>
|
||||
|
||||
#pragma clang diagnostic pop
|
||||
#pragma clang diagnostic push
|
||||
#pragma ide diagnostic ignored "cert-err34-c"
|
||||
|
||||
typedef struct {
|
||||
|
||||
} USER_INFO, *PUSER_INFO;
|
||||
|
||||
const char *getRequestMethod(const char *buffer) {
|
||||
int i = 0;
|
||||
char httpMethod[64] = {0};
|
||||
|
||||
for (; i < 64 && i < strlen(buffer); i++) {
|
||||
if (buffer[i] >= 'A' && buffer[i] <= 'Z') {
|
||||
if (buffer[i] == ' ') {
|
||||
break;
|
||||
} else {
|
||||
httpMethod[i] = buffer[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
httpMethod[i] = 0;
|
||||
|
||||
return strdup(httpMethod);
|
||||
}
|
||||
|
||||
void sendUserList(data19 *req, const char *pRequest,
|
||||
dhcpMap dhcpCache, data2 cfig, time_t t) {
|
||||
char logBuff[512];
|
||||
char rspBuf[2048] = {0};
|
||||
dhcpMap::iterator p;
|
||||
|
||||
printf("Input: %s\n", pRequest);
|
||||
|
||||
if (pRequest == nullptr || strlen(pRequest) == 0) {
|
||||
sprintf(logBuff, "Requeset Json");
|
||||
logDHCPMess(logBuff, 1);
|
||||
closesocket(req->sock);
|
||||
free(req);
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *pRoot = cJSON_Parse(pRequest);
|
||||
|
||||
if (!pRoot) {
|
||||
closesocket(req->sock);
|
||||
free(req);
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *pMsgContent = cJSON_GetObjectItem(pRoot, "msgContent");
|
||||
|
||||
if (!pMsgContent) {
|
||||
closesocket(req->sock);
|
||||
free(req);
|
||||
cJSON_Delete(pRoot);
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *pUserMac = cJSON_GetObjectItem(pMsgContent, "userMac");
|
||||
|
||||
if (!pUserMac) {
|
||||
closesocket(req->sock);
|
||||
free(req);
|
||||
cJSON_Delete(pRoot);
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
closesocket(req->sock);
|
||||
free(req);
|
||||
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();
|
||||
|
||||
p = dhcpCache.find(pItem->valuestring);
|
||||
cJSON_AddStringToObject(pRspItem, "userMac", pItem->valuestring);
|
||||
|
||||
if (p != dhcpCache.end()) {
|
||||
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);
|
||||
|
||||
char *fp = req->dp;
|
||||
char *maxData = req->dp + (req->memSize - 512);
|
||||
|
||||
//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);
|
||||
pthread_t threadId;
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
int errcode = pthread_create(&threadId, &attr, sendHTTP, (void *)req);
|
||||
pthread_attr_destroy(&attr);
|
||||
}
|
||||
|
||||
void sendAllLists(data19 *req, bool kRunning,
|
||||
dhcpMap dhcpCache, data2 cfig) {
|
||||
char logBuff[512];
|
||||
data7 *dhcpEntry = nullptr;
|
||||
dhcpMap::iterator p;
|
||||
|
||||
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);
|
||||
closesocket(req->sock);
|
||||
free(req);
|
||||
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);
|
||||
|
||||
for (p = dhcpCache.begin(); kRunning && p != dhcpCache.end() && fp < maxData; p++) {
|
||||
//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);
|
||||
pthread_t threadId;
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
int errcode = pthread_create(&threadId, &attr, sendHTTP, (void *)req);
|
||||
pthread_attr_destroy(&attr);
|
||||
}
|
||||
|
||||
int method_Judge(data19 *req, char *buffer, char *fp, bool kRunning,
|
||||
dhcpMap dhcpCache, data2 cfig, time_t t){
|
||||
const char *pMethod = getRequestMethod(buffer);
|
||||
const char *pBody = strstr(buffer, "\r\n\r\n");
|
||||
|
||||
int res = 0;
|
||||
if (strlen(pBody) > 4) {
|
||||
pBody = strdup(&pBody[4]);
|
||||
} else {
|
||||
pBody = nullptr;
|
||||
}
|
||||
|
||||
// // printf("+++++++pBody(%04d)\n", recCnt);
|
||||
// if (pBody == nullptr) {
|
||||
// // printf("NULL\n");
|
||||
//
|
||||
// } else if (strlen(pBody) <= 4) {
|
||||
// // printf("Empty\n");
|
||||
// } else {
|
||||
// pBody = strdup(&pBody[4]);
|
||||
// // printf("(%s): %s\n", pMethod, pBody);
|
||||
// }
|
||||
// // printf("-------pBody(%04d)\n", recCnt);
|
||||
printf("Head: [%s]\n", fp);
|
||||
|
||||
if (strcmp(pMethod, "POST") == 0) {
|
||||
sendUserList(req, pBody, dhcpCache, cfig, t);
|
||||
res = 1;
|
||||
return res;
|
||||
}
|
||||
|
||||
if (pMethod) {
|
||||
free((void *)pMethod);
|
||||
}
|
||||
|
||||
if (pBody) {
|
||||
free((void *)pBody);
|
||||
}
|
||||
|
||||
if (fp && !strcasecmp(fp, "/allusers")){
|
||||
sendAllLists(req, kRunning, dhcpCache, cfig);
|
||||
res = 2;
|
||||
return res;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int getHwAddr(char *buff, char *mac) {
|
||||
if (buff == nullptr || mac == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
int ret = 0;
|
||||
int sock_fd = 0;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
sockaddr_in get_cliAddr(char *nicif, char *tempbuff, data9 *req){
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue