I think I have a working gethostbyaddr:

char*
gethostbyaddr(char *net, char *addr)
{
        // Thanks to erik and rog for their help with this code
        char buf[1024], errbuf[ERRMAX], *dq[4], *host;
        int fd, l;

        if(net == nil)
                net = "/net";
        snprint(buf, sizeof buf, "%s/dns", net);
        if((fd = open(buf, ORDWR)) < 0){
                l = snprint(errbuf, ERRMAX, "gethostbyaddr: can't open %s: %r", 
buf);
                errstr(errbuf, l);
                return nil;
        }
        if(getfields(addr, dq, 4, 1, ".") != 4){
                close(fd);
                werrstr("gethostbyaddr: malformed address");
                return nil;
        }
        l = snprint(buf, sizeof buf, "%s.%s.%s.%s.in-addr.arpa ptr",
                dq[3], dq[2], dq[1], dq[0]);
        if(write(fd, buf, l) != l){
                l = snprint(errbuf, ERRMAX,
                        "gethostbyaddr: write error resolving %s.%s.%s.%s: 
%s/dns: %r",
                        dq[0], dq[1], dq[2], dq[3], net);
                close(fd);
                errstr(errbuf, l);
                return nil;
        }
        if(read(fd, buf, sizeof buf) <= 0){
                l = snprint(errbuf, ERRMAX,
                        "gethostbyaddr: read error resolving %s.%s.%s.%s: 
%s/dns: %r",
                        dq[0], dq[1], dq[2], dq[3], net);
                close(fd);
                errstr(errbuf, l);
                return nil;
        }
        close(fd);
        host = strstr(buf, " ptr ");
        if(host == nil){
                werrstr("gethostbyaddr: dns lookup failed");
                return nil;
        }
        return strdup(host);
}

--Joel

Reply via email to