-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulatorGeneral
More file actions
472 lines (358 loc) · 14.4 KB
/
Copy pathsimulatorGeneral
File metadata and controls
472 lines (358 loc) · 14.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
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
/**
* Error Simulator for a TFTP
*Adapted from the File provided by the Professor
*
*Iteration 2:
* 1. Receive File name from client and send to Server.
* 2. Wait to receive from the Server
* 3. Receive acknowledgement from server and send to client
* 4. Then wait to Receive
*
* ERROR: Acknowledgement packet to Client is sending to the wrong port
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class ErrorSimulator {
private helplib help;
private Packet Packet;
// UDP datagram packets and sockets used to send / receive
private DatagramPacket ServerReceivePacket, clientReceivePacket, errorPacket;
private DatagramSocket receiveSocket, sendSocket, sendReceiveSocket, sendSocketw;
private int userInput;
private Scanner sc;
private InetAddress address;
private int clientPort, serverPort, index;
//Constructor for the Error Simulator class
public ErrorSimulator()
{
try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
boolean verbose;
sc = new Scanner(System.in);
while(true){
// Ask the User the Mode to Operate in *This is not really required so take it out
System.out.println("Would you like to run it in verbose mode (Y/N)?");
String input = sc.nextLine();
if(input.toUpperCase().equals("Y")){ verbose=true; break;}
if(input.toUpperCase().equals("N")){ verbose=false; break;}
System.out.println("Invalid Mode! Select either 'Y'(Yes), 'N'(No)");
}
//sc.close();
help = new helplib("ErrorSim",verbose);
//Receive socket bind to port 23
receiveSocket = new DatagramSocket(23);
// socket for send and receive bind to an available port
sendReceiveSocket = new DatagramSocket();
} catch (SocketException se) {
se.printStackTrace();
System.exit(1);
}
}
public void fileTransferReceieved(){
byte[] data = new byte[Packet.PACKETSIZE];
clientReceivePacket = new DatagramPacket(data, data.length);
help.print("Simulator: Waiting for packet.");
// Block until a datagram packet is received from receiveSocket.
try {
sendSocket.receive(clientReceivePacket);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
help.print("Transferring Files");
//Extracting the Packet Received from the Client
help.print("Port the data comes from"+ clientReceivePacket.getPort());
// Process the received datagram.
help.print("File Transfer Simulator: Packet received:");
help.print("File Transfer From host: " + clientReceivePacket.getAddress());
//get the port it received from
clientPort = clientReceivePacket.getPort();
help.print("Host port: " + clientPort);
//Length of the packet received
int len = clientReceivePacket.getLength();
help.print("Length: " + len);
help.print("Containing: " );
data = clientReceivePacket.getData();
help.print("Containing: "+Arrays.toString(data));
// Form a String from the byte array, and print the string.
String received = new String(data,0,len);
help.print(received);
}
public void passOnTFTP()
{
//Loop Forever
// for(;;) {
// Construct a DatagramPacket for receiving packets up
// to Packet.PACKETSIZE bytes long (the length of the byte array).
byte[] data = new byte[Packet.PACKETSIZE];
clientReceivePacket = new DatagramPacket(data, data.length);
help.print("Simulator: Waiting for packet.");
// Block until a datagram packet is received from receiveSocket.
try {
receiveSocket.receive(clientReceivePacket);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
//Extracting the Packet Received from the Client
help.print("Port the data comes from"+ clientReceivePacket.getPort());
// Process the received datagram.
help.print("Simulator: Packet received:");
help.print("From host: " + clientReceivePacket.getAddress());
//get the port it received from
clientPort = clientReceivePacket.getPort();
help.print("Host port: " + clientPort);
//Length of the packet received
int len = clientReceivePacket.getLength();
help.print("Length: " + len);
help.print("Containing: " );
help.print("Containing: "+Arrays.toString(data));
// Form a String from the byte array, and print the string.
String received = new String(data,0,len);
help.print(received);
//Call the method to Add Error to the packet
putError(clientReceivePacket, userInput, sendReceiveSocket, data, 69);
}
public void receiveAckFromServer(){
//Wait to receive Acknowledgement from the Receive
help.print("Waiting to Receive Acknowledgement from Server");
// Construct a DatagramPacket for receiving packets up
// to Packet.PACKETSIZE bytes long (the length of the byte array).
// for(;;){
byte[] data = new byte[Packet.PACKETSIZE];
ServerReceivePacket = new DatagramPacket(data, data.length);
help.print("Simulator: Waiting for packet From Server.");
try {
// Block until a datagram is received via sendReceiveSocket.
sendReceiveSocket.receive(ServerReceivePacket);
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
// Process the received datagram.
help.print("Simulator: Packet received:");
serverPort = ServerReceivePacket.getPort();
address = ServerReceivePacket.getAddress();
help.print("From host: " + address);
help.print("Host port: " + ServerReceivePacket.getPort());
int len = ServerReceivePacket.getLength();
help.print("Length: " + len);
help.printd("Containing: "+Arrays.toString(data));
//Sending Acknowledgement to Client
sendAckToClient(ServerReceivePacket, clientPort, address, data);
// }// end of For loop
}
public void sendAckToClient(DatagramPacket sendPacket, int port, InetAddress addr, byte[] data){
// Construct a datagram packet that is to be sent to a specified port
String msg = new String(sendPacket.getData());
sendPacket = new DatagramPacket(msg.getBytes(), msg.length(), addr, port);
help.print( "Simulator: Sending packet:");
help.print("To host: " + addr);
help.print("Destination host port: " + port);
int len = sendPacket.getLength();
help.print("Length: " + len);
help.printd("Containing: "+Arrays.toString(data));
// Send the datagram packet to the client via a new socket.
try {
sendSocket = new DatagramSocket();
} catch (SocketException se) {
se.printStackTrace();
System.exit(1);
}
try {
sendSocket.send(sendPacket);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
help.print("Simulator: packet sent using port " + sendSocket.getLocalPort());
help.print("");
}
public void putError(DatagramPacket newPacket, int userInput, DatagramSocket soc, byte[] data, int port){
//Putting the packet data into a String
String msg = new String(newPacket.getData());
help.print(msg);
//Converting the packet to byte
byte[] message = new byte[512];
help.print(""+ msg.length());
help.print(""+ message.length);
//Copying the message received into message which of type byte
System.arraycopy(msg.getBytes(), 0, message, 0, msg.length());
help.print("Received Packet: " + msg);
switch(userInput){
case 0:
//Do something
help.print("No changes to Packet");
newPacket = new DatagramPacket(message, message.length, address, 69);
help.print("Calling sendPacket Method Now");
sendPacket(soc, newPacket, address, data, port);
break;
case 1:
help.print("Changed TFTP opcode");
//Change RRQ to WRQ or WRQ to RRQ
if(message[1] == '1'){
message[1] = '2';
help.print("Whats being sent: " + message);
help.print("Case 1: 1st if Sending New Packet");
newPacket = new DatagramPacket(message, message.length, address, 69 );
sendPacket(soc, newPacket, address, data, 69);
break;
}else if(message[1] == '2'){
message[1] = '1';
help.print("Case 1: 2nd if: Sending New Packet");
newPacket = new DatagramPacket(message, message.length, address, 69 );
sendPacket(soc, newPacket, address, data, 69);
break;
}else{
help.print("Case 1: else: Sending New Packet");
newPacket = new DatagramPacket(message, message.length, address, 69 );
sendPacket(soc, newPacket, address, data, 69);
break;
}
case 2:
//Replace the Filename
StringBuilder msgb = new StringBuilder();
for(int i = 1; i < msg.length() ; i++ ){
if(Character.isAlphabetic(msg.charAt(i))){
help.print("inside if Statement");
//msgb.replace(0, i, "GF");
msgb.append(" ");
}
help.print("Case 2: "+ msgb);
newPacket = new DatagramPacket(msg.toString().getBytes(), msg.length(), address, 69 );
sendPacket(soc, newPacket, address, data, 69);
break;
}
case 3:
//Replace the Mode
byte[] mode = "octett".getBytes();
byte[] mode2 = "netasciii".getBytes();
int counter = 0;
int k;
//Find the index of 0 before mode 01Filename0Mode0
for(k = 0; k < message.length ; k++ ){
if(message[k] == 0){
counter++;
}
if ( counter == 2){break;}
}
//Change the mode using arraycopy
System.arraycopy(mode, 0, message, k+1, mode.length);
//Change the byte after the mode to 0
message[k+ mode.length + 1] = 0;
help.print("case 3: Sending New Packet" + message);
newPacket = new DatagramPacket(message, message.length, address, 69 );
sendPacket(soc, newPacket, address, data, 69);
case 4:
//Change the Delimiter btw Filename and Mode to 1
int count = 0;
int i;
for(i = 0; i<message.length-1; i++){
if(message[i] == 0){
count++;
}
if(count == 2){ break;}
}
message[i] = 1;
help.print("Sending New Packet");
newPacket = new DatagramPacket(message, message.length, address, 69 );
sendPacket(soc, newPacket, address, data, 69);
case 5:
//Change the Delimiter to 1
count = 0;
for(i = 0; i < message.length-1; i++){
if(message[i] == 0){
count++;
}
if(count == 3){break;}
}
message[i] = -1;
//help.print(""+ message[message.length-1]);
help.print("Sending New Packet");
newPacket = new DatagramPacket(message, message.length, address, 69 );
sendPacket(soc, newPacket, address, data, 69);
case 6:
//Change the Socket Invalid TID
try {
DatagramSocket socketb = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
help.print("Sending New Packet");
newPacket = new DatagramPacket(message,message.length, address, 69);
try {
sendSocketw.send(newPacket);
} catch (IOException e) {
e.printStackTrace();
}
help.print("Calling sendPacket Method Now");
sendPacket(soc, newPacket, address, data, 69);
}
}
// For sending Packet
public void sendPacket(DatagramSocket soc, DatagramPacket packet, InetAddress addr, byte[] data, int port){
help.print("Simulator: Inside sendPacket method: attempting to send");
//port = 69;
String msg = new String(packet.getData());
packet = new DatagramPacket(msg.getBytes(), msg.length(), addr, port);
help.print("Packet being sent:" + packet.getData());
help.print("To host: " + addr);
help.print("Destination host port: " + packet.getPort());
int len = packet.getLength();
help.print("Length: " + len);
//Printing out the byte that is being sent
help.printd("Containing: "+Arrays.toString(data));
// Send the datagram packet to the server via the send/receive socket.
try {
help.print("Packet Sent to Server");
soc.send(packet);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
// After Sending to Server. Go back to waiting to receive
receiveAckFromServer();
}
public void askInput(){
try{
//Prompt the User to Select what they would like to do the Packet
while(true){
help.print("Choose Option");
help.print("0: Do Nothing");
help.print("1: Invalid TFTP Opcode");
help.print("2: Invalid Filename");
help.print("3: Invalid Mode ");
help.print("4: Invalid Delimeter btw Filename and Mode");
help.print("5: Invalid Delimeter after Mode");
help.print("6: Port Error");
//Get the user input and convert to integer
userInput = Integer.parseInt(sc.nextLine());
System.out.println("Input Selected: "+ userInput);
if(userInput >= 0 && userInput <= 8){ break;}
help.print("Invalid Selection! Please Try Again");
}
}catch(Exception e){ e.printStackTrace(); }
sc.close();
//After asking for input. Call the method to wait to receive
passOnTFTP();
}
public void transfer(){
for(;;){
fileTransferReceieved();
sendPacket(sendReceiveSocket, clientReceivePacket, address, clientReceivePacket.getData(), serverPort);
}
}
public static void main( String args[] )
{
ErrorSimulator s = new ErrorSimulator();
//call the askInput method first to ask the user for what to do
s.askInput();
s.transfer();
}
}