2 * nmrpflash - Netgear Unbrick Utility
3 * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
5 * nmrpflash is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * nmrpflash is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with nmrpflash. If not, see <http://www.gnu.org/licenses/>.
33 #define TFTP_PKT_SIZE 516
35 static const char *opcode_names[] = {
36 "RRQ", "WRQ", "DATA", "ACK", "ERR", "OACK"
48 static bool is_netascii(const char *str)
50 uint8_t *p = (uint8_t*)str;
53 if (*p < 0x20 || *p > 0x7f) {
61 static inline void pkt_mknum(char *pkt, uint16_t n)
63 *(uint16_t*)pkt = htons(n);
66 static inline uint16_t pkt_num(char *pkt)
68 return ntohs(*(uint16_t*)pkt);
71 static char *pkt_mkopt(char *pkt, const char *opt, const char* val)
74 pkt += strlen(pkt) + 1;
76 pkt += strlen(val) + 1;
80 static bool pkt_nextstr(char **pkt, char **str, size_t *rem)
84 if (!isprint(**pkt) || !(len = strnlen(*pkt, *rem))) {
100 static bool pkt_nextopt(char **pkt, char **opt, char **val, size_t *rem)
102 return pkt_nextstr(pkt, opt, rem) && pkt_nextstr(pkt, val, rem);
105 static char *pkt_optval(char* pkt, const char* name)
111 while (pkt_nextopt(&pkt, &opt, &val, &rem)) {
112 if (!strcmp(name, opt)) {
120 static size_t pkt_xrqlen(char *pkt)
122 size_t len = 2, rem = 512;
127 while (pkt_nextopt(&pkt, &opt, &val, &rem)) {
128 len += strlen(opt) + strlen(val) + 2;
134 static void pkt_mkwrq(char *pkt, const char *filename, unsigned blksize)
138 filename = leafname(filename);
139 if (!tftp_is_valid_filename(filename)) {
140 fprintf(stderr, "Overlong/illegal filename; using 'firmware'.\n");
141 filename = "firmware";
142 } else if (!strcmp(filename, "-")) {
143 filename = "firmware";
147 pkt = pkt_mkopt(pkt + 2, filename, "octet");
149 if (blksize && blksize != 512) {
150 pkt = pkt_mkopt(pkt, "blksize", lltostr(blksize, 10));
154 static inline void pkt_print(char *pkt, FILE *fp)
156 uint16_t opcode = pkt_num(pkt);
160 if (!opcode || opcode > OACK) {
161 fprintf(fp, "(%d)", opcode);
163 fprintf(fp, "%s", opcode_names[opcode - 1]);
164 if (opcode == ACK || opcode == DATA) {
165 fprintf(fp, "(%d)", pkt_num(pkt + 2));
166 } else if (opcode == WRQ || opcode == RRQ) {
167 fprintf(fp, "(%s, %s)", pkt + 2, pkt + 2 + strlen(pkt + 2) + 1);
168 } else if (opcode == OACK) {
172 while (pkt_nextopt(&pkt, &opt, &val, &rem)) {
173 fprintf(fp, " %s=%s ", opt, val);
180 static ssize_t tftp_recvfrom(int sock, char *pkt, uint16_t* port,
181 unsigned timeout, size_t blksize)
184 struct sockaddr_in src;
185 #ifndef NMRPFLASH_WINDOWS
191 len = select_fd(sock, timeout);
199 len = recvfrom(sock, pkt, blksize, 0, (struct sockaddr*)&src, &alen);
201 sock_perror("recvfrom");
205 *port = ntohs(src.sin_port);
207 uint16_t opcode = pkt_num(pkt);
210 fprintf(stderr, "Error (%d): %.511s\n", pkt_num(pkt + 2), pkt + 4);
212 } else if (isprint(pkt[0])) {
213 /* In case of a firmware checksum error, the EX2700 I've tested this
214 * on sends a raw UDP packet containing just an error message starting
215 * at offset 0. The limit of 32 chars is arbitrary.
217 fprintf(stderr, "Error: %.32s\n", pkt);
219 } else if (!opcode || opcode > OACK) {
220 fprintf(stderr, "Received invalid packet: ");
221 pkt_print(pkt, stderr);
222 fprintf(stderr, ".\n");
228 pkt_print(pkt, stdout);
235 static ssize_t tftp_sendto(int sock, char *pkt, size_t len,
236 struct sockaddr_in *dst)
240 switch (pkt_num(pkt)) {
243 len = pkt_xrqlen(pkt);
252 len = 4 + strlen(pkt + 4);
255 fprintf(stderr, "Attempted to send invalid packet ");
256 pkt_print(pkt, stderr);
257 fprintf(stderr, "; this is a bug!\n");
263 pkt_print(pkt, stdout);
267 sent = sendto(sock, pkt, len, 0, (struct sockaddr*)dst, sizeof(*dst));
269 sock_perror("sendto");
275 const char *leafname(const char *path)
277 const char *slash, *bslash;
279 slash = strrchr(path, '/');
280 bslash = strrchr(path, '\\');
282 if (slash && bslash) {
283 path = 1 + (slash > bslash ? slash : bslash);
293 #ifdef NMRPFLASH_WINDOWS
294 void sock_perror(const char *msg)
296 win_perror2(msg, WSAGetLastError());
300 inline bool tftp_is_valid_filename(const char *filename)
302 return strlen(filename) <= 500 && is_netascii(filename);
305 int tftp_put(struct nmrpd_args *args)
307 struct sockaddr_in addr;
308 uint16_t block, port, op, blksize;
309 ssize_t len, last_len;
310 int fd, sock, ret, timeout, errors, ackblock;
311 char rx[2048], tx[2048];
312 const char *file_remote = args->file_remote;
323 if (!strcmp(args->file_local, "-")) {
326 file_remote = "firmware";
329 fd = open(args->file_local, O_RDONLY | O_BINARY);
334 } else if (!file_remote) {
335 file_remote = args->file_local;
339 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
341 sock_perror("socket");
346 memset(&addr, 0, sizeof(addr));
348 addr.sin_family = AF_INET;
350 if (args->ipaddr_intf) {
351 if ((addr.sin_addr.s_addr = inet_addr(args->ipaddr_intf)) == INADDR_NONE) {
352 xperror("inet_addr");
356 if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
362 if ((addr.sin_addr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
363 xperror("inet_addr");
366 addr.sin_port = htons(args->port);
373 /* Not really, but this way the loop sends our WRQ before receiving */
376 pkt_mkwrq(tx, file_remote, blksize);
378 while (!g_interrupted) {
383 if (pkt_num(rx) == ACK) {
384 ackblock = pkt_num(rx + 2);
388 } else if (pkt_num(rx) == OACK) {
391 if ((p = pkt_optval(rx, "blksize"))) {
399 if (timeout || ackblock == block) {
403 pkt_mknum(tx + 2, block);
404 len = read(fd, tx + 4, blksize);
410 if (last_len != blksize && last_len != -1) {
418 ret = tftp_sendto(sock, tx, len, &addr);
422 } else if (op != ACK || ackblock > block) {
424 fprintf(stderr, "Expected ACK(%d), got ", block);
425 pkt_print(rx, stderr);
426 fprintf(stderr, ".\n");
429 if (ackblock != -1 && ++errors > 5) {
430 fprintf(stderr, "Protocol error; bailing out.\n");
436 ret = tftp_recvfrom(sock, rx, &port, args->rx_timeout, blksize + 2);
440 if (++timeout < 5 || (!block && timeout < 10)) {
443 fprintf(stderr, "Timeout while waiting for ACK(%d).\n", block);
445 fprintf(stderr, "Timeout while waiting for initial reply.\n");
453 if (!block && port != args->port) {
455 printf("Switching to port %d\n", port);
457 addr.sin_port = htons(port);
462 ret = !g_interrupted ? 0 : -1;
470 #ifndef NMRPFLASH_WINDOWS
471 shutdown(sock, SHUT_RDWR);
474 shutdown(sock, SD_BOTH);