summaryrefslogtreecommitdiff
path: root/sys/src/libndb/ndbgetipaddr.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/libndb/ndbgetipaddr.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/libndb/ndbgetipaddr.c')
-rwxr-xr-xsys/src/libndb/ndbgetipaddr.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/sys/src/libndb/ndbgetipaddr.c b/sys/src/libndb/ndbgetipaddr.c
new file mode 100755
index 000000000..db3fbb018
--- /dev/null
+++ b/sys/src/libndb/ndbgetipaddr.c
@@ -0,0 +1,49 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <ndb.h>
+#include <ip.h>
+
+/* return list of ip addresses for a name */
+Ndbtuple*
+ndbgetipaddr(Ndb *db, char *val)
+{
+ char *attr, *p;
+ Ndbtuple *it, *first, *last, *next;
+ Ndbs s;
+
+ /* already an IP address? */
+ attr = ipattr(val);
+ if(strcmp(attr, "ip") == 0){
+ it = ndbnew("ip", val);
+ ndbsetmalloctag(it, getcallerpc(&db));
+ return it;
+ }
+
+ /* look it up */
+ p = ndbgetvalue(db, &s, attr, val, "ip", &it);
+ if(p == nil)
+ return nil;
+ free(p);
+
+ /* remove the non-ip entries */
+ first = last = nil;
+ for(; it; it = next){
+ next = it->entry;
+ if(strcmp(it->attr, "ip") == 0){
+ if(first == nil)
+ first = it;
+ else
+ last->entry = it;
+ it->entry = nil;
+ it->line = first;
+ last = it;
+ } else {
+ it->entry = nil;
+ ndbfree(it);
+ }
+ }
+
+ ndbsetmalloctag(first, getcallerpc(&db));
+ return first;
+}