TELNET.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package de.tudarmstadt.informatik.hostage.logging.formatter.protocol;
  2. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  3. /**
  4. * Telnet log view formatter.
  5. *
  6. * @author Wulf Pfeiffer
  7. */
  8. public class TELNET extends ProtocolFormatter {
  9. @Override
  10. public String format(String packet) {
  11. byte[] bytes = HelperUtils.hexStringToBytes(packet);
  12. String options = "Options:\n" + checkForOptions(bytes) + "\n";
  13. String content = "Content: " + HelperUtils.byteToStr(bytes);
  14. return options + content;
  15. }
  16. /**
  17. * Checks a packet for option commands and returns their names as Strings.
  18. *
  19. * @param bytes
  20. * that are checked.
  21. * @return names of the option commands as String.
  22. */
  23. private String checkForOptions(byte[] bytes) {
  24. StringBuffer options = new StringBuffer();
  25. for (int i = 0; i < bytes.length; i++) {
  26. if (bytes[i] == (byte) 0xff && i + 2 < bytes.length) {
  27. options.append(checkMode(bytes[i + 1]));
  28. // option name
  29. options.append(checkCommand(bytes[i + 2]));
  30. }
  31. }
  32. return options.toString();
  33. }
  34. /**
  35. * Checks a byte for its mode value.
  36. *
  37. * @param b
  38. * byte that is checked.
  39. * @return name of the mode.
  40. */
  41. private String checkMode(byte b) {
  42. switch (b) {
  43. case (byte) 0xfb:
  44. return " WILL ";
  45. case (byte) 0xfc:
  46. return " WON'T ";
  47. case (byte) 0xfd:
  48. return " DO ";
  49. case (byte) 0xfe:
  50. return " DON'T ";
  51. default:
  52. return " unkown command ";
  53. }
  54. }
  55. /**
  56. * Checks a byte for its command value.
  57. *
  58. * @param b
  59. * byte that is checked.
  60. * @return name of the command.
  61. */
  62. private String checkCommand(byte b) {
  63. switch (b) {
  64. case 0x00:
  65. return "Binary Transmission\n";
  66. case 0x01:
  67. return "Echo\n";
  68. case 0x02:
  69. return "Reconnection\n";
  70. case 0x03:
  71. return "Suppress Go Ahead\n";
  72. case 0x04:
  73. return "Approx Message Size Negotiation\n";
  74. case 0x05:
  75. return "Status\n";
  76. case 0x06:
  77. return "Timing Mark\n";
  78. case 0x07:
  79. return "Remote Controlled Trans and Echo\n";
  80. case 0x08:
  81. return "Output Line Width\n";
  82. case 0x09:
  83. return "Output Page Size\n";
  84. case 0x0a:
  85. return "Output Carriage-Return Disposition\n";
  86. case 0x0b:
  87. return "Output Horizontal Tab Stops\n";
  88. case 0x0c:
  89. return "Output Horizontal Tab Disposition\n";
  90. case 0x0d:
  91. return "Output Formfeed Disposition\n";
  92. case 0x0e:
  93. return "Output Vertical Tabstops\n";
  94. case 0x0f:
  95. return "Output Vertical Tab Disposition\n";
  96. case 0x10:
  97. return "Output Linefeed Disposition\n";
  98. case 0x11:
  99. return "Extended ASCII\n";
  100. case 0x12:
  101. return "Logout\n";
  102. case 0x13:
  103. return "Byte Macro\n";
  104. case 0x14:
  105. return "Data Entry Terminal\n";
  106. case 0x15:
  107. return "SUPDUP\n";
  108. case 0x16:
  109. return "SUPDUP Output\n";
  110. case 0x17:
  111. return "Send Location\n";
  112. case 0x18:
  113. return "Terminal Type\n";
  114. case 0x19:
  115. return "End of Record\n";
  116. case 0x1a:
  117. return "TACACS User Identification\n";
  118. case 0x1b:
  119. return "Output Marking\n";
  120. case 0x1c:
  121. return "Terminal Location Number\n";
  122. case 0x1d:
  123. return "Telnet 3270 Regime\n";
  124. case 0x1e:
  125. return "X.3 PAD\n";
  126. case 0x1f:
  127. return "Negotiate About Window Size\n";
  128. case 0x20:
  129. return "Terminal Speed\n";
  130. case 0x21:
  131. return "Remote Flow Control\n";
  132. case 0x22:
  133. return "Linemode\n";
  134. case 0x23:
  135. return "X Display Location\n";
  136. case (byte) 0xff:
  137. return "Extended-Options-List\n";
  138. default:
  139. return "unknown option\n";
  140. }
  141. }
  142. }