testlook.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ip.h>
  4. #include <bio.h>
  5. #include <ndb.h>
  6. static uchar noether[6];
  7. /*
  8. * Look for a pair with the given attribute. look first on the same line,
  9. * then in the whole entry.
  10. */
  11. static Ndbtuple*
  12. lookval(Ndbtuple *entry, Ndbtuple *line, char *attr, char *to)
  13. {
  14. Ndbtuple *nt;
  15. /* first look on same line (closer binding) */
  16. for(nt = line;;){
  17. if(strcmp(attr, nt->attr) == 0){
  18. strncpy(to, nt->val, Ndbvlen);
  19. return nt;
  20. }
  21. nt = nt->line;
  22. if(nt == line)
  23. break;
  24. }
  25. /* search whole tuple */
  26. for(nt = entry; nt; nt = nt->entry)
  27. if(strcmp(attr, nt->attr) == 0){
  28. strncpy(to, nt->val, Ndbvlen);
  29. return nt;
  30. }
  31. return 0;
  32. }
  33. /*
  34. * lookup an ip address
  35. */
  36. static uchar*
  37. lookupip(Ndb *db, char *name, uchar *to, Ipinfo *iip)
  38. {
  39. Ndbtuple *t, *nt;
  40. char buf[Ndbvlen];
  41. uchar subnet[IPaddrlen];
  42. Ndbs s;
  43. char *attr;
  44. attr = ipattr(name);
  45. if(strcmp(attr, "ip") == 0){
  46. parseip(to, name);
  47. return to;
  48. }
  49. t = ndbgetval(db, &s, attr, name, "ip", buf);
  50. if(t){
  51. /* first look for match on same subnet */
  52. for(nt = t; nt; nt = nt->entry){
  53. if(strcmp(nt->attr, "ip") != 0)
  54. continue;
  55. parseip(to, nt->val);
  56. maskip(to, iip->ipmask, subnet);
  57. if(memcmp(subnet, iip->ipnet, sizeof(subnet)) == 0)
  58. return to;
  59. }
  60. /* otherwise, just take what we have */
  61. ndbfree(t);
  62. parseip(to, buf);
  63. return to;
  64. }
  65. return 0;
  66. }
  67. /*
  68. * lookup a subnet and fill in anything we can
  69. */
  70. static void
  71. recursesubnet(Ndb *db, uchar *mask, Ipinfo *iip, char *fs, char *gw, char *au)
  72. {
  73. Ndbs s;
  74. Ndbtuple *t;
  75. uchar submask[IPaddrlen];
  76. char ip[Ndbvlen];
  77. memmove(iip->ipmask, mask, 4);
  78. maskip(iip->ipaddr, iip->ipmask, iip->ipnet);
  79. sprint(ip, "%I", iip->ipnet);
  80. t = ndbsearch(db, &s, "ip", ip);
  81. print("%s->", ip);
  82. if(t){
  83. /* look for a further subnet */
  84. if(lookval(t, s.t, "ipmask", ip)){
  85. parseip(submask, ip);
  86. /* recurse only if it has changed */
  87. if(!equivip(submask, mask))
  88. recursesubnet(db, submask, iip, fs, gw, au);
  89. }
  90. /* fill in what we don't have */
  91. if(gw[0] == 0)
  92. lookval(t, s.t, "ipgw", gw);
  93. if(fs[0] == 0)
  94. lookval(t, s.t, "fs", fs);
  95. if(au[0] == 0)
  96. lookval(t, s.t, "auth", au);
  97. ndbfree(t);
  98. }
  99. }
  100. #ifdef foo
  101. /*
  102. * find out everything we can about a system from what has been
  103. * specified.
  104. */
  105. int
  106. ipinfo(Ndb *db, char *etherin, char *ipin, char *name, Ipinfo *iip)
  107. {
  108. Ndbtuple *t;
  109. Ndbs s;
  110. char ether[Ndbvlen];
  111. char ip[Ndbvlen];
  112. char fsname[Ndbvlen];
  113. char gwname[Ndbvlen];
  114. char auname[Ndbvlen];
  115. memset(iip, 0, sizeof(Ipinfo));
  116. fsname[0] = 0;
  117. gwname[0] = 0;
  118. auname[0] = 0;
  119. /*
  120. * look for a matching entry
  121. */
  122. t = 0;
  123. if(etherin)
  124. t = ndbgetval(db, &s, "ether", etherin, "ip", ip);
  125. if(t == 0 && ipin)
  126. t = ndbsearch(db, &s, "ip", ipin);
  127. if(t == 0 && name)
  128. t = ndbgetval(db, &s, ipattr(name), name, "ip", ip);
  129. if(t){
  130. /*
  131. * copy in addresses and name
  132. */
  133. if(lookval(t, s.t, "ip", ip))
  134. parseip(iip->ipaddr, ip);
  135. if(lookval(t, s.t, "ether", ether))
  136. parseether(iip->etheraddr, ether);
  137. lookval(t, s.t, "dom", iip->domain);
  138. /*
  139. * Look for bootfile, fs, and gateway.
  140. * If necessary, search through all entries for
  141. * this ip address.
  142. */
  143. while(t){
  144. if(iip->bootf[0] == 0)
  145. lookval(t, s.t, "bootf", iip->bootf);
  146. if(fsname[0] == 0)
  147. lookval(t, s.t, "fs", fsname);
  148. if(gwname[0] == 0)
  149. lookval(t, s.t, "ipgw", gwname);
  150. if(auname[0] == 0)
  151. lookval(t, s.t, "auth", auname);
  152. ndbfree(t);
  153. if(iip->bootf[0] && fsname[0] && gwname[0] && auname[0])
  154. break;
  155. t = ndbsnext(&s, "ether", ether);
  156. }
  157. } else if(ipin) {
  158. /*
  159. * copy in addresses (all we know)
  160. */
  161. parseip(iip->ipaddr, ipin);
  162. if(etherin)
  163. parseether(iip->etheraddr, etherin);
  164. } else
  165. return -1;
  166. /*
  167. * Look up the client's network and find a subnet mask for it.
  168. * Fill in from the subnet (or net) entry anything we can't figure
  169. * out from the client record.
  170. */
  171. recursesubnet(db, classmask[CLASS(iip->ipaddr)], iip, fsname, gwname, auname);
  172. /* lookup fs's and gw's ip addresses */
  173. if(fsname[0])
  174. lookupip(db, fsname, iip->fsip, iip);
  175. if(gwname[0])
  176. lookupip(db, gwname, iip->gwip, iip);
  177. if(auname[0])
  178. lookupip(db, auname, iip->auip, iip);
  179. return 0;
  180. }
  181. #endif
  182. void
  183. main(int argc, char **argv)
  184. {
  185. Ipinfo ii;
  186. Ndb *db;
  187. db = ndbopen(0);
  188. fmtinstall('E', eipconv);
  189. fmtinstall('I', eipconv);
  190. if(argc < 2)
  191. exits(0);
  192. if(strchr(argv[1], '.')){
  193. if(ipinfo(db, 0, argv[1], 0, &ii) < 0)
  194. exits(0);
  195. } else {
  196. if(ipinfo(db, argv[1], 0, 0, &ii) < 0)
  197. exits(0);
  198. }
  199. fprint(2, "a %I m %I n %I f %s e %E\n", ii.ipaddr,
  200. ii.ipmask, ii.ipnet, ii.bootf, ii.etheraddr);
  201. }