OCT 1. 更新接口错误返回JSON数据

This commit is contained in:
huangxin 2022-12-09 16:14:24 +08:00
parent 9794acc41d
commit 92cf406294
4 changed files with 134 additions and 119 deletions

View File

@ -4,10 +4,23 @@ AccessModifierOffset: -4
InsertBraces: true InsertBraces: true
AlignArrayOfStructures: Left AlignArrayOfStructures: Left
AlignAfterOpenBracket: Align AlignAfterOpenBracket: Align
AlignConsecutiveMacros: Consecutive AlignConsecutiveMacros:
AlignConsecutiveAssignments: Consecutive Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
PadOperators: true
AlignCompound: true
AlignConsecutiveBitFields: None AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: Consecutive AlignConsecutiveDeclarations:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
PadOperators: true
AlignCompound: true
AlignEscapedNewlines: Left AlignEscapedNewlines: Left
AlignOperands: DontAlign AlignOperands: DontAlign
AlignTrailingComments: true AlignTrailingComments: true
@ -195,6 +208,7 @@ StatementMacros:
TabWidth: 4 TabWidth: 4
UseCRLF: false UseCRLF: false
UseTab: Never UseTab: Never
SeparateDefinitionBlocks: Always
WhitespaceSensitiveMacros: WhitespaceSensitiveMacros:
- STRINGIZE - STRINGIZE
- PP_STRINGIZE - PP_STRINGIZE

View File

