summaryrefslogtreecommitdiffstats
path: root/main/common/tftp.c
blob: 1bc7d1b25188935fc85b2cdeb498336d6c933b04 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
/**************************************************************************
 *
 * Copyright (c) 2013 Alcatel-Lucent
 *
 * Alcatel Lucent licenses this file to You under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License.  A copy of the License is contained the
 * file LICENSE at the top level of this repository.
 * You may also obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **************************************************************************
 *
 * tftp.c:
 *
 * This code supports the monitor's TFTP server.
 * TFTP is covered under RFC 783.
 *
 * Original author:     Ed Sutter (ed.sutter@alcatel-lucent.com)
 *
 */
#include "config.h"
#if INCLUDE_TFTP
#include <stdarg.h>
#include "endian.h"
#include "genlib.h"
#include "cpuio.h"
#include "ether.h"
#include "tfs.h"
#include "tfsprivate.h"
#include "monflags.h"
#include "stddefs.h"
#include "flash.h"
#include "cli.h"
#include "timer.h"

/* To reduce uMon footprint size, INCLUDE_TFTPSRVR can be set to 0 in
 * config.h...
 */
#ifndef INCLUDE_TFTPSRVR
#define INCLUDE_TFTPSRVR 1
#endif

#if INCLUDE_ETHERVERBOSE
#define TFTPVERBOSE(a) a
#else
#define TFTPVERBOSE(a)
#endif


#define MODE_NULL       0
#define MODE_NETASCII   1
#define MODE_OCTET      2

void ShowTftpStats(void);
static int SendTFTPData(struct ether_header *,ushort,uchar *,int);
static int SendTFTPErr(struct ether_header *,short,int,char *fmt, ...);
static int SendTFTPAck(struct ether_header *,ushort);
static int SendTFTPRRQ(uchar *,uchar *,char *,char *,uchar *);
static int SendTFTPWRQ(uchar *,uchar *,char *,char *);


static  struct elapsed_tmr tftpTmr;

static ushort   tftpPrevBlock;  /* Keeps the value of the block number of
                                 * the previous TFTP transaction.
                                 */
static int TftpWrqMode;         /* Set to MODE_NETASCII, MODE_OCTET or
                                 * MODE_NULL based on the incoming WRQ
                                 * request
                                 */
static int TftpChopCount;       /* Number of characters chopped from the
                                 * incoming file because of NETASCII
                                 * conversion (remove 0x0d).
                                 */
static short TftpLastPktSize;
static uchar TftpLastPkt[TFTP_PKTOVERHEAD+TFTP_DATAMAX+4];
/* Storage of last packet sent.  This is
 * used if it is determined that the packet
 * most recently sent must be sent again.
 */

static long TftpRetryTimeout;   /* Count used during a TFTP transfer to
                                 * kick off a retransmission of a packet.
                                 */
static ushort TftpRmtPort;      /* Remote port of tftp transfer */
static uchar *TftpAddr;         /* Current destination address used for tftp
                                 * file transfer to/from local memory.
                                 */
static char *TftpLTMptr;        /* List-to-mem pointer used when filename
                                 * for RRQ is ".".
                                 */
static int  TftpCount;          /* Running total number of bytes transferred
                                 * for a particular tftp transaction.
                                 */
static char TftpState;          /* Used to keep track of the current state
                                 * of a tftp transaction.
                                 */
static char TftpTurnedOff;      /* If non-zero, then tftp is disabled. */
static char TftpGetActive;      /* If non-zero, then 'get' is in progress. */
static char TftpPutActive;      /* If non-zero, then 'put' is in progress. */
static char TftpErrString[32];  /* Used to post a tftp error message. */
static char TftpTfsFname[TFSNAMESIZE+64];   /* Store name of WRQ destination
                                             * file (plus flags & info).
                                             */
#if INCLUDE_TFTPSRVR
/* ls_to_mem():
 * Turn the list of files into a string buffer that contains
 * each filename followed by a newline.  Each filename is the "full"
 * name, meaning that it will contain commas to delimit the flags and
 * info fields of the file.
 * Return a pointer to the allocated space.
 */
static char *
ls_to_mem(void)
{
#if INCLUDE_TFS
    int     size;
    TFILE   *tfp;
    char    *base, *bp, *info, *flags, fbuf[16];

    /* First determine how much memory will be needed to store
     * the list of files...  Each filename in the list will consist
     * of the string formatted as "name,flags,info\n"...
     */
    size = 0;
    tfp = (TFILE *)0;
    while((tfp = tfsnext(tfp))) {
        /* Increment size to include filename, 2 commas and a newline...
         */
        size += (strlen(TFS_NAME(tfp)) + 3);

        /* Determine if flags and/or info field is to be present in
         * the name, and add that to the size appropriately...
         */
        flags = tfsflagsbtoa(TFS_FLAGS(tfp),fbuf);
        if(!fbuf[0]) {
            flags = 0;
        }
        if(flags) {
            size += strlen(flags);
        }
        if(TFS_INFO(tfp)) {
            size += strlen(TFS_INFO(tfp));
        }
    }

    if(size == 0) {
        return(0);
    }

    /* Allocate the space.  Add one additional byte to the size to
     * account for the NULL termination at the end of the string...
     */
    base = bp = malloc(size+1);
    if(bp == (char *)0) {
        return(0);
    }

    /* Load the buffer with the list of names with their
     * TFS extensions...
     */
    tfp = (TFILE *)0;
    while((tfp = tfsnext(tfp))) {
        flags = tfsflagsbtoa(TFS_FLAGS(tfp),fbuf);
        if(!flags || !fbuf[0]) {
            flags = "";
        }
        if(TFS_INFO(tfp)) {
            info = TFS_INFO(tfp);
        } else {
            info = "";
        }

        bp += sprintf(bp,"%s,%s,%s\n",TFS_NAME(tfp),flags,info);
    }

    return(base);
#else
    return(0);
#endif
}
#endif

static char *
tftpStringState(int state)
{
    switch(state) {
    case TFTPOFF:
        return("OFF");
    case TFTPIDLE:
        return("IDLE");
    case TFTPACTIVE:
        return("ACTIVE");
    case TFTPERROR:
        return("ERROR");
    case TFTPHOSTERROR:
        return("HOSTERROR");
    case TFTPSENTRRQ:
        return("SENTRRQ");
    case TFTPSENTWRQ:
        return("SENTWRQ");
    case TFTPTIMEOUT:
        return("TIMEOUT");
    default:
        return("???");
    }
}

static int
tftpGotoState(int state)
{
    int ostate;

    ostate = TftpState;
    TftpState = state;
#if INCLUDE_ETHERVERBOSE
    if((EtherVerbose & SHOW_TFTP_STATE) && (state != ostate))
        printf("  TFTP State change %s -> %s\n",
               tftpStringState(ostate), tftpStringState(state));
#endif
    return(ostate);
}

