ndbipinfo.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ndb.h>
  5. #include <ip.h>
  6. enum
  7. {
  8. Ffound= 1<<0,
  9. Fignore=1<<1,
  10. Faddr= 1<<2,
  11. };
  12. static Ndbtuple* filter(Ndb *db, Ndbtuple *t, Ndbtuple *f);
  13. static Ndbtuple* mkfilter(int argc, char **argv);
  14. static int filtercomplete(Ndbtuple *f);
  15. static Ndbtuple* toipaddr(Ndb *db, Ndbtuple *t);
  16. static int prefixlen(uchar *ip);
  17. static Ndbtuple* subnet(Ndb *db, uchar *net, Ndbtuple *f, int prefix);
  18. /* make a filter to be used in filter */
  19. static Ndbtuple*
  20. mkfilter(int argc, char **argv)
  21. {
  22. Ndbtuple *t, *first, *last;
  23. char *p;
  24. last = first = nil;
  25. while(argc-- > 0){
  26. t = ndbnew(0, 0);
  27. if(first)
  28. last->entry = t;
  29. else
  30. first = t;
  31. last = t;
  32. p = *argv++;
  33. if(*p == '@'){
  34. t->ptr |= Faddr;
  35. p++;
  36. }
  37. strncpy(t->attr, p, sizeof(t->attr)-1);
  38. }
  39. return first;
  40. }
  41. /* return true if every pair of filter has been used */
  42. static int
  43. filtercomplete(Ndbtuple *f)
  44. {
  45. for(; f; f = f->entry)
  46. if((f->ptr & Fignore) == 0)
  47. return 0;
  48. return 1;
  49. }
  50. /* set the attribute of all entries in a tuple */
  51. static Ndbtuple*
  52. setattr(Ndbtuple *t, char *attr)
  53. {
  54. Ndbtuple *nt;
  55. for(nt = t; nt; nt = nt->entry)
  56. strcpy(nt->attr, attr);
  57. return t;
  58. }
  59. /*
  60. * return only the attr/value pairs in t maching the filter, f.
  61. * others are freed. line structure is preserved.
  62. */
  63. static Ndbtuple*
  64. filter(Ndb *db, Ndbtuple *t, Ndbtuple *f)
  65. {
  66. Ndbtuple *nt, *nf, *next;
  67. /* filter out what we don't want */
  68. for(nt = t; nt; nt = next){
  69. next = nt->entry;
  70. /* look through filter */
  71. for(nf = f; nf != nil; nf = nf->entry){
  72. if(!(nf->ptr&Fignore) && strcmp(nt->attr, nf->attr) == 0)
  73. break;
  74. }
  75. if(nf == nil){
  76. /* remove nt from t */
  77. t = ndbdiscard(t, nt);
  78. } else {
  79. if(nf->ptr & Faddr)
  80. t = ndbsubstitute(t, nt, setattr(ndbgetipaddr(db, nt->val), nt->attr));
  81. nf->ptr |= Ffound;
  82. }
  83. }
  84. /* remember filter etnries that matched */
  85. for(nf = f; nf != nil; nf = nf->entry)
  86. if(nf->ptr & Ffound)
  87. nf->ptr = (nf->ptr & ~Ffound) | Fignore;
  88. return t;
  89. }
  90. static int
  91. prefixlen(uchar *ip)
  92. {
  93. int y, i;
  94. for(y = IPaddrlen-1; y >= 0; y--)
  95. for(i = 8; i > 0; i--)
  96. if(ip[y] & (1<<(8-i)))
  97. return y*8 + i;
  98. return 0;
  99. }
  100. /*
  101. * look through a containing subset
  102. */
  103. static Ndbtuple*
  104. subnet(Ndb *db, uchar *net, Ndbtuple *f, int prefix)
  105. {
  106. Ndbs s;
  107. Ndbtuple *t, *nt, *xt;
  108. char netstr[128];
  109. uchar mask[IPaddrlen];
  110. int masklen;
  111. t = nil;
  112. sprint(netstr, "%I", net);
  113. nt = ndbsearch(db, &s, "ip", netstr);
  114. while(nt != nil){
  115. xt = ndbfindattr(nt, nt, "ipnet");
  116. if(xt){
  117. xt = ndbfindattr(nt, nt, "ipmask");
  118. if(xt)
  119. parseipmask(mask, xt->val);
  120. else
  121. ipmove(mask, defmask(net));
  122. masklen = prefixlen(mask);
  123. if(masklen <= prefix)
  124. t = ndbconcatenate(t, filter(db, nt, f));
  125. } else
  126. ndbfree(nt);
  127. nt = ndbsnext(&s, "ip", netstr);
  128. }
  129. return t;
  130. }
  131. /*
  132. * fill in all the requested attributes for a system.
  133. * if the system's entry doesn't have all required,
  134. * walk through successively more inclusive networks
  135. * for inherited attributes.
  136. */
  137. Ndbtuple*
  138. ndbipinfo(Ndb *db, char *attr, char *val, char **alist, int n)
  139. {
  140. Ndbtuple *t, *nt, *f;
  141. Ndbs s;
  142. char *ipstr;
  143. uchar net[IPaddrlen];
  144. uchar ip[IPaddrlen];
  145. int prefix, smallestprefix;
  146. int force;
  147. /* just in case */
  148. fmtinstall('I', eipfmt);
  149. fmtinstall('M', eipfmt);
  150. /* get needed attributes */
  151. f = mkfilter(n, alist);
  152. /*
  153. * first look for a matching entry with an ip address
  154. */
  155. t = nil;
  156. ipstr = ndbgetvalue(db, &s, attr, val, "ip", &nt);
  157. if(ipstr == nil){
  158. /* none found, make one up */
  159. if(strcmp(attr, "ip") != 0)
  160. return nil;
  161. t = ndbnew("ip", val);
  162. t->line = t;
  163. t->entry = nil;
  164. parseip(net, val);
  165. } else {
  166. /* found one */
  167. while(nt != nil){
  168. nt = ndbreorder(nt, s.t);
  169. t = ndbconcatenate(t, nt);
  170. nt = ndbsnext(&s, attr, val);
  171. }
  172. parseip(net, ipstr);
  173. free(ipstr);
  174. }
  175. ipmove(ip, net);
  176. t = filter(db, t, f);
  177. /*
  178. * now go through subnets to fill in any missing attributes
  179. */
  180. if(isv4(net)){
  181. prefix = 127;
  182. smallestprefix = 100;
  183. force = 0;
  184. } else {
  185. /* in v6, the last 8 bytes have no structure (we hope) */
  186. prefix = 64;
  187. smallestprefix = 2;
  188. memset(net+8, 0, 8);
  189. force = 1;
  190. }
  191. /*
  192. * to find a containing network, keep turning off
  193. * the lower bit and look for a network with
  194. * that address and a shorter mask. tedius but
  195. * complete, we may need to find a trick to speed this up.
  196. */
  197. for(; prefix >= smallestprefix; prefix--){
  198. if(filtercomplete(f))
  199. break;
  200. if(!force && (net[prefix/8] & (1<<(7-(prefix%8)))) == 0)
  201. continue;
  202. force = 0;
  203. net[prefix/8] &= ~(1<<(7-(prefix%8)));
  204. t = ndbconcatenate(t, subnet(db, net, f, prefix));
  205. }
  206. /*
  207. * if there's an unfulfilled ipmask, make one up
  208. */
  209. nt = ndbfindattr(f, f, "ipmask");
  210. if(nt && !(nt->ptr & Fignore)){
  211. char x[64];
  212. snprint(x, sizeof(x), "%M", defmask(ip));
  213. t = ndbconcatenate(t, ndbnew("ipmask", x));
  214. }
  215. ndbfree(f);
  216. return t;
  217. }