Unsupported library that attempts to punch holes through NAT
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * This file is part of JSTUN.
  3. *
  4. * Copyright (c) 2005 Thomas King <king@t-king.de> - All rights
  5. * reserved.
  6. *
  7. * This software is licensed under either the GNU Public License (GPL),
  8. * or the Apache 2.0 license. Copies of both license agreements are
  9. * included in this distribution.
  10. */
  11. package de.javawi.jstun.util;
  12. import java.util.*;
  13. import java.net.*;
  14. public class Address {
  15. int firstOctet;
  16. int secondOctet;
  17. int thirdOctet;
  18. int fourthOctet;
  19. public Address(int firstOctet, int secondOctet, int thirdOctet, int fourthOctet) throws UtilityException {
  20. if ((firstOctet < 0) || (firstOctet > 255) || (secondOctet < 0) || (secondOctet > 255) || (thirdOctet < 0) || (thirdOctet > 255) || (fourthOctet < 0) || (fourthOctet > 255)) {
  21. throw new UtilityException("Address is malformed.");
  22. }
  23. this.firstOctet = firstOctet;
  24. this.secondOctet = secondOctet;
  25. this.thirdOctet = thirdOctet;
  26. this.fourthOctet = fourthOctet;
  27. }
  28. public Address(String address) throws UtilityException {
  29. StringTokenizer st = new StringTokenizer(address, ".");
  30. if (st.countTokens() != 4) {
  31. throw new UtilityException("4 octets in address string are required.");
  32. }
  33. int i = 0;
  34. while (st.hasMoreTokens()) {
  35. int temp = Integer.parseInt(st.nextToken());
  36. if ((temp < 0) || (temp > 255)) {
  37. throw new UtilityException("Address is in incorrect format.");
  38. }
  39. switch (i) {
  40. case 0: firstOctet = temp; ++i; break;
  41. case 1: secondOctet = temp; ++i; break;
  42. case 2: thirdOctet = temp; ++i; break;
  43. case 3: fourthOctet = temp; ++i; break;
  44. }
  45. }
  46. }
  47. public Address(byte[] address) throws UtilityException {
  48. if (address.length < 4) {
  49. throw new UtilityException("4 bytes are required.");
  50. }
  51. firstOctet = Utility.oneByteToInteger(address[0]);
  52. secondOctet = Utility.oneByteToInteger(address[1]);
  53. thirdOctet = Utility.oneByteToInteger(address[2]);
  54. fourthOctet = Utility.oneByteToInteger(address[3]);
  55. }
  56. public String toString() {
  57. return firstOctet + "." + secondOctet + "." + thirdOctet + "." + fourthOctet;
  58. }
  59. public byte[] getBytes() throws UtilityException {
  60. byte[] result = new byte[4];
  61. result[0] = Utility.integerToOneByte(firstOctet);
  62. result[1] = Utility.integerToOneByte(secondOctet);
  63. result[2] = Utility.integerToOneByte(thirdOctet);
  64. result[3] = Utility.integerToOneByte(fourthOctet);
  65. return result;
  66. }
  67. public InetAddress getInetAddress() throws UtilityException, UnknownHostException {
  68. byte[] address = new byte[4];
  69. address[0] = Utility.integerToOneByte(firstOctet);
  70. address[1] = Utility.integerToOneByte(secondOctet);
  71. address[2] = Utility.integerToOneByte(thirdOctet);
  72. address[3] = Utility.integerToOneByte(fourthOctet);
  73. return InetAddress.getByAddress(address);
  74. }
  75. public boolean equals(Object obj) {
  76. if (obj == null) return false;
  77. try {
  78. byte[] data1 = this.getBytes();
  79. byte[] data2 = ((Address) obj).getBytes();
  80. if ((data1[0] == data2[0]) && (data1[1] == data2[1]) &&
  81. (data1[2] == data2[2]) && (data1[3] == data2[3])) return true;
  82. return false;
  83. } catch (UtilityException ue) {
  84. return false;
  85. }
  86. }
  87. public int hashCode() {
  88. return (firstOctet << 24) + (secondOctet << 16) + (thirdOctet << 8) + fourthOctet;
  89. }
  90. }