ndbipinfo.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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;
  108. char netstr[Ndbvlen];
  109. char maskstr[Ndbvlen];
  110. uchar mask[IPaddrlen];
  111. int masklen;
  112. t = nil;
  113. sprint(netstr, "%I", net);
  114. nt = ndbsearch(db, &s, "ip", netstr);
  115. while(nt != nil){
  116. if(ndblookval(nt, nt, "ipnet", maskstr) != nil){
  117. if(ndblookval(nt, nt, "ipmask", maskstr))
  118. parseipmask(mask, maskstr);
  119. else
  120. ipmove(mask, defmask(net));
  121. masklen = prefixlen(mask);
  122. if(masklen <= prefix)
  123. t = ndbconcatenate(t, filter(db, nt, f));
  124. } else
  125. ndbfree(nt);
  126. nt = ndbsnext(&s, "ip", netstr);
  127. }
  128. return t;
  129. }
  130. /*
  131. * fill in all the requested attributes for a system.
  132. * if the system's entry doesn't have all required,
  133. * walk through successively more inclusive networks
  134. * for inherited attributes.
  135. */
  136. Ndbtuple*
  137. ndbipinfo(Ndb *db, char *attr, char *val, char **alist, int n)
  138. {
  139. Ndbtuple *t, *nt, *f;
  140. Ndbs s;
  141. char ipstr[Ndbvlen];
  142. uchar net[IPaddrlen];
  143. int prefix, smallestprefix;
  144. int force;
  145. /* just in case */
  146. fmtinstall('I', eipfmt);
  147. fmtinstall('M', eipfmt);
  148. /* get needed attributes */
  149. f = mkfilter(n, alist);
  150. /*
  151. * first look for a matching entry with an ip address
  152. */
  153. t = nil;
  154. nt = ndbgetval(db, &s, attr, val, "ip", ipstr);
  155. if(nt == nil){
  156. /* none found, make one up */
  157. if(strcmp(attr, "ip") != 0)
  158. return nil;
  159. t = ndbnew("ip", val);
  160. t->line = t;
  161. t->entry = nil;
  162. } else {
  163. /* found one */
  164. while(nt != nil){
  165. nt = ndbreorder(nt, s.t);
  166. t = ndbconcatenate(t, nt);
  167. nt = ndbsnext(&s, attr, val);
  168. }
  169. }
  170. t = filter(db, t, f);
  171. parseip(net, ipstr);
  172. /*
  173. * now go through subnets to fill in any missing attributes
  174. */
  175. if(isv4(net)){
  176. prefix = 127;
  177. smallestprefix = 100;
  178. force = 0;
  179. } else {
  180. /* in v6, the last 8 bytes have no structure (we hope) */
  181. prefix = 64;
  182. smallestprefix = 2;
  183. memset(net+8, 0, 8);
  184. force = 1;
  185. }
  186. /*
  187. * to find a containing network, keep turning off
  188. * the lower bit and look for a network with
  189. * that address and a shorter mask. tedius but
  190. * complete, we may need to find a trick to speed this up.
  191. */
  192. for(; prefix >= smallestprefix; prefix--){
  193. if(filtercomplete(f))
  194. break;
  195. if(!force && (net[prefix/8] & (1<<(7-(prefix%8)))) == 0)
  196. continue;
  197. force = 0;
  198. net[prefix/8] &= ~(1<<(7-(prefix%8)));
  199. t = ndbconcatenate(t, subnet(db, net, f, prefix));
  200. }
  201. ndbfree(f);
  202. return t;
  203. }