/* tftpGet():
 *  Return size of file if successful; else 0.
 */
int
tftpGet(ulong addr,char *tftpsrvr,char *mode, char *hostfile,char *tfsfile,
        char *tfsflags,char *tfsinfo)
{
    int     done;
    uchar   binip[8], binenet[8], *enetaddr;

    setenv("TFTPGET",0);

    /* Convert IP address to binary: */
    if(IpToBin(tftpsrvr,binip) < 0) {
        return(0);
    }

    /* Get the ethernet address for the IP: */
    /* Give ARP the same verbosity (if any) set up for TFTP: */
    TFTPVERBOSE(if(EtherVerbose & SHOW_TFTP_STATE) EtherVerbose |= SHOW_ARP);
    enetaddr = ArpEther(binip,binenet,0);
    TFTPVERBOSE(EtherVerbose &= ~SHOW_ARP);
    if(!enetaddr) {
        printf("ARP failed for %s\n",tftpsrvr);
        return(0);
    }

    printf("Retrieving %s from %s...\n",hostfile,tftpsrvr);

    /* Send the TFTP RRQ to initiate the transfer. */
    if(SendTFTPRRQ(binip,binenet,hostfile,mode,(uchar *)addr) < 0) {
        printf("RRQ failed\n");
        return(0);
    }

    TftpGetActive = 1;

    /* Wait for TftpState to indicate that the transaction has completed... */
    done = 0;
    while(!done) {
        pollethernet();
        switch(TftpState) {
        case TFTPIDLE:
            printf("Rcvd %d bytes",TftpCount);
            done = 1;
            break;
        case TFTPERROR:
            printf("TFTP Terminating\n");
            done = 2;
            break;
        case TFTPHOSTERROR:
            printf("Host error: %s\n",TftpErrString);
            done = 2;
            break;
        case TFTPTIMEOUT:
            printf("Timing out (%d bytes rcvd)\n",TftpCount);
            done = 2;
            break;
        default:
            break;
        }
    }
    TftpGetActive = 0;

    if(done == 2) {
        tftpInit();
        return(0);
    }

    if(tfsfile) {
        int err, filesize;

        filesize = TftpCount - TftpChopCount;
        printf("\nAdding %s (size=%d) to TFS...",tfsfile,filesize);
        err = tfsadd(tfsfile,tfsinfo,tfsflags,(uchar *)addr,filesize);
        if(err != TFS_OKAY) {
            printf("%s: %s\n",tfsfile,(char *)tfsctrl(TFS_ERRMSG,err,0));
        }
    }
    printf("\n");
    shell_sprintf("TFTPGET","%d",TftpCount);
    return(TftpCount);
}

/* tftpPut():
 * Return size of file if successful; else 0.
 *
 * Simple steps from RFC 1350 for PUT are:
 * 1. Send WRQ to port 69 of remote server
 * 2. Receive ACK 0 from Server RemotePort (not 69)
 * 3. Send data less than 512 bytes to RemotePort which closes it out.
 * 4. Reinit if error (above in GET errors if done == 2)
 *
 * This 'put' capability was contributed by Steve Shireman.
 */

int
tftpPut(char *tftpsrvr,char *mode,char *hostfile,char *tfsfile,
        char *tfsflags,char *tfsinfo)
{
#if INCLUDE_TFS
    int     done, tot;
    TFILE   *tfp;
    uchar   binip[8], binenet[8], *enetaddr;

    setenv("TFTPPUT",0);

    if((tfp = tfsstat(tfsfile)) == (TFILE *)0) {
        return(0);
    }

    /* For 'put', the address increments and the count decrements as
     * the transfer progresses.
     */
    TftpAddr = (uchar *)TFS_BASE(tfp);
    TftpCount = tot = TFS_SIZE(tfp);

    /* Convert IP address to binary: */
    if(IpToBin(tftpsrvr,binip) < 0) {
        return(0);
    }

    /* Get the ethernet address for the IP:
     * (give ARP the same verbosity set up for TFTP)
     */
    TFTPVERBOSE(if(EtherVerbose & SHOW_TFTP_STATE) EtherVerbose |= SHOW_ARP);
    enetaddr = ArpEther(binip,binenet,0);
    TFTPVERBOSE(EtherVerbose &= ~SHOW_ARP);

    if(!enetaddr) {
        printf("ARP failed for %s\n",tftpsrvr);
        return(0);
    }

    printf("Sending %s to %s:%s...\n",tfsfile,tftpsrvr,hostfile);
    TftpPutActive = 1;
    tftpPrevBlock = 0;

    /* Send the TFTP WRQ to initiate the transfer. */
    if(SendTFTPWRQ(binip,binenet,hostfile,mode) < 0) {
        printf("WRQ failed\n");
        return(0);
    }

    /* Wait for TftpState to indicate that the transaction has completed... */
    done = 0;
    while(!done) {
        pollethernet();
        switch(TftpState) {
        case TFTPIDLE:
            printf("Sent %d bytes",tot);
            done = 1;
            break;
        case TFTPERROR:
            printf("TFTP Terminating\n");
            done = 2;
            break;
        case TFTPHOSTERROR:
            printf("Host error: %s\n",TftpErrString);
            done = 2;
            break;
        case TFTPTIMEOUT:
            printf("Timing out (%d bytes sent)\n",TftpCount);
            done = 2;
            break;
        default:
            break;
        }
    }
    TftpPutActive = 0;

    if(done == 2) {
        tftpInit();
        return(0);
    }
    printf("\n");
    shell_sprintf("TFTPPUT","%d",tot);
    return(tot);
#else
    printf("TFTP 'put' requires TFS\n");
    return(0);
#endif
}

/* tftpInit():
 *  Called by the ethenet initialization to initialize state variables.
 */
void
tftpInit()
{
    TftpCount = -1;
    TftpRmtPort = 0;
    TftpTurnedOff = 0;
    tftpGotoState(TFTPIDLE);
    TftpAddr = (uchar *)0;
}

/* storePktAndSend():
 *  The final stage in sending a TFTP packet...
 *  1. Compute IP and UDP checksums;
 *  2. Copy ethernet packet to a buffer so that it can be resent
 *      if necessary (by tftpStateCheck()).
 *  3. Store the size of the packet;
 *  4. Send the packet out the interface.
 *  5. Reset the timeout count and re-transmission delay variables.
 */
static void
storePktAndSend(struct ip *ipp, struct ether_header *epkt,int size)
{
    ipChksum(ipp);                          /* Compute csum of ip hdr */
    udpChksum(ipp);                         /* Compute UDP checksum */
    /* Copy packet to static buffer */
    memcpy((char *)TftpLastPkt,(char *)epkt,size);
    TftpLastPktSize = size;                 /* Copy size to static location */
    sendBuffer(size);                       /* Send buffer out ethernet i*/

    /* Re-initialize the re-transmission delay variables.
     */
    TftpRetryTimeout = RetransmitDelay(DELAY_INIT_TFTP);
    startElapsedTimer(&tftpTmr,TftpRetryTimeout * 1000);
}

