/**
- * nmrp-flash - Netgear Unbrick Utility
+ * nmrpflash - Netgear Unbrick Utility
* Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
*
- * nmrp-flash is free software: you can redistribute it and/or modify
+ * nmrpflash is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
- * nmrp-flash is distributed in the hope that it will be useful,
+ * nmrpflash is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with nmrp-flash. If not, see <http://www.gnu.org/licenses/>.
+ * along with nmrpflash. If not, see <http://www.gnu.org/licenses/>.
*
*/
ERR = 5
};
-static const char *leafname(const char *path)
-{
- const char *slash, *bslash;
-
- slash = strrchr(path, '/');
- bslash = strrchr(path, '\\');
-
- if (slash && bslash) {
- path = 1 + (slash > bslash ? slash : bslash);
- } else if (slash) {
- path = 1 + slash;
- } else if (bslash) {
- path = 1 + bslash;
- }
-
- return path;
-}
-
static bool is_netascii(const char *str)
{
- for (; *str; ++str) {
- if (*str < 0x20 || *str > 0x7f) {
+ uint8_t *p = (uint8_t*)str;
+
+ for (; *p; ++p) {
+ if (*p < 0x20 || *p > 0x7f) {
return false;
}
}
size_t len = 2;
filename = leafname(filename);
- if (!is_netascii(filename) || strlen(filename) > 500) {
- fprintf(stderr, "Overlong/illegal filename; using 'firmware.bin'.\n");
- filename = "firmware.bin";
+ if (!tftp_is_valid_filename(filename)) {
+ fprintf(stderr, "Overlong/illegal filename; using 'firmware'.\n");
+ filename = "firmware";
+ } else if (!strcmp(filename, "-")) {
+ filename = "firmware";
}
pkt_mknum(pkt, WRQ);
return sent;
}
+const char *leafname(const char *path)
+{
+ const char *slash, *bslash;
+
+ slash = strrchr(path, '/');
+ bslash = strrchr(path, '\\');
+
+ if (slash && bslash) {
+ path = 1 + (slash > bslash ? slash : bslash);
+ } else if (slash) {
+ path = 1 + slash;
+ } else if (bslash) {
+ path = 1 + bslash;
+ }
+
+ return path;
+}
+
#ifdef NMRPFLASH_WINDOWS
void sock_perror(const char *msg)
{
win_perror2(msg, WSAGetLastError());
}
-#else
-inline void sock_perror(const char *msg)
+#endif
+
+inline bool tftp_is_valid_filename(const char *filename)
{
- perror(msg);
+ return strlen(filename) <= 500 && is_netascii(filename);
}
-#endif
int tftp_put(struct nmrpd_args *args)
{
sock = -1;
ret = -1;
- fd = open(args->filename, O_RDONLY);
- if (fd < 0) {
- perror("open");
- ret = fd;
- goto cleanup;
+ if (!strcmp(args->file_local, "-")) {
+ fd = STDIN_FILENO;
+ } else {
+ fd = open(args->file_local, O_RDONLY);
+ if (fd < 0) {
+ perror("open");
+ ret = fd;
+ goto cleanup;
+ }
}
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
/* Not really, but this way the loop sends our WRQ before receiving */
timeout = 1;
- pkt_mkwrq(tx, args->filename);
+ pkt_mkwrq(tx, args->file_remote);
do {
- ackblock = pkt_num(rx) == ACK ? pkt_num(rx + 2) : -1;
+ if (!timeout && pkt_num(rx) == ACK) {
+ ackblock = pkt_num(rx + 2);
+ } else {
+ ackblock = -1;
+ }
if (timeout || ackblock == block) {
if (!timeout) {
if (ret < 0) {
goto cleanup;
} else if (!ret) {
- if (++timeout < 5) {
+ if (++timeout < 5 || (!block && timeout < 10)) {
continue;
} else if (block) {
fprintf(stderr, "Timeout while waiting for ACK(%d).\n", block);