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/>.
21 #include <arpa/inet.h>
22 #include <sys/socket.h>
31 #define TFTP_PKT_SIZE 516
33 static const char *opcode_names[] = {
34 "RRQ", "WRQ", "DATA", "ACK", "ERR"
45 static inline void pkt_mknum(char *pkt, uint16_t n)
47 *(uint16_t*)pkt = htons(n);
50 static inline uint16_t pkt_num(char *pkt)
52 return ntohs(*(uint16_t*)pkt);
55 static void pkt_mkwrq(char *pkt, const char *filename, const char *mode)
61 strcpy(pkt + len, filename);
62 len += strlen(filename) + 1;
63 strcpy(pkt + len, mode);
64 len += strlen(mode) + 1;
67 static inline void pkt_print(char *pkt, FILE *fp)
69 uint16_t opcode = pkt_num(pkt);
70 if (!opcode || opcode > ERR) {
71 fprintf(fp, "(%d)", opcode);
73 fprintf(fp, "%s", opcode_names[opcode - 1]);
74 if (opcode == ACK || opcode == DATA) {
75 fprintf(fp, "(%d)", pkt_num(pkt + 2));
76 } else if (opcode == WRQ) {
77 fprintf(fp, "(%s, %s)", pkt + 2, pkt + 2 + strlen(pkt + 2) + 1);
82 static ssize_t tftp_recvfrom(int sock, char *pkt, struct sockaddr_in *src)
87 (void)src, (void)socklen;
89 len = recvfrom(sock, pkt, TFTP_PKT_SIZE, 0, NULL, NULL);
91 if (errno != EAGAIN) {
99 uint16_t opcode = pkt_num(pkt);
102 fprintf(stderr, "Error (%d): %.511s\n", pkt_num(pkt + 2), pkt + 4);
104 } else if (!opcode || opcode > ERR) {
105 /* The EX2700 I've tested this on sends a raw TFTP packet with no
106 * opcode, and an error message starting at offset 0.
108 fprintf(stderr, "Error: %.32s\n", pkt);
115 static ssize_t tftp_sendto(int sock, char *pkt, size_t len,
116 struct sockaddr_in *dst)
120 switch (pkt_num(pkt)) {
123 len = 2 + strlen(pkt + 2) + 1;
124 len += strlen(pkt + len) + 1;
133 len = 4 + strlen(pkt + 4);
136 fprintf(stderr, "Error: Invalid packet ");
137 pkt_print(pkt, stderr);
141 sent = sendto(sock, pkt, len, 0, (struct sockaddr*)dst, sizeof(*dst));
149 int sock_set_rx_timeout(int fd, unsigned msec)
154 tv.tv_usec = (msec % 1000) * 1000;
155 tv.tv_sec = msec / 1000;
156 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
157 perror("setsockopt(SO_RCVTIMEO)");
165 int tftp_put(struct nmrpd_args *args)
167 struct sockaddr_in addr;
170 int fd, sock, err, timeout, last_len;
171 char rx[TFTP_PKT_SIZE], tx[TFTP_PKT_SIZE];
173 fd = open(args->filename, O_RDONLY);
180 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
187 err = sock_set_rx_timeout(sock, args->rx_timeout);
192 err = !inet_aton(args->ipaddr, &addr.sin_addr);
198 addr.sin_family = AF_INET;
199 addr.sin_port = htons(args->port);
201 pkt_mkwrq(tx, args->filename, "octet");
203 len = tftp_sendto(sock, tx, 0, &addr);
209 len = tftp_recvfrom(sock, rx, &addr);
220 if (timeout || (pkt_num(rx) == ACK && pkt_num(rx + 2) == block)) {
224 pkt_mknum(tx + 2, block);
225 len = read(fd, tx + 4, 512);
231 if (last_len != 512) {
239 err = tftp_sendto(sock, tx, len, &addr);
243 } else if (pkt_num(rx) != ACK) {
244 fprintf(stderr, "Expected ACK(%d), got ", block);
245 pkt_print(rx, stderr);
246 fprintf(stderr, "!\n");
249 err = tftp_recvfrom(sock, rx, &addr);
255 fprintf(stderr, "Timeout while waiting for ACK(%d)\n.", block);