ndbipinfo.c 5.6 KB

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