]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust: lua wrapper
authorJason Ish <ish@unx.ca>
Mon, 1 May 2017 23:13:20 +0000 (17:13 -0600)
committerJason Ish <ish@unx.ca>
Mon, 5 Jun 2017 20:57:21 +0000 (14:57 -0600)
Rust wrapper for working with lua state.

configure.ac
rust/Cargo.toml.in
rust/Makefile.am
rust/src/lib.rs
rust/src/lua.rs [new file with mode: 0644]

index c0b037c656fe69f46dadebd142b986d957201e6b..42d0892499bbc3eb1a2a8f2d695cee28d48fa6cc 100644 (file)
         fi
     fi
 
+    AM_CONDITIONAL([HAVE_LUA], [test "x$enable_lua" != "xno"])
+
   # libgeoip
     AC_ARG_ENABLE(geoip,
                AS_HELP_STRING([--enable-geoip],[Enable GeoIP support]),
index 010cf0a48763b4c9a76e6f5e8c14fc24a1657a67..dd90d4c45df00746708e4ff35bdd144003bd9fc6 100644 (file)
@@ -8,6 +8,9 @@ crate-type = ["staticlib"]
 [profile.release]
 debug = true
 
+[features]
+lua = []
+
 [dependencies]
 nom = "2.1.0"
 libc = "0.2.0"
index e16131a11c6768b521e95b179959c319186c8291..a3255d995bcde137ac1f061b58851af0db7512fd 100644 (file)
@@ -25,14 +25,21 @@ else
 endif
 
 if HAVE_RUST
+
+FEATURES =
+
+if HAVE_LUA
+FEATURES +=    lua
+endif
+
 all-local:
 if HAVE_PYTHON
        cd $(top_srcdir)/rust && CARGO_TARGET_DIR=$(abs_builddir)/target \
                python ./gen-c-headers.py && \
-               cargo build $(RELEASE) $(FROZEN)
+               cargo build $(RELEASE) $(FROZEN) --features "$(FEATURES)"
 else
        cd $(top_srcdir)/rust && CARGO_TARGET_DIR=$(abs_builddir)/target \
-               cargo build $(RELEASE) $(FROZEN)
+               cargo build $(RELEASE) $(FROZEN) --features "$(FEATURES)"
 endif
 
 clean-local:
index f88c8a8298f3abc7af9c4cab5d9e01fbc7c2cd66..e1803588ebed8970b4924abe54da7b5630b80f4c 100644 (file)
@@ -24,4 +24,8 @@ pub mod log;
 pub mod core;
 pub mod conf;
 pub mod json;
+
+#[cfg(feature = "lua")]
+pub mod lua;
+
 pub mod dns;
diff --git a/rust/src/lua.rs b/rust/src/lua.rs
new file mode 100644 (file)
index 0000000..f225dcc
--- /dev/null
@@ -0,0 +1,62 @@
+/* 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.
+ */
+
+use std::ffi::CString;
+use std::os::raw::c_char;
+use std::os::raw::c_int;
+use std::os::raw::c_long;
+
+/// The Rust place holder for lua_State.
+pub enum CLuaState {}
+
+extern {
+    fn lua_createtable(lua: *mut CLuaState, narr: c_int, nrec: c_int);
+    fn lua_settable(lua: *mut CLuaState, idx: c_long);
+    fn lua_pushstring(lua: *mut CLuaState, s: *const c_char);
+    fn lua_pushinteger(lua: *mut CLuaState, n: c_long);
+}
+
+pub struct LuaState {
+    pub lua: *mut CLuaState,
+}
+
+impl LuaState {
+
+    pub fn newtable(&self) {
+        unsafe {
+            lua_createtable(self.lua, 0, 0);
+        }
+    }
+
+    pub fn settable(&self, idx: i64) {
+        unsafe {
+            lua_settable(self.lua, idx);
+        }
+    }
+
+    pub fn pushstring(&self, val: &str) {
+        unsafe {
+            lua_pushstring(self.lua, CString::new(val).unwrap().as_ptr());
+        }
+    }
+
+    pub fn pushinteger(&self, val: i64) {
+        unsafe {
+            lua_pushinteger(self.lua, val);
+        }
+    }
+}