Revision 5fafdf24 slirp/tftp.c
b/slirp/tftp.c | ||
---|---|---|
1 | 1 |
/* |
2 | 2 |
* tftp.c - a simple, read-only tftp server for qemu |
3 |
*
|
|
3 |
* |
|
4 | 4 |
* Copyright (c) 2004 Magnus Damm <damm@opensource.se> |
5 |
*
|
|
5 |
* |
|
6 | 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | 7 |
* of this software and associated documentation files (the "Software"), to deal |
8 | 8 |
* in the Software without restriction, including without limitation the rights |
... | ... | |
27 | 27 |
struct tftp_session { |
28 | 28 |
int in_use; |
29 | 29 |
unsigned char filename[TFTP_FILENAME_MAX]; |
30 |
|
|
30 |
|
|
31 | 31 |
struct in_addr client_ip; |
32 | 32 |
u_int16_t client_port; |
33 |
|
|
33 |
|
|
34 | 34 |
int timestamp; |
35 | 35 |
}; |
36 | 36 |
|
... | ... | |
127 | 127 |
return bytes_read; |
128 | 128 |
} |
129 | 129 |
|
130 |
static int tftp_send_oack(struct tftp_session *spt,
|
|
130 |
static int tftp_send_oack(struct tftp_session *spt, |
|
131 | 131 |
const char *key, uint32_t value, |
132 | 132 |
struct tftp_t *recv_tp) |
133 | 133 |
{ |
... | ... | |
146 | 146 |
m->m_data += if_maxlinkhdr; |
147 | 147 |
tp = (void *)m->m_data; |
148 | 148 |
m->m_data += sizeof(struct udpiphdr); |
149 |
|
|
149 |
|
|
150 | 150 |
tp->tp_op = htons(TFTP_OACK); |
151 | 151 |
n += sprintf(tp->x.tp_buf + n, "%s", key) + 1; |
152 | 152 |
n += sprintf(tp->x.tp_buf + n, "%u", value) + 1; |
153 | 153 |
|
154 | 154 |
saddr.sin_addr = recv_tp->ip.ip_dst; |
155 | 155 |
saddr.sin_port = recv_tp->udp.uh_dport; |
156 |
|
|
156 |
|
|
157 | 157 |
daddr.sin_addr = spt->client_ip; |
158 | 158 |
daddr.sin_port = spt->client_port; |
159 | 159 |
|
160 |
m->m_len = sizeof(struct tftp_t) - 514 + n -
|
|
160 |
m->m_len = sizeof(struct tftp_t) - 514 + n - |
|
161 | 161 |
sizeof(struct ip) - sizeof(struct udphdr); |
162 | 162 |
udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY); |
163 | 163 |
|
... | ... | |
166 | 166 |
|
167 | 167 |
|
168 | 168 |
|
169 |
static int tftp_send_error(struct tftp_session *spt,
|
|
169 |
static int tftp_send_error(struct tftp_session *spt, |
|
170 | 170 |
u_int16_t errorcode, const char *msg, |
171 | 171 |
struct tftp_t *recv_tp) |
172 | 172 |
{ |
... | ... | |
186 | 186 |
m->m_data += if_maxlinkhdr; |
187 | 187 |
tp = (void *)m->m_data; |
188 | 188 |
m->m_data += sizeof(struct udpiphdr); |
189 |
|
|
189 |
|
|
190 | 190 |
tp->tp_op = htons(TFTP_ERROR); |
191 | 191 |
tp->x.tp_error.tp_error_code = htons(errorcode); |
192 | 192 |
strcpy(tp->x.tp_error.tp_msg, msg); |
... | ... | |
199 | 199 |
|
200 | 200 |
nobytes = 2; |
201 | 201 |
|
202 |
m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
|
|
202 |
m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) - |
|
203 | 203 |
sizeof(struct ip) - sizeof(struct udphdr); |
204 | 204 |
|
205 | 205 |
udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY); |
... | ... | |
209 | 209 |
return 0; |
210 | 210 |
} |
211 | 211 |
|
212 |
static int tftp_send_data(struct tftp_session *spt,
|
|
212 |
static int tftp_send_data(struct tftp_session *spt, |
|
213 | 213 |
u_int16_t block_nr, |
214 | 214 |
struct tftp_t *recv_tp) |
215 | 215 |
{ |
... | ... | |
233 | 233 |
m->m_data += if_maxlinkhdr; |
234 | 234 |
tp = (void *)m->m_data; |
235 | 235 |
m->m_data += sizeof(struct udpiphdr); |
236 |
|
|
236 |
|
|
237 | 237 |
tp->tp_op = htons(TFTP_DATA); |
238 | 238 |
tp->x.tp_data.tp_block_nr = htons(block_nr); |
239 | 239 |
|
... | ... | |
255 | 255 |
return -1; |
256 | 256 |
} |
257 | 257 |
|
258 |
m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
|
|
258 |
m->m_len = sizeof(struct tftp_t) - (512 - nobytes) - |
|
259 | 259 |
sizeof(struct ip) - sizeof(struct udphdr); |
260 | 260 |
|
261 | 261 |
udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY); |
... | ... | |
297 | 297 |
else { |
298 | 298 |
return; |
299 | 299 |
} |
300 |
|
|
300 |
|
|
301 | 301 |
if (src[k] == '\0') { |
302 | 302 |
break; |
303 | 303 |
} |
304 | 304 |
} |
305 |
|
|
305 |
|
|
306 | 306 |
if (k >= n) { |
307 | 307 |
return; |
308 | 308 |
} |
309 |
|
|
309 |
|
|
310 | 310 |
k++; |
311 |
|
|
311 |
|
|
312 | 312 |
/* check mode */ |
313 | 313 |
if ((n - k) < 6) { |
314 | 314 |
return; |
315 | 315 |
} |
316 |
|
|
316 |
|
|
317 | 317 |
if (memcmp(&src[k], "octet\0", 6) != 0) { |
318 | 318 |
tftp_send_error(spt, 4, "Unsupported transfer mode", tp); |
319 | 319 |
return; |
... | ... | |
338 | 338 |
} |
339 | 339 |
|
340 | 340 |
/* check if the file exists */ |
341 |
|
|
341 |
|
|
342 | 342 |
if (tftp_read_data(spt, 0, spt->filename, 0) < 0) { |
343 | 343 |
tftp_send_error(spt, 1, "File not found", tp); |
344 | 344 |
return; |
... | ... | |
399 | 399 |
return; |
400 | 400 |
} |
401 | 401 |
|
402 |
if (tftp_send_data(&tftp_sessions[s],
|
|
403 |
ntohs(tp->x.tp_data.tp_block_nr) + 1,
|
|
402 |
if (tftp_send_data(&tftp_sessions[s], |
|
403 |
ntohs(tp->x.tp_data.tp_block_nr) + 1, |
|
404 | 404 |
tp) < 0) { |
405 | 405 |
return; |
406 | 406 |
} |
Also available in: Unified diff