-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipParser.java
More file actions
200 lines (176 loc) · 5.53 KB
/
ipParser.java
File metadata and controls
200 lines (176 loc) · 5.53 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.util.Arrays;
public class ipParser{
public static byte[] IPparser(byte[] etherPacket){
byte[] ipPacket = null;
byte[] etherType = Arrays.copyOfRange(etherPacket, 12, 14);
if (Lib.byteArrayToInt(etherType) == 2048){ //ARP 0x800
ipPacket = Lib.copyByteArray(etherPacket, 14); //from 14 to end, header end at 37
}
return ipPacket;
}
public static String getIPHeader(byte[] ipPacket){
int version = getversion(ipPacket);
int IHL = getIHL( ipPacket);
int typeS = gettos( ipPacket);
int totLen = gettotalLength( ipPacket);
int IDS = getID( ipPacket);
int flags = getflags( ipPacket);
int fragOffset = getfragOffset( ipPacket);
int TTL = gettimeToLive( ipPacket);
String prot = getprot( ipPacket);
int cs = getActualChecksum( ipPacket);
String ipS = getipSaddress( ipPacket);
String ipD = getipDaddress( ipPacket);
String toPrint = "Version: "+version+"\n"
+"IP Header Length: "+IHL+"\n"
+"Type of Service: "+typeS+"\n"
+"Total Length: "+totLen+"\n"
+"ID: "+IDS+"\n"
+"Flags: "+flags+"\n"
+"Fragment Offset: "+fragOffset+"\n"
+"Time To Live: "+TTL+"\n"
+"Protocol Type: "+prot+"\n"
+"Header Checksum: "+cs+"\n"
+"Source IP Address: "+ipS+"\n"
+"Destination IP Address: "+ipD+"\n";
return toPrint;
}
public static String getIPPacket(byte[] ipPacket){
int version = getversion(ipPacket);
int IHL = getIHL( ipPacket);
int typeS = gettos( ipPacket);
int totLen = gettotalLength( ipPacket);
int IDS = getID( ipPacket);
int flags = getflags( ipPacket);
int fragOffset = getfragOffset(ipPacket);
int TTL = gettimeToLive( ipPacket);
String prot = getprot( ipPacket);
int cs = getActualChecksum( ipPacket);
String ipS = getipSaddress( ipPacket);
String ipD = getipDaddress( ipPacket);
byte[] pl = getIpPayload(ipPacket);
String payload = Lib.getString(pl);
String toPrint = "Version: "+version+"\n"
+"IP Header Length: "+IHL+"\n"
+"Type of Service: "+typeS+"\n"
+"Total Length: "+totLen+"\n"
+"ID: "+IDS+"\n"
+"Flags: "+flags+"\n"
+"Fragment Offset: "+fragOffset+"\n"
+"Time To Live: "+TTL+"\n"
+"Protocol Type: "+prot+"\n"
+"Header Checksum: "+cs+"\n"
+"Source IP Address: "+ipS+"\n"
+"Destination IP Address: "+ipD+"\n"
+"Payload: "+payload+"\n";
return toPrint;
}
public static int getversion(byte[] ipPacket){
int version = ((ipPacket[0] >> 4) & 0x0f);
return version;
}
public static int getIHL(byte[] ipPacket){
int IHL = (ipPacket[0] & 0x0f);
return IHL;
}
public static int gettos(byte[] ipPacket){
byte tos = ipPacket[1];
int typeS = tos;
return typeS;
}
public static int gettotalLength(byte[] ipPacket){
byte[] totalLength = Arrays.copyOfRange(ipPacket, 2, 4);
int totLen = Lib.byteArrayToInt(totalLength);
return totLen;
}
public static int getID(byte[] ipPacket){
byte[] ID = Arrays.copyOfRange(ipPacket, 4, 6);
int IDS = Lib.byteArrayToInt(ID);
return IDS;
}
public static int getflags(byte[] ipPacket){
int flags = ((ipPacket[6] & 0xff) >> 5);
return flags;
}
public static String getFlagsType(byte[] ipPacket){
int flags = getflags(ipPacket);
int fragOffset = getfragOffset(ipPacket);
String flagsType = "";
if (flags == 0 && fragOffset != 0){
flagsType = "LF";
}
if (flags == 0 && fragOffset == 0){
flagsType = "UF";
}
if (flags == 1 && fragOffset != 0){
flagsType = "MF";
}
if (flags == 1 && fragOffset == 0){
flagsType = "MF";
}
if (flags == 2 && fragOffset == 0){
flagsType = "DF";
}
return flagsType;
}
public static int getfragOffset(byte[] ipPacket){
int sh = (int)ipPacket[6];
sh <<= 8;
int fragOffset = (int) ((int)(sh | ipPacket[7]) & 0x1FFF);
return fragOffset;
}
public static int gettimeToLive(byte[] ipPacket){
byte timeToLive = ipPacket[8];
int TTL = timeToLive & 0x7f;
return TTL;
}
public static String getprot(byte[] ipPacket){
byte prot = ipPacket[9];
int p = prot;
String ps = "";
if (p == 6){
ps = "This is a TCP packet";
}
else if (p == 17) {
ps = "This is a UDP packet";
}
else if (p == 1){
ps = "This is a ICMP packet";
}else{
ps = Integer.toString(p);
}
return ps;
}
public static int getIpChecksum(byte[] ipPacket){
byte[] headChecksum = Arrays.copyOfRange(ipPacket, 10, 12);
int cs = Lib.byteArrayToInt(headChecksum);
return cs;
}
public static int getActualChecksum(byte[] ipPacket){
byte[] header = Arrays.copyOfRange(ipPacket, 0, 20);
int actualChecksum = calculateChecksum.getChecksum(header);
return actualChecksum;
}
public static String getipSaddress(byte[] ipPacket){
byte[] ipSaddress = Arrays.copyOfRange(ipPacket, 12, 16);
String ipS = Lib.getIPAddress(Lib.getString(ipSaddress).replaceAll("\\s+", ""));
return ipS;
}
public static String getipDaddress(byte[] ipPacket){
byte[] ipDaddress = Arrays.copyOfRange(ipPacket, 16, 20);
String ipD = Lib.getIPAddress(Lib.getString(ipDaddress).replaceAll("\\s+", ""));
return ipD;
}
public static byte[] getoptions(byte[] ipPacket){
byte[] options = Arrays.copyOfRange(ipPacket, 20, 24);
//modify options here
return options;
}
public static byte[] getIpPayload(byte[] ipPacket){
int IHL = getIHL(ipPacket);
int totalLen = gettotalLength(ipPacket);
byte[] payload = null;
payload = Arrays.copyOfRange(ipPacket, (IHL*4), totalLen);
return payload;
}
}