2004-08-25 01:57:12 +04:00
|
|
|
/*
|
|
|
|
* tftp.c - a simple, read-only tftp server for qemu
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2004-08-25 01:57:12 +04:00
|
|
|
* Copyright (c) 2004 Magnus Damm <damm@opensource.se>
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2004-08-25 01:57:12 +04:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <slirp.h>
|
2009-06-24 16:42:29 +04:00
|
|
|
#include "qemu-common.h"
|
2004-08-25 01:57:12 +04:00
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
static inline int tftp_session_in_use(struct tftp_session *spt)
|
|
|
|
{
|
|
|
|
return (spt->slirp != NULL);
|
|
|
|
}
|
2004-08-25 01:57:12 +04:00
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
static inline void tftp_session_update(struct tftp_session *spt)
|
2004-08-25 01:57:12 +04:00
|
|
|
{
|
2004-08-26 00:55:44 +04:00
|
|
|
spt->timestamp = curtime;
|
2004-08-25 01:57:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tftp_session_terminate(struct tftp_session *spt)
|
|
|
|
{
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(spt->filename);
|
2009-06-24 16:42:31 +04:00
|
|
|
spt->slirp = NULL;
|
2004-08-25 01:57:12 +04:00
|
|
|
}
|
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
|
2004-08-25 01:57:12 +04:00
|
|
|
{
|
|
|
|
struct tftp_session *spt;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
|
2009-06-24 16:42:31 +04:00
|
|
|
spt = &slirp->tftp_sessions[k];
|
2004-08-25 01:57:12 +04:00
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
if (!tftp_session_in_use(spt))
|
2004-08-26 00:55:44 +04:00
|
|
|
goto found;
|
2004-08-25 01:57:12 +04:00
|
|
|
|
|
|
|
/* sessions time out after 5 inactive seconds */
|
2009-06-24 16:42:30 +04:00
|
|
|
if ((int)(curtime - spt->timestamp) > 5000) {
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(spt->filename);
|
2004-08-26 00:55:44 +04:00
|
|
|
goto found;
|
2009-06-24 16:42:30 +04:00
|
|
|
}
|
2004-08-25 01:57:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
found:
|
|
|
|
memset(spt, 0, sizeof(*spt));
|
|
|
|
memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
|
|
|
|
spt->client_port = tp->udp.uh_sport;
|
2009-06-24 16:42:31 +04:00
|
|
|
spt->slirp = slirp;
|
2004-08-25 01:57:12 +04:00
|
|
|
|
|
|
|
tftp_session_update(spt);
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
|
2004-08-25 01:57:12 +04:00
|
|
|
{
|
|
|
|
struct tftp_session *spt;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
|
2009-06-24 16:42:31 +04:00
|
|
|
spt = &slirp->tftp_sessions[k];
|
2004-08-25 01:57:12 +04:00
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
if (tftp_session_in_use(spt)) {
|
2004-08-25 01:57:12 +04:00
|
|
|
if (!memcmp(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip))) {
|
|
|
|
if (spt->client_port == tp->udp.uh_sport) {
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-23 00:15:23 +04:00
|
|
|
static int tftp_read_data(struct tftp_session *spt, uint16_t block_nr,
|
|
|
|
uint8_t *buf, int len)
|
2004-08-25 01:57:12 +04:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int bytes_read = 0;
|
2007-02-20 03:12:07 +03:00
|
|
|
|
2009-06-24 16:42:30 +04:00
|
|
|
fd = open(spt->filename, O_RDONLY | O_BINARY);
|
2004-08-25 01:57:12 +04:00
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len) {
|
|
|
|
lseek(fd, block_nr * 512, SEEK_SET);
|
|
|
|
|
|
|
|
bytes_read = read(fd, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return bytes_read;
|
|
|
|
}
|
|
|
|
|
2007-09-17 01:08:06 +04:00
|
|
|
static int tftp_send_oack(struct tftp_session *spt,
|
2007-02-20 03:07:50 +03:00
|
|
|
const char *key, uint32_t value,
|
|
|
|
struct tftp_t *recv_tp)
|
|
|
|
{
|
|
|
|
struct sockaddr_in saddr, daddr;
|
|
|
|
struct mbuf *m;
|
|
|
|
struct tftp_t *tp;
|
|
|
|
int n = 0;
|
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
m = m_get(spt->slirp);
|
2007-02-20 03:07:50 +03:00
|
|
|
|
|
|
|
if (!m)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(m->m_data, 0, m->m_size);
|
|
|
|
|
2007-10-26 23:01:16 +04:00
|
|
|
m->m_data += IF_MAXLINKHDR;
|
2007-02-20 03:07:50 +03:00
|
|
|
tp = (void *)m->m_data;
|
|
|
|
m->m_data += sizeof(struct udpiphdr);
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2007-02-20 03:07:50 +03:00
|
|
|
tp->tp_op = htons(TFTP_OACK);
|
2011-02-23 21:40:14 +03:00
|
|
|
n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
|
2008-09-20 12:07:15 +04:00
|
|
|
key) + 1;
|
2011-02-23 21:40:14 +03:00
|
|
|
n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
|
2008-09-20 12:07:15 +04:00
|
|
|
value) + 1;
|
2007-02-20 03:07:50 +03:00
|
|
|
|
|
|
|
saddr.sin_addr = recv_tp->ip.ip_dst;
|
|
|
|
saddr.sin_port = recv_tp->udp.uh_dport;
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2007-02-20 03:07:50 +03:00
|
|
|
daddr.sin_addr = spt->client_ip;
|
|
|
|
daddr.sin_port = spt->client_port;
|
|
|
|
|
2007-09-17 01:08:06 +04:00
|
|
|
m->m_len = sizeof(struct tftp_t) - 514 + n -
|
2007-02-20 03:07:50 +03:00
|
|
|
sizeof(struct ip) - sizeof(struct udphdr);
|
|
|
|
udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-24 16:42:30 +04:00
|
|
|
static void tftp_send_error(struct tftp_session *spt,
|
2010-07-23 00:15:23 +04:00
|
|
|
uint16_t errorcode, const char *msg,
|
2009-06-24 16:42:30 +04:00
|
|
|
struct tftp_t *recv_tp)
|
2004-08-25 01:57:12 +04:00
|
|
|
{
|
|
|
|
struct sockaddr_in saddr, daddr;
|
|
|
|
struct mbuf *m;
|
|
|
|
struct tftp_t *tp;
|
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
m = m_get(spt->slirp);
|
2004-08-25 01:57:12 +04:00
|
|
|
|
|
|
|
if (!m) {
|
2009-06-24 16:42:30 +04:00
|
|
|
goto out;
|
2004-08-25 01:57:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(m->m_data, 0, m->m_size);
|
|
|
|
|
2007-10-26 23:01:16 +04:00
|
|
|
m->m_data += IF_MAXLINKHDR;
|
2004-08-25 01:57:12 +04:00
|
|
|
tp = (void *)m->m_data;
|
|
|
|
m->m_data += sizeof(struct udpiphdr);
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2004-08-25 01:57:12 +04:00
|
|
|
tp->tp_op = htons(TFTP_ERROR);
|
|
|
|
tp->x.tp_error.tp_error_code = htons(errorcode);
|
2008-09-20 12:07:15 +04:00
|
|
|
pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
|
2004-08-25 01:57:12 +04:00
|
|
|
|
|
|
|
saddr.sin_addr = recv_tp->ip.ip_dst;
|
|
|
|
saddr.sin_port = recv_tp->udp.uh_dport;
|
|
|
|
|
|
|
|
daddr.sin_addr = spt->client_ip;
|
|
|
|
daddr.sin_port = spt->client_port;
|
|
|
|
|
2007-09-17 01:08:06 +04:00
|
|
|
m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
|
2004-08-25 01:57:12 +04:00
|
|
|
sizeof(struct ip) - sizeof(struct udphdr);
|
|
|
|
|
|
|
|
udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
|
|
|
|
2009-06-24 16:42:30 +04:00
|
|
|
out:
|
2004-08-25 01:57:12 +04:00
|
|
|
tftp_session_terminate(spt);
|
|
|
|
}
|
|
|
|
|
2007-09-17 01:08:06 +04:00
|
|
|
static int tftp_send_data(struct tftp_session *spt,
|
2010-07-23 00:15:23 +04:00
|
|
|
uint16_t block_nr,
|
2004-08-25 01:57:12 +04:00
|
|
|
struct tftp_t *recv_tp)
|
|
|
|
{
|
|
|
|
struct sockaddr_in saddr, daddr;
|
|
|
|
struct mbuf *m;
|
|
|
|
struct tftp_t *tp;
|
|
|
|
int nobytes;
|
|
|
|
|
|
|
|
if (block_nr < 1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
m = m_get(spt->slirp);
|
2004-08-25 01:57:12 +04:00
|
|
|
|
|
|
|
if (!m) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(m->m_data, 0, m->m_size);
|
|
|
|
|
2007-10-26 23:01:16 +04:00
|
|
|
m->m_data += IF_MAXLINKHDR;
|
2004-08-25 01:57:12 +04:00
|
|
|
tp = (void *)m->m_data;
|
|
|
|
m->m_data += sizeof(struct udpiphdr);
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2004-08-25 01:57:12 +04:00
|
|
|
tp->tp_op = htons(TFTP_DATA);
|
|
|
|
tp->x.tp_data.tp_block_nr = htons(block_nr);
|
|
|
|
|
|
|
|
saddr.sin_addr = recv_tp->ip.ip_dst;
|
|
|
|
saddr.sin_port = recv_tp->udp.uh_dport;
|
|
|
|
|
|
|
|
daddr.sin_addr = spt->client_ip;
|
|
|
|
daddr.sin_port = spt->client_port;
|
|
|
|
|
|
|
|
nobytes = tftp_read_data(spt, block_nr - 1, tp->x.tp_data.tp_buf, 512);
|
|
|
|
|
|
|
|
if (nobytes < 0) {
|
|
|
|
m_free(m);
|
|
|
|
|
|
|
|
/* send "file not found" error back */
|
|
|
|
|
|
|
|
tftp_send_error(spt, 1, "File not found", tp);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-09-17 01:08:06 +04:00
|
|
|
m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
|
2004-08-25 01:57:12 +04:00
|
|
|
sizeof(struct ip) - sizeof(struct udphdr);
|
|
|
|
|
|
|
|
udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
|
|
|
|
|
|
|
if (nobytes == 512) {
|
|
|
|
tftp_session_update(spt);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tftp_session_terminate(spt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
|
2004-08-25 01:57:12 +04:00
|
|
|
{
|
|
|
|
struct tftp_session *spt;
|
2009-06-24 16:42:30 +04:00
|
|
|
int s, k;
|
2009-06-24 16:42:30 +04:00
|
|
|
size_t prefix_len;
|
2009-06-24 16:42:30 +04:00
|
|
|
char *req_fname;
|
2004-08-25 01:57:12 +04:00
|
|
|
|
2010-01-07 20:01:28 +03:00
|
|
|
/* check if a session already exists and if so terminate it */
|
|
|
|
s = tftp_session_find(slirp, tp);
|
|
|
|
if (s >= 0) {
|
|
|
|
tftp_session_terminate(&slirp->tftp_sessions[s]);
|
|
|
|
}
|
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
s = tftp_session_allocate(slirp, tp);
|
2004-08-25 01:57:12 +04:00
|
|
|
|
|
|
|
if (s < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
spt = &slirp->tftp_sessions[s];
|
2004-08-25 01:57:12 +04:00
|
|
|
|
2009-06-24 16:42:29 +04:00
|
|
|
/* unspecifed prefix means service disabled */
|
2009-06-24 16:42:31 +04:00
|
|
|
if (!slirp->tftp_prefix) {
|
2009-06-24 16:42:29 +04:00
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-24 16:42:30 +04:00
|
|
|
/* skip header fields */
|
|
|
|
k = 0;
|
2011-02-23 21:40:14 +03:00
|
|
|
pktlen -= offsetof(struct tftp_t, x.tp_buf);
|
2004-08-25 01:57:12 +04:00
|
|
|
|
2009-06-24 16:42:30 +04:00
|
|
|
/* prepend tftp_prefix */
|
2009-06-24 16:42:31 +04:00
|
|
|
prefix_len = strlen(slirp->tftp_prefix);
|
2011-08-21 07:09:37 +04:00
|
|
|
spt->filename = g_malloc(prefix_len + TFTP_FILENAME_MAX + 2);
|
2009-06-24 16:42:31 +04:00
|
|
|
memcpy(spt->filename, slirp->tftp_prefix, prefix_len);
|
2009-06-29 10:47:30 +04:00
|
|
|
spt->filename[prefix_len] = '/';
|
2009-06-24 16:42:30 +04:00
|
|
|
|
2004-08-25 01:57:12 +04:00
|
|
|
/* get name */
|
2009-06-29 10:47:30 +04:00
|
|
|
req_fname = spt->filename + prefix_len + 1;
|
2004-08-25 01:57:12 +04:00
|
|
|
|
2009-06-24 16:42:30 +04:00
|
|
|
while (1) {
|
|
|
|
if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
|
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
2004-08-25 01:57:12 +04:00
|
|
|
return;
|
|
|
|
}
|
2011-02-23 21:40:14 +03:00
|
|
|
req_fname[k] = tp->x.tp_buf[k];
|
2009-06-24 16:42:30 +04:00
|
|
|
if (req_fname[k++] == '\0') {
|
2004-08-25 01:57:12 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2004-08-25 01:57:12 +04:00
|
|
|
/* check mode */
|
2009-06-24 16:42:30 +04:00
|
|
|
if ((pktlen - k) < 6) {
|
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
2004-08-25 01:57:12 +04:00
|
|
|
return;
|
|
|
|
}
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2011-02-23 21:40:14 +03:00
|
|
|
if (strcasecmp(&tp->x.tp_buf[k], "octet") != 0) {
|
2004-08-25 01:57:12 +04:00
|
|
|
tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-20 03:07:50 +03:00
|
|
|
k += 6; /* skipping octet */
|
|
|
|
|
2004-08-25 01:57:12 +04:00
|
|
|
/* do sanity checks on the filename */
|
2009-06-29 10:47:30 +04:00
|
|
|
if (!strncmp(req_fname, "../", 3) ||
|
|
|
|
req_fname[strlen(req_fname) - 1] == '/' ||
|
2009-06-24 16:42:30 +04:00
|
|
|
strstr(req_fname, "/../")) {
|
2004-08-25 01:57:12 +04:00
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if the file exists */
|
2009-06-24 16:42:30 +04:00
|
|
|
if (tftp_read_data(spt, 0, NULL, 0) < 0) {
|
2004-08-25 01:57:12 +04:00
|
|
|
tftp_send_error(spt, 1, "File not found", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-24 16:42:30 +04:00
|
|
|
if (tp->x.tp_buf[pktlen - 1] != 0) {
|
2007-02-20 03:07:50 +03:00
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-24 16:42:30 +04:00
|
|
|
while (k < pktlen) {
|
2007-02-20 03:07:50 +03:00
|
|
|
const char *key, *value;
|
|
|
|
|
2011-02-23 21:40:14 +03:00
|
|
|
key = &tp->x.tp_buf[k];
|
2007-02-20 03:07:50 +03:00
|
|
|
k += strlen(key) + 1;
|
|
|
|
|
2009-06-24 16:42:30 +04:00
|
|
|
if (k >= pktlen) {
|
2007-02-20 03:07:50 +03:00
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-23 21:40:14 +03:00
|
|
|
value = &tp->x.tp_buf[k];
|
2007-02-20 03:07:50 +03:00
|
|
|
k += strlen(value) + 1;
|
|
|
|
|
2011-01-12 16:57:18 +03:00
|
|
|
if (strcasecmp(key, "tsize") == 0) {
|
2007-02-20 03:07:50 +03:00
|
|
|
int tsize = atoi(value);
|
|
|
|
struct stat stat_p;
|
|
|
|
|
2009-06-24 16:42:29 +04:00
|
|
|
if (tsize == 0) {
|
2009-06-24 16:42:30 +04:00
|
|
|
if (stat(spt->filename, &stat_p) == 0)
|
2007-02-20 03:07:50 +03:00
|
|
|
tsize = stat_p.st_size;
|
|
|
|
else {
|
|
|
|
tftp_send_error(spt, 1, "File not found", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tftp_send_oack(spt, "tsize", tsize, tp);
|
2010-01-07 15:39:43 +03:00
|
|
|
return;
|
2007-02-20 03:07:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-25 01:57:12 +04:00
|
|
|
tftp_send_data(spt, 1, tp);
|
|
|
|
}
|
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
|
2004-08-25 01:57:12 +04:00
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
s = tftp_session_find(slirp, tp);
|
2004-08-25 01:57:12 +04:00
|
|
|
|
|
|
|
if (s < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-24 16:42:31 +04:00
|
|
|
if (tftp_send_data(&slirp->tftp_sessions[s],
|
2007-09-17 01:08:06 +04:00
|
|
|
ntohs(tp->x.tp_data.tp_block_nr) + 1,
|
2004-08-25 01:57:12 +04:00
|
|
|
tp) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-07 20:01:28 +03:00
|
|
|
static void tftp_handle_error(Slirp *slirp, struct tftp_t *tp, int pktlen)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = tftp_session_find(slirp, tp);
|
|
|
|
|
|
|
|
if (s < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tftp_session_terminate(&slirp->tftp_sessions[s]);
|
|
|
|
}
|
|
|
|
|
2004-08-25 01:57:12 +04:00
|
|
|
void tftp_input(struct mbuf *m)
|
|
|
|
{
|
|
|
|
struct tftp_t *tp = (struct tftp_t *)m->m_data;
|
|
|
|
|
|
|
|
switch(ntohs(tp->tp_op)) {
|
|
|
|
case TFTP_RRQ:
|
2009-06-24 16:42:31 +04:00
|
|
|
tftp_handle_rrq(m->slirp, tp, m->m_len);
|
2004-08-25 01:57:12 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TFTP_ACK:
|
2009-06-24 16:42:31 +04:00
|
|
|
tftp_handle_ack(m->slirp, tp, m->m_len);
|
2004-08-25 01:57:12 +04:00
|
|
|
break;
|
2010-01-07 20:01:28 +03:00
|
|
|
|
|
|
|
case TFTP_ERROR:
|
|
|
|
tftp_handle_error(m->slirp, tp, m->m_len);
|
|
|
|
break;
|
2004-08-25 01:57:12 +04:00
|
|
|
}
|
|
|
|
}
|