Implement suggestion from Adam Slattery, (don't default to killing closing bug #1190.
[oweals/busybox.git] / telnet.c
index 758cce8b163edc2410201127500523446ef6bb56..ce82a0ee8829699aa584160d189e7eac4f3cca0b 100644 (file)
--- a/telnet.c
+++ b/telnet.c
@@ -27,6 +27,8 @@
  * initial revision
  * Modified 2000/06/13 for inclusion into BusyBox by Erik Andersen
  * <andersen@lineo.com> 
+ * Modified 2001/05/07 to add ability to pass TTYPE to remote host by Jim McQuillan
+ * <jam@ltsp.org>
  *
  */
 
@@ -62,8 +64,8 @@ static const int DOTRACE = 1;
 #include <sys/time.h>
 #endif
 
-static const int DATABUFSIZE = 128;
-static const int IACBUFSIZE = 128;
+#define DATABUFSIZE  128
+#define IACBUFSIZE   128
 
 static const int CHM_TRY = 0;
 static const int CHM_ON = 1;
@@ -85,18 +87,17 @@ enum {
 typedef unsigned char byte;
 
 /* use globals to reduce size ??? */ /* test this hypothesis later */
-struct Globalvars {
+static struct Globalvars {
        int             netfd; /* console fd:s are 0 and 1 (and 2) */
     /* same buffer used both for network and console read/write */
-       char *  buf; /* allocating so static size is smaller */
-       short   len;
+       char    buf[DATABUFSIZE]; /* allocating so static size is smaller */
        byte    telstate; /* telnet negotiation state from network input */
        byte    telwish;  /* DO, DONT, WILL, WONT */
        byte    charmode;
        byte    telflags;
        byte    gotsig;
        /* buffer to handle telnet negotiations */
-       char *  iacbuf;
+       char    iacbuf[IACBUFSIZE];
        short   iaclen; /* could even use byte */
        struct termios termios_def;     
        struct termios termios_raw;     
@@ -108,7 +109,7 @@ struct Globalvars {
 struct Globalvars * Gptr;
 #define G (*Gptr)
 #else
-struct Globalvars G;
+static struct Globalvars G;
 #endif
 
 static inline void iacflush()
@@ -136,6 +137,10 @@ static int local_bind(int port);
 /* Some globals */
 static int one = 1;
 
+#ifdef BB_FEATURE_TELNET_TTYPE
+static char *ttype;
+#endif
+
 static void doexit(int ev)
 {
        cookmode();
@@ -192,7 +197,7 @@ static void conescape()
        G.gotsig = 0;
        
 }
-static void handlenetoutput()
+static void handlenetoutput(int len)
 {
        /*      here we could do smart tricks how to handle 0xFF:s in output
         *      stream  like writing twice every sequence of FF:s (thus doing
@@ -203,7 +208,7 @@ static void handlenetoutput()
        int i;
        byte * p = G.buf;
 
-       for (i = G.len; i > 0; i--, p++)
+       for (i = len; i > 0; i--, p++)
        {
                if (*p == 0x1d)
                {
@@ -213,16 +218,16 @@ static void handlenetoutput()
                if (*p == 0xff)
                        *p = 0x7f;
        }
-       write(G.netfd, G.buf, G.len);
+       write(G.netfd, G.buf, len);
 }
 
 
-static void handlenetinput()
+static void handlenetinput(int len)
 {
        int i;
        int cstart = 0;
 
-       for (i = 0; i < G.len; i++)
+       for (i = 0; i < len; i++)
        {
                byte c = G.buf[i];
 
@@ -284,11 +289,11 @@ static void handlenetinput()
                if (G.iaclen)                   iacflush();
                if (G.telstate == TS_0) G.telstate = 0;
 
-               G.len = cstart;
+               len = cstart;
        }
 
-       if (G.len)
-               write(1, G.buf, G.len);
+       if (len)
+               write(1, G.buf, len);
 }
 
 
@@ -321,6 +326,27 @@ static void putiac1(byte c)
 }
 #endif
 
+#ifdef BB_FEATURE_TELNET_TTYPE
+static void putiac_subopt(byte c, char *str)
+{
+       int     len = strlen(str) + 6;   // ( 2 + 1 + 1 + strlen + 2 )
+
+       if (G.iaclen + len > IACBUFSIZE)
+               iacflush();
+
+       putiac(IAC);
+       putiac(SB);
+       putiac(c);
+       putiac(0);
+
+       while(*str)
+               putiac(*str++);
+
+       putiac(IAC);
+       putiac(SE);
+}
+#endif
+
 /* void putiacstring (subneg strings) */
 
 /* ******************************* */
@@ -427,12 +453,29 @@ static inline void to_sga()
        return;
 }
 
+#ifdef BB_FEATURE_TELNET_TTYPE
+static inline void to_ttype()
+{
+       /* Tell server we will (or won't) do TTYPE */
+
+       if(ttype)
+               putiac2(WILL, TELOPT_TTYPE);
+       else
+               putiac2(WONT, TELOPT_TTYPE);
+
+       return;
+}
+#endif
+
 static void telopt(byte c)
 {
        switch (c)
        {
        case TELOPT_ECHO:               to_echo(c);             break;
        case TELOPT_SGA:                to_sga(c);              break;
+#ifdef BB_FEATURE_TELNET_TTYPE
+       case TELOPT_TTYPE:              to_ttype(c);    break;
+#endif
        default:                                to_notsup(c);   break;
        }
 }
@@ -440,7 +483,7 @@ static void telopt(byte c)
 
 /* ******************************* */
 
-/* subnegotiation -- ignore all */
+/* subnegotiation -- ignore all (except TTYPE) */
 
 static int subneg(byte c)
 {
@@ -449,6 +492,11 @@ static int subneg(byte c)
        case TS_SUB1:
                if (c == IAC)
                        G.telstate = TS_SUB2;
+#ifdef BB_FEATURE_TELNET_TTYPE
+               else
+               if (c == TELOPT_TTYPE)
+                       putiac_subopt(TELOPT_TTYPE,ttype);
+#endif
                break;
        case TS_SUB2:
                if (c == SE)
@@ -481,6 +529,7 @@ extern int telnet_main(int argc, char** argv)
 {
        struct in_addr host;
        int port;
+       int len;
 #ifdef USE_POLL
        struct pollfd ufds[2];
 #else  
@@ -488,6 +537,9 @@ extern int telnet_main(int argc, char** argv)
        int maxfd;
 #endif 
 
+#ifdef BB_FEATURE_TELNET_TTYPE
+    ttype = getenv("TERM");
+#endif
 
        memset(&G, 0, sizeof G);
 
@@ -495,15 +547,11 @@ extern int telnet_main(int argc, char** argv)
                exit(1);
        
        G.termios_raw = G.termios_def;
-
        cfmakeraw(&G.termios_raw);
        
        if (argc < 2)   show_usage();
        port = (argc > 2)? getport(argv[2]): 23;
        
-       G.buf = xmalloc(DATABUFSIZE);
-       G.iacbuf = xmalloc(IACBUFSIZE);
-       
        host = getserver(argv[1]);
 
        G.netfd = remote_connect(host, port);
@@ -547,14 +595,14 @@ extern int telnet_main(int argc, char** argv)
                        if (FD_ISSET(0, &rfds))
 #endif                         
                        {
-                               G.len = read(0, G.buf, DATABUFSIZE);
+                               len = read(0, G.buf, DATABUFSIZE);
 
-                               if (G.len <= 0)
+                               if (len <= 0)
                                        doexit(0);
 
-                               TRACE(0, ("Read con: %d\n", G.len));
+                               TRACE(0, ("Read con: %d\n", len));
                                
-                               handlenetoutput();
+                               handlenetoutput(len);
                        }
 
 #ifdef USE_POLL
@@ -563,16 +611,16 @@ extern int telnet_main(int argc, char** argv)
                        if (FD_ISSET(G.netfd, &rfds))
 #endif                         
                        {
-                               G.len = read(G.netfd, G.buf, DATABUFSIZE);
+                               len = read(G.netfd, G.buf, DATABUFSIZE);
 
-                               if (G.len <= 0)
+                               if (len <= 0)
                                {
                                        WriteCS(1, "Connection closed by foreign host.\r\n");
                                        doexit(1);
                                }
-                               TRACE(0, ("Read netfd (%d): %d\n", G.netfd, G.len));
+                               TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
 
-                               handlenetinput();
+                               handlenetinput(len);
                        }
                }
        }
@@ -592,18 +640,15 @@ static int getport(char * p)
 static struct in_addr getserver(char * host)
 {
        struct in_addr addr;
-       
+
        struct hostent * he;
-       if ((he = gethostbyname(host)) == NULL)
-       {
-               error_msg_and_die("%s: Unknown host", host);
-       }
+       he = xgethostbyname(host);
        memcpy(&addr, he->h_addr, sizeof addr);
 
        TRACE(1, ("addr: %s\n", inet_ntoa(addr)));
-       
+
        return addr;
-}      
+}
 
 static int create_socket()
 {
@@ -612,7 +657,7 @@ static int create_socket()
 
 static void setup_sockaddr_in(struct sockaddr_in * addr, int port)
 {
-       memset(addr, 0, sizeof addr);
+       memset(addr, 0, sizeof(struct sockaddr_in));
        addr->sin_family = AF_INET;
        addr->sin_port = htons(port);
 }