csipinfo.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <ndbhf.h>
  14. /*
  15. * look up the ip attributes 'list' for an entry that has the
  16. * given 'attr=val' and a 'ip=' tuples.
  17. *
  18. * return nil if not found.
  19. */
  20. Ndbtuple*
  21. csipinfo(char *netroot, char *attr, char *val, char **list, int n)
  22. {
  23. Ndbtuple *t, *first, *last;
  24. int i;
  25. char line[1024];
  26. int fd;
  27. char *p, *e;
  28. if(netroot)
  29. snprint(line, sizeof(line), "%s/cs", netroot);
  30. else
  31. strcpy(line, "/net/cs");
  32. fd = open(line, ORDWR);
  33. if(fd < 0)
  34. return 0;
  35. seek(fd, 0, 0);
  36. e = line + sizeof(line);
  37. p = seprint(line, e, "!ipinfo %s=%s", attr, val);
  38. for(i = 0; i < n; i++){
  39. if(*list == nil)
  40. break;
  41. p = seprint(p, e, " %s", *list++);
  42. }
  43. if(write(fd, line, strlen(line)) < 0){
  44. close(fd);
  45. return 0;
  46. }
  47. seek(fd, 0, 0);
  48. first = last = 0;
  49. for(;;){
  50. n = read(fd, line, sizeof(line)-2);
  51. if(n <= 0)
  52. break;
  53. line[n] = '\n';
  54. line[n+1] = 0;
  55. t = _ndbparseline(line);
  56. if(t == 0)
  57. continue;
  58. if(first)
  59. last->entry = t;
  60. else
  61. first = t;
  62. last = t;
  63. while(last->entry)
  64. last = last->entry;
  65. }
  66. close(fd);
  67. ndbsetmalloctag(first, getcallerpc());
  68. return first;
  69. }