ndbgetipaddr.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <ndb.h>
  13. #include <ip.h>
  14. /* return list of ip addresses for a name */
  15. Ndbtuple*
  16. ndbgetipaddr(Ndb *db, char *val)
  17. {
  18. char *attr, *p;
  19. Ndbtuple *it, *first, *last, *next;
  20. Ndbs s;
  21. /* already an IP address? */
  22. attr = ipattr(val);
  23. if(strcmp(attr, "ip") == 0){
  24. it = ndbnew("ip", val);
  25. ndbsetmalloctag(it, getcallerpc());
  26. return it;
  27. }
  28. /* look it up */
  29. p = ndbgetvalue(db, &s, attr, val, "ip", &it);
  30. if(p == nil)
  31. return nil;
  32. free(p);
  33. /* remove the non-ip entries */
  34. first = last = nil;
  35. for(; it; it = next){
  36. next = it->entry;
  37. if(strcmp(it->attr, "ip") == 0){
  38. if(first == nil)
  39. first = it;
  40. else
  41. last->entry = it;
  42. it->entry = nil;
  43. it->line = first;
  44. last = it;
  45. } else {
  46. it->entry = nil;
  47. ndbfree(it);
  48. }
  49. }
  50. ndbsetmalloctag(first, getcallerpc());
  51. return first;
  52. }