chiark / gitweb /
slip: Report unexpected kinds of death from userv
[secnet.git] / resolver.c
index d6bc619..a39e9fc 100644 (file)
@@ -20,6 +20,7 @@ struct adns {
 
 struct query {
     void *cst;
+    const char *name;
     int port;
     struct comm_if *comm;
     resolve_answer_fn *answer;
@@ -34,8 +35,13 @@ static bool_t resolve_request(void *sst, cstring_t name,
     struct adns *st=sst;
     struct query *q;
     int rv;
-    const int maxlitlen=50;
-
+    const int maxlitlen=
+#ifdef CONFIG_IPV6
+       ADNS_ADDR2TEXT_BUFLEN*2
+#else
+       50
+#endif
+       ;
     ssize_t l=strlen(name);
     if (name[0]=='[' && l<maxlitlen && l>2 && name[l-1]==']') {
        char trimmed[maxlitlen+1];
@@ -43,12 +49,29 @@ static bool_t resolve_request(void *sst, cstring_t name,
        trimmed[l-2]=0;
        struct comm_addr ca;
        ca.comm=comm;
+       ca.ix=-1;
+#ifdef CONFIG_IPV6
+       socklen_t salen=sizeof(ca.ia);
+       rv=adns_text2addr(trimmed, port, adns_qf_addrlit_ipv4_quadonly,
+                         &ca.ia.sa, &salen);
+       assert(rv!=ENOSPC);
+       if (rv) {
+           char msg[250];
+           snprintf(msg,sizeof(msg),"invalid address literal: %s",
+                    strerror(rv));
+           msg[sizeof(msg)-1]=0;
+           cb(cst,0,0,0,name,msg);
+       } else {
+           cb(cst,&ca,1,1,name,0);
+       }
+#else
        ca.ia.sin.sin_family=AF_INET;
        ca.ia.sin.sin_port=htons(port);
        if (inet_aton(trimmed,&ca.ia.sin.sin_addr))
-           cb(cst,&ca,1,1);
+           cb(cst,&ca,1,1,name,0);
        else
-           cb(cst,0,0,0);
+           cb(cst,0,0,0,name,"invalid IP address");
+#endif
        return True;
     }
 
@@ -56,6 +79,7 @@ static bool_t resolve_request(void *sst, cstring_t name,
     q->cst=cst;
     q->comm=comm;
     q->port=port;
+    q->name=name;
     q->answer=cb;
 
     rv=adns_submit(st->ast, name, adns_r_addr, 0, q, &q->query);
@@ -94,7 +118,7 @@ static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
        if (rv==0) {
            q=qp;
            if (ans->status!=adns_s_ok) {
-               q->answer(q->cst,NULL,0,0); /* Failure */
+               q->answer(q->cst,NULL,0,0,q->name,adns_strerror(ans->status));
                free(q);
                free(ans);
            } else {
@@ -109,10 +133,16 @@ static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
                    adns_rr_addr *ra=&ans->rrs.addr[rslot];
                    struct comm_addr *ca=&ca_buf[wslot];
                    ca->comm=q->comm;
+                   ca->ix=-1;
                    switch (ra->addr.sa.sa_family) {
                    case AF_INET:
                        assert(ra->len == sizeof(ca->ia.sin));
                        break;
+#ifdef CONFIG_IPV6
+                   case AF_INET6:
+                       assert(ra->len == sizeof(ca->ia.sin6));
+                       break;
+#endif /*CONFIG_IPV6*/
                    default:
                        /* silently skip unexpected AFs from adns */
                        continue;
@@ -120,7 +150,7 @@ static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
                    memcpy(&ca->ia,&ra->addr,ra->len);
                    wslot++;
                }
-               q->answer(q->cst,ca_buf,wslot,total);
+               q->answer(q->cst,ca_buf,wslot,total,q->name,0);
                free(q);
                free(ans);
            }
@@ -170,7 +200,7 @@ static list_t *adnsresolver_apply(closure_t *self, struct cloc loc,
     }
 
     register_for_poll(st, resolver_beforepoll, resolver_afterpoll,
-                     ADNS_POLLFDS_RECOMMENDED+5,"resolver");
+                     "resolver");
 
     return new_closure(&st->cl);
 }