/* getTftpSrcPort():
 *  Each time a TFTP RRQ goes out, use a new source port number.
 *  Cycle through a block of 256 port numbers...
 */
static ushort
getTftpSrcPort(void)
{
    if(TftpSrcPort < (IPPORT_TFTPSRC+256)) {
        TftpSrcPort++;
    } else {
        TftpSrcPort = IPPORT_TFTPSRC;
    }
    return(TftpSrcPort);
}

/* tftpStateCheck():
 *  Called by the pollethernet function to support the ability to retry
 *  on a TFTP transmission that appears to have terminated prematurely
 *  due to a lost packet.  If a packet is sent and the response is not
 *  received within about 1-2 seconds, the packet is re-sent.  The retry
 *  will repeat 8 times; then give up and set the TFTP state to idle.
 *
 *  Taken from RFC 1350 section 2 "Overview of the Protocol"...
 *  ... If a packet gets lost in the network, the intended recipient will
 *  timeout and may retransmit his last packet (which may be data or an
 *  acknowledgement), thus causing the sender of the lost packet to retransmit
 *  the lost packet.
 *
 *  Taken from RFC 1123 section 4.2.3.2 "Timeout Algorithms"...
 *  ... a TFTP implementation MUST use an adaptive timeout ...
 */

void
tftpStateCheck(void)
{
    uchar   *buf;
    long    delay;
    ushort  tftp_opcode;
    struct  ip *ihdr;
    struct  Udphdr *uhdr;
    struct  ether_header *ehdr;

    switch(TftpState) {
    case TFTPIDLE:
    case TFTPTIMEOUT:
    case TFTPERROR:
        return;
    default:
        break;
    }

    /* If timeout occurs, re-transmit the packet...
     */
    if(!msecElapsed(&tftpTmr)) {
        return;
    }

    delay = RetransmitDelay(DELAY_INCREMENT);
    if(delay == RETRANSMISSION_TIMEOUT) {
#if INCLUDE_ETHERVERBOSE
        if(EtherVerbose & SHOW_TFTP_STATE) {
            printf("  TFTP_RETRY giving up\n");
        }
#endif
        tftpGotoState(TFTPTIMEOUT);
        enableBroadcastReception();
        return;
    }

    /* Get a transmit buffer and copy the packet that was last sent.
     * Insert a new IP ID, recalculate the checksums and send it again...
     * If the opcode of the packet to be re-transmitted is RRQ, then
     * use a new port number.
     */
    buf = (uchar *)getXmitBuffer();
    memcpy((char *)buf,(char *)TftpLastPkt,TftpLastPktSize);
    ehdr = (struct ether_header *)buf;
    ihdr = (struct ip *)(ehdr + 1);
    uhdr = (struct Udphdr *)(ihdr + 1);
    tftp_opcode = *(ushort *)(uhdr + 1);
    ihdr->ip_id = ipId();
    if(tftp_opcode == ecs(TFTP_RRQ)) {
        uhdr->uh_sport = getTftpSrcPort();
        self_ecs(uhdr->uh_sport);
    }
    ipChksum(ihdr);
    udpChksum(ihdr);
    sendBuffer(TftpLastPktSize);

#if INCLUDE_ETHERVERBOSE
    if(EtherVerbose & SHOW_TFTP_STATE) {
        printf("  TFTP_RETRY (%ld secs)\n",TftpRetryTimeout);
    }
#endif

    TftpRetryTimeout = delay;
    startElapsedTimer(&tftpTmr,TftpRetryTimeout * 1000);
    return;
}

#if INCLUDE_TFTPSRVR
/* tftpStartSrvrFilter():
 * Called when either a TFTP_RRQ or TFTP_WRQ is received indicating that
 * a client wants to start up a transfer.
 * If TftpState is IDLE, ERROR or TIMEOUT, this means it is safe to start
 * up a new transfer through the server; otherwise return 0.
 * Also, if the server has been disabled by "tftp off" at the command line
 * (i.e. TftpTurnedOff == 1) then return 0.
 */
static int
tftpStartSrvrFilter(struct ether_header *ehdr,struct Udphdr *uhdr)
{
    if(TftpTurnedOff) {
        SendTFTPErr(ehdr,0,1,"TFTP srvr off");
        return(0);
    }

    if((TftpState == TFTPIDLE) || (TftpState == TFTPERROR) ||
            (TftpState == TFTPTIMEOUT)) {
        TftpTfsFname[0] = 0;
        TftpRmtPort = ecs(uhdr->uh_sport);
        tftpGotoState(TFTPACTIVE);
        return(1);
    }
    /* If block is zero and the incoming WRQ request is from the same
     * port as was previously recorded, then assume the ACK sent back
     * to the requester was not received, and this is a WRQ that is
     * being re-sent.  That being the case, just send the Ack back.
     */
    else if((tftpPrevBlock == 0) && (TftpRmtPort == uhdr->uh_sport)) {
        SendTFTPAck(ehdr,0);
        return(0);
    } else {
        /* Note: the value of TftpState is not changed (final arg to
         * SendTFTPErr is 0) to TFTPERROR.  This is because
         * we received a RRQ/WRQ request while processing a different
         * TFTP transfer.  We want to send the error response to the
         * sender, but we don't want to stay in an error state because
         * there is another valid TFTP transfer in progress.
         */
        SendTFTPErr(ehdr,0,0,"TFTP srvr busy");
        return(0);
    }
}
#endif

/* tftpTransferComplete():
 * Print a message indicating that the transfer has completed.
 */
static void
tftpTransferComplete(void)
{
#if INCLUDE_ETHERVERBOSE
    if((EtherVerbose & SHOW_TFTP_STATE) || (!MFLAGS_NOTFTPPRN())) {
        printf("TFTP transfer complete.\n");
    }
#endif
}

/* processTFTP():
 *  This function handles the majority of the TFTP requests that a
 *  TFTP server must be able to handle.  There is no real robust
 *  error handling, but it seems to work pretty well.
 *  Refer to Stevens' "UNIX Network Programming" chap 12 for details
 *  on TFTP.
 *  Note: During TFTP, promiscuity, broadcast & multicast reception
 *  are all turned off.  This is done to speed up the file transfer.
 */
