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);