2 * nmrp-flash - Netgear Unbrick Utility
3 * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
5 * nmrp-flash 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 * nmrp-flash 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 nmrp-flash. If not, see <http://www.gnu.org/licenses/>.
29 #define TFTP_PKT_SIZE 516
31 static const char *opcode_names[] = {
32 "RRQ", "WRQ", "DATA", "ACK", "ERR"
43 static const char *leafname(const char *path)
45 const char *slash, *bslash;
47 slash = strrchr(path, '/');
48 bslash = strrchr(path, '\\');
50 if (slash && bslash) {
51 path = 1 + (slash > bslash ? slash : bslash);
61 static bool is_netascii(const char *str)
64 if (*str < 0x20 || *str > 0x7f) {
72 static inline void pkt_mknum(char *pkt, uint16_t n)
74 *(uint16_t*)pkt = htons(n);
77 static inline uint16_t pkt_num(char *pkt)
79 return ntohs(*(uint16_t*)pkt);
82 static void pkt_mkwrq(char *pkt, const char *filename)
86 filename = leafname(filename);
87 if (!is_netascii(filename) || strlen(filename) > 500) {
88 fprintf(stderr, "Overlong/illegal filename; using 'firmware.bin'.\n");
89 filename = "firmware.bin";
94 strcpy(pkt + len, filename);
95 len += strlen(filename) + 1;
96 strcpy(pkt + len, "octet");
99 static inline void pkt_print(char *pkt, FILE *fp)
101 uint16_t opcode = pkt_num(pkt);
102 if (!opcode || opcode > ERR) {
103 fprintf(fp, "(%d)", opcode);
105 fprintf(fp, "%s", opcode_names[opcode - 1]);
106 if (opcode == ACK || opcode == DATA) {
107 fprintf(fp, "(%d)", pkt_num(pkt + 2));
108 } else if (opcode == WRQ || opcode == RRQ) {
109 fprintf(fp, "(%s, %s)", pkt + 2, pkt + 2 + strlen(pkt + 2) + 1);
114 static ssize_t tftp_recvfrom(int sock, char *pkt, uint16_t* port,
118 struct sockaddr_in src;
119 #ifndef NMRPFLASH_WINDOWS
125 len = select_fd(sock, timeout);
133 len = recvfrom(sock, pkt, TFTP_PKT_SIZE, 0, (struct sockaddr*)&src, &alen);
135 sock_perror("recvfrom");
139 *port = ntohs(src.sin_port);
141 uint16_t opcode = pkt_num(pkt);
144 fprintf(stderr, "Error (%d): %.511s\n", pkt_num(pkt + 2), pkt + 4);
146 } else if (isprint(pkt[0])) {
147 /* In case of a firmware checksum error, the EX2700 I've tested this
148 * on sends a raw UDP packet containing just an error message starting
149 * at offset 0. The limit of 32 chars is arbitrary.
151 fprintf(stderr, "Error: %.32s\n", pkt);
153 } else if (!opcode || opcode > ERR) {
154 fprintf(stderr, "Received invalid packet: ");
155 pkt_print(pkt, stderr);
156 fprintf(stderr, ".\n");
162 pkt_print(pkt, stdout);
169 static ssize_t tftp_sendto(int sock, char *pkt, size_t len,
170 struct sockaddr_in *dst)
174 switch (pkt_num(pkt)) {
177 len = 2 + strlen(pkt + 2) + 1;
178 len += strlen(pkt + len) + 1;
187 len = 4 + strlen(pkt + 4);
190 fprintf(stderr, "Attempted to send invalid packet ");
191 pkt_print(pkt, stderr);
192 fprintf(stderr, "; this is a bug!\n");
198 pkt_print(pkt, stdout);
202 sent = sendto(sock, pkt, len, 0, (struct sockaddr*)dst, sizeof(*dst));
204 sock_perror("sendto");
210 #ifdef NMRPFLASH_WINDOWS
211 void sock_perror(const char *msg)
213 win_perror2(msg, WSAGetLastError());
216 inline void sock_perror(const char *msg)
222 int tftp_put(struct nmrpd_args *args)
224 struct sockaddr_in addr;
225 uint16_t block, port;
226 ssize_t len, last_len;
227 int fd, sock, ret, timeout, errors, ackblock;
228 char rx[TFTP_PKT_SIZE], tx[TFTP_PKT_SIZE];
233 fd = open(args->filename, O_RDONLY);
240 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
242 sock_perror("socket");
247 if ((addr.sin_addr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
252 addr.sin_family = AF_INET;
253 addr.sin_port = htons(args->port);
259 /* Not really, but this way the loop sends our WRQ before receiving */
262 pkt_mkwrq(tx, args->filename);
265 ackblock = pkt_num(rx) == ACK ? pkt_num(rx + 2) : -1;
267 if (timeout || ackblock == block) {
271 pkt_mknum(tx + 2, block);
272 len = read(fd, tx + 4, 512);
278 if (last_len != 512 && last_len != -1) {
286 ret = tftp_sendto(sock, tx, len, &addr);
290 } else if (pkt_num(rx) != ACK || ackblock > block) {
292 fprintf(stderr, "Expected ACK(%d), got ", block);
293 pkt_print(rx, stderr);
294 fprintf(stderr, ".\n");
297 if (ackblock != -1 && ++errors > 5) {
298 fprintf(stderr, "Protocol error; bailing out.\n");
304 ret = tftp_recvfrom(sock, rx, &port, args->rx_timeout);
311 fprintf(stderr, "Timeout while waiting for ACK(%d).\n", block);
313 fprintf(stderr, "Timeout while waiting for initial reply.\n");
321 if (!block && port != args->port) {
323 printf("Switching to port %d\n", port);
325 addr.sin_port = htons(port);
338 #ifndef NMRPFLASH_WINDOWS
339 shutdown(sock, SHUT_RDWR);
342 shutdown(sock, SD_BOTH);