-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLib.java
More file actions
218 lines (195 loc) · 6.4 KB
/
Lib.java
File metadata and controls
218 lines (195 loc) · 6.4 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import java.io.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;
public class Lib{
public static ArrayList<byte[]> read(String filenameRead){
//read in the packets from a file
FileReader fileReader = null;
String line;
BufferedReader bufferedReader;
StringBuffer stringBuffer;
String[] packetString = null;
ArrayList<byte[]> packetsList = new ArrayList<>();
try {
File file = new File(filenameRead);
fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);
stringBuffer = new StringBuffer();
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
if(line.equals("")){
stringBuffer.append("\t");
}
}
String string = stringBuffer.toString();
packetString = string.split("\t"); //read files into a string array
for (int i = 0; i < packetString.length; i++){
packetString[i] = packetString[i].replaceAll("\\s+", "");
packetsList.add(hexStringToByteArray(packetString[i]));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return packetsList;
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
static int getQueueSize(Queue<ByteArray> queue){
int size = 0;
Iterator<ByteArray> it = queue.iterator();
while(it.hasNext()){
byte[] a = it.next().getBytes();
size += a.length;
}
return size;
}
public static byte[] getListofFragments(Queue<ByteArray> queue) {
byte[] byteArray=new byte[getQueueSize(queue)];
int i = 0;
Queue<ByteArray> sortedQueue = new PriorityQueue<>(queue);
while(sortedQueue.size()>=1){
byte[] a = sortedQueue.poll().getBytes();
System.arraycopy(a, 0, byteArray, i, a.length);
i += a.length;
}
return byteArray;
}
public static int getCurrLen(Queue<ByteArray> queue) {
Queue<ByteArray> sortedQueue = new PriorityQueue<>(queue);
//
int currLen = 0;
byte[] b = sortedQueue.poll().getBytes();
int maxOS = ipParser.getfragOffset(b)*8;
int minOS = ipParser.getfragOffset(b)*8;
int i = sortedQueue.size();
while(sortedQueue.size()>=1){
byte[] a = sortedQueue.poll().getBytes();
maxOS = Math.max(maxOS,ipParser.getfragOffset(a)*8);
minOS = Math.min(minOS,ipParser.getfragOffset(a)*8);
i--;
if (i == 0){
currLen = (maxOS - minOS) + ipParser.getIpPayload(a).length;
}
}
return currLen;
}
public static byte[] getPayloadFromQueue(Queue<ByteArray> queue, int size){
byte[] payload = new byte[size];
Queue<ByteArray> sortedQueue = new PriorityQueue<>(queue);
while(sortedQueue.size() >= 1){
byte[] a = sortedQueue.poll().getBytes();
int os = ipParser.getfragOffset(a)*8;
byte[] pl = ipParser.getIpPayload(a);
for (int i = 0; i < pl.length; i++){
payload[i+os] = pl[i];
}
}
return payload;
}
public static byte[] assemblePacket(byte[] etherHeader, byte[] header, byte[] payload, int sid, byte[] fragmentList){
byte[] packet = new byte[etherHeader.length+header.length+payload.length+1+fragmentList.length];
for (int i = 0; i < etherHeader.length; i++){
packet[i] = etherHeader[i];
}
for (int i = 0; i < header.length; i++){
packet[etherHeader.length+i] = header[i];
}
for (int i = 0; i < payload.length; i++){
packet[etherHeader.length+header.length+i] = payload[i];
}
packet[etherHeader.length+header.length+payload.length] = (byte)sid;
for (int i = 0; i < fragmentList.length; i++){
packet[etherHeader.length+i+header.length+1+payload.length] = fragmentList[i];
}
return packet;
}
public static byte[] assemblePacket(byte[] etherPacket, int sid){
byte[] packet = new byte[etherPacket.length + 1];
for (int i = 0; i < etherPacket.length; i++){
packet[i] = etherPacket[i];
}
packet[etherPacket.length] = (byte)sid;
return packet;
}
public static boolean checkForGap(Queue<ByteArray> queue){
boolean isGapped=false;
Queue<ByteArray> sortedQueue = new PriorityQueue<>(queue);
int prev = 0;
byte[] a = sortedQueue.poll().getBytes();
prev = ipParser.getfragOffset(a)*8 + ipParser.getIpPayload(a).length;
while(sortedQueue.size() >= 1){
byte[] b = sortedQueue.poll().getBytes();
int next = ipParser.getfragOffset(b)*8 + ipParser.getIpPayload(b).length;
int nextOS = ipParser.getfragOffset(b)*8;
if(prev >= nextOS){
prev = next;
}else{
isGapped = true;
return isGapped;
}
}
return isGapped;
}
public static boolean checkForOverlap(Queue<ByteArray> queue){
boolean isOverlapped=false;
Queue<ByteArray> sortedQueue = new PriorityQueue<>(queue);
int prev = 0;
byte[] a = sortedQueue.poll().getBytes();
prev = ipParser.getfragOffset(a)*8 + ipParser.getIpPayload(a).length;
while(sortedQueue.size() >= 1){
byte[] b = sortedQueue.poll().getBytes();
int next = ipParser.getfragOffset(b)*8 + ipParser.getIpPayload(b).length;
int nextOS = ipParser.getfragOffset(b)*8;
if(prev > nextOS){
isOverlapped = true;
return isOverlapped;
}else{
prev = next;
}
}
return isOverlapped;
}
public static byte[] copyByteArray(byte[] b, int beginInt){
byte[] a = new byte[b.length-beginInt];
int j = 0;
for (int i = beginInt; i < b.length; i++){
a[j] = b[i];
j++;
}
return a;
}
public static String getString(byte[] b){
StringBuilder sb = new StringBuilder();
for (byte i : b){
sb.append(String.format("%02X", i));
sb.append(" ");
}
String s = sb.toString();
return s;
}
public static String getIPAddress(String ipString){
String ip = "";
for(int i = 0; i < ipString.length(); i = i + 2) {
ip = ip + Integer.valueOf(ipString.substring(i, i+2), 16) + ".";
}
return ip;
}
public static int byteArrayToInt(byte[] b){
ByteBuffer wrapped = ByteBuffer.wrap(b); // big-endian by default
short value = wrapped.getShort(); // 1
return value;
}
}