int
processTFTP(struct ether_header *ehdr,ushort size)
{
    struct  ip *ihdr;
    struct  Udphdr *uhdr;
    uchar   *data;
    int     count;
    ushort  opcode, block, errcode;
    char    *errstring, *tftpp;
#if INCLUDE_TFTPSRVR
    char    *comma, *env, *filename, *mode;
#endif

    ihdr = (struct ip *)(ehdr + 1);
    uhdr = (struct Udphdr *)((char *)ihdr + IP_HLEN(ihdr));
    tftpp = (char *)(uhdr + 1);
    opcode = *(ushort *)tftpp;

    switch(opcode) {
#if INCLUDE_TFTPSRVR
    case ecs(TFTP_WRQ):
        filename = tftpp+2;
#if INCLUDE_ETHERVERBOSE
        if((EtherVerbose & SHOW_TFTP_STATE) || (!MFLAGS_NOTFTPPRN())) {
            printf("TFTP rcvd WRQ: file <%s>\n", filename);
        }
#endif
        if(!tftpStartSrvrFilter(ehdr,uhdr)) {
            return(0);
        }

        mode = filename;
        while(*mode) {
            mode++;
        }
        mode++;

        /* Destination of WRQ can be an address (0x...), environment
         * variable ($...) or a TFS filename...
         */
        if(filename[0] == '$') {
            env = getenv(&filename[1]);
            if(env) {
                TftpAddr = (uchar *)strtol(env,(char **)0,0);
            } else {
                SendTFTPErr(ehdr,0,1,"Shell var not set");
                return(0);
            }
        } else if((filename[0] == '0') && (filename[1] == 'x')) {
            TftpAddr = (uchar *)strtol(filename,(char **)0,0);
        } else {
            if(MFLAGS_NOTFTPOVW() && tfsstat(filename)) {
                SendTFTPErr(ehdr,6,1,"File exists");
                return(0);
            }
            TftpAddr = (uchar *)getAppRamStart();
            strncpy(TftpTfsFname,filename,sizeof(TftpTfsFname)-1);
            TftpTfsFname[sizeof(TftpTfsFname)-1] = 0;
        }
        TftpCount = -1; /* not used with WRQ, so clear it */

        /* Convert mode to lower case... */
        strtolower(mode);
        if(!strcmp(mode,"netascii")) {
            TftpWrqMode = MODE_NETASCII;
        } else if(!strcmp(mode,"octet")) {
            TftpWrqMode = MODE_OCTET;
        } else {
            SendTFTPErr(ehdr,0,1,"Mode '%s' not supported.",mode);
            TftpWrqMode = MODE_NULL;
            TftpCount = -1;
            return(0);
        }
        block = 0;
        tftpPrevBlock = block;
        TftpChopCount = 0;
        disableBroadcastReception();
        break;
    case ecs(TFTP_RRQ):
        filename = tftpp+2;
#if INCLUDE_ETHERVERBOSE
        if((EtherVerbose & SHOW_TFTP_STATE) || (!MFLAGS_NOTFTPPRN())) {
            printf("TFTP rcvd RRQ: file <%s>\n",filename);
        }
#endif
        if(!tftpStartSrvrFilter(ehdr,uhdr)) {
            return(0);
        }
        mode = filename;
        while(*mode) {
            mode++;
        }
        mode++;
        comma = strchr(filename,',');
        if(!comma) {
            TFILE   *tfp;

            if(!strcmp(filename,".")) {
                TftpLTMptr = (char *)ls_to_mem();
                TftpAddr = (uchar *)TftpLTMptr;
                if(TftpLTMptr) {
                    TftpCount = strlen((char *)TftpAddr);
                } else {
                    TftpCount = 0;
                }
            } else {
                tfp = tfsstat(filename);
                if(!tfp) {
                    SendTFTPErr(ehdr,0,1,"File (%s) not found",filename);
                    TftpCount = -1;
                    return(0);
                }
                TftpAddr = (uchar *)TFS_BASE(tfp);
                TftpCount = TFS_SIZE(tfp);
            }
        } else {
            *comma++ = 0;
            if((filename[0] == '$') && (getenv(&filename[1]))) {
                TftpAddr = (uchar *)strtol(getenv(&filename[1]),(char **)0,0);
            } else {
                TftpAddr = (uchar *)strtol(filename,(char **)0,0);
            }
            TftpCount = strtol(comma,(char **)0,0);
        }
        if(strcmp(mode,"octet")) {
            SendTFTPErr(ehdr,0,1,"Must use binary mode");
            TftpCount = -1;
            return(0);
        }
        block = tftpPrevBlock = 1;
        disableBroadcastReception();
        tftpGotoState(TFTPACTIVE);
        SendTFTPData(ehdr,block,TftpAddr,TftpCount);
        return(0);
#endif
    case ecs(TFTP_DAT):
        block = ecs(*(ushort *)(tftpp+2));
        count = ecs(uhdr->uh_ulen) - (sizeof(struct Udphdr)+4);
#if INCLUDE_ETHERVERBOSE
        if(EtherVerbose & SHOW_TFTP_STATE) {
            printf("  Rcvd TFTP_DAT (%d,blk=%d)\n",count,block);
        } else if(EtherVerbose & SHOW_TFTP_TICKER) {
            ticktock();
        }
#endif

        if(TftpState == TFTPSENTRRQ) {      /* See notes in SendTFTPRRQ() */
            tftpPrevBlock = 0;
            if(block == 1) {
                TftpRmtPort = ecs(uhdr->uh_sport);
                tftpGotoState(TFTPACTIVE);
            } else {
                SendTFTPErr(ehdr,0,1,"invalid block (%d)",block);
                return(0);
            }
        }
        /* Since we don't ACK the final TFTP_DAT from the server until after
         * the file has been written, it is possible that we will receive
         * a re-transmitted TFTP_DAT from the server.  This is ignored by
         * Sending another ACK...
         */
        else if((TftpState == TFTPIDLE) && (block == tftpPrevBlock)) {
            SendTFTPAck(ehdr,block);
#if INCLUDE_ETHERVERBOSE
            if(EtherVerbose & SHOW_TFTP_STATE) {
                printf("  (packet ignored)\n");
            }
#endif
            return(0);
        } else if(TftpState != TFTPACTIVE) {
            SendTFTPErr(ehdr,0,1,"Bad state (%s) for incoming TFTP_DAT",
                        tftpStringState(TftpState));
            return(0);
        }

        if(ecs(uhdr->uh_sport) != TftpRmtPort) {
            SendTFTPErr(ehdr,0,0,"TFTP_DAT porterr: rcvd %d, expected %d",
                        ecs(uhdr->uh_sport),TftpRmtPort);
            return(0);
        }
        if(block == tftpPrevBlock) {    /* If block didn't increment, assume */
            SendTFTPAck(ehdr,block);    /* retry.  Ack it and return here.  */
            return(0);          /* Otherwise, if block != tftpPrevBlock+1, */
        }                       /* return an error, and quit now.   */
        else if((block == 0) && ((tftpPrevBlock & 0xffff) == 0xffff)) {
            /* This case occurs when the block number wraps.
             * It is a legal case where the downloaded data exceeds
             * 32Mg (blockno > 0xffff).
             */
        } else if(block != tftpPrevBlock+1) {
#ifdef DONT_IGNORE_OUT_OF_SEQUENCE_BLOCKS
            SendTFTPErr(ehdr,0,1,"TFTP_DAT blockerr: rcvd %d, expected %d",
                        block,tftpPrevBlock+1);
            TftpCount = -1;
#endif
            return(0);
        }
        TftpCount += count;
        tftpPrevBlock = block;
        data = (uchar *)(tftpp+4);

        /* If count is less than TFTP_DATAMAX, this must be the last
         * packet of the transfer, so clean up state here.
         */
        if(count < TFTP_DATAMAX) {
            enableBroadcastReception();
            tftpGotoState(TFTPIDLE);
        }

        /* Make sure the destination address for the data is not flash
         * or BSS space...
         */
        if(inUmonBssSpace((char *)TftpAddr,(char *)(TftpAddr+count))) {
            SendTFTPErr(ehdr,0,1,"TFTP can't write to uMon BSS space");
            TftpCount = -1;
            return(0);
        }

#if INCLUDE_FLASH
        if(InFlashSpace(TftpAddr,count)) {
            SendTFTPErr(ehdr,0,1,"TFTP can't write directly to flash");
            TftpCount = -1;
            return(0);
        }
#endif
        /* Copy data from enet buffer to TftpAddr location.
         * If netascii mode is active, then this transfer is much
         * slower...
         * If not netascii, then we use s_memcpy() because it does
         * a verification of each byte written and will abort as soon
         * as a failure is detected.
         */
        if(TftpWrqMode == MODE_NETASCII) {
            int tmpcount = count;

            while(tmpcount) {
                if(*data == 0x0d) {
                    data++;
                    tmpcount--;
                    TftpChopCount++;
                    continue;
                }

                *TftpAddr = *data;
                if(*TftpAddr != *data) {
                    SendTFTPErr(ehdr,0,1,"Write error at 0x%lx",
                                (ulong)TftpAddr);
                    TftpCount = -1;
                    return(0);
                }
                TftpAddr++;
                data++;
                tmpcount--;
            }
        } else {
            if(s_memcpy((char *)TftpAddr,(char *)data,count,0,0) != 0) {
                SendTFTPErr(ehdr,0,1,"Write error at 0x%lx",(ulong)TftpAddr);
                TftpCount = -1;
                return(0);
            }
            TftpAddr += count;
        }

        /* Check for transfer complete (count < TFTP_DATAMAX)... */
        if(count < TFTP_DATAMAX) {
            if(TftpTfsFname[0]) {
                char *fcomma, *icomma, *flags, *info;
                int err;

                /* If the transfer is complete and TftpTfsFname[0]
                 * is non-zero, then write the data to the specified
                 * TFS file... Note that a comma in the filename is
                 * used to find the start of (if any) the TFS flags
                 * string.  A second comma, marks the info field.
                 */
                info = (char *)0;
                flags = (char *)0;
                fcomma = strchr(TftpTfsFname,',');
                if(fcomma) {
                    icomma = strchr(fcomma+1,',');
                    if(icomma) {
                        *icomma = 0;
                        info = icomma+1;
                    }
                    if(tfsctrl(TFS_FATOB,(long)(fcomma+1),0) != -1) {
                        *fcomma = 0;
                        flags = fcomma+1;
                    } else {
                        SendTFTPErr(ehdr,0,1,"Invalid flag '%s'",
                                    TftpTfsFname);
                        TftpTfsFname[0] = 0;
                        break;
                    }
                }
#if INCLUDE_ETHERVERBOSE
                if((EtherVerbose & SHOW_TFTP_STATE) || (!MFLAGS_NOTFTPPRN())) {
                    printf("TFTP adding file: '%s' to TFS.\n",TftpTfsFname);
                }
#endif
                err = tfsadd(TftpTfsFname,info,flags,
                             (uchar *)getAppRamStart(),TftpCount+1-TftpChopCount);
                if(err != TFS_OKAY) {
                    SendTFTPErr(ehdr,0,1,"TFS err: %s",
                                (char *)tfsctrl(TFS_ERRMSG,err,0));
                }
                TftpTfsFname[0] = 0;
            } else {
                int cnt;
                char *addr;

                /* If the transfer is complete and no file add is to
                 * be done, then we flush d-cache and invalidate
                 * i-cache across the memory space that was just
                 * copied to.  This is necessary in case the
                 * binary data that was just transferred is code.
                 */
                cnt = TftpCount + 1;
                addr = (char *)TftpAddr - cnt;
                flushDcache(addr,cnt);
                invalidateIcache(addr,cnt);
            }
            if(!TftpGetActive) {
                shell_sprintf("TFTPRCV","%d",TftpCount+1);
            }
            tftpTransferComplete();
        }
        break;
    case ecs(TFTP_ACK):
        block = ecs(*(ushort *)(tftpp+2));
#if INCLUDE_ETHERVERBOSE
        if(EtherVerbose & SHOW_TFTP_STATE) {
            printf("  Rcvd TFTP_ACK (blk#%d)\n",block);
        }
#endif
        if((TftpState != TFTPACTIVE) && (TftpState != TFTPSENTWRQ)) {
            SendTFTPErr(ehdr,0,1,"Bad state (%s) for incoming TFTP_ACK",
                        tftpStringState(TftpState));
            return(0);
        }
        if(block == tftpPrevBlock) {
            if(TftpCount > TFTP_DATAMAX) {
                if(TftpState == TFTPACTIVE) {
                    TftpCount -= TFTP_DATAMAX;
                    TftpAddr += TFTP_DATAMAX;
                }
                SendTFTPData(ehdr,block+1,TftpAddr,TftpCount);
                if(TftpState == TFTPSENTWRQ) {
                    TftpCount -= TFTP_DATAMAX;
                    TftpAddr += TFTP_DATAMAX;
                }
                tftpPrevBlock++;
            } else if(TftpCount == TFTP_DATAMAX) {
                if(TftpState == TFTPACTIVE) {
                    TftpCount = 0;
                }
                SendTFTPData(ehdr,block+1,TftpAddr,TftpCount);
                if(TftpState == TFTPSENTWRQ) {
                    TftpCount = 0;
                }
                tftpPrevBlock++;
            } else {
                if(TftpState == TFTPSENTWRQ) {
                    if(TftpCount == -1) {
                        TftpCount = 0;
                        tftpGotoState(TFTPIDLE);
                        enableBroadcastReception();
                        tftpTransferComplete();
                    } else {
                        SendTFTPData(ehdr,block+1,TftpAddr,TftpCount);
                        TftpAddr += TftpCount;
                        TftpCount = -1;
                        tftpPrevBlock++;
                    }
                } else {
                    TftpAddr += TftpCount;
                    TftpCount = 0;
                    tftpGotoState(TFTPIDLE);
                    if(TftpLTMptr) {
                        free(TftpLTMptr);
                        TftpLTMptr = (char *)0;
                    }
                    enableBroadcastReception();
                    tftpTransferComplete();
                }
            }
        } else if(block == tftpPrevBlock-1) {
            SendTFTPData(ehdr,block+1,TftpAddr,TftpCount);
        } else {
#ifdef DONT_IGNORE_OUT_OF_SEQUENCE_BLOCKS
            SendTFTPErr(ehdr,0,1,"TFTP_ACK blockerr: rcvd %d, expected %d",
                        block,tftpPrevBlock-1);
            TftpCount = -1;
#endif
            return(0);
        }
        return(0);
    case ecs(TFTP_ERR):
        errcode = ecs(*(ushort *)(tftpp+2));
        errstring = tftpp+4;
#if INCLUDE_ETHERVERBOSE
        if(EtherVerbose & SHOW_TFTP_STATE) {
            printf("  Rcvd TFTP_ERR #%d (%s)\n",errcode,errstring);
        }
#endif
        TftpCount = -1;
        tftpGotoState(TFTPHOSTERROR);
        strncpy(TftpErrString,errstring,sizeof(TftpErrString)-1);
        TftpErrString[sizeof(TftpErrString)-1] = 0;
        return(0);
    default:
#if INCLUDE_ETHERVERBOSE
        if(EtherVerbose & SHOW_TFTP_STATE) {
            printf("  Rcvd <%04x> unknown TFTP opcode\n", opcode);
        }
#endif
        TftpCount = -1;
        return(-1);
    }
    SendTFTPAck(ehdr,block);
    return(0);
}

