1
0

trashroutes 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python2
  2. # You may redistribute this program and/or modify it under the terms of
  3. # the GNU General Public License as published by the Free Software Foundation,
  4. # either version 3 of the License, or (at your option) any later version.
  5. #
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. import sys;
  14. import math;
  15. from cjdnsadmin.cjdnsadmin import connectWithAdminInfo;
  16. cjdns = connectWithAdminInfo();
  17. allRoutes = [];
  18. def parseRoute(path):
  19. if (path == 0): return [-1];
  20. if ((path & 15) == 1): return [1];
  21. if (path & 1):
  22. path >>= 1;
  23. out = [(path & 7)];
  24. if out[0] == 1: out[0] = 0;
  25. path >>= 3;
  26. elif ((path & 3) == 2):
  27. path >>= 2;
  28. out = [(path & 31)];
  29. if out[0] != 0: out[0]+=1;
  30. path >>= 5;
  31. else:
  32. path >>= 2;
  33. out = [(path & 255)];
  34. if out[0] != 0: out[0]+=1;
  35. path >>= 8;
  36. out.extend(parseRoute(path));
  37. return out;
  38. i = 0;
  39. while True:
  40. table = cjdns.NodeStore_dumpTable(i);
  41. routes = table['routingTable'];
  42. allRoutes += routes;
  43. if (not 'more' in table):
  44. break;
  45. i += 1;
  46. # get the value of a route as cjdns sees it
  47. def getValue(node):
  48. if (node['link'] == 0): return 0
  49. return 64 - int(math.log(int(node['path'].replace(".", ""), 16), 2));
  50. # get only the routes which will actually be user
  51. ar = {};
  52. for route in allRoutes:
  53. if route['ip'] not in ar or getValue(route) > getValue(ar[route['ip']]):
  54. ar[route['ip']] = route;
  55. allRoutes = [];
  56. for k in ar.keys():
  57. allRoutes.append(ar[k]);
  58. # get the actual arrays of interface indexes representing the path
  59. for route in allRoutes:
  60. path = int(route['path'].replace(".", ""), 16);
  61. route['r'] = parseRoute(path);
  62. penaltySum = 0;
  63. lengthSum = 0;
  64. for route in allRoutes:
  65. r = route['r'];
  66. penalty = 0;
  67. for i in range(1,len(r)):
  68. penalize = True;
  69. for routeB in allRoutes:
  70. rb = routeB['r'];
  71. if len(rb) < i or rb == r: continue;
  72. for j in range(0,i+1):
  73. if (rb[j] != r[j]): break;
  74. if (j == i): penalize = False;
  75. if not penalize: break;
  76. if penalize: penalty += 1;
  77. penalty <<= 1;
  78. output = str(penalty) + '\t' + route['ip'] + '@' + route['path'] + ' ' + str(r);
  79. if (r[-1] == -1): output += ' INVALID';
  80. print(output);
  81. penaltySum += penalty;
  82. lengthSum += len(r);
  83. avgPenalty = float(penaltySum) / len(allRoutes);
  84. avgLength = float(lengthSum) / len(allRoutes);
  85. print("average penalty " + str(avgPenalty));
  86. print("average length " + str(avgLength));
  87. print("trashroute index " + str(avgPenalty / avgLength));