ndbgetipaddr.c 792 B

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