Skip to content

Commit dfc79bc

Browse files
committed
get rr ip
1 parent 6df4222 commit dfc79bc

File tree

6 files changed

+59
-24
lines changed

6 files changed

+59
-24
lines changed

ipparse/l3/ip6.moon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
subclass = require"ipparse".subclass
22
IP = require"ipparse.l3.ip"
3+
Net6 = require"ipparse.l3.ipcalc".Net6
34
range = require"ipparse.fun".range
45
:concat = table
56

67
subclass IP, {
78
__name: "IP6"
89

9-
get_ip_at: (off) => concat range(off, off+14, 2)\map((i) -> "%x"\format @short i)\toarray!, ":"
10+
get_ip_at: (off) => Net6(concat range(off, off+14, 2)\map((i) -> "%x"\format @short i)\toarray!, ":")\ip!
1011

1112
is_fragment: -> -- TODO: IPv6 defragmentation
1213

ipparse/l3/ipcalc.moon

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ range, wrap = do
88
local Net, Net4, Net6
99

1010
Net4 = do
11-
__tostring = => concat(range(4)\map((i) -> @[i])\toarray!, ".") .. "/#{@mask}"
11+
ip4 = => concat range(4)\map((i) -> @[i])\toarray!, "."
12+
__tostring = => ip4(@) .. "/#{@mask}"
1213
mt =
1314
__index: (k) => @bits >> 8*(4-k) & (1<<8)-1
1415
__le: (o) =>
@@ -21,19 +22,20 @@ Net4 = do
2122
mask = tonumber(mask) or 32
2223
bits = wrap(@gmatch"[^%.]+")\imap((i) => tonumber(@) << 8*(4-i))\reduce (a, b) -> a + b
2324
bits = bits >> (32-mask) << (32-mask)
24-
setmetatable {:bits, :mask, v: 4}, mt
25+
setmetatable {:bits, :mask, v: 4, ip: ip4}, mt
2526

2627

2728
Net6 = do -- 128 bits and IPv6 representation make it a bit more complex
28-
__repr = =>
29+
ip6 = =>
2930
s = concat range(8)\map((i) -> "%x"\format(@[i]))\toarray!, ":"
3031
for n = 8, 1, -1
3132
zeros = ":" .. concat range(n)\map(-> "0")\toarray!, ":"
3233
s, r = s\gsub zeros, "::", 1
3334
if r > 0
3435
s = s\gsub(":::*", "::")\gsub("^0::", "::")\gsub "^::0$", "::"
3536
break
36-
"#{s}/#{@mask}"
37+
s
38+
__repr = => ip6(@) .. "/#{@mask}"
3739
mt =
3840
__index: (k) =>
3941
@bits[(k-1)//4+1] >> 16*((8-k)%4) & (1<<16)-1
@@ -44,8 +46,8 @@ Net6 = do -- 128 bits and IPv6 representation make it a bit more complex
4446
return false if o.mask >= 64 and @bits[1] ~= o.bits[1]
4547
return false if o.mask < 64 and (@bits[1] >> (64-o.mask) << (64-o.mask)) ~= o.bits[1]
4648
(@bits[2] >> (128-o.mask) << (128-o.mask)) == o.bits[2]
47-
__tostring: __repr, :__repr
48-
Net6 = =>
49+
__tostring: __repr, :__repr, ip: ip6
50+
=>
4951
@, mask = @match"([^/]*)/?([^/]*)$"
5052
mask = tonumber(mask) or 128
5153
address = wrap(@gmatch"([^:]*):?")\toarray!
@@ -68,7 +70,7 @@ Net6 = do -- 128 bits and IPv6 representation make it a bit more complex
6870
if mask < 64
6971
bits[1] = bits[1] >> (64-mask) << (64-mask)
7072
bits[2] = bits[2] >> (128-mask) << (128-mask)
71-
setmetatable {:bits, :mask, v: 6}, mt
73+
setmetatable {:bits, :mask, v: 6, ip: ip6}, mt
7274

7375

7476
Net = => @match":" and Net6(@) or Net4(@)

ipparse/l7/dns/init.moon

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,13 @@ subclass, Packet = do
44
Question = require"ipparse.l7.dns.question"
55
RR = require"ipparse.l7.dns.rr"
66

7-
bidirectional = =>
8-
@[@[i]] = i for i = 1, #@
9-
@
10-
117
local DNS
128
DNS = subclass Packet, {
139
__name: "DNS"
1410

1511
iana_port: 53
1612

17-
types: bidirectional {
18-
"A", "NS", "MD", "MF", "CNAME", "SOA", "MB", "MG", "MR", "NULL",
19-
"WKS", "PTR", "HINFO", "MINFO", "MX", "TXT", "RP", "AFSDB", "X25", "ISDN",
20-
"RT", "NSAP", "NSAP-PTR", "SIG", "KEY", "PX", "GPOS", "AAAA", "LOC", "NXT",
21-
"EID", "NIMLOC", "SRV", "ATMA", "NAPTR", "KX", "CERT", "A6", "DNAME", "SINK",
22-
"OPT", "APL", "DS", "SSHFP", "IPSECKEY", "RRSIG", "NSEC", "DNSKEY", "DHCID", "NSEC3",
23-
"NSEC3PARAM", "TLSA", "SMIMEA", "HIP", "NINFO", "RKEY", "TALINK", "CDS", "CDNSKEY", "OPENPGPKEY",
24-
"CSYNC", "ZONEMD", "SVCB", "HTTPS", "SPF", "EUI48", "EUI64", "TKEY", "TSIG", "IXFR",
25-
"AXFR", "MAILB", "MAILA", "ANY", "URI", "CAA", "AVC", "DOA", "AMTRELAY", "TA",
26-
"DLV"
27-
}
13+
types: require"ipparse.l7.dns.types"
2814

2915
_get_id: => @short 0
3016

ipparse/l7/dns/rr.moon

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ subclass, Packet = do
44
map, range = do
55
_ = require"ipparse.fun"
66
_.map, _.range
7+
Net6 = require"ipparse.l3.ipcalc".Net6
8+
A, AAAA = do
9+
_ = require"ipparse.l7.dns.types"
10+
_.A, _.AAAA
711
concat = table.concat
812

913
subclass Packet, {
@@ -48,10 +52,28 @@ subclass Packet, {
4852

4953
_get_rdlength: => @short @type_offset+8
5054

51-
_get_rdata: => range(0, @rdlength-1)\map((off) -> @byte @type_offset+10+off)\toarray!
55+
_get_rdoff: => @type_offset + 10
56+
57+
_get_rdata: =>
58+
rdoff = @rdoff
59+
range(rdoff, rdoff+@rdlength-1)\map((off) -> @byte off)\toarray!
5260

5361
_get_name: => concat @labels, "."
5462

5563
_get_length: => @type_offset + 10 + @rdlength
64+
65+
_get_ip4: => concat @rdata, "."
66+
67+
_get_ip6: =>
68+
return nil if @rdlength ~= 16
69+
rdoff = @rdoff
70+
Net6(concat range(rdoff, rdoff+14, 2)\map((i) -> "%.4x"\format @short i)\toarray!, ":")\ip!
71+
72+
_get_ip: =>
73+
switch @type
74+
when AAAA
75+
@ip6
76+
when A
77+
@ip4
5678
}
5779

ipparse/l7/dns/types.moon

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
bidirectional = =>
2+
@[@[i]] = i for i = 1, #@
3+
@
4+
5+
bidirectional {
6+
"A", "NS", "MD", "MF", "CNAME", "SOA", "MB", "MG", "MR", "NULL",
7+
"WKS", "PTR", "HINFO", "MINFO", "MX", "TXT", "RP", "AFSDB", "X25", "ISDN",
8+
"RT", "NSAP", "NSAP-PTR", "SIG", "KEY", "PX", "GPOS", "AAAA", "LOC", "NXT",
9+
"EID", "NIMLOC", "SRV", "ATMA", "NAPTR", "KX", "CERT", "A6", "DNAME", "SINK",
10+
"OPT", "APL", "DS", "SSHFP", "IPSECKEY", "RRSIG", "NSEC", "DNSKEY", "DHCID", "NSEC3",
11+
"NSEC3PARAM", "TLSA", "SMIMEA", "HIP", "NINFO", "RKEY", "TALINK", "CDS", "CDNSKEY", "OPENPGPKEY",
12+
"CSYNC", "ZONEMD", "SVCB", "HTTPS", "SPF", "EUI48", "EUI64", "TKEY", "TSIG", "IXFR",
13+
"AXFR", "MAILB", "MAILA", "ANY", "URI", "CAA", "AVC", "DOA", "AMTRELAY", "TA",
14+
"DLV"
15+
}
16+

moontastik_lua/ipparse/install.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env sh
2+
3+
NAME=ipparse
4+
LUA_MODULE_DIR=/lib/modules/lua
5+
6+
mkdir ${LUA_MODULE_DIR}/${NAME} || true
7+
find . -name \*.lua -exec install -vDm 644 {} ${LUA_MODULE_DIR}/${NAME}/{} \;
8+

0 commit comments

Comments
 (0)