From af6632c3b5179855f4c83c96a1641643149971b0 Mon Sep 17 00:00:00 2001 From: huangxin Date: Mon, 16 Jan 2023 09:34:49 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E6=8E=A5=E5=8F=A3=E6=94=AF=E6=8C=81=E7=A9=BA?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=9F=A5=E8=AF=A2=E6=89=80=E6=9C=89=E4=B8=8B?= =?UTF-8?q?=E6=8C=82=E8=AE=BE=E5=A4=87=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/open_dhcp/query.cpp | 67 ++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/srcs/open_dhcp/query.cpp b/srcs/open_dhcp/query.cpp index eaeba6b..bc4f499 100644 --- a/srcs/open_dhcp/query.cpp +++ b/srcs/open_dhcp/query.cpp @@ -48,6 +48,7 @@ static uv_rwlock_t g_uvCacheLock; static int dhcp_get_user_info(data19 *req, const char *pRequest) { char logBuff[512]; const char *pStrContent; + int k; dhcpMap::iterator p; dzlog_debug("Input: %s\n", pRequest); @@ -89,27 +90,26 @@ static int dhcp_get_user_info(data19 *req, const char *pRequest) { return ERR_JSON_PRASE_OBJ; } - // cJSON *pRspRoot = cJSON_CreateObject(); - // cJSON_AddNumberToObject(pRspRoot, "version", 3); - // cJSON_AddNumberToObject(pRspRoot, "cryptoType", 0); - // cJSON_AddNumberToObject(pRspRoot, "timeStamp", (unsigned int)time(nullptr)); - cJSON *pRspMsg = cJSON_CreateObject(); cJSON *pMsgArray = cJSON_CreateArray(); cJSON_AddItemToObject(pRspMsg, "userInfo", pMsgArray); - for (int i = 0; i < cJSON_GetArraySize(pUserMac); i++) { - char tempbuff[512]; - cJSON *pItem = cJSON_GetArrayItem(pUserMac, i); - cJSON *pRspItem = cJSON_CreateObject(); + k = cJSON_GetArraySize(pUserMac); - p = dhcpCache.find(pItem->valuestring); - cJSON_AddStringToObject(pRspItem, "userMac", pItem->valuestring); - - if (p != dhcpCache.end()) { + // 传入数据 userMac 为空数组时,返回所有用户数据 + if (k == 0) { + for (p = dhcpCache.begin(); p != dhcpCache.end(); p++) { + char tempbuff[512]; data7 *dhcpEntry = p->second; + if (!dhcpEntry) { + continue; + } + + cJSON *pRspItem = cJSON_CreateObject(); + cJSON_AddStringToObject(pRspItem, "userMac", dhcpEntry->mapname); + cJSON_AddStringToObject(pRspItem, "ip", IP2String(tempbuff, dhcpEntry->ip)); cJSON_AddStringToObject(pRspItem, "hostname", dhcpEntry->hostname); if (dhcpEntry->display && dhcpEntry->expiry >= t) { @@ -121,12 +121,39 @@ static int dhcp_get_user_info(data19 *req, const char *pRequest) { } cJSON_AddNumberToObject(pRspItem, "status", ERR_SUCCESS); cJSON_AddStringToObject(pRspItem, "message", getErrorEnumDesc(ERR_SUCCESS)); - } else { - cJSON_AddNumberToObject(pRspItem, "status", ERR_ITEM_UNEXISTS); - cJSON_AddStringToObject(pRspItem, "message", getErrorEnumDesc(ERR_ITEM_UNEXISTS)); - } - cJSON_AddItemToArray(pMsgArray, pRspItem); + cJSON_AddItemToArray(pMsgArray, pRspItem); + } + } else { + for (int i = 0; i < k; i++) { + char tempbuff[512]; + cJSON *pItem = cJSON_GetArrayItem(pUserMac, i); + cJSON *pRspItem = cJSON_CreateObject(); + + p = dhcpCache.find(pItem->valuestring); + cJSON_AddStringToObject(pRspItem, "userMac", pItem->valuestring); + + if (p != dhcpCache.end()) { + data7 *dhcpEntry = p->second; + + cJSON_AddStringToObject(pRspItem, "ip", IP2String(tempbuff, dhcpEntry->ip)); + cJSON_AddStringToObject(pRspItem, "hostname", dhcpEntry->hostname); + if (dhcpEntry->display && dhcpEntry->expiry >= t) { + tm *ttm = localtime(&dhcpEntry->expiry); + strftime(tempbuff, sizeof(tempbuff), "%d-%b-%y %X", ttm); + cJSON_AddStringToObject(pRspItem, "leaseExpiry", tempbuff); + } else { + cJSON_AddStringToObject(pRspItem, "leaseExpiry", "Expiry"); + } + cJSON_AddNumberToObject(pRspItem, "status", ERR_SUCCESS); + cJSON_AddStringToObject(pRspItem, "message", getErrorEnumDesc(ERR_SUCCESS)); + } else { + cJSON_AddNumberToObject(pRspItem, "status", ERR_ITEM_UNEXISTS); + cJSON_AddStringToObject(pRspItem, "message", getErrorEnumDesc(ERR_ITEM_UNEXISTS)); + } + + cJSON_AddItemToArray(pMsgArray, pRspItem); + } } const char *pStrPro = proto_create_new(pRspMsg, 200); @@ -1314,9 +1341,9 @@ int process_iptv_multicast(const unsigned char *p, int size, const char *mac) { strcpy(pDev->iptvMAC, mac); #ifdef USER_VNI - pDev->vni = cfg_get_user_vni_id(); + pDev->vni = cfg_get_user_vni_id(); #else - pDev->vni = 0; + pDev->vni = 0; #endif pDev->isReport = 0; uv_rwlock_wrlock(&g_uvCacheLock);