ndbgetipaddr.c 876 B

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