Many Windows fixes
[oweals/nmrpflash.git] / tftp.c
1 /**
2  * nmrp-flash - Netgear Unbrick Utility
3  * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
4  *
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.
9  *
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.
14  *
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/>.
17  *
18  */
19
20 #define _BSD_SOURCE
21 #include <string.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include "nmrpd.h"
27
28 #define TFTP_PKT_SIZE 516
29
30 static const char *opcode_names[] = { 
31         "RRQ", "WRQ", "DATA", "ACK", "ERR"
32 };
33
34 enum tftp_opcode {
35         RRQ  = 1,
36         WRQ  = 2,
37         DATA = 3,
38         ACK  = 4,
39         ERR  = 5
40 };
41
42 static inline void pkt_mknum(char *pkt, uint16_t n)
43 {
44         *(uint16_t*)pkt = htons(n);
45 }
46
47 static inline uint16_t pkt_num(char *pkt)
48 {
49         return ntohs(*(uint16_t*)pkt);
50 }
51
52 static void pkt_mkwrq(char *pkt, const char *filename, const char *mode)
53 {
54         size_t len = 2;
55
56         pkt_mknum(pkt, WRQ);
57
58         strcpy(pkt + len, filename);
59         len += strlen(filename) + 1;
60         strcpy(pkt + len, mode);
61         len += strlen(mode) + 1;
62 }
63
64 static inline void pkt_print(char *pkt, FILE *fp)
65 {
66         uint16_t opcode = pkt_num(pkt);
67         if (!opcode || opcode > ERR) {
68                 fprintf(fp, "(%d)", opcode);
69         } else {
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);
75                 }
76         }
77 }
78
79 static ssize_t tftp_recvfrom(int sock, char *pkt, struct sockaddr_in *src)
80 {
81         ssize_t len;
82
83         len = recvfrom(sock, pkt, TFTP_PKT_SIZE, 0, NULL, NULL);
84         if (len < 0) {
85                 if (errno != EAGAIN) {
86                         perror("recvfrom");
87                         return -1;
88                 }
89
90                 return -2;
91         }
92
93         uint16_t opcode = pkt_num(pkt);
94
95         if (opcode == ERR) {
96                 fprintf(stderr, "Error (%d): %.511s\n", pkt_num(pkt + 2), pkt + 4);
97                 return -1;
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.
101                  */
102                 fprintf(stderr, "Error: %.32s\n", pkt);
103                 return -3;
104         }
105
106         return len;
107 }
108
109 static ssize_t tftp_sendto(int sock, char *pkt, size_t len, 
110                 struct sockaddr_in *dst)
111 {
112         ssize_t sent;
113
114         switch (pkt_num(pkt)) {
115                 case RRQ:
116                 case WRQ:
117                         len = 2 + strlen(pkt + 2) + 1;
118                         len += strlen(pkt + len) + 1;
119                         break;
120                 case DATA:
121                         len += 4;
122                         break;
123                 case ACK:
124                         len = 4;
125                         break;
126                 case ERR:
127                         len = 4 + strlen(pkt + 4);
128                         break;
129                 default:
130                         fprintf(stderr, "Error: Invalid packet ");
131                         pkt_print(pkt, stderr);
132                         return -1;
133         }
134
135         sent = sendto(sock, pkt, len, 0, (struct sockaddr*)dst, sizeof(*dst));
136         if (sent < 0) {
137                 perror("sendto");
138         }
139
140         return sent;
141 }
142
143 int sock_set_rx_timeout(int fd, unsigned msec)
144 {
145         struct timeval tv;
146
147         if (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)");
152                         return 1;
153                 }
154         }
155
156         return 0;
157 }
158
159 int tftp_put(struct nmrpd_args *args)
160 {
161         struct sockaddr_in addr;
162         uint16_t block;
163         ssize_t len;
164         int fd, sock, err, timeout, last_len;
165         char rx[TFTP_PKT_SIZE], tx[TFTP_PKT_SIZE];
166
167         sock = -1;
168
169         fd = open(args->filename, O_RDONLY);
170         if (fd < 0) {
171                 perror("open");
172                 err = fd;
173                 goto cleanup;
174         }
175
176         sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
177         if (sock < 0) {
178                 perror("socket");
179                 err = sock;
180                 goto cleanup;
181         }
182
183         err = sock_set_rx_timeout(sock, args->rx_timeout);
184         if (err) {
185                 goto cleanup;
186         }
187
188         if ((addr.sin_addr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
189                 perror("inet_addr");
190                 goto cleanup;
191         }
192
193         addr.sin_family = AF_INET;
194         addr.sin_port = htons(args->port);
195
196         pkt_mkwrq(tx, args->filename, "octet");
197
198         len = tftp_sendto(sock, tx, 0, &addr);
199         if (len < 0) {
200                 err = len;
201                 goto cleanup;
202         }
203
204         len = tftp_recvfrom(sock, rx, &addr);
205         if (len < 0) {
206                 err = len;
207                 goto cleanup;
208         }
209
210         timeout = 0;
211         block = 0;
212         last_len = -1;
213
214         do {
215                 if (timeout || (pkt_num(rx) == ACK && pkt_num(rx + 2) == block)) {
216                         if (!timeout) {
217                                 ++block;
218                                 pkt_mknum(tx, DATA);
219                                 pkt_mknum(tx + 2, block);
220                                 len = read(fd, tx + 4, 512);
221                                 if (len < 0) {
222                                         perror("read");
223                                         err = len;
224                                         goto cleanup;
225                                 } else if (!len) {
226                                         if (last_len != 512) {
227                                                 break;
228                                         }
229                                 }
230
231                                 last_len = len;
232                         }
233
234                         err = tftp_sendto(sock, tx, len, &addr);
235                         if (err < 0) {
236                                 goto cleanup;
237                         }
238                 } else if (pkt_num(rx) != ACK) {
239                         fprintf(stderr, "Expected ACK(%d), got ", block);
240                         pkt_print(rx, stderr);
241                         fprintf(stderr, "!\n");
242                 }
243
244                 err = tftp_recvfrom(sock, rx, &addr);
245                 if (err < 0) {
246                         if (err == -2) {
247                                 if (++timeout < 5) {
248                                         continue;
249                                 }
250                                 fprintf(stderr, "Timeout while waiting for ACK(%d)\n.", block);
251                         }
252                         goto cleanup;
253                 } else {
254                         timeout = 0;
255                         err = 0;
256                 }
257         } while(1);
258
259         err = 0;
260
261 cleanup:
262         if (fd >= 0) {
263                 close(fd);
264         }
265
266         if (sock >= 0) {
267                 close(sock);
268         }
269
270         return err;
271 }