-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtftp_client.cpp
More file actions
345 lines (259 loc) · 7.07 KB
/
tftp_client.cpp
File metadata and controls
345 lines (259 loc) · 7.07 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
#include "tftp_packet.h"
#include "tftp_client.h"
using namespace std;
TFTPClient::TFTPClient(char* ip, int port) : m_net(port,"192.168.0.5",ip)
{
TFTP_Packet packet;
server_ip = ip;
server_port = port;
socket_descriptor = -1;
}
int TFTPClient::connectToServer()
{
cout << "Connecting to " << server_ip << " on port " << server_port << endl;
if(m_net.net_com_connect() == true)
{
return 1;
}
else
{
return 0;
}
}
int TFTPClient::sendBuffer(char *buffer)
{
return send(socket_descriptor, buffer, (int)strlen(buffer), 0);
}
int TFTPClient::sendPacket(TFTP_Packet* packet)
{
m_net.net_com_sendto((char*)packet->getData(), packet->getSize());
return 1;
}
bool TFTPClient::getFile(char* filename, char* destination)
{
TFTP_Packet packet_rrq, packet_ack;
ofstream file((const char*)destination, ofstream::binary);
char buffer[TFTP_PACKET_DATA_SIZE];
packet_rrq.createRRQ(filename);
sendPacket(&packet_rrq);
int last_packet_no = 1;
int wait_status;
int timeout_count = 0;
while (true)
{
wait_status = waitForPacketData(last_packet_no, TFTP_CLIENT_SERVER_TIMEOUT);
if (wait_status == TFTP_CLIENT_ERROR_PACKET_UNEXPECTED)
{
received_packet.dumpData();
file.close();
return false;
}
else if (wait_status == TFTP_CLIENT_ERROR_TIMEOUT)
{
//- nebesulaukem paketo
timeout_count++;
if (timeout_count < 2)
{ //- kadangi tai pirmas timeout`as, tai bandom pasiusti paskutini ACK
sendPacket(&packet_ack);
continue;
}
else
{
cout << "Connection timeout" << endl;
file.close();
return false;
}
}
if(last_packet_no != received_packet.getNumber())
{
//- paketas kazkur paklydo!
/* TFTP recognizes only one error condition that does not cause
termination, the source port of a received packet being incorrect.
In this case, an error packet is sent to the originating host. */
/* Taip negali nutikti, nes pas mus naudojamas ACK Lock`as */
cout << "This should not happen!" << endl; //- aisku kada kadanors tai vis tiek atsitiks :)
}
else
{
//- paketas tvarkoj
received_packet.dumpData();
last_packet_no++;
//- jei tai susitvarkes timeoutinis paketas, tai pasidziaukim ir leiskim tai pakartot
if(timeout_count == 1)
{
timeout_count = 0;
}
if(received_packet.copyData(4, buffer, TFTP_PACKET_DATA_SIZE))
{
file.write(buffer, received_packet.getSize() - 4);
//- tirkinam, ar gauti duomenis yra mazesni nei buferio dydis
//- jei taip, tai sis paketas buvo paskutinis
//- A data packet of less than 512 bytes signals termination of a transfer.
if (received_packet.getSize() - 4 < TFTP_PACKET_DATA_SIZE)
{
/* The host acknowledging the final DATA packet may terminate its side
of the connection on sending the final ACK. */
packet_ack.createACK((last_packet_no - 1));
if (sendPacket(&packet_ack))
{
break;
}
}
else
{
//- ne paskutinis, tai siunciam ACK
//- Each data packet contains one block of data, and must be acknowledged by
//- an acknowledgment packet before the next packet can be sent.
packet_ack.createACK((last_packet_no - 1)); //- siunciam toki paketo numeri, kuri gavom paskutini
sendPacket(&packet_ack);
DEBUGMSG("Acknoledgement sent");
}
}
}
}
file.close();
return true;
}
int TFTPClient::waitForPacket(TFTP_Packet* packet, int timeout_ms)
{
packet->clear();
fd_set fd_reader; // soketu masyvo struktura
timeval connection_timer; // laiko struktura perduodama select()
connection_timer.tv_sec = timeout_ms / 100; // s
connection_timer.tv_usec = 0; // neveikia o.0 timeout_ms; // ms
FD_ZERO(&fd_reader);
// laukiam, kol bus ka nuskaityti
FD_SET(socket_descriptor, &fd_reader);
//int select_ready = select(socket_descriptor + 1, &fd_reader, NULL, NULL, &connection_timer);
cout << "select" << endl;
//- turim sekminga event`a
int receive_status;
receive_status = m_net.net_com_receive((char*)packet->getData(),TFTP_PACKET_MAX_SIZE);
cout << "receive_status: " << receive_status << endl;
if (receive_status == 0)
{
cout << "Connection was closed by server\n";
return TFTP_CLIENT_ERROR_CONNECTION_CLOSED;
}
if (receive_status == SOCKET_ERROR)
{
DEBUGMSG("recv() error in waitForPackage()");
return TFTP_CLIENT_ERROR_RECEIVE;
}
packet->setSize(receive_status);
return TFTP_CLIENT_ERROR_NO_ERROR;
}
bool TFTPClient::waitForPacketACK(int packet_number, int timeout_ms)
{
TFTP_Packet received_packet;
if (waitForPacket(&received_packet, timeout_ms))
{
if (received_packet.isError())
{
cout << "ACK expected, but got Error" << endl;
errorReceived(&received_packet);
return false;
}
if (received_packet.isACK())
{
cout << "ACK for packet " << received_packet.getNumber() << "(expected: " << packet_number << ")" << endl;
return true;
}
if (received_packet.isData())
{
cout << "DATAK for packet " << received_packet.getNumber() << "(expected: " << packet_number << ")" << endl;
return true;
}
cout << "Unhandled packet" << endl;
}
else
{
DEBUGMSG("We have an error in waitForPacket()");
}
return true;
}
int TFTPClient::waitForPacketData(int packet_number, int timeout_ms)
{
int wait_status = waitForPacket(&received_packet, timeout_ms);
if (wait_status == TFTP_CLIENT_ERROR_NO_ERROR)
{
if (received_packet.isError())
{
errorReceived(&received_packet);
return TFTP_CLIENT_ERROR_PACKET_UNEXPECTED;
}
if (received_packet.isData())
{
return TFTP_CLIENT_ERROR_NO_ERROR;
}
}
return wait_status;
}
bool TFTPClient::sendFile(char* filename, char* destination)
{
TFTP_Packet packet_wrq, packet_data;
ifstream file(filename, ifstream::binary);
char memblock[TFTP_PACKET_DATA_SIZE];
if (!file.is_open() || !file.good())
{
cout << "Unable to open file " << filename << endl;
return false;
}
packet_wrq.createWRQ(destination);
packet_wrq.dumpData();
sendPacket(&packet_wrq);
int last_packet_no = 0;
// int last_error = 0;
while (true)
{
if (waitForPacketACK(last_packet_no++, TFTP_CLIENT_SERVER_TIMEOUT))
{
cout << "send file" << endl;
file.read(memblock, TFTP_PACKET_DATA_SIZE);
packet_data.createData(last_packet_no, (char*)memblock, file.gcount());
sendPacket(&packet_data);
if (file.eof())
{
break;
}
}
else
{
cout << "Server has timed out" << endl;
file.close();
return false;
}
}
file.close();
return true;
}
void TFTPClient::errorReceived(TFTP_Packet* packet)
{
int error_code = packet->getWord(2);
cout << "Error! Error code: " << error_code << endl;
cout << "Error message: ";
switch (error_code)
{
case 1: cout << TFTP_ERROR_1; break;
case 2: cout << TFTP_ERROR_2; break;
case 3: cout << TFTP_ERROR_3; break;
case 4: cout << TFTP_ERROR_4; break;
case 5: cout << TFTP_ERROR_5; break;
case 6: cout << TFTP_ERROR_6; break;
case 7: cout << TFTP_ERROR_7; break;
case 0:
default: cout << TFTP_ERROR_0; break;
}
cout << endl;
this->~TFTPClient();
}
TFTPClient::~TFTPClient()
{
m_net.net_com_close();
}
void DEBUGMSG(char *msg)
{
#ifdef DEBUG
std::cout << msg << "\n";
#endif
}