dumptable 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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('../contrib/nodejs/cjdnsadmin/cjdnsadmin');
  18. var PublicToIp6 = require('./lib/publicToIp6');
  19. var distance = function (myAddr, theirAddr) {
  20. var myInt = Number('0x' + myAddr.substring(20, 28).replace(':', ''));
  21. var theirInt = Number('0x' + theirAddr.substring(20, 28).replace(':', ''));
  22. var out = (myInt ^ theirInt).toString(16);
  23. while (out.length < 7) { out = '0'+out; }
  24. return out;
  25. };
  26. Cjdns.connectWithAdminInfo(function (cjdns) {
  27. var list = [];
  28. var again = function (i) {
  29. cjdns.NodeStore_dumpTable(i, function (err, table) {
  30. if (err) { throw err; }
  31. var j;
  32. for (j = 0; j < table.routingTable.length; j++) {
  33. var r = table.routingTable[j];
  34. list.push(r);
  35. }
  36. if (j) {
  37. again(i+1);
  38. } else {
  39. console.log('ver path addr' +
  40. ' kDist physdist lastPinged');
  41. list.sort(function (a,b) { return a.path < b.path ? -1 : 1; });
  42. var myAddr = PublicToIp6.convert(list[0].addr.replace(/^([^\.]*\.){5}/,''));
  43. list.forEach(function (r) {
  44. var theirAddr = PublicToIp6.convert(r.addr.replace(/^([^\.]*\.){5}/,''));
  45. var dist = distance(myAddr, theirAddr);
  46. console.log(r.addr + ' ' + dist + ' ' + r.link + ' ' + r.time);
  47. });
  48. console.log(table.count + ' nodes ' + table.peers + ' peers');
  49. cjdns.disconnect();
  50. }
  51. });
  52. };
  53. again(0);
  54. });