Unsupported library that attempts to punch holes through NAT
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

MessageAttribute.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.logging.*;
  13. import de.javawi.jstun.util.*;
  14. public abstract class MessageAttribute implements MessageAttributeInterface {
  15. private static Logger logger = Logger.getLogger("de.javawi.stun.util.MessageAttribute");
  16. MessageAttributeType type;
  17. public MessageAttribute() {
  18. }
  19. public MessageAttribute(MessageAttributeType type) {
  20. setType(type);
  21. }
  22. public void setType(MessageAttributeType type) {
  23. this.type = type;
  24. }
  25. public MessageAttribute.MessageAttributeType getType() {
  26. return type;
  27. }
  28. public static int typeToInteger(MessageAttributeType type) {
  29. if (type == MessageAttributeType.MappedAddress) return MAPPEDADDRESS;
  30. if (type == MessageAttributeType.ResponseAddress) return RESPONSEADDRESS;
  31. if (type == MessageAttributeType.ChangeRequest) return CHANGEREQUEST;
  32. if (type == MessageAttributeType.SourceAddress) return SOURCEADDRESS;
  33. if (type == MessageAttributeType.ChangedAddress) return CHANGEDADDRESS;
  34. if (type == MessageAttributeType.Username) return USERNAME;
  35. if (type == MessageAttributeType.Password) return PASSWORD;
  36. if (type == MessageAttributeType.MessageIntegrity) return MESSAGEINTEGRITY;
  37. if (type == MessageAttributeType.ErrorCode) return ERRORCODE;
  38. if (type == MessageAttributeType.UnknownAttribute) return UNKNOWNATTRIBUTE;
  39. if (type == MessageAttributeType.ReflectedFrom) return REFLECTEDFROM;
  40. if (type == MessageAttributeType.Dummy) return DUMMY;
  41. return -1;
  42. }
  43. public static MessageAttributeType intToType(long type) {
  44. if (type == MAPPEDADDRESS) return MessageAttributeType.MappedAddress;
  45. if (type == RESPONSEADDRESS) return MessageAttributeType.ResponseAddress;
  46. if (type == CHANGEREQUEST) return MessageAttributeType.ChangeRequest;
  47. if (type == SOURCEADDRESS) return MessageAttributeType.SourceAddress;
  48. if (type == CHANGEDADDRESS) return MessageAttributeType.ChangedAddress;
  49. if (type == USERNAME) return MessageAttributeType.Username;
  50. if (type == PASSWORD) return MessageAttributeType.Password;
  51. if (type == MESSAGEINTEGRITY) return MessageAttributeType.MessageIntegrity;
  52. if (type == ERRORCODE) return MessageAttributeType.ErrorCode;
  53. if (type == UNKNOWNATTRIBUTE) return MessageAttributeType.UnknownAttribute;
  54. if (type == REFLECTEDFROM) return MessageAttributeType.ReflectedFrom;
  55. if (type == DUMMY) return MessageAttributeType.Dummy;
  56. return null;
  57. }
  58. abstract public byte[] getBytes() throws UtilityException;
  59. //abstract public MessageAttribute parse(byte[] data) throws MessageAttributeParsingException;
  60. public int getLength() throws UtilityException {
  61. int length = getBytes().length;
  62. return length;
  63. }
  64. public static MessageAttribute parseCommonHeader(byte[] data) throws MessageAttributeParsingException {
  65. try {
  66. byte[] typeArray = new byte[2];
  67. System.arraycopy(data, 0, typeArray, 0, 2);
  68. int type = Utility.twoBytesToInteger(typeArray);
  69. byte[] lengthArray = new byte[2];
  70. System.arraycopy(data, 2, lengthArray, 0, 2);
  71. int lengthValue = Utility.twoBytesToInteger(lengthArray);
  72. byte[] valueArray = new byte[lengthValue];
  73. System.arraycopy(data, 4, valueArray, 0, lengthValue);
  74. MessageAttribute ma;
  75. switch (type) {
  76. case MAPPEDADDRESS: ma = MappedAddress.parse(valueArray); break;
  77. case RESPONSEADDRESS: ma = ResponseAddress.parse(valueArray); break;
  78. case CHANGEREQUEST: ma = ChangeRequest.parse(valueArray); break;
  79. case SOURCEADDRESS: ma = SourceAddress.parse(valueArray); break;
  80. case CHANGEDADDRESS: ma = ChangedAddress.parse(valueArray); break;
  81. case USERNAME: ma = Username.parse(valueArray); break;
  82. case PASSWORD: ma = Password.parse(valueArray); break;
  83. case MESSAGEINTEGRITY: ma = MessageIntegrity.parse(valueArray); break;
  84. case ERRORCODE: ma = ErrorCode.parse(valueArray); break;
  85. case UNKNOWNATTRIBUTE: ma = UnknownAttribute.parse(valueArray); break;
  86. case REFLECTEDFROM: ma = ReflectedFrom.parse(valueArray); break;
  87. default:
  88. if (type <= 0x7fff) {
  89. throw new UnknownMessageAttributeException("Unkown mandatory message attribute", intToType(type));
  90. } else {
  91. logger.finer("MessageAttribute with type " + type + " unkown.");
  92. ma = Dummy.parse(valueArray);
  93. break;
  94. }
  95. }
  96. return ma;
  97. } catch (UtilityException ue) {
  98. throw new MessageAttributeParsingException("Parsing error");
  99. }
  100. }
  101. }