Ping_packet.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.packets;
  2. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
  3. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  4. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.ICMPv6Checksum;
  5. /**
  6. * IcMPv6 Ping Packages
  7. *
  8. * @see <a href="http://www.networksorcery.com/enp/protocol/icmpv6.htm">http://www.networksorcery.com/enp/protocol/icmpv6.htm</a>
  9. * @see <a href="http://www.networksorcery.com/enp/rfc/rfc4443.txt">RFC4443</a>
  10. * @author Andreas T. Meyer-Berg
  11. */
  12. public class Ping_packet extends Packet {
  13. /**
  14. * Echo Request type
  15. */
  16. public static byte EchoRequest = 0x10; // 128;
  17. /**
  18. * Echo reply type
  19. */
  20. public static byte EchoReply = 0x11; //129;
  21. /**
  22. * Type of the packet
  23. */
  24. private byte type;
  25. /**
  26. * Code byte
  27. */
  28. private byte code = 0x00;
  29. /**
  30. * Checksum
  31. */
  32. private byte[] checksum = new byte[]{0x00,0x00};
  33. /**
  34. * Identifier
  35. */
  36. private byte[] id = new byte[]{0x00,0x00};
  37. /**
  38. * Sequence Number
  39. */
  40. private byte[] sequenceNumber = new byte[]{0x00,0x00};
  41. /**
  42. * Payload data
  43. */
  44. private byte[] data = "abcdefghijklmnopqrstuvwabcdefghi".getBytes();
  45. /*
  46. * 0 1 2 3
  47. * 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
  48. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  49. * | Type | Code | Checksum |
  50. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  51. * | Identifier | Sequence Number |
  52. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  53. * | Data ...
  54. * +-+-+-+-+-
  55. */
  56. /* Failure codes on response
  57. * 0 network unreachable
  58. * 1 host unreachable
  59. * 2 protocol unreachable
  60. * 3 port unreachable
  61. * 4 fragmentation needed but don‘t fragment bit set
  62. * 5 source route failed
  63. * 6 destination network unknown
  64. * 7 destination host unknown
  65. * 8 source host isolated
  66. * 9 destination network administratively prohibited
  67. * 10 destination host administratively prohibited
  68. * 11 network unreachable for requested type of service
  69. * 12 host unreachable for requested type of service
  70. * 13 communication administratively prohibited by filtering
  71. * 14 host precedence violation
  72. * 15 precedence cutoff in effect
  73. */
  74. /**
  75. * Ping package
  76. * @param timestamp time the package was sent
  77. * @param source source of the package
  78. * @param destination destination of the package
  79. * @param type type, should be EchoRequest, EchoReply
  80. * @param identifier number in the sequence, specified by sender
  81. * @param sequenceNumber number in the sequence, specified by sender
  82. */
  83. public Ping_packet(long timestamp, Port source, Port destination, byte type, short identifier, short sequenceNumber) {
  84. super(timestamp, source, destination);
  85. this.type = type;
  86. this.id = new byte[2];
  87. id[0] = (byte)(identifier & 0xff);
  88. id[1] = (byte)((identifier>>8) & 0xff);
  89. this.sequenceNumber[0] = (byte)(sequenceNumber & 0xff);
  90. this.sequenceNumber[1] = (byte)((sequenceNumber >> 8) & 0xff);
  91. }
  92. /**
  93. * Ping package without identifier
  94. * @param timestamp time the package was sent
  95. * @param source source of the package
  96. * @param destination destination of the package
  97. * @param type type, should be EchoRequest, EchoReply
  98. * @param sequenceNumber number in the sequence, specified by sender
  99. */
  100. public Ping_packet(long timestamp, Port source, Port destination, byte type, short sequenceNumber) {
  101. super(timestamp, source, destination);
  102. this.type = type;
  103. this.id = new byte[]{0x00, 0x00};
  104. this.sequenceNumber[0] = (byte)(sequenceNumber & 0xff);
  105. this.sequenceNumber[1] = (byte)((sequenceNumber >> 8) & 0xff);
  106. }
  107. @Override
  108. public byte[] dumpBytes() {
  109. byte[] bytes = new byte[8+data.length];
  110. //Checksum cleared for calculation
  111. byte[] header = new byte[]{type,code,0x00,0x00,id[0],id[1],sequenceNumber[0],sequenceNumber[1]};
  112. int i;
  113. for(i = 0; i < 8; i++)
  114. bytes[i] = header[i];
  115. for(int j=0;j<data.length;j++){
  116. bytes[i]=data[j];
  117. i++;
  118. }
  119. /**
  120. * checksum
  121. */
  122. long sum = ICMPv6Checksum.calculateChecksum(bytes);
  123. checksum[0] = (byte)(sum & 0xff);
  124. checksum[1] = (byte)(sum>>8 & 0xff);
  125. bytes[2] = checksum[0];
  126. bytes[3] = checksum[1];
  127. return bytes;
  128. }
  129. @Override
  130. public String getTextualRepresentation() {
  131. return "[Ping: "+getType()+"; time:"+timestamp+"; source:"+source.getOwner().getName()+":"+source.getPortNumber()+"; destination:"+destination.getOwner().getName()+":"+destination.getPortNumber()+"; Payload: "+getPayload()+"]";
  132. }
  133. @Override
  134. public String getPayload() {
  135. return new String(data);
  136. }
  137. /**
  138. * Type as String
  139. *
  140. * @return String representation of the Type
  141. */
  142. private String getType(){
  143. if(type == EchoRequest)
  144. return "EchoRequest";
  145. if(type == EchoReply)
  146. return "EchoReply";
  147. return "unknown";
  148. }
  149. /**
  150. * Create package and checksum
  151. * @param args unused
  152. */
  153. public static void main(String[] args) {
  154. Ping_packet pinPacket = new Ping_packet(1000, null, null, EchoRequest, (short) 12);
  155. byte[] bytes = pinPacket.dumpBytes();
  156. for(int i = 0; i<bytes.length; i++)
  157. System.out.println(bytes[i]);
  158. }
  159. @Override
  160. public String getProtocolName() {
  161. return "ICMPv6 Ping";
  162. }
  163. }