/* SendTFTPRRQ():
 *  Pass the ether and ip address of the TFTP server, along with the
 *  filename and mode to start up a target-initiated TFTP download.
 *  The initial TftpState value is TFTPSENTRRQ, this is done so that incoming
 *  TFTP_DAT packets can be verified...
 *   - If a TFTP_DAT packet is received and TftpState is TFTPSENTRRQ, then
 *     the block number should be 1.  If this is true, then that server's
 *     source port is stored away in TftpRmtPort so that all subsequent
 *     TFTP_DAT packets will be compared to the initial source port.  If no
 *     match, then respond with a TFTP error or ICMP PortUnreachable message.
 *   - If a TFTP_DAT packet is received and TftpState is TFTPSENTRRQ, then
 *     if the block number is not 1, generate a error.
 */
static int
SendTFTPRRQ(uchar *ipadd,uchar *eadd,char *filename,char *mode,uchar *loc)
{
    uchar *tftpdat;
    ushort ip_len;
    struct ether_header *te;
    struct ip *ti;
    struct Udphdr *tu;

    TftpChopCount = 0;
    tftpGotoState(TFTPSENTRRQ);
    TftpAddr = loc;
    TftpCount = 0;

    /* Retrieve an ethernet buffer from the driver and populate the
     * ethernet level of packet:
     */
    te = (struct ether_header *) getXmitBuffer();
    memcpy((char *)&te->ether_shost,(char *)BinEnetAddr,6);
    memcpy((char *)&te->ether_dhost,(char *)eadd,6);
    te->ether_type = ecs(ETHERTYPE_IP);

    /* Move to the IP portion of the packet and populate it appropriately: */
    ti = (struct ip *)(te + 1);
    ti->ip_vhl = IP_HDR_VER_LEN;
    ti->ip_tos = 0;
    ip_len = sizeof(struct ip) +
             sizeof(struct Udphdr) + strlen(filename) + strlen(mode) + 4;
    ti->ip_len = ecs(ip_len);
    ti->ip_id = ipId();
    ti->ip_off = 0;
    ti->ip_ttl = UDP_TTL;
    ti->ip_p = IP_UDP;
    memcpy((char *)&ti->ip_src.s_addr,(char *)BinIpAddr,4);
    memcpy((char *)&ti->ip_dst.s_addr,(char *)ipadd,4);

    /* Now udp... */
    tu = (struct Udphdr *)(ti + 1);
    tu->uh_sport = getTftpSrcPort();
    self_ecs(tu->uh_sport);
    tu->uh_dport = ecs(TftpPort);
    tu->uh_ulen = ecs((ushort)(ip_len - sizeof(struct ip)));

    /* Finally, the TFTP specific stuff... */
    tftpdat = (uchar *)(tu+1);
    *(ushort *)(tftpdat) = ecs(TFTP_RRQ);
    strcpy((char *)tftpdat+2,(char *)filename);
    strcpy((char *)tftpdat+2+strlen((char *)filename)+1,mode);

    if(!strcmp(mode,"netascii")) {
        TftpWrqMode = MODE_NETASCII;
    } else {
        TftpWrqMode = MODE_OCTET;
    }

    storePktAndSend(ti, te,TFTPACKSIZE+strlen(filename)+strlen(mode));

#if INCLUDE_ETHERVERBOSE
    if(EtherVerbose & SHOW_TFTP_STATE) {
        printf("\n  Sent TFTP_RRQ (file=%s)\n",filename);
    }
#endif

    return(0);
}


/* SendTFTPWRQ(): SSADDED
 *  Pass the ether and ip address of the TFTP server, along with the
 *  filename and mode to start up a target-initiated TFTP download.
 *  The initial TftpState value is TFTPSENTRRQ, this is done so that incoming
 *  TFTP_DAT packets can be verified...
 *   - If a TFTP_DAT packet is received and TftpState is TFTPSENTRRQ, then
 *     the block number should be 1.  If this is true, then that server's
 *     source port is stored away in TftpRmtPort so that all subsequent
 *     TFTP_DAT packets will be compared to the initial source port.  If no
 *     match, then respond with a TFTP error or ICMP PortUnreachable message.
 *   - If a TFTP_DAT packet is received and TftpState is TFTPSENTRRQ, then
 *     if the block number is not 1, generate a error.
 */
static int
SendTFTPWRQ(uchar *ipadd,uchar *eadd,char *filename,char *mode)
{
    uchar *tftpdat;
    ushort ip_len;
    struct ether_header *te;
    struct ip *ti;
    struct Udphdr *tu;

    TftpChopCount = 0;

    /* Retrieve an ethernet buffer from the driver and populate the
     * ethernet level of packet:
     */
    te = (struct ether_header *) getXmitBuffer();
    memcpy((char *)&te->ether_shost,(char *)BinEnetAddr,6);
    memcpy((char *)&te->ether_dhost,(char *)eadd,6);
    te->ether_type = ecs(ETHERTYPE_IP);

    /* Move to the IP portion of the packet and populate it appropriately: */
    ti = (struct ip *)(te + 1);
    ti->ip_vhl = IP_HDR_VER_LEN;
    ti->ip_tos = 0;
    ip_len = sizeof(struct ip) +
             sizeof(struct Udphdr) + strlen(filename) + strlen(mode) + 4;
    ti->ip_len = ecs(ip_len);
    ti->ip_id = ipId();
    ti->ip_off = 0;
    ti->ip_ttl = UDP_TTL;
    ti->ip_p = IP_UDP;
    memcpy((char *)&ti->ip_src.s_addr,(char *)BinIpAddr,4);
    memcpy((char *)&ti->ip_dst.s_addr,(char *)ipadd,4);

    /* Now udp... */
    tu = (struct Udphdr *)(ti + 1);
    tu->uh_sport = getTftpSrcPort();
    self_ecs(tu->uh_sport);
    tu->uh_dport = ecs(TftpPort);
    tu->uh_ulen = ecs((ushort)(ip_len - sizeof(struct ip)));

    /* Finally, the TFTP specific stuff... */
    tftpdat = (uchar *)(tu+1);
    *(ushort *)(tftpdat) = ecs(TFTP_WRQ);
    strcpy((char *)tftpdat+2,(char *)filename);
    strcpy((char *)tftpdat+2+strlen((char *)filename)+1,mode);

    if(!strcmp(mode,"netascii")) {
        TftpWrqMode = MODE_NETASCII;
    } else {
        TftpWrqMode = MODE_OCTET;
    }

    storePktAndSend(ti, te,TFTPACKSIZE+strlen(filename)+strlen(mode));

#if INCLUDE_ETHERVERBOSE
    if(EtherVerbose & SHOW_TFTP_STATE) {
        printf("\n  Sent TFTP_WRQ (file=%s)\n",filename);
    }
#endif

    tftpGotoState(TFTPSENTWRQ);
    return(0);
}
//ss END OF ADDED SendTRTPWRQ


/* SendTFTPAck():
 */
static int
SendTFTPAck(struct ether_header *re,ushort block)
{
    uchar *tftpdat;
    ushort ip_len;
    struct ether_header *te;
    struct ip *ti, *ri;
    struct Udphdr *tu, *ru;

    te = EtherCopy(re);

    ti = (struct ip *)(te + 1);
    ri = (struct ip *)(re + 1);
    ti->ip_vhl = ri->ip_vhl;
    ti->ip_tos = ri->ip_tos;
    ip_len = sizeof(struct ip) + sizeof(struct Udphdr) + 4;
    ti->ip_len = ecs(ip_len);
    ti->ip_id = ipId();
    ti->ip_off = ri->ip_off;
    ti->ip_ttl = UDP_TTL;
    ti->ip_p = IP_UDP;
    memcpy((char *)&(ti->ip_src.s_addr),(char *)&(ri->ip_dst.s_addr),
           sizeof(struct in_addr));
    memcpy((char *)&(ti->ip_dst.s_addr),(char *)&(ri->ip_src.s_addr),
           sizeof(struct in_addr));

    tu = (struct Udphdr *)(ti + 1);
    ru = (struct Udphdr *)(ri + 1);
    tu->uh_sport = ru->uh_dport;
    tu->uh_dport = ru->uh_sport;
    tu->uh_ulen = ecs((ushort)(ip_len - sizeof(struct ip)));

    tftpdat = (uchar *)(tu+1);
    *(ushort *)(tftpdat) = ecs(TFTP_ACK);
    *(ushort *)(tftpdat+2) = ecs(block);

    storePktAndSend(ti,te,TFTPACKSIZE);

#if INCLUDE_ETHERVERBOSE
    if(EtherVerbose & SHOW_TFTP_STATE) {
        printf("  Sent TFTP_ACK (blk#%d)\n",block);
    }
#endif
    return(0);
}

/* SendTFTPErr():
 */
static int
SendTFTPErr(struct ether_header *re,short errno,int changestate,char *fmt, ...)
{
    va_list argp;
    short len, tftplen, hdrlen;
    uchar *tftpmsg;
    struct ether_header *te;
    struct ip *ti, *ri;
    struct Udphdr *tu, *ru;
    static char errmsg[128];

    if(changestate) {
        tftpGotoState(TFTPERROR);
    }

    va_start(argp,fmt);
    vsnprintf(errmsg,sizeof(errmsg),fmt,argp);
    va_end(argp);

#if INCLUDE_ETHERVERBOSE
    if((EtherVerbose & SHOW_TFTP_STATE) || (!MFLAGS_NOTFTPPRN())) {
        printf("TFTP err: %s\n",errmsg);
    }
#endif

    tftplen = strlen(errmsg) + 1 + 4;
    hdrlen = sizeof(struct ip) + sizeof(struct Udphdr);
    len = tftplen + hdrlen ;

    te = EtherCopy(re);

    ti = (struct ip *)(te + 1);
    ri = (struct ip *)(re + 1);
    ti->ip_vhl = ri->ip_vhl;
    ti->ip_tos = ri->ip_tos;
    ti->ip_len = ecs(len);
    ti->ip_id = ipId();
    ti->ip_off = ri->ip_off;
    ti->ip_ttl = UDP_TTL;
    ti->ip_p = IP_UDP;
    memcpy((char *)&(ti->ip_src.s_addr),(char *)&(ri->ip_dst.s_addr),
           sizeof(struct in_addr));
    memcpy((char *)&(ti->ip_dst.s_addr),(char *)&(ri->ip_src.s_addr),
           sizeof(struct in_addr));

    tu = (struct Udphdr *)(ti + 1);
    ru = (struct Udphdr *)(ri + 1);
    tu->uh_sport = ru->uh_dport;
    tu->uh_dport = ru->uh_sport;
    tu->uh_ulen = sizeof(struct Udphdr) + tftplen;
    self_ecs(tu->uh_ulen);

    tftpmsg = (uchar *)(tu+1);
    *(ushort *)(tftpmsg) = ecs(TFTP_ERR);
    * (ushort *)(tftpmsg+2) = ecs(errno);
    strcpy((char *)tftpmsg+4,(char *)errmsg);

    storePktAndSend(ti,te,TFTPACKSIZE + strlen(errmsg) + 1);

#if INCLUDE_ETHERVERBOSE
    if(EtherVerbose & SHOW_TFTP_STATE) {
        printf("  Sent TFTP Err#%d (%s) \n",errno,errmsg);
    }
#endif

    return(0);
}

/* SendTFTPData():
 */
