dumptable 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env node
  2. /* -*- Mode:Js */
  3. /* vim: set expandtab ts=4 sw=4: */
  4. /*
  5. * You may redistribute this program and/or modify it under the terms of
  6. * the GNU General Public License as published by the Free Software Foundation,
  7. * either version 3 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. var Cjdns = require('./lib/cjdnsadmin/cjdnsadmin');
  18. var PublicToIp6 = require('./lib/publicToIp6');
  19. var distance = function (myAddr, theirAddr) {
  20. var myInt = Number('0x' + myAddr.substring(20, 27).replace(':', ''));
  21. var theirInt = Number('0x' + theirAddr.substring(20, 27).replace(':', ''));
  22. var out = 0;
  23. for (var i = 23; i >= 0; i--) {
  24. if ((myInt >>> i) !== (theirInt >>> i)) { return out; }
  25. out++;
  26. }
  27. return 127;
  28. };
  29. Cjdns.connectWithAdminInfo(function (cjdns) {
  30. var list = [];
  31. var again = function (i) {
  32. cjdns.NodeStore_dumpTable(i, function (err, table) {
  33. if (err) { throw err; }
  34. var j;
  35. for (j = 0; j < table.routingTable.length; j++) {
  36. var r = table.routingTable[j];
  37. list.push(r);
  38. }
  39. if (j) {
  40. again(i+1);
  41. } else {
  42. console.log('ver path addr' +
  43. ' bucket metric lastPinged');
  44. list.sort(function (a,b) { return a.path < b.path ? -1 : 1; });
  45. var myAddr = PublicToIp6.convert(list[0].addr.replace(/^([^\.]*\.){5}/,''));
  46. list.forEach(function (r) {
  47. var theirAddr = PublicToIp6.convert(r.addr.replace(/^([^\.]*\.){5}/,''));
  48. var dist = distance(myAddr, theirAddr);
  49. if (r.bucket && dist !== Number(r.bucket)) { throw new Error(dist + ' !== ' + r.bucket); }
  50. console.log(r.addr + ' ' + dist + ' ' + r.link + ' ' + r.time);
  51. });
  52. console.log(table.count + ' nodes ' + table.peers + ' peers');
  53. cjdns.disconnect();
  54. }
  55. });
  56. };
  57. again(0);
  58. });