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