From 28e47f9361da950fb5b547ca3e6a6a4adf79c80d Mon Sep 17 00:00:00 2001 From: nnposter Date: Sun, 25 Aug 2024 23:25:18 +0000 Subject: [PATCH] Collapse Packet.ether_parse() into the constructor. It was called from there anyway and never called on its own from elsewhere. Furthermore, its documentation did not match its behavior. --- nselib/packet.lua | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/nselib/packet.lua b/nselib/packet.lua index 94ff2b38b..098ccf795 100644 --- a/nselib/packet.lua +++ b/nselib/packet.lua @@ -147,18 +147,20 @@ ETHER_TYPE_ATAOE = 0x88a2 Frame = {} function Frame:new(frame, force_continue) - local packet = nil - local packet_len = 0 - if frame and #frame > 14 then - packet = string.sub(frame, 15, -1) - packet_len = #frame - 14 + local mac_dst, mac_src, ether_type, packet + if frame and #frame >= 14 then + local pos + mac_dst, mac_src, ether_type, pos = ("c6c6>I2"):unpack(frame) + packet = frame:sub(pos, -1) + if #packet == 0 then packet = nil end end - local o = Packet:new(packet, packet_len, force_continue) + local o = Packet:new(packet, packet and #packet or 0, force_continue) o.build_ether_frame = self.build_ether_frame - o.ether_parse = self.ether_parse o.frame_buf = frame - o:ether_parse() + o.mac_dst = mac_dst + o.mac_src = mac_src + o.ether_type = ether_type return o end --- Build an Ethernet frame. @@ -177,19 +179,6 @@ function Frame:build_ether_frame(mac_dst, mac_src, ether_type, packet) end self.frame_buf = self.mac_dst..self.mac_src..(">I2"):pack(self.ether_type)..self.buf end ---- Parse an Ethernet frame. --- @param frame string of the Ether frame. --- @return mac_dst six-byte string of the destination MAC address. --- @return mac_src six-byte string of the source MAC address. --- @return packet string of the payload. -function Frame:ether_parse() - if not self.frame_buf or #self.frame_buf < 14 then -- too short - return false - end - self.mac_dst = string.sub(self.frame_buf, 1, 6) - self.mac_src = string.sub(self.frame_buf, 7, 12) - self.ether_type = u16(self.frame_buf, 12) -end ---------------------------------------------------------------------------------------------------------------- -- Packet is a class