Unsupported library that attempts to punch holes through NAT
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ErrorCode.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.Utility;
  13. import de.javawi.jstun.util.UtilityException;
  14. public class ErrorCode extends MessageAttribute {
  15. /*
  16. * 0 1 2 3
  17. * 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
  18. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  19. * | 0 |Class| Number |
  20. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  21. * | Reason Phrase (variable) ..
  22. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  23. */
  24. int responseCode;
  25. String reason;
  26. public ErrorCode() {
  27. super(MessageAttribute.MessageAttributeType.ErrorCode);
  28. }
  29. public void setResponseCode(int responseCode) throws MessageAttributeException {
  30. switch (responseCode) {
  31. case 400: reason = "Bad Request"; break;
  32. case 401: reason = "Unauthorized"; break;
  33. case 420: reason = "Unkown Attribute"; break;
  34. case 430: reason = "Stale Credentials"; break;
  35. case 431: reason = "Integrity Check Failure"; break;
  36. case 432: reason = "Missing Username"; break;
  37. case 433: reason = "Use TLS"; break;
  38. case 500: reason = "Server Error"; break;
  39. case 600: reason = "Global Failure"; break;
  40. default: throw new MessageAttributeException("Response Code is not valid");
  41. }
  42. this.responseCode = responseCode;
  43. }
  44. public int getResponseCode() {
  45. return responseCode;
  46. }
  47. public String getReason() {
  48. return reason;
  49. }
  50. public byte[] getBytes() throws UtilityException {
  51. int length = reason.length();
  52. // length adjustment
  53. if ((length % 4) != 0) {
  54. length += 4 - (length % 4);
  55. }
  56. // message attribute header
  57. length += 4;
  58. byte[] result = new byte[length];
  59. // message attribute header
  60. // type
  61. System.arraycopy(Utility.integerToTwoBytes(typeToInteger(type)), 0, result, 0, 2);
  62. // length
  63. System.arraycopy(Utility.integerToTwoBytes(length-4), 0, result, 2, 2);
  64. // error code header
  65. int classHeader = (int) Math.floor(((double)responseCode)/100);
  66. result[6] = Utility.integerToOneByte(classHeader);
  67. result[7] = Utility.integerToOneByte(responseCode%100);
  68. byte[] reasonArray = reason.getBytes();
  69. System.arraycopy(reasonArray, 0, result, 8, reasonArray.length);
  70. return result;
  71. }
  72. public static ErrorCode parse(byte[] data) throws MessageAttributeParsingException {
  73. try {
  74. if (data.length < 4) {
  75. throw new MessageAttributeParsingException("Data array too short");
  76. }
  77. byte classHeaderByte = data[3];
  78. int classHeader = Utility.oneByteToInteger(classHeaderByte);
  79. if ((classHeader < 1) || (classHeader > 6)) throw new MessageAttributeParsingException("Class parsing error");
  80. byte numberByte = data[4];
  81. int number = Utility.oneByteToInteger(numberByte);
  82. if ((number < 0) || (number > 99)) throw new MessageAttributeParsingException("Number parsing error");
  83. int responseCode = (classHeader * 100) + number;
  84. ErrorCode result = new ErrorCode();
  85. result.setResponseCode(responseCode);
  86. return result;
  87. } catch (UtilityException ue) {
  88. throw new MessageAttributeParsingException("Parsing error");
  89. } catch (MessageAttributeException mae) {
  90. throw new MessageAttributeParsingException("Parsing error");
  91. }
  92. }
  93. }