Skip to content

Commit 553df18

Browse files
committed
TCP flags + bugfixes
1 parent 8338c2f commit 553df18

File tree

4 files changed

+62
-43
lines changed

4 files changed

+62
-43
lines changed

ipparse/l3/ip.moon

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
:IP6, :IP4 = require"ipparse.l2.ethernet".proto
33
:ip6, :ip62s, :s2ip6, :net62s, :s2net6 = require"ipparse.l3.ip6"
44
:ip4, :ip42s, :s2ip4, :net42s, :s2net4 = require"ipparse.l3.ip4"
5-
su = string.unpack
5+
:sub, unpack: su = string
66

77
get_version = (off) => -- Accepts data string; returns IP version
88
su("B", @, off) >> 4
@@ -38,26 +38,24 @@ net2s = => -- Accepts data string; returns subnet as readable string.
3838
s2net = => -- Accepts readable string; retuns subnet as data string
3939
(@match":" and s2net6 or @match"%." and s2net4) @
4040

41-
contains_subnet = (subnet) => -- Accepts 2 data strings; checks whether net @ contains subnet
42-
return false if #@ ~= #subnet
43-
nmask = su "B", @
44-
smask = su "B", subnet
45-
return false if nmask > smask
46-
fmt, shft = "c#{nmask >> 3}", 8 - (nmask & 0x7)
47-
nbytes, nbits = su fmt, @, 2
48-
sbytes, sbits = su fmt, subnet, 2
49-
return true if nbytes == sbytes and (nbits >> shft) == (sbits >> shft)
50-
false
51-
52-
contains_ip = (i) => -- Accepts 2 data strings; checks whether net @ contains ip
53-
return false if #@ ~= #i+1
54-
nmask = su "B", @
55-
fmt, shft = "c#{nmask >> 3}", 8 - (nmask & 0x7)
41+
contains_ip = (i, nmask) => -- Accepts 2 data strings; checks whether net @ contains ip
42+
if not nmask
43+
return false if #@ ~= #i+1
44+
nmask = su "B", @
45+
return sub(@, 2) == i if nmask == 128
46+
fmt, shft = "c#{nmask >> 3}B", 8 - (nmask & 0x7)
5647
nbytes, nbits = su fmt, @, 2
5748
sbytes, sbits = su fmt, i
5849
return true if nbytes == sbytes and (nbits >> shft) == (sbits >> shft)
5950
false
6051

52+
contains_subnet = (subnet) => -- Accepts 2 data strings; checks whether net @ contains subnet
53+
return false if #@ ~= #subnet
54+
nmask, smask = su("B", @), su("B", subnet)
55+
return false if nmask > smask
56+
return @ == subnet if nmask == smask
57+
contains_ip @, sub(subnet, 2), nmask
58+
6159
proto =
6260
ICMP: 0x01
6361
TCP: 0x06

ipparse/l3/ip4.moon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ net42s = => -- Accepts data string; returns IPv4 address as readable string
2424
format "%d.%d.%d.%d/%d", a, b, c, d, m
2525

2626
s2net4 = =>
27-
b1, b2, b3, b4, mask = @match"(%d+)%.(%d+)%.(%d+)%.(%d+)/?(%d+)"
28-
sp "B BBBB", (tonumber mask or 32), tonumber(b1), tonumber(b2), tonumber(b3), tonumber(b4)
27+
b1, b2, b3, b4, mask = @match"(%d+)%.(%d+)%.(%d+)%.(%d+)/?(%d*)"
28+
sp "B BBBB", (tonumber(mask) or 32), tonumber(b1), tonumber(b2), tonumber(b3), tonumber(b4)
2929

3030

3131
:ip4, :ip42s, :s2ip4, :net42s, :s2net4

ipparse/l4/tcp.moon

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
su = string.unpack
2+
unpack = table.unpack
23

