tftp fixes

This commit is contained in:
Joseph C. Lehner 2017-01-13 09:35:28 +01:00
parent 549e0da06f
commit 3753023819

18
tftp.c
View file

@ -133,8 +133,6 @@ static size_t pkt_xrqlen(char *pkt)
static void pkt_mkwrq(char *pkt, const char *filename, unsigned blksize) static void pkt_mkwrq(char *pkt, const char *filename, unsigned blksize)
{ {
size_t len = 2;
filename = leafname(filename); filename = leafname(filename);
if (!tftp_is_valid_filename(filename)) { if (!tftp_is_valid_filename(filename)) {
fprintf(stderr, "Overlong/illegal filename; using 'firmware'.\n"); fprintf(stderr, "Overlong/illegal filename; using 'firmware'.\n");
@ -308,7 +306,7 @@ int tftp_put(struct nmrpd_args *args)
struct sockaddr_in addr; struct sockaddr_in addr;
uint16_t block, port, op, blksize; uint16_t block, port, op, blksize;
ssize_t len, last_len; ssize_t len, last_len;
int fd, sock, ret, timeout, errors, ackblock; int fd, sock, ret, timeouts, errors, ackblock;
char rx[2048], tx[2048]; char rx[2048], tx[2048];
const char *file_remote = args->file_remote; const char *file_remote = args->file_remote;
char *val, *end; char *val, *end;
@ -372,7 +370,7 @@ int tftp_put(struct nmrpd_args *args)
len = 0; len = 0;
errors = 0; errors = 0;
/* Not really, but this way the loop sends our WRQ before receiving */ /* Not really, but this way the loop sends our WRQ before receiving */
timeout = 1; timeouts = 1;
pkt_mkwrq(tx, file_remote, TFTP_BLKSIZE); pkt_mkwrq(tx, file_remote, TFTP_BLKSIZE);
@ -380,7 +378,7 @@ int tftp_put(struct nmrpd_args *args)
ackblock = -1; ackblock = -1;
op = pkt_num(rx); op = pkt_num(rx);
if (!timeout) { if (!timeouts) {
if (op == ACK) { if (op == ACK) {
ackblock = pkt_num(rx + 2); ackblock = pkt_num(rx + 2);
} else if (op == OACK) { } else if (op == OACK) {
@ -396,8 +394,8 @@ int tftp_put(struct nmrpd_args *args)
} }
} }
if (timeout || ackblock == block) { if (timeouts || ackblock == block) {
if (!timeout) { if (!timeouts) {
++block; ++block;
pkt_mknum(tx, DATA); pkt_mknum(tx, DATA);
pkt_mknum(tx + 2, block); pkt_mknum(tx + 2, block);
@ -433,11 +431,11 @@ int tftp_put(struct nmrpd_args *args)
} }
} }
ret = tftp_recvfrom(sock, rx, &port, args->rx_timeout, blksize + 2); ret = tftp_recvfrom(sock, rx, &port, args->rx_timeout, blksize + 4);
if (ret < 0) { if (ret < 0) {
goto cleanup; goto cleanup;
} else if (!ret) { } else if (!ret) {
if (++timeout < 5 || (!block && timeout < 10)) { if (++timeouts < 5 || (!block && timeouts < 10)) {
continue; continue;
} else if (block) { } else if (block) {
fprintf(stderr, "Timeout while waiting for ACK(%d).\n", block); fprintf(stderr, "Timeout while waiting for ACK(%d).\n", block);
@ -447,7 +445,7 @@ int tftp_put(struct nmrpd_args *args)
ret = -1; ret = -1;
goto cleanup; goto cleanup;
} else { } else {
timeout = 0; timeouts = 0;
ret = 0; ret = 0;
if (!block && port != args->port) { if (!block && port != args->port) {