Unsupported library that attempts to punch holes through NAT
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MappedResponseChangedSourceAddressReflectedFrom.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.attribute;
  12. import de.javawi.jstun.util.*;
  13. public class MappedResponseChangedSourceAddressReflectedFrom extends MessageAttribute {
  14. int port;
  15. Address address;
  16. /*
  17. * 0 1 2 3
  18. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  19. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  20. * |x x x x x x x x| Family | Port |
  21. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  22. * | Address |
  23. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  24. */
  25. public MappedResponseChangedSourceAddressReflectedFrom() {
  26. super();
  27. try {
  28. port = 0;
  29. address = new Address("0.0.0.0");
  30. } catch (UtilityException ue) {
  31. ue.getMessage();
  32. ue.printStackTrace();
  33. }
  34. }
  35. public MappedResponseChangedSourceAddressReflectedFrom(MessageAttribute.MessageAttributeType type) {
  36. super(type);
  37. }
  38. public int getPort() {
  39. return port;
  40. }
  41. public Address getAddress() {
  42. return address;
  43. }
  44. public void setPort(int port) throws MessageAttributeException {
  45. if ((port > 65536) || (port < 0)) {
  46. throw new MessageAttributeException("Port value " + port + " out of range.");
  47. }
  48. this.port = port;
  49. }
  50. public void setAddress(Address address) {
  51. this.address = address;
  52. }
  53. public byte[] getBytes() throws UtilityException {
  54. byte[] result = new byte[12];
  55. // message attribute header
  56. // type
  57. System.arraycopy(Utility.integerToTwoBytes(typeToInteger(type)), 0, result, 0, 2);
  58. // length
  59. System.arraycopy(Utility.integerToTwoBytes(8), 0, result, 2, 2);
  60. // mappedaddress header
  61. // family
  62. result[5] = Utility.integerToOneByte(0x01);
  63. // port
  64. System.arraycopy(Utility.integerToTwoBytes(port), 0, result, 6, 2);
  65. // address
  66. System.arraycopy(address.getBytes(), 0, result, 8, 4);
  67. return result;
  68. }
  69. protected static MappedResponseChangedSourceAddressReflectedFrom parse(MappedResponseChangedSourceAddressReflectedFrom ma, byte[] data) throws MessageAttributeParsingException {
  70. try {
  71. if (data.length < 8) {
  72. throw new MessageAttributeParsingException("Data array too short");
  73. }
  74. int family = Utility.oneByteToInteger(data[1]);
  75. if (family != 0x01) throw new MessageAttributeParsingException("Family " + family + " is not supported");
  76. byte[] portArray = new byte[2];
  77. System.arraycopy(data, 2, portArray, 0, 2);
  78. ma.setPort(Utility.twoBytesToInteger(portArray));
  79. int firstOctet = Utility.oneByteToInteger(data[4]);
  80. int secondOctet = Utility.oneByteToInteger(data[5]);
  81. int thirdOctet = Utility.oneByteToInteger(data[6]);
  82. int fourthOctet = Utility.oneByteToInteger(data[7]);
  83. ma.setAddress(new Address(firstOctet, secondOctet, thirdOctet, fourthOctet));
  84. return ma;
  85. } catch (UtilityException ue) {
  86. throw new MessageAttributeParsingException("Parsing error");
  87. } catch (MessageAttributeException mae) {
  88. throw new MessageAttributeParsingException("Port parsing error");
  89. }
  90. }
  91. public String toString() {
  92. return "Address " +address.toString() + ", Port " + port;
  93. }
  94. }