3-
tcp: (off=0) =>
4-
spt, dpt, seq_n, ack_n, data_off, flags, window, checksum, urg_ptr, _off = su ">H H I4 I4 B B H H H", @, off
5-
data_off = data_off & 0xf0 >> 2
6-
{:spt, :dpt, :seq_n, :ack_n, :off, :data_off, :flags, :window, :checksum, :urg_ptr}, _off
4+
FIN, SYN, RST, PSH, ACK, URG = unpack [ 1 << (i-1) for i = 1, 6 ]
75

6+
{
7+
flags: {:FIN, :SYN, :RST, :PSH, :ACK, :URG}
8+
tcp: (off=0) =>
9+
spt, dpt, seq_n, ack_n, header_len, flags, window, checksum, urg_ptr, _off = su ">H H I4 I4 B B H H H", @, off
10+
header_len = (header_len & 0xf0) >> 2
11+
{
12+
:spt, :dpt, :seq_n, :ack_n
13+
:off, :header_len, data_off: off+header_len
14+
:flags, :window, :checksum, :urg_ptr
15+
urg: flags & URG ~= 0
16+
ack: flags & ACK ~= 0
17+
psh: flags & PSH ~= 0
18+
rst: flags & RST ~= 0
19+
syn: flags & SYN ~= 0
20+
fin: flags & FIN ~= 0
21+
}, _off
22+
}

ipparse/l7/dns.moon

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ format: sf, pack: sp, rep: sr, unpack: su = string
44
:concat = table
55

66
header = (off, is_tcp) => -- Accepts data string, offset and boolean; returns DNS header infos
7-
size, off = su ">H", off if is_tcp
7+
len = #@ - off
8+
local size
9+
if is_tcp
10+
return nil, "No DNS data" if len < 2
11+
size, off = su ">H", @, off
12+
len -= 2
13+
return nil, "No DNS data" if len < 12
814
id, qr_opcode_aa_tc_rd, ra_z_rcode, qdcount, ancount, nscount, arcount, data_off = su ">H B B H H H H", @, off
915
{
1016
:id
@@ -83,30 +89,30 @@ types = bidirectional {
8389
}
8490

8591
ede_codes = {
86-
"Unsupported DNSKEY Algorithm",
87-
"Unsupported DS Digest Type",
88-
"Stale Answer",
89-
"Forged Answer",
90-
"DNSSEC Indeterminate",
91-
"DNSSEC Bogus",
92-
"Signature Expired",
93-
"Signature Not Yet Valid",
94-
"DNSKEY Missing",
95-
"RRSIGs Missing",
96-
"No Zone Key Bit Set",
97-
"NSEC Missing",
98-
"Cached Error",
99-
"Not Ready",
92+
"Unsupported_DNSKEY_Algorithm",
93+
"Unsupported_DS_Digest_Type",
94+
"Stale_Answer",
95+
"Forged_Answer",
96+
"DNSSEC_Indeterminate",
97+
"DNSSEC_Bogus",
98+
"Signature_Expired",
99+
"Signature_Not_Yet_Valid",
100+
"DNSKEY_Missing",
101+
"RRSIGs_Missing",
102+
"No_Zone_Key_Bit_Set",
103+
"NSEC_Missing",
104+
"Cached_Error",
105+
"Not_Ready",
100106
"Blocked",
101107
"Censored",
102108
"Filtered",
103109
"Prohibited",
104-
"Stale NXDOMAIN Answer",
105-
"Not Authoritative",
106-
"Not Supported",
107-
"No Reachable Authority",
108-
"Network Error",
109-
"Invalid Data"
110+
"Stale_NXDOMAIN_Answer",
111+
"Not_Authoritative",
112+
"Not_Supported",
113+
"No_Reachable_Authority",
114+
"Network_Error",
115+
"Invalid_Data"
110116
}
111117
ede_codes[0] = "Other"
112118
ede_codes = bidirectional ede_codes

0 commit comments

Comments
 (0)