root / slirp / tftp.c @ 5fafdf24
History | View | Annotate | Download (8.6 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 | c7f74643 | bellard | |
27 | c7f74643 | bellard | struct tftp_session {
|
28 | a3504c87 | bellard | int in_use;
|
29 | a3504c87 | bellard | unsigned char filename[TFTP_FILENAME_MAX]; |
30 | 5fafdf24 | ths | |
31 | a3504c87 | bellard | struct in_addr client_ip;
|
32 | a3504c87 | bellard | u_int16_t client_port; |
33 | 5fafdf24 | ths | |
34 | a3504c87 | bellard | int timestamp;
|
35 | c7f74643 | bellard | }; |
36 | c7f74643 | bellard | |
37 | c7f74643 | bellard | struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
|
38 | c7f74643 | bellard | |
39 | 9bf05444 | bellard | const char *tftp_prefix; |
40 | c7f74643 | bellard | |
41 | c7f74643 | bellard | static void tftp_session_update(struct tftp_session *spt) |
42 | c7f74643 | bellard | { |
43 | a3504c87 | bellard | spt->timestamp = curtime; |
44 | a3504c87 | bellard | spt->in_use = 1;
|
45 | c7f74643 | bellard | } |
46 | c7f74643 | bellard | |
47 | c7f74643 | bellard | static void tftp_session_terminate(struct tftp_session *spt) |
48 | c7f74643 | bellard | { |
49 | c7f74643 | bellard | spt->in_use = 0;
|
50 | c7f74643 | bellard | } |
51 | c7f74643 | bellard | |
52 | c7f74643 | bellard | static int tftp_session_allocate(struct tftp_t *tp) |
53 | c7f74643 | bellard | { |
54 | c7f74643 | bellard | struct tftp_session *spt;
|
55 | c7f74643 | bellard | int k;
|
56 | c7f74643 | bellard | |
57 | c7f74643 | bellard | for (k = 0; k < TFTP_SESSIONS_MAX; k++) { |
58 | c7f74643 | bellard | spt = &tftp_sessions[k]; |
59 | c7f74643 | bellard | |
60 | a3504c87 | bellard | if (!spt->in_use)
|
61 | a3504c87 | bellard | goto found;
|
62 | c7f74643 | bellard | |
63 | c7f74643 | bellard | /* sessions time out after 5 inactive seconds */
|
64 | a3504c87 | bellard | if ((int)(curtime - spt->timestamp) > 5000) |
65 | a3504c87 | bellard | goto found;
|
66 | c7f74643 | bellard | } |
67 | c7f74643 | bellard | |
68 | c7f74643 | bellard | return -1; |
69 | c7f74643 | bellard | |
70 | c7f74643 | bellard | found:
|
71 | c7f74643 | bellard | memset(spt, 0, sizeof(*spt)); |
72 | c7f74643 | bellard | memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
|
73 | c7f74643 | bellard | spt->client_port = tp->udp.uh_sport; |
74 | c7f74643 | bellard | |
75 | c7f74643 | bellard | tftp_session_update(spt); |
76 | c7f74643 | bellard | |
77 | c7f74643 | bellard | return k;
|
78 | c7f74643 | bellard | } |
79 | c7f74643 | bellard | |
80 | c7f74643 | bellard | static int tftp_session_find(struct tftp_t *tp) |
81 | c7f74643 | bellard | { |
82 | c7f74643 | bellard | struct tftp_session *spt;
|
83 | c7f74643 | bellard | int k;
|
84 | c7f74643 | bellard | |
85 | c7f74643 | bellard | for (k = 0; k < TFTP_SESSIONS_MAX; k++) { |
86 | c7f74643 | bellard | spt = &tftp_sessions[k]; |
87 | c7f74643 | bellard | |
88 | c7f74643 | bellard | if (spt->in_use) {
|
89 | c7f74643 | bellard | if (!memcmp(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip))) { |
90 | c7f74643 | bellard | if (spt->client_port == tp->udp.uh_sport) {
|
91 | c7f74643 | bellard | return k;
|
92 | c7f74643 | bellard | } |
93 | c7f74643 | bellard | } |
94 | c7f74643 | bellard | } |
95 | c7f74643 | bellard | } |
96 | c7f74643 | bellard | |
97 | c7f74643 | bellard | return -1; |
98 | c7f74643 | bellard | } |
99 | c7f74643 | bellard | |
100 | c7f74643 | bellard | static int tftp_read_data(struct tftp_session *spt, u_int16_t block_nr, |
101 | c7f74643 | bellard | u_int8_t *buf, int len)
|
102 | c7f74643 | bellard | { |
103 | c7f74643 | bellard | int fd;
|
104 | c7f74643 | bellard | int bytes_read = 0; |
105 | 0db1137d | ths | char buffer[1024]; |
106 | 0db1137d | ths | int n;
|
107 | c7f74643 | bellard | |
108 | 0db1137d | ths | n = snprintf(buffer, sizeof(buffer), "%s/%s", |
109 | 0db1137d | ths | tftp_prefix, spt->filename); |
110 | 0db1137d | ths | if (n >= sizeof(buffer)) |
111 | 0db1137d | ths | return -1; |
112 | 0db1137d | ths | |
113 | 0db1137d | ths | fd = open(buffer, O_RDONLY | O_BINARY); |
114 | c7f74643 | bellard | |
115 | c7f74643 | bellard | if (fd < 0) { |
116 | c7f74643 | bellard | return -1; |
117 | c7f74643 | bellard | } |
118 | c7f74643 | bellard | |
119 | c7f74643 | bellard | if (len) {
|
120 | c7f74643 | bellard | lseek(fd, block_nr * 512, SEEK_SET);
|
121 | c7f74643 | bellard | |
122 | c7f74643 | bellard | bytes_read = read(fd, buf, len); |
123 | c7f74643 | bellard | } |
124 | c7f74643 | bellard | |
125 | c7f74643 | bellard | close(fd); |
126 | c7f74643 | bellard | |
127 | c7f74643 | bellard | return bytes_read;
|
128 | c7f74643 | bellard | } |
129 | c7f74643 | bellard | |
130 | 5fafdf24 | ths | static int tftp_send_oack(struct tftp_session *spt, |
131 | 1f697db9 | ths | const char *key, uint32_t value, |
132 | 1f697db9 | ths | struct tftp_t *recv_tp)
|
133 | 1f697db9 | ths | { |
134 | 1f697db9 | ths | struct sockaddr_in saddr, daddr;
|
135 | 1f697db9 | ths | struct mbuf *m;
|
136 | 1f697db9 | ths | struct tftp_t *tp;
|
137 | 1f697db9 | ths | int n = 0; |
138 | 1f697db9 | ths | |
139 | 1f697db9 | ths | m = m_get(); |
140 | 1f697db9 | ths | |
141 | 1f697db9 | ths | if (!m)
|
142 | 1f697db9 | ths | return -1; |
143 | 1f697db9 | ths | |
144 | 1f697db9 | ths | memset(m->m_data, 0, m->m_size);
|
145 | 1f697db9 | ths | |
146 | 1f697db9 | ths | m->m_data += if_maxlinkhdr; |
147 | 1f697db9 | ths | tp = (void *)m->m_data;
|
148 | 1f697db9 | ths | m->m_data += sizeof(struct udpiphdr); |
149 | 5fafdf24 | ths | |
150 | 1f697db9 | ths | tp->tp_op = htons(TFTP_OACK); |
151 | 1f697db9 | ths | n += sprintf(tp->x.tp_buf + n, "%s", key) + 1; |
152 | 1f697db9 | ths | n += sprintf(tp->x.tp_buf + n, "%u", value) + 1; |
153 | 1f697db9 | ths | |
154 | 1f697db9 | ths | saddr.sin_addr = recv_tp->ip.ip_dst; |
155 | 1f697db9 | ths | saddr.sin_port = recv_tp->udp.uh_dport; |
156 | 5fafdf24 | ths | |
157 | 1f697db9 | ths | daddr.sin_addr = spt->client_ip; |
158 | 1f697db9 | ths | daddr.sin_port = spt->client_port; |
159 | 1f697db9 | ths | |
160 | 5fafdf24 | ths | m->m_len = sizeof(struct tftp_t) - 514 + n - |
161 | 1f697db9 | ths | sizeof(struct ip) - sizeof(struct udphdr); |
162 | 1f697db9 | ths | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
163 | 1f697db9 | ths | |
164 | 1f697db9 | ths | return 0; |
165 | 1f697db9 | ths | } |
166 | 1f697db9 | ths | |
167 | 1f697db9 | ths | |
168 | 1f697db9 | ths | |
169 | 5fafdf24 | ths | static int tftp_send_error(struct tftp_session *spt, |
170 | c7f74643 | bellard | u_int16_t errorcode, const char *msg, |
171 | c7f74643 | bellard | struct tftp_t *recv_tp)
|
172 | c7f74643 | bellard | { |
173 | c7f74643 | bellard | struct sockaddr_in saddr, daddr;
|
174 | c7f74643 | bellard | struct mbuf *m;
|
175 | c7f74643 | bellard | struct tftp_t *tp;
|
176 | c7f74643 | bellard | int nobytes;
|
177 | c7f74643 | bellard | |
178 | c7f74643 | bellard | m = m_get(); |
179 | c7f74643 | bellard | |
180 | c7f74643 | bellard | if (!m) {
|
181 | c7f74643 | bellard | return -1; |
182 | c7f74643 | bellard | } |
183 | c7f74643 | bellard | |
184 | c7f74643 | bellard | memset(m->m_data, 0, m->m_size);
|
185 | c7f74643 | bellard | |
186 | c7f74643 | bellard | m->m_data += if_maxlinkhdr; |
187 | c7f74643 | bellard | tp = (void *)m->m_data;
|
188 | c7f74643 | bellard | m->m_data += sizeof(struct udpiphdr); |
189 | 5fafdf24 | ths | |
190 | c7f74643 | bellard | tp->tp_op = htons(TFTP_ERROR); |
191 | c7f74643 | bellard | tp->x.tp_error.tp_error_code = htons(errorcode); |
192 | c7f74643 | bellard | strcpy(tp->x.tp_error.tp_msg, msg); |
193 | c7f74643 | bellard | |
194 | c7f74643 | bellard | saddr.sin_addr = recv_tp->ip.ip_dst; |
195 | c7f74643 | bellard | saddr.sin_port = recv_tp->udp.uh_dport; |
196 | c7f74643 | bellard | |
197 | c7f74643 | bellard | daddr.sin_addr = spt->client_ip; |
198 | c7f74643 | bellard | daddr.sin_port = spt->client_port; |
199 | c7f74643 | bellard | |
200 | c7f74643 | bellard | nobytes = 2;
|
201 | c7f74643 | bellard | |
202 | 5fafdf24 | ths | m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) - |
203 | c7f74643 | bellard | sizeof(struct ip) - sizeof(struct udphdr); |
204 | c7f74643 | bellard | |
205 | c7f74643 | bellard | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
206 | c7f74643 | bellard | |
207 | c7f74643 | bellard | tftp_session_terminate(spt); |
208 | c7f74643 | bellard | |
209 | c7f74643 | bellard | return 0; |
210 | c7f74643 | bellard | } |
211 | c7f74643 | bellard | |
212 | 5fafdf24 | ths | static int tftp_send_data(struct tftp_session *spt, |
213 | c7f74643 | bellard | u_int16_t block_nr, |
214 | c7f74643 | bellard | struct tftp_t *recv_tp)
|
215 | c7f74643 | bellard | { |
216 | c7f74643 | bellard | struct sockaddr_in saddr, daddr;
|
217 | c7f74643 | bellard | struct mbuf *m;
|
218 | c7f74643 | bellard | struct tftp_t *tp;
|
219 | c7f74643 | bellard | int nobytes;
|
220 | c7f74643 | bellard | |
221 | c7f74643 | bellard | if (block_nr < 1) { |
222 | c7f74643 | bellard | return -1; |
223 | c7f74643 | bellard | } |
224 | c7f74643 | bellard | |
225 | c7f74643 | bellard | m = m_get(); |
226 | c7f74643 | bellard | |
227 | c7f74643 | bellard | if (!m) {
|
228 | c7f74643 | bellard | return -1; |
229 | c7f74643 | bellard | } |
230 | c7f74643 | bellard | |
231 | c7f74643 | bellard | memset(m->m_data, 0, m->m_size);
|
232 | c7f74643 | bellard | |
233 | c7f74643 | bellard | m->m_data += if_maxlinkhdr; |
234 | c7f74643 | bellard | tp = (void *)m->m_data;
|
235 | c7f74643 | bellard | m->m_data += sizeof(struct udpiphdr); |
236 | 5fafdf24 | ths | |
237 | c7f74643 | bellard | tp->tp_op = htons(TFTP_DATA); |
238 | c7f74643 | bellard | tp->x.tp_data.tp_block_nr = htons(block_nr); |
239 | c7f74643 | bellard | |
240 | c7f74643 | bellard | saddr.sin_addr = recv_tp->ip.ip_dst; |
241 | c7f74643 | bellard | saddr.sin_port = recv_tp->udp.uh_dport; |
242 | c7f74643 | bellard | |
243 | c7f74643 | bellard | daddr.sin_addr = spt->client_ip; |
244 | c7f74643 | bellard | daddr.sin_port = spt->client_port; |
245 | c7f74643 | bellard | |
246 | c7f74643 | bellard | nobytes = tftp_read_data(spt, block_nr - 1, tp->x.tp_data.tp_buf, 512); |
247 | c7f74643 | bellard | |
248 | c7f74643 | bellard | if (nobytes < 0) { |
249 | c7f74643 | bellard | m_free(m); |
250 | c7f74643 | bellard | |
251 | c7f74643 | bellard | /* send "file not found" error back */
|
252 | c7f74643 | bellard | |
253 | c7f74643 | bellard | tftp_send_error(spt, 1, "File not found", tp); |
254 | c7f74643 | bellard | |
255 | c7f74643 | bellard | return -1; |
256 | c7f74643 | bellard | } |
257 | c7f74643 | bellard | |
258 | 5fafdf24 | ths | m->m_len = sizeof(struct tftp_t) - (512 - nobytes) - |
259 | c7f74643 | bellard | sizeof(struct ip) - sizeof(struct udphdr); |
260 | c7f74643 | bellard | |
261 | c7f74643 | bellard | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
262 | c7f74643 | bellard | |
263 | c7f74643 | bellard | if (nobytes == 512) { |
264 | c7f74643 | bellard | tftp_session_update(spt); |
265 | c7f74643 | bellard | } |
266 | c7f74643 | bellard | else {
|
267 | c7f74643 | bellard | tftp_session_terminate(spt); |
268 | c7f74643 | bellard | } |
269 | c7f74643 | bellard | |
270 | c7f74643 | bellard | return 0; |
271 | c7f74643 | bellard | } |
272 | c7f74643 | bellard | |
273 | c7f74643 | bellard | static void tftp_handle_rrq(struct tftp_t *tp, int pktlen) |
274 | c7f74643 | bellard | { |
275 | c7f74643 | bellard | struct tftp_session *spt;
|
276 | c7f74643 | bellard | int s, k, n;
|
277 | c7f74643 | bellard | u_int8_t *src, *dst; |
278 | c7f74643 | bellard | |
279 | c7f74643 | bellard | s = tftp_session_allocate(tp); |
280 | c7f74643 | bellard | |
281 | c7f74643 | bellard | if (s < 0) { |
282 | c7f74643 | bellard | return;
|
283 | c7f74643 | bellard | } |
284 | c7f74643 | bellard | |
285 | c7f74643 | bellard | spt = &tftp_sessions[s]; |
286 | c7f74643 | bellard | |
287 | c7f74643 | bellard | src = tp->x.tp_buf; |
288 | c7f74643 | bellard | dst = spt->filename; |
289 | c7f74643 | bellard | n = pktlen - ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
|
290 | c7f74643 | bellard | |
291 | c7f74643 | bellard | /* get name */
|
292 | c7f74643 | bellard | |
293 | c7f74643 | bellard | for (k = 0; k < n; k++) { |
294 | c7f74643 | bellard | if (k < TFTP_FILENAME_MAX) {
|
295 | c7f74643 | bellard | dst[k] = src[k]; |
296 | c7f74643 | bellard | } |
297 | c7f74643 | bellard | else {
|
298 | c7f74643 | bellard | return;
|
299 | c7f74643 | bellard | } |
300 | 5fafdf24 | ths | |
301 | c7f74643 | bellard | if (src[k] == '\0') { |
302 | c7f74643 | bellard | break;
|
303 | c7f74643 | bellard | } |
304 | c7f74643 | bellard | } |
305 | 5fafdf24 | ths | |
306 | c7f74643 | bellard | if (k >= n) {
|
307 | c7f74643 | bellard | return;
|
308 | c7f74643 | bellard | } |
309 | 5fafdf24 | ths | |
310 | c7f74643 | bellard | k++; |
311 | 5fafdf24 | ths | |
312 | c7f74643 | bellard | /* check mode */
|
313 | c7f74643 | bellard | if ((n - k) < 6) { |
314 | c7f74643 | bellard | return;
|
315 | c7f74643 | bellard | } |
316 | 5fafdf24 | ths | |
317 | c7f74643 | bellard | if (memcmp(&src[k], "octet\0", 6) != 0) { |
318 | c7f74643 | bellard | tftp_send_error(spt, 4, "Unsupported transfer mode", tp); |
319 | c7f74643 | bellard | return;
|
320 | c7f74643 | bellard | } |
321 | c7f74643 | bellard | |
322 | 1f697db9 | ths | k += 6; /* skipping octet */ |
323 | 1f697db9 | ths | |
324 | c7f74643 | bellard | /* do sanity checks on the filename */
|
325 | c7f74643 | bellard | |
326 | c7f74643 | bellard | if ((spt->filename[0] != '/') |
327 | c7f74643 | bellard | || (spt->filename[strlen(spt->filename) - 1] == '/') |
328 | c7f74643 | bellard | || strstr(spt->filename, "/../")) {
|
329 | c7f74643 | bellard | tftp_send_error(spt, 2, "Access violation", tp); |
330 | c7f74643 | bellard | return;
|
331 | c7f74643 | bellard | } |
332 | c7f74643 | bellard | |
333 | c7f74643 | bellard | /* only allow exported prefixes */
|
334 | c7f74643 | bellard | |
335 | 0db1137d | ths | if (!tftp_prefix) {
|
336 | c7f74643 | bellard | tftp_send_error(spt, 2, "Access violation", tp); |
337 | c7f74643 | bellard | return;
|
338 | c7f74643 | bellard | } |
339 | c7f74643 | bellard | |
340 | c7f74643 | bellard | /* check if the file exists */
|
341 | 5fafdf24 | ths | |
342 | c7f74643 | bellard | if (tftp_read_data(spt, 0, spt->filename, 0) < 0) { |
343 | c7f74643 | bellard | tftp_send_error(spt, 1, "File not found", tp); |
344 | c7f74643 | bellard | return;
|
345 | c7f74643 | bellard | } |
346 | c7f74643 | bellard | |
347 | 1f697db9 | ths | if (src[n - 1] != 0) { |
348 | 1f697db9 | ths | tftp_send_error(spt, 2, "Access violation", tp); |
349 | 1f697db9 | ths | return;
|
350 | 1f697db9 | ths | } |
351 | 1f697db9 | ths | |
352 | 1f697db9 | ths | while (k < n) {
|
353 | 1f697db9 | ths | const char *key, *value; |
354 | 1f697db9 | ths | |
355 | 1f697db9 | ths | key = src + k; |
356 | 1f697db9 | ths | k += strlen(key) + 1;
|
357 | 1f697db9 | ths | |
358 | 1f697db9 | ths | if (k >= n) {
|
359 | 1f697db9 | ths | tftp_send_error(spt, 2, "Access violation", tp); |
360 | 1f697db9 | ths | return;
|
361 | 1f697db9 | ths | } |
362 | 1f697db9 | ths | |
363 | 1f697db9 | ths | value = src + k; |
364 | 1f697db9 | ths | k += strlen(value) + 1;
|
365 | 1f697db9 | ths | |
366 | 1f697db9 | ths | if (strcmp(key, "tsize") == 0) { |
367 | 1f697db9 | ths | int tsize = atoi(value);
|
368 | 1f697db9 | ths | struct stat stat_p;
|
369 | 1f697db9 | ths | |
370 | 1f697db9 | ths | if (tsize == 0 && tftp_prefix) { |
371 | 1f697db9 | ths | char buffer[1024]; |
372 | 1f697db9 | ths | int len;
|
373 | 1f697db9 | ths | |
374 | 1f697db9 | ths | len = snprintf(buffer, sizeof(buffer), "%s/%s", |
375 | 1f697db9 | ths | tftp_prefix, spt->filename); |
376 | 1f697db9 | ths | |
377 | 1f697db9 | ths | if (stat(buffer, &stat_p) == 0) |
378 | 1f697db9 | ths | tsize = stat_p.st_size; |
379 | 1f697db9 | ths | else {
|
380 | 1f697db9 | ths | tftp_send_error(spt, 1, "File not found", tp); |
381 | 1f697db9 | ths | return;
|
382 | 1f697db9 | ths | } |
383 | 1f697db9 | ths | } |
384 | 1f697db9 | ths | |
385 | 1f697db9 | ths | tftp_send_oack(spt, "tsize", tsize, tp);
|
386 | 1f697db9 | ths | } |
387 | 1f697db9 | ths | } |
388 | 1f697db9 | ths | |
389 | c7f74643 | bellard | tftp_send_data(spt, 1, tp);
|
390 | c7f74643 | bellard | } |
391 | c7f74643 | bellard | |
392 | c7f74643 | bellard | static void tftp_handle_ack(struct tftp_t *tp, int pktlen) |
393 | c7f74643 | bellard | { |
394 | c7f74643 | bellard | int s;
|
395 | c7f74643 | bellard | |
396 | c7f74643 | bellard | s = tftp_session_find(tp); |
397 | c7f74643 | bellard | |
398 | c7f74643 | bellard | if (s < 0) { |
399 | c7f74643 | bellard | return;
|
400 | c7f74643 | bellard | } |
401 | c7f74643 | bellard | |
402 | 5fafdf24 | ths | if (tftp_send_data(&tftp_sessions[s],
|
403 | 5fafdf24 | ths | ntohs(tp->x.tp_data.tp_block_nr) + 1,
|
404 | c7f74643 | bellard | tp) < 0) {
|
405 | c7f74643 | bellard | return;
|
406 | c7f74643 | bellard | } |
407 | c7f74643 | bellard | } |
408 | c7f74643 | bellard | |
409 | c7f74643 | bellard | void tftp_input(struct mbuf *m) |
410 | c7f74643 | bellard | { |
411 | c7f74643 | bellard | struct tftp_t *tp = (struct tftp_t *)m->m_data; |
412 | c7f74643 | bellard | |
413 | c7f74643 | bellard | switch(ntohs(tp->tp_op)) {
|
414 | c7f74643 | bellard | case TFTP_RRQ:
|
415 | c7f74643 | bellard | tftp_handle_rrq(tp, m->m_len); |
416 | c7f74643 | bellard | break;
|
417 | c7f74643 | bellard | |
418 | c7f74643 | bellard | case TFTP_ACK:
|
419 | c7f74643 | bellard | tftp_handle_ack(tp, m->m_len); |
420 | c7f74643 | bellard | break;
|
421 | c7f74643 | bellard | } |
422 | c7f74643 | bellard | } |