Advertisement
Guest User

Untitled

a guest
May 16th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. public class Test {
  2.  
  3. public static void main(String [] args) {
  4.  
  5. String datagram = "45 00 00 4e f7 fa 40 00 38 06 9d 33 d4 b6 18 1b c0 a8 00 02 0b 54 b9 a6 fb f9 3c 57 c1 0a 06 c1 80 18 00 e3 ce 9c 00 00 01 01 08 0a 03 a6 eb 01 00 0b f8 e5 6e 65 74 77 6f 72 6b 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20 69 73 20 66 75 6e";
  6.  
  7. String datagram2 = datagram.replace(" ", "");
  8. String version = datagram2.substring(0,1);
  9. String protokol = datagram2.substring(19,20);
  10. String sourceAddress1 = datagram2.substring(24,26);
  11. String sourceAddress2 = datagram2.substring(26,28);
  12. String sourceAddress3 = datagram2.substring(28,30);
  13. String sourceAddress4 = datagram2.substring(30,32);
  14. String destinationAddress1 = datagram2.substring(32,34);
  15. String destinationAddress2 = datagram2.substring(34,36);
  16. String destinationAddress3 = datagram2.substring(36,38);
  17. String destinationAddress4 = datagram2.substring(37,40);
  18.  
  19.  
  20. int numberVersion= Integer.parseInt(version.trim(), 16);
  21. int numberProtokol = Integer.parseInt(protokol.trim(), 16);
  22. int numberSourceAddress1 = Integer.parseInt(sourceAddress1.trim(), 16);
  23. int numberSourceAddress2 = Integer.parseInt(sourceAddress2.trim(), 16);
  24. int numberSourceAddress3 = Integer.parseInt(sourceAddress3.trim(), 16);
  25. int numberSourceAddress4 = Integer.parseInt(sourceAddress4.trim(), 16);
  26. int numberDestinationAddress1 = Integer.parseInt(destinationAddress1.trim(), 16);
  27. int numberDestinationAddress2 = Integer.parseInt(destinationAddress2.trim(), 16);
  28. int numberDestinationAddress3 = Integer.parseInt(destinationAddress3.trim(), 16);
  29. int numberDestinationAddress4 = Integer.parseInt(destinationAddress4.trim(), 16);
  30.  
  31.  
  32. System.out.println("wersja: " + numberVersion);
  33. System.out.println("protokol:" + numberProtokol);
  34. System.out.println("adres źródłowy: " + numberSourceAddress1 + "." + numberSourceAddress2 + "."
  35. + numberSourceAddress3 + "." + numberSourceAddress4);
  36.  
  37. System.out.println("adres docelowy: " + numberDestinationAddress1 + "." + numberDestinationAddress2 + "."
  38. + numberDestinationAddress3 + "." + numberDestinationAddress4);
  39.  
  40. if(numberProtokol == 6){
  41.  
  42. String sourcePort = datagram2.substring(40, 44).replace(" ", "");
  43. String destinationPort = datagram2.substring(44, 48).replace(" ", "");
  44.  
  45. int numberSourcePort = Integer.parseInt(sourcePort.trim(), 16);
  46. int numberDestinationPort = Integer.parseInt(destinationPort.trim(), 16);
  47.  
  48. System.out.println("Numer portu zrodlowego: " + numberSourcePort);
  49. System.out.println("Numer portu docelowego: " + numberDestinationPort);
  50.  
  51. String data = datagram2.substring(104).replace(" ", "");
  52.  
  53. StringBuilder output = new StringBuilder();
  54. for (int i = 0; i < data.length(); i+=2) {
  55. String str = data.substring(i, i+2);
  56. output.append((char)Integer.parseInt(str.trim(), 16));
  57. }
  58.  
  59. System.out.println(output);
  60. }
  61.  
  62. else if(numberProtokol == 17){
  63.  
  64. String sourcePort = datagram2.substring(40, 44).replace(" ", "");
  65. String destinationPort = datagram2.substring(44, 48).replace(" ", "");
  66.  
  67. int numberSourcePort = Integer.parseInt(sourcePort.trim(), 16);
  68. int numberDestinationPort = Integer.parseInt(destinationPort.trim(), 16);
  69.  
  70. System.out.println("Numer portu zrodlowego: " + numberSourcePort);
  71. System.out.println("Numer portu docelowego: " + numberDestinationPort);
  72.  
  73. String data = datagram.substring(56).replace(" ", "");
  74.  
  75. StringBuilder output = new StringBuilder();
  76. for (int i = 0; i < data.length(); i += 2) {
  77. String str = data.substring(i, i + 2);
  78. output.append((char) Integer.parseInt(str, 16));
  79. }
  80.  
  81. System.out.println(output);
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement