IPSubnet.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. {
  33. throw new UnknownHostException(input);
  34. }
  35. }
  36. else
  37. {
  38. _addr = InetAddress.getByName(input).getAddress();
  39. _mask = getMask(_addr.length * 8, _addr.length); // host, no need to check mask
  40. _isIPv4 = _addr.length == 4;
  41. }
  42. }
  43. public IPSubnet(InetAddress addr, int mask) throws UnknownHostException
  44. {
  45. _addr = addr.getAddress();
  46. _isIPv4 = _addr.length == 4;
  47. _mask = getMask(mask, _addr.length);
  48. if (!applyMask(_addr))
  49. {
  50. throw new UnknownHostException(addr.toString() + "/" + mask);
  51. }
  52. }
  53. public byte[] getAddress()
  54. {
  55. return _addr;
  56. }
  57. public boolean applyMask(byte[] addr)
  58. {
  59. // V4 vs V4 or V6 vs V6 checks
  60. if (_isIPv4 == (addr.length == 4))
  61. {
  62. for (int i = 0; i < _addr.length; i++)
  63. {
  64. if ((addr[i] & _mask[i]) != _addr[i])
  65. {
  66. return false;
  67. }
  68. }
  69. }
  70. else
  71. {
  72. // check for embedded v4 in v6 addr (not done !)
  73. if (_isIPv4)
  74. {
  75. // my V4 vs V6
  76. for (int i = 0; i < _addr.length; i++)
  77. {
  78. if ((addr[i + 12] & _mask[i]) != _addr[i])
  79. {
  80. return false;
  81. }
  82. }
  83. }
  84. else
  85. {
  86. // my V6 vs V4
  87. for (int i = 0; i < _addr.length; i++)
  88. {
  89. if ((addr[i] & _mask[i + 12]) != _addr[i + 12])
  90. {
  91. return false;
  92. }
  93. }
  94. }
  95. }
  96. return true;
  97. }
  98. @Override
  99. public String toString()
  100. {
  101. int size = 0;
  102. for (byte element : _mask)
  103. {
  104. size += Integer.bitCount((element & 0xFF));
  105. }
  106. try
  107. {
  108. return InetAddress.getByAddress(_addr).toString() + "/" + size;
  109. }
  110. catch (UnknownHostException e)
  111. {
  112. return "Invalid";
  113. }
  114. }
  115. @Override
  116. public boolean equals(Object o)
  117. {
  118. if (this == o)
  119. {
  120. return true;
  121. }
  122. if (o instanceof IPSubnet)
  123. {
  124. return applyMask(((IPSubnet) o).getAddress());
  125. }
  126. else if (o instanceof InetAddress)
  127. {
  128. return applyMask(((InetAddress) o).getAddress());
  129. }
  130. return false;
  131. }
  132. private static final byte[] getMask(int n, int maxLength) throws UnknownHostException
  133. {
  134. if ((n > (maxLength << 3)) || (n < 0))
  135. {
  136. throw new UnknownHostException("Invalid netmask: " + n);
  137. }
  138. final byte[] result = new byte[maxLength];
  139. for (int i = 0; i < maxLength; i++)
  140. {
  141. result[i] = (byte) 0xFF;
  142. }
  143. for (int i = (maxLength << 3) - 1; i >= n; i--)
  144. {
  145. result[i >> 3] = (byte) (result[i >> 3] << 1);
  146. }
  147. return result;
  148. }
  149. }