OCT 1. opendhcpd适配vcpe HTTP service框架
This commit is contained in:
parent
9dc9d645aa
commit
362a75f596
|
@ -2,7 +2,9 @@ SET(DHCPD_PROJECT_TARGET opendhcpd)
|
||||||
|
|
||||||
PROJECT(${DHCPD_PROJECT_TARGET} CXX)
|
PROJECT(${DHCPD_PROJECT_TARGET} CXX)
|
||||||
|
|
||||||
INCLUDE_DIRECTORIES(../libs/include ./)
|
add_definitions(-Wno-write-strings)
|
||||||
|
|
||||||
|
INCLUDE_DIRECTORIES(../libs/include ./ ../httpserver/include)
|
||||||
|
|
||||||
FILE(GLOB DHCPD_HEADS ./*.h)
|
FILE(GLOB DHCPD_HEADS ./*.h)
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
API_EXPORT int dhcpd_main(int argc, char **argv);
|
API_EXPORT int dhcpd_main(int daemon, const char *pInifile, const char *pStatusFile, const char *pIfName);
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -30,20 +30,20 @@ using namespace std;
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <csignal>
|
|
||||||
#include <cstdio>
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
|
#include <zlog.h>
|
||||||
#include "opendhcpd.h"
|
#include "opendhcpd.h"
|
||||||
#include "dhcpd.h"
|
#include "dhcpd.h"
|
||||||
//#include "s2j/cJSON.h"
|
#include "task_manager.h"
|
||||||
|
|
||||||
#define STR2INT(val) ((int)strtol((val), nullptr, 10))
|
#define STR2INT(val) ((int)strtol((val), nullptr, 10))
|
||||||
|
|
||||||
|
void on_system_exit(void *p);
|
||||||
//Global Variables
|
//Global Variables
|
||||||
timeval tv;
|
timeval tv;
|
||||||
fd_set readfds;
|
fd_set readfds;
|
||||||
|
@ -63,7 +63,7 @@ char serviceName[] = "OpenDHCPServer";
|
||||||
bool verbatim = false;
|
bool verbatim = false;
|
||||||
char iniFile[256] = "";
|
char iniFile[256] = "";
|
||||||
char leaFile[256] = "";
|
char leaFile[256] = "";
|
||||||
char logFile[256] = "";
|
//char logFile[256] = "";
|
||||||
char filePATH[256] = "";
|
char filePATH[256] = "";
|
||||||
char htmlTitle[256] = "";
|
char htmlTitle[256] = "";
|
||||||
char nicif[256] = "";
|
char nicif[256] = "";
|
||||||
|
@ -218,16 +218,11 @@ const data4 opData[] = {
|
||||||
{"NextServer", 254, 3, true},
|
{"NextServer", 254, 3, true},
|
||||||
};
|
};
|
||||||
|
|
||||||
int dhcpd_main(int argc, char **argv) {
|
int dhcpd_main(int daemon, const char *pInifile, const char *pStatusFile, const char *pIfName) {
|
||||||
char logBuff[256] = "";
|
char logBuff[256] = "";
|
||||||
|
|
||||||
signal(SIGINT, catch_system_interupt);
|
task_add_exit_event_handler(on_system_exit, nullptr);
|
||||||
signal(SIGABRT, catch_system_interupt);
|
#if 0
|
||||||
signal(SIGTERM, catch_system_interupt);
|
|
||||||
signal(SIGQUIT, catch_system_interupt);
|
|
||||||
signal(SIGTSTP, catch_system_interupt);
|
|
||||||
signal(SIGHUP, catch_system_interupt);
|
|
||||||
|
|
||||||
//printf("%i\n", argc);
|
//printf("%i\n", argc);
|
||||||
/*
|
/*
|
||||||
logBuff[0] = 0;
|
logBuff[0] = 0;
|
||||||
|
@ -280,15 +275,27 @@ int dhcpd_main(int argc, char **argv) {
|
||||||
else
|
else
|
||||||
sprintf(logBuff, "Error: Invalid Argument %s", argv[i]);
|
sprintf(logBuff, "Error: Invalid Argument %s", argv[i]);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (!leaFile[0])
|
verbatim = (daemon > 0) ? true : false;
|
||||||
strcpy(leaFile, "/tmp/opendhcp.state");
|
|
||||||
|
|
||||||
if (!iniFile[0])
|
if (pInifile && strlen(pInifile) > 0) {
|
||||||
|
strcpy(iniFile, pInifile);
|
||||||
|
} else {
|
||||||
strcpy(iniFile, "/etc/opendhcp.ini");
|
strcpy(iniFile, "/etc/opendhcp.ini");
|
||||||
|
}
|
||||||
|
|
||||||
if (!nicif[0])
|
if (pStatusFile && strlen(pStatusFile) > 0) {
|
||||||
|
strcpy(leaFile, pStatusFile);
|
||||||
|
} else {
|
||||||
|
strcpy(leaFile, "/tmp/opendhcp.state");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pIfName && strlen(pIfName) > 0) {
|
||||||
|
strcpy(nicif, pIfName);
|
||||||
|
} else {
|
||||||
strcpy(nicif, "vxlan0");
|
strcpy(nicif, "vxlan0");
|
||||||
|
}
|
||||||
|
|
||||||
strcpy(filePATH, iniFile);
|
strcpy(filePATH, iniFile);
|
||||||
|
|
||||||
|
@ -561,7 +568,7 @@ void closeConn() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void catch_system_interupt(int sig_num) {
|
void on_system_exit(void *p) {
|
||||||
char logBuff[256];
|
char logBuff[256];
|
||||||
|
|
||||||
//printf("%u=%u\n", cfig.ppid, getpid());
|
//printf("%u=%u\n", cfig.ppid, getpid());
|
||||||
|
@ -615,6 +622,7 @@ MYBYTE pIP(void *raw, MYDWORD data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void procHTTP(data19 *req) {
|
void procHTTP(data19 *req) {
|
||||||
|
#if 0
|
||||||
static int recCnt = 0;
|
static int recCnt = 0;
|
||||||
//debug("procHTTP");
|
//debug("procHTTP");
|
||||||
char tempbuff[512];
|
char tempbuff[512];
|
||||||
|
@ -696,15 +704,15 @@ void procHTTP(data19 *req) {
|
||||||
fp = myGetToken(buffer, 1);
|
fp = myGetToken(buffer, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int judge_flag = method_Judge(req, buffer1, fp, kRunning, &dhcpCache, &cfig, t);
|
int judge_flag = method_Judge(req, buffer1);
|
||||||
if (judge_flag == 1 || judge_flag == 2)
|
if (judge_flag == 1 || judge_flag == 2)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (fp && !strcasecmp(fp, "/"))
|
if (fp && !strcasecmp(fp, "/")) {
|
||||||
sendStatus(req);
|
//sendStatus(req);
|
||||||
// else if (fp && !strcasecmp(fp, "/scopestatus"))
|
// else if (fp && !strcasecmp(fp, "/scopestatus"))
|
||||||
// sendScopeStatus(req)
|
// sendScopeStatus(req)
|
||||||
else {
|
} else {
|
||||||
if (fp && (verbatim || cfig.dhcpLogLevel >= 2)) {
|
if (fp && (verbatim || cfig.dhcpLogLevel >= 2)) {
|
||||||
sprintf(logBuff, "HTTP Client %s, %s not found", IP2String(tempbuff, req->remote.sin_addr.s_addr), fp);
|
sprintf(logBuff, "HTTP Client %s, %s not found", IP2String(tempbuff, req->remote.sin_addr.s_addr), fp);
|
||||||
logDHCPMess(logBuff, 2);
|
logDHCPMess(logBuff, 2);
|
||||||
|
@ -723,9 +731,10 @@ void procHTTP(data19 *req) {
|
||||||
int errcode = pthread_create(&threadId, &attr, sendHTTP, (void *)req);
|
int errcode = pthread_create(&threadId, &attr, sendHTTP, (void *)req);
|
||||||
pthread_attr_destroy(&attr);
|
pthread_attr_destroy(&attr);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendStatus(data19 *req) {
|
void prepareUserHtmlRespStatus(data19 *req) {
|
||||||
//debug("sendStatus");
|
//debug("sendStatus");
|
||||||
char ipbuff[16];
|
char ipbuff[16];
|
||||||
char extbuff[16];
|
char extbuff[16];
|
||||||
|
@ -877,12 +886,12 @@ void sendStatus(data19 *req) {
|
||||||
//MYBYTE x = sprintf(tempbuff, "%u", (fp - contentStart));
|
//MYBYTE x = sprintf(tempbuff, "%u", (fp - contentStart));
|
||||||
//memcpy((contentStart - 12), tempbuff, x);
|
//memcpy((contentStart - 12), tempbuff, x);
|
||||||
req->bytes = (int)(fp - req->dp);
|
req->bytes = (int)(fp - req->dp);
|
||||||
pthread_t threadId;
|
// pthread_t threadId;
|
||||||
pthread_attr_t attr;
|
// pthread_attr_t attr;
|
||||||
pthread_attr_init(&attr);
|
// pthread_attr_init(&attr);
|
||||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
// pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||||
int errcode = pthread_create(&threadId, &attr, sendHTTP, (void *)req);
|
// int errcode = pthread_create(&threadId, &attr, sendHTTP, (void *)req);
|
||||||
pthread_attr_destroy(&attr);
|
// pthread_attr_destroy(&attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -4570,6 +4579,10 @@ void *init(void *lparam) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if 1
|
||||||
|
network.httpConn.ready = false;
|
||||||
|
opendhcp_init_http_server();
|
||||||
|
#else
|
||||||
newNetwork.httpConn.port = 6789;
|
newNetwork.httpConn.port = 6789;
|
||||||
newNetwork.httpConn.server = newNetwork.dhcpConn[0].server;
|
newNetwork.httpConn.server = newNetwork.dhcpConn[0].server;
|
||||||
newNetwork.httpConn.loaded = true;
|
newNetwork.httpConn.loaded = true;
|
||||||
|
@ -4680,7 +4693,7 @@ void *init(void *lparam) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
newNetwork.maxFD++;
|
newNetwork.maxFD++;
|
||||||
|
|
||||||
for (MYBYTE m = 0; m < MAX_SERVERS && newNetwork.allServers[m]; m++)
|
for (MYBYTE m = 0; m < MAX_SERVERS && newNetwork.allServers[m]; m++)
|
||||||
|
@ -5066,6 +5079,7 @@ void debug(const char *mess) {
|
||||||
logDHCPMess(tmp, 1);
|
logDHCPMess(tmp, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
void *logThread(void *lpParam) {
|
void *logThread(void *lpParam) {
|
||||||
char *mess = (char *)lpParam;
|
char *mess = (char *)lpParam;
|
||||||
pthread_mutex_lock(&mutLogFile);
|
pthread_mutex_lock(&mutLogFile);
|
||||||
|
@ -5112,20 +5126,29 @@ void *logThread(void *lpParam) {
|
||||||
pthread_mutex_unlock(&mutLogFile);
|
pthread_mutex_unlock(&mutLogFile);
|
||||||
pthread_exit(nullptr);
|
pthread_exit(nullptr);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void logDHCPMess(char *logBuff, MYBYTE logLevel) {
|
void logDHCPMess(char *logBuff, MYBYTE logLevel) {
|
||||||
if (verbatim)
|
// if (verbatim)
|
||||||
printf("%s\n", logBuff);
|
// printf("%s\n", logBuff);
|
||||||
|
|
||||||
if (logFile[0] && logLevel <= cfig.dhcpLogLevel) {
|
if (logLevel == 2) {
|
||||||
char *mess = cloneString(logBuff);
|
dzlog_debug("%s\n", logBuff);
|
||||||
pthread_t threadId;
|
} else if (logLevel == 1) {
|
||||||
pthread_attr_t attr;
|
dzlog_info("%s\n", logBuff);
|
||||||
pthread_attr_init(&attr);
|
} else if (logLevel == 0) {
|
||||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
dzlog_error("%s\n", logBuff);
|
||||||
int errcode = pthread_create(&threadId, &attr, logThread, mess);
|
|
||||||
pthread_attr_destroy(&attr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if (logFile[0] && logLevel <= cfig.dhcpLogLevel) {
|
||||||
|
// char *mess = cloneString(logBuff);
|
||||||
|
// pthread_t threadId;
|
||||||
|
// pthread_attr_t attr;
|
||||||
|
// pthread_attr_init(&attr);
|
||||||
|
// pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||||
|
// int errcode = pthread_create(&threadId, &attr, logThread, mess);
|
||||||
|
// pthread_attr_destroy(&attr);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
data7 *createCache(data71 *pLump) {
|
data7 *createCache(data71 *pLump) {
|
||||||
|
|
|
@ -633,7 +633,8 @@ MYWORD myTokenize(char*, char*, const char*, bool);
|
||||||
MYWORD pQu(char*, char*);
|
MYWORD pQu(char*, char*);
|
||||||
MYWORD qLen(char*);
|
MYWORD qLen(char*);
|
||||||
const char *getRequestMethod(const char *buffer);
|
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 prepareUserHtmlRespStatus(data19 *req);
|
||||||
|
void opendhcp_init_http_server();
|
||||||
void sendUserList(data19 *req, const char *pRequest, 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);
|
void sendAllLists(data19 *req, bool kRunning, dhcpMap *dhcpCache, data2 *cfig);
|
||||||
int getHwAddr(char *buff, char *mac);
|
int getHwAddr(char *buff, char *mac);
|
||||||
|
|
|
@ -5,41 +5,26 @@
|
||||||
#include <map>
|
#include <map>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <cstdio>
|
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include "opendhcpd.h"
|
#include "opendhcpd.h"
|
||||||
#include "s2j/cJSON.h"
|
#include "s2j/cJSON.h"
|
||||||
|
#include "haywire.h"
|
||||||
#include <net/if_arp.h>
|
#include <net/if_arp.h>
|
||||||
|
|
||||||
const char *getRequestMethod(const char *buffer) {
|
extern data2 cfig;
|
||||||
int i = 0;
|
extern bool kRunning;
|
||||||
char httpMethod[64] = {0};
|
extern dhcpMap dhcpCache;
|
||||||
|
extern time_t t;
|
||||||
|
|
||||||
for (; i < 64 && i < strlen(buffer); i++) {
|
static void sendUserList(data19 *req, const char *pRequest) {
|
||||||
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 logBuff[512];
|
||||||
dhcpMap::iterator p;
|
dhcpMap::iterator p;
|
||||||
|
|
||||||
|
@ -48,24 +33,18 @@ void sendUserList(data19 *req, const char *pRequest, dhcpMap *dhcpCache, data2 *
|
||||||
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);
|
||||||
closesocket(req->sock);
|
|
||||||
free(req);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON *pRoot = cJSON_Parse(pRequest);
|
cJSON *pRoot = cJSON_Parse(pRequest);
|
||||||
|
|
||||||
if (!pRoot) {
|
if (!pRoot) {
|
||||||
closesocket(req->sock);
|
|
||||||
free(req);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON *pMsgContent = cJSON_GetObjectItem(pRoot, "msgContent");
|
cJSON *pMsgContent = cJSON_GetObjectItem(pRoot, "msgContent");
|
||||||
|
|
||||||
if (!pMsgContent) {
|
if (!pMsgContent) {
|
||||||
closesocket(req->sock);
|
|
||||||
free(req);
|
|
||||||
cJSON_Delete(pRoot);
|
cJSON_Delete(pRoot);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -73,20 +52,16 @@ void sendUserList(data19 *req, const char *pRequest, dhcpMap *dhcpCache, data2 *
|
||||||
cJSON *pUserMac = cJSON_GetObjectItem(pMsgContent, "userMac");
|
cJSON *pUserMac = cJSON_GetObjectItem(pMsgContent, "userMac");
|
||||||
|
|
||||||
if (!pUserMac) {
|
if (!pUserMac) {
|
||||||
closesocket(req->sock);
|
|
||||||
free(req);
|
|
||||||
cJSON_Delete(pRoot);
|
cJSON_Delete(pRoot);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
if (!req->dp) {
|
if (!req->dp) {
|
||||||
sprintf(logBuff, "Memory Error");
|
sprintf(logBuff, "Memory Error");
|
||||||
logDHCPMess(logBuff, 1);
|
logDHCPMess(logBuff, 1);
|
||||||
closesocket(req->sock);
|
|
||||||
free(req);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,10 +80,10 @@ void sendUserList(data19 *req, const char *pRequest, dhcpMap *dhcpCache, data2 *
|
||||||
cJSON *pItem = cJSON_GetArrayItem(pUserMac, i);
|
cJSON *pItem = cJSON_GetArrayItem(pUserMac, i);
|
||||||
cJSON *pRspItem = cJSON_CreateObject();
|
cJSON *pRspItem = cJSON_CreateObject();
|
||||||
|
|
||||||
p = dhcpCache->find(pItem->valuestring);
|
p = dhcpCache.find(pItem->valuestring);
|
||||||
cJSON_AddStringToObject(pRspItem, "userMac", pItem->valuestring);
|
cJSON_AddStringToObject(pRspItem, "userMac", pItem->valuestring);
|
||||||
|
|
||||||
if (p != dhcpCache->end()) {
|
if (p != dhcpCache.end()) {
|
||||||
data7 *dhcpEntry = p->second;
|
data7 *dhcpEntry = p->second;
|
||||||
|
|
||||||
cJSON_AddStringToObject(pRspItem, "ip", IP2String(tempbuff, dhcpEntry->ip));
|
cJSON_AddStringToObject(pRspItem, "ip", IP2String(tempbuff, dhcpEntry->ip));
|
||||||
|
@ -140,27 +115,19 @@ void sendUserList(data19 *req, const char *pRequest, dhcpMap *dhcpCache, data2 *
|
||||||
cJSON_Delete(pRspRoot);
|
cJSON_Delete(pRspRoot);
|
||||||
|
|
||||||
req->bytes = (int)(fp - req->dp);
|
req->bytes = (int)(fp - req->dp);
|
||||||
pthread_t threadId;
|
|
||||||
pthread_attr_t attr;
|
|
||||||
pthread_attr_init(&attr);
|
|
||||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
||||||
pthread_create(&threadId, &attr, sendHTTP, (void *)req);
|
|
||||||
pthread_attr_destroy(&attr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendAllLists(data19 *req, bool kRunning, dhcpMap *dhcpCache, data2 *cfig) {
|
static void sendAllLists(data19 *req) {
|
||||||
char logBuff[512];
|
char logBuff[512];
|
||||||
data7 *dhcpEntry;
|
data7 *dhcpEntry;
|
||||||
dhcpMap::iterator p;
|
dhcpMap::iterator p;
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
if (!req->dp) {
|
if (!req->dp) {
|
||||||
sprintf(logBuff, "Memory Error");
|
sprintf(logBuff, "Memory Error");
|
||||||
logDHCPMess(logBuff, 1);
|
logDHCPMess(logBuff, 1);
|
||||||
closesocket(req->sock);
|
|
||||||
free(req);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,7 +144,7 @@ void sendAllLists(data19 *req, bool kRunning, dhcpMap *dhcpCache, data2 *cfig) {
|
||||||
|
|
||||||
cJSON_AddItemToObject(pRspMsg, "users", pMsgArray);
|
cJSON_AddItemToObject(pRspMsg, "users", pMsgArray);
|
||||||
|
|
||||||
for (p = dhcpCache->begin(); kRunning && p != dhcpCache->end() && fp < maxData; p++) {
|
for (p = dhcpCache.begin(); kRunning && p != dhcpCache.end() && fp < maxData; p++) {
|
||||||
//cJSON *pRspItem = cJSON_CreateObject();
|
//cJSON *pRspItem = cJSON_CreateObject();
|
||||||
if ((dhcpEntry = p->second)) {
|
if ((dhcpEntry = p->second)) {
|
||||||
cJSON_AddStringToObject(pMsgArray, "", dhcpEntry->mapname);
|
cJSON_AddStringToObject(pMsgArray, "", dhcpEntry->mapname);
|
||||||
|
@ -193,58 +160,6 @@ void sendAllLists(data19 *req, bool kRunning, dhcpMap *dhcpCache, data2 *cfig) {
|
||||||
cJSON_Delete(pRspRoot);
|
cJSON_Delete(pRspRoot);
|
||||||
|
|
||||||
req->bytes = (int)(fp - req->dp);
|
req->bytes = (int)(fp - req->dp);
|
||||||
pthread_t threadId;
|
|
||||||
pthread_attr_t attr;
|
|
||||||
pthread_attr_init(&attr);
|
|
||||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma clang diagnostic push
|
#pragma clang diagnostic push
|
||||||
|
@ -318,3 +233,128 @@ sockaddr_in get_cliAddr(char *nicif, char *tempbuff, data9 *req) {
|
||||||
cliAddr.sin_addr.s_addr = inet_addr(IP2String(tempbuff, req->dhcpp.header.bp_yiaddr));
|
cliAddr.sin_addr.s_addr = inet_addr(IP2String(tempbuff, req->dhcpp.header.bp_yiaddr));
|
||||||
return cliAddr;
|
return cliAddr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void opendhcp_http_info(http_request *request, hw_http_response *response, void *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;
|
||||||
|
|
||||||
|
static data19 req;
|
||||||
|
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);
|
||||||
|
|
||||||
|
if(request->method != HW_HTTP_GET) {
|
||||||
|
SETSTRING(status_code, HTTP_STATUS_405);
|
||||||
|
SETSTRING(body, HTTP_STATUS_405);
|
||||||
|
} else {
|
||||||
|
SETSTRING(status_code, HTTP_STATUS_200);
|
||||||
|
prepareUserHtmlRespStatus(&req);
|
||||||
|
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, user_data, nullptr);
|
||||||
|
|
||||||
|
free(req.dp);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void opendhcp_http_get_userinfo(http_request *request, hw_http_response *response, void *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;
|
||||||
|
|
||||||
|
static data19 req;
|
||||||
|
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);
|
||||||
|
|
||||||
|
if(request->method != HW_HTTP_GET) {
|
||||||
|
SETSTRING(status_code, HTTP_STATUS_405);
|
||||||
|
SETSTRING(body, HTTP_STATUS_405);
|
||||||
|
} else {
|
||||||
|
SETSTRING(status_code, HTTP_STATUS_200);
|
||||||
|
sendUserList(&req, request->body->value);
|
||||||
|
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, user_data, nullptr);
|
||||||
|
|
||||||
|
free(req.dp);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void opendhcp_http_get_alluser(http_request *request, hw_http_response *response, void *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;
|
||||||
|
|
||||||
|
static data19 req;
|
||||||
|
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);
|
||||||
|
|
||||||
|
if(request->method != HW_HTTP_GET) {
|
||||||
|
SETSTRING(status_code, HTTP_STATUS_405);
|
||||||
|
SETSTRING(body, HTTP_STATUS_405);
|
||||||
|
} else {
|
||||||
|
SETSTRING(status_code, HTTP_STATUS_200);
|
||||||
|
sendAllLists(&req);
|
||||||
|
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, user_data, nullptr);
|
||||||
|
|
||||||
|
free(req.dp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void opendhcp_init_http_server() {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue