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