IPSubnet.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.util;
  16. import java.net.InetAddress;
  17. import java.net.UnknownHostException;
  18. public class IPSubnet
  19. {
  20. final byte[] _addr;
  21. final byte[] _mask;
  22. final boolean _isIPv4;
  23. public IPSubnet(String input) throws UnknownHostException, NumberFormatException, ArrayIndexOutOfBoundsException
  24. {
  25. int idx = input.indexOf("/");
  26. if (idx > 0)
  27. {
  28. _addr = InetAddress.getByName(input.substring(0, idx)).getAddress();
  29. _mask = getMask(Integer.parseInt(input.substring(idx + 1)), _addr.length);
  30. _isIPv4 = _addr.length == 4;
  31. if (!applyMask(_addr))
  32. throw new UnknownHostException(input);
  33. }
  34. else
  35. {
  36. _addr = InetAddress.getByName(input).getAddress();
  37. _mask = getMask(_addr.length * 8, _addr.length); // host, no need to check mask
  38. _isIPv4 = _addr.length == 4;
  39. }
  40. }
  41. public IPSubnet(InetAddress addr, int mask) throws UnknownHostException
  42. {
  43. _addr = addr.getAddress();
  44. _isIPv4 = _addr.length == 4;
  45. _mask = getMask(mask, _addr.length);
  46. if (!applyMask(_addr))
  47. throw new UnknownHostException(addr.toString() + "/" + mask);
  48. }
  49. public byte[] getAddress()
  50. {
  51. return _addr;
  52. }
  53. public boolean applyMask(byte[] addr)
  54. {
  55. // V4 vs V4 or V6 vs V6 checks
  56. if (_isIPv4 == (addr.length == 4))
  57. {
  58. for (int i = 0; i < _addr.length; i++)
  59. {
  60. if ((addr[i] & _mask[i]) != _addr[i])
  61. return false;
  62. }
  63. }
  64. else
  65. {
  66. // check for embedded v4 in v6 addr (not done !)
  67. if (_isIPv4)
  68. {
  69. // my V4 vs V6
  70. for (int i = 0; i < _addr.length; i++)
  71. {
  72. if ((addr[i + 12] & _mask[i]) != _addr[i])
  73. return false;
  74. }
  75. }
  76. else
  77. {
  78. // my V6 vs V4
  79. for (int i = 0; i < _addr.length; i++)
  80. {
  81. if ((addr[i] & _mask[i + 12]) != _addr[i + 12])
  82. return false;
  83. }
  84. }
  85. }
  86. return true;
  87. }
  88. @Override
  89. public String toString()
  90. {
  91. int size = 0;
  92. for (int i = 0; i < _mask.length; i++)
  93. size += Integer.bitCount((_mask[i] & 0xFF));
  94. try
  95. {
  96. return InetAddress.getByAddress(_addr).toString() + "/" + size;
  97. }
  98. catch (UnknownHostException e)
  99. {
  100. return "Invalid";
  101. }
  102. }
  103. @Override
  104. public boolean equals(Object o)
  105. {
  106. if (o instanceof IPSubnet)
  107. return applyMask(((IPSubnet)o).getAddress());
  108. else if (o instanceof InetAddress)
  109. return applyMask(((InetAddress)o).getAddress());
  110. return false;
  111. }
  112. private static final byte[] getMask(int n, int maxLength) throws UnknownHostException
  113. {
  114. if (n > (maxLength << 3) || n < 0)
  115. throw new UnknownHostException("Invalid netmask: " + n);
  116. final byte[] result = new byte[maxLength];
  117. for (int i = 0; i < maxLength; i++)
  118. result[i] = (byte)0xFF;
  119. for (int i = (maxLength << 3) - 1; i >= n; i--)
  120. result[i >> 3] = (byte)(result[i >> 3] << 1);
  121. return result;
  122. }
  123. }