-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathtcp-connect.lua
45 lines (42 loc) · 1.84 KB
/
tcp-connect.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
local syn_table = {}
print(string.format("\n Time IP:Port Retries Conenct Cost(s)"))
print(string.format("-------------------------- ---------------------------------------------- -------------- ---------------------"))
function process(packet)
if packet.size ~= 0 then
return -- // skip the packet with payload
end
if packet.flags == 2 then
-- syn packet
local key = packet.sip..":"..packet.sport.." => "..packet.dip..":"..packet.dport
if not syn_table[key] then
syn_table[key] = {
seq = packet.seq, count = 0, tv_sec = packet.tv_sec, tv_usec = packet.tv_usec
}
return
end
if syn_table[key].seq == packet.seq then
-- duplicate syn
syn_table[key].count = syn_table[key].count + 1
end
return
end
if packet.flags == 18 or (bit32.band(packet.flags, 4) ~= 0) then
local key = packet.dip..":"..packet.dport.." => "..packet.sip..":"..packet.sport
local first_syn_packet = syn_table[key]
if first_syn_packet and first_syn_packet.count >= 0 then
local time_str = os.date('%Y-%m-%d %H:%M:%S', packet.tv_sec).."."..packet.tv_usec
if packet.tv_usec < first_syn_packet.tv_usec then
packet.tv_sec = packet.tv_sec - 1
packet.tv_usec = packet.tv_usec + 1000000
end
print(string.format("%26s %47s %6d %12d.%06d",
time_str,
key,
syn_table[key].count,
packet.tv_sec-first_syn_packet.tv_sec,
packet.tv_usec-first_syn_packet.tv_usec
))
end
syn_table[key] = nil
end
end