csipinfo.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ndb.h>
  5. #include <ndbhf.h>
  6. /*
  7. * look up the ip attributes 'list' for an entry that has the
  8. * given 'attr=val' and a 'ip=' tuples.
  9. *
  10. * return nil if not found.
  11. */
  12. Ndbtuple*
  13. csipinfo(char *netroot, char *attr, char *val, char **list, int n)
  14. {
  15. Ndbtuple *t, *first, *last;
  16. int i;
  17. char line[1024];
  18. int fd;
  19. char *p, *e;
  20. if(netroot)
  21. snprint(line, sizeof(line), "%s/cs", netroot);
  22. else
  23. strcpy(line, "/net/cs");
  24. fd = open(line, ORDWR);
  25. if(fd < 0)
  26. return 0;
  27. seek(fd, 0, 0);
  28. e = line + sizeof(line);
  29. p = seprint(line, e, "!ipinfo %s=%s", attr, val);
  30. for(i = 0; i < n; i++){
  31. if(*list == nil)
  32. break;
  33. p = seprint(p, e, " %s", *list++);
  34. }
  35. if(write(fd, line, strlen(line)) < 0){
  36. close(fd);
  37. return 0;
  38. }
  39. seek(fd, 0, 0);
  40. first = last = 0;
  41. for(;;){
  42. n = read(fd, line, sizeof(line)-2);
  43. if(n <= 0)
  44. break;
  45. line[n] = '\n';
  46. line[n+1] = 0;
  47. t = _ndbparseline(line);
  48. if(t == 0)
  49. continue;
  50. if(first)
  51. last->entry = t;
  52. else
  53. first = t;
  54. last = t;
  55. while(last->entry)
  56. last = last->entry;
  57. }
  58. close(fd);
  59. return first;
  60. }