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
|
/****************************************************************************
* netuils/tftp/tftpc_put.c
*
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 NuttX 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 OWNER 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, TFTP_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.
*
****************************************************************************/
/****************************************************************************
* Compilation Switches
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <debug.h>
#include <net/uip/uipopt.h>
#include <net/uip/uip.h>
#include <net/uip/tftp.h>
#include "tftpc_internal.h"
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
/****************************************************************************
* Definitions
****************************************************************************/
#define TFTP_RETRIES 3
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: tftp_incrndx
****************************************************************************/
#if CONFIG_NETUTILS_TFTP_ACKPACKETS > 1
static inline int tftp_incrndx(int ndx)
{
if (++ndx >= TFTP_MAXACKPACKETS)
{
ndx = 0;
}
return ndx;
}
#endif
/****************************************************************************
* Name: tftp_read
****************************************************************************/
static inline ssize_t tftp_read(int fd, ubyte *buf, size_t buflen)
{
ssize_t nbytesread;
ssize_t totalread = 0;
while (totalread < buflen)
{
/* Read the data... repeating the read in the event that it was
* interrupted by a signal.
*/
do
{
nbytesread = read(fd, buf, buflen - totalread);
}
while (nbytesread < 0 && errno == EINTR);
/* Check for non-EINTR errors */
if (nbytesread < 0)
{
ndbg(g_tftpcallfailed, "read", errno);
return ERROR;
}
/* Check for end of file */
else if (nbytesread == 0)
{
break;
}
/* Handle partial reads. Partial reads can happen normally
* when the source is some device driver that returns data
* in bits and pieces as received (such as a pipe)
*/
totalread += nbytesread;
buf += nbytesread;
}
return totalread;
}
/****************************************************************************
* Name: tftp_mkdatapacket
*
* Description:
* DATA message format:
*
* 2 bytes: Opcode (network order == big-endian)
* 2 bytes: Block number (network order == big-endian)
* N bytes: Data (where N <= 512)
*
* Input Parameters:
* fd - File descriptor used to read from the file
* offset - File offset to read from
* packet - Buffer to write the data packet into
* blockno - The block number of the packet
*
* Return Value:
* Number of bytes read into the packet. <TFTP_PACKETSIZE means end of file;
* <1 if an error occurs.
*
****************************************************************************/
int tftp_mkdatapacket(int fd, off_t offset, ubyte *packet, uint16 blockno)
{
off_t tmp;
int nbytesread;
/* Format the DATA message header */
packet[0] = TFTP_DATA >> 8;
packet[1] = TFTP_DATA & 0xff;
packet[2] = blockno >> 8;
packet[3] = blockno & 0xff;
/* Seek to the correct offset in the file */
tmp = lseek(fd, offset, SEEK_SET);
if (tmp == (off_t)-1)
{
ndbg(g_tftpcallfailed, "lseek", errno);
return ERROR;
}
/* Read the file data into the packet buffer */
nbytesread = tftp_read(fd, &packet[TFTP_DATAHEADERSIZE], TFTP_DATASIZE);
if (nbytesread < 0)
{
return ERROR;
}
return nbytesread + TFTP_DATAHEADERSIZE;
}
/****************************************************************************
* Name: tftp_rcvack
*
* Description:
* ACK message format:
*
* 2 bytes: Opcode (network order == big-endian)
* 2 bytes: Block number (network order == big-endian)
*
* Input Parameters:
* sd - Socket descriptor to use in in the transfer
* packet - buffer to use for the tranfers
* server - The address of the server
* port - The port number of the server (0 if not yet known)
* blockno - The block number of the ACK
*
* Returned Value:
* OK:success and blockno valid, ERROR:failure.
*
****************************************************************************/
static int tftp_rcvack(int sd, ubyte *packet, struct sockaddr_in *server,
uint16 *port, uint16 *blockno)
{
struct sockaddr_in from; /* The address the last UDP message recv'd from */
ssize_t nbytes; /* The number of bytes received. */
uint16 opcode; /* The received opcode */
uint16 rblockno; /* The received block number */
int packetlen; /* Packet length */
int retry; /* Retry counter */
/* Try up to TFTP_RETRIES times */
for (retry = 0; retry < TFTP_RETRIES; retry++)
{
/* Try for until a valid ACK is received or some error occurs */
for (;;)
{
/* Receive the next UDP packet from the server */
nbytes = tftp_recvfrom(sd, packet, TFTP_IOBUFSIZE, &from);
if (nbytes >= TFTP_ACKHEADERSIZE)
{
/* Get the port being used by the server if that has not yet been established */
if (!*port)
{
*port = from.sin_port;
server->sin_port = from.sin_port;
}
/* Verify that the packet was received from the correct host and port */
if (server->sin_addr.s_addr != from.sin_addr.s_addr)
{
nvdbg(g_tftpaddress, "recvfrom");
continue;
}
if (*port != server->sin_port)
{
nvdbg(g_tftpport, "recvfrom");
packetlen = tftp_mkerrpacket(packet, TFTP_ERR_UNKID, TFTP_ERRST_UNKID);
(void)tftp_sendto(sd, packet, packetlen, server);
continue;
}
/* Parse the error message */
opcode = (uint16)packet[0] << 8 | (uint16)packet[1];
rblockno = (uint16)packet[2] << 8 | (uint16)packet[3];
/* Verify that the message that we received is an ACK for the
* expected block number.
*/
if (opcode != TFTP_ACK)
{
nvdbg("Bad opcode%d\n");
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
if (opcode == TFTP_ERR)
{
(void)tftp_parseerrpacket(packet);
}
else
#endif
if (opcode > TFTP_MAXRFC1350)
{
packetlen = tftp_mkerrpacket(packet, TFTP_ERR_ILLEGALOP, TFTP_ERRST_ILLEGALOP);
(void)tftp_sendto(sd, packet, packetlen, server);
}
/* Break out an bump up the retry count */
break;
}
/* Success! */
nvdbg("Received ACK for block %d\n", rblockno);
*blockno = rblockno;
return OK;
}
}
}
/* We have tried TFTP_RETRIES times */
ndbg("Timeout, No ACK for block %d\n", blockno);
return ERROR; /* Will never get here */
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: tftpput
*
* Input Parameters:
* local - Path to the file system object to be sent.
* remote - The name of the file on the TFTP server.
* addr - The IP address of the server in network order
* binary - TRUE: Perform binary ('octect') transfer
* FALSE: Perform text ('netascii') transfer
*
****************************************************************************/
int tftpput(const char *local, const char *remote, in_addr_t addr, boolean binary)
{
struct sockaddr_in server; /* The address of the TFTP server */
boolean eof = FALSE; /* TRUE: at end of file */
ubyte *packet; /* Allocated memory to hold one packet */
uint16 blockno = 0; /* The current transfer block number */
uint16 rblockno; /* The ACK'ed block number */
uint16 port = 0; /* This is the port number for the transfer */
int packetlen; /* The length of the data packet */
int sd; /* Socket descriptor for socket I/O */
int fd; /* File descriptor for file I/O */
int result = ERROR; /* Assume failure */
int ret; /* Generic return status */
#if CONFIG_NETUTILS_TFTP_ACKPACKETS > 1
off_t offsets[TFTP_MAXACKPACKETS]; /* Offsets into source file (circular) */
int head; /* Head index into offsets[] */
int tail; /* Tail index into offsets[] */
int hblockno; /* Block number at the head of offsets[] */
int tmp; /* For temporary usage */
#else
off_t offset; /* Offset into source file */
off_t next; /* Offset to the next block */
#endif
/* Allocate the buffer to used for socket/disk I/O */
packet = (ubyte*)zalloc(TFTP_IOBUFSIZE);
if (!packet)
{
ndbg(g_tftpnomemory, "packet");
errno = ENOMEM;
goto errout;
}
/* Open the file for reading */
fd = open(local, O_RDONLY);
if (fd < 0)
{
ndbg(g_tftpcallfailed, "open", errno);
goto errout_with_packet;
}
/* Initialize a UDP socket and setup the server addresss */
sd = tftp_sockinit(&server, addr);
if (sd < 0)
{
goto errout_with_fd;
}
/* Send the write request */
packetlen = tftp_mkreqpacket(packet, TFTP_WRQ, remote, binary);
ret = tftp_sendto(sd, packet, packetlen, &server);
if (ret != packetlen)
{
goto errout_with_sd;
}
/* Receive the ACK for the write request */
(void)tftp_rcvack(sd, packet, &server, &port, NULL);
/* Then loop sending the entire file to the server in chunks */
#if CONFIG_NETUTILS_TFTP_ACKPACKETS > 1
head = 0;
tail = 0;
offsets[0] = 0;
hblockno = 1;
#else
offset = 0;
next = 0;
#endif
for (;;)
{
if (!eof)
{
#if CONFIG_NETUTILS_TFTP_ACKPACKETS > 1
/* Construct the next data packet */
packetlen = tftp_mkdatapacket(fd, offsets[tail], packet, blockno);
if (packetlen < 0)
{
goto errout_with_sd;
}
/* Check for end of file */
if (packetlen < TFTP_PACKETSIZE)
{
eof = TRUE;
}
/* Update counts */
blockno++;
/* Increment the tail (and probably the tail) index of the
* cicular offset list.
*/
tmp = tail;
tail = tftp_incrndx(tail);
/* Make sure that incrementing the tail doesn't make the
* buffer appear empty.
*/
if (head == tail)
{
head = tftp_incrndx(head);
hblockno++;
}
/* Now calculate the next file offset */
offsets[tail] = offsets[tmp] + packetlen;
#else
/* Construct the next data packet */
packetlen = tftp_mkdatapacket(fd, offset, packet, blockno);
if (packetlen < 0)
{
goto errout_with_sd;
}
/* Check for end of file */
if (packetlen < TFTP_PACKETSIZE)
{
eof = TRUE;
}
/* Now calculate the next file offset */
next = offset + packetlen;
#endif
/* Send the next data block */
ret = tftp_sendto(sd, packet, packetlen, &server);
if (ret != packetlen)
{
goto errout_with_sd;
}
}
/* Check for an ACK for any of the preceding data chunks */
#if CONFIG_NETUTILS_TFTP_ACKPACKETS > 1
if (blockno - hblockno >= CONFIG_NETUTILS_TFTP_ACKPACKETS || eof)
{
int ndx;
/* Get the next ACK from the wire */
if (tftp_rcvack(sd, packet, &server, &port, &rblockno) == OK)
{
while (hblockno < rblockno && tail != head)
{
head = tftp_incrndx(head);
hblockno++;
}
}
/* If we are at the end of the file and if all of the packets
* have been ACKed, then we are done.
*/
if (eof && head == tail)
{
break;
}
/* Otherwise, resend all un-acknowledged packets */
for (ndx = head, rblockno = hblockno;
ndx != tail;
ndx = tftp_incrndx(ndx), hblockno++)
{
/* Format the data packet, re-reading the data from the file */
packetlen = tftp_mkdatapacket(fd, offsets[ndx], packet, rblockno);
if (packetlen < 0)
{
goto errout_with_sd;
}
/* Re-send the data packet */
ret = tftp_sendto(sd, packet, packetlen, &server);
if (ret != packetlen)
{
goto errout_with_sd;
}
}
}
#else
/* Get the next ACK from the wire */
if (tftp_rcvack(sd, packet, &server, &port, &rblockno) == OK)
{
/* If we are at the end of the file and if all of the packets
* have been ACKed, then we are done.
*/
if (eof)
{
break;
}
/* Check if the packet that we just sent was ACK'ed. If not,
* we just loop to resend the same packet (same blockno, same
* file offset.
*/
if (rblockno == blockno)
{
/* Yes.. set up for the next block */
blockno++;
offset = next;
}
}
#endif
}
/* Return success */
result = OK;
errout_with_sd:
close(sd);
errout_with_fd:
close(fd);
errout_with_packet:
free(packet);
errout:
return result;
}
#endif /* CONFIG_NET && CONFIG_NET_UDP && CONFIG_NFILE_DESCRIPTORS > 0 */
|