-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmytftpclient.c
479 lines (407 loc) · 14.4 KB
/
mytftpclient.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
/* TFTP opcodes */
#define RRQ 1
#define WRQ 2
#define DATA 3
#define ACK 4
#define ERROR 5
#define OACK 6
#define OACK_ERROR 8
typedef struct Command{
int operation;
char *file;
int timeout;
int block_size;
char *mode;
char *address;
char *port_num;
} Command;
int get_mtu()
{
/* Get MTU for Blocksize option*/
struct ifconf ifc;
struct ifreq *ifr;
int sock;
sock = socket(AF_INET, SOCK_DGRAM, 0); // Create new socket
if (sock == -1) {
fprintf(stderr, "error: opening stream socket");
return -1;
}
char buf[1024];
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if(ioctl(sock, SIOCGIFCONF, &ifc) < 0)
return -1;
ifr = ifc.ifc_req;
int len = ifc.ifc_len / sizeof(struct ifreq);
struct ifreq *tmp = &ifr[0];
ioctl(sock, SIOCGIFMTU, tmp);
int min_mtu = tmp->ifr_mtu;
for(int i = 1; i < len; i++) {
tmp = &ifr[i];
ioctl(sock, SIOCGIFMTU, tmp);
if(tmp->ifr_mtu < min_mtu)
min_mtu = tmp->ifr_mtu;
}
// size of an Ethernet MTU minus the headers of TFTP (4 bytes), UDP (8 bytes) and IP (20 bytes).
return min_mtu - 32;
}
int set_blksize(int client_blksize)
{
/* Set data blocksize for transfer */
int limit = get_mtu();
if(client_blksize > limit) {
fprintf(stdout, "Block size is limited to range [8 B - %d B].\nMaximum transfer block size is set to %d.\n", limit, limit);
return limit;
}
return client_blksize;
}
void print_time()
{
char time_buff[50];
time_t rawtime;
time(&rawtime);
struct tm *timeinfo;
timeinfo = localtime(&rawtime);
strftime(time_buff, 50, "%Y-%m-%d %X", timeinfo);
struct timeval time;
gettimeofday(&time, NULL); // Get current time
/* Print timestamp */
printf("[%s.%d]", time_buff, (int)(time.tv_usec / 1000));
}
bool tftp_parse_command(char *line, Command *command)
{
bool RW_opt = false;
bool d_opt = false;
bool s_opt = false;
bool t_opt = false;
char *token;
token = strtok(line, " ");
while(token) {
if(strcmp(token, "-R")==0) {
command->operation = RRQ;
RW_opt = true;
} else if(strcmp(token, "-W")==0) {
command->operation = WRQ;
RW_opt = true;
} else if(strcmp(token, "-t")==0) {
token = strtok(NULL, " ");
if(token) {
char *ptr;
command->timeout = (int)strtol(token, &ptr, 10);
if (command->timeout == 0 || !strcmp(ptr, "")==0)
return false;
} else
return false;
t_opt = true;
} else if(strcmp(token, "-a")==0) {
token = strtok(NULL, " "); // "address,port"
command->address = strsep(&token, ",");
command->port_num = strsep(&token, ",");
if(!command->address || !command->port_num)
return false;
} else if(strcmp(token, "-d")==0) {
d_opt = true;
command->file = strtok(NULL, " ");
if(! command->file)
return false;
} else if(strcmp(token, "-c")==0) {
char *tmp = strtok(NULL, " ");
if(strcmp(tmp, "ascii")==0 || strcmp(tmp, "netascii")==0)
command->mode = "netascii";
else if(strcmp(tmp, "binary")==0 || strcmp(tmp, "octet")==0)
command->mode = "octet";
else
return false;
} else if(strcmp(token, "-s")==0) {
char *bsptr;
bsptr = strtok(NULL, " ");
if(bsptr) {
command->block_size = (int)strtol(bsptr, &bsptr, 10);
if (command->block_size == 0 || !strcmp(bsptr, "")==0){
return false;
}
s_opt = true;
} else {
return false;
}
} else { // Unknown parameter
return false;
}
token = strtok(NULL, " ");
}
if(!RW_opt || !d_opt) // Required options
return false;
if(!s_opt) {
command->block_size = 512; // Default
} else {
if(command->block_size <= 0)
return false;
command->block_size = set_blksize(command->block_size);
}
if(!t_opt) {
command->timeout = 0;
} else {
printf("%d ", command->timeout);
if(command->timeout < 1 || command->timeout > 255)
return false;
}
return true;
}
void tftp_process_command(Command *command)
{
int sock;
struct addrinfo hints, *server;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
hints.ai_socktype = SOCK_DGRAM; //UDP
if(getaddrinfo(command->address, command->port_num, &hints, &server) != 0) {
fprintf(stderr, "error: getaddrinfo()\n");
return;
}
// Create socket
sock = socket(server->ai_family, server->ai_socktype, server->ai_protocol);
FILE *fp;
int bufsize = command->block_size + 100;
char buf[bufsize];
unsigned int count;
if(command->operation == RRQ) { // Read file from the server
char *filename = strrchr(command->file, '/'); // Get filename from the path
if(!filename)
filename = command->file;
else
filename = filename + 1;
/* Opcode(2 bytes) | Filename | 0 | Mode | 0 | tsize | 0 | size | 0 | [blksize] | 0 | [size] | [timout] | [#secs]0*/
char *p;
*(int *)buf = htons(RRQ);
p = buf + 2;
strcpy(p, command->file);
p += strlen(command->file) + 1;
strcpy(p, command->mode);
p += strlen(command->mode) + 1;
strcpy(p, "tsize");
p += 6;
strcpy(p, "0");
p += 2;
if(command->block_size != 512) { // Include blksize option
strcpy(p, "blksize");
p += 8;
char blksize[10];
sprintf(blksize, "%d", command->block_size);
strcpy(p, blksize);
p += strlen(blksize) + 1;
}
if(command->timeout != 0) { // Include timout option
strcpy(p, "timeout");
p += 8;
char timeout[10];
sprintf(timeout, "%d", command->timeout);
strcpy(p, timeout);
p += strlen(timeout) + 1;
}
sendto(sock, buf, p-buf, 0, server->ai_addr, server->ai_addrlen); // Send read request
print_time();
fprintf(stdout, " Requesting READ from server %s:%s\n", command->address, command->port_num);
char tsize[100];
count = recvfrom(sock, buf, bufsize, 0, server->ai_addr, &(server->ai_addrlen)); // Receive response
if(buf[1] == OACK) { //acknowledge of RRQ and the options
if((fp = fopen(filename, "w")) == NULL) {
fprintf(stderr, "error: opening a file");
return;
}
tsize[0] = '\0';
int i;
for(i=8; i<count-1; i++) {
int tmp = (int)(buf[i]);
if(tmp == 0) {
break;
}
sprintf(tsize+strlen(tsize), "%d", tmp-48);
}
if(command->block_size != 512) {
if(strcmp(buf+i+1, "blksize")!=0) { // blksize option is rejected - it's omitted from OACK packet
fprintf(stdout, "Block size is omitted by server. Maximum transfer block size is set by default to 512.\n");
command->block_size = 512;
bufsize = 600;
}
}
count = sprintf(buf, "%c%c%c%c", 0, ACK, 0, 0);
sendto(sock, buf, count, 0, server->ai_addr, server->ai_addrlen); // Send ACK - response to an OACK
} else if(buf[1] == DATA) { // acknowledge of RRQ but not the options
if((fp = fopen(filename, "w")) == NULL) {
fprintf(stderr, "error: opening a file");
return;
}
strcpy(tsize, "total");
print_time();
fprintf(stdout, " Receiving DATA ... %d B of %s B\n", count-4, tsize);
fwrite(buf+4, sizeof(char), count-4, fp);
} else { // RRQ was rejected -> error
fprintf(stderr, "tftp: %s\n", buf+4);
return;
}
bufsize = command->block_size + 100;
char data_buf[bufsize];
unsigned int blocknum = 1, data_sum = 0;
while(1) {
count = recvfrom(sock, data_buf, bufsize, 0, server->ai_addr, &(server->ai_addrlen)); // Receive data
if(data_buf[1] == DATA) {
data_sum += count-4;
print_time();
fprintf(stdout, " Receiving DATA ... %d B of %s B\n", data_sum, tsize);
fwrite(data_buf+4, sizeof(char), count-4, fp);
blocknum++;
data_buf[1] = ACK;
sendto(sock, data_buf, 4, 0, server->ai_addr, server->ai_addrlen); // Send ACK
if(count < command->block_size+4) {
print_time();
fprintf(stdout, " Transfer succesfully completed.\n");
break;
}
} else if(data_buf[1] == OACK_ERROR) {
fprintf(stderr, "tftp: %s\n", data_buf+4);
break;
}
}
}
if(command->operation == WRQ) { //Write file to a server
if((fp = fopen(command->file, "r")) == NULL) {
fprintf(stderr, "error: opening a file");
return;
}
char *filename = strrchr(command->file, '/'); // Get filename from the path
if(!filename)
filename = command->file;
else
filename = filename + 1;
char *p;
*(int *)buf = htons(WRQ);
p = buf + 2;
strcpy(p, filename);
p += strlen(filename) + 1;
strcpy(p, command->mode);
p += strlen(command->mode) + 1;
strcpy(p, "tsize");
p += 6;
// Find file size
fseek(fp, 0L, SEEK_END);
char fsize[30];
long tsize = ftell(fp);
sprintf(fsize , "%ld", tsize);
fseek(fp, 0, SEEK_SET);
strcpy(p, fsize);
p += strlen(fsize) + 1;
if(command->block_size != 512) { // Include blksize option
strcpy(p, "blksize");
p += 8;
char blksize[10];
sprintf(blksize, "%d", command->block_size);
strcpy(p, blksize);
p += strlen(blksize) + 1;
}
if(command->timeout != 0) { // Include timout option
strcpy(p, "timeout");
p += 8;
char timeout[10];
sprintf(timeout, "%d", command->timeout);
strcpy(p, timeout);
p += strlen(timeout) + 1;
}
sendto(sock, buf, p-buf, 0, server->ai_addr, server->ai_addrlen); // Send write request
print_time();
fprintf(stdout, " Requesting WRITE from server %s:%s\n", command->address, command->port_num);
count = recvfrom(sock, buf, bufsize, 0, server->ai_addr, &(server->ai_addrlen)); // Receive response (OACK expected)
if(buf[1] == OACK) { //acknowledge of RRQ and the options
int i;
for(i=8; i<count-1; i++) {
int tmp = (int)(buf[i]);
if(tmp == 0) {
break;
}
}
if(command->block_size != 512) {
if(strcmp(buf+i+1, "blksize")!=0) { // blksize option is rejected - it's omitted from OACK packet
fprintf(stdout, "Block size is omitted by server. Maximum transfer block size is set by default to 512.\n");
command->block_size = 512;
bufsize = 600;
}
}
} else if(buf[1] == OACK_ERROR) {
fprintf(stderr, "tftp: %s\n", buf+4);
}
unsigned int blocknum = 1, bn_first_byte = 0, data_sum = 0;
char data_buf[bufsize];
char message[bufsize];
int bufsize = command->block_size + 100;
while(1) {
memset(data_buf, 0, sizeof(data_buf));
if(blocknum != 1) {
recvfrom(sock, message, bufsize, 0, server->ai_addr, &(server->ai_addrlen)); // ACK or ERROR
if(buf[1] == ERROR) {
fprintf(stderr, "tftp: %s", message+4);
break;
}
}
size_t nmemb = fread(data_buf, 1, command->block_size, fp);
data_sum += nmemb;
if(blocknum > 255)
bn_first_byte = (int)blocknum / 256;
count = sprintf(message, "%c%c%c%c", 0, DATA, bn_first_byte, blocknum);
memcpy(message+count, data_buf, nmemb);
sendto(sock, message, count+nmemb, 0, server->ai_addr, server->ai_addrlen); // Send data
print_time();
fprintf(stdout, " Sending DATA ... %d B of %ld B\n", data_sum, tsize);
if(nmemb < command->block_size) { // Last block of data
print_time();
fprintf(stdout, " Transfer succesfully completed.\n");
break;
}
blocknum++;
}
fclose(fp);
}
}
void tftp_client()
{
Command *command = (Command *) malloc(sizeof(Command)); // Alocate space for command
// Default values
command->address = "127.0.0.1";
command->port_num = "69";
command->mode = "octet";
char *line = NULL;
size_t n = 256;
while(1) {
printf(">");
if(getline(&line, &n, stdin) == -1)
break;
if(strcmp(line, "\n") == 0)
break;
int len = strlen(line);
line[len-1] = '\0';
if(!tftp_parse_command(line, command)) {
fprintf(stderr, "Invalid command.\nUsage: -R/W -d adresar/soubor -t timeout -s velikost -a adresa,port -c mód\n");
} else {
tftp_process_command(command);
printf("\n");
}
line = NULL;
}
free(command);
}
int main(int argc, char *argv[])
{
tftp_client();
return 0;
}