lua: add support for ethernet addresses to the Address class
authorMartin Kaiser <wireshark@kaiser.cx>
Wed, 10 Oct 2018 15:26:59 +0000 (17:26 +0200)
committerPeter Wu <peter@lekensteyn.nl>
Thu, 18 Oct 2018 07:51:36 +0000 (07:51 +0000)
The code for the Address class already contains commented-out code for a
number of additional address types.

Activate the draft constructor for ethernet addresses and complete it.
Use the newly-added function to parse a string that contains an ethernet
address.

Add a basic test tvb.lua. Read an ethernet address from a tvb and
compare it to a constant Address.ether object.

Change-Id: I9771dd6e7ade4b572a8b864b8986d641b4eba3e5
Reviewed-on: https://code.wireshark.org/review/30163
Reviewed-by: Martin Kaiser <wireshark@kaiser.cx>
Petri-Dish: Martin Kaiser <wireshark@kaiser.cx>
Tested-by: Petri Dish Buildbot
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
epan/wslua/wslua_address.c
test/lua/tvb.lua

index 04bb6e5102b19934dde765a0791fbd7e150eb72f..7bbce3653b98b4ae89963d89860459abbc6df3b7 100644 (file)
@@ -60,17 +60,25 @@ WSLUA_CONSTRUCTOR Address_ipv6(lua_State* L) {
     WSLUA_RETURN(1); /* The Address object */
 }
 
-#if 0
-/* TODO */
-static int Address_ss7(lua_State* L) {
-    Address addr = g_malloc(sizeof(address));
+WSLUA_CONSTRUCTOR Address_ether(lua_State *L) {
+    /* Creates an Address Object representing an Ethernet address. */
 
-    /* alloc_address() */
+#define WSLUA_ARG_Address_ether_ETH 1 /* The Ethernet address. */
+    Address addr = (Address)g_malloc(sizeof(address));
+    const gchar *name = luaL_checkstring(L, WSLUA_ARG_Address_ether_ETH);
+    guint8 eth_buf[6];
 
-    pushAddress(L,addr);
-    return 1;
+    if(!str_to_eth(name, eth_buf))
+        memset(eth_buf, 0, sizeof(eth_buf));
+
+    alloc_address_wmem(NULL, addr, AT_ETHER, sizeof(eth_buf), eth_buf);
+    pushAddress(L, addr);
+    WSLUA_RETURN(1); /* The Address object. */
 }
-static int Address_eth(lua_State* L) {
+
+#if 0
+/* TODO */
+static int Address_ss7(lua_State* L) {
     Address addr = g_malloc(sizeof(address));
 
     /* alloc_address() */
@@ -164,9 +172,9 @@ WSLUA_METHODS Address_methods[] = {
     WSLUA_CLASS_FNREG(Address,ip),
     WSLUA_CLASS_FNREG_ALIAS(Address,ipv4,ip),
     WSLUA_CLASS_FNREG(Address,ipv6),
+    WSLUA_CLASS_FNREG(Address,ether),
 #if 0
     WSLUA_CLASS_FNREG_ALIAS(Address,ss7pc,ss7),
-    WSLUA_CLASS_FNREG(Address,eth),
     WSLUA_CLASS_FNREG(Address,sna},
     WSLUA_CLASS_FNREG(Address,atalk),
     WSLUA_CLASS_FNREG(Address,vines),
index 43a042ee852b6b90eb1b2779f0ce1582522a9d81..cfc068fcd670c1b0e8111155f6c5b37e88b2808a 100644 (file)
@@ -54,7 +54,7 @@ end
 --     number of verifyFields() * (1 + number of fields) +
 --     number of verifyResults() * (1 + 2 * number of values)
 --
-local taptests = { [FRAME]=4, [OTHER]=330 }
+local taptests = { [FRAME]=4, [OTHER]=333 }
 
 local function getResults()
     print("\n-----------------------------\n")
@@ -166,6 +166,7 @@ local testfield =
         ABSOLUTE_UTC   = ProtoField.absolute_time("test.basic.absolute.utc",  "Basic absolute utc", base.UTC),
         IPv4           = ProtoField.ipv4   ("test.basic.ipv4",    "Basic ipv4 address"),
         IPv6           = ProtoField.ipv6   ("test.basic.ipv6",    "Basic ipv6 address"),
+        ETHER          = ProtoField.ether  ("test.basic.ether",   "Basic ethernet address"),
         -- GUID           = ProtoField.guid   ("test.basic.guid",    "Basic GUID"),
     },
 
@@ -216,6 +217,7 @@ local getfield =
         ABSOLUTE_UTC   = Field.new ("test.basic.absolute.utc"),
         IPv4           = Field.new ("test.basic.ipv4"),
         IPv6           = Field.new ("test.basic.ipv6"),
+        ETHER          = Field.new ("test.basic.ether"),
         -- GUID           = Field.new ("test.basic.guid"),
     },
 
@@ -576,6 +578,18 @@ function test_proto.dissector(tvbuf,pktinfo,root)
 
     verifyFields("basic.IPv4", ipv4_match_fields)
 
+----------------------------------------
+    testing(OTHER, "tree:add ether")
+
+    local tvb = ByteArray.new("010203040506"):tvb("Ether")
+    local ether = testfield.basic.ETHER
+    local ether_match_fields = {}
+
+    execute ("ether", pcall (callTreeAdd, tree, ether, tvb:range(0,6)))
+    addMatchFields(ether_match_fields, Address.ether('01:02:03:04:05:06'))
+
+    verifyFields("basic.ETHER", ether_match_fields)
+
 ----------------------------------------
     testing(OTHER, "tree:add_packet_field Bytes")