diff options
Diffstat (limited to 'plugins/mmjsonparse/mmjsonparse.c')
-rw-r--r-- | plugins/mmjsonparse/mmjsonparse.c | 126 |
1 files changed, 96 insertions, 30 deletions
diff --git a/plugins/mmjsonparse/mmjsonparse.c b/plugins/mmjsonparse/mmjsonparse.c index c47aceb..ba942f8 100644 --- a/plugins/mmjsonparse/mmjsonparse.c +++ b/plugins/mmjsonparse/mmjsonparse.c @@ -35,8 +35,7 @@ #include <errno.h> #include <unistd.h> #include <ctype.h> -#include <libestr.h> -#include <json/json.h> +#include <json.h> #include "conf.h" #include "syslogd-types.h" #include "template.h" @@ -59,15 +58,36 @@ DEFobjCurrIf(errmsg); DEF_OMOD_STATIC_DATA typedef struct _instanceData { - struct json_tokener *tokener; + char *cookie; + uchar *container; + int lenCookie; + /* REMOVE dummy when real data items are to be added! */ } instanceData; +typedef struct wrkrInstanceData { + instanceData *pData; + struct json_tokener *tokener; +} wrkrInstanceData_t; + struct modConfData_s { rsconf_t *pConf; /* our overall config object */ }; static modConfData_t *loadModConf = NULL;/* modConf ptr to use for the current load process */ static modConfData_t *runModConf = NULL;/* modConf ptr to use for the current exec process */ +/* tables for interfacing with the v6 config system */ +/* action (instance) parameters */ +static struct cnfparamdescr actpdescr[] = { + { "cookie", eCmdHdlrString, 0 }, + { "container", eCmdHdlrString, 0 } +}; +static struct cnfparamblk actpblk = + { CNFPARAMBLK_VERSION, + sizeof(actpdescr)/sizeof(struct cnfparamdescr), + actpdescr + }; + + BEGINbeginCnfLoad CODESTARTbeginCnfLoad @@ -95,14 +115,22 @@ ENDfreeCnf BEGINcreateInstance CODESTARTcreateInstance - pData->tokener = json_tokener_new(); - if(pData->tokener == NULL) { + CHKmalloc(pData->container = (uchar*)strdup("!")); + CHKmalloc(pData->cookie = strdup("@cee:")); + pData->lenCookie = strlen(pData->cookie); +finalize_it: +ENDcreateInstance + +BEGINcreateWrkrInstance +CODESTARTcreateWrkrInstance + pWrkrData->tokener = json_tokener_new(); + if(pWrkrData->tokener == NULL) { errmsg.LogError(0, RS_RET_ERR, "error: could not create json " - "tokener, cannot activate action"); + "tokener, cannot activate instance"); ABORT_FINALIZE(RS_RET_ERR); } finalize_it: -ENDcreateInstance +ENDcreateWrkrInstance BEGINisCompatibleWithFeature @@ -112,10 +140,16 @@ ENDisCompatibleWithFeature BEGINfreeInstance CODESTARTfreeInstance - if(pData->tokener != NULL) - json_tokener_free(pData->tokener); + free(pData->cookie); + free(pData->container); ENDfreeInstance +BEGINfreeWrkrInstance +CODESTARTfreeWrkrInstance + if(pWrkrData->tokener != NULL) + json_tokener_free(pWrkrData->tokener); +ENDfreeWrkrInstance + BEGINdbgPrintInstInfo CODESTARTdbgPrintInstInfo @@ -129,28 +163,32 @@ ENDtryResume static rsRetVal -processJSON(instanceData *pData, msg_t *pMsg, char *buf, size_t lenBuf) +processJSON(wrkrInstanceData_t *pWrkrData, msg_t *pMsg, char *buf, size_t lenBuf) { struct json_object *json; const char *errMsg; DEFiRet; - assert(pData->tokener != NULL); + assert(pWrkrData->tokener != NULL); DBGPRINTF("mmjsonparse: toParse: '%s'\n", buf); - json_tokener_reset(pData->tokener); + json_tokener_reset(pWrkrData->tokener); - json = json_tokener_parse_ex(pData->tokener, buf, lenBuf); + json = json_tokener_parse_ex(pWrkrData->tokener, buf, lenBuf); if(Debug) { errMsg = NULL; if(json == NULL) { enum json_tokener_error err; - err = pData->tokener->err; + err = pWrkrData->tokener->err; if(err != json_tokener_continue) - errMsg = json_tokener_errors[err]; +# if HAVE_JSON_TOKENER_ERROR_DESC + errMsg = json_tokener_error_desc(err); +# else + errMsg = json_tokener_errors[err]; +# endif else errMsg = "Unterminated input"; - } else if((size_t)pData->tokener->char_offset < lenBuf) + } else if((size_t)pWrkrData->tokener->char_offset < lenBuf) errMsg = "Extra characters after JSON object"; else if(!json_object_is_type(json, json_type_object)) errMsg = "JSON value is not an object"; @@ -160,25 +198,25 @@ processJSON(instanceData *pData, msg_t *pMsg, char *buf, size_t lenBuf) } } if(json == NULL - || ((size_t)pData->tokener->char_offset < lenBuf) + || ((size_t)pWrkrData->tokener->char_offset < lenBuf) || (!json_object_is_type(json, json_type_object))) { ABORT_FINALIZE(RS_RET_NO_CEE_MSG); } - msgAddJSON(pMsg, (uchar*)"!", json); + msgAddJSON(pMsg, pWrkrData->pData->container, json); finalize_it: RETiRet; } -#define COOKIE "@cee:" -#define LEN_COOKIE (sizeof(COOKIE)-1) BEGINdoAction msg_t *pMsg; uchar *buf; int bSuccess = 0; struct json_object *jval; struct json_object *json; + instanceData *pData; CODESTARTdoAction + pData = pWrkrData->pData; pMsg = (msg_t*) ppString[0]; /* note that we can performance-optimize the interface, but this also * requires changes to the libraries. For now, we accept message @@ -190,12 +228,12 @@ CODESTARTdoAction ++buf; } - if(*buf == '\0' || strncmp((char*)buf, COOKIE, LEN_COOKIE)) { + if(*buf == '\0' || strncmp((char*)buf, pData->cookie, pData->lenCookie)) { DBGPRINTF("mmjsonparse: no JSON cookie: '%s'\n", buf); ABORT_FINALIZE(RS_RET_NO_CEE_MSG); } - buf += LEN_COOKIE; - CHKiRet(processJSON(pData, pMsg, (char*) buf, strlen((char*)buf))); + buf += pData->lenCookie; + CHKiRet(processJSON(pWrkrData, pMsg, (char*) buf, strlen((char*)buf))); bSuccess = 1; finalize_it: if(iRet == RS_RET_NO_CEE_MSG) { @@ -203,27 +241,54 @@ finalize_it: json = json_object_new_object(); jval = json_object_new_string((char*)buf); json_object_object_add(json, "msg", jval); - msgAddJSON(pMsg, (uchar*)"!", json); + msgAddJSON(pMsg, pData->container, json); iRet = RS_RET_OK; } MsgSetParseSuccess(pMsg, bSuccess); ENDdoAction +static inline void +setInstParamDefaults(instanceData *pData) +{ + pData->cookie = NULL; +} + BEGINnewActInst + struct cnfparamvals *pvals; + int i; CODESTARTnewActInst - /* Note: we currently do not have any parameters, so we do not need - * the lst ptr. However, we will most probably need params in the - * future. - */ DBGPRINTF("newActInst (mmjsonparse)\n"); + if((pvals = nvlstGetParams(lst, &actpblk, NULL)) == NULL) { + ABORT_FINALIZE(RS_RET_MISSING_CNFPARAMS); + } + CODE_STD_STRING_REQUESTnewActInst(1) CHKiRet(OMSRsetEntry(*ppOMSR, 0, NULL, OMSR_TPL_AS_MSG)); CHKiRet(createInstance(&pData)); - /*setInstParamDefaults(pData);*/ + setInstParamDefaults(pData); + + for(i = 0 ; i < actpblk.nParams ; ++i) { + if(!pvals[i].bUsed) + continue; + if(!strcmp(actpblk.descr[i].name, "cookie")) { + free(pData->cookie); + pData->cookie = es_str2cstr(pvals[i].val.d.estr, NULL); + } else if(!strcmp(actpblk.descr[i].name, "container")) { + free(pData->container); + pData->container = (uchar*)es_str2cstr(pvals[i].val.d.estr, NULL); + } else { + dbgprintf("mmjsonparse: program error, non-handled param '%s'\n", actpblk.descr[i].name); + } + } + if(pData->container == NULL) + CHKmalloc(pData->container = (uchar*) strdup("!")); + if(pData->cookie == NULL) + CHKmalloc(pData->cookie = strdup("@cee:")); + pData->lenCookie = strlen(pData->cookie); CODE_STD_FINALIZERnewActInst -/* cnfparamvalsDestruct(pvals, &actpblk);*/ + cnfparamvalsDestruct(pvals, &actpblk); ENDnewActInst BEGINparseSelectorAct @@ -258,6 +323,7 @@ ENDmodExit BEGINqueryEtryPt CODESTARTqueryEtryPt CODEqueryEtryPt_STD_OMOD_QUERIES +CODEqueryEtryPt_STD_OMOD8_QUERIES CODEqueryEtryPt_STD_CONF2_OMOD_QUERIES CODEqueryEtryPt_STD_CONF2_QUERIES ENDqueryEtryPt |