-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRIVA_Receiver.java
More file actions
592 lines (464 loc) · 20.8 KB
/
RIVA_Receiver.java
File metadata and controls
592 lines (464 loc) · 20.8 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
public class RIVA_Receiver extends Thread {
private ServerSocket ss;
static AtomicBoolean allTransfersCompleted = new AtomicBoolean(false);
static String baseDir = "./";
static String baseDirInjected= "./";
public static int injectedPages=0;
static long totalTransferredBytes = 0L;
static long totalChecksumBytes = 0L;
static long INTEGRITY_VERIFICATION_BLOCK_SIZE = 256 * 1024 * 1024;
public static volatile boolean transferdone=false;
public static volatile boolean allPagesinjected=false;
boolean debug = false;
long startTime;
public static boolean everythingEnded = false;
public static String filesystemName;
static LinkedBlockingQueue<FiverFile> files = new LinkedBlockingQueue<>();
static LinkedBlockingQueue<FiverFile> injectedfiles = new LinkedBlockingQueue<>();
static HashMap<String, ArrayList> PageOffsetForEachFile =new HashMap<>();
public static class FiverFile {
public FiverFile(File file, long offset, long length) {
this.file = file;
this.offset = offset;
this.length = length;
}
File file;
Long offset;
Long length;
boolean isEvicted = false;
}
public RIVA_Receiver(int port) {
try {
ss = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
Socket clientSock = ss.accept();
// clientSock.setSoTimeout(60000);
System.out.println("Connection established from " + clientSock.getInetAddress());
saveFile(clientSock);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void saveFile(Socket clientSock) throws IOException, InterruptedException {
startTime = System.currentTimeMillis();
DataInputStream dataInputStream = new DataInputStream(clientSock.getInputStream());
INTEGRITY_VERIFICATION_BLOCK_SIZE = dataInputStream.readLong();
allTransfersCompleted.set(false);
totalTransferredBytes = 0L;
totalChecksumBytes = 0L;
ChecksumRunnable checksumRunnable = new ChecksumRunnable();
Thread checksumThread = new Thread(checksumRunnable, "checksumThread");
checksumThread.start();
byte[] buffer = new byte[128 * 1024];
while (true) {
String fileName = dataInputStream.readUTF();
if (fileName.equals("done")) {
break;
}
long offset = dataInputStream.readLong();
long fileSize = dataInputStream.readLong();
System.out.println("File " + fileName + "\t" + humanReadableByteCount(fileSize, false) + " bytes" + " time:" + (System.currentTimeMillis() - startTime) / 1000.0 + " s");
RandomAccessFile randomAccessFile = new RandomAccessFile(baseDir + fileName, "rw");
if (offset > 0) {
randomAccessFile.getChannel().position(offset);
}
long remaining = fileSize;
int readBytes = 0;
long currentBlockSize = Math.min(INTEGRITY_VERIFICATION_BLOCK_SIZE, fileSize);
int currentBlock = 0;
long bytesReadForBlock = 0L;
while (remaining > 0) {
readBytes = dataInputStream.read(buffer, 0, (int) Math.min(buffer.length, remaining));
randomAccessFile.write(buffer, 0, readBytes);
if (readBytes == -1)
break;
totalTransferredBytes += readBytes;
remaining -= readBytes;
bytesReadForBlock += readBytes;
if (bytesReadForBlock >= currentBlockSize) {
if (debug) {
System.out.println("Adding item queue " + fileName + " currentBlock" + currentBlock + " BytesRead:" + bytesReadForBlock
+" offset:" + (currentBlock * INTEGRITY_VERIFICATION_BLOCK_SIZE) + " blocksize:" + currentBlockSize);
}
FiverFile fiverFile = new FiverFile(new File(baseDir + fileName),
currentBlock * INTEGRITY_VERIFICATION_BLOCK_SIZE, currentBlockSize);
files.offer(fiverFile);
CacheEvictRunnable cacheEvictRunnable = new CacheEvictRunnable(fiverFile);
new Thread(cacheEvictRunnable).start();
bytesReadForBlock = bytesReadForBlock - currentBlockSize;
currentBlockSize = Math.min(INTEGRITY_VERIFICATION_BLOCK_SIZE, remaining + bytesReadForBlock);
currentBlock++;
}
}
transferdone=true;
System.out.println("transferdone in writer");
randomAccessFile.close();
if (readBytes == -1) {
System.out.println("Read -1, closing the connection...");
return;
}
}
everythingEnded = true;
System.out.println("receiver done");
dataInputStream.close();
}
public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
public static void getPagesAndInject(String path)
{
File file =new File(path);
int counter=0;
try
{
List<File> listOfFiles = new LinkedList<>();
if(file.isDirectory())
{
for (File f : file.listFiles())
{
listOfFiles.add(f);
}
}
else {
listOfFiles.add(file);
}
System.out.println("There are " + listOfFiles.size() + " files");
for (File f : listOfFiles) {
String fileName = f.getPath();
System.out.println(f.getPath());
String cmd2[] = {"/bin/bash", "-c", "filefrag -v " + f.getPath()};
Process proc2 = null;
proc2 = Runtime.getRuntime().exec(cmd2);
proc2.waitFor();
BufferedReader input2 = new BufferedReader(new InputStreamReader(proc2.getInputStream()));
String line;
String phy_offset = "";
int length;
boolean start = false;
while ((line = input2.readLine()) != null) {
//System.out.println(line);
if (line.contains("ext")) {
start = true;
continue;
}
if (start) {
line = line.replaceAll("\\s+", "");
line = line.replaceAll("\\.\\.", ":");
String splitted[] = line.split(":");
if (splitted[0].equals(fileName)) {
start = false;
continue;
}
phy_offset = splitted[3];
length = Integer.parseInt(splitted[5]);
System.out.println("phy_offset=" + phy_offset + "\tlength=" + length);
// for(int i=0; i<length; i++)
// {
String cmd3[] = {"/bin/bash", "-c", "sudo dd if=/dev/zero of=" + filesystemName + " bs=4096 seek=" + phy_offset + " count=" + length +
" oflag=direct"};
Process proc3 = Runtime.getRuntime().exec(cmd3);
proc3.waitFor();
counter++;
//}
}
}
input2.close();
System.out.println("totalInjectedPages=" + counter);
}
}catch (Exception e) { e.printStackTrace(); }
}
public static void CheckSumForInjected(String path)
{
File file =new File(path);
byte[] buffer = new byte[128 * 1024];
try
{
if(file.isDirectory())
{
for (File f : file.listFiles())
{
String fileName = f.getName();
long fileSize = f.length();
FileInputStream fis = new FileInputStream(f);
long remaining = fileSize;
int readBytes = 0;
long currentBlockSize = Math.min(INTEGRITY_VERIFICATION_BLOCK_SIZE, fileSize);
int currentBlock = 0;
long bytesReadForBlock = 0L;
while (remaining > 0) {
readBytes = fis.read(buffer, 0, (int) Math.min(buffer.length, remaining));
if (readBytes == -1)
break;
remaining -= readBytes;
bytesReadForBlock += readBytes;
if (bytesReadForBlock >= currentBlockSize) {
FiverFile fiverFile = new FiverFile(new File(baseDirInjected + fileName),
currentBlock * INTEGRITY_VERIFICATION_BLOCK_SIZE, currentBlockSize);
injectedfiles.offer(fiverFile);
bytesReadForBlock = bytesReadForBlock - currentBlockSize;
currentBlockSize = Math.min(INTEGRITY_VERIFICATION_BLOCK_SIZE, remaining + bytesReadForBlock);
currentBlock++;
}
}
fis.close();
}
}
MessageDigest md = null;
md = MessageDigest.getInstance("MD5");
md.reset();
buffer = new byte[128 * 1024];
while (!injectedfiles.isEmpty())
{
FiverFile fiverFile = injectedfiles.poll(100, TimeUnit.MILLISECONDS);
FileInputStream fis = new FileInputStream(fiverFile.file);
if (fiverFile.offset > 0) {
fis.getChannel().position(fiverFile.offset);
}
DigestInputStream dis = new DigestInputStream(fis, md);
long remaining = fiverFile.length;
int read;
while (remaining > 0)
{
read = dis.read(buffer, 0, (int)Math.min(buffer.length, remaining));
if (read == -1) {
Thread.sleep(100);
}
else {
totalChecksumBytes += read;
remaining -= read;
}
}
dis.close();
fis.close();
byte[] digest = md.digest();
String hex = (new HexBinaryAdapter()).marshal(digest);
System.out.println(hex);
md.reset();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length > 0) {
baseDir = args[0];
baseDirInjected = args[0];
}
//////////////////////////////////////////////////////////////////////////////////
//IMPORTANT -- DONT UNCOMMENT THIS PART UNLESS YOU SURE WHAT YOU DOING//
//IT WILL MAKE FAULT INJECTION TO TRANSFERRED FILES AND IF YOU DID NOT//
//PROVIDE CORRECT FILESYSTEM NAME AND BASE DIRECTORY IT CAN MESS YOUR SYSTEM//
filesystemName="/dev/sda2";
//getPagesAndInject(baseDir );
//CheckSumForInjected(baseDir);
//////////////////////////////////////////////////////////////////////////////////
RIVA_Receiver fs = new RIVA_Receiver(2010);
fs.start();
}
public class ChecksumRunnable implements Runnable {
MessageDigest md = null;
long totalChecksumTime = 0;
@Override
public void run() {
System.out.println("MyThread - START " + Thread.currentThread().getName());
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
md.reset();
DataOutputStream dataOutputStream = null;
try {
ServerSocket socket = new ServerSocket(20180);
dataOutputStream = new DataOutputStream(socket.accept().getOutputStream());
System.out.println("Checksum Connection accepted");
} catch (IOException e) {
e.printStackTrace();
}
byte[] buffer = new byte[128 * 1024];
while (true) {
if (everythingEnded)
break;
try {
FiverFile fiverFile = files.poll(100, TimeUnit.MILLISECONDS);
if (fiverFile == null) {
continue;
}
while (true) {
synchronized (fiverFile) {
if (fiverFile.isEvicted)
break;
}
Thread.sleep(10);
}
FileInputStream fis = new FileInputStream(fiverFile.file);
if (fiverFile.offset > 0) {
fis.getChannel().position(fiverFile.offset);
}
DigestInputStream dis = new DigestInputStream(fis, md);
long startingTime = System.currentTimeMillis();
long remaining = fiverFile.length;
int read;
while (remaining > 0) {
read = dis.read(buffer, 0, (int)Math.min(buffer.length, remaining));
if (read == -1) {
System.out.println("Read -1, size:" + humanReadableByteCount(fiverFile.length, false) +
" remaining:" + remaining +
" time:" + (System.currentTimeMillis() - startTime) / 1000.0 + " sec");
Thread.sleep(100);
}
else {
totalChecksumBytes += read;
remaining -= read;
}
}
dis.close();
fis.close();
byte[] digest = md.digest();
String hex = (new HexBinaryAdapter()).marshal(digest);
System.out.println("Sending hex:" + hex + " duration:" + (System.currentTimeMillis() - startingTime)/1000.0 + " time " + (System.currentTimeMillis() - startTime)/1000.0);
dataOutputStream.writeUTF(hex);
System.out.println(hex);
//System.out.println("injectedPages="+injectedPages);
injectedPages++;
md.reset();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void injectToFiverFile(FiverFile fiverFile)
{
try {
File f = fiverFile.file;
String fileName = f.getName();
// System.out.println(fileName);
String cmd2[] = {"/bin/bash", "-c", "filefrag -v " + baseDir + fileName};
Process proc2 = null;
proc2 = Runtime.getRuntime().exec(cmd2);
proc2.waitFor();
BufferedReader input2 = new BufferedReader(new InputStreamReader(proc2.getInputStream()));
String line;
String phy_offset = "";
int length;
boolean start = false;
int lengthCumulative = 0;
while ((line = input2.readLine()) != null) {
if (line.contains("ext")) {
start = true;
continue;
}
if (start) {
line = line.replaceAll("\\s+", "");
line = line.replaceAll("\\.\\.", ":");
String splitted[] = line.split(":");
if (splitted[0].equals(fileName)) {
start = false;
continue;
}
phy_offset = splitted[3];
length = Integer.parseInt(splitted[5]);
lengthCumulative = lengthCumulative + length;
if (Integer.parseInt(phy_offset) > 3000) {
String cmd3[] = {"/bin/bash", "-c", "sudo dd if=/dev/zero of=" + filesystemName + " bs=4096 seek=" + phy_offset + " count=" + length + " oflag=direct"};
Process proc3 = Runtime.getRuntime().exec(cmd3);
proc3.waitFor();
}
}
}
input2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static class CacheEvictRunnable implements Runnable{
FiverFile fiverFile;
public CacheEvictRunnable(FiverFile fiverFile) {
this.fiverFile = fiverFile;
}
public void run () {
try {
long start = (fiverFile.offset / 1048576);
long end = (fiverFile.offset + fiverFile.length) / 1048576;
String evictionRange = start + "m-" + end + "m";
while(InCache(fiverFile.file.getPath(),evictionRange))
{
String cmd2[] = {"vmtouch", "-e", "-p", evictionRange, fiverFile.file.getPath()};
Process proc2 = Runtime.getRuntime().exec(cmd2);
proc2.waitFor();
}
//////////////////////////////////////////////////////////////////////////////////
//IMPORTANT -- DONT UNCOMMENT THIS PART UNLESS YOU SURE WHAT YOU DOING//
//IT WILL MAKE FAULT INJECTION TO TRANSFERRED FILES AND IF YOU DID NOT//
//PROVIDE CORRECT FILESYSTEM NAME AND BASE DIRECTORY IT CAN MESS YOUR SYSTEM//
//injectToFiverFile(fiverFile);
//////////////////////////////////////////////////////////////////////////
synchronized (fiverFile) {
fiverFile.isEvicted = true;
}
} catch (Exception e) {
System.out.println("Exception in cache evict");
}
}
}
public static boolean InCache(String filename, String range) {
String percentage = "";
try {
String cmd2[] = {"/bin/bash", "-c", "vmtouch -p " + range + " " + filename};
Process proc2 = Runtime.getRuntime().exec(cmd2);
// System.out.println(Arrays.toString(cmd2));
BufferedReader input2 = new BufferedReader(new InputStreamReader(proc2.getInputStream()));
String line;
while ((line = input2.readLine()) != null) {
//System.out.println(line);
if (line.contains("Resident")) {
//System.out.println(line);
line = line.replaceAll("\\s+", ",");
//System.out.println(line);
String splitted[] = line.split(",");
percentage = splitted[splitted.length - 1];
//System.out.println("percentage="+percentage);
break;
}
}
input2.close();
} catch (Exception e) {
System.out.println("Exception in cache check");
}
float p;
try {
p = Float.parseFloat(percentage.split("%")[0]);
} catch (Exception e) {
System.out.println("Exception inCache fun for input: range=" + range + "\tfilename=" + filename + "\tp=" + percentage);
p = 3;
}
if (p < 1)
return false;
else
return true;
}
}