OCT 1. opendhcpd适配vcpe HTTP service框架

This commit is contained in:
huangxin 2022-11-07 14:22:42 +08:00
parent 9dc9d645aa
commit 362a75f596
5 changed files with 209 additions and 143 deletions

View File

@ -2,7 +2,9 @@ SET(DHCPD_PROJECT_TARGET opendhcpd)
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)

View File

@ -11,7 +11,7 @@
extern "C" {
#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
}
#endif

View File

@ -30,20 +30,20 @@ using namespace std;
#include <net/if.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <csignal>
#include <cstdio>
#include <fcntl.h>
#include <cerrno>
#include <memory.h>
#include <sys/stat.h>
#include <cstdlib>
#include <syslog.h>
#include <zlog.h>
#include "opendhcpd.h"
#include "dhcpd.h"
//#include "s2j/cJSON.h"
#include "task_manager.h"
#define STR2INT(val) ((int)strtol((val), nullptr, 10))
void on_system_exit(void *p);
//Global Variables
timeval tv;
fd_set readfds;
@ -63,7 +63,7 @@ char serviceName[] = "OpenDHCPServer";
bool verbatim = false;
char iniFile[256] = "";
char leaFile[256] = "";
char logFile[256] = "";
//char logFile[256] = "";
char filePATH[256] = "";
char htmlTitle[256] = "";
char nicif[256] = "";
@ -218,16 +218,11 @@ const data4 opData[] = {
{"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] = "";
signal(SIGINT, catch_system_interupt);
signal(SIGABRT, catch_system_interupt);
signal(SIGTERM, catch_system_interupt);
signal(SIGQUIT, catch_system_interupt);
signal(SIGTSTP, catch_system_interupt);
signal(SIGHUP, catch_system_interupt);
task_add_exit_event_handler(on_system_exit, nullptr);
#if 0
//printf("%i\n", argc);
/*
logBuff[0] = 0;
@ -280,15 +275,27 @@ int dhcpd_main(int argc, char **argv) {
else
sprintf(logBuff, "Error: Invalid Argument %s", argv[i]);
}
#endif
if (!leaFile[0])
strcpy(leaFile, "/tmp/opendhcp.state");
verbatim = (daemon > 0) ? true : false;
if (!iniFile[0])
if (pInifile && strlen(pInifile) > 0) {
strcpy(iniFile, pInifile);
} else {
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(filePATH, iniFile);
@ -561,7 +568,7 @@ void closeConn() {
}
}
void catch_system_interupt(int sig_num) {
void on_system_exit(void *p) {
char logBuff[256];
//printf("%u=%u\n", cfig.ppid, getpid());
@ -615,6 +622,7 @@ MYBYTE pIP(void *raw, MYDWORD data) {
}
void procHTTP(data19 *req) {
#if 0
static int recCnt = 0;
//debug("procHTTP");
char tempbuff[512];
@ -696,15 +704,15 @@ void procHTTP(data19 *req) {
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)
return;
if (fp && !strcasecmp(fp, "/"))
sendStatus(req);
if (fp && !strcasecmp(fp, "/")) {
//sendStatus(req);
// else if (fp && !strcasecmp(fp, "/scopestatus"))
// sendScopeStatus(req)
else {
} else {
if (fp && (verbatim || cfig.dhcpLogLevel >= 2)) {
sprintf(logBuff, "HTTP Client %s, %s not found", IP2String(tempbuff, req->remote.sin_addr.s_addr), fp);
logDHCPMess(logBuff, 2);
@ -723,9 +731,10 @@ void procHTTP(data19 *req) {
int errcode = pthread_create(&threadId, &attr, sendHTTP, (void *)req);
pthread_attr_destroy(&attr);
}
#endif
}
void sendStatus(data19 *req) {
void prepareUserHtmlRespStatus(data19 *req) {
//debug("sendStatus");
char ipbuff[16];
char extbuff[16];
@ -877,12 +886,12 @@ void sendStatus(data19 *req) {
//MYBYTE x = sprintf(tempbuff, "%u", (fp - contentStart));
//memcpy((contentStart - 12), tempbuff, x);
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);
// 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);
}
/*
@ -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.server = newNetwork.dhcpConn[0].server;
newNetwork.httpConn.loaded = true;
@ -4680,7 +4693,7 @@ void *init(void *lparam) {
}
}
}
#endif
newNetwork.maxFD++;
for (MYBYTE m = 0; m < MAX_SERVERS && newNetwork.allServers[m]; m++)
@ -5066,6 +5079,7 @@ void debug(const char *mess) {
logDHCPMess(tmp, 1);
}
#if 0
void *logThread(void *lpParam) {
char *mess = (char *)lpParam;
pthread_mutex_lock(&mutLogFile);
@ -5112,20 +5126,29 @@ void *logThread(void *lpParam) {
pthread_mutex_unlock(&mutLogFile);
pthread_exit(nullptr);
}
#endif
void logDHCPMess(char *logBuff, MYBYTE logLevel) {
if (verbatim)
printf("%s\n", logBuff);
// if (verbatim)
// printf("%s\n", logBuff);
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);
if (logLevel == 2) {
dzlog_debug("%s\n", logBuff);
} else if (logLevel == 1) {
dzlog_info("%s\n", logBuff);
} else if (logLevel == 0) {
dzlog_error("%s\n", logBuff);
}
// 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) {

View File

@ -633,7 +633,8 @@ 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 prepareUserHtmlRespStatus(data19 *req);
void opendhcp_init_http_server();
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);

View File

@ -5,41 +5,26 @@
#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 "haywire.h"
#include <net/if_arp.h>
const char *getRequestMethod(const char *buffer) {
int i = 0;
char httpMethod[64] = {0};
extern data2 cfig;
extern bool kRunning;
extern dhcpMap dhcpCache;
extern time_t t;
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) {
static void sendUserList(data19 *req, const char *pRequest) {
char logBuff[512];
dhcpMap::iterator p;
@ -48,24 +33,18 @@ void sendUserList(data19 *req, const char *pRequest, dhcpMap *dhcpCache, data2 *
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;
}
@ -73,20 +52,16 @@ void sendUserList(data19 *req, const char *pRequest, dhcpMap *dhcpCache, data2 *
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->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;
}
@ -105,10 +80,10 @@ void sendUserList(data19 *req, const char *pRequest, dhcpMap *dhcpCache, data2 *
cJSON *pItem = cJSON_GetArrayItem(pUserMac, i);
cJSON *pRspItem = cJSON_CreateObject();
p = dhcpCache->find(pItem->valuestring);
p = dhcpCache.find(pItem->valuestring);
cJSON_AddStringToObject(pRspItem, "userMac", pItem->valuestring);
if (p != dhcpCache->end()) {
if (p != dhcpCache.end()) {
data7 *dhcpEntry = p->second;
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);
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];
data7 *dhcpEntry;
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);
if (!req->dp) {
sprintf(logBuff, "Memory Error");
logDHCPMess(logBuff, 1);
closesocket(req->sock);
free(req);
return;
}
@ -177,7 +144,7 @@ void sendAllLists(data19 *req, bool kRunning, dhcpMap *dhcpCache, data2 *cfig) {
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();
if ((dhcpEntry = p->second)) {
cJSON_AddStringToObject(pMsgArray, "", dhcpEntry->mapname);
@ -193,58 +160,6 @@ void sendAllLists(data19 *req, bool kRunning, dhcpMap *dhcpCache, data2 *cfig) {
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);
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
@ -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));
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);
}