]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libndb/ndbgetipaddr.c
merge
[plan9front.git] / sys / src / libndb / ndbgetipaddr.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ndb.h>
5 #include <ip.h>
6
7 /* return list of ip addresses for a name */
8 Ndbtuple*
9 ndbgetipaddr(Ndb *db, char *val)
10 {
11         char *attr, *p;
12         Ndbtuple *it, *first, *last, *next;
13         Ndbs s;
14
15         /* already an IP address? */
16         attr = ipattr(val);
17         if(strcmp(attr, "ip") == 0){
18                 it = ndbnew("ip", val);
19                 ndbsetmalloctag(it, getcallerpc(&db));
20                 return it;
21         }
22
23         /* look it up */
24         p = ndbgetvalue(db, &s, attr, val, "ip", &it);
25         if(p == nil)
26                 return nil;
27         free(p);
28
29         /* remove the non-ip entries */
30         first = last = nil;
31         for(; it; it = next){
32                 next = it->entry;
33                 if(strcmp(it->attr, "ip") == 0){
34                         if(first == nil)
35                                 first = it;
36                         else
37                                 last->entry = it;
38                         it->entry = nil;
39                         it->line = first;
40                         last = it;
41                 } else {
42                         it->entry = nil;
43                         ndbfree(it);
44                 }
45         }
46
47         ndbsetmalloctag(first, getcallerpc(&db));
48         return first;
49 }