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