-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicmpParser.java
More file actions
53 lines (46 loc) · 1.36 KB
/
icmpParser.java
File metadata and controls
53 lines (46 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.Arrays;
public class icmpParser{
public static byte[] ICMPparser(byte[] ipPacket) {
byte prot = ipPacket[9];
byte[] icmpPacket = null;
if (Byte.toString(prot).equals("1")){ //icmpParser 0x01
icmpPacket = ipParser.getIpPayload(ipPacket);
}
return icmpPacket;
}
public static String getICMPHeader(byte[] icmpPacket){
int type = geticmpType(icmpPacket);
int c = getcode(icmpPacket);
int cs = getchecksum(icmpPacket);
String toPrint = "Type: "+type+"\n"
+"Code: "+c+"\n"
+"Checksum: "+cs+"\n";
return toPrint;
}
public static String getICMPPacket(byte[] icmpPacket){
int type = geticmpType(icmpPacket);
int c = getcode(icmpPacket);
int cs = getchecksum(icmpPacket);
byte[] payload = Lib.copyByteArray(icmpPacket, 4);
String toPrint = "Type: "+type+"\n"
+"Code: "+c+"\n"
+"Checksum: "+cs+"\n"
+"Payload: "+ Lib.getString(payload);
return toPrint;
}
public static int geticmpType(byte[] icmpPacket) {
byte icmpType = icmpPacket[0];
int type = icmpType;
return type;
}
public static int getcode(byte[] icmpPacket) {
byte code = icmpPacket[1];
int c = code;
return c;
}
public static int getchecksum(byte[] icmpPacket) {
byte [] checksum = Arrays.copyOfRange(icmpPacket, 2, 4);
int cs = Lib.byteArrayToInt(checksum);
return cs;
}
}