#include <bio.h>
#include <ndb.h>
#include <ndbhf.h>
+#include <ip.h>
-static void nstrcpy(char*, char*, int);
static void mkptrname(char*, char*, int);
static Ndbtuple *doquery(int, char *dn, char *type);
* search for a tuple that has the given 'attr=val' and also 'rattr=x'.
* copy 'x' into 'buf' and return the whole tuple.
*
- * return 0 if not found.
+ * return nil if not found.
*/
Ndbtuple*
dnsquery(char *net, char *val, char *type)
{
- char rip[128];
- char *p;
+ char buf[128];
Ndbtuple *t;
int fd;
if(net == nil)
net = "/net";
- snprint(rip, sizeof(rip), "%s/dns", net);
- fd = open(rip, ORDWR);
- if(fd < 0){
- if(strcmp(net, "/net") == 0)
- snprint(rip, sizeof(rip), "/srv/dns");
- else {
- snprint(rip, sizeof(rip), "/srv/dns%s", net);
- p = strrchr(rip, '/');
- *p = '_';
- }
- fd = open(rip, ORDWR);
- if(fd < 0)
- return nil;
- if(mount(fd, -1, net, MBEFORE, "") < 0){
- close(fd);
- return nil;
- }
- /* fd is now closed */
- snprint(rip, sizeof(rip), "%s/dns", net);
- fd = open(rip, ORDWR);
- if(fd < 0)
- return nil;
- }
+
+ snprint(buf, sizeof(buf), "%s/dns", net);
+ if((fd = open(buf, ORDWR|OCEXEC)) < 0)
+ return nil;
/* zero out the error string */
werrstr("");
/* if this is a reverse lookup, first lookup the domain name */
if(strcmp(type, "ptr") == 0){
- mkptrname(val, rip, sizeof rip);
- t = doquery(fd, rip, "ptr");
+ mkptrname(val, buf, sizeof buf);
+ t = doquery(fd, buf, "ptr");
} else
t = doquery(fd, val, type);
static void
mkptrname(char *ip, char *rip, int rlen)
{
- char buf[128];
- char *p, *np;
- int len;
-
- if(strstr(ip, "in-addr.arpa") || strstr(ip, "IN-ADDR.ARPA")){
- nstrcpy(rip, ip, rlen);
- return;
- }
-
- nstrcpy(buf, ip, sizeof buf);
- for(p = buf; *p; p++)
- ;
- *p = '.';
- np = rip;
- len = 0;
- while(p >= buf){
- len++;
- p--;
- if(*p == '.'){
- memmove(np, p+1, len);
- np += len;
- len = 0;
+ uchar a[IPaddrlen];
+ char *p, *e;
+ int i;
+
+ if(cistrstr(ip, "in-addr.arpa") || cistrstr(ip, "ip6.arpa") || parseip(a, ip) == -1)
+ snprint(rip, rlen, "%s", ip);
+ else if(isv4(a))
+ snprint(rip, rlen, "%ud.%ud.%ud.%ud.in-addr.arpa",
+ a[15], a[14], a[13], a[12]);
+ else{
+ p = rip;
+ e = rip + rlen;
+ for(i = 15; i >= 0; i--){
+ p = seprint(p, e, "%ux.", a[i]&0xf);
+ p = seprint(p, e, "%ux.", a[i]>>4);
}
+ seprint(p, e, "ip6.arpa");
}
- memmove(np, p+1, len);
- np += len;
- strcpy(np, "in-addr.arpa");
-}
-
-static void
-nstrcpy(char *to, char *from, int len)
-{
- strncpy(to, from, len);
- to[len-1] = 0;
}
static Ndbtuple*
int n;
Ndbtuple *t, *first, *last;
- seek(fd, 0, 0);
snprint(buf, sizeof(buf), "!%s %s", dn, type);
if(write(fd, buf, strlen(buf)) < 0)
return nil;
t = _ndbparseline(buf);
if(t != nil){
- if(first)
+ if(first != nil)
last->entry = t;
else
first = t;
last = t;
-
- while(last->entry)
+ while(last->entry != nil)
last = last->entry;
}
}
-
- ndbsetmalloctag(first, getcallerpc(&fd));
return first;
}