#define O_BINARY 0
#endif
-#define TFTP_PKT_SIZE 516
+#define TFTP_BLKSIZE 1456
static const char *opcode_names[] = {
"RRQ", "WRQ", "DATA", "ACK", "ERR", "OACK"
static char *pkt_mkopt(char *pkt, const char *opt, const char* val)
{
strcpy(pkt, opt);
- pkt += strlen(pkt) + 1;
+ pkt += strlen(opt) + 1;
strcpy(pkt, val);
pkt += strlen(val) + 1;
return pkt;
pkt += 2;
while (pkt_nextopt(&pkt, &opt, &val, &rem)) {
- if (!strcmp(name, opt)) {
+ if (!strcasecmp(name, opt)) {
return val;
}
}
static void pkt_mkwrq(char *pkt, const char *filename, unsigned blksize)
{
- size_t len = 2;
-
filename = leafname(filename);
if (!tftp_is_valid_filename(filename)) {
fprintf(stderr, "Overlong/illegal filename; using 'firmware'.\n");
switch (pkt_num(pkt)) {
case RRQ:
case WRQ:
+ case OACK:
len = pkt_xrqlen(pkt);
break;
case DATA:
const char *leafname(const char *path)
{
+ if (!path) {
+ return NULL;
+ }
+
const char *slash, *bslash;
slash = strrchr(path, '/');
inline bool tftp_is_valid_filename(const char *filename)
{
- return strlen(filename) <= 500 && is_netascii(filename);
+ return strlen(filename) <= 255 && is_netascii(filename);
}
+static const char *spinner = "\\|/-";
+
int tftp_put(struct nmrpd_args *args)
{
struct sockaddr_in addr;
uint16_t block, port, op, blksize;
ssize_t len, last_len;
- int fd, sock, ret, timeout, errors, ackblock;
+ int fd, sock, ret, timeouts, errors, ackblock;
char rx[2048], tx[2048];
const char *file_remote = args->file_remote;
- char *p;
+ char *val, *end;
+ bool rollover;
+ const unsigned rx_timeout = MAX(args->rx_timeout / (args->blind ? 50 : 5), 2000);
+ const unsigned max_timeouts = args->blind ? 3 : 5;
sock = -1;
ret = -1;
xperror("inet_addr");
goto cleanup;
}
+
addr.sin_port = htons(args->port);
blksize = 512;
last_len = -1;
len = 0;
errors = 0;
+ rollover = false;
/* Not really, but this way the loop sends our WRQ before receiving */
- timeout = 1;
+ timeouts = 1;
- pkt_mkwrq(tx, file_remote, 1456);
+ pkt_mkwrq(tx, file_remote, TFTP_BLKSIZE);
while (!g_interrupted) {
ackblock = -1;
op = pkt_num(rx);
- if (!timeout) {
+ if (!timeouts) {
if (op == ACK) {
ackblock = pkt_num(rx + 2);
} else if (op == OACK) {
ackblock = 0;
- if ((p = pkt_optval(rx, "blksize"))) {
- blksize = atoi(p);
+ if ((val = pkt_optval(rx, "blksize"))) {
+ blksize = strtol(val, &end, 10);
+ if (*end != '\0' || blksize < 8 || blksize > TFTP_BLKSIZE) {
+ fprintf(stderr, "Error: invalid blksize in OACK: %s\n", val);
+ ret = -1;
+ goto cleanup;
+ }
+
+ if (verbosity) {
+ printf("Remote accepted blksize option: %d b\n", blksize);
+ }
}
}
}
- if (timeout || ackblock == block) {
- if (!timeout) {
- ++block;
+ if (timeouts || ackblock == block) {
+ if (!timeouts) {
+ if (++block == 0) {
+ if (!rollover) {
+ printf("Warning: TFTP block rollover. Upload might fail!\n");
+ rollover = true;
+ }
+ }
+
+ printf("%c ", spinner[block & 3]);
+ fflush(stdout);
+ printf("\b\b");
+
pkt_mknum(tx, DATA);
pkt_mknum(tx + 2, block);
len = read(fd, tx + 4, blksize);
}
}
- ret = tftp_recvfrom(sock, rx, &port, args->rx_timeout, blksize + 2);
+ ret = tftp_recvfrom(sock, rx, &port, rx_timeout, blksize + 4);
if (ret < 0) {
goto cleanup;
} else if (!ret) {
- if (++timeout < 5 || (!block && timeout < 10)) {
+ if (++timeouts < max_timeouts || (!block && timeouts < (max_timeouts * 4))) {
+ continue;
+ } else if (args->blind) {
+ timeouts = 0;
+ // fake an ACK packet
+ pkt_mknum(rx, ACK);
+ pkt_mknum(rx + 2, block);
continue;
} else if (block) {
fprintf(stderr, "Timeout while waiting for ACK(%d).\n", block);
} else {
- fprintf(stderr, "Timeout while waiting for initial reply.\n");
+ fprintf(stderr, "Timeout while waiting for ACK(0)/OACK.\n");
}
ret = -1;
goto cleanup;
} else {
- timeout = 0;
+ timeouts = 0;
ret = 0;
if (!block && port != args->port) {