shithub: riscv

Download patch

ref: 2899b719ae11e4ae18fef3995cdb84c64ac60bc3
parent: 2d56837b2fabf6a7cc11f02b51f20a1e35128c57
author: cinap_lenrek <[email protected]>
date: Wed Nov 3 15:38:36 EDT 2021

libndb: move mkptrname() into libndb to avoid duplication

--- a/sys/include/ndb.h
+++ b/sys/include/ndb.h
@@ -128,6 +128,7 @@
 Ndbtuple*	csipinfo(char*, char*, char*, char**, int);
 Ndbtuple*	dnsquery(char*, char*, char*);
 char*		ipattr(char*);
+void		mkptrname(char *ip, char *rip, int rlen);
 Ndb*		ndbcat(Ndb*, Ndb*);
 int		ndbchanged(Ndb*);
 void		ndbclose(Ndb*);
--- a/sys/man/2/ndb
+++ b/sys/man/2/ndb
@@ -1,6 +1,6 @@
 .TH NDB 2
 .SH NAME
-ndbopen, ndbcat, ndbchanged, ndbclose, ndbreopen, ndbsearch, ndbsnext, ndbgetvalue, ndbfree, ipattr, ndbgetipaddr, ndbipinfo, csipinfo, ndbhash, ndbparse, csgetvalue, ndbfindattr, dnsquery, ndbdiscard, ndbconcatenate, ndbreorder, ndbsubstitute, ndbdedup \- network database
+ndbopen, ndbcat, ndbchanged, ndbclose, ndbreopen, ndbsearch, ndbsnext, ndbgetvalue, ndbfree, ipattr, mkptrname, ndbgetipaddr, ndbipinfo, csipinfo, ndbhash, ndbparse, csgetvalue, ndbfindattr, dnsquery, ndbdiscard, ndbconcatenate, ndbreorder, ndbsubstitute, ndbdedup \- network database
 .SH SYNOPSIS
 .B #include <u.h>
 .br
@@ -47,6 +47,9 @@
 char*	ipattr(char *name)
 .PP
 .B
+void	mkptrname(char *ip, char *rip, int rlen);
+.PP
+.B
 Ndbtuple*	ndbgetipaddr(Ndb *db, char *sys);
 .PP
 .B
@@ -235,6 +238,26 @@
 .B sys
 system name
 .RE
+.PP
+.I Mkptrname
+converts the address string
+.I ip
+to a reverse lookup domain-name, returned in
+.IR rip .
+The
+.I rlen
+argument gives the maximum size of the
+.I rip
+buffer including the NUL-terminator.
+If
+.I ip
+already is a reverse lookup domain-name
+or has invalid ip address syntax,
+then
+.I ip
+is copied into
+.I rip
+verbatim.
 .PP
 .I Ndbgetipaddr
 looks in
--- a/sys/src/cmd/ndb/dnsdebug.c
+++ b/sys/src/cmd/ndb/dnsdebug.c
@@ -129,32 +129,6 @@
 	return x;
 }
 
-/*
- *  convert address into a reverse lookup address
- */
-static void
-mkptrname(char *ip, char *rip, int rlen)
-{
-	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");
-	}
-}
-
 int
 prettyrrfmt(Fmt *f)
 {
--- a/sys/src/cmd/ndb/dnsquery.c
+++ b/sys/src/cmd/ndb/dnsquery.c
@@ -4,7 +4,6 @@
 #include <ctype.h>
 #include <ndb.h>
 #include "dns.h"
-#include "ip.h"
 
 static void
 querydns(int fd, char *line, int n)
@@ -20,32 +19,6 @@
 	while((n = read(fd, buf, sizeof(buf)-1)) > 0){
 		buf[n++] = '\n';
 		write(1, buf, n);
-	}
-}
-
-/*
- *  convert address into a reverse lookup address
- */
-static void
-mkptrname(char *ip, char *rip, int rlen)
-{
-	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");
 	}
 }
 
--- a/sys/src/libndb/dnsquery.c
+++ b/sys/src/libndb/dnsquery.c
@@ -5,7 +5,6 @@
 #include <ndbhf.h>
 #include <ip.h>
 
-static void mkptrname(char*, char*, int);
 static Ndbtuple *doquery(int, char *dn, char *type);
 
 /*
@@ -49,32 +48,6 @@
 	close(fd);
 	ndbsetmalloctag(t, getcallerpc(&net));
 	return t;
-}
-
-/*
- *  convert address into a reverse lookup address
- */
-static void
-mkptrname(char *ip, char *rip, int rlen)
-{
-	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");
-	}
 }
 
 static Ndbtuple*
--- a/sys/src/libndb/mkfile
+++ b/sys/src/libndb/mkfile
@@ -6,6 +6,7 @@
 	csipinfo.$O\
 	dnsquery.$O\
 	ipattr.$O\
+	mkptrname.$O\
 	ndbaux.$O\
 	ndbcache.$O\
 	ndbcat.$O\
--- /dev/null
+++ b/sys/src/libndb/mkptrname.c
@@ -1,0 +1,29 @@
+#include <u.h>
+#include <libc.h>
+#include <ip.h>
+
+/*
+ *  convert address into a reverse lookup address
+ */
+void
+mkptrname(char *ip, char *rip, int rlen)
+{
+	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");
+	}
+}