-
Notifications
You must be signed in to change notification settings - Fork 4
/
tftp.c
1196 lines (1108 loc) · 34 KB
/
tftp.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* hey emacs! -*- Mode: C; c-file-style: "k&r"; indent-tabs-mode: nil -*- */
/*
* tftp.c
* main client file.
*
* $Id: tftp.c,v 1.47 2004/03/15 23:55:56 jp Exp $
*
* Copyright (c) 2000 Jean-Pierre Lefebvre <[email protected]>
* and Remi Lefebvre <[email protected]>
*
* atftp is free software; you can redistribute them and/or modify them
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#if HAVE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
#if HAVE_ARGZ
#include <argz.h>
#else
#include "argz.h"
#endif
#include "tftp.h"
#include "tftp_io.h"
#include "tftp_def.h"
#include "logger.h"
#include "options.h"
/* Maximum number of args on a line. 20 should be more than enough. */
#define MAXARG 20
/* for readline */
#define HISTORY_FILE ".atftp_history"
/* defined as extern in tftp_file.c and mtftp_file.c, set by the signal
handler */
int tftp_cancel = 0;
/* local flags */
int interactive = 1; /* if false, we run in batch mode */
int tftp_result = OK; /* status of tftp_send_file or
tftp_receive_file, used for status() */
/* Structure to hold some information that must be passed to
* functions.
*/
struct client_data data;
/* tftp.c local only functions. */
static void signal_handler(int signal);
int read_cmd(void);
#if HAVE_READLINE
# if (HAVE_RL_COMPLETION_MATCHES | HAVE_COMPLETION_MATCHES)
int getc_func(FILE *fp);
char **completion(const char *text, int start, int end);
char *command_generator(const char *text, int state);
# endif
#endif
void make_arg(char *string, int *argc, char ***argv);
int process_cmd(int argc, char **argv);
int tftp_cmd_line_options(int argc, char **argv);
void tftp_usage(void);
/* Functions associated with the tftp commands. */
int set_peer(int argc, char **argv);
int set_mode(int argc, char **argv);
int set_option(int argc, char **argv);
int put_file(int argc, char **argv);
int get_file(int argc, char **argv);
#ifdef HAVE_MTFTP
int mtftp_opt(int argc, char **argv);
#endif
int quit(int argc, char **argv);
int set_verbose(int argc, char **argv);
int set_trace(int argc, char **argv);
int status(int argc, char **argv);
int set_timeout(int argc, char **argv);
int help(int argc, char **argv);
/* All supported commands. */
struct command {
const char *name;
int (*func)(int argc, char **argv);
const char *helpmsg;
} cmdtab[] = {
{"connect", set_peer, "connect to tftp server"},
{"mode", set_mode, "set file transfer mode (netascii/octet)"},
{"option", set_option, "set RFC1350 options"},
{"put", put_file, "upload a file to the host"},
{"get", get_file, "download a file from the host"},
#ifdef HAVE_MTFTP
{"mtftp", mtftp_opt, "set mtftp variables"},
{"mget", get_file, "download file from mtftp server"},
#endif
{"quit", quit, "exit tftp"},
{"verbose", set_verbose, "toggle verbose mode"},
{"trace", set_trace, "toggle trace mode"},
{"status", status, "print status information"},
{"timeout", set_timeout, "set the timeout before a retry"},
{"help", help, "print help message"},
{"?", help, "print help message"},
{NULL, NULL, NULL}
};
/*
* Open the socket, register signal handler and
* pass the control to read_cmd.
*/
int main(int argc, char **argv)
{
/* Allocate memory for data buffer. */
if ((data.data_buffer = malloc((size_t)SEGSIZE+4)) == NULL)
{
fprintf(stderr, "tftp: memory allcoation failed.\n");
exit(ERR);
}
data.data_buffer_size = SEGSIZE + 4;
/* Allocate memory for tftp option structure. */
if ((data.tftp_options =
malloc(sizeof(tftp_default_options))) == NULL)
{
fprintf(stderr, "tftp: memory allocation failed.\n");
exit(ERR);
}
/* Copy default options. */
memcpy(data.tftp_options, tftp_default_options,
sizeof(tftp_default_options));
/* Allocate memory for tftp option reply from server. */
if ((data.tftp_options_reply =
malloc(sizeof(tftp_default_options))) == NULL)
{
fprintf(stderr, "tftp: memory allocation failed.\n");
exit(ERR);
}
/* Copy default options. */
memcpy(data.tftp_options_reply, tftp_default_options,
sizeof(tftp_default_options));
/* default options */
#ifdef HAVE_MTFTP
data.mtftp_client_port = 76;
Strncpy(data.mtftp_mcast_ip, "0.0.0.0", MAXLEN);
data.mtftp_listen_delay = 2;
data.mtftp_timeout_delay = 2;
#endif
data.timeout = TIMEOUT;
data.checkport = 1;
data.trace = 0;
data.verbose = 0;
#ifdef DEBUG
data.delay = 0;
#endif
/* register SIGINT -> C-c */
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
/* parse options, and maybe run in non interractive mode */
tftp_cmd_line_options(argc, argv);
if (interactive)
return read_cmd();
return OK;
}
/*
* When we receive a signal, we set tftp_cancel in order to
* abort ongoing transfer.
*/
void signal_handler(int signal)
{
/*
* if receiving or sending files, we should abort
* and send and error ACK
*/
tftp_cancel = 1;
}
/*
* Read commands with a nice prompt and history (if compile with
* libreadline. Otherway we only get the basic.
*/
int read_cmd(void)
{
int run = 1;
#if HAVE_READLINE
char *string = NULL;
#else
char string[MAXLEN];
#endif
int argc;
char **argv = NULL;
#if HAVE_READLINE
char history[MAXLEN];
if (getenv("HOME") != NULL)
snprintf(history, sizeof(history), "%s/%s", getenv("HOME"), HISTORY_FILE);
else
snprintf(history, sizeof(history), "%s", HISTORY_FILE);
# if (HAVE_RL_COMPLETION_MATCHES | HAVE_COMPLETION_MATCHES)
rl_attempted_completion_function = completion;
rl_getc_function = getc_func;
# endif
#endif
#if HAVE_READLINE
using_history();
read_history(history);
#endif
while (run)
{
#if HAVE_READLINE
if ((string = readline("tftp> ")) == NULL)
{
fprintf(stderr, "\n");
break;
}
#else
fprintf(stderr, "tftp> ");
if (fgets(string, MAXLEN, stdin) == NULL)
{
fprintf(stderr, "\n");
break;
}
#endif
else
{
#ifndef HAVE_READLINE
string[strlen(string)-1] = 0;
#endif
if (strlen(string) != 0)
{
make_arg(string, &argc, &argv);
if (argc > 0)
{
#if HAVE_READLINE
add_history(string);
#endif
if (process_cmd(argc, argv) == QUIT)
run = 0;
}
#if HAVE_READLINE
free(string);
#endif
}
}
}
#if HAVE_READLINE
/* save history */
write_history(history);
#endif
return 0;
}
#if HAVE_READLINE
# if (HAVE_RL_COMPLETION_MATCHES | HAVE_COMPLETION_MATCHES)
int getc_func(FILE *fp)
{
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(fileno(fp), &rfds);
if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0)
{
rl_kill_full_line(0,0);
return '\n';
}
return rl_getc(fp);
}
char **completion(const char *text, int start, int end)
{
char **matches;
matches = (char **)NULL;
/* If this word is at the start of the line, then it is a command
to complete. Otherwise it is the name of a file in the current
directory. */
if (start == 0)
#if HAVE_RL_COMPLETION_MATCHES
matches = rl_completion_matches(text, command_generator);
#endif
#if HAVE_COMPLETION_MATCHES
matches = completion_matches(text, command_generator);
#endif
return (matches);
}
/* Generator function for command completion. STATE lets us
know whether to start from scratch; without any state
(i.e. STATE == 0), then we start at the top of the list. */
char *command_generator(const char *text, int state)
{
static int list_index, len;
char *name;
/* If this is a new word to complete, initialize now. This
includes saving the length of TEXT for efficiency, and
initializing the index variable to 0. */
if (!state)
{
list_index = 0;
len = strlen (text);
}
/* Return the next name which partially matches from the
command list. */
while ((name = (char *)cmdtab[list_index].name))
{
list_index++;
if (strncmp (name, text, len) == 0)
return strdup(name);
}
/* If no names matched, then return NULL. */
return NULL;
}
# endif
#endif
/*
* Split a string into args.
*/
void make_arg(char *string, int *argc, char ***argv)
{
static char *tmp = NULL;
size_t argz_len;
/* split the string to an argz vector */
if (argz_create_sep(string, ' ', &tmp, &argz_len) != 0)
{
*argc = 0;
return;
}
/* retreive the number of arguments */
*argc = argz_count(tmp, argz_len);
/* give enough space for all arguments to **argv */
if ((*argv = realloc(*argv, (*argc + 1) * sizeof(char *))) == NULL)
{
*argc = 0;
return;
}
/* extract arguments */
argz_extract(tmp, argz_len, *argv);
/* if the last argument is an empty string ... it happens
when some extra space are added at the end of string :( */
if (strlen((*argv)[*argc - 1]) == 0)
*argc = *argc - 1;
}
/*
* Once a line have been read and splitted, find the corresponding
* function and call it.
*/
int process_cmd(int argc, char **argv)
{
int i = 0;
/* find the command in the command table */
while (1)
{
if (cmdtab[i].name == NULL)
{
fprintf(stderr, "tftp: bad command name.\n");
return 0;
}
if (strcasecmp(cmdtab[i].name, argv[0]) == 0)
{
return (cmdtab[i].func)(argc, argv);
}
i++;
}
}
/*
* Update sa_peer structure, hostname and port number adequately
*/
int set_peer(int argc, char **argv)
{
struct hostent *host; /* for host name lookup */
struct servent *sp; /* server entry for tftp service */
/* sanity check */
if ((argc < 2) || (argc > 3))
{
fprintf(stderr, "Usage: %s host-name [port]\n", argv[0]);
return ERR;
}
/* get the server entry */
sp = getservbyname("tftp", "udp");
if (sp == 0) {
fprintf(stderr, "tftp: udp/tftp, unknown service.\n");
return ERR;
}
/* look up the host */
host = gethostbyname(argv[1]);
/* if valid, update s_inn structure */
if (host)
{
data.sa_peer.sin_family = host->h_addrtype;
if (host->h_length > sizeof(data.sa_peer.sin_addr))
host->h_length = sizeof(data.sa_peer.sin_addr);
memcpy(&data.sa_peer.sin_addr, host->h_addr, host->h_length);
Strncpy(data.hostname, host->h_name,
sizeof(data.hostname));
data.hostname[sizeof(data.hostname)-1] = 0;
data.sa_peer.sin_port = sp->s_port;
}
else
{
fprintf(stderr, "tftp: unknown host %s.\n", argv[1]);
data.connected = 0;
return ERR;
}
/* get the server port */
if (argc == 3)
{
sp->s_port = htons(atoi(argv[2]));
if (sp->s_port < 0)
{
fprintf(stderr, "%s: bad port number.\n", argv[2]);
data.connected = 0;
return ERR;
}
data.sa_peer.sin_port = sp->s_port;
}
/* copy port number to data structure */
data.port = ntohs(sp->s_port);
data.connected = 1;
return OK;
}
/*
* Set the mode to netascii or octet
*/
int set_mode(int argc, char **argv)
{
if (argc > 2)
{
fprintf(stderr, "Usage: %s [netascii] [octet]\n", argv[0]);
return ERR;
}
if (argc == 1)
{
fprintf(stderr, "Current mode is %s.\n",
data.tftp_options[OPT_MODE].value);
return OK;
}
if (strcasecmp("netascii", argv[1]) == 0)
Strncpy(data.tftp_options[OPT_MODE].value, "netascii",
VAL_SIZE);
else if (strcasecmp("octet", argv[1]) == 0)
Strncpy(data.tftp_options[OPT_MODE].value, "octet",
VAL_SIZE);
else
{
fprintf(stderr, "tftp: unkowned mode %s.\n", argv[1]);
fprintf(stderr, "Usage: %s [netascii] [octet]\n", argv[0]);
return ERR;
}
return OK;
}
/*
* Set an option
*/
int set_option(int argc, char **argv)
{
char value[VAL_SIZE];
/* if there are no arguments */
if ((argc < 2) || (argc > 3))
{
fprintf(stderr, "Usage: option <option name> [option value]\n");
fprintf(stderr, " option disable <option name>\n");
if (data.tftp_options[OPT_TSIZE].specified)
fprintf(stderr, " tsize: enabled\n");
else
fprintf(stderr, " tsize: disabled\n");
if (data.tftp_options[OPT_BLKSIZE].specified)
fprintf(stderr, " blksize: %s\n",
data.tftp_options[OPT_BLKSIZE].value);
else
fprintf(stderr, " blksize: disabled\n");
if (data.tftp_options[OPT_TIMEOUT].specified)
fprintf(stderr, " timeout: %s\n",
data.tftp_options[OPT_TIMEOUT].value);
else
fprintf(stderr, " timeout: disabled\n");
if (data.tftp_options[OPT_MULTICAST].specified)
fprintf(stderr, " multicast: enabled\n");
else
fprintf(stderr, " multicast: disabled\n");
return ERR;
}
/* if disabling an option */
if (strcasecmp("disable", argv[1]) == 0)
{
if (argc != 3)
{
fprintf(stderr, "Usage: option disable <option name>\n");
return ERR;
}
/* disable it */
if (opt_disable_options(data.tftp_options, argv[2]) == ERR)
{
fprintf(stderr, "no option named %s\n", argv[2]);
return ERR;
}
if (opt_get_options(data.tftp_options, argv[1], value) == ERR)
fprintf(stderr, "Option %s disabled\n", argv[2]);
else
fprintf(stderr, "Option %s = %s\n", argv[1], value);
return OK;
}
/* ok, we are setting an argument */
if (argc == 2)
{
if (opt_set_options(data.tftp_options, argv[1], NULL) == ERR)
{
fprintf(stderr, "no option named %s\n", argv[1]);
return ERR;
}
}
if (argc == 3)
{
if (opt_set_options(data.tftp_options, argv[1], argv[2]) == ERR)
{
fprintf(stderr, "no option named %s\n", argv[1]);
return ERR;
}
}
/* print the new value for that option */
if (opt_get_options(data.tftp_options, argv[1], value) == ERR)
fprintf(stderr, "Option %s disabled\n", argv[1]);
else
fprintf(stderr, "Option %s = %s\n", argv[1], value);
return OK;
}
/*
* Put a file to the server.
*/
int put_file(int argc, char **argv)
{
socklen_t len = sizeof(data.sa_local);
if ((argc < 2) || (argc > 3))
{
fprintf(stderr, "Usage: put local_file [remote_file]\n");
return ERR;
}
if (!data.connected)
{
fprintf(stderr, "Not connected.\n");
return ERR;
}
if (argc == 2)
{
Strncpy(data.local_file, argv[1], VAL_SIZE);
Strncpy(data.tftp_options[OPT_FILENAME].value, argv[1], VAL_SIZE);
}
else
{
Strncpy(data.local_file, argv[1], VAL_SIZE);
Strncpy(data.tftp_options[OPT_FILENAME].value, argv[2], VAL_SIZE);
}
/* open a UDP socket */
data.sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (data.sockfd < 0) {
perror("tftp: ");
exit(ERR);
}
memset(&data.sa_local, 0, sizeof(data.sa_local));
bind(data.sockfd, (struct sockaddr *)&data.sa_local,
sizeof(data.sa_local));
getsockname(data.sockfd, (struct sockaddr *)&data.sa_local, &len);
/* do the transfer */
gettimeofday(&data.start_time, NULL);
tftp_result = tftp_send_file(&data);
gettimeofday(&data.end_time, NULL);
/* close the socket */
fsync(data.sockfd);
close(data.sockfd);
return OK;
}
/*
* Receive a file from the server.
*/
int get_file(int argc, char **argv)
{
#if HAVE_READLINE
char *string;
#else
char string[MAXLEN];
#endif
#ifdef HAVE_MTFTP
int use_mtftp;
#endif
char *tmp;
FILE *fp;
socklen_t len = sizeof(data.sa_local);
#ifdef HAVE_MTFTP
if (strcmp(argv[0], "mget") == 0)
use_mtftp = 1;
else
use_mtftp = 0;
#endif
if ((argc < 2) || (argc > 3))
{
fprintf(stderr, "Usage: %s remote_file [local_file]\n", argv[0]);
return ERR;
}
if (!data.connected)
{
fprintf(stderr, "Not connected.\n");
return ERR;
}
if (argc == 2)
{
Strncpy(data.tftp_options[OPT_FILENAME].value,
argv[1], VAL_SIZE);
tmp = strrchr(argv[1], '/');
if (tmp < argv[1])
tmp = argv[1];
else
tmp++;
Strncpy(data.local_file, tmp, VAL_SIZE);
}
else
{
Strncpy(data.local_file, argv[2], VAL_SIZE);
Strncpy(data.tftp_options[OPT_FILENAME].value, argv[1], VAL_SIZE);
}
/* if interractive, verify if localfile exists */
if (interactive)
{
/* if localfile if stdout, nothing to verify */
if (strncmp(data.local_file, "/dev/stdout", VAL_SIZE) != 0)
{
if ((fp = fopen(data.local_file, "r")) != NULL)
{
fclose(fp);
#if HAVE_READLINE
string = readline("Overwite local file [y/n]? ");
#else
fprintf(stderr, "Overwite local file [y/n]? ");
fgets(string, MAXLEN, stdin);
string[strlen(string) - 1] = 0;
#endif
if (!(strcasecmp(string, "y") == 0))
{
#if HAVE_READLINE
free(string);
#endif
return OK;
}
#if HAVE_READLINE
free(string);
#endif
}
}
}
/* open a UDP socket */
data.sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (data.sockfd < 0) {
perror("tftp: ");
exit(ERR);
}
memset(&data.sa_local, 0, sizeof(data.sa_local));
bind(data.sockfd, (struct sockaddr *)&data.sa_local,
sizeof(data.sa_local));
getsockname(data.sockfd, (struct sockaddr *)&data.sa_local, &len);
/* do the transfer */
gettimeofday(&data.start_time, NULL);
#ifdef HAVE_MTFTP
if (use_mtftp)
tftp_result = tftp_mtftp_receive_file(&data);
else
#endif
tftp_result = tftp_receive_file(&data);
gettimeofday(&data.end_time, NULL);
/* close the socket */
fsync(data.sockfd);
close(data.sockfd);
return tftp_result;
}
#ifdef HAVE_MTFTP
/*
* Set ot get mtftp variable value
*/
int mtftp_opt(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "Usage: mtftp <option name> <option value>\n");
/* print current value of variables */
fprintf(stderr, " client-port: %d\n", data.mtftp_client_port);
fprintf(stderr, " mcast-ip: %s\n", data.mtftp_mcast_ip);
fprintf(stderr, " listen-delay: %d\n", data.mtftp_listen_delay);
fprintf(stderr, " timeout-delay: %d\n", data.mtftp_timeout_delay);
}
else
{
if (strcmp(argv[1], "client-port") == 0)
{
data.mtftp_client_port = atoi(argv[2]);
fprintf(stderr, "mtftp client-port = %d\n",
data.mtftp_client_port);
}
else if (strcmp(argv[1], "mcast-ip") == 0)
{
Strncpy(data.mtftp_mcast_ip, argv[2], MAXLEN);
fprintf(stderr, "mtftp mcast-ip = %s\n", data.mtftp_mcast_ip);
}
else if (strcmp(argv[1], "listen-delay") == 0)
{
data.mtftp_listen_delay = atoi(argv[2]);
fprintf(stderr, "mtftp listen-delay = %d\n",
data.mtftp_listen_delay);
}
else if (strcmp(argv[1], "timeout-delay") == 0)
{
data.mtftp_timeout_delay = atoi(argv[2]);
fprintf(stderr, "mtftp timeout-delay = %d\n",
data.mtftp_timeout_delay);
}
else
{
fprintf(stderr, "no mtftp variable named %s\n", argv[1]);
return ERR;
}
}
return OK;
}
#endif
/*
* Exit tftp
*/
int quit(int argc, char **argv)
{
return QUIT;
}
int set_verbose(int argc, char **argv)
{
if (data.verbose)
{
data.verbose = 0;
fprintf(stderr, "Verbose mode off.\n");
}
else
{
data.verbose = 1;
fprintf(stderr, "Verbose mode on.\n");
}
return OK;
}
int set_trace(int argc, char **argv)
{
if (data.trace)
{
data.trace = 0;
fprintf(stderr, "Trace mode off.\n");
}
else
{
data.trace = 1;
fprintf(stderr, "Trace mode on.\n");
}
return OK;
}
int status(int argc, char **argv)
{
struct timeval tmp;
#if HAVE_READLINE
HIST_ENTRY *history = history_get(history_length-1);
#endif
char string[MAXLEN];
timeval_diff(&tmp, &data.end_time, &data.start_time);
if (!data.connected)
fprintf(stderr, "Not connected\n");
else
fprintf(stderr, "Connected: %s port %d\n", data.hostname,
data.port);
fprintf(stderr, "Mode: %s\n", data.tftp_options[OPT_MODE].value);
if (data.verbose)
fprintf(stderr, "Verbose: on\n");
else
fprintf(stderr, "Verbose: off\n");
if (data.trace)
fprintf(stderr, "Trace: on\n");
else
fprintf(stderr, "Trace: off\n");
fprintf(stderr, "Options\n");
if (data.tftp_options[OPT_TSIZE].specified)
fprintf(stderr, " tsize: enabled\n");
else
fprintf(stderr, " tsize: disabled\n");
if (data.tftp_options[OPT_BLKSIZE].specified)
fprintf(stderr, " blksize: enabled\n");
else
fprintf(stderr, " blksize: disabled\n");
if (data.tftp_options[OPT_TIMEOUT].specified)
fprintf(stderr, " timeout: enabled\n");
else
fprintf(stderr, " timeout: disabled\n");
if (data.tftp_options[OPT_MULTICAST].specified)
fprintf(stderr, " multicast: enabled\n");
else
fprintf(stderr, " multicast: disabled\n");
#ifdef HAVE_MTFTP
fprintf(stderr, "mtftp variables\n");
fprintf(stderr, " client-port: %d\n", data.mtftp_client_port);
fprintf(stderr, " mcast-ip: %s\n", data.mtftp_mcast_ip);
fprintf(stderr, " listen-delay: %d\n", data.mtftp_listen_delay);
fprintf(stderr, " timeout-delay: %d\n", data.mtftp_timeout_delay);
#endif
#if HAVE_READLINE
if (history)
fprintf(stderr, "Last command: %s\n", history->line);
else
fprintf(stderr, "Last command: %s\n", "---");
#endif
if (strlen(data.tftp_options[OPT_FILENAME].value) > 0)
{
fprintf(stderr, "Last file: %s\n",
data.tftp_options[OPT_FILENAME].value);
if (tftp_result == OK)
{
print_eng((double)data.file_size, string, sizeof(string), "%3.3f%cB");
fprintf(stderr, " Bytes transfered: %s\n", string);
fprintf(stderr, " Time of transfer: %8.3fs\n",
(double)(tmp.tv_sec + tmp.tv_usec * 1e-6));
fprintf(stderr, " Throughput: ");
print_eng((data.file_size /
(double)(tmp.tv_sec + tmp.tv_usec * 1e-6)),
string, sizeof(string), "%3.3f%cB/s\n");
fprintf(stderr, "%s", string);
}
else
fprintf(stderr, " Transfer aborted\n");
}
return OK;
}
int set_timeout(int argc, char **argv)
{
if (argc == 1)
fprintf(stderr, "Timeout set to %d seconds.\n", data.timeout);
if (argc == 2)
data.timeout = atoi(argv[1]);
if (argc > 2)
{
fprintf(stderr, "Usage: timeout [value]\n");
return ERR;
}
return OK;
}
int help(int argc, char **argv)
{
int i = 0;
if (argc == 1)
{
/* general help */
fprintf(stderr, "Available command are:\n");
while (cmdtab[i].name != NULL)
{
fprintf(stderr, "%s\t\t%s\n", cmdtab[i].name,
cmdtab[i].helpmsg);
i++;
}
}
if (argc > 1)
{
while (cmdtab[i].name != NULL)
{
if (strcasecmp(cmdtab[i].name, argv[1]) == 0)
fprintf(stderr, "%s: %s\n", cmdtab[i].name,
cmdtab[i].helpmsg);
i++;
}
}
return OK;
}
#define PUT 1
#define GET 2
#define MGET 3
/*
* If tftp is called with --help, usage is printed and we exit.
* With --version, version is printed and we exit too.
* if --get --put --remote-file or --local-file is set, it imply non
* interactive invocation of tftp.
*/
int tftp_cmd_line_options(int argc, char **argv)
{
int c;
int ac; /* to format arguments for process_cmd */
char **av = NULL; /* same */
char string[MAXLEN];
char local_file[MAXLEN] = " ";
char remote_file[MAXLEN] = " ";
int action = 0;
int option_index = 0;
static struct option options[] = {
{ "get", 0, NULL, 'g'},
#ifdef HAVE_MTFTP
{ "mget", 0, NULL, 'G'},
#endif
{ "put", 0, NULL, 'p'},
{ "local-file", 1, NULL, 'l'},
{ "remote-file", 1, NULL, 'r'},
{ "tftp-timeout", 1, NULL, 'T'},
{ "mode", 1, NULL, 'M'},
{ "option", 1, NULL, 'O'},
#if 1
{ "timeout", 1, NULL, 't'},
{ "blksize", 1, NULL, 'b'},
{ "tsize", 0, NULL, 's'},
{ "multicast", 0, NULL, 'm'},
#endif
{ "mtftp", 1, NULL, '1'},
{ "no-source-port-checking", 0, NULL, '0'},
{ "verbose", 0, NULL, 'v'},
{ "trace", 0, NULL, 'd'},
#if DEBUG
{ "delay", 1, NULL, 'D'},
#endif
{ "version", 0, NULL, 'V'},
{ "help", 0, NULL, 'h' },
{ 0, 0, 0, 0 }
};
/* Support old argument until 0.8 */
while ((c = getopt_long(argc, argv, /*"gpl:r:Vh"*/ "gpl:r:Vht:b:sm",
options, &option_index)) != EOF)
{
switch (c)
{