Skip to content

Commit

Permalink
Inline some ICMP helpers into their use modules
Browse files Browse the repository at this point in the history
  • Loading branch information
wingo committed Aug 23, 2017
1 parent 44515e7 commit da223f5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
20 changes: 19 additions & 1 deletion src/apps/ipv4/echo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ local engine = require("core.app")
local receive, transmit = link.receive, link.transmit
local wr16, rd32, wr32 = lwutil.wr16, lwutil.rd32, lwutil.wr32
local get_ihl_from_offset = lwutil.get_ihl_from_offset
local is_ipv4 = lwutil.is_ipv4
local htons = lib.htons

local ehs = constants.ethernet_header_size
local proto_icmp = constants.proto_icmp
local o_ipv4_proto = constants.ethernet_header_size + constants.o_ipv4_proto
local o_ipv4_ver_and_ihl = ehs + constants.o_ipv4_ver_and_ihl
local o_ipv4_checksum = ehs + constants.o_ipv4_checksum
local o_icmpv4_msg_type_sans_ihl = ehs + constants.o_icmpv4_msg_type
local o_icmpv4_msg_code_sans_ihl = ehs + constants.o_icmpv4_msg_code
local o_icmpv4_checksum_sans_ihl = ehs + constants.o_icmpv4_checksum
local icmpv4_echo_request = constants.icmpv4_echo_request
local icmpv4_echo_reply = constants.icmpv4_echo_reply
Expand All @@ -41,13 +45,27 @@ function ICMPEcho:new(conf)
return setmetatable({addresses = addresses}, {__index = ICMPEcho})
end

local function is_icmpv4(pkt)
return is_ipv4(pkt) and pkt.data[o_ipv4_proto] == proto_icmp
end

local function is_icmpv4_echo_request(pkt)
if is_icmpv4(pkt) then
local ihl = get_ihl_from_offset(pkt, o_ipv4_ver_and_ihl)
return pkt.data[o_icmpv4_msg_type_sans_ihl + ihl] == icmpv4_echo_request
and pkt.data[o_icmpv4_msg_code_sans_ihl + ihl] == 0
else
return false
end
end

function ICMPEcho:push()
local l_in, l_out, l_reply = self.input.south, self.output.north, self.output.south

for _ = 1, link.nreadable(l_in) do
local out, pkt = l_out, receive(l_in)

if icmp.is_icmpv4_message(pkt, icmpv4_echo_request, 0) then
if is_icmpv4_echo_request(pkt) then
local pkt_ipv4 = ipv4:new_from_mem(pkt.data + ehs,
pkt.length - ehs)
local pkt_ipv4_dst = rd32(pkt_ipv4:dst())
Expand Down
15 changes: 14 additions & 1 deletion src/apps/ipv6/echo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ local ipv6_fixed_header_size = constants.ipv6_fixed_header_size

local proto_icmpv6 = constants.proto_icmpv6
local ethernet_header_size = constants.ethernet_header_size
local o_ipv6_next_header = ethernet_header_size + constants.o_ipv6_next_header
local o_icmpv6_header = ethernet_header_size + ipv6_fixed_header_size
local o_icmpv6_msg_type = o_icmpv6_header + constants.o_icmpv6_msg_type
local o_icmpv6_msg_code = o_icmpv6_header + constants.o_icmpv6_msg_code
local o_icmpv6_checksum = o_icmpv6_header + constants.o_icmpv6_checksum
local icmpv6_echo_request = constants.icmpv6_echo_request
local icmpv6_echo_reply = constants.icmpv6_echo_reply
Expand All @@ -47,13 +49,24 @@ function ICMPEcho:new(conf)
return setmetatable({addresses = addresses}, {__index = ICMPEcho})
end

local function is_icmpv6(pkt)
return is_ipv6(pkt) and pkt.data[o_ipv6_next_header] == proto_icmpv6
end


