-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSftpClient.java
More file actions
441 lines (380 loc) · 13 KB
/
SftpClient.java
File metadata and controls
441 lines (380 loc) · 13 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
import java.io.*;
import java.net.*;
import java.util.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
public class SftpClient
{
public static void main(String[] args) throws Exception
{
// Get command line argument.
if (args.length != 4) {
System.out.println("Required arguments: address, port, filename, loss rate");
return;
}
int port = Integer.parseInt(args[1]);
InetAddress ipaddr = InetAddress.getByName(args[0]);
DatagramSocket socket = new DatagramSocket(port);
socket.setSoTimeout(2000); //two second timeout! (default)
//file readin
Path path = Paths.get(args[2]);
String filename = args[2];
byte[] filebuf = Files.readAllBytes(path); //read in the file
int fileptr = 0; //pointer before which all data in the filebuf has been sent
System.out.println("File size: "+filebuf.length);
//for simulating loss
int lossrate = Integer.parseInt(args[3]);
//for timing of packet RTT
long sendtime;
long rectime;
//outgoing data variables
boolean synflag = true;
boolean ackflag = false; //will always be false for client
boolean finflag = false;
byte[] header = new byte[8]; //header byte array
byte[] datasent = new byte[0]; //datagram body byte array
short dlength;
//incoming data variables
boolean rec_syn;
boolean rec_ack;
boolean rec_fin;
int rec_dlength;
int rec_seqack;
//time variables for keeping track of total transfer time
long starttime = 0;
long totaltime = 0;
//internal state variables
boolean acked = false; //whether the client has gotten the initial ack from the server
boolean nameacked = false; //whether the client has had the filename acked by the server
int seq = 0; //the client's sequence number representing the back of the window
int ack = 0; //largest ack the client has received from the server
int sent = 0; //sequence number of the last packet sent out
//variables for buffering the last N packets
int n = 1; //number of old packets to buffer
byte[][] packetholder;
packetholder = new byte[n][408];
//go back N-related timers
double acktimer = 0; //stores the last time a packet was acked
double rttimer = System.currentTimeMillis(); //millisstores the moment the last volley of packets was sent out for retransmission purposes
//initial filling up of buffer
int minifileptr = 0;
int packets = filebuf.length/400+1; //number of packets needed to send whole file
System.out.println("Number of packets:"+packets);
int filesize = 400;
for (int i=0; i<Math.min(n,packets); i++)
{
filesize = 400;
finflag = false;
if (filebuf.length-minifileptr<400) {filesize = (filebuf.length-minifileptr); finflag=true;}
datasent = prepData(filebuf, minifileptr, (short)filesize);
header = formPacket(false,false,finflag,(short)filesize,1+filename.length()+minifileptr,datasent);
packetholder[i]=header;
minifileptr = minifileptr + filesize;
}
// Processing loop.
while (true) {
//Thread.sleep(2000);
if (seq == 0)
{
synflag = true;
datasent = new byte[0];
dlength = (short)datasent.length;
}
else if (seq == 1)
{
synflag = false;
datasent = filename.getBytes();
dlength = (short)datasent.length;
}
else
{
synflag = false;
datasent = packetholder[0];
dlength = (short)datasent.length;
}
sendtime = System.currentTimeMillis();
if (starttime ==0) {starttime = System.currentTimeMillis();}
if (seq <= 1)
{
header = formPacket(synflag,false,false,dlength,seq,datasent);
DatagramPacket packet = new DatagramPacket(header,header.length,ipaddr,port);
socket.send(packet);
if (seq==0) {sent = 0;}
if (seq==1) {sent = 1;}
}
else
{
int sentsoon=sent; //this value will become the new "sent" after this packet volley
header = packetholder[Math.min(n,packets)-1];
rec_seqack = decodeVal(header, 4, 7); //figure out the seq number of the last packet in the packetholder
if (packets>n) { //rolling system for updating the buffer with packets
header = packetholder[0];
int rec_oldseq = decodeVal(header, 4, 7); //look at the sequence number of the first packet
//while (rec_seqack-seq<Math.min(400*(Math.min(n,packets)-1),filebuf.length-rec_seqack)) //the window is expanded ahead
while (seq>rec_oldseq && filebuf.length-rec_seqack>=400)
{
header = packetholder[Math.min(n,packets)-1]; //look at the last packet
int old_dlength = -decodeVal(header, 2, 3); //the dlength of the old last packet
short send_dlength = (short)Math.min(filebuf.length-rec_seqack-400+filename.length()+1,400); //the dlength of the new last packet
if (send_dlength<0) {send_dlength=0;}
datasent = prepData(filebuf,rec_seqack+old_dlength-filename.length()-1,send_dlength); //get the new packet's data ready
if (send_dlength<400)
{
finflag=true;
int finseq = rec_seqack+old_dlength;
System.out.println("The FIN packet is in the buffer. (seq= "+finseq+")");
}
else {finflag = false;}
header = formPacket(false,false,finflag,send_dlength,rec_seqack+400,datasent);
updatePacketBuffer (packetholder, header, n-1);
rec_seqack = decodeVal(header, 4, 7);
header = packetholder[0];
rec_oldseq = decodeVal(header, 4, 7);
}
}
for (int i=0; i<n; i++)
{
header = packetholder[i];
rec_seqack = decodeVal(header, 4, 7);
}
//set the timer for the oldest packet
rttimer = System.currentTimeMillis();
for (int i=0; i<Math.min(n,packets); i++) //in this loop, packets are actually sent (and "send" is updated)
{
header = packetholder[i];
rec_seqack = decodeVal(header, 4, 7);
if (rec_seqack>sent)
{
DatagramPacket packet = new DatagramPacket(header,header.length,ipaddr,port);
rec_dlength = -decodeVal(header, 2, 3);
System.out.println("Packet sent. (seq = "+rec_seqack+")");
//printBytes(header,12);
if (Math.random()*100<lossrate)
{
System.out.println("Packet dropped by client! (seq = "+rec_seqack+")");
}
else
{
socket.send(packet);
}
sentsoon = rec_seqack;
//Thread.sleep(250);
}
}
sent = sentsoon;
}
double progress = 100*seq/filebuf.length;
System.out.println("File transfer progress: "+progress+"%");
DatagramPacket request = new DatagramPacket(new byte[408], 408);
socket.setSoTimeout((int)(2000+rttimer-System.currentTimeMillis())); //set the socket timeout based on rttimer
try{
socket.receive(request);
rectime = System.currentTimeMillis();
acktimer = System.currentTimeMillis();
System.out.println("Reply received. (seq = "+seq+")");
//Decode the received ack.
byte[] recdata = request.getData();
if((recdata[0] & 0x80)==0x80)
{
rec_syn=true;
}
else
{
rec_syn=false;
}
if((recdata[0] & 0x40)==0x40)
{
rec_ack=true;
}
else
{
rec_ack=false;
}
if((recdata[0] & 0x20)==0x20)
{
rec_fin=true;
}
else
{
rec_fin=false;
}
rec_dlength = decodeVal(recdata, 2, 3);
rec_seqack = decodeVal(recdata, 4, 7);
// Internal counting related to received datagram.
if (seq==0 && rec_seqack>1) {
System.out.println("Cannot connect. Server is already connected.");
break;
}
else
{
if (rec_syn==true)
{
System.out.println("Connection established. (seq is now "+seq+")");
seq=1;
}
else if (rec_fin==true)
{
System.out.println("FIN signal acknowledged. Shutting down.");
totaltime = System.currentTimeMillis() - starttime;
writeStatusReport(filename, filebuf.length, totaltime);
break;
}
else
{
if (rec_seqack>seq){
seq=rec_seqack;
System.out.println("ACK="+rec_seqack+" received. SEQ updated to "+seq);
if (rec_seqack > filename.length()+1)
{
fileptr = Math.min((fileptr + datasent.length),filebuf.length);
}
}
else
{
System.out.println("Error: received an old ACK! (ack = "+rec_seqack+")");
}
}
}
}
catch(SocketTimeoutException ex)
{
System.out.println("Retransmitting:");
//set the timer for the oldest retransmission packet
rttimer = System.currentTimeMillis();
for (int i=0; i<Math.min(n,packets); i++)
{
header = packetholder[i];
rec_seqack = decodeVal(header, 4, 7);
if (rec_seqack<=sent && rec_seqack>=seq)
{
DatagramPacket packet = new DatagramPacket(header,header.length,ipaddr,port);
rec_dlength = -decodeVal(header, 2, 3);
System.out.println("Retransmitted packet sent. (seq = "+rec_seqack+")");
//printBytes(header,12);
if (Math.random()*100<lossrate)
{
System.out.println("Packet dropped by client! (seq = "+rec_seqack+")");
}
else
{
socket.send(packet);
}
//Thread.sleep(250);
}
}
System.out.println("Sent: "+sent+")");
}
}
}
private static byte[] formPacket(boolean syn, boolean ack, boolean fin, short dlength, int seqack, byte[] message) //puts the appropriate header on the UDP packet
{
//headerlead is the first two bytes of the header, which are handled bitwise
byte flagbit = 0x00;
byte[] packetHeader = new byte[8+message.length];
//set the flags (first two bytes)
if (syn==true) flagbit = (byte)(flagbit ^ 0x80);
if (ack==true) flagbit = (byte)(flagbit ^ 0x40);
if (fin==true) flagbit = (byte)(flagbit ^ 0x20);
packetHeader[0] = flagbit;
//read in the data length
packetHeader[2] = (byte)((dlength & 0xFF00)>>8);
packetHeader[3] = (byte)(dlength & 0x00FF);
//read in the seq/ack number
packetHeader[4] = (byte)((seqack & 0xFF000000)>>24);
packetHeader[5] = (byte)((seqack & 0x00FF0000)>>16);
packetHeader[6] = (byte)((seqack & 0x0000FF00)>>8);
packetHeader[7] = (byte)(seqack & 0x000000FF);
//append data
for(int i=8; i<8+message.length; i++)
{
packetHeader[i]=message[i-8];
}
return packetHeader;
}
private static int decodeVal(byte[] data, int a, int b) //decodes the number represented by the value in between bytes a and b in data
{
int doutput = 0;
int abe = (int)Math.pow(2,(8*(1+b-a)-1))+1;
for (int i=a; i<b+1; i++)
{
int babe = 128;
for (int j=0; j<8; j++)
{
int input = data[i] & babe;
int output;
if (input == 0) {output=0;} else {output=1;}
doutput = doutput + abe*(output);
abe=abe/2;
babe=babe/2;
}
}
//System.out.println();
int foutput = (int)(-doutput);
// System.out.println(doutput);
return foutput;
}
public static String decodeDataString(byte[] buf) throws Exception
{
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
InputStreamReader isr = new InputStreamReader(bais);
BufferedReader br = new BufferedReader(isr);
String line = br.readLine();
return line;
}
private static void printBytes(byte[] data, int length) //prints out the packet for diagnostic purposes
{
for (int i=0; i<length; i++)
{
int k=128;
for (int j=0; j<8; j++)
{
String output;
int input = k & data[i];
if (input == 0) {output="0";} else {output="1";}
System.out.print(output);
k=k/2;
}
{System.out.println();}
}
System.out.println("END OF PACKET");
}
private static byte[] prepData (byte[] data, int ptr, short dlength) //readies the 1016-byte data contents of next UDP packet
{
byte[] output = new byte[dlength];
for (int i=0; i<dlength; i++)
{
output[i] = data[i+ptr];
}
return output;
}
public static void writeStatusReport(String filename, int filesize, long time) {
String strFilePath = "stfp_status_report.txt";
time = time/1000; //convert to seconds
try
{
FileOutputStream fos = new FileOutputStream(strFilePath);
String strContent = "FILE TRANSFER STATUS REPORT \n Filename: "+filename+"\n Filesize: "+filesize+" bytes \n Transfer time: "+time+" s";
byte[] data = strContent.getBytes();
fos.write(data);
fos.close();
}
catch(FileNotFoundException ex)
{
System.out.println("FileNotFoundException : " + ex);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
}
}
private static byte[][] updatePacketBuffer (byte[][] packetbuffer, byte[] packet, int n) //adds newest packet to buffer; discards the oldest
{
for (int i=0; i<n; i++)
{
packetbuffer[i] = packetbuffer[i+1];
}
packetbuffer[n] = packet;
return packetbuffer;
}
}