diff options
| author | Marek Vasut <marek.vasut+renesas@mailbox.org> | 2026-01-29 00:43:45 +0100 |
|---|---|---|
| committer | Jerome Forissier <jerome.forissier@arm.com> | 2026-02-06 16:42:37 +0100 |
| commit | 337f50bad2ac8e1db126e4f6d372a3186bba2893 (patch) | |
| tree | 893e2b6ce6c75fa1fbf900cb1a1c57c3f03df254 /lib | |
| parent | a28db0f1ccd6d7f88e9715486376bc039975f72c (diff) | |
net: lwip: tftp: add support of tsize option to client
The TFTP server can report the size of the entire file that is about to
be received in the Transfer Size Option, this is described in RFC 2349.
This functionality is optional and the server may not report tsize in
case it is not supported.
Always send tsize request to the server to query the transfer size,
and in case the server does respond, cache that information locally
in tftp_state.tsize, otherwise cache size 0. Introduce new function
tftp_client_get_tsize() which returns the cached tftp_state.tsize so
clients can determine the transfer size and use it.
Update net/lwip/tftp.c to make use of tftp_client_get_tsize() and
avoid excessive printing of '#' during TFTP transfers in case the
transfer size is reported by the server.
Submitted upstream: https://savannah.nongnu.org/patch/index.php?item_id=10557
Signed-off-by: Marek Vasut <marek.vasut+renesas@mailbox.org>
Acked-by: Jerome Forissier <jerome.forissier@arm.com>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/lwip/lwip/src/apps/tftp/tftp.c | 51 | ||||
| -rw-r--r-- | lib/lwip/lwip/src/include/lwip/apps/tftp_client.h | 1 |
2 files changed, 43 insertions, 9 deletions
diff --git a/lib/lwip/lwip/src/apps/tftp/tftp.c b/lib/lwip/lwip/src/apps/tftp/tftp.c index 25da952e925..e73bea20e63 100644 --- a/lib/lwip/lwip/src/apps/tftp/tftp.c +++ b/lib/lwip/lwip/src/apps/tftp/tftp.c @@ -98,6 +98,7 @@ struct tftp_state { int last_pkt; u16_t blknum; u16_t blksize; + u32_t tsize; u8_t retries; u8_t mode_write; u8_t tftp_mode; @@ -167,6 +168,7 @@ send_request(const ip_addr_t *addr, u16_t port, u16_t opcode, const char* fname, { size_t fname_length = strlen(fname)+1; size_t mode_length = strlen(mode)+1; + size_t tsize_length = strlen("tsize")+3; /* "tsize\0\0\0" */ size_t blksize_length = 0; int blksize = tftp_state.blksize; struct pbuf* p; @@ -182,7 +184,7 @@ send_request(const ip_addr_t *addr, u16_t port, u16_t opcode, const char* fname, } } - p = init_packet(opcode, 0, fname_length + mode_length + blksize_length - 2); + p = init_packet(opcode, 0, fname_length + mode_length + blksize_length + tsize_length - 2); if (p == NULL) { return ERR_MEM; } @@ -190,8 +192,9 @@ send_request(const ip_addr_t *addr, u16_t port, u16_t opcode, const char* fname, payload = (char*) p->payload; MEMCPY(payload+2, fname, fname_length); MEMCPY(payload+2+fname_length, mode, mode_length); + sprintf(payload+2+fname_length+mode_length, "tsize%c%u%c", 0, 0, 0); if (tftp_state.blksize) - sprintf(payload+2+fname_length+mode_length, "blksize%c%d", 0, tftp_state.blksize); + sprintf(payload+2+fname_length+mode_length+tsize_length, "blksize%c%d", 0, tftp_state.blksize); tftp_state.wait_oack = true; ret = udp_sendto(tftp_state.upcb, p, addr, port); @@ -450,14 +453,24 @@ tftp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr } blknum = lwip_ntohs(sbuf[1]); - if (tftp_state.blksize && tftp_state.wait_oack) { + if (tftp_state.wait_oack) { /* - * Data received while we are expecting an OACK for our blksize option. + * Data received while we are expecting an OACK for our tsize option. * This means the server doesn't support it, let's switch back to the * default block size. */ - tftp_state.blksize = 0; - tftp_state.wait_oack = false; + tftp_state.tsize = 0; + tftp_state.wait_oack = false; + + if (tftp_state.blksize) { + /* + * Data received while we are expecting an OACK for our blksize option. + * This means the server doesn't support it, let's switch back to the + * default block size. + */ + tftp_state.blksize = 0; + tftp_state.wait_oack = false; + } } if (blknum == tftp_state.blknum) { pbuf_remove_header(p, TFTP_HEADER_LENGTH); @@ -527,21 +540,31 @@ tftp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr } break; case PP_HTONS(TFTP_OACK): { - const char *optval = find_option(p, "blksize"); + const char *blksizeoptval = find_option(p, "blksize"); + const char *tsizeoptval = find_option(p, "tsize"); u16_t srv_blksize = 0; + u32_t srv_tsize = 0; tftp_state.wait_oack = false; - if (optval) { + if (blksizeoptval) { if (!tftp_state.blksize) { /* We did not request this option */ send_error(addr, port, TFTP_ERROR_ILLEGAL_OPERATION, "blksize unexpected"); } - srv_blksize = atoi(optval); + srv_blksize = atoi(blksizeoptval); if (srv_blksize <= 0 || srv_blksize > tftp_state.blksize) { send_error(addr, port, TFTP_ERROR_ILLEGAL_OPERATION, "Invalid blksize"); } LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, ("tftp: accepting blksize=%d\n", srv_blksize)); tftp_state.blksize = srv_blksize; } + if (tsizeoptval) { + srv_tsize = atoi(tsizeoptval); + if (srv_tsize <= 0) { + srv_tsize = 0; /* tsize is optional */ + } + LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, ("tftp: accepting tsize=%d\n", srv_tsize)); + tftp_state.tsize = srv_tsize; + } send_ack(addr, port, 0); break; } @@ -656,6 +679,16 @@ tftp_init_client(const struct tftp_context *ctx) } /** @ingroup tftp + * Get the transfer size used by the TFTP client. The server may + * report zero in case this is unsupported. + */ +u32_t +tftp_client_get_tsize(void) +{ + return tftp_state.tsize; +} + +/** @ingroup tftp * Set the block size to be used by the TFTP client. The server may choose to * accept a lower value. * @param blksize Block size in bytes diff --git a/lib/lwip/lwip/src/include/lwip/apps/tftp_client.h b/lib/lwip/lwip/src/include/lwip/apps/tftp_client.h index e1e21d06b67..78de50f4924 100644 --- a/lib/lwip/lwip/src/include/lwip/apps/tftp_client.h +++ b/lib/lwip/lwip/src/include/lwip/apps/tftp_client.h @@ -45,6 +45,7 @@ enum tftp_transfer_mode { err_t tftp_init_client(const struct tftp_context* ctx); void tftp_client_set_blksize(u16_t blksize); +u32_t tftp_client_get_tsize(void); err_t tftp_get(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, enum tftp_transfer_mode mode); err_t tftp_put(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, enum tftp_transfer_mode mode); |
