ref: 52865cb63a6676c88e0c2703903741e03e623380
parent: 79494c975307b4c5c82c836587002140b31b3b10
parent: a4c065b4c05dd6ba3e8e0bb5d4bf243ee213ab5c
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Jun 8 04:53:22 EDT 2017
Merge branch 'master' of git+ssh://git.eigenstate.org/git/ori/mc
--- a/lib/std/fmtfuncs.myr
+++ b/lib/std/fmtfuncs.myr
@@ -5,6 +5,7 @@
use "introspect"
use "strbuf"
use "varargs"
+use "ipparse"
pkg std =
;;
@@ -12,11 +13,13 @@
const __init__ = {
var bigint : bigint#
var bitset : bitset#
+ var ipaddr : netaddr
bigint = mkbigint(0)
bitset = mkbs()
fmtinstall(typeof(bigint), bigfmt, [][:])
fmtinstall(typeof(bitset), bsfmt, [][:])
+ fmtinstall(typeof(ipaddr), ipfmt, [][:])
bigfree(bigint)
bsfree(bitset)
}
@@ -56,3 +59,21 @@
;;
sbputs(sb, "}")
}
+
+const ipfmt = {sb, ap, opts
+ var ip : netaddr
+ var sep = ""
+
+ ip = std.vanext(ap)
+ match ip
+ | `Ipv4 a:
+ std.put("{}.{}.{}.{}", a[0], a[1], a[2], a[3])
+ | `Ipv6 a:
+ sep = ""
+ for var i = 0; i < 16; i += 2
+ std.put("{}{w=2,p=0,x}{w=2,p=0,x}", sep, a[i+0], a[i+1])
+ sep = ":"
+ ;;
+ ;;
+}
+
--- a/lib/std/resolve+posixy.myr
+++ b/lib/std/resolve+posixy.myr
@@ -36,6 +36,7 @@
`DnsMX /* mail exchange */
`DnsTXT /* text strings */
`DnsAAAA /* ipv6 host address */
+ `DnsInval /* invalid */
;;
type resolveerr = union
@@ -72,6 +73,7 @@
}
const resolve = {host
+ /*TODO: v4 and v6 */
-> resolverec(host, `DnsA)
}
@@ -84,7 +86,7 @@
| `Some hinf:
-> `Ok sldup([hinf][:])
| `None:
- -> dnsresolve(host, rectype(`DnsA))
+ -> dnsresolve(host, rectype(t))
;;
}
@@ -325,42 +327,55 @@
}
const hosts = {pkt, id : uint16
- var off
- var v, q, a
+ var off, ni
+ var r, n, q, a, t
var hinf : hostinfo[:]
+ var v6dat : byte[16]
off = 0
/* parse header */
- (v, off) = unpack16(pkt, off) /* id */
- if v != id
+ (r, off) = unpack16(pkt, off) /* id */
+ if r != id
-> `Err `Badresp
;;
- (v, off) = unpack16(pkt, off) /* flags */
+ (_, off) = unpack16(pkt, off) /* flags */
(q, off) = unpack16(pkt, off) /* qdcount */
(a, off) = unpack16(pkt, off) /* ancount */
- (v, off) = unpack16(pkt, off) /* nscount */
- (v, off) = unpack16(pkt, off) /* arcount */
+ (_, off) = unpack16(pkt, off) /* nscount */
+ (_, off) = unpack16(pkt, off) /* arcount */
/* skip past query records */
for var i = 0; i < q; i++
off = skipname(pkt, off) /* name */
- (v, off) = unpack16(pkt, off) /* type */
- (v, off) = unpack16(pkt, off) /* class */
+ (_, off) = unpack16(pkt, off) /* type */
+ (_, off) = unpack16(pkt, off) /* class */
;;
/* parse answer records */
+ ni = 0
hinf = slalloc((a : size))
for var i = 0; i < a; i++
off = skipname(pkt, off) /* name */
- (v, off) = unpack16(pkt, off) /* type */
- (v, off) = unpack16(pkt, off) /* class */
- (hinf[i].ttl, off) = unpack32(pkt, off) /* ttl */
- (v, off) = unpack16(pkt, off) /* rdatalen */
- /* the thing we're interested in: our IP address */
- hinf[i].addr = `Ipv4 [pkt[off], pkt[off+1], pkt[off+2], pkt[off+3]]
- off += 4;
+ (t, off) = unpack16(pkt, off) /* type */
+ (_, off) = unpack16(pkt, off) /* class */
+ (hinf[ni].ttl, off) = unpack32(pkt, off) /* ttl */
+ (n, off) = unpack16(pkt, off) /* rdatalen */
+ match id2type(t)
+ | `DnsA:
+ /* the thing we're interested in: our IP address */
+ hinf[ni].addr = `Ipv4 [pkt[off], pkt[off+1], pkt[off+2], pkt[off+3]]
+ off += 4;
+ ni++
+ | `DnsAAAA:
+ std.slcp(v6dat[:], pkt[off:off+16])
+ hinf[ni].addr = `Ipv6 v6dat
+ off += 16
+ ni++
+ | _:
+ off += (n : std.size)
+ ;;
;;
- -> `Ok hinf
+ -> `Ok hinf[:ni]
}
@@ -454,6 +469,23 @@
-> true
}
+const id2type = {rtype
+ match rtype
+ | 1: -> `DnsA
+ | 2: -> `DnsNS
+ | 5: -> `DnsCNAME
+ | 6: -> `DnsSOA
+ | 11: -> `DnsWKS
+ | 12: -> `DnsPTR
+ | 13: -> `DnsHINFO
+ | 14: -> `DnsMINFO
+ | 15: -> `DnsMX
+ | 16: -> `DnsTXT
+ | 28: -> `DnsAAAA
+ | _: -> `DnsInval
+ ;;
+}
+
const rectype = {rtype
match rtype
| `DnsA: -> 1 /* host address */
@@ -467,6 +499,7 @@
| `DnsMX: -> 15 /* mail exchange */
| `DnsTXT: -> 16 /* text strings */
| `DnsAAAA: -> 28 /* ipv6 host address */
+ | `DnsInval: -> -1
;;
}