slirp: Remove unused return value of tftp_send_next_block
[qemu.git] / slirp / tftp.c
blobcf7e3b8238f197bb8f506c7b11690064580c8d77
1 /*
2 * tftp.c - a simple, read-only tftp server for qemu
4 * Copyright (c) 2004 Magnus Damm <damm@opensource.se>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include <slirp.h>
26 #include "qemu-common.h"
28 static inline int tftp_session_in_use(struct tftp_session *spt)
30 return (spt->slirp != NULL);
33 static inline void tftp_session_update(struct tftp_session *spt)
35 spt->timestamp = curtime;
38 static void tftp_session_terminate(struct tftp_session *spt)
40 if (spt->fd >= 0) {
41 close(spt->fd);
42 spt->fd = -1;
44 g_free(spt->filename);
45 spt->slirp = NULL;
48 static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
50 struct tftp_session *spt;
51 int k;
53 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
54 spt = &slirp->tftp_sessions[k];
56 if (!tftp_session_in_use(spt))
57 goto found;
59 /* sessions time out after 5 inactive seconds */
60 if ((int)(curtime - spt->timestamp) > 5000) {
61 tftp_session_terminate(spt);
62 goto found;
66 return -1;
68 found:
69 memset(spt, 0, sizeof(*spt));
70 memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
71 spt->fd = -1;
72 spt->client_port = tp->udp.uh_sport;
73 spt->slirp = slirp;
75 tftp_session_update(spt);
77 return k;
80 static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
82 struct tftp_session *spt;
83 int k;
85 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
86 spt = &slirp->tftp_sessions[k];
88 if (tftp_session_in_use(spt)) {
89 if (!memcmp(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip))) {
90 if (spt->client_port == tp->udp.uh_sport) {
91 return k;
97 return -1;
100 static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
101 uint8_t *buf, int len)
103 int bytes_read = 0;
105 if (spt->fd < 0) {
106 spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
109 if (spt->fd < 0) {
110 return -1;
113 if (len) {
114 lseek(spt->fd, block_nr * 512, SEEK_SET);
116 bytes_read = read(spt->fd, buf, len);
119 return bytes_read;
122 static int tftp_send_oack(struct tftp_session *spt,
123 const char *key, uint32_t value,
124 struct tftp_t *recv_tp)
126 struct sockaddr_in saddr, daddr;
127 struct mbuf *m;
128 struct tftp_t *tp;
129 int n = 0;
131 m = m_get(spt->slirp);
133 if (!m)
134 return -1;
136 memset(m->m_data, 0, m->m_size);
138 m->m_data += IF_MAXLINKHDR;
139 tp = (void *)m->m_data;
140 m->m_data += sizeof(struct udpiphdr);
142 tp->tp_op = htons(TFTP_OACK);
143 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
144 key) + 1;
145 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
146 value) + 1;
148 saddr.sin_addr = recv_tp->ip.ip_dst;
149 saddr.sin_port = recv_tp->udp.uh_dport;
151 daddr.sin_addr = spt->client_ip;
152 daddr.sin_port = spt->client_port;
154 m->m_len = sizeof(struct tftp_t) - 514 + n -
155 sizeof(struct ip) - sizeof(struct udphdr);
156 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
158 return 0;
161 static void tftp_send_error(struct tftp_session *spt,
162 uint16_t errorcode, const char *msg,
163 struct tftp_t *recv_tp)
165 struct sockaddr_in saddr, daddr;
166 struct mbuf *m;
167 struct tftp_t *tp;
169 m = m_get(spt->slirp);
171 if (!m) {
172 goto out;
175 memset(m->m_data, 0, m->m_size);
177 m->m_data += IF_MAXLINKHDR;
178 tp = (void *)m->m_data;
179 m->m_data += sizeof(struct udpiphdr);
181 tp->tp_op = htons(TFTP_ERROR);
182 tp->x.tp_error.tp_error_code = htons(errorcode);
183 pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
185 saddr.sin_addr = recv_tp->ip.ip_dst;
186 saddr.sin_port = recv_tp->udp.uh_dport;
188 daddr.sin_addr = spt->client_ip;
189 daddr.sin_port = spt->client_port;
191 m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
192 sizeof(struct ip) - sizeof(struct udphdr);
194 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
196 out:
197 tftp_session_terminate(spt);
200 static void tftp_send_next_block(struct tftp_session *spt,
201 struct tftp_t *recv_tp)
203 struct sockaddr_in saddr, daddr;
204 struct mbuf *m;
205 struct tftp_t *tp;
206 int nobytes;
208 m = m_get(spt->slirp);
210 if (!m) {
211 return;
214 memset(m->m_data, 0, m->m_size);
216 m->m_data += IF_MAXLINKHDR;
217 tp = (void *)m->m_data;
218 m->m_data += sizeof(struct udpiphdr);
220 tp->tp_op = htons(TFTP_DATA);
221 tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
223 saddr.sin_addr = recv_tp->ip.ip_dst;
224 saddr.sin_port = recv_tp->udp.uh_dport;
226 daddr.sin_addr = spt->client_ip;
227 daddr.sin_port = spt->client_port;
229 nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf, 512);
231 if (nobytes < 0) {
232 m_free(m);
234 /* send "file not found" error back */
236 tftp_send_error(spt, 1, "File not found", tp);
238 return;
241 m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
242 sizeof(struct ip) - sizeof(struct udphdr);
244 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
246 if (nobytes == 512) {
247 tftp_session_update(spt);
249 else {
250 tftp_session_terminate(spt);
253 spt->block_nr++;
256 static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
258 struct tftp_session *spt;
259 int s, k;
260 size_t prefix_len;
261 char *req_fname;
263 /* check if a session already exists and if so terminate it */
264 s = tftp_session_find(slirp, tp);
265 if (s >= 0) {
266 tftp_session_terminate(&slirp->tftp_sessions[s]);
269 s = tftp_session_allocate(slirp, tp);
271 if (s < 0) {
272 return;
275 spt = &slirp->tftp_sessions[s];
277 /* unspecifed prefix means service disabled */
278 if (!slirp->tftp_prefix) {
279 tftp_send_error(spt, 2, "Access violation", tp);
280 return;
283 /* skip header fields */
284 k = 0;
285 pktlen -= offsetof(struct tftp_t, x.tp_buf);
287 /* prepend tftp_prefix */
288 prefix_len = strlen(slirp->tftp_prefix);
289 spt->filename = g_malloc(prefix_len + TFTP_FILENAME_MAX + 2);
290 memcpy(spt->filename, slirp->tftp_prefix, prefix_len);
291 spt->filename[prefix_len] = '/';
293 /* get name */
294 req_fname = spt->filename + prefix_len + 1;
296 while (1) {
297 if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
298 tftp_send_error(spt, 2, "Access violation", tp);
299 return;
301 req_fname[k] = tp->x.tp_buf[k];
302 if (req_fname[k++] == '\0') {
303 break;
307 /* check mode */
308 if ((pktlen - k) < 6) {
309 tftp_send_error(spt, 2, "Access violation", tp);
310 return;
313 if (strcasecmp(&tp->x.tp_buf[k], "octet") != 0) {
314 tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
315 return;
318 k += 6; /* skipping octet */
320 /* do sanity checks on the filename */
321 if (!strncmp(req_fname, "../", 3) ||
322 req_fname[strlen(req_fname) - 1] == '/' ||
323 strstr(req_fname, "/../")) {
324 tftp_send_error(spt, 2, "Access violation", tp);
325 return;
328 /* check if the file exists */
329 if (tftp_read_data(spt, 0, NULL, 0) < 0) {
330 tftp_send_error(spt, 1, "File not found", tp);
331 return;
334 if (tp->x.tp_buf[pktlen - 1] != 0) {
335 tftp_send_error(spt, 2, "Access violation", tp);
336 return;
339 while (k < pktlen) {
340 const char *key, *value;
342 key = &tp->x.tp_buf[k];
343 k += strlen(key) + 1;
345 if (k >= pktlen) {
346 tftp_send_error(spt, 2, "Access violation", tp);
347 return;
350 value = &tp->x.tp_buf[k];
351 k += strlen(value) + 1;
353 if (strcasecmp(key, "tsize") == 0) {
354 int tsize = atoi(value);
355 struct stat stat_p;
357 if (tsize == 0) {
358 if (stat(spt->filename, &stat_p) == 0)
359 tsize = stat_p.st_size;
360 else {
361 tftp_send_error(spt, 1, "File not found", tp);
362 return;
366 tftp_send_oack(spt, "tsize", tsize, tp);
367 return;
371 spt->block_nr = 0;
372 tftp_send_next_block(spt, tp);
375 static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
377 int s;
379 s = tftp_session_find(slirp, tp);
381 if (s < 0) {
382 return;
385 tftp_send_next_block(&slirp->tftp_sessions[s], tp);
388 static void tftp_handle_error(Slirp *slirp, struct tftp_t *tp, int pktlen)
390 int s;
392 s = tftp_session_find(slirp, tp);
394 if (s < 0) {
395 return;
398 tftp_session_terminate(&slirp->tftp_sessions[s]);
401 void tftp_input(struct mbuf *m)
403 struct tftp_t *tp = (struct tftp_t *)m->m_data;
405 switch(ntohs(tp->tp_op)) {
406 case TFTP_RRQ:
407 tftp_handle_rrq(m->slirp, tp, m->m_len);
408 break;
410 case TFTP_ACK:
411 tftp_handle_ack(m->slirp, tp, m->m_len);
412 break;
414 case TFTP_ERROR:
415 tftp_handle_error(m->slirp, tp, m->m_len);
416 break;