-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1565 lines (1214 loc) · 45.9 KB
/
main.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
/*
* :ts=4
*
* TFTP client program for the Amiga, using only the SANA-II network
* device driver API, and no TCP/IP stack
*
* The "trivial file transfer protocol" is anything but trivial
* to implement...
*
* Copyright © 2016 by Olaf Barthel <obarthel at gmx dot net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. MY CAPS LOCK KEY SEEMS TO BE STUCK.
*/
// tftpclient device=ariadne_ii.device unit=0 verbose localaddress=192.168.1.230 from=192.168.1.116:/private/tftpboot/unix-tricks.txt to=unix-tricks.txt
// tftpclient device=ariadne_ii.device unit=0 verbose localaddress=192.168.1.230 from=unix-tricks.txt to=192.168.1.116:/private/tftpboot/write-test
#include <devices/timer.h>
#include <exec/memory.h>
#include <devices/sana2.h>
#include <dos/rdargs.h>
#include <dos/stdio.h>
/****************************************************************************/
#define __USE_INLINE__
#include <proto/utility.h>
#include <proto/exec.h>
#include <proto/dos.h>
/****************************************************************************/
#if defined(__amigaos4__)
#include <dos/obsolete.h>
#endif /* __amigaos4__ */
/****************************************************************************/
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
/****************************************************************************/
#include "macros.h"
#include "network-io.h"
#include "network-arp.h"
#include "network-ip-udp.h"
#include "network-tftp.h"
#include "error-codes.h"
#include "testing.h"
#include "timer.h"
#include "args.h"
/****************************************************************************/
#include "assert.h"
/****************************************************************************/
#include "TFTPClient_rev.h"
#ifndef TESTING
const char VersionTag[] = VERSTAG;
#else
const char VersionTag[] = VERSTAG " TESTING";
#endif /* !TESTING */
/****************************************************************************/
struct Library * UtilityBase;
/****************************************************************************/
#if defined(__amigaos4__)
struct UtilityIFace * IUtility;
#endif /* __amigaos4__ */
/****************************************************************************/
/* This function stops all I/O operations and releases all the resources
* allocated by the setup() function. It is safe to call even if setup()
* was never called, or setup() was only partially successful, or if
* cleanup() has already been called.
*/
static void
cleanup(void)
{
ENTER();
network_cleanup();
timer_cleanup();
#if defined(__amigaos4__)
{
if(IUtility != NULL)
{
DropInterface((struct Interface *)IUtility);
IUtility = NULL;
}
}
#endif /* __amigaos4__ */
if(UtilityBase != NULL)
{
CloseLibrary(UtilityBase);
UtilityBase = NULL;
}
LEAVE();
}
/****************************************************************************/
/* Initialize the network I/O operations, allocating SANA-II I/O requests,
* opening the network device driver, the timer, etc. Returns 0 on success,
* and -1 on failure.
*/
static int
setup(BPTR error_output, const struct cmd_args * args)
{
int result = FAILURE;
ENTER();
atexit(cleanup);
/* We would like to use the utility.library V39 function GetUniqueID().
* If it's not available, we'll use a different approach instead. So
* there is no need to succeed in opening utility.library V39.
*/
UtilityBase = OpenLibrary("utility.library", 39);
#if defined(__amigaos4__)
{
if(UtilityBase != NULL)
{
IUtility = (struct UtilityIFace *)GetInterface(UtilityBase, "main", 1, 0);
if(IUtility == NULL)
{
CloseLibrary(UtilityBase);
UtilityBase = NULL;
}
}
}
#endif /* __amigaos4__ */
if(timer_setup(error_output,args) != OK)
goto out;
if(network_setup(error_output, args) != OK)
goto out;
result = OK;
out:
RETURN(result);
return(result);
}
/****************************************************************************/
int
main(int argc,char ** argv)
{
/* The TFTP exchange rattles through the following
* steps until the transmission has completed or an
* error has occured.
*/
enum tftp_state_t
{
tftp_state_request_ethernet_address,
tftp_state_request_read,
tftp_state_request_write,
tftp_state_write_to_file,
tftp_state_read_from_file,
};
enum tftp_state_t tftp_state = tftp_state_request_ethernet_address;
struct RDArgs * rda = NULL;
int result = RETURN_FAIL;
struct cmd_args args;
TEXT device_name[256];
LONG device_unit;
TEXT local_ip_address[20];
STRPTR local_filename;
STRPTR remote_filename;
ULONG from_ipv4_address;
STRPTR from_path;
ULONG to_ipv4_address;
STRPTR to_path = NULL;
ULONG signals_received;
ULONG time_signal_mask, net_signal_mask, signal_mask;
int num_arp_resolution_attempts = 4;
BPTR source_file = (BPTR)NULL;
BPTR destination_file = (BPTR)NULL;
BOOL delete_destination_file = FALSE;
int client_udp_port_number;
int server_udp_port_number = TFTP_PORT_NUMBER;
BOOL server_udp_port_number_known = FALSE;
UBYTE tftp_packet[2 * sizeof(UWORD) + SEGSIZE]; /* opcode and block, followed by data */
struct tftphdr * tftp_output = (struct tftphdr *)tftp_packet;
int tftp_output_length = 0;
int tftp_payload_length = 0;
int block_number = 1;
BOOL last_block_transmitted = FALSE;
int num_eof_acknowledgements = 3;
LONG total_num_bytes_transferred = 0;
const struct Process * this_process = (struct Process *)FindTask(NULL);
BPTR error_output = this_process->pr_CES != (BPTR)NULL ? this_process->pr_CES : Output();
char ipv4_address[20];
const char * from_computer;
const char * to_computer;
SETDEBUGLEVEL(DEBUGLEVEL_CallTracing);
memset(&args,0,sizeof(args));
if(((struct Library *)DOSBase)->lib_Version < 37)
{
char * error_message = "This program requires AmigaOS 2.04 or better.\n";
Write(Output(),error_message,strlen(error_message));
goto out;
}
/* Process the command line parameters. */
rda = ReadArgs(cmd_template,(LONG *)&args,NULL);
if(rda == NULL)
{
PrintFault(IoErr(),"TFTPClient");
goto out;
}
if(args.Quiet)
args.Verbose = FALSE;
if(args.Verbose)
args.Quiet = FALSE;
/* If no DEVICE argument was provided, try the "TFTPDEVICE" environment variable. */
if(args.DeviceName == NULL)
{
if(GetVar("TFTPDEVICE",device_name,sizeof(device_name),0) > 0)
args.DeviceName = device_name;
}
if(args.DeviceName == NULL)
{
if(!args.Quiet)
FPrintf(error_output, "%s: Required %s argument is missing.\n","TFTPClient", "DEVICENAME");
goto out;
}
/* If no UNIT argument was provided, try the "TFTPUNIT" environment variable. */
if(args.DeviceUnit == NULL)
{
TEXT value[16];
if(GetVar("TFTPUNIT",value,sizeof(value),0) > 0)
{
if(StrToLong(value,&device_unit) > 0)
args.DeviceUnit = &device_unit;
}
}
if(args.DeviceUnit == NULL)
{
device_unit = 0;
args.DeviceUnit = &device_unit;
}
/* If no LOCALADDRESS argument was provided, try the "TFTPLOCALADDRESS" environment variable. */
if(args.LocalIPAddress == NULL)
{
if(GetVar("TFTPLOCALADDRESS",local_ip_address,sizeof(local_ip_address),0) > 0)
args.LocalIPAddress = local_ip_address;
}
if(args.LocalIPAddress == NULL)
{
if(!args.Quiet)
FPrintf(error_output, "%s: Required %s argument is missing.\n","TFTPClient", "LOCALADDRESS");
goto out;
}
/* The local IP address should be given in one of the
* standard IPv4 formats. Some minimal filtering of
* unusable addresses is performed here, too.
*/
if(!inet_aton(args.LocalIPAddress,&local_ipv4_address) || local_ipv4_address == 0 ||
local_ipv4_address == 0xFFFFFFFFUL || local_ipv4_address == 0x7F000001)
{
if(!args.Quiet)
FPrintf(error_output, "%s: '%s' is not a valid local IPv4 address.\n","TFTPClient",args.LocalIPAddress);
goto out;
}
if(args.RemotePort != NULL)
{
LONG remote_port = (*args.RemotePort);
if(remote_port < 0 || remote_port > 65535)
{
if(!args.Quiet)
FPrintf(error_output, "%s: Remote port number %ld is out of range; valid range is 1..65535, default is 69.\n","TFTPClient",remote_port);
goto out;
}
server_udp_port_number = remote_port;
}
/* The source is either a file name ("example"), or a file name with
* an IPv4 address prefix, like a device name ("192.168.0.1:example").
*/
get_ipv4_address_and_path_from_name(args.Source, &from_ipv4_address, &from_path);
if(from_ipv4_address == local_ipv4_address || from_ipv4_address == 0x7F000001)
from_ipv4_address = 0;
/* The file name is mandatory. */
if((*from_path) == '\0')
{
if(!args.Quiet)
FPrintf(error_output, "%s: FROM argument \"%s\" must include a file name.\n","TFTPClient", args.Source);
goto out;
}
/* The destination is either a file name ("example"), or a file name with
* an IPv4 address prefix, like a device name ("192.168.0.1:example").
*/
get_ipv4_address_and_path_from_name(args.Destination, &to_ipv4_address, &to_path);
if((*to_path) == '\0')
to_path = (STRPTR)FilePart(from_path);
if(to_ipv4_address == local_ipv4_address || to_ipv4_address == 0x7F000001)
to_ipv4_address = 0;
if(to_ipv4_address == 0xFFFFFFFFUL)
{
if(!args.Quiet)
FPrintf(error_output, "%s: '%s' is not a valid IPv4 destination address.\n","TFTPClient",args.Destination);
goto out;
}
if(from_ipv4_address != 0 && to_ipv4_address != 0)
{
if(!args.Quiet)
FPrintf(error_output, "%s: Please provide a source or destination IPv4 address, but not both.\n","TFTPClient");
goto out;
}
if(from_ipv4_address == to_ipv4_address)
{
if(!args.Quiet)
FPrintf(error_output, "%s: Please provide either a source or destination IPv4 address.\n","TFTPClient");
goto out;
}
if(from_ipv4_address != 0)
{
local_filename = to_path;
remote_ipv4_address = from_ipv4_address;
remote_filename = from_path;
}
else
{
local_filename = from_path;
remote_ipv4_address = to_ipv4_address;
remote_filename = to_path;
}
/* Make sure that the file name is not too long to be transmitted safely. */
if(offsetof(struct tftphdr, th_data) + strlen(remote_filename) + 1 + strlen("octet") + 1 > sizeof(tftp_packet))
{
if(!args.Quiet)
{
FPrintf(error_output, "%s: File name \"%s\" is too long (up to %ld characters are allowed).\n","TFTPClient",
remote_filename,sizeof(tftp_packet) - (offsetof(struct tftphdr, th_data) + 1 + strlen("octet") + 1));
}
goto out;
}
/* The arguments check out, now set up the network and timer I/O. */
if(setup(error_output, &args) < 0)
goto out;
if(from_ipv4_address == 0)
{
sprintf(ipv4_address,"%lu.%lu.%lu.%lu",
(to_ipv4_address >> 24) & 0xff,
(to_ipv4_address >> 16) & 0xff,
(to_ipv4_address >> 8) & 0xff,
to_ipv4_address & 0xff);
from_computer = "this computer";
to_computer = ipv4_address;
}
else
{
sprintf(ipv4_address,"%lu.%lu.%lu.%lu",
(from_ipv4_address >> 24) & 0xff,
(from_ipv4_address >> 16) & 0xff,
(from_ipv4_address >> 8) & 0xff,
from_ipv4_address & 0xff);
from_computer = ipv4_address;
to_computer = "this computer";
}
if(args.Verbose)
{
Printf("Copy \"%s\" (%s) to \"%s\" (%s)\n",
from_path,
from_computer,
to_path,
to_computer);
}
D(("Will copy \"%s\" (%s) to \"%s\" (%s).",
from_path,
from_computer,
to_path,
to_computer
));
/* We are sending a file? */
if(from_ipv4_address == 0)
{
source_file = Open(from_path, MODE_OLDFILE);
if(source_file == (BPTR)NULL)
{
TEXT error_message[256];
Fault(IoErr(),NULL,error_message,sizeof(error_message));
if(!args.Quiet)
FPrintf(error_output, "%s: Could not open file \"%s\" for reading (%s).\n","TFTPClient",from_path,error_message);
D(("Could not open file '%s' for reading (%s).",from_path,error_message));
goto out;
}
D(("Opened '%s' for reading.", from_path));
/* Use a read buffer. */
SetVBuf(source_file,NULL,BUF_FULL,8192);
}
/* We are receiving a file. */
else
{
/* Unless specifically requested, we verify that the
* file to write to does not yet exist and exit with
* error if does.
*/
if(!args.Overwrite)
{
BPTR test_lock;
test_lock = Lock(to_path, EXCLUSIVE_LOCK);
if(test_lock == (BPTR)NULL)
{
/* It's acceptable if the file in question does
* not exist yet. But everything else we count
* as an error, report it and abort.
*/
if(IoErr() != ERROR_OBJECT_NOT_FOUND)
{
TEXT error_message[256];
Fault(IoErr(),NULL,error_message,sizeof(error_message));
if(!args.Quiet)
FPrintf(error_output, "%s: Could not check if file \"%s\" exists (%s).\n","TFTPClient",to_path,error_message);
D(("Could not check if file '%s' exists (%s).",to_path,error_message));
goto out;
}
}
/* The file already exists. */
else
{
UnLock(test_lock);
if(!args.Quiet)
Printf("%s: Destination file \"%s\" already exists. Use OVERWRITE argument to replace it.\n","TFTPClient",to_path);
D(("Destination file '%s' already exists.",to_path));
result = RETURN_WARN;
goto out;
}
}
/* This will either create or overwrite the file. */
destination_file = Open(to_path, MODE_NEWFILE);
if(destination_file == (BPTR)NULL)
{
TEXT error_message[256];
Fault(IoErr(),NULL,error_message,sizeof(error_message));
if(!args.Quiet)
FPrintf(error_output, "%s: Could not open file \"%s\" for writing (%s).\n","TFTPClient",to_path,error_message);
D(("Could not open file '%s' for writing (%s).",to_path,error_message));
goto out;
}
D(("Opened '%s' for writing.", to_path));
/* Use a write buffer. */
SetVBuf(destination_file,NULL,BUF_FULL,8192);
/* Delete an empty file. */
delete_destination_file = TRUE;
}
/* We need to send UDP datagrams using a specific port number, which
* uniquely identifies the TFTP session. This picks an "ephemeral"
* port number, which should be in the range 49152..65535.
*/
if(UtilityBase != NULL && UtilityBase->lib_Version >= 39)
{
/* Really use a unique ID. */
client_udp_port_number = 49152 + (GetUniqueID() % 16384);
}
else
{
/* Try to get by with a pseudo-randomly chosen port. */
srand(time(NULL));
client_udp_port_number = 49152 + (rand() % 16384);
}
D(("client udp port number = %ld", client_udp_port_number));
if(args.Verbose)
Printf("Sending ARP query...\n");
SHOWMSG("Sending ARP query.");
/* We need to know the Ethernet address corresponding to the IPv4
* address of the remote TFTP server.
*/
broadcast_arp_query(remote_ipv4_address);
D(("starting the timer"));
start_time(1);
time_signal_mask = (1UL << time_port->mp_SigBit);
net_signal_mask = (1UL << net_read_port->mp_SigBit);
signal_mask = SIGBREAKF_CTRL_C | time_signal_mask | net_signal_mask;
signals_received = 0;
while(TRUE)
{
/* Wait for something to happen... */
if(signals_received == 0)
signals_received = Wait(signal_mask);
/* Keep processing the signals set by previous Wait(),
* polling for new signal events.
*/
else
signals_received |= (SetSignal(0,signal_mask) & signal_mask);
/* Stop the program? */
if(signals_received & SIGBREAKF_CTRL_C)
break;
/* New network data has arrived? */
if(signals_received & net_signal_mask)
{
struct NetIORequest * read_request;
/* Pick up the next network I/O request available, then
* mark it as no longer in use.
*/
read_request = (struct NetIORequest *)GetMsg(net_read_port);
if(read_request != NULL)
D(("Read request 0x%08lx has returned.", read_request));
#if defined(TESTING)
{
if(read_request != NULL)
{
const char * type = (read_request->nior_Type == ETHERTYPE_IP) ? "IP" : "ARP";
read_request->nior_InUse = FALSE;
if(0 < drop_rx && (rand() % 100) < drop_rx)
{
Printf("TESTING: Dropping received %s packet.\n", type);
D(("TESTING: Dropping received %s packet.", type));
send_net_io_read_request(read_request,read_request->nior_Type);
read_request = NULL;
}
else if (0 < trash_rx && (rand() % 100) < trash_rx)
{
if(read_request->nior_IOS2.ios2_DataLength > 0)
{
Printf("TESTING: Trashing received %s packet.\n", type);
D(("TESTING: Trashing received %s packet.", type));
ASSERT( read_request->nior_IOS2.ios2_DataLength <= read_request->nior_BufferSize );
((UBYTE *)read_request->nior_Buffer)[rand() % read_request->nior_IOS2.ios2_DataLength] ^= 0x81;
}
}
}
}
#endif /* TESTING */
if(read_request != NULL)
{
read_request->nior_InUse = FALSE;
/* Is this an IP datagram? */
if (read_request->nior_Type == ETHERTYPE_IP)
{
struct ip * ip = read_request->nior_Buffer;
SHOWMSG("received an IP datagram");
/* Verify that the IP header checksum is correct. */
if(read_request->nior_IOS2.ios2_DataLength >= sizeof(*ip) && in_cksum(ip,sizeof(*ip)) == 0)
{
/* This should be an IPv4 datagram, and it should contain
* an UDP datagram.
*/
if(((ip->ip_v_hl >> 4) & 15) == IPVERSION && ip->ip_pr == IPPROTO_UDP)
{
struct udphdr * udp = (struct udphdr *)&ip[1];
int checksum;
SHOWMSG("datagram contains UDP data");
/* Is the UDP datagram checksum OK, and the datagram is
* intended for our use? Furthermore, are we even ready to
* process it yet?
*/
checksum = verify_udp_datagram_checksum(ip);
if(checksum == 0 &&
udp->uh_dport == client_udp_port_number &&
(!server_udp_port_number_known || udp->uh_sport == server_udp_port_number) &&
tftp_state > tftp_state_request_ethernet_address)
{
const struct tftphdr * tftp = (struct tftphdr *)&udp[1];
int length = udp->uh_ulen - sizeof(*udp);
/* Server responded with an error? We print the error message and abort. */
if (tftp->th_opcode == TFTP_PACKET_ERROR)
{
const char * error_text;
char number[20];
char message_buffer[SEGSIZE+1];
int message_length;
SHOWMSG("TFTP opcode = TFTP_PACKET_ERROR");
error_text = get_tftp_error_text(tftp->th_code);
if(error_text == NULL)
{
sprintf(number,"error %d",tftp->th_code);
error_text = number;
}
message_length = length - offsetof(struct tftphdr, th_msg);
ASSERT( message_length >= 0 );
ASSERT( message_length < (int)sizeof(message_buffer) );
memmove(message_buffer,tftp->th_msg,message_length);
message_buffer[message_length] = '\0';
if(!args.Quiet)
FPrintf(error_output, "%s: Server responded with error '%s', \"%s\".\n","TFTPClient",error_text,message_buffer);
D(("Server responded with error '%s', '%s'.",error_text,message_buffer));
result = RETURN_ERROR;
goto out;
}
/* Server has transmitted data? */
else if (tftp->th_opcode == TFTP_PACKET_DATA)
{
int payload_length = length - offsetof(struct tftphdr, th_data);
SHOWMSG("TFTP opcode = TFTP_PACKET_DATA");
/* Make sure that the data packet size is sane. */
if(payload_length > SEGSIZE)
{
if(args.Verbose)
{
Printf("Data packet size (%ld bytes) is larger than expected; keeping only the first %ld bytes.\n",
payload_length, SEGSIZE);
}
D(("Data packet size (%ld bytes) is larger than expected; keeping only the first %ld bytes.",payload_length, SEGSIZE));
payload_length = SEGSIZE;
}
/* Did we just request to start reception of data? */
if (tftp_state == tftp_state_request_read)
{
/* This should be the very first data block. */
if(tftp->th_block == 1)
{
/* This is important: the server's tftp session is bound
* to a specific port number now.
*/
server_udp_port_number = udp->uh_sport;
server_udp_port_number_known = TRUE;
if(args.Verbose)
Printf("Server has acknowledged the read request (using UDP port number %ld).\n", server_udp_port_number);
D(("Server has acknowledged the read request (using UDP port number %ld).", server_udp_port_number));
/* Store the data, if any. */
if(payload_length > 0)
{
if(args.Verbose)
Printf("Writing block #%ld (%ld bytes).\n",tftp->th_block,payload_length);
D(("Writing block #%ld (%ld bytes).",tftp->th_block,payload_length));
SetIoErr(0);
if(FWrite(destination_file,(APTR)tftp->th_data,payload_length,1) == 0)
{
TEXT error_message[256];
Fault(IoErr(),NULL,error_message,sizeof(error_message));
if(!args.Quiet)
FPrintf(error_output, "%s: Error writing to file \"%s\" (%s).\n","TFTPClient",to_path,error_message);
D(("Error writing to file '%s' (%s).",to_path,error_message));
send_tftp_error(TFTP_ERROR_UNDEF,"Error writing to file",client_udp_port_number,server_udp_port_number,tftp_packet);
result = RETURN_ERROR;
goto out;
}
total_num_bytes_transferred += payload_length;
/* We received some data to keep, so do not delete the file. */
delete_destination_file = FALSE;
}
else
{
SHOWMSG("no data in the packet");
}
/* Is this the last data to be received? */
if(payload_length < SEGSIZE)
{
SHOWMSG("this is the last block transmitted by the server");
last_block_transmitted = TRUE;
num_eof_acknowledgements--;
}
tftp_state = tftp_state_write_to_file;
block_number = 2;
if(args.Verbose)
Printf("Acknowledging receipt of block #%ld.\n",block_number-1);
D(("Acknowledging receipt of block #%ld.",block_number-1));
send_tftp_acknowledgement(block_number-1,client_udp_port_number,server_udp_port_number,tftp_packet);
/* Restart the timer; make sure that we won't try to
* service the returning time request or there will
* be trouble.
*/
signals_received &= ~time_signal_mask;
D(("starting the timer"));
start_time(1);
}
else
{
if(args.Verbose)
Printf("Ignoring receipt of block #%ld.\n",tftp->th_block);
D(("Ignoring receipt of block #%ld.",tftp->th_block));
}
}
/* Are we already receiving data to be written? */
else if (tftp_state == tftp_state_write_to_file)
{
/* Is this the next block we expected? */
if(tftp->th_block == block_number)
{
/* Store the data, if any. */
if(payload_length > 0)
{
if(args.Verbose)
Printf("Writing block #%ld (%ld bytes).\n",tftp->th_block,payload_length);
D(("Writing block #%ld (%ld bytes).",tftp->th_block,payload_length));
SetIoErr(0);
if(FWrite(destination_file,(APTR)tftp->th_data,payload_length,1) == 0)
{
TEXT error_message[256];
Fault(IoErr(),NULL,error_message,sizeof(error_message));
if(!args.Quiet)
FPrintf(error_output, "%s: Error writing to file \"%s\" (%s).\n","TFTPClient",to_path,error_message);
D(("Error writing to file '%s' (%s).",to_path,error_message));
send_tftp_error(TFTP_ERROR_UNDEF,"Error writing to file",client_udp_port_number,server_udp_port_number,tftp_packet);
result = RETURN_ERROR;
goto out;
}
total_num_bytes_transferred += payload_length;
/* We received some data to keep, do not delete the file. */
delete_destination_file = FALSE;
}
/* Is this the last data to be received? */
if(payload_length < SEGSIZE)
{
last_block_transmitted = TRUE;
num_eof_acknowledgements--;
}
block_number++;
if(args.Verbose)
Printf("Acknowledging receipt of block #%ld.\n",block_number-1);
D(("Acknowledging receipt of block #%ld.",block_number-1));
send_tftp_acknowledgement(block_number-1,client_udp_port_number,server_udp_port_number,tftp_packet);
/* Restart the timer; make sure that we won't try to
* service the returning time request or there will
* be trouble.
*/
signals_received &= ~time_signal_mask;
D(("starting the timer"));
start_time(1);
}
/* No, this is the wrong block. */
else
{
if(args.Verbose)
Printf("Ignoring receipt of block #%ld; was expecting block #%ld instead.\n",tftp->th_block,block_number);
D(("Ignoring receipt of block #%ld; was expecting block #%ld instead.",tftp->th_block,block_number));
}
}
else
{
if(args.Verbose)
Printf("Ignoring receipt of unexpected data block #%ld.\n",tftp->th_block);
D(("Ignoring receipt of unexpected data block #%ld.",tftp->th_block));
}
}
/* Server has acknowledged reception of data, or of the write request? */
else if (tftp->th_opcode == TFTP_PACKET_ACK)
{
SHOWMSG("TFTP opcode = TFTP_PACKET_ACK");
/* Could this be the server response to the write request? */
if (tftp_state == tftp_state_request_write)
{
/* The acknowledgement comes in the form of block #0 only. */
if(tftp->th_block == 0)
{
LONG num_bytes_read;
/* This is important: the server's tftp session is bound
* to a specific port number now.
*/
server_udp_port_number = udp->uh_sport;
server_udp_port_number_known = TRUE;
if(args.Verbose)
Printf("Server has acknowledged the write request (using UDP port number %ld).\n", server_udp_port_number);
D(("Server has acknowledged the write request (using UDP port number %ld).", server_udp_port_number));
tftp_state = tftp_state_read_from_file;
block_number = 1;
if(args.Verbose)
Printf("Reading block #%ld.\n",block_number);
D(("Reading block #%ld.",block_number));
SetIoErr(0);
num_bytes_read = FRead(source_file,tftp_output->th_data,1,SEGSIZE);
if(num_bytes_read == 0 && IoErr() != 0)
{
TEXT error_message[256];
Fault(IoErr(),NULL,error_message,sizeof(error_message));
if(!args.Quiet)
FPrintf(error_output, "%s: Error reading from file \"%s\" (%s).\n","TFTPClient",from_path,error_message);
D(("Error reading from file '%s' (%s).",from_path,error_message));
send_tftp_error(TFTP_ERROR_UNDEF,"Error reading from file",client_udp_port_number,server_udp_port_number,tftp_packet);
result = RETURN_ERROR;
goto out;
}
total_num_bytes_transferred += num_bytes_read;
/* Did we just read the last data to be transmitted? */
if(num_bytes_read < SEGSIZE)
{
last_block_transmitted = TRUE;
if(args.Verbose)
Printf("This is the last block to be read.\n");
D(("This is the last block to be read."));
}