]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
sip/eve: convert to jsonbuilder
authorJason Ish <jason.ish@oisf.net>
Wed, 4 Mar 2020 22:56:07 +0000 (16:56 -0600)
committerVictor Julien <victor@inliniac.net>
Wed, 3 Jun 2020 11:36:55 +0000 (13:36 +0200)
rust/cbindgen.toml
rust/src/sip/log.rs
src/output-json-alert.c
src/output-json-sip.c
src/output-json-sip.h

index d53fd7f235d471098ab51039b84e4611bbddc5f2..dfcf8d03d5b3d1297db6c8319b789c726f063499 100644 (file)
@@ -71,7 +71,10 @@ documentation_style = "doxy"
 # found but otherwise don't appear to be used by the public API.
 #
 # default: []
-include = ["AppLayerGetTxIterTuple"]
+include = [
+    "AppLayerGetTxIterTuple",
+    "SIPState",
+]
 
 # A list of items to not include in the generated bindings
 # default: []
index 1c1813366e17401e0d9ca1c8b054cfdad649f46d..d7efbd89270e133beab3a9d2f19e0ccfd00bfb93 100644 (file)
 
 // written by Giuseppe Longo <giuseppe@glongo.it>
 
-use crate::json::*;
-use crate::sip::sip::{SIPState, SIPTransaction};
+use crate::jsonbuilder::{JsonBuilder, JsonError};
+use crate::sip::sip::SIPTransaction;
 
-#[no_mangle]
-pub extern "C" fn rs_sip_log_json(_state: &mut SIPState, tx: &mut SIPTransaction) -> *mut JsonT {
-    let js = Json::object();
-
-    match tx.request {
-        Some(ref req) => {
-            js.set_string("method", &req.method);
-            js.set_string("uri", &req.path);
-            js.set_string("version", &req.version);
-        }
-        None => {}
+fn log(tx: &SIPTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> {
+    js.open_object("sip")?;
+
+    if let Some(req) = &tx.request {
+        js.set_string("method", &req.method)?
+            .set_string("uri", &req.path)?
+            .set_string("version", &req.version)?;
     }
-    match tx.request_line {
-        Some(ref req_line) => {
-            js.set_string("request_line", &req_line);
-        }
-        None => {}
+
+    if let Some(req_line) = &tx.request_line {
+        js.set_string("request_line", &req_line)?;
     }
-    match tx.response {
-        Some(ref resp) => {
-            js.set_string("version", &resp.version);
-            js.set_string("code", &resp.code);
-            js.set_string("reason", &resp.reason);
-        }
-        None => {}
+
+    if let Some(resp) = &tx.response {
+        js.set_string("version", &resp.version)?
+            .set_string("code", &resp.code)?
+            .set_string("reason", &resp.reason)?;
     }
-    match tx.response_line {
-        Some(ref resp_line) => {
-            js.set_string("response_line", &resp_line);
-        }
-        None => {}
+
+    if let Some(resp_line) = &tx.response_line {
+        js.set_string("response_line", &resp_line)?;
     }
 
-    return js.unwrap();
+    js.close()?;
+
+    Ok(())
 }
+
+#[no_mangle]
+pub extern "C" fn rs_sip_log_json(tx: &mut SIPTransaction, js: &mut JsonBuilder) -> bool {
+    log(tx, js).is_ok()
+}
\ No newline at end of file
index fed8b3c2b7c4a7795f3e8380ae23c7b3e7c48c52..cbe6fb228ea0d17392667a2a8835332e917823c8 100644 (file)
@@ -531,11 +531,7 @@ static int AlertJson(ThreadVars *tv, JsonAlertLogThread *aft, const Packet *p)
                     }
                     break;
                 case ALPROTO_SIP:
-                    hjs = JsonSIPAddMetadata(p->flow, pa->tx_id);
-                    if (hjs) {
-                        jb_set_jsont(jb, "sip", hjs);
-                        json_decref(hjs);
-                    }
+                    JsonSIPAddMetadata(jb, p->flow, pa->tx_id);
                     break;
                 case ALPROTO_RFB:
                     hjs = JsonRFBAddMetadata(p->flow, pa->tx_id);
index bc8e066de0b5498a4eaf11b793ac823ae7c58ca2..3fa4d3595e4c40b545fa177d1ac7842709e3ddf7 100644 (file)
@@ -59,17 +59,15 @@ typedef struct LogSIPLogThread_ {
     MemBuffer          *buffer;
 } LogSIPLogThread;
 
-json_t *JsonSIPAddMetadata(const Flow *f, uint64_t tx_id)
+void JsonSIPAddMetadata(JsonBuilder *js, const Flow *f, uint64_t tx_id)
 {
     SIPState *state = FlowGetAppState(f);
     if (state) {
         SIPTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_SIP, state, tx_id);
         if (tx) {
-            return rs_sip_log_json(state, tx);
+            rs_sip_log_json(tx, js);
         }
     }
-
-    return NULL;
 }
 
 static int JsonSIPLogger(ThreadVars *tv, void *thread_data,
@@ -77,29 +75,28 @@ static int JsonSIPLogger(ThreadVars *tv, void *thread_data,
 {
     SIPTransaction *siptx = tx;
     LogSIPLogThread *thread = thread_data;
-    json_t *js, *sipjs;
 
-    js = CreateJSONHeader(p, LOG_DIR_PACKET, "sip", NULL);
+    JsonBuilder *js = CreateEveHeader((Packet *)p, LOG_DIR_PACKET, "sip", NULL);
     if (unlikely(js == NULL)) {
-        return TM_ECODE_FAILED;
+        return TM_ECODE_OK;
     }
+    EveAddCommonOptions(&thread->siplog_ctx->cfg, p, f, js);
 
-    JsonAddCommonOptions(&thread->siplog_ctx->cfg, p, f, js);
-
-    sipjs = rs_sip_log_json(state, siptx);
-    if (unlikely(sipjs == NULL)) {
-      goto error;
+    if (!rs_sip_log_json(siptx, js)) {
+        goto error;
+    }
+    if (!jb_close(js)) {
+        goto error;
     }
-    json_object_set_new(js, "sip", sipjs);
 
     MemBufferReset(thread->buffer);
-    OutputJSONBuffer(js, thread->siplog_ctx->file_ctx, &thread->buffer);
+    OutputJsonBuilderBuffer(js, thread->siplog_ctx->file_ctx, &thread->buffer);
+    jb_free(js);
 
-    json_decref(js);
     return TM_ECODE_OK;
 
 error:
-    json_decref(js);
+    jb_free(js);
     return TM_ECODE_FAILED;
 }
 
index bc8836dad6b4f3c8fcab894f1b8852a0135f2b86..60145dab5b984bca202883b735b58bb8f2188b89 100644 (file)
@@ -26,6 +26,6 @@
 
 void JsonSIPLogRegister(void);
 
-json_t *JsonSIPAddMetadata(const Flow *f, uint64_t tx_id);
+void JsonSIPAddMetadata(JsonBuilder *js, const Flow *f, uint64_t tx_id);
 
 #endif /* __OUTPUT_JSON_SIP_H__ */