From 05838baa80f918dad91dca6c51ec345ade9ab36f Mon Sep 17 00:00:00 2001 From: dongwenze Date: Mon, 13 Mar 2023 15:46:31 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=E5=A2=9E=E5=8A=A0JSON=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E8=A7=84=E8=8C=83=E6=A0=BC=E5=BC=8F=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=202.=E7=AE=80=E5=8C=96=E5=88=A0=E9=99=A4=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E6=B1=A0=E6=8E=A5=E5=8F=A3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/libs/include/proto.h | 8 +++++- srcs/libs/protocol/protocol.c | 53 +++++++++++++++++++++++++++++++++-- srcs/opendhcp183/query.cpp | 18 ++---------- 3 files changed, 60 insertions(+), 19 deletions(-) diff --git a/srcs/libs/include/proto.h b/srcs/libs/include/proto.h index 4291ef1..017dc6b 100644 --- a/srcs/libs/include/proto.h +++ b/srcs/libs/include/proto.h @@ -18,8 +18,14 @@ typedef enum { CRYPTO_AES256 = 4, } 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_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 } #endif diff --git a/srcs/libs/protocol/protocol.c b/srcs/libs/protocol/protocol.c index eafda62..0d305ce 100644 --- a/srcs/libs/protocol/protocol.c +++ b/srcs/libs/protocol/protocol.c @@ -36,6 +36,12 @@ static JSON_SCHEMA_CTX g_json_sch[] = { {"{\"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) { int i; @@ -72,10 +78,39 @@ const char *proto_schema_validation(const char *pJsonStr) { json_object_put(pJs); 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 -const char *proto_decode_context(const char *pString, unsigned int *pVer, unsigned long long *pTm, int *pErrCode) { - cJSON *pMsgCtx; +const char *proto_decode_context(const char *pString, unsigned int *pVer, + unsigned long long *pTm, PROTO_POST_TYPE type_id, int *pErrCode) { unsigned char *pBase64; int decodeSize; 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) { cJSON_Delete(pRoot); diff --git a/srcs/opendhcp183/query.cpp b/srcs/opendhcp183/query.cpp index 20b1087..4814432 100644 --- a/srcs/opendhcp183/query.cpp +++ b/srcs/opendhcp183/query.cpp @@ -56,7 +56,7 @@ static int dhcp_get_user_info(const char **pRsp, const char *pRequest) { 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) { 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; } - pStrContent = proto_decode_context(pRequest, nullptr, nullptr, &errCode); + pStrContent = proto_decode_context(pRequest, nullptr, nullptr, TYPE_ADD_RNG, &errCode); if (pStrContent == nullptr) { 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; } - pStrContent = proto_decode_context(pRequest, nullptr, nullptr, &errCode); + pStrContent = proto_decode_context(pRequest, nullptr, nullptr, TYPE_DEL_RNG, &errCode); if (pStrContent == nullptr) { sprintf(logBuff, "Requeset Json error %s", pRequest); logDHCPMess(logBuff, 1); @@ -471,7 +471,6 @@ static int delete_dhcpd_rangeset(const char **pRsp, const char *pRequest) { char end[128]; char del_range[256]; for (int i = 0; i < cJSON_GetArraySize(pdhcp_range); i++) { - cJSON *pdel_Item = cJSON_CreateObject(); cJSON *pdelRange = cJSON_GetArrayItem(pdhcp_range, i); if (!pdelRange) { @@ -486,16 +485,6 @@ static int delete_dhcpd_rangeset(const char **pRsp, const char *pRequest) { st_addr = htonl(inet_addr(start)); 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; HASH_FIND_INT(delMap, &st_addr, s); 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].expiry); free(cfig.dhcpRanges[i].dhcpEntry); - free(*(cfig.dhcpRanges[i].dhcpEntry)); } else { memcpy(&dhcpRanges[resCount], &cfig.dhcpRanges[i], sizeof(struct data13)); resCount++;