-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTFTPClient.java
More file actions
289 lines (240 loc) · 11.6 KB
/
TFTPClient.java
File metadata and controls
289 lines (240 loc) · 11.6 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
import java.net.*;
import java.io.*;
import java.nio.file.*;
import java.util.Scanner;
class TFTPClient {
private static final int TFTP_DEFAULT_PORT = 1069;
private static final int BUFFER_SIZE = 516;
private static final int DATA_SIZE = 512;
private static final int MAX_RETRIES = 5;
private static final int TIMEOUT_MS = 10000;
private static final String DEFAULT_DIRECTORY = "client_files";
private final InetAddress serverAddress; // Storing IP address of TFTP server
private final Path clientDirectory;
public TFTPClient(String serverHost) throws UnknownHostException {
this.serverAddress = InetAddress.getByName(serverHost); // Gets the server IP address
this.clientDirectory = Paths.get(DEFAULT_DIRECTORY);
initializeDirectory();
}
private void initializeDirectory() { // Function to create 'client_files'
try {
Files.createDirectories(clientDirectory);
} catch (IOException e) {
System.err.println("Failed to create client directory: " + e.getMessage());
throw new RuntimeException("Failed to initialize client", e);
}
}
public boolean authenticate(String username, String password) throws IOException {
try (DatagramSocket socket = new DatagramSocket()) {
socket.setSoTimeout(TIMEOUT_MS);
// Create authentication packet
byte[] authData = createAuthPacket(username, password);
DatagramPacket authPacket = new DatagramPacket(authData, authData.length, serverAddress, TFTP_DEFAULT_PORT);
sendWithRetry(socket, authPacket);
// Wait for authentication response
byte[] buffer = new byte[BUFFER_SIZE];
DatagramPacket responsePacket = new DatagramPacket(buffer, buffer.length);
socket.receive(responsePacket);
// Check response
String response = new String(responsePacket.getData(), 0, responsePacket.getLength()).trim();
return "AUTH_SUCCESS".equals(response); // Fix: Added trim() to handle any extra spaces
} catch (SocketTimeoutException e) {
System.err.println("Authentication timeout.");
return false;
}
}
private byte[] createAuthPacket(String username, String password) {
String authMessage = "AUTH:" + username + ":" + password;
byte[] authData = new byte[2 + authMessage.length()];
authData[0] = 0; // TFTP opcode
authData[1] = 5; // AUTH opcode
System.arraycopy(authMessage.getBytes(), 0, authData, 2, authMessage.length());
// Debugging: Output the constructed authentication message
System.out.println("Constructed Auth Packet: " + authMessage);
return authData;
}
public void downloadFile(String filename) throws IOException { // RRQ
try (DatagramSocket socket = new DatagramSocket()) {
socket.setSoTimeout(TIMEOUT_MS);
byte[] rrqData = createRequestPacket(OpCode.RRQ, filename, "octet");
DatagramPacket rrqPacket = new DatagramPacket(rrqData, rrqData.length, serverAddress, TFTP_DEFAULT_PORT);
sendWithRetry(socket, rrqPacket);
Path outputFile = clientDirectory.resolve(filename);
try (OutputStream fos = new FileOutputStream(outputFile.toFile())) {
int blockNumber = 1;
do {
DataPacketResult result = receiveDataPacket(socket, blockNumber);
if (!result.success) break;
if (result.data.length > 0) {
fos.write(result.data);
System.out.println("Writing " + result.data.length + " bytes to file.");
} else {
System.out.println("Received empty data block.");
}
sendAck(socket, blockNumber, result.address, result.port);
if (result.lastPacket) {
System.out.println("Last packet received, closing file.");
break;
}
blockNumber++;
} while (true);
}
}
}
public void uploadFile(String filename) throws IOException {
try (DatagramSocket socket = new DatagramSocket()) {
socket.setSoTimeout(TIMEOUT_MS);
byte[] wrqData = createRequestPacket(OpCode.WRQ, filename, "octet");
DatagramPacket wrqPacket = new DatagramPacket(wrqData, wrqData.length, serverAddress, TFTP_DEFAULT_PORT);
sendWithRetry(socket, wrqPacket);
Path inputFile = clientDirectory.resolve(filename);
byte[] fileData = Files.readAllBytes(inputFile);
int blockNumber = 1;
int offset = 0;
while (offset < fileData.length) {
int bytesToSend = Math.min(DATA_SIZE, fileData.length - offset);
byte[] dataBlock = new byte[bytesToSend + 4];
dataBlock[0] = 0;
dataBlock[1] = OpCode.DATA.getValue();
dataBlock[2] = (byte) (blockNumber >> 8);
dataBlock[3] = (byte) (blockNumber & 0xFF);
System.arraycopy(fileData, offset, dataBlock, 4, bytesToSend);
DatagramPacket dataPacket = new DatagramPacket(dataBlock, dataBlock.length, serverAddress, TFTP_DEFAULT_PORT);
sendWithRetry(socket, dataPacket);
offset += bytesToSend;
blockNumber++;
}
System.out.println("File uploaded successfully.");
}
}
private byte[] createRequestPacket(OpCode opCode, String filename, String mode) {
byte[] filenameBytes = filename.getBytes();
byte[] modeBytes = mode.getBytes();
byte[] packet = new byte[2 + filenameBytes.length + 1 + modeBytes.length + 1];
packet[0] = 0;
packet[1] = opCode.getValue();
System.arraycopy(filenameBytes, 0, packet, 2, filenameBytes.length);
packet[2 + filenameBytes.length] = 0;
System.arraycopy(modeBytes, 0, packet, 3 + filenameBytes.length, modeBytes.length);
packet[3 + filenameBytes.length + modeBytes.length] = 0;
return packet;
}
private void sendWithRetry(DatagramSocket socket, DatagramPacket packet) throws IOException {
int attempts = 0;
while (attempts < MAX_RETRIES) {
try {
socket.send(packet);
System.out.println("Sent request packet, waiting for response...");
return;
} catch (IOException e) {
System.err.println("Retrying... Attempt " + (attempts + 1));
attempts++;
}
}
throw new IOException("Failed to send packet after " + MAX_RETRIES + " attempts");
}
private DataPacketResult receiveDataPacket(DatagramSocket socket, int expectedBlock) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
int attempts = 0;
while (attempts < MAX_RETRIES) {
try {
socket.receive(packet);
byte[] receivedData = packet.getData();
int blockNumber = getBlockNumber(receivedData);
// Check if the block number matches the expected one
if (blockNumber != expectedBlock) {
System.err.println("Expected block " + expectedBlock + ", but received block " + blockNumber);
continue; // Skip this packet and try again
}
int dataLength = packet.getLength() - 4; // Excluding the 4-byte header
byte[] data = new byte[dataLength];
System.arraycopy(receivedData, 4, data, 0, dataLength);
System.out.println("Received block " + blockNumber + " with " + dataLength + " bytes.");
// Return a DataPacketResult containing data and other information
return new DataPacketResult(true, data, dataLength < DATA_SIZE, packet.getAddress(), packet.getPort());
} catch (SocketTimeoutException e) {
System.err.println("Timeout waiting for data packet. Retrying...");
attempts++;
if (attempts >= MAX_RETRIES) {
System.err.println("Max retries reached. Aborting.");
return new DataPacketResult(false, new byte[0], false, null, 0);
}
}
}
return new DataPacketResult(false, new byte[0], false, null, 0); // Timeout reached or max retries exceeded
}
private void sendAck(DatagramSocket socket, int blockNumber, InetAddress address, int port) throws IOException {
byte[] ackData = new byte[4];
ackData[0] = 0;
ackData[1] = OpCode.ACK.getValue();
ackData[2] = (byte) (blockNumber >> 8);
ackData[3] = (byte) (blockNumber & 0xFF);
DatagramPacket ackPacket = new DatagramPacket(ackData, ackData.length, address, port);
socket.send(ackPacket);
System.out.println("Sent ACK for block " + blockNumber);
}
private static int getBlockNumber(byte[] data) {
return ((data[2] & 0xFF) << 8) | (data[3] & 0xFF);
}
private static class DataPacketResult {
final boolean success;
final byte[] data;
final boolean lastPacket;
final InetAddress address;
final int port;
DataPacketResult(boolean success, byte[] data, boolean lastPacket, InetAddress address, int port) {
this.success = success;
this.data = data;
this.lastPacket = lastPacket;
this.address = address;
this.port = port;
}
}
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("Usage: java TFTPClient <server> <get/put> <filename>");
return;
}
try {
TFTPClient client = new TFTPClient(args[0]);
// Prompt for username and password
Scanner scanner = new Scanner(System.in);
System.out.print("Enter username: ");
String username = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();
// Authenticate
if (!client.authenticate(username, password)) {
System.out.println("Authentication failed. Exiting.");
return;
}
String filename = args[2];
if ("get".equalsIgnoreCase(args[1])) {
client.downloadFile(filename);
System.out.println("File downloaded successfully: " + filename);
} else if ("put".equalsIgnoreCase(args[1])) {
client.uploadFile(filename);
System.out.println("File uploaded successfully: " + filename);
} else {
System.out.println("Invalid operation. Use 'get' or 'put'.");
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
enum OpCode {
RRQ((byte) 1),
WRQ((byte) 2),
DATA((byte) 3),
ACK((byte) 4);
private final byte value;
OpCode(byte value) {
this.value = value;
}
public byte getValue() {
return value;
}
}