Mod aaa-12 增加搜索、详细查询功能
RCA: SOL: 修改人:huangxin 检视人:huangxin
This commit is contained in:
parent
a310af034b
commit
0bccd072ef
File diff suppressed because it is too large
Load Diff
|
@ -11,8 +11,9 @@
|
|||
|
||||
#define MAX_NAME_LEN (32 * 4)
|
||||
#define MAX_SESSION (32 + 1)
|
||||
#define MAX_PATH (256)
|
||||
#define MAX_PAGE_ITEMS (10)
|
||||
#define MAX_PATH (256)
|
||||
#define MAX_PAGE_ITEMS (100)
|
||||
#define MAX_REGEX_LEN (4096)
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -747,7 +747,6 @@ static const char *__obj_detail_encode(void *pData)
|
|||
continue;
|
||||
}
|
||||
|
||||
pObjArray = cJSON_AddArrayToObject(pItem, "objContent");
|
||||
cJSON_AddStringToObject(pItem, "name", p->data[i].name);
|
||||
cJSON_AddNumberToObject(pItem, "retCode", p->data[i].ret_code);
|
||||
|
||||
|
@ -762,6 +761,7 @@ static const char *__obj_detail_encode(void *pData)
|
|||
cJSON_AddNumberToObject(pItem, "type", p->data[i].pObjK->type);
|
||||
cJSON_AddNumberToObject(pItem, "ref_count", p->data[i].pObjK->ref_count);
|
||||
cJSON_AddNumberToObject(pItem, "prio", p->data[i].prio);
|
||||
pObjArray = cJSON_AddArrayToObject(pItem, "objContent");
|
||||
|
||||
if(pObjArray) {
|
||||
int j;
|
||||
|
@ -779,6 +779,7 @@ static const char *__obj_detail_encode(void *pData)
|
|||
switch(p->data[i].pObjK->type / 10) {
|
||||
case OBJ_TYPE_SERVER:
|
||||
cJSON_AddNumberToObject(pObjItem, "proType", pSvr->pro_type);
|
||||
|
||||
if(pSvr->str_val && strlen(pSvr->str_val) > 0) {
|
||||
cJSON_AddStringToObject(pObjItem, "porPort", pSvr->str_val);
|
||||
}
|
||||
|
@ -786,7 +787,7 @@ static const char *__obj_detail_encode(void *pData)
|
|||
break;
|
||||
|
||||
case OBJ_TYPE_ADDR:
|
||||
if(pAddr->str_val && strlen(pDt->str_val) > 0) {
|
||||
if(pAddr->str_val && strlen(pAddr->str_val) > 0) {
|
||||
cJSON_AddStringToObject(pObjItem, "ipAddr", pAddr->str_val);
|
||||
}
|
||||
|
||||
|
@ -794,6 +795,7 @@ static const char *__obj_detail_encode(void *pData)
|
|||
|
||||
case OBJ_TYPE_DATETIME:
|
||||
cJSON_AddNumberToObject(pObjItem, "repType", pDt->rep_mode);
|
||||
|
||||
if(pDt->str_val && strlen(pDt->str_val) > 0) {
|
||||
cJSON_AddStringToObject(pObjItem, "timeValue", pDt->str_val);
|
||||
}
|
||||
|
@ -859,6 +861,99 @@ static int __obj_detail_decode(const char *pJsonS, void **pStruct)
|
|||
return RET_OK;
|
||||
}
|
||||
|
||||
static int __obj_search_decode(const char *pJsonS, void **pStruct)
|
||||
{
|
||||
cJSON *pRoot, *pVal;
|
||||
PIFACE_SEARCH_OBJ pData;
|
||||
int arraySize;
|
||||
int i;
|
||||
*pStruct = malloc(sizeof(IFACE_SEARCH_OBJ));
|
||||
|
||||
if(*pStruct == NULL) {
|
||||
return -RET_NOMEM;
|
||||
}
|
||||
|
||||
pData = (PIFACE_SEARCH_OBJ) * pStruct;
|
||||
pRoot = cJSON_Parse(pJsonS);
|
||||
|
||||
if(!pRoot) {
|
||||
return -RET_SYSERR;
|
||||
}
|
||||
|
||||
pVal = cJSON_GetObjectItem(pRoot, "key");
|
||||
|
||||
if(cJSON_IsString(pVal)) {
|
||||
strncpy(pData->key, pVal->valuestring, MAX_REGEX_LEN - 1);
|
||||
}
|
||||
|
||||
pVal = cJSON_GetObjectItem(pRoot, "type");
|
||||
|
||||
if(cJSON_IsNumber(pVal)) {
|
||||
pData->type = pVal->valueint;
|
||||
}
|
||||
|
||||
pVal = cJSON_GetObjectItem(pRoot, "regex");
|
||||
|
||||
if(cJSON_IsNumber(pVal)) {
|
||||
pData->regex = pVal->valueint;
|
||||
}
|
||||
|
||||
pVal = cJSON_GetObjectItem(pRoot, "maxItems");
|
||||
|
||||
if(cJSON_IsNumber(pVal)) {
|
||||
pData->maxItems = pVal->valueint;
|
||||
}
|
||||
|
||||
cJSON_Delete(pRoot);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
static int __obj_search_encode(const char *pJsonS, void **pStruct)
|
||||
{
|
||||
cJSON *pRoot, *pVal;
|
||||
PIFACE_SEARCH_LIST pData;
|
||||
*pStruct = malloc(sizeof(IFACE_SEARCH_LIST));
|
||||
|
||||
if(*pStruct == NULL) {
|
||||
return -RET_NOMEM;
|
||||
}
|
||||
|
||||
pData = (PIFACE_SEARCH_LIST) * pStruct;
|
||||
pRoot = cJSON_Parse(pJsonS);
|
||||
|
||||
if(!pRoot) {
|
||||
return -RET_SYSERR;
|
||||
}
|
||||
|
||||
memset(pData, 0, sizeof(IFACE_SEARCH_LIST));
|
||||
pVal = cJSON_GetObjectItem(pRoot, "type");
|
||||
|
||||
if(cJSON_IsNumber(pVal)) {
|
||||
pData->type = pVal->valueint;
|
||||
}
|
||||
|
||||
pVal = cJSON_GetObjectItem(pRoot, "start");
|
||||
|
||||
if(cJSON_IsNumber(pVal)) {
|
||||
pData->start = pVal->valueint;
|
||||
}
|
||||
|
||||
pVal = cJSON_GetObjectItem(pRoot, "end");
|
||||
|
||||
if(cJSON_IsNumber(pVal)) {
|
||||
pData->end = pVal->valueint;
|
||||
}
|
||||
|
||||
pVal = cJSON_GetObjectItem(pRoot, "session");
|
||||
|
||||
if(cJSON_IsString(pVal)) {
|
||||
strncpy(pData->session, pVal->valuestring, MAX_SESSION - 1);
|
||||
}
|
||||
|
||||
cJSON_Delete(pRoot);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
static JSON_ENGINE g_jSonEngine[] = {
|
||||
{JE_INTERFACE, __interface_encode, __interface_decode, NULL},
|
||||
{OBJ_CMD_ADD, __obj_add_encode, __obj_add_decode, NULL},
|
||||
|
@ -866,6 +961,7 @@ static JSON_ENGINE g_jSonEngine[] = {
|
|||
{JE_OBJ_MOD, __obj_mod_encode, __obj_mod_decode, NULL},
|
||||
{JE_OBJ_QUERYLIST, __obj_query_list_encode, __obj_query_list_decode, NULL},
|
||||
{JE_OBJ_QUERYDETAIL, __obj_detail_encode, __obj_detail_decode, NULL},
|
||||
{JE_OBJ_SEARCH, __obj_search_encode, __obj_search_decode, NULL},
|
||||
{JSON_ENGINE_MAX, NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ typedef enum {
|
|||
JE_OBJ_MOD,
|
||||
JE_OBJ_QUERYLIST,
|
||||
JE_OBJ_QUERYDETAIL,
|
||||
JE_OBJ_SEARCH,
|
||||
|
||||
JSON_ENGINE_MAX,
|
||||
} JSON_ENGINE_TYPE;
|
||||
|
@ -56,7 +57,14 @@ typedef struct {
|
|||
int type;
|
||||
int start;
|
||||
int end;
|
||||
} IFACE_QUERY_LIST, *PIFACE_QUERY_LIST;
|
||||
} IFACE_QUERY_LIST, IFACE_SEARCH_LIST, *PIFACE_QUERY_LIST, *PIFACE_SEARCH_LIST;
|
||||
|
||||
typedef struct {
|
||||
char key[MAX_REGEX_LEN];
|
||||
int type;
|
||||
int regex;
|
||||
int maxItems;
|
||||
} IFACE_SEARCH_OBJ, *PIFACE_SEARCH_OBJ;
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
|
@ -72,9 +80,9 @@ typedef struct {
|
|||
} IFC_RET_MSG, *PIFC_RET_MSG;
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
char name[MAX_NAME_LEN];
|
||||
char *mesg;
|
||||
char* *desc;
|
||||
char* desc[MAX_DESC];
|
||||
int prio;
|
||||
int ret_code;
|
||||
POBJECT_K pObjK;
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"cmdId": 105,
|
||||
"ver": 1,
|
||||
"cryptoType": 0,
|
||||
"timeStamp": 1526625689,
|
||||
"msgContent": "{\"key\":\"RTP*\",\"regex\":0,\"type\":0,\"maxItems\":100}"
|
||||
}
|
|
@ -26,6 +26,7 @@
|
|||
#define MOD_JS_FILE (JS_FILE_ROOT"/secogateway/Product/user/object_manager/json_test/mod.json")
|
||||
#define QUERY_LIST_JS_FILE (JS_FILE_ROOT"/secogateway/Product/user/object_manager/json_test/page_queue.json")
|
||||
#define DETAIL_JS_FILE (JS_FILE_ROOT"/secogateway/Product/user/object_manager/json_test/detail.json")
|
||||
#define SEARCH_JS_FILE (JS_FILE_ROOT"/secogateway/Product/user/object_manager/json_test/search.json")
|
||||
|
||||
static PSPLIT_PAGE g_pSplitPage = NULL;
|
||||
static PCMHI_OBJECT g_pObject = NULL;
|
||||
|
@ -94,6 +95,7 @@ static void dump_object(void)
|
|||
LOG_EX(LOG_Info, "prio: %d\n", pObj->prio);
|
||||
LOG_EX(LOG_Info, "Tags: %s\n", g_objItem[pObj->obj_index].memTag);
|
||||
LOG_EX(LOG_Info, "type: %d\n", g_objItem[pObj->obj_index].type);
|
||||
LOG_EX(LOG_Info, "refs: %d\n", g_objItem[pObj->obj_index].ref_count);
|
||||
LOG_EX(LOG_Info, "items: %d\n", g_objItem[pObj->obj_index].ctx_num);
|
||||
|
||||
for(i = 0; i < g_objItem[pObj->obj_index].ctx_num; i++) {
|
||||
|
@ -970,8 +972,9 @@ static void test_add_object(void)
|
|||
POBJECT_K pObjKItem = malloc(sizeof(OBJECT_K));
|
||||
|
||||
if(pObjItem && pObjKItem) {
|
||||
memset(pObjKItem, 0, sizeof(OBJECT_K));
|
||||
memcpy(pObjItem, pAdd->pCtx[0].pObj, sizeof(CMHI_OBJECT));
|
||||
memcpy(pObjKItem, &pAdd->pCtx[i].objk, sizeof(OBJECT_K));
|
||||
memcpy(pObjKItem, &pAdd->pCtx[0].objk, sizeof(OBJECT_K));
|
||||
sprintf(pObjItem->name, "%s_%02d", pAdd->pCtx[0].pObj->name, i);
|
||||
pObjKItem->type = 1;
|
||||
pObjKItem->ctx_num = 1;
|
||||
|
@ -1420,6 +1423,7 @@ static void test_page_object(char *pVal)
|
|||
}
|
||||
|
||||
retCtx.n_items = 0;
|
||||
pPage->last_time = time(NULL);
|
||||
|
||||
for(i = retCtx.start; i < pQuery->end && iCount < MAX_PAGE_ITEMS && iCount < retCtx.tolItems; i++) {
|
||||
pStr = (char **)utarray_eltptr(pPage->name_list, i);
|
||||
|
@ -1439,7 +1443,6 @@ static void test_page_object(char *pVal)
|
|||
}
|
||||
|
||||
retCtx.end = retCtx.start + iCount - 1;
|
||||
|
||||
pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret);
|
||||
|
||||
if(!pRetJson || ret != RET_OK) {
|
||||
|
@ -1545,21 +1548,30 @@ static void test_detail_object(void)
|
|||
retCtx.n_items = pDetail->n_obj;
|
||||
}
|
||||
|
||||
retCtx.ret_code = RET_OK;
|
||||
|
||||
for(i = 0; i < pDetail->n_obj; i++) {
|
||||
ret = object_del(pDetail->name[i]);
|
||||
PCMHI_OBJECT pObj = NULL;
|
||||
pthread_mutex_lock(&g_obj_lock);
|
||||
HASH_FIND_STR(g_pObject, pDetail->name[i], pObj);
|
||||
pthread_mutex_unlock(&g_obj_lock);
|
||||
strncpy(retCtx.data[i].name, pDetail->name[i], MAX_NAME_LEN - 1);
|
||||
|
||||
if(ret != RET_OK && retCtx.ret_code == RET_OK) {
|
||||
retCtx.ret_code = ret;
|
||||
if(pObj) {
|
||||
retCtx.data[i].ret_code = RET_OK;
|
||||
retCtx.data[i].mesg = get_err_message(RET_OK);
|
||||
strncpy(retCtx.data[i].desc, pObj->desc, MAX_DESC - 1);
|
||||
retCtx.data[i].prio = pObj->prio;
|
||||
retCtx.data[i].pObjK = &g_objItem[pObj->obj_index];
|
||||
} else {
|
||||
retCtx.ret_code = -RET_NOTFOUND;
|
||||
retCtx.data[i].ret_code = -RET_NOTFOUND;
|
||||
retCtx.data[i].mesg = get_err_message(RET_NOTFOUND);
|
||||
}
|
||||
|
||||
retCtx.data[i].name = pDetail->name[i];
|
||||
retCtx.data[i].ret_code = ret;
|
||||
retCtx.data[i].mesg = get_err_message(ret);
|
||||
}
|
||||
|
||||
dump_object();
|
||||
retCtx.mesg = get_err_message(retCtx.ret_code);
|
||||
pRetJson = Struct2Json(&retCtx, JE_OBJ_DEL, FALSE, &ret);
|
||||
pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYDETAIL, FALSE, &ret);
|
||||
|
||||
if(!pRetJson || ret != RET_OK) {
|
||||
LOG_EX(LOG_Error, "Json format error: %d\n", ret);
|
||||
|
@ -1591,6 +1603,197 @@ static void test_detail_object(void)
|
|||
free((void *)pJson);
|
||||
}
|
||||
|
||||
static void test_search_object(char *pVal)
|
||||
{
|
||||
int i, ret, size;
|
||||
PCMHI_OBJECT pObj, pTmp;
|
||||
char **pStr;
|
||||
PSPLIT_PAGE pPage;
|
||||
char regex_str[MAX_REGEX_LEN];
|
||||
IFC_RET_PAGE_MSG retCtx;
|
||||
int pos = 0;
|
||||
const char *pRetJson = NULL;
|
||||
PIFACE_SEARCH_OBJ pSearch = NULL;
|
||||
PJSON_INTERFACE p = NULL;
|
||||
int sess_type = OBJ_TYPE_MAX;
|
||||
PSPLIT_PAGE pNew = (PSPLIT_PAGE)malloc(sizeof(SPLIT_PAGE));
|
||||
const char *pJson = read_json_file(SEARCH_JS_FILE);
|
||||
ret = Json2Struct(pJson, &p, JE_INTERFACE, FALSE);
|
||||
memset(&retCtx, 0, sizeof(IFC_RET_PAGE_MSG));
|
||||
|
||||
if(ret != RET_OK || p == NULL || pNew == NULL) {
|
||||
LOG_EX(LOG_Error, "Decode json error: %d\n", ret);
|
||||
free((void *)pJson);
|
||||
|
||||
if(p) {
|
||||
if(p->msgContent) {
|
||||
free((void *)p->msgContent);
|
||||
}
|
||||
|
||||
free(p);
|
||||
}
|
||||
|
||||
if(pNew == NULL) {
|
||||
retCtx.ret_code = -RET_NOMEM;
|
||||
} else {
|
||||
retCtx.ret_code = -RET_JSONERR;
|
||||
free(pNew);
|
||||
}
|
||||
|
||||
retCtx.mesg = get_err_message(retCtx.ret_code);
|
||||
retCtx.n_items = 0;
|
||||
pRetJson = Struct2Json(&retCtx, JE_OBJ_SEARCH, FALSE, &ret);
|
||||
|
||||
if(!pRetJson || ret != RET_OK) {
|
||||
LOG_EX(LOG_Error, "Json format error: %d\n", ret);
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson);
|
||||
free((void *)pRetJson);
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_EX(LOG_Info, "cmdId: %d\n", p->cmdId);
|
||||
LOG_EX(LOG_Info, "ver: %d\n", p->ver);
|
||||
LOG_EX(LOG_Info, "cryptoType: %d\n", p->cryptoType);
|
||||
LOG_EX(LOG_Info, "timeStamp: %d\n", p->timeStamp);
|
||||
LOG_EX(LOG_Info, "msgContent: %s\n", p->msgContent);
|
||||
ret = Json2Struct(p->msgContent, &pSearch, JE_OBJ_SEARCH, FALSE);
|
||||
|
||||
if(ret != RET_OK || pSearch == NULL) {
|
||||
LOG_EX(LOG_Error, "Decode json error: %d\n", ret);
|
||||
free(pNew);
|
||||
|
||||
if(pSearch) {
|
||||
free(pSearch);
|
||||
}
|
||||
|
||||
if(p->msgContent) {
|
||||
free((void *)p->msgContent);
|
||||
}
|
||||
|
||||
free(p);
|
||||
free((void *)pJson);
|
||||
retCtx.ret_code = -RET_JSONERR;
|
||||
retCtx.mesg = get_err_message(RET_JSONERR);
|
||||
retCtx.n_items = 0;
|
||||
pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret);
|
||||
|
||||
if(!pRetJson || ret != RET_OK) {
|
||||
LOG_EX(LOG_Error, "Json format error: %d\n", ret);
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson);
|
||||
free((void *)pRetJson);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(regex_str, 0, MAX_REGEX_LEN);
|
||||
|
||||
if(strlen(pSearch->key) == 0) {
|
||||
regex_str[0] = '.';
|
||||
regex_str[1] = '*';
|
||||
} else if(pSearch->regex) {
|
||||
strncpy(regex_str, pSearch->key, MAX_REGEX_LEN - 1);
|
||||
} else {
|
||||
for(i = 0; i < strlen(pSearch->key); i++) {
|
||||
if(pSearch->key[i] == '*') {
|
||||
regex_str[pos++] = '.';
|
||||
regex_str[pos++] = '*';
|
||||
} else {
|
||||
regex_str[pos++] = pSearch->key[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(pSearch->type == 1) {
|
||||
sess_type = OBJ_TYPE_SERVER;
|
||||
} else if(pSearch->type == 2) {
|
||||
sess_type = OBJ_TYPE_ADDR;
|
||||
} else if(pSearch->type == 3) {
|
||||
sess_type = OBJ_TYPE_DATETIME;
|
||||
}
|
||||
|
||||
retCtx.n_items = 0;
|
||||
retCtx.tolItems = 0;
|
||||
|
||||
pthread_mutex_lock(&g_obj_lock);
|
||||
HASH_ITER(hh, g_pObject, pObj, pTmp) {
|
||||
if(g_objItem[pObj->obj_index].type / 10 == sess_type
|
||||
|| sess_type == OBJ_TYPE_MAX) {
|
||||
if(pcre_match_str(regex_str, pObj->name) == TRUE) {
|
||||
char *pStr = strdup(pObj->name);
|
||||
pNew->last_time = time(NULL);
|
||||
|
||||
if(pNew->name_list == NULL) {
|
||||
utarray_new(pNew->name_list, &ut_str_icd);
|
||||
}
|
||||
|
||||
utarray_push_back(pNew->name_list, &pStr);
|
||||
retCtx.tolItems++;
|
||||
|
||||
if(retCtx.n_items < MAX_PAGE_ITEMS) {
|
||||
retCtx.data[retCtx.n_items].name = pObj->name;
|
||||
retCtx.data[retCtx.n_items].desc = pObj->desc;
|
||||
retCtx.data[retCtx.n_items].ref_count = g_objItem[pObj->obj_index].ref_count;
|
||||
retCtx.data[retCtx.n_items].type = g_objItem[pObj->obj_index].type;
|
||||
retCtx.n_items++;
|
||||
}
|
||||
|
||||
LOG_EX(LOG_Debug, "%s is match %s\n", pObj->name, regex_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&g_obj_lock);
|
||||
|
||||
if(retCtx.tolItems > MAX_PAGE_ITEMS) {
|
||||
random_string(pNew->session, MAX_SESSION - 1);
|
||||
HASH_ADD_STR(g_pSplitPage, session, pNew);
|
||||
retCtx.session = pNew->session;
|
||||
} else {
|
||||
retCtx.session = "";
|
||||
free(pNew);
|
||||
}
|
||||
|
||||
retCtx.ret_code = RET_OK;
|
||||
retCtx.mesg = get_err_message(retCtx.ret_code);
|
||||
retCtx.start = 0;
|
||||
retCtx.end = retCtx.n_items > 0 ? retCtx.n_items - 1 : 0;
|
||||
|
||||
pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret);
|
||||
|
||||
if(!pRetJson || ret != RET_OK) {
|
||||
LOG_EX(LOG_Error, "Json format error: %d\n", ret);
|
||||
|
||||
if(pRetJson) {
|
||||
free((void *)pRetJson);
|
||||
}
|
||||
|
||||
free(pSearch);
|
||||
|
||||
if(p->msgContent) {
|
||||
free((void *)p->msgContent);
|
||||
}
|
||||
|
||||
free(p);
|
||||
free((void *)pJson);
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson);
|
||||
free((void *)pRetJson);
|
||||
free(pSearch);
|
||||
|
||||
if(p->msgContent) {
|
||||
free((void *)p->msgContent);
|
||||
}
|
||||
|
||||
free(p);
|
||||
free((void *)pJson);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 应用程序主函数
|
||||
* @param argc 输入参数个数
|
||||
|
@ -1604,12 +1807,14 @@ int main(int argc, char **argv)
|
|||
{ "help", no_argument, NULL, 'h'},
|
||||
{ "version", no_argument, NULL, 'v'},
|
||||
// TODO 添加其它需要处理的参数配置
|
||||
{ "interface", no_argument, NULL, 'i'},
|
||||
{ "regex", required_argument, NULL, 'r'},
|
||||
{ "split_str", required_argument, NULL, 's'},
|
||||
{ "ip_addr", required_argument, NULL, 'p'},
|
||||
{ "str_time", required_argument, NULL, 't'},
|
||||
{ "page_split", optional_argument, NULL, 'a'},
|
||||
{ "interface", no_argument, NULL, 'a'},
|
||||
{ "regex", required_argument, NULL, 'b'},
|
||||
{ "split_str", required_argument, NULL, 'c'},
|
||||
{ "ip_addr", required_argument, NULL, 'd'},
|
||||
{ "str_time", required_argument, NULL, 'e'},
|
||||
{ "page_split", optional_argument, NULL, 'f'},
|
||||
{ "detail", optional_argument, NULL, 'g'},
|
||||
{ "search", optional_argument, NULL, 'i'},
|
||||
{NULL, 0, NULL, 0}
|
||||
};
|
||||
IHW_InitLOG("obj", NULL, TRUE);
|
||||
|
@ -1623,7 +1828,7 @@ int main(int argc, char **argv)
|
|||
LOG_EX(LOG_Debug, "SVR_OBJECT = %u bytes\n", sizeof(SVR_OBJECT) * MAX_OBJ_CONTENT);
|
||||
LOG_EX(LOG_Debug, "ADDR_OBJECT = %u bytes\n", sizeof(ADDR_OBJECT) * MAX_OBJ_CONTENT);
|
||||
|
||||
while((c = getopt_long(argc, argv, "ir:s:p:t:a::hv", long_opts, &optidx)) != -1) {
|
||||
while((c = getopt_long(argc, argv, "ab:c:d:e:f::i::ghv", long_opts, &optidx)) != -1) {
|
||||
switch(c) {
|
||||
case 'v':
|
||||
LOG_EX(LOG_Info, "User demo version: %s(%s)\n", sGATE_GIT_TAGS, sGATE_GIT_VERS);
|
||||
|
@ -1639,32 +1844,42 @@ int main(int argc, char **argv)
|
|||
break;
|
||||
|
||||
//TODO 添加其它必要处理参数过程
|
||||
case 'i':
|
||||
case 'a':
|
||||
test_add_object();
|
||||
//test_del_object();
|
||||
//test_mod_object();
|
||||
break;
|
||||
|
||||
case 's':
|
||||
test_strsep(optarg);
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
case 'b':
|
||||
test_regex(optarg);
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
case 'c':
|
||||
test_strsep(optarg);
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
test_ipaddr_format(optarg);
|
||||
break;
|
||||
|
||||
case 't':
|
||||
case 'e':
|
||||
test_str_time(optarg);
|
||||
break;
|
||||
|
||||
case 'a':
|
||||
case 'f':
|
||||
test_add_object();
|
||||
test_page_object(optarg);
|
||||
break;
|
||||
|
||||
case 'g':
|
||||
test_add_object();
|
||||
test_detail_object();
|
||||
break;
|
||||
|
||||
case 'i':
|
||||
test_add_object();
|
||||
test_search_object(optarg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,42 +4,59 @@
|
|||
#include "log.h"
|
||||
#include "regex_table.h"
|
||||
|
||||
typedef struct {
|
||||
int regId;
|
||||
pcre *re;
|
||||
char *regex;
|
||||
} REGEX_CONTEXT, *PREGEX_CONTEXT;
|
||||
|
||||
#define SVR_REGEX_STR "^(([1-9]\\d{0,3})|([1-5]\\d{4})|(6[0-4]\\d{3})|(65[0-4]\\d{2})|(655[0-2]" \
|
||||
"\\d)|(6553[0-5]))$"
|
||||
|
||||
#define IP_REGEX_STR "^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?" \
|
||||
")(\\/(\\d|[1-2]\\d|3[0-2]))?$|^([\\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\\d" \
|
||||
"|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([" \
|
||||
"0-1]\\d|2[0-8]))))?$|^::([\\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\\d|[01]" \
|
||||
"?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([0-1]" \
|
||||
"\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:):([\\da-fA-F]{1,4}:){0,3}((25[0-5]|" \
|
||||
"2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]" \
|
||||
"?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:){2}:([\\da-fA-F]{1,4}:" \
|
||||
"){0,2}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?" \
|
||||
"\\d\\d?)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:){3}:" \
|
||||
"([\\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]" \
|
||||
"|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-" \
|
||||
"fA-F]{1,4}:){4}:((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]" \
|
||||
"\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1" \
|
||||
",4}:){7}[\\da-fA-F]{1,4}(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^:((:[" \
|
||||
"\\da-fA-F]{1,4}){1,6}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^[\\da-f" \
|
||||
"A-F]{1,4}:((:[\\da-fA-F]{1,4}){1,5}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8])" \
|
||||
")))?$|^([\\da-fA-F]{1,4}:){2}((:[\\da-fA-F]{1,4}){1,4}|:)(\\/([1-9]?\\d|" \
|
||||
"(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:){3}((:[\\da-fA-F]{1,4}){1,3" \
|
||||
"}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:){4}((:[" \
|
||||
"\\da-fA-F]{1,4}){1,2}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-" \
|
||||
"fA-F]{1,4}:){5}:([\\da-fA-F]{1,4})?(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))" \
|
||||
"?$|^([\\da-fA-F]{1,4}:){6}:(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$"
|
||||
|
||||
#define DT_REGEX_STR "^(?:(?!0000)[0-9]{4}\\/(?:(?:0[1-9]|1[0-2])\\/(?:0[1-9]|1[0-9]|2[0-8])|(" \
|
||||
"?:0[13-9]|1[0-2])\\/(?:29|30)|(?:0[13578]|1[02])\\/31)|(?:[0-9]{2}(?:0[48" \
|
||||
"]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)\\/02\\/29" \
|
||||
") (20|21|22|23|[0-1][0-9]):[0-5][0-9]$|^(20|21|22|23|[0-1][0-9]):[0-5][0-" \
|
||||
"9]$"
|
||||
|
||||
#define TIME_REGEX_STR "^(20|21|22|23|[0-1][0-9]):[0-5][0-9]$"
|
||||
|
||||
const char *g_regex_tbl[REGEX_MAX] = {
|
||||
SVR_REGEX_STR,
|
||||
IP_REGEX_STR,
|
||||
DT_REGEX_STR,
|
||||
TIME_REGEX_STR,
|
||||
};
|
||||
|
||||
"^(([1-9]\\d{0,3})|([1-5]\\d{4})|(6[0-4]\\d{3})|(65[0-4]\\d{2})|(655[0-2]"
|
||||
"\\d)|(6553[0-5]))$",
|
||||
|
||||
"^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?"
|
||||
")(\\/(\\d|[1-2]\\d|3[0-2]))?$|^([\\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\\d"
|
||||
"|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1(["
|
||||
"0-1]\\d|2[0-8]))))?$|^::([\\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\\d|[01]"
|
||||
"?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([0-1]"
|
||||
"\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:):([\\da-fA-F]{1,4}:){0,3}((25[0-5]|"
|
||||
"2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]"
|
||||
"?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:){2}:([\\da-fA-F]{1,4}:"
|
||||
"){0,2}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?"
|
||||
"\\d\\d?)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:){3}:"
|
||||
"([\\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]"
|
||||
"|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-"
|
||||
"fA-F]{1,4}:){4}:((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]"
|
||||
"\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1"
|
||||
",4}:){7}[\\da-fA-F]{1,4}(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^:((:["
|
||||
"\\da-fA-F]{1,4}){1,6}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^[\\da-f"
|
||||
"A-F]{1,4}:((:[\\da-fA-F]{1,4}){1,5}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8])"
|
||||
")))?$|^([\\da-fA-F]{1,4}:){2}((:[\\da-fA-F]{1,4}){1,4}|:)(\\/([1-9]?\\d|"
|
||||
"(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:){3}((:[\\da-fA-F]{1,4}){1,3"
|
||||
"}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:){4}((:["
|
||||
"\\da-fA-F]{1,4}){1,2}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-"
|
||||
"fA-F]{1,4}:){5}:([\\da-fA-F]{1,4})?(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))"
|
||||
"?$|^([\\da-fA-F]{1,4}:){6}:(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$",
|
||||
|
||||
"^(?:(?!0000)[0-9]{4}\\/(?:(?:0[1-9]|1[0-2])\\/(?:0[1-9]|1[0-9]|2[0-8])|("
|
||||
"?:0[13-9]|1[0-2])\\/(?:29|30)|(?:0[13578]|1[02])\\/31)|(?:[0-9]{2}(?:0[48"
|
||||
"]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)\\/02\\/29"
|
||||
") (20|21|22|23|[0-1][0-9]):[0-5][0-9]$|^(20|21|22|23|[0-1][0-9]):[0-5][0-"
|
||||
"9]$",
|
||||
|
||||
"^(20|21|22|23|[0-1][0-9]):[0-5][0-9]$",
|
||||
static REGEX_CONTEXT g_reg_ctx[REGEX_MAX] = {
|
||||
{REGEX_SVR_PORT, NULL, SVR_REGEX_STR},
|
||||
{REGEX_IP_ADDR, NULL, IP_REGEX_STR},
|
||||
{REGEX_DT, NULL, DT_REGEX_STR},
|
||||
{REGEX_TIME, NULL, TIME_REGEX_STR}
|
||||
};
|
||||
|
||||
int pcre_match(int regId, const char *pStr)
|
||||
|
@ -126,6 +143,90 @@ int pcre_match(int regId, const char *pStr)
|
|||
return ret;
|
||||
}
|
||||
|
||||
int pcre_match_str(const char *pRegex, const char *pStr)
|
||||
{
|
||||
pcre *re;
|
||||
pcre_extra *pcreExtra;
|
||||
const char *error;
|
||||
int ret, erroffset;
|
||||
|
||||
if(!pRegex || !pStr) {
|
||||
LOG_EX(LOG_Error, "Input params error: %p, %p\n", pRegex, pStr);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
re = pcre_compile(pRegex, 0, &error, &erroffset, NULL);
|
||||
|
||||
if(!re) {
|
||||
LOG_EX(LOG_Error, "pcre_compile error at %d: %s, %s\n", erroffset,
|
||||
pRegex, error);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
pcreExtra = pcre_study(re, 0, &error);
|
||||
|
||||
if(error != NULL) {
|
||||
LOG_EX(LOG_Error, "pcre_study error: %s, %s\n", pRegex, error);
|
||||
pcre_free(re);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ret = pcre_exec(re,
|
||||
pcreExtra,
|
||||
pStr,
|
||||
strlen(pStr), // length of string
|
||||
0, // Start looking at this point
|
||||
0, // OPTIONS
|
||||
NULL,
|
||||
0); // Length of subStrVec
|
||||
|
||||
if(ret < 0) {
|
||||
switch(ret) {
|
||||
case PCRE_ERROR_NOMATCH :
|
||||
LOG_EX(LOG_Error, "[%s] String did not match the pattern\n", pStr);
|
||||
break;
|
||||
|
||||
case PCRE_ERROR_NULL :
|
||||
LOG_EX(LOG_Error, "[%s] Something was null\n", pStr);
|
||||
break;
|
||||
|
||||
case PCRE_ERROR_BADOPTION :
|
||||
LOG_EX(LOG_Error, "[%s] A bad option was passed\n", pStr);
|
||||
break;
|
||||
|
||||
case PCRE_ERROR_BADMAGIC :
|
||||
LOG_EX(LOG_Error, "Magic number bad (compiled re corrupt?)\n");
|
||||
break;
|
||||
|
||||
case PCRE_ERROR_UNKNOWN_NODE :
|
||||
LOG_EX(LOG_Error, "[%s] Something kooky in the compiled re\n", pStr);
|
||||
break;
|
||||
|
||||
case PCRE_ERROR_NOMEMORY :
|
||||
LOG_EX(LOG_Error, "[%s] Ran out of memory\n", pStr);
|
||||
break;
|
||||
|
||||
default :
|
||||
LOG_EX(LOG_Error, "[%s] Unknown error\n", pStr);
|
||||
break;
|
||||
} /* end switch */
|
||||
} else {
|
||||
ret = TRUE;
|
||||
}
|
||||
|
||||
pcre_free(re);
|
||||
|
||||
if(pcreExtra != NULL) {
|
||||
#ifdef PCRE_CONFIG_JIT
|
||||
pcre_free_study(pcreExtra);
|
||||
#else
|
||||
pcre_free(pcreExtra);
|
||||
#endif
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if 0
|
||||
int regex_match(const char *regex, const char *pStr)
|
||||
{
|
||||
|
|
|
@ -16,6 +16,7 @@ typedef enum {
|
|||
extern "C" {
|
||||
#endif
|
||||
int pcre_match(int regId, const char *pStr);
|
||||
int pcre_match_str(const char* pRegex, const char *pStr);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue