aboutsummaryrefslogtreecommitdiff
path: root/networking/tftp.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-03-21 18:29:01 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-03-21 18:29:01 +0000
commit4824ccae8ef0896b9fcb2a15ffa666d3c2aa8a89 (patch)
tree305bc282125eeb2c1cbfeb89fa870be634c996bc /networking/tftp.c
parent0dc490bda21c85aee9b7462d0cf401d03a94c06d (diff)
downloadbusybox-w32-4824ccae8ef0896b9fcb2a15ffa666d3c2aa8a89.tar.gz
busybox-w32-4824ccae8ef0896b9fcb2a15ffa666d3c2aa8a89.tar.bz2
busybox-w32-4824ccae8ef0896b9fcb2a15ffa666d3c2aa8a89.zip
*: fix build problems found with randomconfig
Diffstat (limited to 'networking/tftp.c')
-rw-r--r--networking/tftp.c73
1 files changed, 40 insertions, 33 deletions
diff --git a/networking/tftp.c b/networking/tftp.c
index bd9dc9b63..143279757 100644
--- a/networking/tftp.c
+++ b/networking/tftp.c
@@ -25,10 +25,11 @@
25 25
26#if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT 26#if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT
27 27
28#define TFTP_BLKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */ 28#define TFTP_BLKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */
29#define TFTP_TIMEOUT_MS 50 29#define TFTP_BLKSIZE_DEFAULT_STR "512"
30#define TFTP_MAXTIMEOUT_MS 2000 30#define TFTP_TIMEOUT_MS 50
31#define TFTP_NUM_RETRIES 12 /* number of backed-off retries */ 31#define TFTP_MAXTIMEOUT_MS 2000
32#define TFTP_NUM_RETRIES 12 /* number of backed-off retries */
32 33
33/* opcodes we support */ 34/* opcodes we support */
34#define TFTP_RRQ 1 35#define TFTP_RRQ 1
@@ -181,7 +182,7 @@ static int tftp_protocol(
181 int open_mode, local_fd; 182 int open_mode, local_fd;
182 int retries, waittime_ms; 183 int retries, waittime_ms;
183 int io_bufsize = blksize + 4; 184 int io_bufsize = blksize + 4;
184 char *cp = cp; /* for compiler */ 185 char *cp;
185 /* Can't use RESERVE_CONFIG_BUFFER here since the allocation 186 /* Can't use RESERVE_CONFIG_BUFFER here since the allocation
186 * size varies meaning BUFFERS_GO_ON_STACK would fail */ 187 * size varies meaning BUFFERS_GO_ON_STACK would fail */
187 /* We must keep the transmit and receive buffers seperate */ 188 /* We must keep the transmit and receive buffers seperate */
@@ -193,6 +194,7 @@ static int tftp_protocol(
193 setsockopt_reuseaddr(socket_fd); 194 setsockopt_reuseaddr(socket_fd);
194 195
195 block_nr = 1; 196 block_nr = 1;
197 cp = xbuf + 2;
196 198
197 if (!ENABLE_TFTP || our_lsa) { 199 if (!ENABLE_TFTP || our_lsa) {
198 /* tftpd */ 200 /* tftpd */
@@ -216,17 +218,6 @@ static int tftp_protocol(
216 block_nr = 0; 218 block_nr = 0;
217 } 219 }
218 220
219#if ENABLE_FEATURE_TFTP_BLOCKSIZE
220 if (blksize != TFTP_BLKSIZE_DEFAULT) {
221 /* Create and send OACK packet */
222 /* For the download case, block_nr is still 1 -
223 * we expect 1st ACK from peer to be for (block_nr-1),
224 * that is, for "block 0" which is our OACK pkt */
225 opcode = TFTP_OACK;
226 cp = xbuf + 2;
227 /* to be continued, see below */
228 }
229#endif
230 if (user_opt) { 221 if (user_opt) {
231 struct passwd *pw = getpwnam(user_opt); 222 struct passwd *pw = getpwnam(user_opt);
232 if (!pw) 223 if (!pw)
@@ -263,11 +254,19 @@ static int tftp_protocol(
263 if (!ENABLE_TFTP || our_lsa) { 254 if (!ENABLE_TFTP || our_lsa) {
264#if ENABLE_FEATURE_TFTP_BLOCKSIZE 255#if ENABLE_FEATURE_TFTP_BLOCKSIZE
265 if (blksize != TFTP_BLKSIZE_DEFAULT) { 256 if (blksize != TFTP_BLKSIZE_DEFAULT) {
266 /* Create and send OACK packet. continued */ 257 /* Create and send OACK packet. */
258 /* For the download case, block_nr is still 1 -
259 * we expect 1st ACK from peer to be for (block_nr-1),
260 * that is, for "block 0" which is our OACK pkt */
261 opcode = TFTP_OACK;
267 goto add_blksize_opt; 262 goto add_blksize_opt;
268 } 263 }
269#endif 264#endif
270 } else { 265 }
266 else {
267/* Removing it, or using if() statement instead may lead to
268 * "warning: null argument where non-null required": */
269#if ENABLE_TFTP
271 /* tftp */ 270 /* tftp */
272 271
273 /* We can't (and don't really need to) bind the socket: 272 /* We can't (and don't really need to) bind the socket:
@@ -286,7 +285,6 @@ static int tftp_protocol(
286 if (CMD_GET(option_mask32)) { 285 if (CMD_GET(option_mask32)) {
287 opcode = TFTP_RRQ; 286 opcode = TFTP_RRQ;
288 } 287 }
289 cp = xbuf + 2;
290 /* add filename and mode */ 288 /* add filename and mode */
291 /* fill in packet if the filename fits into xbuf */ 289 /* fill in packet if the filename fits into xbuf */
292 len = strlen(remote_file) + 1; 290 len = strlen(remote_file) + 1;
@@ -301,19 +299,24 @@ static int tftp_protocol(
301 cp += sizeof("octet"); 299 cp += sizeof("octet");
302 300
303#if ENABLE_FEATURE_TFTP_BLOCKSIZE 301#if ENABLE_FEATURE_TFTP_BLOCKSIZE
304 if (blksize != TFTP_BLKSIZE_DEFAULT) { 302 if (blksize == TFTP_BLKSIZE_DEFAULT)
305 /* rfc2348 says that 65464 is a max allowed value */ 303 goto send_pkt;
306 if ((&xbuf[io_bufsize - 1] - cp) < sizeof("blksize NNNNN")) { 304
307 bb_error_msg("remote filename is too long"); 305 /* Non-standard blocksize: add option to pkt */
308 goto ret; 306 if ((&xbuf[io_bufsize - 1] - cp) < sizeof("blksize NNNNN")) {
309 } 307 bb_error_msg("remote filename is too long");
310 want_option_ack = 1; 308 goto ret;
311 add_blksize_opt:
312 /* add "blksize", <nul>, blksize, <nul> */
313 strcpy(cp, "blksize");
314 cp += sizeof("blksize");
315 cp += snprintf(cp, 6, "%d", blksize) + 1;
316 } 309 }
310 want_option_ack = 1;
311#endif
312#endif /* ENABLE_TFTP */
313
314#if ENABLE_FEATURE_TFTP_BLOCKSIZE
315 add_blksize_opt:
316 /* add "blksize", <nul>, blksize, <nul> */
317 strcpy(cp, "blksize");
318 cp += sizeof("blksize");
319 cp += snprintf(cp, 6, "%d", blksize) + 1;
317#endif 320#endif
318 /* First packet is built, so skip packet generation */ 321 /* First packet is built, so skip packet generation */
319 goto send_pkt; 322 goto send_pkt;
@@ -540,7 +543,9 @@ int tftp_main(int argc ATTRIBUTE_UNUSED, char **argv)
540 len_and_sockaddr *peer_lsa; 543 len_and_sockaddr *peer_lsa;
541 const char *local_file = NULL; 544 const char *local_file = NULL;
542 const char *remote_file = NULL; 545 const char *remote_file = NULL;
543 const char *blksize_str = "512"; 546#if ENABLE_FEATURE_TFTP_BLOCKSIZE
547 const char *blksize_str = TFTP_BLKSIZE_DEFAULT_STR;
548#endif
544 int blksize; 549 int blksize;
545 int result; 550 int result;
546 int port; 551 int port;
@@ -567,6 +572,8 @@ int tftp_main(int argc ATTRIBUTE_UNUSED, char **argv)
567 //bb_error_msg("bad block size"); 572 //bb_error_msg("bad block size");
568 return EXIT_FAILURE; 573 return EXIT_FAILURE;
569 } 574 }
575#else
576 blksize = TFTP_BLKSIZE_DEFAULT;
570#endif 577#endif
571 578
572 if (!local_file) 579 if (!local_file)
@@ -647,7 +654,7 @@ int tftpd_main(int argc ATTRIBUTE_UNUSED, char **argv)
647 opcode = ntohs(*(uint16_t*)block_buf); 654 opcode = ntohs(*(uint16_t*)block_buf);
648 if (result < 4 || result >= sizeof(block_buf) 655 if (result < 4 || result >= sizeof(block_buf)
649 || block_buf[result-1] != '\0' 656 || block_buf[result-1] != '\0'
650 || (USE_FEATURE_TFTP_GET(opcode != TFTP_RRQ) /* not download */ 657 || (USE_FEATURE_TFTP_PUT(opcode != TFTP_RRQ) /* not download */
651 USE_GETPUT(&&) 658 USE_GETPUT(&&)
652 USE_FEATURE_TFTP_GET(opcode != TFTP_WRQ) /* not upload */ 659 USE_FEATURE_TFTP_GET(opcode != TFTP_WRQ) /* not upload */
653 ) 660 )