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/>.
28 #define TFTP_PKT_SIZE 516
30 static const char *opcode_names[] = {
31 "RRQ", "WRQ", "DATA", "ACK", "ERR"
42 static inline void pkt_mknum(char *pkt, uint16_t n)
44 *(uint16_t*)pkt = htons(n);
47 static inline uint16_t pkt_num(char *pkt)
49 return ntohs(*(uint16_t*)pkt);
52 static void pkt_mkwrq(char *pkt, const char *filename, const char *mode)
58 strcpy(pkt + len, filename);
59 len += strlen(filename) + 1;
60 strcpy(pkt + len, mode);
61 len += strlen(mode) + 1;
64 static inline void pkt_print(char *pkt, FILE *fp)
66 uint16_t opcode = pkt_num(pkt);
67 if (!opcode || opcode > ERR) {
68 fprintf(fp, "(%d)", opcode);
70 fprintf(fp, "%s", opcode_names[opcode - 1]);
71 if (opcode == ACK || opcode == DATA) {
72 fprintf(fp, "(%d)", pkt_num(pkt + 2));
73 } else if (opcode == WRQ) {
74 fprintf(fp, "(%s, %s)", pkt + 2, pkt + 2 + strlen(pkt + 2) + 1);
79 static ssize_t tftp_recvfrom(int sock, char *pkt, struct sockaddr_in *src)
83 len = recvfrom(sock, pkt, TFTP_PKT_SIZE, 0, NULL, NULL);
85 if (errno != EAGAIN) {
93 uint16_t opcode = pkt_num(pkt);
96 fprintf(stderr, "Error (%d): %.511s\n", pkt_num(pkt + 2), pkt + 4);
98 } else if (!opcode || opcode > ERR) {
99 /* The EX2700 I've tested this on sends a raw TFTP packet with no
100 * opcode, and an error message starting at offset 0.
102 fprintf(stderr, "Error: %.32s\n", pkt);
109 static ssize_t tftp_sendto(int sock, char *pkt, size_t len,
110 struct sockaddr_in *dst)
114 switch (pkt_num(pkt)) {
117 len = 2 + strlen(pkt + 2) + 1;
118 len += strlen(pkt + len) + 1;
127 len = 4 + strlen(pkt + 4);
130 fprintf(stderr, "Error: Invalid packet ");
131 pkt_print(pkt, stderr);
135 sent = sendto(sock, pkt, len, 0, (struct sockaddr*)dst, sizeof(*dst));
143 int sock_set_rx_timeout(int fd, unsigned msec)
148 tv.tv_usec = (msec % 1000) * 1000;
149 tv.tv_sec = msec / 1000;
150 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv)) < 0) {
151 perror("setsockopt(SO_RCVTIMEO)");
159 int tftp_put(struct nmrpd_args *args)
161 struct sockaddr_in addr;
164 int fd, sock, err, timeout, last_len;
165 char rx[TFTP_PKT_SIZE], tx[TFTP_PKT_SIZE];
169 fd = open(args->filename, O_RDONLY);
176 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
183 err = sock_set_rx_timeout(sock, args->rx_timeout);
188 if ((addr.sin_addr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
193 addr.sin_family = AF_INET;
194 addr.sin_port = htons(args->port);
196 pkt_mkwrq(tx, args->filename, "octet");
198 len = tftp_sendto(sock, tx, 0, &addr);
204 len = tftp_recvfrom(sock, rx, &addr);
215 if (timeout || (pkt_num(rx) == ACK && pkt_num(rx + 2) == block)) {
219 pkt_mknum(tx + 2, block);
220 len = read(fd, tx + 4, 512);
226 if (last_len != 512) {
234 err = tftp_sendto(sock, tx, len, &addr);
238 } else if (pkt_num(rx) != ACK) {
239 fprintf(stderr, "Expected ACK(%d), got ", block);
240 pkt_print(rx, stderr);
241 fprintf(stderr, "!\n");
244 err = tftp_recvfrom(sock, rx, &addr);
250 fprintf(stderr, "Timeout while waiting for ACK(%d)\n.", block);