]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust: add libjansson wrapper for rust
authorJason Ish <ish@unx.ca>
Wed, 26 Apr 2017 22:12:51 +0000 (16:12 -0600)
committerJason Ish <ish@unx.ca>
Mon, 5 Jun 2017 20:57:20 +0000 (14:57 -0600)
rust/src/json.rs [new file with mode: 0644]
rust/src/lib.rs

diff --git a/rust/src/json.rs b/rust/src/json.rs
new file mode 100644 (file)
index 0000000..28dbb6b
--- /dev/null
@@ -0,0 +1,91 @@
+/* Copyright (C) 2017 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+//! Expose portions of the libjansson API to Rust so Rust code can
+//! populate a json_t and return it for logging by Suricata.
+
+use std::ffi::CString;
+use std::os::raw::c_char;
+
+/// The Rust place holder for the json_t pointer.
+pub enum JsonT {}
+
+/// Expose the jansson functions we need.
+extern {
+    fn json_object() -> *mut JsonT;
+    fn json_object_set_new(js: *mut JsonT, key: *const c_char,
+                           val: *mut JsonT) -> u32;
+
+    fn json_array() -> *mut JsonT;
+    fn json_array_append_new(array: *mut JsonT, value: *mut JsonT);
+
+    fn json_string(value: *const c_char) -> *mut JsonT;
+    fn json_integer(val: u64) -> *mut JsonT;
+}
+
+pub struct Json {
+    pub js: *mut JsonT,
+}
+
+impl Json {
+
+    pub fn object() -> Json {
+        return Json{
+            js: unsafe{json_object()},
+        }
+    }
+
+    pub fn array() -> Json {
+        return Json{
+            js: unsafe{json_array()},
+        }
+    }
+
+    pub fn unwrap(&self) -> *mut JsonT {
+        return self.js;
+    }
+
+    pub fn set(&self, key: &str, val: Json) {
+        unsafe {
+            json_object_set_new(self.js,
+                                CString::new(key).unwrap().as_ptr(),
+                                val.js);
+        }
+    }
+
+    pub fn set_string(&self, key: &str, val: &str) {
+        unsafe{
+            json_object_set_new(self.js,
+                                CString::new(key).unwrap().as_ptr(),
+                                json_string(CString::new(val).unwrap().as_ptr()));
+        }
+    }
+
+    pub fn set_integer(&self, key: &str, val: u64) {
+        unsafe {
+            json_object_set_new(self.js,
+                                CString::new(key).unwrap().as_ptr(),
+                                json_integer(val));
+        }
+    }
+
+    pub fn array_append(&self, val: Json) {
+        unsafe {
+            json_array_append_new(self.js, val.js);
+        }
+    }
+}
index ad4661df93edcf3cc78aaf087976becb6e9d2c2e..52ee7128c26a8f7fd7c8860884fa283ba17f15f9 100644 (file)
@@ -3,3 +3,4 @@ pub mod log;
 
 pub mod conf;
 pub mod dns;
+pub mod json;