UI for Zipcoin Blue

netmask.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Generated by CoffeeScript 1.10.0
  2. (function() {
  3. var Netmask, ip2long, long2ip;
  4. long2ip = function(long) {
  5. var a, b, c, d;
  6. a = (long & (0xff << 24)) >>> 24;
  7. b = (long & (0xff << 16)) >>> 16;
  8. c = (long & (0xff << 8)) >>> 8;
  9. d = long & 0xff;
  10. return [a, b, c, d].join('.');
  11. };
  12. ip2long = function(ip) {
  13. var b, byte, i, j, len;
  14. b = (ip + '').split('.');
  15. if (b.length === 0 || b.length > 4) {
  16. throw new Error('Invalid IP');
  17. }
  18. for (i = j = 0, len = b.length; j < len; i = ++j) {
  19. byte = b[i];
  20. if (isNaN(parseInt(byte, 10))) {
  21. throw new Error("Invalid byte: " + byte);
  22. }
  23. if (byte < 0 || byte > 255) {
  24. throw new Error("Invalid byte: " + byte);
  25. }
  26. }
  27. return ((b[0] || 0) << 24 | (b[1] || 0) << 16 | (b[2] || 0) << 8 | (b[3] || 0)) >>> 0;
  28. };
  29. Netmask = (function() {
  30. function Netmask(net, mask) {
  31. var error, error1, error2, i, j, ref;
  32. if (typeof net !== 'string') {
  33. throw new Error("Missing `net' parameter");
  34. }
  35. if (!mask) {
  36. ref = net.split('/', 2), net = ref[0], mask = ref[1];
  37. }
  38. if (!mask) {
  39. switch (net.split('.').length) {
  40. case 1:
  41. mask = 8;
  42. break;
  43. case 2:
  44. mask = 16;
  45. break;
  46. case 3:
  47. mask = 24;
  48. break;
  49. case 4:
  50. mask = 32;
  51. break;
  52. default:
  53. throw new Error("Invalid net address: " + net);
  54. }
  55. }
  56. if (typeof mask === 'string' && mask.indexOf('.') > -1) {
  57. try {
  58. this.maskLong = ip2long(mask);
  59. } catch (error1) {
  60. error = error1;
  61. throw new Error("Invalid mask: " + mask);
  62. }
  63. for (i = j = 32; j >= 0; i = --j) {
  64. if (this.maskLong === (0xffffffff << (32 - i)) >>> 0) {
  65. this.bitmask = i;
  66. break;
  67. }
  68. }
  69. } else if (mask) {
  70. this.bitmask = parseInt(mask, 10);
  71. this.maskLong = 0;
  72. if (this.bitmask > 0) {
  73. this.maskLong = (0xffffffff << (32 - this.bitmask)) >>> 0;
  74. }
  75. } else {
  76. throw new Error("Invalid mask: empty");
  77. }
  78. try {
  79. this.netLong = (ip2long(net) & this.maskLong) >>> 0;
  80. } catch (error2) {
  81. error = error2;
  82. throw new Error("Invalid net address: " + net);
  83. }
  84. if (!(this.bitmask <= 32)) {
  85. throw new Error("Invalid mask for ip4: " + mask);
  86. }
  87. this.size = Math.pow(2, 32 - this.bitmask);
  88. this.base = long2ip(this.netLong);
  89. this.mask = long2ip(this.maskLong);
  90. this.hostmask = long2ip(~this.maskLong);
  91. this.first = this.bitmask <= 30 ? long2ip(this.netLong + 1) : this.base;
  92. this.last = this.bitmask <= 30 ? long2ip(this.netLong + this.size - 2) : long2ip(this.netLong + this.size - 1);
  93. this.broadcast = this.bitmask <= 30 ? long2ip(this.netLong + this.size - 1) : void 0;
  94. }
  95. Netmask.prototype.contains = function(ip) {
  96. if (typeof ip === 'string' && (ip.indexOf('/') > 0 || ip.split('.').length !== 4)) {
  97. ip = new Netmask(ip);
  98. }
  99. if (ip instanceof Netmask) {
  100. return this.contains(ip.base) && this.contains(ip.broadcast || ip.last);
  101. } else {
  102. return (ip2long(ip) & this.maskLong) >>> 0 === (this.netLong & this.maskLong) >>> 0;
  103. }
  104. };
  105. Netmask.prototype.next = function(count) {
  106. if (count == null) {
  107. count = 1;
  108. }
  109. return new Netmask(long2ip(this.netLong + (this.size * count)), this.mask);
  110. };
  111. Netmask.prototype.forEach = function(fn) {
  112. var index, j, k, len, long, range, ref, ref1, results, results1;
  113. range = (function() {
  114. results = [];
  115. for (var j = ref = ip2long(this.first), ref1 = ip2long(this.last); ref <= ref1 ? j <= ref1 : j >= ref1; ref <= ref1 ? j++ : j--){ results.push(j); }
  116. return results;
  117. }).apply(this);
  118. results1 = [];
  119. for (index = k = 0, len = range.length; k < len; index = ++k) {
  120. long = range[index];
  121. results1.push(fn(long2ip(long), long, index));
  122. }
  123. return results1;
  124. };
  125. Netmask.prototype.toString = function() {
  126. return this.base + "/" + this.bitmask;
  127. };
  128. return Netmask;
  129. })();
  130. exports.ip2long = ip2long;
  131. exports.long2ip = long2ip;
  132. exports.Netmask = Netmask;
  133. }).call(this);