TELNETFormatter.java 3.2 KB

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