IPSubnet.java 3.7 KB

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