]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libndb/ndbgetipaddr.c
libndb: make ndbipinfo() walk ipnet for all matching entries, concatenate and dedup...
[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                 it->line = it;
20                 ndbsetmalloctag(it, getcallerpc(&db));
21                 return it;
22         }
23
24         /* look it up */
25         p = ndbgetvalue(db, &s, attr, val, "ip", &it);
26         if(p == nil)
27                 return nil;
28         free(p);
29         first = last = nil;
30         do {
31                 /* remove the non-ip entries */
32                 for(; it != nil; it = next){
33                         next = it->entry;
34                         if(strcmp(it->attr, "ip") == 0){
35                                 if(first == nil)
36                                         first = it;
37                                 else {
38                                         last->entry = it;
39                                         last->line = it;
40                                 }
41                                 it->entry = nil;
42                                 it->line = first;
43                                 last = it;
44                         } else {
45                                 it->entry = nil;
46                                 ndbfree(it);
47                         }
48                 }
49         } while((it = ndbsnext(&s, attr, val)) != nil);
50
51         first = ndbdedup(first);
52
53         ndbsetmalloctag(first, getcallerpc(&db));
54         return first;
55 }