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.

UnknownAttribute.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 java.util.*;
  13. import de.javawi.jstun.util.Utility;
  14. import de.javawi.jstun.util.UtilityException;
  15. public class UnknownAttribute extends MessageAttribute {
  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. * | Attribute 1 Type | Attribute 2 Type |
  21. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  22. * | Attribute 3 Type | Attribute 4 Type ...
  23. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  24. */
  25. Vector<MessageAttributeType> unkown = new Vector<MessageAttributeType>();
  26. public UnknownAttribute() {
  27. super(MessageAttribute.MessageAttributeType.UnknownAttribute);
  28. }
  29. public void addAttribute(MessageAttributeType attribute) {
  30. unkown.add(attribute);
  31. }
  32. public byte[] getBytes() throws UtilityException {
  33. int length = 0;
  34. if (unkown.size()%2 == 1) {
  35. length = 2 * (unkown.size() + 1) + 4;
  36. } else {
  37. length = 2 * unkown.size() + 4;
  38. }
  39. byte[] result = new byte[length];
  40. // message attribute header
  41. // type
  42. System.arraycopy(Utility.integerToTwoBytes(typeToInteger(type)), 0, result, 0, 2);
  43. // length
  44. System.arraycopy(Utility.integerToTwoBytes(length - 4), 0, result, 2, 2);
  45. // unkown attribute header
  46. Iterator<MessageAttributeType> it = unkown.iterator();
  47. while(it.hasNext()) {
  48. MessageAttributeType attri = it.next();
  49. System.arraycopy(Utility.integerToTwoBytes(typeToInteger(attri)), 0, result, 4, 2);
  50. }
  51. // padding
  52. if (unkown.size()%2 == 1) {
  53. System.arraycopy(Utility.integerToTwoBytes(typeToInteger(unkown.elementAt(1))), 0, result, 4, 2);
  54. }
  55. return result;
  56. }
  57. public static UnknownAttribute parse(byte[] data) throws MessageAttributeParsingException {
  58. try {
  59. UnknownAttribute result = new UnknownAttribute();
  60. if (data.length % 4 != 0) throw new MessageAttributeParsingException("Data array too short");
  61. for (int i = 0; i < data.length; i += 4) {
  62. byte[] temp = new byte[4];
  63. System.arraycopy(data, i, temp, 0, 4);
  64. long attri = Utility.fourBytesToLong(temp);
  65. result.addAttribute(MessageAttribute.intToType(attri));
  66. }
  67. return result;
  68. } catch (UtilityException ue) {
  69. throw new MessageAttributeParsingException("Parsing error");
  70. }
  71. }
  72. }