root / slirp / tftp.c @ 14899cdf
History | View | Annotate | Download (8.8 kB)
1 | c7f74643 | bellard | /*
|
---|---|---|---|
2 | c7f74643 | bellard | * tftp.c - a simple, read-only tftp server for qemu
|
3 | 5fafdf24 | ths | *
|
4 | c7f74643 | bellard | * Copyright (c) 2004 Magnus Damm <damm@opensource.se>
|
5 | 5fafdf24 | ths | *
|
6 | c7f74643 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | c7f74643 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | c7f74643 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | c7f74643 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | c7f74643 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | c7f74643 | bellard | * furnished to do so, subject to the following conditions:
|
12 | c7f74643 | bellard | *
|
13 | c7f74643 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | c7f74643 | bellard | * all copies or substantial portions of the Software.
|
15 | c7f74643 | bellard | *
|
16 | c7f74643 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | c7f74643 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | c7f74643 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | c7f74643 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | c7f74643 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | c7f74643 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | c7f74643 | bellard | * THE SOFTWARE.
|
23 | c7f74643 | bellard | */
|
24 | c7f74643 | bellard | |
25 | c7f74643 | bellard | #include <slirp.h> |
26 | 363a37d5 | blueswir1 | #include "qemu-common.h" // for pstrcpy |
27 | c7f74643 | bellard | |
28 | c7f74643 | bellard | struct tftp_session {
|
29 | a3504c87 | bellard | int in_use;
|
30 | a3504c87 | bellard | unsigned char filename[TFTP_FILENAME_MAX]; |
31 | 3b46e624 | ths | |
32 | a3504c87 | bellard | struct in_addr client_ip;
|
33 | a3504c87 | bellard | u_int16_t client_port; |
34 | 3b46e624 | ths | |
35 | a3504c87 | bellard | int timestamp;
|
36 | c7f74643 | bellard | }; |
37 | c7f74643 | bellard | |
38 | 9634d903 | blueswir1 | static struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX]; |
39 | c7f74643 | bellard | |
40 | 9bf05444 | bellard | const char *tftp_prefix; |
41 | c7f74643 | bellard | |
42 | c7f74643 | bellard | static void tftp_session_update(struct tftp_session *spt) |
43 | c7f74643 | bellard | { |
44 | a3504c87 | bellard | spt->timestamp = curtime; |
45 | a3504c87 | bellard | spt->in_use = 1;
|
46 | c7f74643 | bellard | } |
47 | c7f74643 | bellard | |
48 | c7f74643 | bellard | static void tftp_session_terminate(struct tftp_session *spt) |
49 | c7f74643 | bellard | { |
50 | c7f74643 | bellard | spt->in_use = 0;
|
51 | c7f74643 | bellard | } |
52 | c7f74643 | bellard | |
53 | c7f74643 | bellard | static int tftp_session_allocate(struct tftp_t *tp) |
54 | c7f74643 | bellard | { |
55 | c7f74643 | bellard | struct tftp_session *spt;
|
56 | c7f74643 | bellard | int k;
|
57 | c7f74643 | bellard | |
58 | c7f74643 | bellard | for (k = 0; k < TFTP_SESSIONS_MAX; k++) { |
59 | c7f74643 | bellard | spt = &tftp_sessions[k]; |
60 | c7f74643 | bellard | |
61 | a3504c87 | bellard | if (!spt->in_use)
|
62 | a3504c87 | bellard | goto found;
|
63 | c7f74643 | bellard | |
64 | c7f74643 | bellard | /* sessions time out after 5 inactive seconds */
|
65 | a3504c87 | bellard | if ((int)(curtime - spt->timestamp) > 5000) |
66 | a3504c87 | bellard | goto found;
|
67 | c7f74643 | bellard | } |
68 | c7f74643 | bellard | |
69 | c7f74643 | bellard | return -1; |
70 | c7f74643 | bellard | |
71 | c7f74643 | bellard | found:
|
72 | c7f74643 | bellard | memset(spt, 0, sizeof(*spt)); |
73 | c7f74643 | bellard | memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
|
74 | c7f74643 | bellard | spt->client_port = tp->udp.uh_sport; |
75 | c7f74643 | bellard | |
76 | c7f74643 | bellard | tftp_session_update(spt); |
77 | c7f74643 | bellard | |
78 | c7f74643 | bellard | return k;
|
79 | c7f74643 | bellard | } |
80 | c7f74643 | bellard | |
81 | c7f74643 | bellard | static int tftp_session_find(struct tftp_t *tp) |
82 | c7f74643 | bellard | { |
83 | c7f74643 | bellard | struct tftp_session *spt;
|
84 | c7f74643 | bellard | int k;
|
85 | c7f74643 | bellard | |
86 | c7f74643 | bellard | for (k = 0; k < TFTP_SESSIONS_MAX; k++) { |
87 | c7f74643 | bellard | spt = &tftp_sessions[k]; |
88 | c7f74643 | bellard | |
89 | c7f74643 | bellard | if (spt->in_use) {
|
90 | c7f74643 | bellard | if (!memcmp(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip))) { |
91 | c7f74643 | bellard | if (spt->client_port == tp->udp.uh_sport) {
|
92 | c7f74643 | bellard | return k;
|
93 | c7f74643 | bellard | } |
94 | c7f74643 | bellard | } |
95 | c7f74643 | bellard | } |
96 | c7f74643 | bellard | } |
97 | c7f74643 | bellard | |
98 | c7f74643 | bellard | return -1; |
99 | c7f74643 | bellard | } |
100 | c7f74643 | bellard | |
101 | c7f74643 | bellard | static int tftp_read_data(struct tftp_session *spt, u_int16_t block_nr, |
102 | c7f74643 | bellard | u_int8_t *buf, int len)
|
103 | c7f74643 | bellard | { |
104 | c7f74643 | bellard | int fd;
|
105 | c7f74643 | bellard | int bytes_read = 0; |
106 | 0db1137d | ths | char buffer[1024]; |
107 | 0db1137d | ths | int n;
|
108 | c7f74643 | bellard | |
109 | 0db1137d | ths | n = snprintf(buffer, sizeof(buffer), "%s/%s", |
110 | 0db1137d | ths | tftp_prefix, spt->filename); |
111 | 0db1137d | ths | if (n >= sizeof(buffer)) |
112 | 0db1137d | ths | return -1; |
113 | 0db1137d | ths | |
114 | 0db1137d | ths | fd = open(buffer, O_RDONLY | O_BINARY); |
115 | c7f74643 | bellard | |
116 | c7f74643 | bellard | if (fd < 0) { |
117 | c7f74643 | bellard | return -1; |
118 | c7f74643 | bellard | } |
119 | c7f74643 | bellard | |
120 | c7f74643 | bellard | if (len) {
|
121 | c7f74643 | bellard | lseek(fd, block_nr * 512, SEEK_SET);
|
122 | c7f74643 | bellard | |
123 | c7f74643 | bellard | bytes_read = read(fd, buf, len); |
124 | c7f74643 | bellard | } |
125 | c7f74643 | bellard | |
126 | c7f74643 | bellard | close(fd); |
127 | c7f74643 | bellard | |
128 | c7f74643 | bellard | return bytes_read;
|
129 | c7f74643 | bellard | } |
130 | c7f74643 | bellard | |
131 | 5fafdf24 | ths | static int tftp_send_oack(struct tftp_session *spt, |
132 | 1f697db9 | ths | const char *key, uint32_t value, |
133 | 1f697db9 | ths | struct tftp_t *recv_tp)
|
134 | 1f697db9 | ths | { |
135 | 1f697db9 | ths | struct sockaddr_in saddr, daddr;
|
136 | 1f697db9 | ths | struct mbuf *m;
|
137 | 1f697db9 | ths | struct tftp_t *tp;
|
138 | 1f697db9 | ths | int n = 0; |
139 | 1f697db9 | ths | |
140 | 1f697db9 | ths | m = m_get(); |
141 | 1f697db9 | ths | |
142 | 1f697db9 | ths | if (!m)
|
143 | 1f697db9 | ths | return -1; |
144 | 1f697db9 | ths | |
145 | 1f697db9 | ths | memset(m->m_data, 0, m->m_size);
|
146 | 1f697db9 | ths | |
147 | 9634d903 | blueswir1 | m->m_data += IF_MAXLINKHDR; |
148 | 1f697db9 | ths | tp = (void *)m->m_data;
|
149 | 1f697db9 | ths | m->m_data += sizeof(struct udpiphdr); |
150 | 3b46e624 | ths | |
151 | 1f697db9 | ths | tp->tp_op = htons(TFTP_OACK); |
152 | b55266b5 | blueswir1 | n += snprintf((char *)tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s", |
153 | b55266b5 | blueswir1 | key) + 1;
|
154 | b55266b5 | blueswir1 | n += snprintf((char *)tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u", |
155 | b55266b5 | blueswir1 | value) + 1;
|
156 | 1f697db9 | ths | |
157 | 1f697db9 | ths | saddr.sin_addr = recv_tp->ip.ip_dst; |
158 | 1f697db9 | ths | saddr.sin_port = recv_tp->udp.uh_dport; |
159 | 3b46e624 | ths | |
160 | 1f697db9 | ths | daddr.sin_addr = spt->client_ip; |
161 | 1f697db9 | ths | daddr.sin_port = spt->client_port; |
162 | 1f697db9 | ths | |
163 | 5fafdf24 | ths | m->m_len = sizeof(struct tftp_t) - 514 + n - |
164 | 1f697db9 | ths | sizeof(struct ip) - sizeof(struct udphdr); |
165 | 1f697db9 | ths | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
166 | 1f697db9 | ths | |
167 | 1f697db9 | ths | return 0; |
168 | 1f697db9 | ths | } |
169 | 1f697db9 | ths | |
170 | 1f697db9 | ths | |
171 | 1f697db9 | ths | |
172 | 5fafdf24 | ths | static int tftp_send_error(struct tftp_session *spt, |
173 | c7f74643 | bellard | u_int16_t errorcode, const char *msg, |
174 | c7f74643 | bellard | struct tftp_t *recv_tp)
|
175 | c7f74643 | bellard | { |
176 | c7f74643 | bellard | struct sockaddr_in saddr, daddr;
|
177 | c7f74643 | bellard | struct mbuf *m;
|
178 | c7f74643 | bellard | struct tftp_t *tp;
|
179 | c7f74643 | bellard | int nobytes;
|
180 | c7f74643 | bellard | |
181 | c7f74643 | bellard | m = m_get(); |
182 | c7f74643 | bellard | |
183 | c7f74643 | bellard | if (!m) {
|
184 | c7f74643 | bellard | return -1; |
185 | c7f74643 | bellard | } |
186 | c7f74643 | bellard | |
187 | c7f74643 | bellard | memset(m->m_data, 0, m->m_size);
|
188 | c7f74643 | bellard | |
189 | 9634d903 | blueswir1 | m->m_data += IF_MAXLINKHDR; |
190 | c7f74643 | bellard | tp = (void *)m->m_data;
|
191 | c7f74643 | bellard | m->m_data += sizeof(struct udpiphdr); |
192 | 3b46e624 | ths | |
193 | c7f74643 | bellard | tp->tp_op = htons(TFTP_ERROR); |
194 | c7f74643 | bellard | tp->x.tp_error.tp_error_code = htons(errorcode); |
195 | b55266b5 | blueswir1 | pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg); |
196 | c7f74643 | bellard | |
197 | c7f74643 | bellard | saddr.sin_addr = recv_tp->ip.ip_dst; |
198 | c7f74643 | bellard | saddr.sin_port = recv_tp->udp.uh_dport; |
199 | c7f74643 | bellard | |
200 | c7f74643 | bellard | daddr.sin_addr = spt->client_ip; |
201 | c7f74643 | bellard | daddr.sin_port = spt->client_port; |
202 | c7f74643 | bellard | |
203 | c7f74643 | bellard | nobytes = 2;
|
204 | c7f74643 | bellard | |
205 | 5fafdf24 | ths | m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) - |
206 | c7f74643 | bellard | sizeof(struct ip) - sizeof(struct udphdr); |
207 | c7f74643 | bellard | |
208 | c7f74643 | bellard | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
209 | c7f74643 | bellard | |
210 | c7f74643 | bellard | tftp_session_terminate(spt); |
211 | c7f74643 | bellard | |
212 | c7f74643 | bellard | return 0; |
213 | c7f74643 | bellard | } |
214 | c7f74643 | bellard | |
215 | 5fafdf24 | ths | static int tftp_send_data(struct tftp_session *spt, |
216 | c7f74643 | bellard | u_int16_t block_nr, |
217 | c7f74643 | bellard | struct tftp_t *recv_tp)
|
218 | c7f74643 | bellard | { |
219 | c7f74643 | bellard | struct sockaddr_in saddr, daddr;
|
220 | c7f74643 | bellard | struct mbuf *m;
|
221 | c7f74643 | bellard | struct tftp_t *tp;
|
222 | c7f74643 | bellard | int nobytes;
|
223 | c7f74643 | bellard | |
224 | c7f74643 | bellard | if (block_nr < 1) { |
225 | c7f74643 | bellard | return -1; |
226 | c7f74643 | bellard | } |
227 | c7f74643 | bellard | |
228 | c7f74643 | bellard | m = m_get(); |
229 | c7f74643 | bellard | |
230 | c7f74643 | bellard | if (!m) {
|
231 | c7f74643 | bellard | return -1; |
232 | c7f74643 | bellard | } |
233 | c7f74643 | bellard | |
234 | c7f74643 | bellard | memset(m->m_data, 0, m->m_size);
|
235 | c7f74643 | bellard | |
236 | 9634d903 | blueswir1 | m->m_data += IF_MAXLINKHDR; |
237 | c7f74643 | bellard | tp = (void *)m->m_data;
|
238 | c7f74643 | bellard | m->m_data += sizeof(struct udpiphdr); |
239 | 3b46e624 | ths | |
240 | c7f74643 | bellard | tp->tp_op = htons(TFTP_DATA); |
241 | c7f74643 | bellard | tp->x.tp_data.tp_block_nr = htons(block_nr); |
242 | c7f74643 | bellard | |
243 | c7f74643 | bellard | saddr.sin_addr = recv_tp->ip.ip_dst; |
244 | c7f74643 | bellard | saddr.sin_port = recv_tp->udp.uh_dport; |
245 | c7f74643 | bellard | |
246 | c7f74643 | bellard | daddr.sin_addr = spt->client_ip; |
247 | c7f74643 | bellard | daddr.sin_port = spt->client_port; |
248 | c7f74643 | bellard | |
249 | c7f74643 | bellard | nobytes = tftp_read_data(spt, block_nr - 1, tp->x.tp_data.tp_buf, 512); |
250 | c7f74643 | bellard | |
251 | c7f74643 | bellard | if (nobytes < 0) { |
252 | c7f74643 | bellard | m_free(m); |
253 | c7f74643 | bellard | |
254 | c7f74643 | bellard | /* send "file not found" error back */
|
255 | c7f74643 | bellard | |
256 | c7f74643 | bellard | tftp_send_error(spt, 1, "File not found", tp); |
257 | c7f74643 | bellard | |
258 | c7f74643 | bellard | return -1; |
259 | c7f74643 | bellard | } |
260 | c7f74643 | bellard | |
261 | 5fafdf24 | ths | m->m_len = sizeof(struct tftp_t) - (512 - nobytes) - |
262 | c7f74643 | bellard | sizeof(struct ip) - sizeof(struct udphdr); |
263 | c7f74643 | bellard | |
264 | c7f74643 | bellard | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
265 | c7f74643 | bellard | |
266 | c7f74643 | bellard | if (nobytes == 512) { |
267 | c7f74643 | bellard | tftp_session_update(spt); |
268 | c7f74643 | bellard | } |
269 | c7f74643 | bellard | else {
|
270 | c7f74643 | bellard | tftp_session_terminate(spt); |
271 | c7f74643 | bellard | } |
272 | c7f74643 | bellard | |
273 | c7f74643 | bellard | return 0; |
274 | c7f74643 | bellard | } |
275 | c7f74643 | bellard | |
276 | c7f74643 | bellard | static void tftp_handle_rrq(struct tftp_t *tp, int pktlen) |
277 | c7f74643 | bellard | { |
278 | c7f74643 | bellard | struct tftp_session *spt;
|
279 | c7f74643 | bellard | int s, k, n;
|
280 | c7f74643 | bellard | u_int8_t *src, *dst; |
281 | c7f74643 | bellard | |
282 | c7f74643 | bellard | s = tftp_session_allocate(tp); |
283 | c7f74643 | bellard | |
284 | c7f74643 | bellard | if (s < 0) { |
285 | c7f74643 | bellard | return;
|
286 | c7f74643 | bellard | } |
287 | c7f74643 | bellard | |
288 | c7f74643 | bellard | spt = &tftp_sessions[s]; |
289 | c7f74643 | bellard | |
290 | c7f74643 | bellard | src = tp->x.tp_buf; |
291 | c7f74643 | bellard | dst = spt->filename; |
292 | c7f74643 | bellard | n = pktlen - ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
|
293 | c7f74643 | bellard | |
294 | c7f74643 | bellard | /* get name */
|
295 | c7f74643 | bellard | |
296 | c7f74643 | bellard | for (k = 0; k < n; k++) { |
297 | c7f74643 | bellard | if (k < TFTP_FILENAME_MAX) {
|
298 | c7f74643 | bellard | dst[k] = src[k]; |
299 | c7f74643 | bellard | } |
300 | c7f74643 | bellard | else {
|
301 | c7f74643 | bellard | return;
|
302 | c7f74643 | bellard | } |
303 | 3b46e624 | ths | |
304 | c7f74643 | bellard | if (src[k] == '\0') { |
305 | c7f74643 | bellard | break;
|
306 | c7f74643 | bellard | } |
307 | c7f74643 | bellard | } |
308 | 3b46e624 | ths | |
309 | c7f74643 | bellard | if (k >= n) {
|
310 | c7f74643 | bellard | return;
|
311 | c7f74643 | bellard | } |
312 | 3b46e624 | ths | |
313 | c7f74643 | bellard | k++; |
314 | 3b46e624 | ths | |
315 | c7f74643 | bellard | /* check mode */
|
316 | c7f74643 | bellard | if ((n - k) < 6) { |
317 | c7f74643 | bellard | return;
|
318 | c7f74643 | bellard | } |
319 | 3b46e624 | ths | |
320 | c7f74643 | bellard | if (memcmp(&src[k], "octet\0", 6) != 0) { |
321 | c7f74643 | bellard | tftp_send_error(spt, 4, "Unsupported transfer mode", tp); |
322 | c7f74643 | bellard | return;
|
323 | c7f74643 | bellard | } |
324 | c7f74643 | bellard | |
325 | 1f697db9 | ths | k += 6; /* skipping octet */ |
326 | 1f697db9 | ths | |
327 | c7f74643 | bellard | /* do sanity checks on the filename */
|
328 | c7f74643 | bellard | |
329 | c7f74643 | bellard | if ((spt->filename[0] != '/') |
330 | b55266b5 | blueswir1 | || (spt->filename[strlen((char *)spt->filename) - 1] == '/') |
331 | b55266b5 | blueswir1 | || strstr((char *)spt->filename, "/../")) { |
332 | c7f74643 | bellard | tftp_send_error(spt, 2, "Access violation", tp); |
333 | c7f74643 | bellard | return;
|
334 | c7f74643 | bellard | } |
335 | c7f74643 | bellard | |
336 | c7f74643 | bellard | /* only allow exported prefixes */
|
337 | c7f74643 | bellard | |
338 | 0db1137d | ths | if (!tftp_prefix) {
|
339 | c7f74643 | bellard | tftp_send_error(spt, 2, "Access violation", tp); |
340 | c7f74643 | bellard | return;
|
341 | c7f74643 | bellard | } |
342 | c7f74643 | bellard | |
343 | c7f74643 | bellard | /* check if the file exists */
|
344 | 3b46e624 | ths | |
345 | c7f74643 | bellard | if (tftp_read_data(spt, 0, spt->filename, 0) < 0) { |
346 | c7f74643 | bellard | tftp_send_error(spt, 1, "File not found", tp); |
347 | c7f74643 | bellard | return;
|
348 | c7f74643 | bellard | } |
349 | c7f74643 | bellard | |
350 | 1f697db9 | ths | if (src[n - 1] != 0) { |
351 | 1f697db9 | ths | tftp_send_error(spt, 2, "Access violation", tp); |
352 | 1f697db9 | ths | return;
|
353 | 1f697db9 | ths | } |
354 | 1f697db9 | ths | |
355 | 1f697db9 | ths | while (k < n) {
|
356 | 1f697db9 | ths | const char *key, *value; |
357 | 1f697db9 | ths | |
358 | b55266b5 | blueswir1 | key = (char *)src + k;
|
359 | 1f697db9 | ths | k += strlen(key) + 1;
|
360 | 1f697db9 | ths | |
361 | 1f697db9 | ths | if (k >= n) {
|
362 | 1f697db9 | ths | tftp_send_error(spt, 2, "Access violation", tp); |
363 | 1f697db9 | ths | return;
|
364 | 1f697db9 | ths | } |
365 | 1f697db9 | ths | |
366 | b55266b5 | blueswir1 | value = (char *)src + k;
|
367 | 1f697db9 | ths | k += strlen(value) + 1;
|
368 | 1f697db9 | ths | |
369 | 1f697db9 | ths | if (strcmp(key, "tsize") == 0) { |
370 | 1f697db9 | ths | int tsize = atoi(value);
|
371 | 1f697db9 | ths | struct stat stat_p;
|
372 | 1f697db9 | ths | |
373 | 1f697db9 | ths | if (tsize == 0 && tftp_prefix) { |
374 | 1f697db9 | ths | char buffer[1024]; |
375 | 1f697db9 | ths | int len;
|
376 | 1f697db9 | ths | |
377 | 1f697db9 | ths | len = snprintf(buffer, sizeof(buffer), "%s/%s", |
378 | 1f697db9 | ths | tftp_prefix, spt->filename); |
379 | 1f697db9 | ths | |
380 | 1f697db9 | ths | if (stat(buffer, &stat_p) == 0) |
381 | 1f697db9 | ths | tsize = stat_p.st_size; |
382 | 1f697db9 | ths | else {
|
383 | 1f697db9 | ths | tftp_send_error(spt, 1, "File not found", tp); |
384 | 1f697db9 | ths | return;
|
385 | 1f697db9 | ths | } |
386 | 1f697db9 | ths | } |
387 | 1f697db9 | ths | |
388 | 1f697db9 | ths | tftp_send_oack(spt, "tsize", tsize, tp);
|
389 | 1f697db9 | ths | } |
390 | 1f697db9 | ths | } |
391 | 1f697db9 | ths | |
392 | c7f74643 | bellard | tftp_send_data(spt, 1, tp);
|
393 | c7f74643 | bellard | } |
394 | c7f74643 | bellard | |
395 | c7f74643 | bellard | static void tftp_handle_ack(struct tftp_t *tp, int pktlen) |
396 | c7f74643 | bellard | { |
397 | c7f74643 | bellard | int s;
|
398 | c7f74643 | bellard | |
399 | c7f74643 | bellard | s = tftp_session_find(tp); |
400 | c7f74643 | bellard | |
401 | c7f74643 | bellard | if (s < 0) { |
402 | c7f74643 | bellard | return;
|
403 | c7f74643 | bellard | } |
404 | c7f74643 | bellard | |
405 | 5fafdf24 | ths | if (tftp_send_data(&tftp_sessions[s],
|
406 | 5fafdf24 | ths | ntohs(tp->x.tp_data.tp_block_nr) + 1,
|
407 | c7f74643 | bellard | tp) < 0) {
|
408 | c7f74643 | bellard | return;
|
409 | c7f74643 | bellard | } |
410 | c7f74643 | bellard | } |
411 | c7f74643 | bellard | |
412 | c7f74643 | bellard | void tftp_input(struct mbuf *m) |
413 | c7f74643 | bellard | { |
414 | c7f74643 | bellard | struct tftp_t *tp = (struct tftp_t *)m->m_data; |
415 | c7f74643 | bellard | |
416 | c7f74643 | bellard | switch(ntohs(tp->tp_op)) {
|
417 | c7f74643 | bellard | case TFTP_RRQ:
|
418 | c7f74643 | bellard | tftp_handle_rrq(tp, m->m_len); |
419 | c7f74643 | bellard | break;
|
420 | c7f74643 | bellard | |
421 | c7f74643 | bellard | case TFTP_ACK:
|
422 | c7f74643 | bellard | tftp_handle_ack(tp, m->m_len); |
423 | c7f74643 | bellard | break;
|
424 | c7f74643 | bellard | } |
425 | c7f74643 | bellard | } |