-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudpParser.java
More file actions
62 lines (55 loc) · 1.78 KB
/
udpParser.java
File metadata and controls
62 lines (55 loc) · 1.78 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
54
55
56
57
58
59
60
61
62
import java.util.Arrays;
public class udpParser{
public static byte[] UDPparser(byte[] ipPacket) {
byte prot = ipPacket[9];
byte[] udpPacket = null;
if (Byte.toString(prot).equals("17")){
udpPacket = ipParser.getIpPayload(ipPacket);
}
return udpPacket;
}
public static String getUDPHeader(byte[] udpPacket){
int udpS = getudpSport(udpPacket) ;
int udpD = getudpDport(udpPacket);
int len = getlength(udpPacket);
int cs = getudpChecksum(udpPacket);
String toPrint = "Source Port: "+udpS+"\n"
+"Destination Port: "+udpD+"\n"
+"Length: "+len+"\n"
+"Checksum: "+cs+"\n";
return toPrint;
}
public static String getUDPPacket(byte[] udpPacket){
int udpS = getudpSport(udpPacket) ;
int udpD = getudpDport(udpPacket);
int len = getlength(udpPacket);
int cs = getudpChecksum(udpPacket);
byte[] payload = Lib.copyByteArray(udpPacket, 8);
String toPrint = "Source Port: "+udpS+"\n"
+"Destination Port: "+udpD+"\n"
+"Length: "+len+"\n"
+"Checksum: "+cs+"\n"
+"Payload: "+ Lib.getString(payload);
return toPrint;
}
public static int getudpSport(byte[] udpPacket) {
byte [] udpSport = Arrays.copyOfRange(udpPacket, 0, 2);
int udpS = Lib.byteArrayToInt(udpSport);
return udpS;
}
public static int getudpDport(byte[] udpPacket) {
byte [] udpDport = Arrays.copyOfRange(udpPacket, 2, 4);
int udpD = Lib.byteArrayToInt(udpDport);
return udpD;
}
public static int getlength(byte[] udpPacket) {
byte [] length = Arrays.copyOfRange(udpPacket, 4, 6);
int len = Lib.byteArrayToInt(length);
return len;
}
public static int getudpChecksum(byte[] udpPacket) {
byte [] udpChecksum = Arrays.copyOfRange(udpPacket, 6, 8);
int cs = Lib.byteArrayToInt(udpChecksum);
return cs;
}
}