slirp: Handle more than 65535 blocks in TFTP transfers
RFC 1350 does not mention block count roll-over. However, a lot of TFTP servers
implement it to be able to transmit big files, so do it also.
Current block size is 512 bytes, so TFTP files were limited to 32 MB.
Signed-off-by: Hervé Poussineau <hpoussin@reactos.org>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
diff --git a/tftp.c b/tftp.c
index 7009675..6e6aee8 100644
--- a/tftp.c
+++ b/tftp.c
@@ -98,7 +98,7 @@
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)
{
int bytes_read = 0;
@@ -194,18 +194,14 @@
tftp_session_terminate(spt);
}
-static int tftp_send_data(struct tftp_session *spt, uint16_t block_nr,
- struct tftp_t *recv_tp)
+static int tftp_send_next_block(struct tftp_session *spt,
+ 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;
- }
-
m = m_get(spt->slirp);
if (!m) {
@@ -219,7 +215,7 @@
m->m_data += sizeof(struct udpiphdr);
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_port = recv_tp->udp.uh_dport;
@@ -227,7 +223,7 @@
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);
+ nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf, 512);
if (nobytes < 0) {
m_free(m);
@@ -250,6 +246,7 @@
tftp_session_terminate(spt);
}
+ spt->block_nr++;
return 0;
}
@@ -367,7 +364,8 @@
}
}
- tftp_send_data(spt, 1, tp);
+ spt->block_nr = 0;
+ tftp_send_next_block(spt, tp);
}
static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
@@ -380,8 +378,7 @@
return;
}
- if (tftp_send_data(&slirp->tftp_sessions[s],
- ntohs(tp->x.tp_data.tp_block_nr) + 1, tp) < 0) {
+ if (tftp_send_next_block(&slirp->tftp_sessions[s], tp) < 0) {
return;
}
}
diff --git a/tftp.h b/tftp.h
index b0952e2..c19b159 100644
--- a/tftp.h
+++ b/tftp.h
@@ -37,6 +37,7 @@
struct in_addr client_ip;
uint16_t client_port;
+ uint32_t block_nr;
int timestamp;
};