#define TFTP_ERROR 5
#define TFTP_OACK 6
-static const char *tftp_error_msg[] = {
+static const char *tftp_bb_error_msg[] = {
"Undefined error",
"File not found",
"Access violation",
if ((bufsize && (blocksize > bufsize)) ||
(blocksize < 8) || (blocksize > 65464)) {
- error_msg("bad blocksize");
+ bb_error_msg("bad blocksize");
return 0;
}
tftp_bufsize += 4;
if ((socketfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
- perror_msg("socket");
+ bb_perror_msg("socket");
return EXIT_FAILURE;
}
}
if (too_long || ((&buf[tftp_bufsize - 1] - cp) < 6)) {
- error_msg("too long remote-filename");
+ bb_error_msg("too long remote-filename");
break;
}
if (len != TFTP_BLOCKSIZE_DEFAULT) {
if ((&buf[tftp_bufsize - 1] - cp) < 15) {
- error_msg("too long remote-filename");
+ bb_error_msg("too long remote-filename");
break;
}
len = read(localfd, cp, tftp_bufsize - 4);
if (len < 0) {
- perror_msg("read");
+ bb_perror_msg("read");
break;
}
}
cp += len;
- } else if (finished) {
- break;
}
}
/* send packet */
+ timeout = bb_tftp_num_retries; /* re-initialize */
do {
len = cp - buf;
#endif
if (sendto(socketfd, buf, len, 0,
(struct sockaddr *) &sa, sizeof(sa)) < 0) {
- perror_msg("send");
+ bb_perror_msg("send");
len = -1;
break;
}
- /* receive packet */
+ if (finished) {
+ break;
+ }
+ /* receive packet */
memset(&from, 0, sizeof(from));
fromlen = sizeof(from);
(struct sockaddr *) &from, &fromlen);
if (len < 0) {
- perror_msg("recvfrom");
+ bb_perror_msg("recvfrom");
break;
}
timeout = bb_tftp_num_retries;
case 0:
- error_msg("timeout");
+ bb_error_msg("timeout");
if (timeout == 0) {
len = -1;
- error_msg("last timeout");
+ bb_error_msg("last timeout");
} else {
timeout--;
}
break;
default:
- perror_msg("select");
+ bb_perror_msg("select");
len = -1;
}
} while (timeout && (len >= 0));
- if (len < 0) {
+ if ((finished) || (len < 0)) {
break;
}
if (buf[4] != '\0') {
msg = &buf[4];
buf[tftp_bufsize - 1] = '\0';
- } else if (tmp < (sizeof(tftp_error_msg)
+ } else if (tmp < (sizeof(tftp_bb_error_msg)
/ sizeof(char *))) {
- msg = (char *) tftp_error_msg[tmp];
+ msg = (char *) tftp_bb_error_msg[tmp];
}
if (msg) {
- error_msg("server says: %s", msg);
+ bb_error_msg("server says: %s", msg);
}
break;
"blksize");
if (res) {
- int foo = atoi(res);
+ int blksize = atoi(res);
- if (tftp_blocksize_check(foo,
+ if (tftp_blocksize_check(blksize,
tftp_bufsize - 4)) {
if (cmd_put) {
opcode = TFTP_ACK;
}
#ifdef CONFIG_FEATURE_TFTP_DEBUG
- printf("using blksize %u\n");
+ printf("using blksize %u\n", blksize);
#endif
- tftp_bufsize = foo + 4;
+ tftp_bufsize = blksize + 4;
block_nr = 0;
continue;
}
}
/* FIXME:
* we should send ERROR 8 */
- error_msg("bad server option");
+ bb_error_msg("bad server option");
break;
}
- error_msg("warning: blksize not supported by server"
+ bb_error_msg("warning: blksize not supported by server"
" - reverting to 512");
tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4;
len = write(localfd, &buf[4], len - 4);
if (len < 0) {
- perror_msg("write");
+ bb_perror_msg("write");
break;
}
#ifdef CONFIG_FEATURE_CLEAN_UP
close(socketfd);
- RELEASE_CONFIG_BUFFER(buf);
+ free(buf);
#endif
return finished ? EXIT_SUCCESS : EXIT_FAILURE;
break;
#endif
case 'l':
- localfile = xstrdup(optarg);
+ localfile = bb_xstrdup(optarg);
break;
case 'r':
- remotefile = xstrdup(optarg);
+ remotefile = bb_xstrdup(optarg);
break;
}
}
if ((cmd == 0) || (optind == argc)) {
- show_usage();
+ bb_show_usage();
+ }
+ if(localfile && strcmp(localfile, "-") == 0) {
+ fd = fileno((cmd==tftp_cmd_get)? stdout : stdin);
+ }
+ if(localfile == NULL)
+ localfile = remotefile;
+ if(remotefile == NULL)
+ remotefile = localfile;
+ if (fd==-1) {
+ fd = open(localfile, flags, 0644);
}
- if(cmd == tftp_cmd_get)
- if(localfile == NULL)
- localfile = remotefile;
-
- if(cmd == tftp_cmd_put)
- if(remotefile == NULL)
- remotefile = localfile;
-
- fd = open(localfile, flags, 0644);
if (fd < 0) {
- perror_msg_and_die("local file");
+ bb_perror_msg_and_die("local file");
}
host = xgethostbyname(argv[optind]);
result = tftp(cmd, host, remotefile, fd, port, blocksize);
#ifdef CONFIG_FEATURE_CLEAN_UP
- close(fd);
+ if (!(fd == fileno(stdout) || fd == fileno(stdin))) {
+ close(fd);
+ }
#endif
return(result);
}