Bug fix: line discipline selection is not enabled until after ssh
[u/mdw/putty] / telnet.c
index b6e4802..5e6ba3c 100644 (file)
--- a/telnet.c
+++ b/telnet.c
@@ -133,10 +133,7 @@ static struct Opt *opts[] = {
     &o_we_sga, &o_they_sga, NULL
 };
 
-#if 0
 static int in_synch;
-#endif
-
 static int sb_opt, sb_len;
 static char *sb_buf = NULL;
 static int sb_size = 0;
@@ -168,24 +165,13 @@ static void s_write (void *buf, int len) {
     try_write();
 }
 
-static void c_write (char *buf, int len) {
-    while (len--) {
-       int new_head = (inbuf_head + 1) & INBUF_MASK;
-       int c = (unsigned char) *buf;
-       if (new_head != inbuf_reap) {
-           inbuf[inbuf_head] = *buf++;
-           inbuf_head = new_head;
-       }
-    }
-}
-
 static void log_option (char *sender, int cmd, int option) {
     char buf[50];
     sprintf(buf, "%s:\t%s %s", sender,
            (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" :
             cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"),
            telopt(option));
-    lognegot(buf);
+    logevent(buf);
 }
 
 static void send_opt (int cmd, int option) {
@@ -214,6 +200,8 @@ static void activate_option (struct Opt *o) {
         */
        deactivate_option (o->option==TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
     }
+    if (o->option == TELOPT_ECHO && cfg.ldisc_term)
+       ldisc = &ldisc_simple;
 }
 
 static void refused_option (struct Opt *o) {
@@ -222,6 +210,8 @@ static void refused_option (struct Opt *o) {
        send_opt (WILL, TELOPT_OLD_ENVIRON);
        o_oenv.state = REQUESTED;
     }
+    if (o->option == TELOPT_ECHO && cfg.ldisc_term)
+       ldisc = &ldisc_term;
 }
 
 static void proc_rec_opt (int cmd, int option) {
@@ -286,11 +276,11 @@ static void process_subneg (void) {
            n = 4 + strlen(cfg.termspeed);
            b[n] = IAC; b[n+1] = SE;
            s_write (b, n+2);
-           lognegot("server:\tSB TSPEED SEND");
+           logevent("server:\tSB TSPEED SEND");
            sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed);
-           lognegot (logbuf);
+           logevent (logbuf);
        } else
-           lognegot ("server:\tSB TSPEED <something weird>");
+           logevent ("server:\tSB TSPEED <something weird>");
        break;
       case TELOPT_TTYPE:
        if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) {
@@ -303,11 +293,11 @@ static void process_subneg (void) {
            b[n+4] = IAC; b[n+5] = SE;
            s_write (b, n+6);
            b[n+4] = 0;
-           lognegot("server:\tSB TTYPE SEND");
+           logevent("server:\tSB TTYPE SEND");
            sprintf(logbuf, "client:\tSB TTYPE IS %s", b+4);
-           lognegot(logbuf);
+           logevent(logbuf);
        } else
-           lognegot("server:\tSB TTYPE <something weird>\r\n");
+           logevent("server:\tSB TTYPE <something weird>\r\n");
        break;
       case TELOPT_OLD_ENVIRON:
       case TELOPT_NEW_ENVIRON: 
@@ -317,7 +307,7 @@ static void process_subneg (void) {
            char logbuf[50];
            p++;
            sprintf (logbuf, "server:\tSB %s SEND", telopt(sb_opt));
-           lognegot (logbuf);
+           logevent (logbuf);
            if (sb_opt == TELOPT_OLD_ENVIRON) {
                if (cfg.rfc_environ) {
                    value = RFC_VALUE;
@@ -369,7 +359,7 @@ static void process_subneg (void) {
            s_write (b, n);
            sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt),
                    n==6 ? "<nothing>" : "<stuff>");
-           lognegot (logbuf);
+           logevent (logbuf);
        }
        break;
     }
@@ -381,7 +371,6 @@ static enum {
 } telnet_state = TOPLEVEL;
 
 static void do_telnet_read (char *buf, int len) {
-    unsigned char b[10];
 
     while (len--) {
        int c = (unsigned char) *buf++;
@@ -394,11 +383,20 @@ static void do_telnet_read (char *buf, int len) {
            else if (c == IAC)
                telnet_state = SEENIAC;
            else {
-               b[0] = c;
-#if 0
                if (!in_synch)
+                   c_write1(c);
+
+#if 1
+               /* I can't get the F***ing winsock to insert the urgent IAC
+                * into the right position! Even with SO_OOBINLINE it gives
+                * it to recv too soon. And of course the DM byte (that
+                * arrives in the same packet!) appears several K later!!
+                *
+                * Oh well, we do get the DM in the right place so I'll
+                * just stop hiding on the next 0xf2 and hope for the best.
+                */
+               else if (c == DM) in_synch = 0;
 #endif
-                   c_write (b, 1);
                if (c == CR)
                    telnet_state = SEENCR;
                else
@@ -411,7 +409,17 @@ static void do_telnet_read (char *buf, int len) {
            else if (c == WILL) telnet_state = SEENWILL;
            else if (c == WONT) telnet_state = SEENWONT;
            else if (c == SB) telnet_state = SEENSB;
-           else telnet_state = TOPLEVEL;/* ignore _everything_ else! */
+           else if (c == DM) {
+               in_synch = 0;
+               telnet_state = TOPLEVEL;
+           } 
+           else {
+               /* ignore everything else; print it if it's IAC */
+               if (c == IAC) {
+                   c_write1(c);
+               }
+               telnet_state = TOPLEVEL;
+           }
            break;
          case SEENWILL:
            proc_rec_opt (WILL, c);
@@ -443,8 +451,8 @@ static void do_telnet_read (char *buf, int len) {
                    char *newbuf;
                    sb_size += SB_DELTA;
                    newbuf = (sb_buf ?
-                             realloc(sb_buf, sb_size) :
-                             malloc(sb_size));
+                             srealloc(sb_buf, sb_size) :
+                             smalloc(sb_size));
                    if (newbuf)
                        sb_buf = newbuf;
                    else
@@ -513,12 +521,10 @@ static char *telnet_init (HWND hwnd, char *host, int port, char **realhost) {
          default: return "socket(): unknown error";
        }
 
-#if 0
     {
        BOOL b = TRUE;
        setsockopt (s, SOL_SOCKET, SO_OOBINLINE, (void *)&b, sizeof(b));
     }
-#endif
 
     /*
      * Bind to local address.
@@ -546,7 +552,7 @@ static char *telnet_init (HWND hwnd, char *host, int port, char **realhost) {
          default: return "connect(): unknown error";
        }
 
-    if (WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ |
+    if (hwnd && WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ |
                        FD_WRITE | FD_OOB | FD_CLOSE) == SOCKET_ERROR)
        switch (WSAGetLastError()) {
          case WSAENETDOWN: return "Network is down";
@@ -556,6 +562,15 @@ static char *telnet_init (HWND hwnd, char *host, int port, char **realhost) {
     /*
      * Initialise option states.
      */
+    if( cfg.ldisc_term )
+    {
+       struct Opt **o;
+
+       for (o = opts; *o; o++)
+           if ((*o)->state == REQUESTED)
+               (*o)->state = INACTIVE;
+    }
+    else
     {
        struct Opt **o;
 
@@ -564,12 +579,15 @@ static char *telnet_init (HWND hwnd, char *host, int port, char **realhost) {
                send_opt ((*o)->send, (*o)->option);
     }
 
-#if 0
     /*
      * Set up SYNCH state.
      */
     in_synch = FALSE;
-#endif
+
+    /*
+     * We have no pre-session phase.
+     */
+    begin_session();
 
     return NULL;
 }
@@ -580,57 +598,59 @@ static char *telnet_init (HWND hwnd, char *host, int port, char **realhost) {
  */
 static int telnet_msg (WPARAM wParam, LPARAM lParam) {
     int ret;
-    char buf[256];
+    /* This needs to be larger than the packet size now that inbuf
+     * cannot overflow, in fact the fewer calls we make to windows
+     * the faster we will run!
+     */
+    char buf[16384];   
 
-    if (s == INVALID_SOCKET)          /* how the hell did we get here?! */
-       return -5000;
+    /*
+     * Because reading less than the whole of the available pending
+     * data can generate an FD_READ event, we need to allow for the
+     * possibility that FD_READ may arrive with FD_CLOSE already in
+     * the queue; so it's possible that we can get here even with s
+     * invalid. If so, we return 1 and don't worry about it.
+     */
+    if (s == INVALID_SOCKET)
+       return 1;
 
-    if (WSAGETSELECTERROR(lParam) != 0)
+    if (WSAGETSELECTERROR(lParam) != 0) {
+        closesocket(s);
+        s = INVALID_SOCKET;
        return -WSAGETSELECTERROR(lParam);
+    }
 
     switch (WSAGETSELECTEVENT(lParam)) {
       case FD_READ:
-       ret = recv(s, buf, sizeof(buf), 0);
-       if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
-           return 1;
-       if (ret < 0)                   /* any _other_ error */
-           return -10000-WSAGetLastError();
-       if (ret == 0) {
-           s = INVALID_SOCKET;
-           return 0;                  /* can't happen, in theory */
-       }
-#if 0
-       if (in_synch) {
-           BOOL i;
-           if (ioctlsocket (s, SIOCATMARK, &i) < 0) {
+      case FD_CLOSE:
+       {
+           int clear_of_oob = 1;
+           if (ioctlsocket (s, SIOCATMARK, &clear_of_oob) < 0 )
                return -20000-WSAGetLastError();
-           }
-           if (i)
-               in_synch = FALSE;
+
+           in_synch = !clear_of_oob;
+
+           do {
+               ret = recv(s, buf, sizeof(buf), 0);
+               if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
+                   return 1;
+               if (ret < 0) {                 /* any _other_ error */
+                    closesocket(s);
+                    s = INVALID_SOCKET;
+                   return -10000-WSAGetLastError();
+                }
+               if (ret == 0) {
+                   s = INVALID_SOCKET;
+                   return 0;
+               }
+               do_telnet_read (buf, ret);
+           } while (in_synch);
        }
-#endif
-       do_telnet_read (buf, ret);
-       return 1;
-      case FD_OOB:
-       do {
-           ret = recv(s, buf, sizeof(buf), 0);
-       } while (ret > 0);
-       telnet_state = TOPLEVEL;
-       do {
-           ret = recv(s, buf, 1, MSG_OOB);
-           if (ret > 0)
-               do_telnet_read (buf, ret);
-       } while (ret > 0);
-       if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
-           return -30000-WSAGetLastError();
        return 1;
       case FD_WRITE:
        if (outbuf_head != outbuf_reap)
            try_write();
        return 1;
-      case FD_CLOSE:
-       s = INVALID_SOCKET;
-       return 0;
     }
     return 1;                         /* shouldn't happen, but WTF */
 }
@@ -642,6 +662,7 @@ static void telnet_send (char *buf, int len) {
     char *p;
     static unsigned char iac[2] = { IAC, IAC };
     static unsigned char cr[2] = { CR, NUL };
+    static unsigned char nl[2] = { CR, LF };
 
     if (s == INVALID_SOCKET)
        return;
@@ -654,7 +675,7 @@ static void telnet_send (char *buf, int len) {
        s_write (q, p-q);
 
        while (p < buf+len && !iswritable((unsigned char)*p)) {
-           s_write ((unsigned char)*p == IAC ? iac : cr, 2);
+           s_write ((unsigned char)*p == IAC ? iac : nl, 2);
            p++;
        }
     }
@@ -677,7 +698,7 @@ static void telnet_size(void) {
     sprintf(logbuf, "client:\tSB NAWS %d,%d",
            ((unsigned char)b[3] << 8) + (unsigned char)b[4],
            ((unsigned char)b[5] << 8) + (unsigned char)b[6]);
-    lognegot (logbuf);
+    logevent (logbuf);
 }
 
 /*
@@ -705,16 +726,34 @@ static void telnet_special (Telnet_Special code) {
       case TS_EOF: b[1] = xEOF; s_write (b, 2); break;
       case TS_SYNCH:
        outbuf_head = outbuf_reap = 0;
-       b[0] = DM;
-       send (s, b, 1, MSG_OOB);
+       b[1] = DM;
+       send (s, b, 2, MSG_OOB);
+       break;
+      case TS_RECHO:
+       if (o_echo.state == INACTIVE || o_echo.state == REALLY_INACTIVE) {
+           o_echo.state = REQUESTED;
+           send_opt (o_echo.send, o_echo.option);
+       }
+       break;
+      case TS_LECHO:
+       if (o_echo.state == ACTIVE) {
+           o_echo.state = REQUESTED;
+           send_opt (o_echo.nsend, o_echo.option);
+       }
        break;
     }
 }
 
+static SOCKET telnet_socket(void) { return s; }
+
+static int telnet_sendok(void) { return 1; }
+
 Backend telnet_backend = {
     telnet_init,
     telnet_msg,
     telnet_send,
     telnet_size,
-    telnet_special
+    telnet_special,
+    telnet_socket,
+    telnet_sendok
 };