Patch from Denis Vlasenko turning static const int (which gets emitted into
[oweals/busybox.git] / networking / telnetd.c
index b3d0a1166d8f279bb17d50280bae8f7988043f07..8943b2e3b181c7e44080d199a17e56556d530a85 100644 (file)
@@ -33,6 +33,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <signal.h>
@@ -93,7 +94,7 @@ struct tsession {
    +-------+     wridx1++     +------+     rdidx1++     +----------+
    |       | <--------------  | buf1 | <--------------  |          |
    |       |     size1--      +------+     size1++      |          |
-   |  pty  |                                            |  socket  |
+   |  pty  |                                       |  socket  |
    |       |     rdidx2++     +------+     wridx2++     |          |
    |       |  --------------> | buf2 |  --------------> |          |
    +-------+     size2++      +------+     size2--      +----------+
@@ -130,7 +131,7 @@ static struct tsession *sessions;
   */
 static char *
 remove_iacs(struct tsession *ts, int *pnum_totty) {
-       unsigned char *ptr0 = ts->buf1 + ts->wridx1;
+       unsigned char *ptr0 = (unsigned char *)ts->buf1 + ts->wridx1;
        unsigned char *ptr = ptr0;
        unsigned char *totty = ptr;
        unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1);
@@ -165,7 +166,7 @@ remove_iacs(struct tsession *ts, int *pnum_totty) {
                        else if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) {
                                struct winsize ws;
                                if ((ptr+8) >= end)
-                                       break;  /* incomplete, can't process */
+                                       break;  /* incomplete, can't process */
                                ws.ws_col = (ptr[3] << 8) | ptr[4];
                                ws.ws_row = (ptr[5] << 8) | ptr[6];
                                (void) ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
@@ -382,7 +383,7 @@ int
 telnetd_main(int argc, char **argv)
 {
 #ifndef CONFIG_FEATURE_TELNETD_INETD
-        sockaddr_type sa;
+       sockaddr_type sa;
        int master_fd;
 #endif /* CONFIG_FEATURE_TELNETD_INETD */
        fd_set rdfdset, wrfdset;
@@ -390,13 +391,14 @@ telnetd_main(int argc, char **argv)
 #ifndef CONFIG_FEATURE_TELNETD_INETD
        int on = 1;
        int portnbr = 23;
+       struct in_addr bind_addr = { .s_addr = 0x0 };
 #endif /* CONFIG_FEATURE_TELNETD_INETD */
        int c;
        static const char options[] =
 #ifdef CONFIG_FEATURE_TELNETD_INETD
                "f:l:";
 #else /* CONFIG_EATURE_TELNETD_INETD */
-               "f:l:p:";
+               "f:l:p:b:";
 #endif /* CONFIG_FEATURE_TELNETD_INETD */
        int maxlen, w, r;
 
@@ -418,6 +420,10 @@ telnetd_main(int argc, char **argv)
                        case 'p':
                                portnbr = atoi(optarg);
                                break;
+                       case 'b':
+                               if (inet_aton(optarg, &bind_addr) == 0)
+                                       bb_show_usage();
+                               break;
 #endif /* CONFIG_FEATURE_TELNETD_INETD */
                        default:
                                bb_show_usage();
@@ -440,7 +446,7 @@ telnetd_main(int argc, char **argv)
 
        /* Grab a TCP socket.  */
 
-        master_fd = socket(SOCKET_TYPE, SOCK_STREAM, 0);
+       master_fd = socket(SOCKET_TYPE, SOCK_STREAM, 0);
        if (master_fd < 0) {
                bb_perror_msg_and_die("socket");
        }
@@ -452,9 +458,11 @@ telnetd_main(int argc, char **argv)
 #ifdef CONFIG_FEATURE_IPV6
        sa.sin6_family = AF_INET6;
        sa.sin6_port = htons(portnbr);
+       /* sa.sin6_addr = bind_addr6; */
 #else
        sa.sin_family = AF_INET;
        sa.sin_port = htons(portnbr);
+       sa.sin_addr = bind_addr;
 #endif
 
        if (bind(master_fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {