+ /* Examples of network traffic.
+ * Note two cases when ACKs with block# of 0 are sent.
+ *
+ * Download without options:
+ * tftp -> "\0\1FILENAME\0octet\0"
+ * "\0\3\0\1FILEDATA..." <- tftpd
+ * tftp -> "\0\4\0\1"
+ * ...
+ * Download with option of blksize 16384:
+ * tftp -> "\0\1FILENAME\0octet\0blksize\00016384\0"
+ * "\0\6blksize\00016384\0" <- tftpd
+ * tftp -> "\0\4\0\0"
+ * "\0\3\0\1FILEDATA..." <- tftpd
+ * tftp -> "\0\4\0\1"
+ * ...
+ * Upload without options:
+ * tftp -> "\0\2FILENAME\0octet\0"
+ * "\0\4\0\0" <- tftpd
+ * tftp -> "\0\3\0\1FILEDATA..."
+ * "\0\4\0\1" <- tftpd
+ * ...
+ * Upload with option of blksize 16384:
+ * tftp -> "\0\2FILENAME\0octet\0blksize\00016384\0"
+ * "\0\6blksize\00016384\0" <- tftpd
+ * tftp -> "\0\3\0\1FILEDATA..."
+ * "\0\4\0\1" <- tftpd
+ * ...
+ */
+ block_nr = 1;
+ cp = xbuf + 2;
+
+ if (!ENABLE_TFTP || our_lsa) { /* tftpd */
+ /* Open file (must be after changing user) */
+ local_fd = open(local_file, open_mode, 0666);
+ if (local_fd < 0) {
+ error_pkt_reason = ERR_NOFILE;
+ strcpy((char*)error_pkt_str, "can't open file");
+ goto send_err_pkt;
+ }
+/* gcc 4.3.1 would NOT optimize it out as it should! */
+#if ENABLE_FEATURE_TFTP_BLOCKSIZE
+ if (blksize != TFTP_BLKSIZE_DEFAULT || want_transfer_size) {
+ /* Create and send OACK packet. */
+ /* For the download case, block_nr is still 1 -
+ * we expect 1st ACK from peer to be for (block_nr-1),
+ * that is, for "block 0" which is our OACK pkt */
+ opcode = TFTP_OACK;
+ goto add_blksize_opt;
+ }
+#endif
+ if (CMD_GET(option_mask32)) {
+ /* It's upload and we don't send OACK.
+ * We must ACK 1st packet (with filename)
+ * as if it is "block 0" */
+ block_nr = 0;
+ }
+
+ } else { /* tftp */
+ /* Open file (must be after changing user) */
+ local_fd = CMD_GET(option_mask32) ? STDOUT_FILENO : STDIN_FILENO;
+ if (NOT_LONE_DASH(local_file))
+ local_fd = xopen(local_file, open_mode);
+/* Removing #if, or using if() statement instead of #if may lead to
+ * "warning: null argument where non-null required": */
+#if ENABLE_TFTP
+ /* tftp */
+
+ /* We can't (and don't really need to) bind the socket:
+ * we don't know from which local IP datagrams will be sent,
+ * but kernel will pick the same IP every time (unless routing
+ * table is changed), thus peer will see dgrams consistently
+ * coming from the same IP.
+ * We would like to connect the socket, but since peer's
+ * UDP code can be less perfect than ours, _peer's_ IP:port
+ * in replies may differ from IP:port we used to send
+ * our first packet. We can connect() only when we get
+ * first reply. */
+
+ /* build opcode */