@ -166,6 +166,7 @@ typedef struct {
void *headers; void *headers;
hw_string *body; hw_string *body;
size_t body_length; size_t body_length;
enum { enum {
OK, OK,
SIZE_EXCEEDED, SIZE_EXCEEDED,
@ -174,7 +175,8 @@ typedef struct {
} state; } state;
} http_request; } http_request;
typedef void(HAYWIRE_CALLING_CONVENTION *http_request_callback)(http_request *request, hw_http_response *response, typedef void(HAYWIRE_CALLING_CONVENTION *http_request_callback)(http_request *request,
hw_http_response *response,
void *user_data); void *user_data);
typedef void(HAYWIRE_CALLING_CONVENTION *http_response_complete_callback)(void *user_data); typedef void(HAYWIRE_CALLING_CONVENTION *http_response_complete_callback)(void *user_data);
@ -190,7 +192,8 @@ HAYWIRE_EXTERN void hw_set_http_version(hw_http_response *response, unsigned sho
HAYWIRE_EXTERN void hw_set_response_status_code(hw_http_response *response, hw_string *status_code); HAYWIRE_EXTERN void hw_set_response_status_code(hw_http_response *response, hw_string *status_code);
HAYWIRE_EXTERN void hw_set_response_header(hw_http_response *response, hw_string *name, hw_string *value); HAYWIRE_EXTERN void hw_set_response_header(hw_http_response *response, hw_string *name, hw_string *value);
HAYWIRE_EXTERN void hw_set_body(hw_http_response *response, hw_string *body); HAYWIRE_EXTERN void hw_set_body(hw_http_response *response, hw_string *body);
HAYWIRE_EXTERN void hw_http_response_send(hw_http_response *response, void *user_data, HAYWIRE_EXTERN void hw_http_response_send(hw_http_response *response,
void *user_data,
http_response_complete_callback callback); http_response_complete_callback callback);
HAYWIRE_EXTERN void hw_http_response_send_error(hw_http_response *response, const char *error, const char *err_msg); HAYWIRE_EXTERN void hw_http_response_send_error(hw_http_response *response, const char *error, const char *err_msg);
HAYWIRE_EXTERN void hw_print_request_headers(http_request *request); HAYWIRE_EXTERN void hw_print_request_headers(http_request *request);

View File

@ -4999,10 +4999,10 @@ void getInterfaces(data1 *pNetwork) {
Ifc.ifc_buf = (char *)IfcBuf; Ifc.ifc_buf = (char *)IfcBuf;
if (ioctl(cfig.fixedSocket, SIOCGIFCONF, &Ifc) >= 0) { if (ioctl(cfig.fixedSocket, SIOCGIFCONF, &Ifc) >= 0) {
struct ifreq pIfr {};
MYDWORD addr, mask; MYDWORD addr, mask;
short flags; short flags;
struct ifreq pIfr {};
MYBYTE numInterfaces = Ifc.ifc_len / sizeof(ifreq); MYBYTE numInterfaces = Ifc.ifc_len / sizeof(ifreq);
for (MYBYTE i = 0; i < numInterfaces; i++) { for (MYBYTE i = 0; i < numInterfaces; i++) {

View File

@ -3,8 +3,6 @@
// //
#include <string> #include <string>
#include <map> #include <map>
using namespace std;
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -25,6 +23,8 @@ using namespace std;
#include "user_errno.h" #include "user_errno.h"
#include "uthash/uthash.h" #include "uthash/uthash.h"
using namespace std;
extern data2 cfig; extern data2 cfig;
extern bool kRunning; extern bool kRunning;
extern dhcpMap dhcpCache; extern dhcpMap dhcpCache;
@ -393,7 +393,7 @@ static int add_dhcpd_rangeset(data19 *req, const char *pRequest) {
return ERR_SUCCESS; return ERR_SUCCESS;
} }
struct hash_map{ struct hash_map {
unsigned int key; unsigned int key;
unsigned int value; unsigned int value;
UT_hash_handle hh; UT_hash_handle hh;
@ -450,7 +450,7 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest) {
for (int i = 0; i < cJSON_GetArraySize(pdhcp_range); i++) { for (int i = 0; i < cJSON_GetArraySize(pdhcp_range); i++) {
cJSON *pdelRange = cJSON_GetArrayItem(pdhcp_range, i); cJSON *pdelRange = cJSON_GetArrayItem(pdhcp_range, i);
char del_range[256]; char del_range[256];
auto *delItem = (struct hash_map*)malloc(sizeof(hash_map)); auto *delItem = (struct hash_map *)malloc(sizeof(hash_map));
if (!pdelRange) { if (!pdelRange) {
continue; continue;
@ -467,15 +467,15 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest) {
en_addr = htonl(inet_addr(end)); en_addr = htonl(inet_addr(end));
HASH_FIND_INT(delMap, &st_addr, delItem); HASH_FIND_INT(delMap, &st_addr, delItem);
if (delItem == nullptr){ if (delItem == nullptr) {
hash_map *s = (struct hash_map*)malloc(sizeof(struct hash_map)); hash_map *s = (struct hash_map *)malloc(sizeof(struct hash_map));
s->key = st_addr; s->key = st_addr;
s->value = en_addr; s->value = en_addr;
HASH_ADD_INT(delMap, key, s); HASH_ADD_INT(delMap, key, s);
} }
} }
for(int i = 0; i < cfig.rangeCount; i++){ for (int i = 0; i < cfig.rangeCount; i++) {
cJSON *pdel_Item = cJSON_CreateObject(); cJSON *pdel_Item = cJSON_CreateObject();
hash_map *delRange; hash_map *delRange;
char del_range[256]; char del_range[256];
@ -484,15 +484,15 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest) {
memset(del_range, 0, 256); memset(del_range, 0, 256);
HASH_FIND_INT(delMap, &cfig.dhcpRanges[i].rangeStart, delRange); HASH_FIND_INT(delMap, &cfig.dhcpRanges[i].rangeStart, delRange);
if(delRange != nullptr){ if (delRange != nullptr) {
IP2String(saddr, ntohl(delRange->key)); IP2String(saddr, ntohl(delRange->key));
IP2String(eaddr, ntohl(delRange->value)); IP2String(eaddr, ntohl(delRange->value));
sprintf(del_range, "%s-%s", saddr, eaddr); sprintf(del_range, "%s-%s", saddr, eaddr);
cJSON_AddStringToObject(pdel_Item, "dhcpRange", del_range); cJSON_AddStringToObject(pdel_Item, "dhcpRange", del_range);
//judge whether the input has error //judge whether the input has error
if(delRange->key && delRange->value && if (delRange->key && delRange->value && (delRange->key == cfig.dhcpRanges[i].rangeStart) &&
(delRange->key == cfig.dhcpRanges[i].rangeStart) && (delRange->value == cfig.dhcpRanges[i].rangeEnd)){ (delRange->value == cfig.dhcpRanges[i].rangeEnd)) {
cJSON_AddNumberToObject(pdel_Item, "status", ERR_SUCCESS); cJSON_AddNumberToObject(pdel_Item, "status", ERR_SUCCESS);
cJSON_AddStringToObject(pdel_Item, "message", getErrorEnumDesc(ERR_SUCCESS)); cJSON_AddStringToObject(pdel_Item, "message", getErrorEnumDesc(ERR_SUCCESS));
cJSON_AddItemToArray(pdelArray, pdel_Item); cJSON_AddItemToArray(pdelArray, pdel_Item);
@ -512,8 +512,8 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest) {
} }
//rewite cfig.dhcpRanges //rewite cfig.dhcpRanges
for(int i = 0; i < cfig.rangeCount; i++){ for (int i = 0; i < cfig.rangeCount; i++) {
if(i < resCount){ if (i < resCount) {
memcpy(&cfig.dhcpRanges[i], &dhcpRanges[i], sizeof(struct data13)); memcpy(&cfig.dhcpRanges[i], &dhcpRanges[i], sizeof(struct data13));
} else { } else {
memset(&cfig.dhcpRanges[i], 0, sizeof(struct data13)); memset(&cfig.dhcpRanges[i], 0, sizeof(struct data13));
@ -532,14 +532,13 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest) {
return ERR_SUCCESS; return ERR_SUCCESS;
} }
static void revert(unsigned int* num) { static void revert(unsigned int *num) {
unsigned int v = *num ; unsigned int v = *num;
v = ((v & 0x000000FF) << 24) | ((v & 0x0000FF00) << 8) | v = ((v & 0x000000FF) << 24) | ((v & 0x0000FF00) << 8) | ((v & 0x00FF0000) >> 8) | ((v & 0xFF000000) >> 24);
((v & 0x00FF0000) >> 8) | ((v & 0xFF000000) >> 24) ; *num = v;
*num = v ;
} }
static int query_dhcpd_rangeset(data19 *req){ static int query_dhcpd_rangeset(data19 *req) {
char logBuff[512]; char logBuff[512];
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);
@ -557,7 +556,7 @@ static int query_dhcpd_rangeset(data19 *req){
cJSON *pMsgArray = cJSON_CreateArray(); cJSON *pMsgArray = cJSON_CreateArray();
cJSON_AddItemToObject(pRspMsg, "rangeSet", pMsgArray); cJSON_AddItemToObject(pRspMsg, "rangeSet", pMsgArray);
for (char rangeInd = 0; rangeInd < cfig.rangeCount; rangeInd++){ for (char rangeInd = 0; rangeInd < cfig.rangeCount; rangeInd++) {
char addrStart[64]; char addrStart[64];
char addrEnd[64]; char addrEnd[64];
char rangeSet[128]; char rangeSet[128];
@ -567,7 +566,7 @@ static int query_dhcpd_rangeset(data19 *req){
cJSON *pRangeItem = cJSON_CreateObject(); cJSON *pRangeItem = cJSON_CreateObject();
unsigned int lease; unsigned int lease;
memset(domainServer, 0 , 128); memset(domainServer, 0, 128);
IP2String(addrStart, ntohl(cfig.dhcpRanges[rangeInd].rangeStart)); IP2String(addrStart, ntohl(cfig.dhcpRanges[rangeInd].rangeStart));
IP2String(addrEnd, ntohl(cfig.dhcpRanges[rangeInd].rangeEnd)); IP2String(addrEnd, ntohl(cfig.dhcpRanges[rangeInd].rangeEnd));
IP2String(rangeMask, cfig.dhcpRanges[rangeInd].mask); IP2String(rangeMask, cfig.dhcpRanges[rangeInd].mask);
@ -592,28 +591,28 @@ static int query_dhcpd_rangeset(data19 *req){
opPointer++; opPointer++;
memcpy(op.value, opPointer, op.size); memcpy(op.value, opPointer, op.size);
if(op.opt_code == DHCP_OPTION_DNS){ if (op.opt_code == DHCP_OPTION_DNS) {
dnsSize = op.size; dnsSize = op.size;
do { do {
tmpVal = fIP(op.value + offset); tmpVal = fIP(op.value + offset);
revert(&tmpVal); revert(&tmpVal);
IP2String(dns_op, ntohl(tmpVal)); IP2String(dns_op, ntohl(tmpVal));
sprintf(domainServer, "%s%s", domainServer, dns_op); sprintf(domainServer, "%s%s", domainServer, dns_op);
if(dnsSize != 4){ if (dnsSize != 4) {
sprintf(domainServer, "%s,", domainServer); sprintf(domainServer, "%s,", domainServer);
} }
dnsSize -= 4; dnsSize -= 4;
offset = op.size - dnsSize; offset = op.size - dnsSize;
}while (dnsSize != 0); } while (dnsSize != 0);
cJSON_AddStringToObject(pRangeItem, "domainServer", domainServer); cJSON_AddStringToObject(pRangeItem, "domainServer", domainServer);
}else if(op.opt_code == DHCP_OPTION_ROUTER){ } else if (op.opt_code == DHCP_OPTION_ROUTER) {
tmpVal = fIP(op.value); tmpVal = fIP(op.value);
revert(&tmpVal); revert(&tmpVal);
IP2String(gateway, ntohl(tmpVal)); IP2String(gateway, ntohl(tmpVal));
cJSON_AddStringToObject(pRangeItem, "gateway", gateway); cJSON_AddStringToObject(pRangeItem, "gateway", gateway);
}else if(op.opt_code == DHCP_OPTION_IPADDRLEASE){ } else if (op.opt_code == DHCP_OPTION_IPADDRLEASE) {
lease = fUInt(op.value); lease = fUInt(op.value);
cJSON_AddNumberToObject(pRangeItem, "lease", lease); cJSON_AddNumberToObject(pRangeItem, "lease", lease);
@ -638,6 +637,7 @@ static int query_dhcpd_rangeset(data19 *req){
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma ide diagnostic ignored "cert-err34-c" #pragma ide diagnostic ignored "cert-err34-c"
int getHwAddr(char *buff, char *mac) { int getHwAddr(char *buff, char *mac) {
if (buff == nullptr || mac == nullptr) { if (buff == nullptr || mac == nullptr) {
return -1; return -1;
@ -656,6 +656,7 @@ int getHwAddr(char *buff, char *mac) {
return 0; return 0;
} }
#pragma clang diagnostic pop #pragma clang diagnostic pop
int arpSet(const char *ifname, char *ipStr, char *mac) { int arpSet(const char *ifname, char *ipStr, char *mac) {
@ -831,7 +832,6 @@ static void opendhcp_http_get_alluser(http_request *request, hw_http_response *r
hw_string keep_alive_name; hw_string keep_alive_name;
hw_string keep_alive_value; hw_string keep_alive_value;
int ret; int ret;
auto *req = (data19 *)malloc(sizeof(struct data19)); auto *req = (data19 *)malloc(sizeof(struct data19));
if (req == nullptr) { if (req == nullptr) {
@ -879,16 +879,15 @@ static void opendhcp_http_add_rangeset(http_request *request, hw_http_response *
hw_string keep_alive_name; hw_string keep_alive_name;
hw_string keep_alive_value; hw_string keep_alive_value;
int ret; int ret;
auto *req = (data19 *)malloc(sizeof(struct data19)); auto *req = (data19 *)malloc(sizeof(struct data19));
if (req == nullptr) { if (req == nullptr) {
hw_http_response_send_error(response, HTTP_STATUS_500, "memory error"); proto_response_error(response, 500, HTTP_STATUS_500, ERR_MALLOC_MEMORY);
return; return;
} }
if (request->method != HW_HTTP_POST) { if (request->method != HW_HTTP_POST) {
hw_http_response_send_error(response, HTTP_STATUS_405, HTTP_STATUS_405); proto_response_error(response, 405, HTTP_STATUS_405, ERR_HTTP_UNSUP_METHOD);
return; return;
} }
@ -927,16 +926,15 @@ static void opendhcp_http_delete_rangeset(http_request *request, hw_http_respons
hw_string keep_alive_name; hw_string keep_alive_name;
hw_string keep_alive_value; hw_string keep_alive_value;
int ret; int ret;
auto *req = (data19 *)malloc(sizeof(struct data19)); auto *req = (data19 *)malloc(sizeof(struct data19));
if (req == nullptr) { if (req == nullptr) {
hw_http_response_send_error(response, HTTP_STATUS_500, "memory error"); proto_response_error(response, 500, HTTP_STATUS_500, ERR_MALLOC_MEMORY);
return; return;
} }
if (request->method != HW_HTTP_POST) { if (request->method != HW_HTTP_POST) {
hw_http_response_send_error(response, HTTP_STATUS_405, HTTP_STATUS_405); proto_response_error(response, 405, HTTP_STATUS_405, ERR_HTTP_UNSUP_METHOD);
return; return;
} }
@ -975,16 +973,15 @@ static void opendhcp_http_query_rangeset(http_request *request, hw_http_response
hw_string keep_alive_name; hw_string keep_alive_name;
hw_string keep_alive_value; hw_string keep_alive_value;
int ret; int ret;
auto *req = (data19 *)malloc(sizeof(struct data19)); auto *req = (data19 *)malloc(sizeof(struct data19));
if (req == nullptr) { if (req == nullptr) {
hw_http_response_send_error(response, HTTP_STATUS_500, "memory error"); proto_response_error(response, 500, HTTP_STATUS_500, ERR_MALLOC_MEMORY);
return; return;
} }
if (request->method != HW_HTTP_GET) { if (request->method != HW_HTTP_GET) {
hw_http_response_send_error(response, HTTP_STATUS_405, HTTP_STATUS_405); proto_response_error(response, 405, HTTP_STATUS_405, ERR_HTTP_UNSUP_METHOD);
return; return;
} }
@ -1014,6 +1011,7 @@ static void opendhcp_http_query_rangeset(http_request *request, hw_http_response
hw_http_response_send(response, req, response_complete); hw_http_response_send(response, req, response_complete);
} }
/** /**
* *
* @return * @return