local function is_icmpv6_echo_request(pkt)
return is_icmpv6(pkt)
and pkt.data[o_icmpv6_msg_type] == icmpv6_echo_request
and pkt.data[o_icmpv6_msg_code] == 0
end

function ICMPEcho:push()
local l_in, l_out, l_reply = self.input.south, self.output.north, self.output.south

for _ = 1, link.nreadable(l_in) do
local out, pkt = l_out, receive(l_in)

if icmp.is_icmpv6_message(pkt, icmpv6_echo_request, 0) then
if is_icmpv6_echo_request(pkt) then
local pkt_ipv6 = ipv6:new_from_mem(pkt.data + ethernet_header_size,
pkt.length - ethernet_header_size)
local pkt_ipv6_dst = ffi.string(pkt_ipv6:dst(), 16)
Expand Down
33 changes: 0 additions & 33 deletions src/apps/lwaftr/icmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,10 @@ local lib = require("core.lib")
local band, bnot = bit.band, bit.bnot
local C = ffi.C
local wr16, wr32 = lwutil.wr16, lwutil.wr32
local is_ipv4, is_ipv6 = lwutil.is_ipv4, lwutil.is_ipv6
local get_ihl_from_offset = lwutil.get_ihl_from_offset
local htons, ntohs = lib.htons, lib.ntohs
local write_eth_header = lwheader.write_eth_header

local proto_icmp = constants.proto_icmp
local proto_icmpv6 = constants.proto_icmpv6
local o_ipv4_proto = constants.ethernet_header_size + constants.o_ipv4_proto
local o_ipv4_ver_and_ihl = constants.ethernet_header_size + constants.o_ipv4_ver_and_ihl
local o_icmpv4_msg_type_sans_ihl = constants.ethernet_header_size + constants.o_icmpv4_msg_type
local o_icmpv4_msg_code_sans_ihl = constants.ethernet_header_size + constants.o_icmpv4_msg_code
local o_ipv6_next_header = constants.ethernet_header_size + constants.o_ipv6_next_header
local o_icmpv6_msg_type = constants.ethernet_header_size + constants.ipv6_fixed_header_size + constants.o_icmpv6_msg_type
local o_icmpv6_msg_code = constants.ethernet_header_size + constants.ipv6_fixed_header_size + constants.o_icmpv6_msg_code
local ehs = constants.ethernet_header_size

local function calculate_payload_size(dst_pkt, initial_pkt, max_size, config)
Expand Down Expand Up @@ -106,20 +96,6 @@ function new_icmpv4_packet(from_ip, to_ip, initial_pkt, config)
return new_pkt
end

function is_icmpv4(pkt)
return is_ipv4(pkt) and pkt.data[o_ipv4_proto] == proto_icmp
end

function is_icmpv4_message(pkt, msg_type, msg_code)
if is_icmpv4(pkt) then
local ihl = get_ihl_from_offset(pkt, o_ipv4_ver_and_ihl)
return pkt.data[o_icmpv4_msg_type_sans_ihl + ihl] == msg_type
and pkt.data[o_icmpv4_msg_code_sans_ihl + ihl] == msg_code
else
return false
end
end

function new_icmpv6_packet(from_ip, to_ip, initial_pkt, config)
local new_pkt = packet.allocate()
local dgram = to_datagram(new_pkt)
Expand All @@ -144,12 +120,3 @@ function new_icmpv6_packet(from_ip, to_ip, initial_pkt, config)
ipv6_header:free()
return new_pkt
end

function is_icmpv6(pkt)
return is_ipv6(pkt) and pkt.data[o_ipv6_next_header] == proto_icmpv6
end


function is_icmpv6_message(pkt, msg_type, msg_code)
return is_icmpv6(pkt) and pkt.data[o_icmpv6_msg_type] == msg_type and pkt.data[o_icmpv6_msg_code] == msg_code
end

0 comments on commit da223f5

Please sign in to comment.