Apply Glenn's tftp rewrite

This commit is contained in:
Eric Andersen 2001-08-20 17:47:49 +00:00
parent bac490fe53
commit 76fa8ea790
2 changed files with 214 additions and 276 deletions

View File

@ -47,37 +47,6 @@
//#define BB_FEATURE_TFTP_DEBUG //#define BB_FEATURE_TFTP_DEBUG
/* we don't need #ifdefs with these constants and optimization... */
#ifdef BB_FEATURE_TFTP_GET
#define BB_TFTP_GET (1 << 0)
#else
#define BB_TFTP_GET 0
#endif
#ifdef BB_FEATURE_TFTP_PUT
#define BB_TFTP_PUT (1 << 1)
#else
#define BB_TFTP_PUT 0
#endif
#ifdef BB_FEATURE_TFTP_DEBUG
#define BB_TFTP_DEBUG 1
#else
#define BB_TFTP_DEBUG 0
#endif
#define BB_TFTP_NO_RETRIES 5
#define BB_TFTP_TIMEOUT 5 /* seconds */
#define RRQ 1 /* read request */
#define WRQ 2 /* write request */
#define DATA 3 /* data packet */
#define ACK 4 /* acknowledgement */
#define ERROR 5 /* error code */
#define BUFSIZE (512+4)
static const char *tftp_error_msg[] = { static const char *tftp_error_msg[] = {
"Undefined error", "Undefined error",
"File not found", "File not found",
@ -89,24 +58,32 @@ static const char *tftp_error_msg[] = {
"No such user" "No such user"
}; };
static inline int tftp(int cmd, struct hostent *host, const int tftp_cmd_get = 1;
char *serverfile, int localfd, int port) const int tftp_cmd_put = 2;
static inline int tftp(const int cmd, const struct hostent *host,
const char *serverfile, int localfd, const int port, int tftp_bufsize)
{ {
const int cmd_get = cmd & tftp_cmd_get;
const int cmd_put = cmd & tftp_cmd_put;
const int bb_tftp_num_retries = 5;
struct sockaddr_in sa; struct sockaddr_in sa;
int socketfd;
struct timeval tv;
fd_set rfds;
struct sockaddr_in from; struct sockaddr_in from;
struct timeval tv;
socklen_t fromlen; socklen_t fromlen;
fd_set rfds;
char *cp; char *cp;
unsigned short tmp; unsigned short tmp;
int len, opcode, finished; int socketfd;
int timeout, block_nr; int len;
int opcode = 0;
int finished = 0;
int timeout = bb_tftp_num_retries;
int block_nr = 1;
RESERVE_BB_BUFFER(buf, tftp_bufsize + 4); // Why 4 ?
RESERVE_BB_BUFFER(buf, BUFSIZE); tftp_bufsize += 4;
opcode = finished = timeout = 0;
block_nr = 1;
if ((socketfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { if ((socketfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
perror_msg("socket"); perror_msg("socket");
@ -125,12 +102,12 @@ static inline int tftp(int cmd, struct hostent *host,
/* build opcode */ /* build opcode */
if (cmd & BB_TFTP_GET) { if (cmd_get) {
opcode = RRQ; opcode = 1; // read request = 1
} }
if (cmd & BB_TFTP_PUT) { if (cmd_put) {
opcode = WRQ; opcode = 2; // write request = 2
} }
while (1) { while (1) {
@ -147,16 +124,17 @@ static inline int tftp(int cmd, struct hostent *host,
/* add filename and mode */ /* add filename and mode */
if ((BB_TFTP_GET && (opcode == RRQ)) || if ((cmd_get && (opcode == 1)) || // read request = 1
(BB_TFTP_PUT && (opcode == WRQ))) { (cmd_put && (opcode == 2))) { // write request = 2
while (cp != &buf[BUFSIZE - 1]) { /* what is this trying to do ? */
while (cp != &buf[tftp_bufsize - 1]) {
if ((*cp = *serverfile++) == '\0') if ((*cp = *serverfile++) == '\0')
break; break;
cp++; cp++;
} }
/* and this ? */
if ((*cp != '\0') || (&buf[BUFSIZE - 1] - cp) < 7) { if ((*cp != '\0') || (&buf[tftp_bufsize - 1] - cp) < 7) {
error_msg("too long server-filename"); error_msg("too long server-filename");
break; break;
} }
@ -167,8 +145,8 @@ static inline int tftp(int cmd, struct hostent *host,
/* add ack and data */ /* add ack and data */
if ((BB_TFTP_GET && (opcode == ACK)) || if ((cmd_get && (opcode == 4)) || // acknowledgement = 4
(BB_TFTP_PUT && (opcode == DATA))) { (cmd_put && (opcode == 3))) { // data packet == 3
*((unsigned short *) cp) = htons(block_nr); *((unsigned short *) cp) = htons(block_nr);
@ -176,15 +154,15 @@ static inline int tftp(int cmd, struct hostent *host,
block_nr++; block_nr++;
if (BB_TFTP_PUT && (opcode == DATA)) { if (cmd_put && (opcode == 3)) { // data packet == 3
len = read(localfd, cp, BUFSIZE - 4); len = read(localfd, cp, tftp_bufsize - 4);
if (len < 0) { if (len < 0) {
perror_msg("read"); perror_msg("read");
break; break;
} }
if (len != (BUFSIZE - 4)) { if (len != (tftp_bufsize - 4)) {
finished++; finished++;
} }
@ -202,16 +180,14 @@ static inline int tftp(int cmd, struct hostent *host,
len = cp - buf; len = cp - buf;
if (BB_TFTP_DEBUG) { #ifdef BB_FEATURE_TFTP_DEBUG
printf("sending %u bytes\n", len); printf("sending %u bytes\n", len);
for (cp = buf; cp < &buf[len]; cp++)
for (cp = buf; cp < &buf[len]; cp++) printf("%02x ", *cp);
printf("%02x ", *cp); printf("\n");
printf("\n"); #endif
}
if (sendto(socketfd, buf, len, 0, if (sendto(socketfd, buf, len, 0,
(struct sockaddr *) &sa, sizeof(sa)) < 0) { (struct sockaddr *) &sa, sizeof(sa)) < 0) {
perror_msg("send"); perror_msg("send");
len = -1; len = -1;
break; break;
@ -224,7 +200,7 @@ static inline int tftp(int cmd, struct hostent *host,
memset(&from, 0, sizeof(from)); memset(&from, 0, sizeof(from));
fromlen = sizeof(from); fromlen = sizeof(from);
tv.tv_sec = BB_TFTP_TIMEOUT; tv.tv_sec = 5; // BB_TFPT_TIMEOUT = 5
tv.tv_usec = 0; tv.tv_usec = 0;
FD_ZERO(&rfds); FD_ZERO(&rfds);
@ -232,9 +208,8 @@ static inline int tftp(int cmd, struct hostent *host,
switch (select(FD_SETSIZE, &rfds, NULL, NULL, &tv)) { switch (select(FD_SETSIZE, &rfds, NULL, NULL, &tv)) {
case 1: case 1:
len = recvfrom(socketfd, buf, len = recvfrom(socketfd, buf, tftp_bufsize, 0,
BUFSIZE, 0, (struct sockaddr *) &from, &fromlen);
(struct sockaddr *) &from, &fromlen);
if (len < 0) { if (len < 0) {
perror_msg("recvfrom"); perror_msg("recvfrom");
@ -245,28 +220,23 @@ static inline int tftp(int cmd, struct hostent *host,
if (sa.sin_port == htons(port)) { if (sa.sin_port == htons(port)) {
sa.sin_port = from.sin_port; sa.sin_port = from.sin_port;
break;
} }
if (sa.sin_port == from.sin_port) { if (sa.sin_port == from.sin_port) {
break; break;
} }
/* fall-through for bad packets! */ /* fall-through for bad packets! */
/* discard the packet - treat as timeout */ /* discard the packet - treat as timeout */
timeout = bb_tftp_num_retries;
case 0: case 0:
error_msg("timeout"); error_msg("timeout");
if (!timeout) { if (timeout == 0) {
timeout = BB_TFTP_NO_RETRIES;
} else {
timeout--;
}
if (!timeout) {
len = -1; len = -1;
error_msg("last timeout"); error_msg("last timeout");
} else {
timeout--;
} }
break; break;
@ -287,11 +257,11 @@ static inline int tftp(int cmd, struct hostent *host,
opcode = ntohs(*((unsigned short *) buf)); opcode = ntohs(*((unsigned short *) buf));
tmp = ntohs(*((unsigned short *) &buf[2])); tmp = ntohs(*((unsigned short *) &buf[2]));
if (BB_TFTP_DEBUG) { #ifdef BB_FEATURE_TFTP_DEBUG
printf("received %d bytes: %04x %04x\n", len, opcode, tmp); printf("received %d bytes: %04x %04x\n", len, opcode, tmp);
} #endif
if (BB_TFTP_GET && (opcode == DATA)) { if (cmd_get && (opcode == 3)) { // data packet == 3
if (tmp == block_nr) { if (tmp == block_nr) {
len = write(localfd, &buf[4], len - 4); len = write(localfd, &buf[4], len - 4);
@ -301,33 +271,33 @@ static inline int tftp(int cmd, struct hostent *host,
break; break;
} }
if (len != (BUFSIZE - 4)) { if (len != (tftp_bufsize - 4)) {
finished++; finished++;
} }
opcode = ACK; opcode = 4; // acknowledgement = 4
continue; continue;
} }
} }
if (BB_TFTP_PUT && (opcode == ACK)) { if (cmd_put && (opcode == 4)) { // acknowledgement = 4
if (tmp == (block_nr - 1)) { if (tmp == (block_nr - 1)) {
if (finished) { if (finished) {
break; break;
} }
opcode = DATA; opcode = 3; // data packet == 3
continue; continue;
} }
} }
if (opcode == ERROR) { if (opcode == 5) { // error code == 5
char *msg = NULL; char *msg = NULL;
if (buf[4] != '\0') { if (buf[4] != '\0') {
msg = &buf[4]; msg = &buf[4];
buf[BUFSIZE - 1] = '\0'; buf[tftp_bufsize - 1] = '\0';
} else if (tmp < (sizeof(tftp_error_msg) / sizeof(char *))) { } else if (tmp < (sizeof(tftp_error_msg) / sizeof(char *))) {
msg = (char *) tftp_error_msg[tmp]; msg = (char *) tftp_error_msg[tmp];
} }
@ -347,68 +317,67 @@ static inline int tftp(int cmd, struct hostent *host,
int tftp_main(int argc, char **argv) int tftp_main(int argc, char **argv)
{ {
char *cp, *s; struct hostent *host = NULL;
char *serverstr; char *localfile = NULL;
struct hostent *host; char *remotefile = NULL;
char *serverfile; int port = 69;
char *localfile; int cmd = 0;
int cmd, flags, fd, bad; int fd = -1;
int flags = 0;
int opt;
int result;
int blocksize = 512;
host = (void *) serverstr = serverfile = localfile = NULL; while ((opt = getopt(argc, argv, "b:gpl:r:")) != -1) {
flags = cmd = 0; switch (opt) {
bad = 1; case 'b':
blocksize = atoi(optarg);
if (argc > 3) { break;
if (BB_TFTP_GET && (strcmp(argv[1], "get") == 0)) { #ifdef BB_FEATURE_TFTP_GET
cmd = BB_TFTP_GET; case 'g':
cmd = tftp_cmd_get;
flags = O_WRONLY | O_CREAT; flags = O_WRONLY | O_CREAT;
serverstr = argv[2]; break;
localfile = argv[3]; #endif
} #ifdef BB_FEATURE_TFTP_PUT
case 'p':
if (BB_TFTP_PUT && (strcmp(argv[1], "put") == 0)) { cmd = tftp_cmd_put;
cmd = BB_TFTP_PUT;
flags = O_RDONLY; flags = O_RDONLY;
localfile = argv[2]; break;
serverstr = argv[3]; #endif
case 'l':
localfile = xstrdup(optarg);
break;
case 'r':
remotefile = xstrdup(optarg);
break;
} }
} }
if (!(cmd & (BB_TFTP_GET | BB_TFTP_PUT))) { if ((cmd == 0) || (optind == argc)) {
show_usage(); show_usage();
} }
for (cp = serverstr; *cp != '\0'; cp++) fd = open(localfile, flags, 0644);
if (*cp == ':') if (fd < 0) {
break;
if (*cp == ':') {
serverfile = cp + 1;
s = xstrdup(serverstr);
s[cp - serverstr] = '\0';
host = xgethostbyname(s);
free(s);
}
if (BB_TFTP_DEBUG) {
printf("using server \"%s\", serverfile \"%s\","
"localfile \"%s\".\n",
inet_ntoa(*((struct in_addr *) host->h_addr)),
serverfile, localfile);
}
if ((fd = open(localfile, flags, 0644)) < 0) {
perror_msg_and_die("local file"); perror_msg_and_die("local file");
} }
flags = tftp(cmd, host, serverfile, fd, 69); host = xgethostbyname(argv[optind]);
if (optind + 2 == argc) {
port = atoi(argv[optind + 1]);
}
#ifdef BB_FEATURE_TFTP_DEBUG
printf("using server \"%s\", serverfile \"%s\","
"localfile \"%s\".\n",
inet_ntoa(*((struct in_addr *) host->h_addr)),
remotefile, localfile);
#endif
result = tftp(cmd, host, remotefile, fd, port, blocksize);
close(fd); close(fd);
return flags; return(result);
} }

243
tftp.c
View File

@ -47,37 +47,6 @@
//#define BB_FEATURE_TFTP_DEBUG //#define BB_FEATURE_TFTP_DEBUG
/* we don't need #ifdefs with these constants and optimization... */
#ifdef BB_FEATURE_TFTP_GET
#define BB_TFTP_GET (1 << 0)
#else
#define BB_TFTP_GET 0
#endif
#ifdef BB_FEATURE_TFTP_PUT
#define BB_TFTP_PUT (1 << 1)
#else
#define BB_TFTP_PUT 0
#endif
#ifdef BB_FEATURE_TFTP_DEBUG
#define BB_TFTP_DEBUG 1
#else
#define BB_TFTP_DEBUG 0
#endif
#define BB_TFTP_NO_RETRIES 5
#define BB_TFTP_TIMEOUT 5 /* seconds */
#define RRQ 1 /* read request */
#define WRQ 2 /* write request */
#define DATA 3 /* data packet */
#define ACK 4 /* acknowledgement */
#define ERROR 5 /* error code */
#define BUFSIZE (512+4)
static const char *tftp_error_msg[] = { static const char *tftp_error_msg[] = {
"Undefined error", "Undefined error",
"File not found", "File not found",
@ -89,24 +58,32 @@ static const char *tftp_error_msg[] = {
"No such user" "No such user"
}; };
static inline int tftp(int cmd, struct hostent *host, const int tftp_cmd_get = 1;
char *serverfile, int localfd, int port) const int tftp_cmd_put = 2;
static inline int tftp(const int cmd, const struct hostent *host,
const char *serverfile, int localfd, const int port, int tftp_bufsize)
{ {
const int cmd_get = cmd & tftp_cmd_get;
const int cmd_put = cmd & tftp_cmd_put;
const int bb_tftp_num_retries = 5;
struct sockaddr_in sa; struct sockaddr_in sa;
int socketfd;
struct timeval tv;
fd_set rfds;
struct sockaddr_in from; struct sockaddr_in from;
struct timeval tv;
socklen_t fromlen; socklen_t fromlen;
fd_set rfds;
char *cp; char *cp;
unsigned short tmp; unsigned short tmp;
int len, opcode, finished; int socketfd;
int timeout, block_nr; int len;
int opcode = 0;
int finished = 0;
int timeout = bb_tftp_num_retries;
int block_nr = 1;
RESERVE_BB_BUFFER(buf, tftp_bufsize + 4); // Why 4 ?
RESERVE_BB_BUFFER(buf, BUFSIZE); tftp_bufsize += 4;
opcode = finished = timeout = 0;
block_nr = 1;
if ((socketfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { if ((socketfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
perror_msg("socket"); perror_msg("socket");
@ -125,12 +102,12 @@ static inline int tftp(int cmd, struct hostent *host,
/* build opcode */ /* build opcode */
if (cmd & BB_TFTP_GET) { if (cmd_get) {
opcode = RRQ; opcode = 1; // read request = 1
} }
if (cmd & BB_TFTP_PUT) { if (cmd_put) {
opcode = WRQ; opcode = 2; // write request = 2
} }
while (1) { while (1) {
@ -147,16 +124,17 @@ static inline int tftp(int cmd, struct hostent *host,
/* add filename and mode */ /* add filename and mode */
if ((BB_TFTP_GET && (opcode == RRQ)) || if ((cmd_get && (opcode == 1)) || // read request = 1
(BB_TFTP_PUT && (opcode == WRQ))) { (cmd_put && (opcode == 2))) { // write request = 2
while (cp != &buf[BUFSIZE - 1]) { /* what is this trying to do ? */
while (cp != &buf[tftp_bufsize - 1]) {
if ((*cp = *serverfile++) == '\0') if ((*cp = *serverfile++) == '\0')
break; break;
cp++; cp++;
} }
/* and this ? */
if ((*cp != '\0') || (&buf[BUFSIZE - 1] - cp) < 7) { if ((*cp != '\0') || (&buf[tftp_bufsize - 1] - cp) < 7) {
error_msg("too long server-filename"); error_msg("too long server-filename");
break; break;
} }
@ -167,8 +145,8 @@ static inline int tftp(int cmd, struct hostent *host,
/* add ack and data */ /* add ack and data */
if ((BB_TFTP_GET && (opcode == ACK)) || if ((cmd_get && (opcode == 4)) || // acknowledgement = 4
(BB_TFTP_PUT && (opcode == DATA))) { (cmd_put && (opcode == 3))) { // data packet == 3
*((unsigned short *) cp) = htons(block_nr); *((unsigned short *) cp) = htons(block_nr);
@ -176,15 +154,15 @@ static inline int tftp(int cmd, struct hostent *host,
block_nr++; block_nr++;
if (BB_TFTP_PUT && (opcode == DATA)) { if (cmd_put && (opcode == 3)) { // data packet == 3
len = read(localfd, cp, BUFSIZE - 4); len = read(localfd, cp, tftp_bufsize - 4);
if (len < 0) { if (len < 0) {
perror_msg("read"); perror_msg("read");
break; break;
} }
if (len != (BUFSIZE - 4)) { if (len != (tftp_bufsize - 4)) {
finished++; finished++;
} }
@ -202,16 +180,14 @@ static inline int tftp(int cmd, struct hostent *host,
len = cp - buf; len = cp - buf;
if (BB_TFTP_DEBUG) { #ifdef BB_FEATURE_TFTP_DEBUG
printf("sending %u bytes\n", len); printf("sending %u bytes\n", len);
for (cp = buf; cp < &buf[len]; cp++)
for (cp = buf; cp < &buf[len]; cp++) printf("%02x ", *cp);
printf("%02x ", *cp); printf("\n");
printf("\n"); #endif
}
if (sendto(socketfd, buf, len, 0, if (sendto(socketfd, buf, len, 0,
(struct sockaddr *) &sa, sizeof(sa)) < 0) { (struct sockaddr *) &sa, sizeof(sa)) < 0) {
perror_msg("send"); perror_msg("send");
len = -1; len = -1;
break; break;
@ -224,7 +200,7 @@ static inline int tftp(int cmd, struct hostent *host,
memset(&from, 0, sizeof(from)); memset(&from, 0, sizeof(from));
fromlen = sizeof(from); fromlen = sizeof(from);
tv.tv_sec = BB_TFTP_TIMEOUT; tv.tv_sec = 5; // BB_TFPT_TIMEOUT = 5
tv.tv_usec = 0; tv.tv_usec = 0;
FD_ZERO(&rfds); FD_ZERO(&rfds);
@ -232,9 +208,8 @@ static inline int tftp(int cmd, struct hostent *host,
switch (select(FD_SETSIZE, &rfds, NULL, NULL, &tv)) { switch (select(FD_SETSIZE, &rfds, NULL, NULL, &tv)) {
case 1: case 1:
len = recvfrom(socketfd, buf, len = recvfrom(socketfd, buf, tftp_bufsize, 0,
BUFSIZE, 0, (struct sockaddr *) &from, &fromlen);
(struct sockaddr *) &from, &fromlen);
if (len < 0) { if (len < 0) {
perror_msg("recvfrom"); perror_msg("recvfrom");
@ -245,28 +220,23 @@ static inline int tftp(int cmd, struct hostent *host,
if (sa.sin_port == htons(port)) { if (sa.sin_port == htons(port)) {
sa.sin_port = from.sin_port; sa.sin_port = from.sin_port;
break;
} }
if (sa.sin_port == from.sin_port) { if (sa.sin_port == from.sin_port) {
break; break;
} }
/* fall-through for bad packets! */ /* fall-through for bad packets! */
/* discard the packet - treat as timeout */ /* discard the packet - treat as timeout */
timeout = bb_tftp_num_retries;
case 0: case 0:
error_msg("timeout"); error_msg("timeout");
if (!timeout) { if (timeout == 0) {
timeout = BB_TFTP_NO_RETRIES;
} else {
timeout--;
}
if (!timeout) {
len = -1; len = -1;
error_msg("last timeout"); error_msg("last timeout");
} else {
timeout--;
} }
break; break;
@ -287,11 +257,11 @@ static inline int tftp(int cmd, struct hostent *host,
opcode = ntohs(*((unsigned short *) buf)); opcode = ntohs(*((unsigned short *) buf));
tmp = ntohs(*((unsigned short *) &buf[2])); tmp = ntohs(*((unsigned short *) &buf[2]));
if (BB_TFTP_DEBUG) { #ifdef BB_FEATURE_TFTP_DEBUG
printf("received %d bytes: %04x %04x\n", len, opcode, tmp); printf("received %d bytes: %04x %04x\n", len, opcode, tmp);
} #endif
if (BB_TFTP_GET && (opcode == DATA)) { if (cmd_get && (opcode == 3)) { // data packet == 3
if (tmp == block_nr) { if (tmp == block_nr) {
len = write(localfd, &buf[4], len - 4); len = write(localfd, &buf[4], len - 4);
@ -301,33 +271,33 @@ static inline int tftp(int cmd, struct hostent *host,
break; break;
} }
if (len != (BUFSIZE - 4)) { if (len != (tftp_bufsize - 4)) {
finished++; finished++;
} }
opcode = ACK; opcode = 4; // acknowledgement = 4
continue; continue;
} }
} }
if (BB_TFTP_PUT && (opcode == ACK)) { if (cmd_put && (opcode == 4)) { // acknowledgement = 4
if (tmp == (block_nr - 1)) { if (tmp == (block_nr - 1)) {
if (finished) { if (finished) {
break; break;
} }
opcode = DATA; opcode = 3; // data packet == 3
continue; continue;
} }
} }
if (opcode == ERROR) { if (opcode == 5) { // error code == 5
char *msg = NULL; char *msg = NULL;
if (buf[4] != '\0') { if (buf[4] != '\0') {
msg = &buf[4]; msg = &buf[4];
buf[BUFSIZE - 1] = '\0'; buf[tftp_bufsize - 1] = '\0';
} else if (tmp < (sizeof(tftp_error_msg) / sizeof(char *))) { } else if (tmp < (sizeof(tftp_error_msg) / sizeof(char *))) {
msg = (char *) tftp_error_msg[tmp]; msg = (char *) tftp_error_msg[tmp];
} }
@ -347,68 +317,67 @@ static inline int tftp(int cmd, struct hostent *host,
int tftp_main(int argc, char **argv) int tftp_main(int argc, char **argv)
{ {
char *cp, *s; struct hostent *host = NULL;
char *serverstr; char *localfile = NULL;
struct hostent *host; char *remotefile = NULL;
char *serverfile; int port = 69;
char *localfile; int cmd = 0;
int cmd, flags, fd, bad; int fd = -1;
int flags = 0;
int opt;
int result;
int blocksize = 512;
host = (void *) serverstr = serverfile = localfile = NULL; while ((opt = getopt(argc, argv, "b:gpl:r:")) != -1) {
flags = cmd = 0; switch (opt) {
bad = 1; case 'b':
blocksize = atoi(optarg);
if (argc > 3) { break;
if (BB_TFTP_GET && (strcmp(argv[1], "get") == 0)) { #ifdef BB_FEATURE_TFTP_GET
cmd = BB_TFTP_GET; case 'g':
cmd = tftp_cmd_get;
flags = O_WRONLY | O_CREAT; flags = O_WRONLY | O_CREAT;
serverstr = argv[2]; break;
localfile = argv[3]; #endif
} #ifdef BB_FEATURE_TFTP_PUT
case 'p':
if (BB_TFTP_PUT && (strcmp(argv[1], "put") == 0)) { cmd = tftp_cmd_put;
cmd = BB_TFTP_PUT;
flags = O_RDONLY; flags = O_RDONLY;
localfile = argv[2]; break;
serverstr = argv[3]; #endif
case 'l':
localfile = xstrdup(optarg);
break;
case 'r':
remotefile = xstrdup(optarg);
break;
} }
} }
if (!(cmd & (BB_TFTP_GET | BB_TFTP_PUT))) { if ((cmd == 0) || (optind == argc)) {
show_usage(); show_usage();
} }
for (cp = serverstr; *cp != '\0'; cp++) fd = open(localfile, flags, 0644);
if (*cp == ':') if (fd < 0) {
break;
if (*cp == ':') {
serverfile = cp + 1;
s = xstrdup(serverstr);
s[cp - serverstr] = '\0';
host = xgethostbyname(s);
free(s);
}
if (BB_TFTP_DEBUG) {
printf("using server \"%s\", serverfile \"%s\","
"localfile \"%s\".\n",
inet_ntoa(*((struct in_addr *) host->h_addr)),
serverfile, localfile);
}
if ((fd = open(localfile, flags, 0644)) < 0) {
perror_msg_and_die("local file"); perror_msg_and_die("local file");
} }
flags = tftp(cmd, host, serverfile, fd, 69); host = xgethostbyname(argv[optind]);
if (optind + 2 == argc) {
port = atoi(argv[optind + 1]);
}
#ifdef BB_FEATURE_TFTP_DEBUG
printf("using server \"%s\", serverfile \"%s\","
"localfile \"%s\".\n",
inet_ntoa(*((struct in_addr *) host->h_addr)),
remotefile, localfile);
#endif
result = tftp(cmd, host, remotefile, fd, port, blocksize);
close(fd); close(fd);
return flags; return(result);
} }