root / slirp / tftp.c @ f97572e5
History | View | Annotate | Download (8.7 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 | 363a37d5 | blueswir1 | n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s", key) + 1; |
153 | 363a37d5 | blueswir1 | n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u", value) + 1; |
154 | 1f697db9 | ths | |
155 | 1f697db9 | ths | saddr.sin_addr = recv_tp->ip.ip_dst; |
156 | 1f697db9 | ths | saddr.sin_port = recv_tp->udp.uh_dport; |
157 | 3b46e624 | ths | |
158 | 1f697db9 | ths | daddr.sin_addr = spt->client_ip; |
159 | 1f697db9 | ths | daddr.sin_port = spt->client_port; |
160 | 1f697db9 | ths | |
161 | 5fafdf24 | ths | m->m_len = sizeof(struct tftp_t) - 514 + n - |
162 | 1f697db9 | ths | sizeof(struct ip) - sizeof(struct udphdr); |
163 | 1f697db9 | ths | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
164 | 1f697db9 | ths | |
165 | 1f697db9 | ths | return 0; |
166 | 1f697db9 | ths | } |
167 | 1f697db9 | ths | |
168 | 1f697db9 | ths | |
169 | 1f697db9 | ths | |
170 | 5fafdf24 | ths | static int tftp_send_error(struct tftp_session *spt, |
171 | c7f74643 | bellard | u_int16_t errorcode, const char *msg, |
172 | c7f74643 | bellard | struct tftp_t *recv_tp)
|
173 | c7f74643 | bellard | { |
174 | c7f74643 | bellard | struct sockaddr_in saddr, daddr;
|
175 | c7f74643 | bellard | struct mbuf *m;
|
176 | c7f74643 | bellard | struct tftp_t *tp;
|
177 | c7f74643 | bellard | int nobytes;
|
178 | c7f74643 | bellard | |
179 | c7f74643 | bellard | m = m_get(); |
180 | c7f74643 | bellard | |
181 | c7f74643 | bellard | if (!m) {
|
182 | c7f74643 | bellard | return -1; |
183 | c7f74643 | bellard | } |
184 | c7f74643 | bellard | |
185 | c7f74643 | bellard | memset(m->m_data, 0, m->m_size);
|
186 | c7f74643 | bellard | |
187 | 9634d903 | blueswir1 | m->m_data += IF_MAXLINKHDR; |
188 | c7f74643 | bellard | tp = (void *)m->m_data;
|
189 | c7f74643 | bellard | m->m_data += sizeof(struct udpiphdr); |
190 | 3b46e624 | ths | |
191 | c7f74643 | bellard | tp->tp_op = htons(TFTP_ERROR); |
192 | c7f74643 | bellard | tp->x.tp_error.tp_error_code = htons(errorcode); |
193 | 363a37d5 | blueswir1 | pstrcpy(tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
|
194 | c7f74643 | bellard | |
195 | c7f74643 | bellard | saddr.sin_addr = recv_tp->ip.ip_dst; |
196 | c7f74643 | bellard | saddr.sin_port = recv_tp->udp.uh_dport; |
197 | c7f74643 | bellard | |
198 | c7f74643 | bellard | daddr.sin_addr = spt->client_ip; |
199 | c7f74643 | bellard | daddr.sin_port = spt->client_port; |
200 | c7f74643 | bellard | |
201 | c7f74643 | bellard | nobytes = 2;
|
202 | c7f74643 | bellard | |
203 | 5fafdf24 | ths | m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) - |
204 | c7f74643 | bellard | sizeof(struct ip) - sizeof(struct udphdr); |
205 | c7f74643 | bellard | |
206 | c7f74643 | bellard | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
207 | c7f74643 | bellard | |
208 | c7f74643 | bellard | tftp_session_terminate(spt); |
209 | c7f74643 | bellard | |
210 | c7f74643 | bellard | return 0; |
211 | c7f74643 | bellard | } |
212 | c7f74643 | bellard | |
213 | 5fafdf24 | ths | static int tftp_send_data(struct tftp_session *spt, |
214 | c7f74643 | bellard | u_int16_t block_nr, |
215 | c7f74643 | bellard | struct tftp_t *recv_tp)
|
216 | c7f74643 | bellard | { |
217 | c7f74643 | bellard | struct sockaddr_in saddr, daddr;
|
218 | c7f74643 | bellard | struct mbuf *m;
|
219 | c7f74643 | bellard | struct tftp_t *tp;
|
220 | c7f74643 | bellard | int nobytes;
|
221 | c7f74643 | bellard | |
222 | c7f74643 | bellard | if (block_nr < 1) { |
223 | c7f74643 | bellard | return -1; |
224 | c7f74643 | bellard | } |
225 | c7f74643 | bellard | |
226 | c7f74643 | bellard | m = m_get(); |
227 | c7f74643 | bellard | |
228 | c7f74643 | bellard | if (!m) {
|
229 | c7f74643 | bellard | return -1; |
230 | c7f74643 | bellard | } |
231 | c7f74643 | bellard | |
232 | c7f74643 | bellard | memset(m->m_data, 0, m->m_size);
|
233 | c7f74643 | bellard | |
234 | 9634d903 | blueswir1 | m->m_data += IF_MAXLINKHDR; |
235 | c7f74643 | bellard | tp = (void *)m->m_data;
|
236 | c7f74643 | bellard | m->m_data += sizeof(struct udpiphdr); |
237 | 3b46e624 | ths | |
238 | c7f74643 | bellard | tp->tp_op = htons(TFTP_DATA); |
239 | c7f74643 | bellard | tp->x.tp_data.tp_block_nr = htons(block_nr); |
240 | c7f74643 | bellard | |
241 | c7f74643 | bellard | saddr.sin_addr = recv_tp->ip.ip_dst; |
242 | c7f74643 | bellard | saddr.sin_port = recv_tp->udp.uh_dport; |
243 | c7f74643 | bellard | |
244 | c7f74643 | bellard | daddr.sin_addr = spt->client_ip; |
245 | c7f74643 | bellard | daddr.sin_port = spt->client_port; |
246 | c7f74643 | bellard | |
247 | c7f74643 | bellard | nobytes = tftp_read_data(spt, block_nr - 1, tp->x.tp_data.tp_buf, 512); |
248 | c7f74643 | bellard | |
249 | c7f74643 | bellard | if (nobytes < 0) { |
250 | c7f74643 | bellard | m_free(m); |
251 | c7f74643 | bellard | |
252 | c7f74643 | bellard | /* send "file not found" error back */
|
253 | c7f74643 | bellard | |
254 | c7f74643 | bellard | tftp_send_error(spt, 1, "File not found", tp); |
255 | c7f74643 | bellard | |
256 | c7f74643 | bellard | return -1; |
257 | c7f74643 | bellard | } |
258 | c7f74643 | bellard | |
259 | 5fafdf24 | ths | m->m_len = sizeof(struct tftp_t) - (512 - nobytes) - |
260 | c7f74643 | bellard | sizeof(struct ip) - sizeof(struct udphdr); |
261 | c7f74643 | bellard | |
262 | c7f74643 | bellard | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
263 | c7f74643 | bellard | |
264 | c7f74643 | bellard | if (nobytes == 512) { |
265 | c7f74643 | bellard | tftp_session_update(spt); |
266 | c7f74643 | bellard | } |
267 | c7f74643 | bellard | else {
|
268 | c7f74643 | bellard | tftp_session_terminate(spt); |
269 | c7f74643 | bellard | } |
270 | c7f74643 | bellard | |
271 | c7f74643 | bellard | return 0; |
272 | c7f74643 | bellard | } |
273 | c7f74643 | bellard | |
274 | c7f74643 | bellard | static void tftp_handle_rrq(struct tftp_t *tp, int pktlen) |
275 | c7f74643 | bellard | { |
276 | c7f74643 | bellard | struct tftp_session *spt;
|
277 | c7f74643 | bellard | int s, k, n;
|
278 | c7f74643 | bellard | u_int8_t *src, *dst; |
279 | c7f74643 | bellard | |
280 | c7f74643 | bellard | s = tftp_session_allocate(tp); |
281 | c7f74643 | bellard | |
282 | c7f74643 | bellard | if (s < 0) { |
283 | c7f74643 | bellard | return;
|
284 | c7f74643 | bellard | } |
285 | c7f74643 | bellard | |
286 | c7f74643 | bellard | spt = &tftp_sessions[s]; |
287 | c7f74643 | bellard | |
288 | c7f74643 | bellard | src = tp->x.tp_buf; |
289 | c7f74643 | bellard | dst = spt->filename; |
290 | c7f74643 | bellard | n = pktlen - ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
|
291 | c7f74643 | bellard | |
292 | c7f74643 | bellard | /* get name */
|
293 | c7f74643 | bellard | |
294 | c7f74643 | bellard | for (k = 0; k < n; k++) { |
295 | c7f74643 | bellard | if (k < TFTP_FILENAME_MAX) {
|
296 | c7f74643 | bellard | dst[k] = src[k]; |
297 | c7f74643 | bellard | } |
298 | c7f74643 | bellard | else {
|
299 | c7f74643 | bellard | return;
|
300 | c7f74643 | bellard | } |
301 | 3b46e624 | ths | |
302 | c7f74643 | bellard | if (src[k] == '\0') { |
303 | c7f74643 | bellard | break;
|
304 | c7f74643 | bellard | } |
305 | c7f74643 | bellard | } |
306 | 3b46e624 | ths | |
307 | c7f74643 | bellard | if (k >= n) {
|
308 | c7f74643 | bellard | return;
|
309 | c7f74643 | bellard | } |
310 | 3b46e624 | ths | |
311 | c7f74643 | bellard | k++; |
312 | 3b46e624 | ths | |
313 | c7f74643 | bellard | /* check mode */
|
314 | c7f74643 | bellard | if ((n - k) < 6) { |
315 | c7f74643 | bellard | return;
|
316 | c7f74643 | bellard | } |
317 | 3b46e624 | ths | |
318 | c7f74643 | bellard | if (memcmp(&src[k], "octet\0", 6) != 0) { |
319 | c7f74643 | bellard | tftp_send_error(spt, 4, "Unsupported transfer mode", tp); |
320 | c7f74643 | bellard | return;
|
321 | c7f74643 | bellard | } |
322 | c7f74643 | bellard | |
323 | 1f697db9 | ths | k += 6; /* skipping octet */ |
324 | 1f697db9 | ths | |
325 | c7f74643 | bellard | /* do sanity checks on the filename */
|
326 | c7f74643 | bellard | |
327 | c7f74643 | bellard | if ((spt->filename[0] != '/') |
328 | c7f74643 | bellard | || (spt->filename[strlen(spt->filename) - 1] == '/') |
329 | c7f74643 | bellard | || strstr(spt->filename, "/../")) {
|
330 | c7f74643 | bellard | tftp_send_error(spt, 2, "Access violation", tp); |
331 | c7f74643 | bellard | return;
|
332 | c7f74643 | bellard | } |
333 | c7f74643 | bellard | |
334 | c7f74643 | bellard | /* only allow exported prefixes */
|
335 | c7f74643 | bellard | |
336 | 0db1137d | ths | if (!tftp_prefix) {
|
337 | c7f74643 | bellard | tftp_send_error(spt, 2, "Access violation", tp); |
338 | c7f74643 | bellard | return;
|
339 | c7f74643 | bellard | } |
340 | c7f74643 | bellard | |
341 | c7f74643 | bellard | /* check if the file exists */
|
342 | 3b46e624 | ths | |
343 | c7f74643 | bellard | if (tftp_read_data(spt, 0, spt->filename, 0) < 0) { |
344 | c7f74643 | bellard | tftp_send_error(spt, 1, "File not found", tp); |
345 | c7f74643 | bellard | return;
|
346 | c7f74643 | bellard | } |
347 | c7f74643 | bellard | |
348 | 1f697db9 | ths | if (src[n - 1] != 0) { |
349 | 1f697db9 | ths | tftp_send_error(spt, 2, "Access violation", tp); |
350 | 1f697db9 | ths | return;
|
351 | 1f697db9 | ths | } |
352 | 1f697db9 | ths | |
353 | 1f697db9 | ths | while (k < n) {
|
354 | 1f697db9 | ths | const char *key, *value; |
355 | 1f697db9 | ths | |
356 | 1f697db9 | ths | key = src + k; |
357 | 1f697db9 | ths | k += strlen(key) + 1;
|
358 | 1f697db9 | ths | |
359 | 1f697db9 | ths | if (k >= n) {
|
360 | 1f697db9 | ths | tftp_send_error(spt, 2, "Access violation", tp); |
361 | 1f697db9 | ths | return;
|
362 | 1f697db9 | ths | } |
363 | 1f697db9 | ths | |
364 | 1f697db9 | ths | value = src + k; |
365 | 1f697db9 | ths | k += strlen(value) + 1;
|
366 | 1f697db9 | ths | |
367 | 1f697db9 | ths | if (strcmp(key, "tsize") == 0) { |
368 | 1f697db9 | ths | int tsize = atoi(value);
|
369 | 1f697db9 | ths | struct stat stat_p;
|
370 | 1f697db9 | ths | |
371 | 1f697db9 | ths | if (tsize == 0 && tftp_prefix) { |
372 | 1f697db9 | ths | char buffer[1024]; |
373 | 1f697db9 | ths | int len;
|
374 | 1f697db9 | ths | |
375 | 1f697db9 | ths | len = snprintf(buffer, sizeof(buffer), "%s/%s", |
376 | 1f697db9 | ths | tftp_prefix, spt->filename); |
377 | 1f697db9 | ths | |
378 | 1f697db9 | ths | if (stat(buffer, &stat_p) == 0) |
379 | 1f697db9 | ths | tsize = stat_p.st_size; |
380 | 1f697db9 | ths | else {
|
381 | 1f697db9 | ths | tftp_send_error(spt, 1, "File not found", tp); |
382 | 1f697db9 | ths | return;
|
383 | 1f697db9 | ths | } |
384 | 1f697db9 | ths | } |
385 | 1f697db9 | ths | |
386 | 1f697db9 | ths | tftp_send_oack(spt, "tsize", tsize, tp);
|
387 | 1f697db9 | ths | } |
388 | 1f697db9 | ths | } |
389 | 1f697db9 | ths | |
390 | c7f74643 | bellard | tftp_send_data(spt, 1, tp);
|
391 | c7f74643 | bellard | } |
392 | c7f74643 | bellard | |
393 | c7f74643 | bellard | static void tftp_handle_ack(struct tftp_t *tp, int pktlen) |
394 | c7f74643 | bellard | { |
395 | c7f74643 | bellard | int s;
|
396 | c7f74643 | bellard | |
397 | c7f74643 | bellard | s = tftp_session_find(tp); |
398 | c7f74643 | bellard | |
399 | c7f74643 | bellard | if (s < 0) { |
400 | c7f74643 | bellard | return;
|
401 | c7f74643 | bellard | } |
402 | c7f74643 | bellard | |
403 | 5fafdf24 | ths | if (tftp_send_data(&tftp_sessions[s],
|
404 | 5fafdf24 | ths | ntohs(tp->x.tp_data.tp_block_nr) + 1,
|
405 | c7f74643 | bellard | tp) < 0) {
|
406 | c7f74643 | bellard | return;
|
407 | c7f74643 | bellard | } |
408 | c7f74643 | bellard | } |
409 | c7f74643 | bellard | |
410 | c7f74643 | bellard | void tftp_input(struct mbuf *m) |
411 | c7f74643 | bellard | { |
412 | c7f74643 | bellard | struct tftp_t *tp = (struct tftp_t *)m->m_data; |
413 | c7f74643 | bellard | |
414 | c7f74643 | bellard | switch(ntohs(tp->tp_op)) {
|
415 | c7f74643 | bellard | case TFTP_RRQ:
|
416 | c7f74643 | bellard | tftp_handle_rrq(tp, m->m_len); |
417 | c7f74643 | bellard | break;
|
418 | c7f74643 | bellard | |
419 | c7f74643 | bellard | case TFTP_ACK:
|
420 | c7f74643 | bellard | tftp_handle_ack(tp, m->m_len); |
421 | c7f74643 | bellard | break;
|
422 | c7f74643 | bellard | } |
423 | c7f74643 | bellard | } |