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 char *x_basename(const char *path)
47 slash = rindex(path, '/');
48 bslash = rindex(path, '\\');
50 if (slash && bslash) {
51 path = 1 + (slash > bslash ? slash : bslash);
61 static const char *sanitize_netascii(char *str)
66 if (*p < 0x20 || *p > 0x7f) {
74 static inline void pkt_mknum(char *pkt, uint16_t n)
76 *(uint16_t*)pkt = htons(n);
79 static inline uint16_t pkt_num(char *pkt)
81 return ntohs(*(uint16_t*)pkt);
84 static void pkt_mkwrq(char *pkt, const char *filename, const char *mode)
90 strcpy(pkt + len, filename);
91 len += strlen(filename) + 1;
92 strcpy(pkt + len, mode);
93 len += strlen(mode) + 1;
96 static inline void pkt_print(char *pkt, FILE *fp)
98 uint16_t opcode = pkt_num(pkt);
99 if (!opcode || opcode > ERR) {
100 fprintf(fp, "(%d)", opcode);
102 fprintf(fp, "%s", opcode_names[opcode - 1]);
103 if (opcode == ACK || opcode == DATA) {
104 fprintf(fp, "(%d)", pkt_num(pkt + 2));
105 } else if (opcode == WRQ) {
106 fprintf(fp, "(%s, %s)", pkt + 2, pkt + 2 + strlen(pkt + 2) + 1);
111 static ssize_t tftp_recvfrom(int sock, char *pkt, struct sockaddr_in *src)
115 len = recvfrom(sock, pkt, TFTP_PKT_SIZE, 0, NULL, NULL);
117 if (errno != EAGAIN) {
125 uint16_t opcode = pkt_num(pkt);
128 fprintf(stderr, "Error (%d): %.511s\n", pkt_num(pkt + 2), pkt + 4);
130 } else if (!opcode || opcode > ERR) {
131 /* The EX2700 I've tested this on sends a raw TFTP packet with no
132 * opcode, and an error message starting at offset 0.
134 fprintf(stderr, "Error: %.32s\n", pkt);
141 static ssize_t tftp_sendto(int sock, char *pkt, size_t len,
142 struct sockaddr_in *dst)
146 switch (pkt_num(pkt)) {
149 len = 2 + strlen(pkt + 2) + 1;
150 len += strlen(pkt + len) + 1;
159 len = 4 + strlen(pkt + 4);
162 fprintf(stderr, "Error: Invalid packet ");
163 pkt_print(pkt, stderr);
167 sent = sendto(sock, pkt, len, 0, (struct sockaddr*)dst, sizeof(*dst));
175 int sock_set_rx_timeout(int fd, unsigned msec)
180 tv.tv_usec = (msec % 1000) * 1000;
181 tv.tv_sec = msec / 1000;
182 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv)) < 0) {
183 perror("setsockopt(SO_RCVTIMEO)");
191 int tftp_put(struct nmrpd_args *args)
193 struct sockaddr_in addr;
197 int fd, sock, err, timeout, last_len;
198 char rx[TFTP_PKT_SIZE], tx[TFTP_PKT_SIZE];
203 fd = open(args->filename, O_RDONLY);
210 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
217 err = sock_set_rx_timeout(sock, args->rx_timeout);
222 if ((addr.sin_addr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
227 addr.sin_family = AF_INET;
228 addr.sin_port = htons(args->port);
230 filename = x_basename(args->filename);
232 perror("x_basename");
234 } else if (strlen(filename) > 256) {
235 fprintf(stderr, "Filename exceeds maximum of 256 characters.\n");
239 sanitize_netascii(filename);
241 printf("%s -> %s\n", args->filename, filename);
244 pkt_mkwrq(tx, filename, "octet");
246 len = tftp_sendto(sock, tx, 0, &addr);
252 len = tftp_recvfrom(sock, rx, &addr);
263 if (timeout || (pkt_num(rx) == ACK && pkt_num(rx + 2) == block)) {
267 pkt_mknum(tx + 2, block);
268 len = read(fd, tx + 4, 512);
274 if (last_len != 512) {
282 err = tftp_sendto(sock, tx, len, &addr);
286 } else if (pkt_num(rx) != ACK) {
287 fprintf(stderr, "Expected ACK(%d), got ", block);
288 pkt_print(rx, stderr);
289 fprintf(stderr, "!\n");
292 err = tftp_recvfrom(sock, rx, &addr);
298 fprintf(stderr, "Timeout while waiting for ACK(%d)\n.", block);
317 shutdown(sock, SHUT_RDWR);
318 #ifndef NMRPFLASH_WINDOWS