OCT 1.增加JSON字段规范格式检查

2.简化删除地址池接口代码
This commit is contained in:
dongwenze 2023-03-13 15:46:31 +08:00
parent 701d9ac1f0
commit 05838baa80
3 changed files with 60 additions and 19 deletions

View File

@ -18,8 +18,14 @@ typedef enum {
CRYPTO_AES256 = 4, CRYPTO_AES256 = 4,
} PROTO_CRYPTO_TYPE; } PROTO_CRYPTO_TYPE;
typedef enum {
TYPE_ADD_RNG = 0,
TYPE_DEL_RNG = 1,
TYPE_QUE_USR = 2,
} PROTO_POST_TYPE;
const char *proto_create_new(cJSON *pMsgCtx, int httpCode); const char *proto_create_new(cJSON *pMsgCtx, int httpCode);
const char *proto_decode_context(const char *pString, unsigned int *pVer, unsigned long long *pTm, int *pErrCode); const char *proto_decode_context(const char *pString, unsigned int *pVer, unsigned long long *pTm, PROTO_POST_TYPE type_id, int *pErrCode);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -36,6 +36,12 @@ static JSON_SCHEMA_CTX g_json_sch[] = {
{"{\"type\":\"object\",\"required\":[\"msgContent\"]}", "Missing required field [msgContent]"}, {"{\"type\":\"object\",\"required\":[\"msgContent\"]}", "Missing required field [msgContent]"},
}; };
static JSON_SCHEMA_CTX g_json_msg[] = {
{"{\"type\":\"object\",\"required\":[\"rangeSet\"],\"properties\":{\"rangeSet\":{\"type\":\"array\",\"minItems\":1,\"items\":{\"type\":\"object\",\"required\":[\"dhcpRange\"]}}}}", "Incorrect [rangeSet]"},
{"{\"type\":\"object\",\"required\":[\"dhcpRange\"],\"properties\":{\"dhcpRange\":{\"type\":\"array\",\"minItems\":1}}}", "Incorrect [dhcpRange]"},
{"{\"type\":\"object\",\"required\":[\"userMac\"],\"properties\":{\"userMac\":{\"type\":\"array\",\"minItems\":1}}}", "Incorrect [userMac]"},
};
const char *proto_schema_validation(const char *pJsonStr) { const char *proto_schema_validation(const char *pJsonStr) {
int i; int i;
@ -72,10 +78,39 @@ const char *proto_schema_validation(const char *pJsonStr) {
json_object_put(pJs); json_object_put(pJs);
return NULL; return NULL;
} }
const char *proto_msg_validation(const char *pJsonStr, PROTO_POST_TYPE type_id) {
json_object *pJs = json_tokener_parse(pJsonStr);
if (!pJs) {
cJSON *pRspRoot = cJSON_CreateObject();
cJSON_AddNumberToObject(pRspRoot, "status", ERR_JSON_PRASE_OBJ);
cJSON_AddStringToObject(pRspRoot, "message", getErrorEnumDesc(ERR_JSON_PRASE_OBJ));
return proto_create_new(pRspRoot, 200);
}
json_object *pSc = json_tokener_parse(g_json_msg[type_id].pSchJson);
if (!pSc) {
LOG_MOD(error, ZLOG_MOD_PROTO, "Json schema format error: [%s]\n", g_json_msg[type_id].pSchJson);
}
if (jdac_validate(pJs, pSc) != JDAC_ERR_VALID) {
cJSON *pRspRoot = cJSON_CreateObject();
cJSON_AddNumberToObject(pRspRoot, "status", ERR_JSON_VALID_SCH);
cJSON_AddStringToObject(pRspRoot, "message", getErrorEnumDesc(ERR_JSON_VALID_SCH));
cJSON_AddStringToObject(pRspRoot, "details", g_json_msg[type_id].pErrMsg);
json_object_put(pSc);
json_object_put(pJs);
return proto_create_new(pRspRoot, 200);
}
json_object_put(pSc);
json_object_put(pJs);
return NULL;
}
#endif #endif
const char *proto_decode_context(const char *pString, unsigned int *pVer, unsigned long long *pTm, int *pErrCode) { const char *proto_decode_context(const char *pString, unsigned int *pVer,
cJSON *pMsgCtx; unsigned long long *pTm, PROTO_POST_TYPE type_id, int *pErrCode) {
unsigned char *pBase64; unsigned char *pBase64;
int decodeSize; int decodeSize;
unsigned int outSize = 0; unsigned int outSize = 0;
@ -132,7 +167,19 @@ const char *proto_decode_context(const char *pString, unsigned int *pVer, unsign
} }
} }
pMsgCtx = cJSON_GetObjectItem(pRoot, "msgContent"); cJSON *pMsgCtx = cJSON_GetObjectItem(pRoot, "msgContent");
char *msgCont = cJSON_Print(pMsgCtx);
#ifdef JSON_SCHEMA_ON
pSchJson = proto_msg_validation(msgCont, type_id);
free((void *)msgCont);
if (pSchJson != NULL && strlen(pSchJson) > 0) {
*pErrCode = ERR_JSON_VALID_SCH;
cJSON_Delete(pRoot);
return pSchJson;
}
#endif
if (!pMsgCtx) { if (!pMsgCtx) {
cJSON_Delete(pRoot); cJSON_Delete(pRoot);

View File

@ -56,7 +56,7 @@ static int dhcp_get_user_info(const char **pRsp, const char *pRequest) {
return ERR_INPUT_PARAMS; return ERR_INPUT_PARAMS;
} }
pStrContent = proto_decode_context(pRequest, nullptr, nullptr, &errCode); pStrContent = proto_decode_context(pRequest, nullptr, nullptr, TYPE_QUE_USR, &errCode);
if (pStrContent == nullptr) { if (pStrContent == nullptr) {
sprintf(logBuff, "Requeset Json error %s", pRequest); sprintf(logBuff, "Requeset Json error %s", pRequest);
@ -334,7 +334,7 @@ static int add_dhcpd_rangeset(const char **pRsp, const char *pRequest) {
return ERR_INPUT_PARAMS; return ERR_INPUT_PARAMS;
} }
pStrContent = proto_decode_context(pRequest, nullptr, nullptr, &errCode); pStrContent = proto_decode_context(pRequest, nullptr, nullptr, TYPE_ADD_RNG, &errCode);
if (pStrContent == nullptr) { if (pStrContent == nullptr) {
sprintf(logBuff, "Requeset Json error %s", pRequest); sprintf(logBuff, "Requeset Json error %s", pRequest);
@ -441,7 +441,7 @@ static int delete_dhcpd_rangeset(const char **pRsp, const char *pRequest) {
return ERR_INPUT_PARAMS; return ERR_INPUT_PARAMS;
} }
pStrContent = proto_decode_context(pRequest, nullptr, nullptr, &errCode); pStrContent = proto_decode_context(pRequest, nullptr, nullptr, TYPE_DEL_RNG, &errCode);
if (pStrContent == nullptr) { if (pStrContent == nullptr) {
sprintf(logBuff, "Requeset Json error %s", pRequest); sprintf(logBuff, "Requeset Json error %s", pRequest);
logDHCPMess(logBuff, 1); logDHCPMess(logBuff, 1);
@ -471,7 +471,6 @@ static int delete_dhcpd_rangeset(const char **pRsp, const char *pRequest) {
char end[128]; char end[128];
char del_range[256]; char del_range[256];
for (int i = 0; i < cJSON_GetArraySize(pdhcp_range); i++) { for (int i = 0; i < cJSON_GetArraySize(pdhcp_range); i++) {
cJSON *pdel_Item = cJSON_CreateObject();
cJSON *pdelRange = cJSON_GetArrayItem(pdhcp_range, i); cJSON *pdelRange = cJSON_GetArrayItem(pdhcp_range, i);
if (!pdelRange) { if (!pdelRange) {
@ -486,16 +485,6 @@ static int delete_dhcpd_rangeset(const char **pRsp, const char *pRequest) {
st_addr = htonl(inet_addr(start)); st_addr = htonl(inet_addr(start));
en_addr = htonl(inet_addr(end)); en_addr = htonl(inet_addr(end));
if (st_addr > en_addr) {
cJSON_AddStringToObject(pdel_Item, "dhcpRange", del_range);
cJSON_AddNumberToObject(pdel_Item, "status", ERR_INPUT_PARAMS);
cJSON_AddStringToObject(pdel_Item, "message", getErrorEnumDesc(ERR_INPUT_PARAMS));
cJSON_AddItemToArray(pdelArray, pdel_Item);
break;
} else {
cJSON_Delete(pdel_Item);
}
PHASH_MAP s; PHASH_MAP s;
HASH_FIND_INT(delMap, &st_addr, s); HASH_FIND_INT(delMap, &st_addr, s);
if (s == nullptr) { if (s == nullptr) {
@ -527,7 +516,6 @@ static int delete_dhcpd_rangeset(const char **pRsp, const char *pRequest) {
free(cfig.dhcpRanges[i].options); free(cfig.dhcpRanges[i].options);
free(cfig.dhcpRanges[i].expiry); free(cfig.dhcpRanges[i].expiry);
free(cfig.dhcpRanges[i].dhcpEntry); free(cfig.dhcpRanges[i].dhcpEntry);
free(*(cfig.dhcpRanges[i].dhcpEntry));
} else { } else {
memcpy(&dhcpRanges[resCount], &cfig.dhcpRanges[i], sizeof(struct data13)); memcpy(&dhcpRanges[resCount], &cfig.dhcpRanges[i], sizeof(struct data13));
resCount++; resCount++;