static int
SendTFTPData(struct ether_header *re,ushort block,uchar *data,int count)
{
    int len, tftplen, hdrlen;
    uchar *tftpmsg;
    struct ether_header *te;
    struct ip *ti, *ri;
    struct Udphdr *tu, *ru;

    if(count > TFTP_DATAMAX) {
        count = TFTP_DATAMAX;
    }

    tftplen = count + 2 + 2;    /* sizeof (data + opcode + blockno) */
    hdrlen = sizeof(struct ip) + sizeof(struct Udphdr);
    len = tftplen + hdrlen ;

    te = EtherCopy(re);

    ti = (struct ip *)(te + 1);
    ri = (struct ip *)(re + 1);
    ti->ip_vhl = ri->ip_vhl;
    ti->ip_tos = ri->ip_tos;
    ti->ip_len = ecs(len);
    ti->ip_id = ipId();
    ti->ip_off = ri->ip_off;
    ti->ip_ttl = UDP_TTL;
    ti->ip_p = IP_UDP;
    memcpy((char *)&(ti->ip_src.s_addr),(char *)&(ri->ip_dst.s_addr),
           sizeof(struct in_addr));
    memcpy((char *)&(ti->ip_dst.s_addr),(char *)&(ri->ip_src.s_addr),
           sizeof(struct in_addr));

    tu = (struct Udphdr *)(ti + 1);
    ru = (struct Udphdr *)(ri + 1);
    tu->uh_sport = ru->uh_dport;
    tu->uh_dport = ru->uh_sport;
    tu->uh_ulen = sizeof(struct Udphdr) + tftplen;
    self_ecs(tu->uh_ulen);

    tftpmsg = (uchar *)(tu+1);
    *(ushort *)(tftpmsg) = ecs(TFTP_DAT);
    *(ushort *)(tftpmsg+2) = ecs(block);
    memcpy((char *)tftpmsg+4,(char *)data,count);

    len+=sizeof(struct ether_header);

    storePktAndSend(ti,te,len);

#if INCLUDE_ETHERVERBOSE
    if(EtherVerbose & SHOW_TFTP_STATE)
        printf("  Sent TFTP data blk#%d (%d bytes @ 0x%lx) \n",
               block,count,(ulong)data);
#endif
    return(0);
}

#if 0
THIS IS NOT YET TESTED
/* parseLongFilename():
 * Given a full filename that may contain up to three different
 * comma-delimited tokens, this function will parse that string and
 * return the requested tokens.
 *
 * The syntax of fullname can be...
 *  - name
 *  - name,flags
 *  - name,flags,info
 *  - name,,info
 *
 * Return 0 if successful, -1 if failure.
 */
int
parseLongFilename(char *fullname,char *name, char *flags, char *info)
{
    char    *comma, *cp;

    /* Parse the fullname string looking for commas to determine what
     * information is in the string.  Then, based on the presence of
     * the commas, populate the incoming pointers with the appropriate
     * portion of the fullname string.
     * If the pointer is NULL, then just skip over that portion of the
     * algorithm.
     */
    cp = fullname;
    comma = strchr(cp,',');
    if(comma) {
        if(name) {
            while(cp != comma) {
                *name++ = *cp++;
            }
            *name = 0;
        }
        cp++;
        comma = strchr(cp,',');
        if(comma) {
            if(flags) {
                while(cp != comma) {
                    *flags++ = *cp++;
                }
                *flags = 0;
            }
            cp++;
            if(info) {
                strcpy(info,cp);
            }
        } else {
            if(flags) {
                strcpy(flags,cp);
            }
        }
    } else {
        if(name) {
            strcpy(name,fullname);
        }
    }
    return(0);
}
#endif

/* Tftp():
 *  Initiate a tftp transfer at the target (target is client).
 *  Command line:
 *      tftp [options] {IP} {get|put} {file|addr} [len]...
 *      tftp [options] {IP} get file dest_addr
 *      tftp [options] {IP} put addr dest_file len
 *  Currently, only "get" is supported.
 */

char *TftpHelp[] = {
    "Trivial file transfer protocol",
    "-[aF:f:i:vV] [on|off|IP] {get|put filename [addr]} ss",
#if INCLUDE_VERBOSEHELP
    " -a        use netascii mode",
    " -F {file} name of tfs file to copy to",
    " -f {flgs} file flags (see tfs)",
    " -i {info} file info (see tfs)",
    " -v        verbosity = ticker",
    " -V        verbosity = state",
#endif
    0,
};

int
Tftp(int argc,char *argv[])
{
    int     opt, verbose;
    char    *mode, *file, *info, *flags;
    ulong   addr;

    verbose = 0;
    file = (char *)0;
    info = (char *)0;
    flags = (char *)0;
    mode = "octet";
    while((opt=getopt(argc,argv,"aF:f:i:vV")) != -1) {
        switch(opt) {
        case 'a':
            mode = "netascii";
            break;
        case 'f':
            flags = optarg;
            break;
        case 'F':
            file = optarg;
            break;
        case 'i':
            info = optarg;
            break;
        case 'v':
            verbose |= SHOW_TFTP_TICKER;
            break;
        case 'V':
            verbose |= SHOW_TFTP_STATE;
            break;
        default:
            return(CMD_PARAM_ERROR);
        }
    }
    if(argc < (optind+1)) {
        return(CMD_PARAM_ERROR);
    }

    if(argc == optind+1) {
        if(!strcmp(argv[optind],"on")) {
            TftpTurnedOff = 0;
            TftpGetActive = 0;
            TftpPutActive = 0; //ss ADDED
        } else if(!strcmp(argv[optind],"off")) {
            TftpTurnedOff = 1;
            TftpGetActive = 0;
            tftpGotoState(TFTPIDLE);
            TftpPutActive = 0; //ss ADDED
        } else {
            return(CMD_PARAM_ERROR);
        }
        return(CMD_SUCCESS);
    }

    /* If either the info or flags field has been specified, but the */
    /* filename is not specified, error here... */
    if((info || flags) && (!file)) {
        printf("Filename missing\n");
        return(CMD_FAILURE);
    }

    if(!strcmp(argv[optind+1],"get")) {

        if(argc == optind+4) {
            addr = (ulong)strtol(argv[optind+3],0,0);
        } else if(argc == optind+3) {
            addr = getAppRamStart();
        } else {
            return(CMD_PARAM_ERROR);
        }

        TFTPVERBOSE(EtherVerbose |= verbose);
        tftpGet(addr,argv[optind],mode,argv[optind+2],file,flags,info);
        TFTPVERBOSE(EtherVerbose &= ~verbose);
    } else if(!strcmp(argv[optind+1],"put")) {

        if(argc == (optind+3)) {
            file = argv[optind+2];
        } else if(argc == (optind+4)) {
            file = argv[optind+3];
        } else {
            return(CMD_PARAM_ERROR);
        }

        TFTPVERBOSE(EtherVerbose |= verbose);
        tftpPut(argv[optind],mode,file,argv[optind+2],flags,info);
        TFTPVERBOSE(EtherVerbose &= ~verbose);

    } else {
        return(CMD_PARAM_ERROR);
    }

    return(CMD_SUCCESS);
}

void
ShowTftpStats(void)
{
    printf("Current TFTP state: %s\n",tftpStringState(TftpState));
}

#endif