Skip to contentSkip to conversation

tftp: prepare for picotcp

PATCH Applied Antony Pavlov 2 participants 5 messages Jun 28, 2015 – Jun 29, 2015 VFS

lore
Share & tools
lore
#0 lore raw

cover tftp: prepare for picotcp

This patchseries makes it much easier to run barebox tftp client on top of picotcp network stack in the future.

Antony Pavlov (3):
fs/tftp: handle incoming packets in the separate tftp_recv() function tftp_recv(): according to RFC1350 minimal tftp packet length is 4
bytes
tftp_recv(): handle opcode field in a more natural way

 fs/tftp.c | 47 ++++++++++++++++++++++++++---------------------
 1 file changed, 26 insertions(+), 21 deletions(-)
Signature · 7 lines
2.1.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 1/3 fs/tftp: handle incoming packets in the separate tftp_recv() function

The separation of incoming packets handling makes it much easier to run barebox tftp client on top of picotcp network stack in the future.

---
 fs/tftp.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
fs/tftp.c +17 -10
index 72e4983..d970c60 100644
@@ -253,15 +253,12 @@ static void tftp_timer_reset(struct file_priv *priv)
 	priv->progress_timeout = priv->resend_timeout = get_time_ns();
 }
 
-static void tftp_handler(void *ctx, char *packet, unsigned len)
+static void tftp_recv(struct file_priv *priv,
+			uint8_t *pkt, unsigned len, uint16_t uh_sport)
 {
-	struct file_priv *priv = ctx;
 	uint16_t proto;
 	uint16_t *s;
-	char *pkt = net_eth_to_udp_payload(packet);
-	struct udphdr *udp = net_eth_to_udphdr(packet);
 
-	len = net_eth_to_udplen(packet);
 	if (len < 2)
 		return;
 
@@ -296,14 +293,14 @@ static void tftp_handler(void *ctx, char *packet, unsigned len)
 			priv->state = STATE_DONE;
 			break;
 		}
-		priv->tftp_con->udp->uh_dport = udp->uh_sport;
+		priv->tftp_con->udp->uh_dport = uh_sport;
 		priv->state = STATE_WDATA;
 		break;
 
 	case TFTP_OACK:
 		tftp_parse_oack(priv, pkt, len);
-		priv->server_port = ntohs(udp->uh_sport);
-		priv->tftp_con->udp->uh_dport = udp->uh_sport;
+		priv->server_port = ntohs(uh_sport);
+		priv->tftp_con->udp->uh_dport = uh_sport;
 
 		if (priv->push) {
 			/* send first block */
@@ -326,8 +323,8 @@ static void tftp_handler(void *ctx, char *packet, unsigned len)
 		if (priv->state == STATE_RRQ || priv->state == STATE_OACK) {
 			/* first block received */
 			priv->state = STATE_RDATA;
-			priv->tftp_con->udp->uh_dport = udp->uh_sport;
-			priv->server_port = ntohs(udp->uh_sport);
+			priv->tftp_con->udp->uh_dport = uh_sport;
+			priv->server_port = ntohs(uh_sport);
 			priv->last_block = 0;
 
 			if (priv->block != 1) {	/* Assertion */
@@ -376,6 +373,16 @@ static void tftp_handler(void *ctx, char *packet, unsigned len)
 	}
 }
 
+static void tftp_handler(void *ctx, char *packet, unsigned len)
+{
+	struct file_priv *priv = ctx;
+	char *pkt = net_eth_to_udp_payload(packet);
+	struct udphdr *udp = net_eth_to_udphdr(packet);
+
+	(void)len;
+	tftp_recv(priv, pkt, net_eth_to_udplen(packet), udp->uh_sport);
+}
+
 static struct file_priv *tftp_do_open(struct device_d *dev,
 		int accmode, const char *filename)
 {
Signature · 7 lines
2.1.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

PATCH 2/3 tftp_recv(): according to RFC1350 minimal tftp packet length is 4 bytes

In reply to Antony Pavlov
---
 fs/tftp.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/tftp.c +2 -3
index d970c60..e36c1c8 100644
@@ -259,7 +259,8 @@ static void tftp_recv(struct file_priv *priv,
 	uint16_t proto;
 	uint16_t *s;
 
-	if (len < 2)
+	/* according to RFC1350 minimal tftp packet length is 4 bytes */
+	if (len < 4)
 		return;
 
 	len -= 2;
@@ -315,8 +316,6 @@ static void tftp_recv(struct file_priv *priv,
 
 		break;
 	case TFTP_DATA:
-		if (len < 2)
-			return;
 		len -= 2;
 		priv->block = ntohs(*(uint16_t *)pkt);
 
Signature · 7 lines
2.1.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

PATCH 3/3 tftp_recv(): handle opcode field in a more natural way

In reply to Antony Pavlov

RFC1350 uses the 'opcode' term for the first 2-bytes field of TFTP packet. But the U-boot tftp code uses the 'proto' term for the same thing.

The patch takes back original term and makes opcode calculation more clear.

---
 fs/tftp.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
fs/tftp.c +7 -8
index e36c1c8..0de215e 100644
@@ -256,22 +256,21 @@ static void tftp_timer_reset(struct file_priv *priv)
 static void tftp_recv(struct file_priv *priv,
 			uint8_t *pkt, unsigned len, uint16_t uh_sport)
 {
-	uint16_t proto;
-	uint16_t *s;
+	uint16_t opcode;
 
 	/* according to RFC1350 minimal tftp packet length is 4 bytes */
 	if (len < 4)
 		return;
 
-	len -= 2;
+	opcode = ntohs(*(uint16_t *)pkt);
 
-	s = (uint16_t *)pkt;
-	proto = *s++;
-	pkt = (unsigned char *)s;
+	/* skip tftp opcode 2-byte field */
+	len -= 2;
+	pkt += 2;
 
-	debug("%s: proto 0x%04x\n", __func__, proto);
+	debug("%s: opcode 0x%04x\n", __func__, opcode);
 
-	switch (ntohs(proto)) {
+	switch (opcode) {
 	case TFTP_RRQ:
 	case TFTP_WRQ:
 	default:
Signature · 7 lines
2.1.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Applied
#4 lore raw
In reply to Antony Pavlov
Quoted 11 lines · Antony Pavlov

This patchseries makes it much easier to run barebox tftp client on top of picotcp network stack in the future.

Antony Pavlov (3):
fs/tftp: handle incoming packets in the separate tftp_recv() function tftp_recv(): according to RFC1350 minimal tftp packet length is 4
bytes
tftp_recv(): handle opcode field in a more natural way

 fs/tftp.c | 47 ++++++++++++++++++++++++++---------------------
 1 file changed, 26 insertions(+), 21 deletions(-)

Applied, thanks

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox