ndbipinfo.c 5.1 KB

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