*: move get_sock_lsa and xwrite_str to libbb, use where appropriate
[oweals/busybox.git] / networking / tftp.c
index 71869e25f46d82b5d7983ef94e47c7c0b9ec6a3a..fa0851615b6a7658ec2056c58534f6ca6d563207 100644 (file)
@@ -92,9 +92,7 @@ struct globals {
 #define block_buf        (G.block_buf   )
 #define user_opt         (G.user_opt    )
 #define error_pkt        (G.error_pkt   )
-#define INIT_G() \
-       do { \
-       } while (0)
+#define INIT_G() do { } while (0)
 
 #define error_pkt_reason (error_pkt[3])
 #define error_pkt_str    (error_pkt + 4)
@@ -115,7 +113,7 @@ static int tftp_blksize_check(const char *blksize_str, int maxsize)
                bb_error_msg("bad blocksize '%s'", blksize_str);
                return -1;
        }
-#if ENABLE_DEBUG_TFTP
+#if ENABLE_TFTP_DEBUG
        bb_error_msg("using blksize %u", blksize);
 #endif
        return blksize;
@@ -225,9 +223,7 @@ static int tftp_protocol(
                }
 
                if (user_opt) {
-                       struct passwd *pw = getpwnam(user_opt);
-                       if (!pw)
-                               bb_error_msg_and_die("unknown user '%s'", user_opt);
+                       struct passwd *pw = xgetpwnam(user_opt);
                        change_identity(pw); /* initgroups, setgid, setuid */
                }
        }
@@ -258,6 +254,8 @@ static int tftp_protocol(
        }
 
        if (!ENABLE_TFTP || our_lsa) {
+/* gcc 4.3.1 would NOT optimize it out as it should! */
+#if ENABLE_FEATURE_TFTP_BLOCKSIZE
                if (blksize != TFTP_BLKSIZE_DEFAULT || tsize) {
                        /* Create and send OACK packet. */
                        /* For the download case, block_nr is still 1 -
@@ -266,6 +264,7 @@ static int tftp_protocol(
                        opcode = TFTP_OACK;
                        goto add_blksize_opt;
                }
+#endif
        } else {
 /* Removing it, or using if() statement instead of #if may lead to
  * "warning: null argument where non-null required": */
@@ -301,6 +300,7 @@ static int tftp_protocol(
                strcpy(cp, "octet");
                cp += sizeof("octet");
 
+#if ENABLE_FEATURE_TFTP_BLOCKSIZE
                if (blksize == TFTP_BLKSIZE_DEFAULT)
                        goto send_pkt;
 
@@ -309,9 +309,11 @@ static int tftp_protocol(
                        bb_error_msg("remote filename is too long");
                        goto ret;
                }
-               USE_FEATURE_TFTP_BLOCKSIZE(want_option_ack = 1;)
+               want_option_ack = 1;
+#endif
 #endif /* ENABLE_TFTP */
 
+#if ENABLE_FEATURE_TFTP_BLOCKSIZE
  add_blksize_opt:
 #if ENABLE_TFTPD
                if (tsize) {
@@ -329,6 +331,7 @@ static int tftp_protocol(
                        cp += sizeof("blksize");
                        cp += snprintf(cp, 6, "%d", blksize) + 1;
                }
+#endif
                /* First packet is built, so skip packet generation */
                goto send_pkt;
        }
@@ -364,7 +367,7 @@ static int tftp_protocol(
                waittime_ms = TFTP_TIMEOUT_MS;
 
  send_again:
-#if ENABLE_DEBUG_TFTP
+#if ENABLE_TFTP_DEBUG
                fprintf(stderr, "sending %u bytes\n", send_len);
                for (cp = xbuf; cp < &xbuf[send_len]; cp++)
                        fprintf(stderr, "%02x ", (unsigned char) *cp);
@@ -426,7 +429,7 @@ static int tftp_protocol(
                /* Process recv'ed packet */
                opcode = ntohs( ((uint16_t*)rbuf)[0] );
                recv_blk = ntohs( ((uint16_t*)rbuf)[1] );
-#if ENABLE_DEBUG_TFTP
+#if ENABLE_TFTP_DEBUG
                fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, recv_blk);
 #endif
                if (opcode == TFTP_ERROR) {
@@ -550,7 +553,7 @@ static int tftp_protocol(
 #if ENABLE_TFTP
 
 int tftp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int tftp_main(int argc ATTRIBUTE_UNUSED, char **argv)
+int tftp_main(int argc UNUSED_PARAM, char **argv)
 {
        len_and_sockaddr *peer_lsa;
        const char *local_file = NULL;
@@ -597,7 +600,7 @@ int tftp_main(int argc ATTRIBUTE_UNUSED, char **argv)
        port = bb_lookup_port(argv[1], "udp", 69);
        peer_lsa = xhost2sockaddr(argv[0], port);
 
-#if ENABLE_DEBUG_TFTP
+#if ENABLE_TFTP_DEBUG
        fprintf(stderr, "using server '%s', remote_file '%s', local_file '%s'\n",
                        xmalloc_sockaddr2dotted(&peer_lsa->u.sa),
                        remote_file, local_file);
@@ -619,23 +622,8 @@ int tftp_main(int argc ATTRIBUTE_UNUSED, char **argv)
 #endif /* ENABLE_TFTP */
 
 #if ENABLE_TFTPD
-
-/* TODO: libbb candidate? */
-static len_and_sockaddr *get_sock_lsa(int s)
-{
-       len_and_sockaddr *lsa;
-       socklen_t len = 0;
-
-       if (getsockname(s, NULL, &len) != 0)
-               return NULL;
-       lsa = xzalloc(LSA_LEN_SIZE + len);
-       lsa->len = len;
-       getsockname(s, &lsa->u.sa, &lsa->len);
-       return lsa;
-}
-
 int tftpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int tftpd_main(int argc ATTRIBUTE_UNUSED, char **argv)
+int tftpd_main(int argc UNUSED_PARAM, char **argv)
 {
        len_and_sockaddr *our_lsa;
        len_and_sockaddr *peer_lsa;
@@ -648,8 +636,15 @@ int tftpd_main(int argc ATTRIBUTE_UNUSED, char **argv)
        INIT_G();
 
        our_lsa = get_sock_lsa(STDIN_FILENO);
-       if (!our_lsa)
-               bb_perror_msg_and_die("stdin is not a socket");
+       if (!our_lsa) {
+               /* This is confusing:
+                *bb_error_msg_and_die("stdin is not a socket");
+                * Better: */
+               bb_show_usage();
+               /* Help text says that tftpd must be used as inetd service,
+                * which is by far the most usual cause of get_sock_lsa
+                * failure */
+       }
        peer_lsa = xzalloc(LSA_LEN_SIZE + our_lsa->len);
        peer_lsa->len = our_lsa->len;