提交 6b80f7db 编写于 作者: A Anthony Liguori

Merge remote-tracking branch 'kiszka/queues/slirp' into staging

* kiszka/queues/slirp:
  slirp: Implement TFTP Blocksize option
  slirp: Remove unused return value of tftp_send_next_block
  slirp: Handle more than 65535 blocks in TFTP transfers
  slirp: improve TFTP performance
  slirp: Fix error reported by static code analysis
  slirp: Remove wrong type casts ins debug statements
...@@ -114,9 +114,9 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m, ...@@ -114,9 +114,9 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
int win = 0; int win = 0;
DEBUG_CALL("tcp_respond"); DEBUG_CALL("tcp_respond");
DEBUG_ARG("tp = %lx", (long)tp); DEBUG_ARG("tp = %p", tp);
DEBUG_ARG("ti = %lx", (long)ti); DEBUG_ARG("ti = %p", ti);
DEBUG_ARG("m = %lx", (long)m); DEBUG_ARG("m = %p", m);
DEBUG_ARG("ack = %u", ack); DEBUG_ARG("ack = %u", ack);
DEBUG_ARG("seq = %u", seq); DEBUG_ARG("seq = %u", seq);
DEBUG_ARG("flags = %x", flags); DEBUG_ARG("flags = %x", flags);
...@@ -124,7 +124,7 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m, ...@@ -124,7 +124,7 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
if (tp) if (tp)
win = sbspace(&tp->t_socket->so_rcv); win = sbspace(&tp->t_socket->so_rcv);
if (m == NULL) { if (m == NULL) {
if ((m = m_get(tp->t_socket->slirp)) == NULL) if (!tp || (m = m_get(tp->t_socket->slirp)) == NULL)
return; return;
tlen = 0; tlen = 0;
m->m_data += IF_MAXLINKHDR; m->m_data += IF_MAXLINKHDR;
......
...@@ -37,6 +37,10 @@ static inline void tftp_session_update(struct tftp_session *spt) ...@@ -37,6 +37,10 @@ static inline void tftp_session_update(struct tftp_session *spt)
static void tftp_session_terminate(struct tftp_session *spt) static void tftp_session_terminate(struct tftp_session *spt)
{ {
if (spt->fd >= 0) {
close(spt->fd);
spt->fd = -1;
}
g_free(spt->filename); g_free(spt->filename);
spt->slirp = NULL; spt->slirp = NULL;
} }
...@@ -54,7 +58,7 @@ static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp) ...@@ -54,7 +58,7 @@ static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
/* sessions time out after 5 inactive seconds */ /* sessions time out after 5 inactive seconds */
if ((int)(curtime - spt->timestamp) > 5000) { if ((int)(curtime - spt->timestamp) > 5000) {
g_free(spt->filename); tftp_session_terminate(spt);
goto found; goto found;
} }
} }
...@@ -64,6 +68,7 @@ static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp) ...@@ -64,6 +68,7 @@ static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
found: found:
memset(spt, 0, sizeof(*spt)); memset(spt, 0, sizeof(*spt));
memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip)); memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
spt->fd = -1;
spt->client_port = tp->udp.uh_sport; spt->client_port = tp->udp.uh_sport;
spt->slirp = slirp; spt->slirp = slirp;
...@@ -92,37 +97,36 @@ static int tftp_session_find(Slirp *slirp, struct tftp_t *tp) ...@@ -92,37 +97,36 @@ static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
return -1; return -1;
} }
static int tftp_read_data(struct tftp_session *spt, uint16_t block_nr, static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
uint8_t *buf, int len) uint8_t *buf, int len)
{ {
int fd; int bytes_read = 0;
int bytes_read = 0;
fd = open(spt->filename, O_RDONLY | O_BINARY); if (spt->fd < 0) {
spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
}
if (fd < 0) { if (spt->fd < 0) {
return -1; return -1;
} }
if (len) { if (len) {
lseek(fd, block_nr * 512, SEEK_SET); lseek(spt->fd, block_nr * 512, SEEK_SET);
bytes_read = read(fd, buf, len); bytes_read = read(spt->fd, buf, len);
} }
close(fd);
return bytes_read; return bytes_read;
} }
static int tftp_send_oack(struct tftp_session *spt, static int tftp_send_oack(struct tftp_session *spt,
const char *key, uint32_t value, const char *keys[], uint32_t values[], int nb,
struct tftp_t *recv_tp) struct tftp_t *recv_tp)
{ {
struct sockaddr_in saddr, daddr; struct sockaddr_in saddr, daddr;
struct mbuf *m; struct mbuf *m;
struct tftp_t *tp; struct tftp_t *tp;
int n = 0; int i, n = 0;
m = m_get(spt->slirp); m = m_get(spt->slirp);
...@@ -136,10 +140,12 @@ static int tftp_send_oack(struct tftp_session *spt, ...@@ -136,10 +140,12 @@ static int tftp_send_oack(struct tftp_session *spt,
m->m_data += sizeof(struct udpiphdr); m->m_data += sizeof(struct udpiphdr);
tp->tp_op = htons(TFTP_OACK); tp->tp_op = htons(TFTP_OACK);
n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s", for (i = 0; i < nb; i++) {
key) + 1; n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u", keys[i]) + 1;
value) + 1; n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
values[i]) + 1;
}
saddr.sin_addr = recv_tp->ip.ip_dst; saddr.sin_addr = recv_tp->ip.ip_dst;
saddr.sin_port = recv_tp->udp.uh_dport; saddr.sin_port = recv_tp->udp.uh_dport;
...@@ -193,23 +199,18 @@ out: ...@@ -193,23 +199,18 @@ out:
tftp_session_terminate(spt); tftp_session_terminate(spt);
} }
static int tftp_send_data(struct tftp_session *spt, static void tftp_send_next_block(struct tftp_session *spt,
uint16_t block_nr, struct tftp_t *recv_tp)
struct tftp_t *recv_tp)
{ {
struct sockaddr_in saddr, daddr; struct sockaddr_in saddr, daddr;
struct mbuf *m; struct mbuf *m;
struct tftp_t *tp; struct tftp_t *tp;
int nobytes; int nobytes;
if (block_nr < 1) {
return -1;
}
m = m_get(spt->slirp); m = m_get(spt->slirp);
if (!m) { if (!m) {
return -1; return;
} }
memset(m->m_data, 0, m->m_size); memset(m->m_data, 0, m->m_size);
...@@ -219,7 +220,7 @@ static int tftp_send_data(struct tftp_session *spt, ...@@ -219,7 +220,7 @@ static int tftp_send_data(struct tftp_session *spt,
m->m_data += sizeof(struct udpiphdr); m->m_data += sizeof(struct udpiphdr);
tp->tp_op = htons(TFTP_DATA); tp->tp_op = htons(TFTP_DATA);
tp->x.tp_data.tp_block_nr = htons(block_nr); tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
saddr.sin_addr = recv_tp->ip.ip_dst; saddr.sin_addr = recv_tp->ip.ip_dst;
saddr.sin_port = recv_tp->udp.uh_dport; saddr.sin_port = recv_tp->udp.uh_dport;
...@@ -227,7 +228,7 @@ static int tftp_send_data(struct tftp_session *spt, ...@@ -227,7 +228,7 @@ static int tftp_send_data(struct tftp_session *spt,
daddr.sin_addr = spt->client_ip; daddr.sin_addr = spt->client_ip;
daddr.sin_port = spt->client_port; daddr.sin_port = spt->client_port;
nobytes = tftp_read_data(spt, block_nr - 1, tp->x.tp_data.tp_buf, 512); nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf, 512);
if (nobytes < 0) { if (nobytes < 0) {
m_free(m); m_free(m);
...@@ -236,7 +237,7 @@ static int tftp_send_data(struct tftp_session *spt, ...@@ -236,7 +237,7 @@ static int tftp_send_data(struct tftp_session *spt,
tftp_send_error(spt, 1, "File not found", tp); tftp_send_error(spt, 1, "File not found", tp);
return -1; return;
} }
m->m_len = sizeof(struct tftp_t) - (512 - nobytes) - m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
...@@ -251,7 +252,7 @@ static int tftp_send_data(struct tftp_session *spt, ...@@ -251,7 +252,7 @@ static int tftp_send_data(struct tftp_session *spt,
tftp_session_terminate(spt); tftp_session_terminate(spt);
} }
return 0; spt->block_nr++;
} }
static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen) static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
...@@ -260,6 +261,9 @@ static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen) ...@@ -260,6 +261,9 @@ static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
int s, k; int s, k;
size_t prefix_len; size_t prefix_len;
char *req_fname; char *req_fname;
const char *option_name[2];
uint32_t option_value[2];
int nb_options = 0;
/* check if a session already exists and if so terminate it */ /* check if a session already exists and if so terminate it */
s = tftp_session_find(slirp, tp); s = tftp_session_find(slirp, tp);
...@@ -337,7 +341,7 @@ static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen) ...@@ -337,7 +341,7 @@ static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
return; return;
} }
while (k < pktlen) { while (k < pktlen && nb_options < ARRAY_SIZE(option_name)) {
const char *key, *value; const char *key, *value;
key = &tp->x.tp_buf[k]; key = &tp->x.tp_buf[k];
...@@ -364,12 +368,32 @@ static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen) ...@@ -364,12 +368,32 @@ static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
} }
} }
tftp_send_oack(spt, "tsize", tsize, tp); option_name[nb_options] = "tsize";
return; option_value[nb_options] = tsize;
nb_options++;
} else if (strcasecmp(key, "blksize") == 0) {
int blksize = atoi(value);
/* If blksize option is bigger than what we will
* emit, accept the option with our packet size.
* Otherwise, simply do as we didn't see the option.
*/
if (blksize >= 512) {
option_name[nb_options] = "blksize";
option_value[nb_options] = 512;
nb_options++;
}
} }
} }
tftp_send_data(spt, 1, tp); if (nb_options > 0) {
assert(nb_options <= ARRAY_SIZE(option_name));
tftp_send_oack(spt, option_name, option_value, nb_options, tp);
return;
}
spt->block_nr = 0;
tftp_send_next_block(spt, tp);
} }
static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen) static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
...@@ -382,11 +406,7 @@ static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen) ...@@ -382,11 +406,7 @@ static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
return; return;
} }
if (tftp_send_data(&slirp->tftp_sessions[s], tftp_send_next_block(&slirp->tftp_sessions[s], tp);
ntohs(tp->x.tp_data.tp_block_nr) + 1,
tp) < 0) {
return;
}
} }
static void tftp_handle_error(Slirp *slirp, struct tftp_t *tp, int pktlen) static void tftp_handle_error(Slirp *slirp, struct tftp_t *tp, int pktlen)
......
...@@ -33,9 +33,11 @@ struct tftp_t { ...@@ -33,9 +33,11 @@ struct tftp_t {
struct tftp_session { struct tftp_session {
Slirp *slirp; Slirp *slirp;
char *filename; char *filename;
int fd;
struct in_addr client_ip; struct in_addr client_ip;
uint16_t client_port; uint16_t client_port;
uint32_t block_nr;
int timestamp; int timestamp;
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册