Packet.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package de.tudarmstadt.informatik.hostage.wrapper;
  2. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  3. /**
  4. * Wrapper class for the payload of a network packet.
  5. *
  6. * @author Mihai Plasoianu
  7. * @author Wulf Pfeiffer
  8. */
  9. public class Packet {
  10. private byte[] payload;
  11. private String protocol;
  12. /**
  13. * Constructs Packet from byte[]
  14. *
  15. * @param payload
  16. * The byte[] payload
  17. */
  18. public Packet(byte[] payload, String protocol) {
  19. this.payload = payload;
  20. this.protocol = protocol;
  21. }
  22. /**
  23. * Constructs Packet from String
  24. *
  25. * @param payload
  26. * The String payload
  27. */
  28. public Packet(String payload, String protocol) {
  29. this.payload = payload.getBytes();
  30. this.protocol = protocol;
  31. }
  32. /**
  33. * Returns a byte[] representation of the payload.
  34. *
  35. * @return byte[] representation.
  36. */
  37. public byte[] getBytes() {
  38. return payload;
  39. }
  40. /**
  41. * Returns a String representation of the payload.
  42. * Depending on the protocol, the String will be represented
  43. * as a String of it's byte values.
  44. * E.g.: the byte[] {0x01, 0x4A, 0x03} would look like
  45. * the String "01, 4A, 03", or
  46. * otherwise a normal String will be created with the payload.
  47. *
  48. * @return String representation.
  49. */
  50. @Override
  51. public String toString() {
  52. if (protocol.equals("FTP")
  53. || protocol.equals("HTTP")
  54. || protocol.equals("HTTPS")
  55. || protocol.equals("SIP")) {
  56. return new String(payload);
  57. } else {
  58. return HelperUtils.bytesToHexString(payload);
  59. }
  60. }
  61. }