This commit is contained in:
rsc 2006-01-27 08:56:16 +00:00
parent 6021858725
commit 18d5a36a28

View file

@ -96,56 +96,7 @@ enum
{ {
MAXRR = 100, MAXRR = 100,
MAXDNS = 4096, MAXDNS = 4096,
};
static int name2type(char*);
static uchar *skipquestion(uchar*, uchar*, uchar*, int);
static uchar *unpack(uchar*, uchar*, uchar*, Ndbtuple**, int);
static uchar *rrnext(uchar*, uchar*, uchar*, Ndbtuple**);
static Ndbtuple *rrunpack(uchar*, uchar*, uchar**, char*, ...);
static Ndbtuple*
doquery(char *name, char *type)
{
int n, nstype;
uchar *buf, *p;
HEADER *h;
Ndbtuple *t;
if((nstype = name2type(type)) < 0){
werrstr("unknown dns type %s", type);
return nil;
}
buf = malloc(MAXDNS);
if(buf == nil)
return nil;
if((n = res_search(name, ns_c_in, nstype, buf, MAXDNS)) < 0){
free(buf);
return nil;
}
if(n >= MAXDNS){
free(buf);
werrstr("too much dns information");
return nil;
}
h = (HEADER*)buf;
h->qdcount = ntohs(h->qdcount);
h->ancount = ntohs(h->ancount);
h->nscount = ntohs(h->nscount);
h->arcount = ntohs(h->arcount);
p = buf+sizeof(HEADER);
p = skipquestion(buf, buf+n, p, h->qdcount);
p = unpack(buf, buf+n, p, &t, h->ancount);
USED(p);
return t;
}
enum
{
/* RR types */ /* RR types */
Ta= 1, Ta= 1,
Tns= 2, Tns= 2,
@ -187,6 +138,53 @@ enum
}; };
static int name2type(char*);
static uchar *skipquestion(uchar*, uchar*, uchar*, int);
static uchar *unpack(uchar*, uchar*, uchar*, Ndbtuple**, int);
static uchar *rrnext(uchar*, uchar*, uchar*, Ndbtuple**);
static Ndbtuple *rrunpack(uchar*, uchar*, uchar**, char*, ...);
static Ndbtuple*
doquery(char *name, char *type)
{
int n, nstype;
uchar *buf, *p;
HEADER *h;
Ndbtuple *t;
if((nstype = name2type(type)) < 0){
werrstr("unknown dns type %s", type);
return nil;
}
buf = malloc(MAXDNS);
if(buf == nil)
return nil;
if((n = res_search(name, Cin, nstype, buf, MAXDNS)) < 0){
free(buf);
return nil;
}
if(n >= MAXDNS){
free(buf);
werrstr("too much dns information");
return nil;
}
h = (HEADER*)buf;
h->qdcount = ntohs(h->qdcount);
h->ancount = ntohs(h->ancount);
h->nscount = ntohs(h->nscount);
h->arcount = ntohs(h->arcount);
p = buf+sizeof(HEADER);
p = skipquestion(buf, buf+n, p, h->qdcount);
p = unpack(buf, buf+n, p, &t, h->ancount);
USED(p);
return t;
}
static struct { static struct {
char *s; char *s;
int t; int t;
@ -315,7 +313,7 @@ rrnext(uchar *buf, uchar *ebuf, uchar *p, Ndbtuple **tt)
rrlen = G2(p+8); rrlen = G2(p+8);
p += 10; p += 10;
if(rrtype == ns_t_ptr) if(rrtype == Tptr)
first = ndbnew("ptr", b); first = ndbnew("ptr", b);
else else
first = ndbnew("dom", b); first = ndbnew("dom", b);
@ -323,27 +321,27 @@ rrnext(uchar *buf, uchar *ebuf, uchar *p, Ndbtuple **tt)
switch(rrtype){ switch(rrtype){
default: default:
goto end; goto end;
case ns_t_hinfo: case Thinfo:
t = rrunpack(buf, ebuf, &p, "YY", "cpu", "os"); t = rrunpack(buf, ebuf, &p, "YY", "cpu", "os");
break; break;
case ns_t_minfo: case Tminfo:
t = rrunpack(buf, ebuf, &p, "NN", "mbox", "mbox"); t = rrunpack(buf, ebuf, &p, "NN", "mbox", "mbox");
break; break;
case ns_t_mx: case Tmx:
t = rrunpack(buf, ebuf, &p, "SN", "pref", "mx"); t = rrunpack(buf, ebuf, &p, "SN", "pref", "mx");
break; break;
case ns_t_cname: case Tcname:
case ns_t_md: case Tmd:
case ns_t_mf: case Tmf:
case ns_t_mg: case Tmg:
case ns_t_mr: case Tmr:
case ns_t_mb: case Tmb:
case ns_t_ns: case Tns:
case ns_t_ptr: case Tptr:
case ns_t_rp: case Trp:
t = rrunpack(buf, ebuf, &p, "N", type2name(rrtype)); t = rrunpack(buf, ebuf, &p, "N", type2name(rrtype));
break; break;
case ns_t_a: case Ta:
if(rrlen != IPv4addrlen) if(rrlen != IPv4addrlen)
goto corrupt; goto corrupt;
memmove(ip, v4prefix, IPaddrlen); memmove(ip, v4prefix, IPaddrlen);
@ -352,37 +350,37 @@ rrnext(uchar *buf, uchar *ebuf, uchar *p, Ndbtuple **tt)
t = ndbnew("ip", tmp); t = ndbnew("ip", tmp);
p += rrlen; p += rrlen;
break; break;
case ns_t_aaaa: case Taaaa:
if(rrlen != IPaddrlen) if(rrlen != IPaddrlen)
goto corrupt; goto corrupt;
snprint(tmp, sizeof tmp, "%I", ip); snprint(tmp, sizeof tmp, "%I", ip);
t = ndbnew("ip", tmp); t = ndbnew("ip", tmp);
p += rrlen; p += rrlen;
break; break;
case ns_t_null: case Tnull:
snprint(tmp, sizeof tmp, "%.*H", rrlen, p); snprint(tmp, sizeof tmp, "%.*H", rrlen, p);
t = ndbnew("null", tmp); t = ndbnew("null", tmp);
p += rrlen; p += rrlen;
break; break;
case ns_t_txt: case Ttxt:
t = rrunpack(buf, ebuf, &p, "Y", "txt"); t = rrunpack(buf, ebuf, &p, "Y", "txt");
break; break;
case ns_t_soa: case Tsoa:
t = rrunpack(buf, ebuf, &p, "NNLLLLL", "ns", "mbox", t = rrunpack(buf, ebuf, &p, "NNLLLLL", "ns", "mbox",
"serial", "refresh", "retry", "expire", "ttl"); "serial", "refresh", "retry", "expire", "ttl");
break; break;
case ns_t_key: case Tkey:
t = rrunpack(buf, ebuf, &p, "SCCY", "flags", "proto", "alg", "key"); t = rrunpack(buf, ebuf, &p, "SCCY", "flags", "proto", "alg", "key");
break; break;
case ns_t_sig: case Tsig:
t = rrunpack(buf, ebuf, &p, "SCCLLLSNY", "type", "alg", "labels", t = rrunpack(buf, ebuf, &p, "SCCLLLSNY", "type", "alg", "labels",
"ttl", "exp", "incep", "tag", "signer", "sig"); "ttl", "exp", "incep", "tag", "signer", "sig");
break; break;
case ns_t_cert: case Tcert:
t = rrunpack(buf, ebuf, &p, "SSCY", "type", "tag", "alg", "cert"); t = rrunpack(buf, ebuf, &p, "SSCY", "type", "tag", "alg", "cert");
break; break;
} }