plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

ndbgetipaddr.c (792B)


      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 		return it;
     20 	}
     21 
     22 	/* look it up */
     23 	p = ndbgetvalue(db, &s, attr, val, "ip", &it);
     24 	if(p == nil)
     25 		return nil;
     26 	free(p);
     27 
     28 	/* remove the non-ip entries */
     29 	first = last = nil;
     30 	for(; it; it = next){
     31 		next = it->entry;
     32 		if(strcmp(it->attr, "ip") == 0){
     33 			if(first == nil)
     34 				first = it;
     35 			else
     36 				last->entry = it;
     37 			it->entry = nil;
     38 			it->line = first;
     39 			last = it;
     40 		} else {
     41 			it->entry = nil;
     42 			ndbfree(it);
     43 		}
     44 	}
     45 
     46 	return first;
     47 }