+ if (CMD_GET(option_mask32)) {
+ opcode = TFTP_RRQ;
+ }
+ /* add filename and mode */
+ /* fill in packet if the filename fits into xbuf */
+ len = strlen(remote_file) + 1;
+ if (2 + len + sizeof("octet") >= io_bufsize) {
+ bb_error_msg("remote filename is too long");
+ goto ret;
+ }
+ strcpy(cp, remote_file);
+ cp += len;
+ /* add "mode" part of the packet */
+ strcpy(cp, "octet");
+ cp += sizeof("octet");
+
+# if ENABLE_FEATURE_TFTP_BLOCKSIZE
+ if (blksize == TFTP_BLKSIZE_DEFAULT && !want_transfer_size)
+ goto send_pkt;
+
+ /* Need to add option to pkt */
+ if ((&xbuf[io_bufsize - 1] - cp) < sizeof("blksize NNNNN tsize ") + sizeof(off_t)*3) {
+ bb_error_msg("remote filename is too long");
+ goto ret;
+ }
+ expect_OACK = 1;
+# endif
+#endif /* ENABLE_TFTP */
+
+#if ENABLE_FEATURE_TFTP_BLOCKSIZE
+ add_blksize_opt:
+ if (blksize != TFTP_BLKSIZE_DEFAULT) {
+ /* add "blksize", <nul>, blksize, <nul> */
+ strcpy(cp, "blksize");
+ cp += sizeof("blksize");
+ cp += snprintf(cp, 6, "%d", blksize) + 1;
+ }
+ if (want_transfer_size) {
+ /* add "tsize", <nul>, size, <nul> (see RFC2349) */
+ /* if tftp and downloading, we send "0" (since we opened local_fd with O_TRUNC)
+ * and this makes server to send "tsize" option with the size */
+ /* if tftp and uploading, we send file size (maybe dont, to not confuse old servers???) */
+ /* if tftpd and downloading, we are answering to client's request */
+ /* if tftpd and uploading: !want_transfer_size, this code is not executed */
+ struct stat st;
+ strcpy(cp, "tsize");
+ cp += sizeof("tsize");
+ st.st_size = 0;
+ fstat(local_fd, &st);
+ cp += sprintf(cp, "%"OFF_FMT"u", (off_t)st.st_size) + 1;
+# if ENABLE_FEATURE_TFTP_PROGRESS_BAR
+ /* Save for progress bar. If 0 (tftp downloading),
+ * we look at server's reply later */
+ G.size = st.st_size;
+ if (remote_file && st.st_size)
+ tftp_progress_init();
+# endif
+ }
+#endif
+ /* First packet is built, so skip packet generation */
+ goto send_pkt;