ref: 49c4706703a9e3cebc6ea39e8bf4ba0f6a1d43ab
parent: 9014efa1621b6530c070ff55b26c7195910fe4ae
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Mar 21 20:33:08 EDT 2016
Improve error handling for dial/resolve.
--- a/lib/std/dial+posixy.myr
+++ b/lib/std/dial+posixy.myr
@@ -137,8 +137,13 @@
| `None:
match resolve(addr)
| `Ok hi:
- ip = hi[0].addr
- slfree(hi)
+ if hi.len > 0
+ ip = hi[0].addr
+ slfree(hi)
+ else
+ slfree(hi)
+ -> `Fail "no hosts resolved for addr"
+ ;;
| `Fail m:
-> `Fail "could not get host info"
;;
--- a/lib/std/resolve+posixy.myr
+++ b/lib/std/resolve+posixy.myr
@@ -20,6 +20,8 @@
use "types"
use "utf"
+use "fmt"
+
pkg std =
type rectype = union
`DnsA /* host address */
@@ -36,6 +38,7 @@
;;
type resolveerr = union
+ `Badconn
`Badhost
`Badsrv
`Badquery
@@ -278,6 +281,7 @@
/* header */
off = 0
+ nextid++
off += pack16(pkt[:], off, nextid) /* id */
off += pack16(pkt[:], off, Ra) /* flags */
off += pack16(pkt[:], off, 1) /* qdcount */
@@ -291,7 +295,7 @@
off += pack16(pkt[:], off, 0x1) /* qclass: inet4 */
sys.write(srv, pkt[:off])
- -> nextid++
+ -> nextid
}
const rquery = {srv, id
@@ -301,6 +305,7 @@
n = sys.read(srv, pktbuf[:])
if n < 0
+ -> `Fail `Badconn
;;
pkt = pktbuf[:n]
-> hosts(pkt, id)
@@ -315,7 +320,7 @@
/* parse header */
(v, off) = unpack16(pkt, off) /* id */
if v != id
- -> `Fail (`Badresp)
+ -> `Fail `Badresp
;;
(v, off) = unpack16(pkt, off) /* flags */
(q, off) = unpack16(pkt, off) /* qdcount */