#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)
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;
}
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 */
}
}
}
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 -
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": */
strcpy(cp, "octet");
cp += sizeof("octet");
+#if ENABLE_FEATURE_TFTP_BLOCKSIZE
if (blksize == TFTP_BLKSIZE_DEFAULT)
goto send_pkt;
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) {
cp += sizeof("blksize");
cp += snprintf(cp, 6, "%d", blksize) + 1;
}
+#endif
/* First packet is built, so skip packet generation */
goto send_pkt;
}
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);
/* 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) {
#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;
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);
#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;
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;